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 "Define.h"
22#include "Optional.h"
23#include "StringFormatFwd.h"
24#include <fmt/core.h>
25
26namespace Trinity
27{
28 template<typename... Args>
29 using FormatString = fmt::format_string<Args...>;
30
31 using FormatStringView = fmt::string_view;
32
33 using FormatArgs = fmt::format_args;
34
35 template<typename... Args>
36 constexpr auto MakeFormatArgs(Args&&... args) { return fmt::make_format_args(args...); }
37
38 namespace Impl
39 {
40 TC_COMMON_API std::string StringVFormat(FormatStringView fmt, FormatArgs args) noexcept;
41
42 TC_COMMON_API void StringVFormatToImpl(fmt::detail::buffer<char>& buffer, FormatStringView fmt, FormatArgs args) noexcept;
43 }
44
46
47 template<typename OutputIt>
48 inline OutputIt StringVFormatTo(OutputIt out, FormatStringView fmt, FormatArgs args) noexcept
49 {
50 auto&& buf = fmt::detail::get_buffer<char>(out);
52 return fmt::detail::get_iterator(buf, out);
53 }
54
56 template<typename... Args>
57 inline std::string StringFormat(FormatString<Args...> fmt, Args&&... args) noexcept
58 {
59 return Trinity::StringVFormat(fmt, Trinity::MakeFormatArgs(std::forward<Args>(args)...));
60 }
61
62 template<typename OutputIt, typename... Args>
63 inline OutputIt StringFormatTo(OutputIt out, FormatString<Args...> fmt, Args&&... args) noexcept
64 {
65 return Trinity::StringVFormatTo(out, fmt, Trinity::MakeFormatArgs(std::forward<Args>(args)...));
66 }
67
69 inline bool IsFormatEmptyOrNull(char const* fmt)
70 {
71 return fmt == nullptr;
72 }
73
75 inline bool IsFormatEmptyOrNull(std::string const& fmt)
76 {
77 return fmt.empty();
78 }
79
81 inline constexpr bool IsFormatEmptyOrNull(std::string_view fmt)
82 {
83 return fmt.empty();
84 }
85
86 inline constexpr bool IsFormatEmptyOrNull(fmt::string_view fmt)
87 {
88 return fmt.size() == 0;
89 }
90}
91
92template<typename T, typename Char>
93struct fmt::formatter<Optional<T>, Char> : formatter<T, Char>
94{
95 template<typename FormatContext>
96 auto format(Optional<T> const& value, FormatContext& ctx) const -> decltype(ctx.out())
97 {
98 if (value.has_value())
99 return formatter<T, Char>::format(*value, ctx);
100
101 return formatter<string_view, Char>().format("(nullopt)", ctx);
102 }
103};
104
105// allow implicit enum to int conversions for formatting
106template <typename E, std::enable_if_t<std::is_enum_v<E>, std::nullptr_t> = nullptr>
107inline constexpr auto format_as(E e) { return static_cast<std::underlying_type_t<E>>(e); }
108
109#endif
#define TC_COMMON_API
Definition Define.h:99
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
constexpr auto format_as(E e)
void StringVFormatToImpl(fmt::detail::buffer< char > &buffer, FormatStringView fmt, FormatArgs args) noexcept
std::string StringVFormat(FormatStringView fmt, FormatArgs args) noexcept
OutputIt StringVFormatTo(OutputIt out, FormatStringView fmt, FormatArgs args) noexcept
fmt::format_args FormatArgs
OutputIt StringFormatTo(OutputIt out, FormatString< Args... > fmt, Args &&... args) noexcept
std::string StringFormat(FormatString< Args... > fmt, Args &&... args) noexcept
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)
fmt::string_view FormatStringView
auto format(Optional< T > const &value, FormatContext &ctx) const -> decltype(ctx.out())