WPILibC++ 2027.0.0-alpha-3
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
59template <typename Char>
62 locale_ref loc = {}) {
63 static_assert(!std::is_same<Char, char>::value, "");
64 auto out = basic_appender<Char>(buf);
66 fmt, format_handler<Char>{parse_context<Char>(fmt), {out, args, loc}});
67}
68} // namespace detail
69
71
77
78template <typename Char, typename... T> struct basic_fstring {
79 private:
81
82 static constexpr int num_static_named_args =
84
86 Char, static_cast<int>(sizeof...(T)), num_static_named_args,
87 num_static_named_args != detail::count_named_args<T...>()>;
88
89 using arg_pack = detail::arg_pack<T...>;
90
91 public:
93
94 template <typename S,
96 std::is_convertible<const S&, basic_string_view<Char>>::value)>
101 template <typename S,
102 FMT_ENABLE_IF(std::is_base_of<detail::compile_string, S>::value&&
103 std::is_same<typename S::char_type, Char>::value)>
104 FMT_ALWAYS_INLINE basic_fstring(const S&) : str_(S()) {
106 FMT_CONSTEXPR int ignore =
107 (parse_format_string(sv, checker(sv, arg_pack())), 0);
108 detail::ignore_unused(ignore);
109 }
111
112 operator basic_string_view<Char>() const { return str_; }
113 auto get() const -> basic_string_view<Char> { return str_; }
114};
115
116template <typename Char, typename... T>
118
119template <typename... T>
120using wformat_string = typename basic_format_string<wchar_t, T...>::t;
122 return {{s}};
123}
124
125template <typename... T>
126constexpr auto make_wformat_args(T&... args)
127 -> decltype(fmt::make_format_args<wformat_context>(args...)) {
128 return fmt::make_format_args<wformat_context>(args...);
129}
130
131#if !FMT_USE_NONTYPE_TEMPLATE_ARGS
132inline namespace literals {
133inline auto operator""_a(const wchar_t* s, size_t) -> detail::udl_arg<wchar_t> {
134 return {s};
135}
136} // namespace literals
137#endif
138
139template <typename It, typename Sentinel>
140auto join(It begin, Sentinel end, wstring_view sep)
142 return {begin, end, sep};
143}
144
145template <typename Range, FMT_ENABLE_IF(!is_tuple_like<Range>::value)>
146auto join(Range&& range, wstring_view sep)
147 -> join_view<decltype(std::begin(range)), decltype(std::end(range)),
148 wchar_t> {
149 return join(std::begin(range), std::end(range), sep);
150}
151
152template <typename T>
153auto join(std::initializer_list<T> list, wstring_view sep)
155 return join(std::begin(list), std::end(list), sep);
156}
157
158template <typename Tuple, FMT_ENABLE_IF(is_tuple_like<Tuple>::value)>
159auto join(const Tuple& tuple, basic_string_view<wchar_t> sep)
161 return {tuple, sep};
162}
163
164template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
167 -> std::basic_string<Char> {
168 auto buf = basic_memory_buffer<Char>();
169 detail::vformat_to(buf, fmt, args);
170 return {buf.data(), buf.size()};
171}
172
173template <typename... T>
174auto format(wformat_string<T...> fmt, T&&... args) -> std::wstring {
175 return vformat(fmt::wstring_view(fmt), fmt::make_wformat_args(args...));
176}
177
178template <typename OutputIt, typename... T>
179auto format_to(OutputIt out, wformat_string<T...> fmt, T&&... args)
180 -> OutputIt {
181 return vformat_to(out, fmt::wstring_view(fmt),
182 fmt::make_wformat_args(args...));
183}
184
185// Pass char_t as a default template parameter instead of using
186// std::basic_string<char_t<S>> to reduce the symbol size.
187template <typename S, typename... T,
188 typename Char = detail::format_string_char_t<S>,
189 FMT_ENABLE_IF(!std::is_same<Char, char>::value &&
190 !std::is_same<Char, wchar_t>::value)>
191auto format(const S& fmt, T&&... args) -> std::basic_string<Char> {
193 fmt::make_format_args<buffered_context<Char>>(args...));
194}
195
196template <typename S, typename Char = detail::format_string_char_t<S>,
197 FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
198inline auto vformat(locale_ref loc, const S& fmt,
200 -> std::basic_string<Char> {
201 auto buf = basic_memory_buffer<Char>();
202 detail::vformat_to(buf, detail::to_string_view(fmt), args, loc);
203 return {buf.data(), buf.size()};
204}
205
206template <typename S, typename... T,
207 typename Char = detail::format_string_char_t<S>,
209inline auto format(locale_ref loc, const S& fmt, T&&... args)
210 -> std::basic_string<Char> {
211 return vformat(loc, detail::to_string_view(fmt),
212 fmt::make_format_args<buffered_context<Char>>(args...));
213}
214
215template <typename OutputIt, typename S,
216 typename Char = detail::format_string_char_t<S>,
219auto vformat_to(OutputIt out, const S& fmt,
220 basic_format_args<buffered_context<Char>> args) -> OutputIt {
221 auto&& buf = detail::get_buffer<Char>(out);
223 return detail::get_iterator(buf, out);
224}
225
226template <typename OutputIt, typename S, typename... T,
227 typename Char = detail::format_string_char_t<S>,
229 !std::is_same<Char, char>::value &&
230 !std::is_same<Char, wchar_t>::value)>
231inline auto format_to(OutputIt out, const S& fmt, T&&... args) -> OutputIt {
232 return vformat_to(out, detail::to_string_view(fmt),
233 fmt::make_format_args<buffered_context<Char>>(args...));
234}
235
236template <typename S, typename OutputIt, typename... Args,
237 typename Char = detail::format_string_char_t<S>,
240inline auto vformat_to(OutputIt out, locale_ref loc, const S& fmt,
242 -> OutputIt {
243 auto&& buf = detail::get_buffer<Char>(out);
244 vformat_to(buf, detail::to_string_view(fmt), args, loc);
245 return detail::get_iterator(buf, out);
246}
247
248template <typename OutputIt, typename S, typename... T,
249 typename Char = detail::format_string_char_t<S>,
252inline auto format_to(OutputIt out, locale_ref loc, const S& fmt, 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(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(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
333inline void vprint(std::wostream& os, wstring_view fmt, wformat_args args) {
334 auto buffer = basic_memory_buffer<wchar_t>();
335 detail::vformat_to(buffer, fmt, args);
336 detail::write_buffer(os, buffer);
337}
338
339template <typename... T>
340void print(std::wostream& os, wformat_string<T...> fmt, T&&... args) {
341 vprint(os, fmt, fmt::make_format_args<buffered_context<wchar_t>>(args...));
342}
343
344template <typename... T>
345void println(std::wostream& os, wformat_string<T...> fmt, T&&... args) {
346 print(os, L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
347}
348
349/// Converts `value` to `std::wstring` using the default format for type `T`.
350template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
351 return format(FMT_STRING(L"{}"), value);
352}
355
356#endif // FMT_XCHAR_H_
typename std::enable_if< B, T >::type enable_if_t
Definition base.h:304
#define FMT_END_EXPORT
Definition base.h:264
std::integral_constant< bool, B > bool_constant
Definition base.h:307
#define FMT_CONSTEVAL
Definition base.h:140
#define FMT_CONSTEXPR
Definition base.h:113
#define FMT_ALWAYS_INLINE
Definition base.h:243
#define FMT_BEGIN_NAMESPACE
Definition base.h:253
#define FMT_USE_CONSTEVAL
Definition base.h:118
#define FMT_ENABLE_IF(...)
Definition base.h:341
#define FMT_BEGIN_EXPORT
Definition base.h:263
void void_t
Definition base.h:328
#define FMT_END_NAMESPACE
Definition base.h:256
conditional_t< std::is_same< Char, char >::value, context, generic_context< basic_appender< Char >, Char > > buffered_context
Definition base.h:635
Definition base.h:2469
A view of a collection of formatting arguments.
Definition base.h:2570
An implementation of std::basic_string_view for pre-C++17.
Definition base.h:515
A contiguous memory buffer with an optional growing ability.
Definition base.h:1763
FMT_CONSTEXPR void push_back(const T &value)
Definition base.h:1832
constexpr auto size() const noexcept -> size_t
Returns the size of this buffer.
Definition base.h:1805
FMT_CONSTEXPR auto data() noexcept -> T *
Returns a pointer to the buffer data (not null-terminated).
Definition base.h:1811
Definition base.h:2020
Definition base.h:1874
Definition base.h:1696
Definition base.h:1891
Definition base.h:2163
Definition format.h:3862
Definition base.h:912
Parsing context consisting of a format string range being parsed and an argument counter for automati...
Definition base.h:850
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:4260
#define FMT_STRING(s)
Constructs a legacy compile-time format string from a string literal s.
Definition format.h:4239
#define FMT_THROW(x)
Definition format.h:173
Converts a string literal into a format string that will be parsed at compile time and converted into...
Definition printf.h:50
auto get_iterator(Buf &buf, OutputIt) -> decltype(buf.out())
Definition base.h:2126
FMT_CONSTEXPR void ignore_unused(const T &...)
Definition base.h:358
constexpr auto count_static_named_args() -> int
Definition base.h:1079
constexpr auto count_named_args() -> int
Definition base.h:1076
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:1982
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:1646
FMT_FUNC void vformat_to(buffer< char > &buf, string_view fmt, format_args args, locale_ref loc)
Definition format-inl.h:1456
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:973
constexpr auto to_string_view(const Char *s) -> basic_string_view< Char >
Definition base.h:948
auto get_buffer(OutputIt out) -> iterator_buffer< OutputIt, T >
Definition base.h:2116
Definition json.h:5164
Definition PointerIntPair.h:280
Definition xchar.h:78
FMT_CONSTEVAL FMT_ALWAYS_INLINE basic_fstring(const S &s)
Definition xchar.h:97
basic_fstring(runtime_format_string< Char > fmt)
Definition xchar.h:110
FMT_ALWAYS_INLINE basic_fstring(const S &)
Definition xchar.h:104
auto get() const -> basic_string_view< Char >
Definition xchar.h:113
Definition base.h:1693
Definition xchar.h:29
Definition base.h:2319
Definition format.h:2007
Definition format-inl.h:58
auto grouping() const -> std::string
Definition format-inl.h:59
auto thousands_sep() const -> Char
Definition format-inl.h:60
Definition format.h:3739
Definition base.h:839
Definition base.h:2864
Definition ranges.h:624
Definition base.h:2706
Definition ranges.h:675
#define S(label, offset, message)
Definition Errors.h:113
auto format(wformat_string< T... > fmt, T &&... args) -> std::wstring
Definition xchar.h:174
auto vformat(basic_string_view< Char > fmt, basic_format_args< buffered_context< Char > > args) -> std::basic_string< Char >
Definition xchar.h:165
buffered_context< wchar_t > wformat_context
Definition xchar.h:74
auto formatted_size(const S &fmt, T &&... args) -> size_t
Definition xchar.h:283
constexpr auto make_wformat_args(T &... args) -> decltype(fmt::make_format_args< wformat_context >(args...))
Definition xchar.h:126
basic_string_view< wchar_t > wstring_view
Definition xchar.h:72
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:76
auto vformat_to_n(OutputIt out, size_t n, basic_string_view< Char > fmt, basic_format_args< buffered_context< Char > > args) -> format_to_n_result< OutputIt >
Definition xchar.h:261
typename basic_format_string< wchar_t, T... >::t wformat_string
Definition xchar.h:120
auto format_to(OutputIt out, wformat_string< T... > fmt, T &&... args) -> OutputIt
Definition xchar.h:179
auto to_wstring(const T &value) -> std::wstring
Converts value to std::wstring using the default format for type T.
Definition xchar.h:350
auto join(It begin, Sentinel end, wstring_view sep) -> join_view< It, Sentinel, wchar_t >
Definition xchar.h:140
void print(std::FILE *f, wformat_string< T... > fmt, T &&... args)
Definition xchar.h:303
auto vformat_to(OutputIt out, const S &fmt, basic_format_args< buffered_context< Char > > args) -> OutputIt
Definition xchar.h:219
auto runtime(wstring_view s) -> runtime_format_string< wchar_t >
Definition xchar.h:121
void vprint(std::FILE *f, wstring_view fmt, wformat_args args)
Definition xchar.h:290
void println(std::FILE *f, wformat_string< T... > fmt, T &&... args)
Definition xchar.h:312