TrinityCore
Loading...
Searching...
No Matches
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 "StringFormatFwd.h"
23#include <fmt/core.h>
24
25namespace Trinity
26{
27 template<typename... Args>
28 using FormatString = fmt::format_string<Args...>;
29
30 using FormatStringView = fmt::string_view;
31
32 using FormatArgs = fmt::format_args;
33
34 template<typename... Args>
35 constexpr auto MakeFormatArgs(Args&&... args) { return fmt::make_format_args(args...); }
36
38 template<typename... Args>
39 inline std::string StringFormat(FormatString<Args...> fmt, Args&&... args)
40 {
41 try
42 {
43 return fmt::format(fmt, std::forward<Args>(args)...);
44 }
45 catch (std::exception const& formatError)
46 {
47 return fmt::format("An error occurred formatting string \"{}\" : {}", FormatStringView(fmt), formatError.what());
48 }
49 }
50
51 template<typename OutputIt, typename... Args>
52 inline OutputIt StringFormatTo(OutputIt out, FormatString<Args...> fmt, Args&&... args)
53 {
54 try
55 {
56 return fmt::format_to(out, fmt, std::forward<Args>(args)...);
57 }
58 catch (std::exception const& formatError)
59 {
60 return fmt::format_to(out, "An error occurred formatting string \"{}\" : {}", FormatStringView(fmt), formatError.what());
61 }
62 }
63
64 inline std::string StringVFormat(FormatStringView fmt, FormatArgs args)
65 {
66 try
67 {
68 return fmt::vformat(fmt, args);
69 }
70 catch (std::exception const& formatError)
71 {
72 return fmt::format("An error occurred formatting string \"{}\" : {}", fmt, formatError.what());
73 }
74 }
75
76 template<typename OutputIt>
77 inline OutputIt StringVFormatTo(OutputIt out, FormatStringView fmt, FormatArgs args)
78 {
79 try
80 {
81 return fmt::vformat_to(out, fmt, args);
82 }
83 catch (std::exception const& formatError)
84 {
85 return fmt::format_to(out, "An error occurred formatting string \"{}\" : {}", fmt, formatError.what());
86 }
87 }
88
90 inline bool IsFormatEmptyOrNull(char const* fmt)
91 {
92 return fmt == nullptr;
93 }
94
96 inline bool IsFormatEmptyOrNull(std::string const& fmt)
97 {
98 return fmt.empty();
99 }
100
102 inline constexpr bool IsFormatEmptyOrNull(std::string_view fmt)
103 {
104 return fmt.empty();
105 }
106
107 inline constexpr bool IsFormatEmptyOrNull(fmt::string_view fmt)
108 {
109 return fmt.size() == 0;
110 }
111}
112
113template<typename T, typename Char>
114struct fmt::formatter<Optional<T>, Char> : formatter<T, Char>
115{
116 template<typename FormatContext>
117 auto format(Optional<T> const& value, FormatContext& ctx) const -> decltype(ctx.out())
118 {
119 if (value.has_value())
120 return formatter<T, Char>::format(*value, ctx);
121
122 return formatter<string_view, Char>().format("(nullopt)", ctx);
123 }
124};
125
126// allow implicit enum to int conversions for formatting
127template <typename E, std::enable_if_t<std::is_enum_v<E>, std::nullptr_t> = nullptr>
128auto format_as(E e) { return std::underlying_type_t<E>(e); }
129
130#endif
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
auto format_as(E e)
fmt::format_args FormatArgs
OutputIt StringFormatTo(OutputIt out, FormatString< Args... > fmt, Args &&... args)
std::string StringVFormat(FormatStringView fmt, FormatArgs args)
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default TC string format function.
bool IsFormatEmptyOrNull(char const *fmt)
Returns true if the given char pointer is null.
fmt::format_string< Args... > FormatString
constexpr auto MakeFormatArgs(Args &&... args)
OutputIt StringVFormatTo(OutputIt out, FormatStringView fmt, FormatArgs args)
fmt::string_view FormatStringView
auto format(Optional< T > const &value, FormatContext &ctx) const -> decltype(ctx.out())