TrinityCore
StringFormat.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITYCORE_STRING_FORMAT_H
19#define TRINITYCORE_STRING_FORMAT_H
20
21#include "Optional.h"
22#include <fmt/core.h>
23
24namespace Trinity
25{
26 template<typename... Args>
27 using FormatString = fmt::format_string<Args...>;
28
29 using FormatStringView = fmt::string_view;
30
31 using FormatArgs = fmt::format_args;
32
33 template<typename... Args>
34 constexpr auto MakeFormatArgs(Args&&... args) { return fmt::make_format_args(args...); }
35
37 template<typename... Args>
38 inline std::string StringFormat(FormatString<Args...> fmt, Args&&... args)
39 {
40 try
41 {
42 return fmt::format(fmt, std::forward<Args>(args)...);
43 }
44 catch (std::exception const& formatError)
45 {
46 return fmt::format("An error occurred formatting string \"{}\" : {}", fmt, formatError.what());
47 }
48 }
49
50 template<typename OutputIt, typename... Args>
51 inline OutputIt StringFormatTo(OutputIt out, FormatString<Args...> fmt, Args&&... args)
52 {
53 try
54 {
55 return fmt::format_to(out, fmt, std::forward<Args>(args)...);
56 }
57 catch (std::exception const& formatError)
58 {
59 return fmt::format_to(out, "An error occurred formatting string \"{}\" : {}", fmt, formatError.what());
60 }
61 }
62
63 inline std::string StringVFormat(FormatStringView fmt, FormatArgs args)
64 {
65 try
66 {
67 return fmt::vformat(fmt, args);
68 }
69 catch (std::exception const& formatError)
70 {
71 return fmt::format("An error occurred formatting string \"{}\" : {}", fmt, formatError.what());
72 }
73 }
74
75 template<typename OutputIt>
76 inline OutputIt StringVFormatTo(OutputIt out, FormatStringView fmt, FormatArgs args)
77 {
78 try
79 {
80 return fmt::vformat_to(out, fmt, args);
81 }
82 catch (std::exception const& formatError)
83 {
84 return fmt::format_to(out, "An error occurred formatting string \"{}\" : {}", fmt, formatError.what());
85 }
86 }
87
89 inline bool IsFormatEmptyOrNull(char const* fmt)
90 {
91 return fmt == nullptr;
92 }
93
95 inline bool IsFormatEmptyOrNull(std::string const& fmt)
96 {
97 return fmt.empty();
98 }
99
101 inline constexpr bool IsFormatEmptyOrNull(std::string_view fmt)
102 {
103 return fmt.empty();
104 }
105
106 inline constexpr bool IsFormatEmptyOrNull(fmt::string_view fmt)
107 {
108 return fmt.size() == 0;
109 }
110}
111
112template<typename T, typename Char>
113struct fmt::formatter<Optional<T>, Char> : formatter<T, Char>
114{
115 template<typename FormatContext>
116 auto format(Optional<T> const& value, FormatContext& ctx) const -> decltype(ctx.out())
117 {
118 if (value.has_value())
119 return formatter<T, Char>::format(*value, ctx);
120
121 return formatter<std::string_view, Char>().format("(nullopt)", ctx);
122 }
123};
124
125#endif
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:25
fmt::format_args FormatArgs
Definition: StringFormat.h:31
OutputIt StringFormatTo(OutputIt out, FormatString< Args... > fmt, Args &&... args)
Definition: StringFormat.h:51
std::string StringVFormat(FormatStringView fmt, FormatArgs args)
Definition: StringFormat.h:63
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default TC string format function.
Definition: StringFormat.h:38
bool IsFormatEmptyOrNull(char const *fmt)
Returns true if the given char pointer is null.
Definition: StringFormat.h:89
fmt::format_string< Args... > FormatString
Definition: StringFormat.h:27
constexpr auto MakeFormatArgs(Args &&... args)
Definition: StringFormat.h:34
OutputIt StringVFormatTo(OutputIt out, FormatStringView fmt, FormatArgs args)
Definition: StringFormat.h:76
fmt::string_view FormatStringView
Definition: StringFormat.h:29
auto format(Optional< T > const &value, FormatContext &ctx) const -> decltype(ctx.out())
Definition: StringFormat.h:116