WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
xchar.h
Go to the documentation of this file.
1// Formatting library for C++ - optional wchar_t and exotic character support
2//
3// Copyright (c) 2012 - present, Victor Zverovich
4// All rights reserved.
5//
6// For the license information refer to format.h.
7
8#ifndef FMT_XCHAR_H_
9#define FMT_XCHAR_H_
10
11#include "color.h"
12#include "format.h"
13#include "ostream.h"
14#include "ranges.h"
15
16#ifndef FMT_MODULE
17# include <cwchar>
18# if FMT_USE_LOCALE
19# include <locale>
20# endif
21#endif
22
24namespace detail {
25
26template <typename T>
28
29template <typename S, typename = void> struct format_string_char {};
30
31template <typename S>
33 S, void_t<decltype(sizeof(detail::to_string_view(std::declval<S>())))>> {
34 using type = char_t<S>;
35};
36
37template <typename S>
39 S, enable_if_t<std::is_base_of<detail::compile_string, S>::value>> {
40 using type = typename S::char_type;
41};
42
43template <typename S>
45
47 const format_specs& specs, locale_ref loc) -> bool {
48#if FMT_USE_LOCALE
49 auto& numpunct =
50 std::use_facet<std::numpunct<wchar_t>>(loc.get<std::locale>());
51 auto separator = std::wstring();
52 auto grouping = numpunct.grouping();
53 if (!grouping.empty()) separator = std::wstring(1, numpunct.thousands_sep());
54 return value.visit(loc_writer<wchar_t>{out, specs, separator, grouping, {}});
55#endif
56 return false;
57}
58} // namespace detail
59
61
67
68template <typename Char, typename... T> struct basic_fstring {
69 private:
71
72 static constexpr int num_static_named_args =
74
76 Char, static_cast<int>(sizeof...(T)), num_static_named_args,
77 num_static_named_args != detail::count_named_args<T...>()>;
78
79 using arg_pack = detail::arg_pack<T...>;
80
81 public:
83
84 template <typename S,
86 std::is_convertible<const S&, basic_string_view<Char>>::value)>
91 template <typename S,
92 FMT_ENABLE_IF(std::is_base_of<detail::compile_string, S>::value&&
93 std::is_same<typename S::char_type, Char>::value)>
94 FMT_ALWAYS_INLINE basic_fstring(const S&) : str_(S()) {
96 FMT_CONSTEXPR int ignore =
97 (parse_format_string(sv, checker(sv, arg_pack())), 0);
99 }
101
102 operator basic_string_view<Char>() const { return str_; }
103 auto get() const -> basic_string_view<Char> { return str_; }
104};
105
106template <typename Char, typename... T>
108
109template <typename... T>
110using wformat_string = typename basic_format_string<wchar_t, T...>::t;
112 return {{s}};
113}
114
115template <> struct is_char<wchar_t> : std::true_type {};
116template <> struct is_char<char16_t> : std::true_type {};
117template <> struct is_char<char32_t> : std::true_type {};
118
119#ifdef __cpp_char8_t
120template <> struct is_char<char8_t> : bool_constant<detail::is_utf8_enabled> {};
121#endif
122
123template <typename... T>
124constexpr auto make_wformat_args(T&... args)
125 -> decltype(fmt::make_format_args<wformat_context>(args...)) {
126 return fmt::make_format_args<wformat_context>(args...);
127}
128
129#if !FMT_USE_NONTYPE_TEMPLATE_ARGS
130inline namespace literals {
131inline auto operator""_a(const wchar_t* s, size_t) -> detail::udl_arg<wchar_t> {
132 return {s};
133}
134} // namespace literals
135#endif
136
137template <typename It, typename Sentinel>
138auto join(It begin, Sentinel end, wstring_view sep)
140 return {begin, end, sep};
141}
142
143template <typename Range, FMT_ENABLE_IF(!is_tuple_like<Range>::value)>
144auto join(Range&& range, wstring_view sep)
145 -> join_view<decltype(std::begin(range)), decltype(std::end(range)),
146 wchar_t> {
147 return join(std::begin(range), std::end(range), sep);
148}
149
150template <typename T>
151auto join(std::initializer_list<T> list, wstring_view sep)
153 return join(std::begin(list), std::end(list), sep);
154}
155
156template <typename Tuple, FMT_ENABLE_IF(is_tuple_like<Tuple>::value)>
157auto join(const Tuple& tuple, basic_string_view<wchar_t> sep)
159 return {tuple, sep};
160}
161
162template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
165 -> std::basic_string<Char> {
166 auto buf = basic_memory_buffer<Char>();
167 detail::vformat_to(buf, fmt, args);
168 return {buf.data(), buf.size()};
169}
170
171template <typename... T>
172auto format(wformat_string<T...> fmt, T&&... args) -> std::wstring {
173 return vformat(fmt::wstring_view(fmt), fmt::make_wformat_args(args...));
174}
175
176template <typename OutputIt, typename... T>
177auto format_to(OutputIt out, wformat_string<T...> fmt, T&&... args)
178 -> OutputIt {
179 return vformat_to(out, fmt::wstring_view(fmt),
180 fmt::make_wformat_args(args...));
181}
182
183// Pass char_t as a default template parameter instead of using
184// std::basic_string<char_t<S>> to reduce the symbol size.
185template <typename S, typename... T,
186 typename Char = detail::format_string_char_t<S>,
187 FMT_ENABLE_IF(!std::is_same<Char, char>::value &&
188 !std::is_same<Char, wchar_t>::value)>
189auto format(const S& fmt, T&&... args) -> std::basic_string<Char> {
191 fmt::make_format_args<buffered_context<Char>>(args...));
192}
193
194template <typename S, typename Char = detail::format_string_char_t<S>,
195 FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
196inline auto vformat(detail::locale_ref loc, const S& fmt,
198 -> std::basic_string<Char> {
199 auto buf = basic_memory_buffer<Char>();
201 detail::locale_ref(loc));
202 return {buf.data(), buf.size()};
203}
204
205template <typename S, typename... T,
206 typename Char = detail::format_string_char_t<S>,
208inline auto format(detail::locale_ref loc, const S& fmt, T&&... args)
209 -> std::basic_string<Char> {
210 return vformat(loc, detail::to_string_view(fmt),
211 fmt::make_format_args<buffered_context<Char>>(args...));
212}
213
214template <typename OutputIt, typename S,
215 typename Char = detail::format_string_char_t<S>,
218auto vformat_to(OutputIt out, const S& fmt,
219 typename detail::vformat_args<Char>::type args) -> OutputIt {
220 auto&& buf = detail::get_buffer<Char>(out);
222 return detail::get_iterator(buf, out);
223}
224
225template <typename OutputIt, typename S, typename... T,
226 typename Char = detail::format_string_char_t<S>,
228 !std::is_same<Char, char>::value &&
229 !std::is_same<Char, wchar_t>::value)>
230inline auto format_to(OutputIt out, const S& fmt, T&&... args) -> OutputIt {
231 return vformat_to(out, detail::to_string_view(fmt),
232 fmt::make_format_args<buffered_context<Char>>(args...));
233}
234
235template <typename S, typename OutputIt, typename... Args,
236 typename Char = detail::format_string_char_t<S>,
239inline auto vformat_to(OutputIt out, detail::locale_ref loc, const S& fmt,
241 -> OutputIt {
242 auto&& buf = detail::get_buffer<Char>(out);
244 return detail::get_iterator(buf, out);
245}
246
247template <typename OutputIt, typename S, typename... T,
248 typename Char = detail::format_string_char_t<S>,
251inline auto format_to(OutputIt out, detail::locale_ref loc, const S& fmt,
252 T&&... args) ->
253 typename std::enable_if<enable, OutputIt>::type {
254 return vformat_to(out, loc, detail::to_string_view(fmt),
255 fmt::make_format_args<buffered_context<Char>>(args...));
256}
257
258template <typename OutputIt, typename Char, typename... Args,
261inline auto vformat_to_n(OutputIt out, size_t n, basic_string_view<Char> fmt,
264 using traits = detail::fixed_buffer_traits;
266 detail::vformat_to(buf, fmt, args);
267 return {buf.out(), buf.count()};
268}
269
270template <typename OutputIt, typename S, typename... T,
271 typename Char = detail::format_string_char_t<S>,
274inline auto format_to_n(OutputIt out, size_t n, const S& fmt, T&&... args)
276 return vformat_to_n(out, n, fmt::basic_string_view<Char>(fmt),
277 fmt::make_format_args<buffered_context<Char>>(args...));
278}
279
280template <typename S, typename... T,
281 typename Char = detail::format_string_char_t<S>,
283inline auto formatted_size(const S& fmt, T&&... args) -> size_t {
286 fmt::make_format_args<buffered_context<Char>>(args...));
287 return buf.count();
288}
289
290inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) {
291 auto buf = wmemory_buffer();
292 detail::vformat_to(buf, fmt, args);
293 buf.push_back(L'\0');
294 if (std::fputws(buf.data(), f) == -1)
295 FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
296}
297
298inline void vprint(wstring_view fmt, wformat_args args) {
299 vprint(stdout, fmt, args);
300}
301
302template <typename... T>
303void print(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
304 return vprint(f, wstring_view(fmt), fmt::make_wformat_args(args...));
305}
306
307template <typename... T> void print(wformat_string<T...> fmt, T&&... args) {
308 return vprint(wstring_view(fmt), fmt::make_wformat_args(args...));
309}
310
311template <typename... T>
312void println(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
313 return print(f, L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
314}
315
316template <typename... T> void println(wformat_string<T...> fmt, T&&... args) {
317 return print(L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
318}
319
320inline auto vformat(const text_style& ts, wstring_view fmt, wformat_args args)
321 -> std::wstring {
322 auto buf = wmemory_buffer();
323 detail::vformat_to(buf, ts, fmt, args);
324 return {buf.data(), buf.size()};
325}
326
327template <typename... T>
328inline auto format(const text_style& ts, wformat_string<T...> fmt, T&&... args)
329 -> std::wstring {
330 return fmt::vformat(ts, fmt, fmt::make_wformat_args(args...));
331}
332
333template <typename... T>
334FMT_DEPRECATED void print(std::FILE* f, const text_style& ts,
335 wformat_string<T...> fmt, const T&... args) {
336 vprint(f, ts, fmt, fmt::make_wformat_args(args...));
337}
338
339template <typename... T>
341 const T&... args) {
342 return print(stdout, ts, fmt, args...);
343}
344
345inline void vprint(std::wostream& os, wstring_view fmt, wformat_args args) {
346 auto buffer = basic_memory_buffer<wchar_t>();
347 detail::vformat_to(buffer, fmt, args);
348 detail::write_buffer(os, buffer);
349}
350
351template <typename... T>
352void print(std::wostream& os, wformat_string<T...> fmt, T&&... args) {
353 vprint(os, fmt, fmt::make_format_args<buffered_context<wchar_t>>(args...));
354}
355
356template <typename... T>
357void println(std::wostream& os, wformat_string<T...> fmt, T&&... args) {
358 print(os, L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
359}
360
361/// Converts `value` to `std::wstring` using the default format for type `T`.
362template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
363 return format(FMT_STRING(L"{}"), value);
364}
367
368#endif // FMT_XCHAR_H_
Definition base.h:2408
A view of a collection of formatting arguments.
Definition base.h:2509
An implementation of std::basic_string_view for pre-C++17.
Definition base.h:504
Definition base.h:1955
Definition base.h:1809
Definition base.h:1641
Definition base.h:1826
Definition base.h:2081
Definition format.h:3720
Parsing context consisting of a format string range being parsed and an argument counter for automati...
Definition base.h:845
A text style consisting of foreground and background colors and emphasis.
Definition color.h:231
auto system_error(int error_code, format_string< T... > fmt, T &&... args) -> std::system_error
Constructs std::system_error with a message formatted with fmt::format(fmt, args.....
Definition format.h:4113
#define FMT_STRING(s)
Constructs a legacy compile-time format string from a string literal s.
Definition format.h:4092
#define FMT_THROW(x)
Definition format.h:149
detail namespace with internal helper functions
Definition input_adapters.h:32
auto get_iterator(Buf &buf, OutputIt) -> decltype(buf.out())
Definition base.h:2044
FMT_CONSTEXPR void ignore_unused(const T &...)
Definition base.h:348
constexpr auto count_static_named_args() -> int
Definition base.h:1036
constexpr auto count_named_args() -> int
Definition base.h:1033
typename std::enable_if< B, T >::type enable_if_t
Definition cpp_future.h:38
@ value
the parser finished reading a JSON value
typename format_string_char< S >::type format_string_char_t
Definition xchar.h:44
auto write_loc(OutputIt, const loc_value &, const format_specs &, locale_ref) -> bool
Definition format.h:1958
bool_constant<!std::is_same< T, char >::value > is_exotic_char
Definition xchar.h:27
FMT_CONSTEXPR void parse_format_string(basic_string_view< Char > fmt, Handler &&handler)
Definition base.h:1591
typename make_void< Ts... >::type void_t
Definition void_t.h:21
void write_buffer(std::basic_ostream< Char > &os, buffer< Char > &buf)
Definition ostream.h:55
typename V::value_type char_t
String's character (code unit) type. detail:: is intentional to prevent ADL.
Definition base.h:935
constexpr auto to_string_view(const Char *s) -> basic_string_view< Char >
Definition base.h:910
std::integral_constant< bool, Value > bool_constant
Definition type_traits.h:743
auto get_buffer(OutputIt out) -> iterator_buffer< OutputIt, T >
Definition base.h:2034
void vformat_to(buffer< Char > &buf, basic_string_view< Char > fmt, typename vformat_args< Char >::type args, locale_ref loc={})
Definition format.h:3665
Definition json.h:5183
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
Definition xchar.h:68
FMT_CONSTEVAL FMT_ALWAYS_INLINE basic_fstring(const S &s)
Definition xchar.h:87
basic_fstring(runtime_format_string< Char > fmt)
Definition xchar.h:100
FMT_ALWAYS_INLINE basic_fstring(const S &)
Definition xchar.h:94
auto get() const -> basic_string_view< Char >
Definition xchar.h:103
Definition base.h:1638
Definition xchar.h:29
Definition base.h:2237
Definition format.h:1984
Definition base.h:2252
Definition format-inl.h:93
auto grouping() const -> std::string
Definition format-inl.h:94
auto thousands_sep() const -> Char
Definition format-inl.h:95
Definition format.h:3580
Definition base.h:834
Definition base.h:2808
Definition base.h:615
Definition ranges.h:621
Definition base.h:2648
Definition ranges.h:671
#define S(label, offset, message)
Definition Errors.h:113
#define FMT_END_EXPORT
Definition base.h:250
std::integral_constant< bool, B > bool_constant
Definition base.h:300
#define FMT_CONSTEVAL
Definition base.h:140
#define FMT_CONSTEXPR
Definition base.h:113
#define FMT_ALWAYS_INLINE
Definition base.h:203
#define FMT_BEGIN_NAMESPACE
Definition base.h:239
#define FMT_USE_CONSTEVAL
Definition base.h:118
#define FMT_ENABLE_IF(...)
Definition base.h:334
#define FMT_BEGIN_EXPORT
Definition base.h:249
std::is_constructible< formatter< T, Char > > FMT_DEPRECATED
Definition base.h:2732
#define FMT_END_NAMESPACE
Definition base.h:242
conditional_t< std::is_same< Char, char >::value, context, generic_context< basic_appender< Char >, Char > > buffered_context
Definition base.h:638
auto format(wformat_string< T... > fmt, T &&... args) -> std::wstring
Definition xchar.h:172
auto vformat_to_n(OutputIt out, size_t n, basic_string_view< Char > fmt, typename detail::vformat_args< Char >::type args) -> format_to_n_result< OutputIt >
Definition xchar.h:261
buffered_context< wchar_t > wformat_context
Definition xchar.h:64
auto formatted_size(const S &fmt, T &&... args) -> size_t
Definition xchar.h:283
auto vformat_to(OutputIt out, const S &fmt, typename detail::vformat_args< Char >::type args) -> OutputIt
Definition xchar.h:218
constexpr auto make_wformat_args(T &... args) -> decltype(fmt::make_format_args< wformat_context >(args...))
Definition xchar.h:124
basic_string_view< wchar_t > wstring_view
Definition xchar.h:62
auto format_to_n(OutputIt out, size_t n, const S &fmt, T &&... args) -> format_to_n_result< OutputIt >
Definition xchar.h:274
basic_memory_buffer< wchar_t > wmemory_buffer
Definition xchar.h:66
typename basic_format_string< wchar_t, T... >::t wformat_string
Definition xchar.h:110
auto format_to(OutputIt out, wformat_string< T... > fmt, T &&... args) -> OutputIt
Definition xchar.h:177
auto to_wstring(const T &value) -> std::wstring
Converts value to std::wstring using the default format for type T.
Definition xchar.h:362
auto join(It begin, Sentinel end, wstring_view sep) -> join_view< It, Sentinel, wchar_t >
Definition xchar.h:138
void print(std::FILE *f, wformat_string< T... > fmt, T &&... args)
Definition xchar.h:303
auto runtime(wstring_view s) -> runtime_format_string< wchar_t >
Definition xchar.h:111
void vprint(std::FILE *f, wstring_view fmt, wformat_args args)
Definition xchar.h:290
auto vformat(basic_string_view< Char > fmt, typename detail::vformat_args< Char >::type args) -> std::basic_string< Char >
Definition xchar.h:163
void println(std::FILE *f, wformat_string< T... > fmt, T &&... args)
Definition xchar.h:312