WPILibC++ 2027.0.0-alpha-2
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
115#ifdef __cpp_char8_t
116template <> struct is_char<char8_t> : bool_constant<detail::is_utf8_enabled> {};
117#endif
118
119template <typename... T>
120constexpr auto make_wformat_args(T&... args)
121 -> decltype(fmt::make_format_args<wformat_context>(args...)) {
122 return fmt::make_format_args<wformat_context>(args...);
123}
124
125#if !FMT_USE_NONTYPE_TEMPLATE_ARGS
126inline namespace literals {
127inline auto operator""_a(const wchar_t* s, size_t) -> detail::udl_arg<wchar_t> {
128 return {s};
129}
130} // namespace literals
131#endif
132
133template <typename It, typename Sentinel>
134auto join(It begin, Sentinel end, wstring_view sep)
136 return {begin, end, sep};
137}
138
139template <typename Range, FMT_ENABLE_IF(!is_tuple_like<Range>::value)>
140auto join(Range&& range, wstring_view sep)
141 -> join_view<decltype(std::begin(range)), decltype(std::end(range)),
142 wchar_t> {
143 return join(std::begin(range), std::end(range), sep);
144}
145
146template <typename T>
147auto join(std::initializer_list<T> list, wstring_view sep)
149 return join(std::begin(list), std::end(list), sep);
150}
151
152template <typename Tuple, FMT_ENABLE_IF(is_tuple_like<Tuple>::value)>
153auto join(const Tuple& tuple, basic_string_view<wchar_t> sep)
155 return {tuple, sep};
156}
157
158template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
161 -> std::basic_string<Char> {
162 auto buf = basic_memory_buffer<Char>();
163 detail::vformat_to(buf, fmt, args);
164 return {buf.data(), buf.size()};
165}
166
167template <typename... T>
168auto format(wformat_string<T...> fmt, T&&... args) -> std::wstring {
169 return vformat(fmt::wstring_view(fmt), fmt::make_wformat_args(args...));
170}
171
172template <typename OutputIt, typename... T>
173auto format_to(OutputIt out, wformat_string<T...> fmt, T&&... args)
174 -> OutputIt {
175 return vformat_to(out, fmt::wstring_view(fmt),
176 fmt::make_wformat_args(args...));
177}
178
179// Pass char_t as a default template parameter instead of using
180// std::basic_string<char_t<S>> to reduce the symbol size.
181template <typename S, typename... T,
182 typename Char = detail::format_string_char_t<S>,
183 FMT_ENABLE_IF(!std::is_same<Char, char>::value &&
184 !std::is_same<Char, wchar_t>::value)>
185auto format(const S& fmt, T&&... args) -> std::basic_string<Char> {
187 fmt::make_format_args<buffered_context<Char>>(args...));
188}
189
190template <typename Locale, typename S,
191 typename Char = detail::format_string_char_t<S>,
194inline auto vformat(const Locale& loc, const S& fmt,
196 -> std::basic_string<Char> {
197 auto buf = basic_memory_buffer<Char>();
199 detail::locale_ref(loc));
200 return {buf.data(), buf.size()};
201}
202
203template <typename Locale, typename S, typename... T,
204 typename Char = detail::format_string_char_t<S>,
207inline auto format(const Locale& loc, const S& fmt, T&&... args)
208 -> std::basic_string<Char> {
209 return vformat(loc, detail::to_string_view(fmt),
210 fmt::make_format_args<buffered_context<Char>>(args...));
211}
212
213template <typename OutputIt, typename S,
214 typename Char = detail::format_string_char_t<S>,
217auto vformat_to(OutputIt out, const S& fmt,
218 typename detail::vformat_args<Char>::type args) -> OutputIt {
219 auto&& buf = detail::get_buffer<Char>(out);
221 return detail::get_iterator(buf, out);
222}
223
224template <typename OutputIt, typename S, typename... T,
225 typename Char = detail::format_string_char_t<S>,
227 !std::is_same<Char, char>::value &&
228 !std::is_same<Char, wchar_t>::value)>
229inline auto format_to(OutputIt out, const S& fmt, T&&... args) -> OutputIt {
230 return vformat_to(out, detail::to_string_view(fmt),
231 fmt::make_format_args<buffered_context<Char>>(args...));
232}
233
234template <typename Locale, typename S, typename OutputIt, typename... Args,
235 typename Char = detail::format_string_char_t<S>,
239inline auto vformat_to(OutputIt out, const Locale& 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 Locale, typename OutputIt, typename S, typename... T,
248 typename Char = detail::format_string_char_t<S>,
252inline auto format_to(OutputIt out, const Locale& loc, const S& fmt,
253 T&&... args) ->
254 typename std::enable_if<enable, OutputIt>::type {
255 return vformat_to(out, loc, detail::to_string_view(fmt),
256 fmt::make_format_args<buffered_context<Char>>(args...));
257}
258
259template <typename OutputIt, typename Char, typename... Args,
262inline auto vformat_to_n(OutputIt out, size_t n, basic_string_view<Char> fmt,
265 using traits = detail::fixed_buffer_traits;
267 detail::vformat_to(buf, fmt, args);
268 return {buf.out(), buf.count()};
269}
270
271template <typename OutputIt, typename S, typename... T,
272 typename Char = detail::format_string_char_t<S>,
275inline auto format_to_n(OutputIt out, size_t n, const S& fmt, T&&... args)
277 return vformat_to_n(out, n, fmt::basic_string_view<Char>(fmt),
278 fmt::make_format_args<buffered_context<Char>>(args...));
279}
280
281template <typename S, typename... T,
282 typename Char = detail::format_string_char_t<S>,
284inline auto formatted_size(const S& fmt, T&&... args) -> size_t {
287 fmt::make_format_args<buffered_context<Char>>(args...));
288 return buf.count();
289}
290
291inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) {
292 auto buf = wmemory_buffer();
293 detail::vformat_to(buf, fmt, args);
294 buf.push_back(L'\0');
295 if (std::fputws(buf.data(), f) == -1)
296 FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
297}
298
299inline void vprint(wstring_view fmt, wformat_args args) {
300 vprint(stdout, fmt, args);
301}
302
303template <typename... T>
304void print(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
305 return vprint(f, wstring_view(fmt), fmt::make_wformat_args(args...));
306}
307
308template <typename... T> void print(wformat_string<T...> fmt, T&&... args) {
309 return vprint(wstring_view(fmt), fmt::make_wformat_args(args...));
310}
311
312template <typename... T>
313void println(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
314 return print(f, L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
315}
316
317template <typename... T> void println(wformat_string<T...> fmt, T&&... args) {
318 return print(L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
319}
320
321inline auto vformat(text_style ts, wstring_view fmt, wformat_args args)
322 -> std::wstring {
323 auto buf = wmemory_buffer();
324 detail::vformat_to(buf, ts, fmt, args);
325 return {buf.data(), buf.size()};
326}
327
328template <typename... T>
329inline auto format(text_style ts, wformat_string<T...> fmt, T&&... args)
330 -> std::wstring {
331 return fmt::vformat(ts, fmt, fmt::make_wformat_args(args...));
332}
333
334template <typename... T>
336 const T&... args) {
337 vprint(f, ts, fmt, fmt::make_wformat_args(args...));
338}
339
340template <typename... T>
342 const T&... args) {
343 return print(stdout, ts, fmt, args...);
344}
345
346inline void vprint(std::wostream& os, wstring_view fmt, wformat_args args) {
347 auto buffer = basic_memory_buffer<wchar_t>();
348 detail::vformat_to(buffer, fmt, args);
349 detail::write_buffer(os, buffer);
350}
351
352template <typename... T>
353void print(std::wostream& os, wformat_string<T...> fmt, T&&... args) {
354 vprint(os, fmt, fmt::make_format_args<buffered_context<wchar_t>>(args...));
355}
356
357template <typename... T>
358void println(std::wostream& os, wformat_string<T...> fmt, T&&... args) {
359 print(os, L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
360}
361
362/// Converts `value` to `std::wstring` using the default format for type `T`.
363template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
364 return format(FMT_STRING(L"{}"), value);
365}
368
369#endif // FMT_XCHAR_H_
Definition base.h:2474
A view of a collection of formatting arguments.
Definition base.h:2575
An implementation of std::basic_string_view for pre-C++17.
Definition base.h:518
Definition base.h:2003
Definition base.h:1857
Definition base.h:1679
Definition base.h:1874
Definition base.h:2318
Definition base.h:2146
Definition format.h:3824
Parsing context consisting of a format string range being parsed and an argument counter for automati...
Definition base.h:866
A text style consisting of foreground and background colors and emphasis.
Definition color.h:236
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:4217
#define FMT_STRING(s)
Constructs a legacy compile-time format string from a string literal s.
Definition format.h:4196
#define FMT_THROW(x)
Definition format.h:178
detail namespace with internal helper functions
Definition input_adapters.h:32
auto get_iterator(Buf &buf, OutputIt) -> decltype(buf.out())
Definition base.h:2109
FMT_CONSTEXPR void ignore_unused(const T &...)
Definition base.h:363
constexpr auto count_static_named_args() -> int
Definition base.h:1062
constexpr auto count_named_args() -> int
Definition base.h:1059
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:1951
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:1629
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:956
constexpr auto to_string_view(const Char *s) -> basic_string_view< Char >
Definition base.h:931
std::integral_constant< bool, Value > bool_constant
Definition type_traits.h:743
auto get_buffer(OutputIt out) -> iterator_buffer< OutputIt, T >
Definition base.h:2099
void vformat_to(buffer< Char > &buf, basic_string_view< Char > fmt, typename vformat_args< Char >::type args, locale_ref loc={})
Definition format.h:3769
Definition json.h:5183
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:1676
Definition xchar.h:29
Definition format.h:3756
Definition base.h:2303
Definition format.h:1976
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:3678
Definition base.h:855
Definition base.h:2877
Definition base.h:628
Definition ranges.h:624
Definition base.h:2716
Definition ranges.h:675
#define S(label, offset, message)
Definition Errors.h:113
#define FMT_END_EXPORT
Definition base.h:272
std::integral_constant< bool, B > bool_constant
Definition base.h:315
#define FMT_CONSTEVAL
Definition base.h:140
#define FMT_CONSTEXPR
Definition base.h:113
#define FMT_ALWAYS_INLINE
Definition base.h:251
#define FMT_BEGIN_NAMESPACE
Definition base.h:261
#define FMT_USE_CONSTEVAL
Definition base.h:118
#define FMT_ENABLE_IF(...)
Definition base.h:349
#define FMT_BEGIN_EXPORT
Definition base.h:271
std::is_constructible< formatter< T, Char > > FMT_DEPRECATED
Definition base.h:2801
#define FMT_END_NAMESPACE
Definition base.h:264
conditional_t< std::is_same< Char, char >::value, context, generic_context< basic_appender< Char >, Char > > buffered_context
Definition base.h:651
auto format(wformat_string< T... > fmt, T &&... args) -> std::wstring
Definition xchar.h:168
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:262
buffered_context< wchar_t > wformat_context
Definition xchar.h:64
auto formatted_size(const S &fmt, T &&... args) -> size_t
Definition xchar.h:284
auto vformat_to(OutputIt out, const S &fmt, typename detail::vformat_args< Char >::type args) -> OutputIt
Definition xchar.h:217
constexpr auto make_wformat_args(T &... args) -> decltype(fmt::make_format_args< wformat_context >(args...))
Definition xchar.h:120
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:275
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:173
auto to_wstring(const T &value) -> std::wstring
Converts value to std::wstring using the default format for type T.
Definition xchar.h:363
auto join(It begin, Sentinel end, wstring_view sep) -> join_view< It, Sentinel, wchar_t >
Definition xchar.h:134
void print(std::FILE *f, wformat_string< T... > fmt, T &&... args)
Definition xchar.h:304
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:291
auto vformat(basic_string_view< Char > fmt, typename detail::vformat_args< Char >::type args) -> std::basic_string< Char >
Definition xchar.h:159
void println(std::FILE *f, wformat_string< T... > fmt, T &&... args)
Definition xchar.h:313