WPILibC++ 2024.3.2
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 <cwchar>
12
13#include "format.h"
14
15#ifndef FMT_STATIC_THOUSANDS_SEPARATOR
16# include <locale>
17#endif
18
20namespace detail {
21
22template <typename T>
24
25inline auto write_loc(std::back_insert_iterator<detail::buffer<wchar_t>> out,
27 locale_ref loc) -> bool {
28#ifndef FMT_STATIC_THOUSANDS_SEPARATOR
29 auto& numpunct =
30 std::use_facet<std::numpunct<wchar_t>>(loc.get<std::locale>());
31 auto separator = std::wstring();
32 auto grouping = numpunct.grouping();
33 if (!grouping.empty()) separator = std::wstring(1, numpunct.thousands_sep());
34 return value.visit(loc_writer<wchar_t>{out, specs, separator, grouping, {}});
35#endif
36 return false;
37}
38} // namespace detail
39
41
47
48#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
49// Workaround broken conversion on older gcc.
50template <typename... Args> using wformat_string = wstring_view;
51inline auto runtime(wstring_view s) -> wstring_view { return s; }
52#else
53template <typename... Args>
56 return {{s}};
57}
58#endif
59
60template <> struct is_char<wchar_t> : std::true_type {};
61template <> struct is_char<detail::char8_type> : std::true_type {};
62template <> struct is_char<char16_t> : std::true_type {};
63template <> struct is_char<char32_t> : std::true_type {};
64
65template <typename... T>
67 const T&... args) {
68 return {args...};
69}
70
71inline namespace literals {
72#if FMT_USE_USER_DEFINED_LITERALS && !FMT_USE_NONTYPE_TEMPLATE_ARGS
73constexpr detail::udl_arg<wchar_t> operator"" _a(const wchar_t* s, size_t) {
74 return {s};
75}
76#endif
77} // namespace literals
78
79template <typename It, typename Sentinel>
80auto join(It begin, Sentinel end, wstring_view sep)
82 return {begin, end, sep};
83}
84
85template <typename Range>
86auto join(Range&& range, wstring_view sep)
88 wchar_t> {
89 return join(std::begin(range), std::end(range), sep);
90}
91
92template <typename T>
93auto join(std::initializer_list<T> list, wstring_view sep)
95 return join(std::begin(list), std::end(list), sep);
96}
97
98template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
101 -> std::basic_string<Char> {
102 auto buf = basic_memory_buffer<Char>();
103 detail::vformat_to(buf, format_str, args);
104 return to_string(buf);
105}
106
107template <typename... T>
108auto format(wformat_string<T...> fmt, T&&... args) -> std::wstring {
109 return vformat(fmt::wstring_view(fmt), fmt::make_wformat_args(args...));
110}
111
112// Pass char_t as a default template parameter instead of using
113// std::basic_string<char_t<S>> to reduce the symbol size.
114template <typename S, typename... T, typename Char = char_t<S>,
115 FMT_ENABLE_IF(!std::is_same<Char, char>::value &&
116 !std::is_same<Char, wchar_t>::value)>
117auto format(const S& format_str, T&&... args) -> std::basic_string<Char> {
118 return vformat(detail::to_string_view(format_str),
120}
121
122template <typename Locale, typename S, typename Char = char_t<S>,
123 FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
124 detail::is_exotic_char<Char>::value)>
125inline auto vformat(
126 const Locale& loc, const S& format_str,
128 -> std::basic_string<Char> {
129 return detail::vformat(loc, detail::to_string_view(format_str), args);
130}
131
132template <typename Locale, typename S, typename... T, typename Char = char_t<S>,
133 FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
135inline auto format(const Locale& loc, const S& format_str, T&&... args)
136 -> std::basic_string<Char> {
137 return detail::vformat(loc, detail::to_string_view(format_str),
139}
140
141template <typename OutputIt, typename S, typename Char = char_t<S>,
142 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
143 detail::is_exotic_char<Char>::value)>
144auto vformat_to(OutputIt out, const S& format_str,
146 -> OutputIt {
147 auto&& buf = detail::get_buffer<Char>(out);
148 detail::vformat_to(buf, detail::to_string_view(format_str), args);
149 return detail::get_iterator(buf, out);
150}
151
152template <typename OutputIt, typename S, typename... T,
153 typename Char = char_t<S>,
156inline auto format_to(OutputIt out, const S& fmt, T&&... args) -> OutputIt {
157 return vformat_to(out, detail::to_string_view(fmt),
159}
160
161template <typename Locale, typename S, typename OutputIt, typename... Args,
162 typename Char = char_t<S>,
164 detail::is_locale<Locale>::value&&
166inline auto vformat_to(
167 OutputIt out, const Locale& loc, const S& format_str,
169 auto&& buf = detail::get_buffer<Char>(out);
170 vformat_to(buf, detail::to_string_view(format_str), args,
171 detail::locale_ref(loc));
172 return detail::get_iterator(buf, out);
173}
174
175template <
176 typename OutputIt, typename Locale, typename S, typename... T,
177 typename Char = char_t<S>,
179 detail::is_locale<Locale>::value&& detail::is_exotic_char<Char>::value>
180inline auto format_to(OutputIt out, const Locale& loc, const S& format_str,
181 T&&... args) ->
183 return vformat_to(out, loc, detail::to_string_view(format_str),
185}
186
187template <typename OutputIt, typename Char, typename... Args,
190inline auto vformat_to_n(
191 OutputIt out, size_t n, basic_string_view<Char> format_str,
194 using traits = detail::fixed_buffer_traits;
196 detail::vformat_to(buf, format_str, args);
197 return {buf.out(), buf.count()};
198}
199
200template <typename OutputIt, typename S, typename... T,
201 typename Char = char_t<S>,
204inline auto format_to_n(OutputIt out, size_t n, const S& fmt, T&&... args)
206 return vformat_to_n(out, n, detail::to_string_view(fmt),
208}
209
210template <typename S, typename... T, typename Char = char_t<S>,
212inline auto formatted_size(const S& fmt, T&&... args) -> size_t {
216 return buf.count();
217}
218
219inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) {
220 auto buf = wmemory_buffer();
221 detail::vformat_to(buf, fmt, args);
222 buf.push_back(L'\0');
223 std::fputws(buf.data(), f);
224}
225
226inline void vprint(wstring_view fmt, wformat_args args) {
227 vprint(stdout, fmt, args);
228}
229
230template <typename... T>
231void print(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
232 return vprint(f, wstring_view(fmt), fmt::make_wformat_args(args...));
233}
234
235template <typename... T> void print(wformat_string<T...> fmt, T&&... args) {
236 return vprint(wstring_view(fmt), fmt::make_wformat_args(args...));
237}
238
239template <typename... T>
240void println(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
241 return print(f, L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
242}
243
244template <typename... T> void println(wformat_string<T...> fmt, T&&... args) {
245 return print(L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
246}
247
248/**
249 Converts *value* to ``std::wstring`` using the default format for type *T*.
250 */
251template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
252 return format(FMT_STRING(L"{}"), value);
253}
256
257#endif // FMT_XCHAR_H_
\rst A view of a collection of formatting arguments.
Definition: core.h:1857
Definition: core.h:1706
\rst Parsing context consisting of a format string range being parsed and an argument counter for aut...
Definition: core.h:656
A compile-time format string.
Definition: core.h:2721
An implementation of std::basic_string_view for pre-C++17.
Definition: core.h:398
Definition: core.h:1019
Definition: core.h:888
Definition: core.h:905
Definition: core.h:1529
Definition: core.h:1238
\rst An array of references to arguments.
Definition: core.h:1779
Definition: format.h:1080
#define FMT_END_EXPORT
Definition: core.h:185
typename detail::char_t_impl< S >::type char_t
String's character type.
Definition: core.h:646
#define FMT_BEGIN_NAMESPACE
Definition: core.h:174
constexpr auto make_format_args(T &... args) -> format_arg_store< Context, remove_cvref_t< T >... >
\rst Constructs a ~fmtformat_arg_store object that contains references to arguments and can be implic...
Definition: core.h:1824
#define FMT_ENABLE_IF(...)
Definition: core.h:286
#define FMT_BEGIN_EXPORT
Definition: core.h:184
typename type_identity< T >::type type_identity_t
Definition: core.h:267
#define FMT_END_NAMESPACE
Definition: core.h:177
#define FMT_STRING(s)
\rst Constructs a compile-time format string from a string literal s.
Definition: format.h:1912
detail namespace with internal helper functions
Definition: xchar.h:20
decltype(std::end(std::declval< T & >())) sentinel_t
Definition: format.h:552
auto write_loc(std::back_insert_iterator< detail::buffer< wchar_t > > out, loc_value value, const format_specs< wchar_t > &specs, locale_ref loc) -> bool
Definition: xchar.h:25
bool_constant<!std::is_same< T, char >::value > is_exotic_char
Definition: xchar.h:23
FMT_INLINE auto to_string_view(const Char *s) -> basic_string_view< Char >
Definition: core.h:517
auto vformat(const Locale &loc, basic_string_view< Char > fmt, basic_format_args< buffer_context< type_identity_t< Char > > > args) -> std::basic_string< Char >
Definition: format.h:3934
std::integral_constant< bool, Value > bool_constant
Definition: type_traits.h:688
FMT_INLINE auto get_iterator(Buf &buf, OutputIt) -> decltype(buf.out())
Definition: core.h:1127
void vformat_to(buffer< Char > &buf, const text_style &ts, basic_string_view< Char > format_str, basic_format_args< buffer_context< type_identity_t< Char > > > args)
Definition: color.h:436
type
Definition: core.h:556
char8_type
Definition: format.h:639
Definition: xchar.h:71
std::string to_string(const T &t)
Definition: base.h:92
cubed< length::millimeter > L
Definition: volume.h:49
Definition: core.h:1514
Definition: format.h:2192
Definition: core.h:2042
Definition: core.h:2819
Specifies if T is a character type.
Definition: core.h:505
Definition: format.h:4210
Definition: core.h:2716
#define S(label, offset, message)
Definition: Errors.h:119
auto format(wformat_string< T... > fmt, T &&... args) -> std::wstring
Definition: xchar.h:108
auto vformat_to(OutputIt out, const S &format_str, basic_format_args< buffer_context< type_identity_t< Char > > > args) -> OutputIt
Definition: xchar.h:144
auto formatted_size(const S &fmt, T &&... args) -> size_t
Definition: xchar.h:212
auto vformat(basic_string_view< Char > format_str, basic_format_args< buffer_context< type_identity_t< Char > > > args) -> std::basic_string< Char >
Definition: xchar.h:99
auto format_to(OutputIt out, const S &fmt, T &&... args) -> OutputIt
Definition: xchar.h:156
basic_string_view< wchar_t > wstring_view
Definition: xchar.h:42
auto vformat_to_n(OutputIt out, size_t n, basic_string_view< Char > format_str, basic_format_args< buffer_context< type_identity_t< Char > > > args) -> format_to_n_result< OutputIt >
Definition: xchar.h:190
auto format_to_n(OutputIt out, size_t n, const S &fmt, T &&... args) -> format_to_n_result< OutputIt >
Definition: xchar.h:204
basic_memory_buffer< wchar_t > wmemory_buffer
Definition: xchar.h:46
auto to_wstring(const T &value) -> std::wstring
Converts value to std::wstring using the default format for type T.
Definition: xchar.h:251
auto join(It begin, Sentinel end, wstring_view sep) -> join_view< It, Sentinel, wchar_t >
Definition: xchar.h:80
constexpr format_arg_store< wformat_context, T... > make_wformat_args(const T &... args)
Definition: xchar.h:66
void print(std::FILE *f, wformat_string< T... > fmt, T &&... args)
Definition: xchar.h:231
auto runtime(wstring_view s) -> runtime_format_string< wchar_t >
Definition: xchar.h:55
void vprint(std::FILE *f, wstring_view fmt, wformat_args args)
Definition: xchar.h:219
void println(std::FILE *f, wformat_string< T... > fmt, T &&... args)
Definition: xchar.h:240
buffer_context< wchar_t > wformat_context
Definition: xchar.h:44