Debian Codegraph @

podofo/3rdparty/fmtlib/fmt/xchar.h

raw
1 // Formatting library for C++ - optional wchar_t and exotic character support
2 //
3 // Copyright (c) 2012 - present, Victor Zverovich and {fmt} contributors
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
23 FMT_BEGIN_NAMESPACE
24 namespace detail {
25
26 template <typename T>
27 using is_exotic_char = bool_constant<!std::is_same<T, char>::value>;
28
29 template <typename S, typename = void> struct format_string_char {};
30
31 template <typename S>
32 struct format_string_char<
33 S, void_t<decltype(sizeof(detail::to_string_view(std::declval<S>())))>> {
34 using type = char_t<S>;
35 };
36
37 template <typename S>
38 struct format_string_char<
39 S, enable_if_t<std::is_base_of<detail::compile_string, S>::value>> {
40 using type = typename S::char_type;
41 };
42
43 template <typename S>
44 using format_string_char_t = typename format_string_char<S>::type;
45
46 inline auto write_loc(basic_appender<wchar_t> out, loc_value value,
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
59 template <typename Char>
60 void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
61 basic_format_args<buffered_context<Char>> args,
62 locale_ref loc = {}) {
63 static_assert(!std::is_same<Char, char>::value, "");
64 auto out = basic_appender<Char>(buf);
65 parse_format_string(
66 fmt, format_handler<Char>{parse_context<Char>(fmt), {out, args, loc}});
67 }
68 } // namespace detail
69
70 FMT_BEGIN_EXPORT
71
72 using wstring_view = basic_string_view<wchar_t>;
73 using wformat_parse_context = parse_context<wchar_t>;
74 using wformat_context = buffered_context<wchar_t>;
75 using wformat_args = basic_format_args<wformat_context>;
76 using wmemory_buffer = basic_memory_buffer<wchar_t>;
77
78 template <typename Char, typename... T> struct basic_fstring {
79 private:
80 basic_string_view<Char> str_;
81
82 static constexpr int num_static_named_args =
83 detail::count_static_named_args<T...>();
84
85 using checker = detail::format_string_checker<
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:
92 using t = basic_fstring;
93
94 template <typename S,
95 FMT_ENABLE_IF(
96 std::is_convertible<const S&, basic_string_view<Char>>::value)>
97 FMT_CONSTEVAL FMT_ALWAYS_INLINE basic_fstring(const S& s) : str_(s) {
98 if (FMT_USE_CONSTEVAL)
99 detail::parse_format_string<Char>(s, checker(s, arg_pack()));
100 }
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()) {
105 FMT_CONSTEXPR auto sv = basic_string_view<Char>(S());
106 FMT_CONSTEXPR int ignore =
107 (parse_format_string(sv, checker(sv, arg_pack())), 0);
108 detail::ignore_unused(ignore);
109 }
110 basic_fstring(runtime_format_string<Char> fmt) : str_(fmt.str) {}
111
112 FMT_DEPRECATED operator basic_string_view<Char>() const { return str_; }
113 auto get() const -> basic_string_view<Char> { return str_; }
114 };
115
116 template <typename Char, typename... T>
117 using basic_format_string = basic_fstring<Char, T...>;
118
119 template <typename... T>
120 using wformat_string = typename basic_format_string<wchar_t, T...>::t;
121 inline auto runtime(wstring_view s) -> runtime_format_string<wchar_t> {
122 return {{s}};
123 }
124
125 template <typename... T>
126 constexpr 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
132 inline namespace literals {
133 inline auto operator""_a(const wchar_t* s, size_t) -> detail::udl_arg<wchar_t> {
134 return {s};
135 }
136 } // namespace literals
137 #endif
138
139 template <typename T>
140 auto arg(const wchar_t* name, const T& arg) -> named_arg<T, wchar_t> {
141 return {name, arg};
142 }
143
144 template <typename It, typename Sentinel, typename S,
145 typename Char = typename decltype(detail::to_string_view(
146 std::declval<S>()))::value_type,
147 FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
148 auto join(It begin, Sentinel end, S&& sep) -> join_view<It, Sentinel, Char> {
149 return {begin, end, detail::to_string_view(sep)};
150 }
151
152 template <typename Range, typename S,
153 typename Char = typename decltype(detail::to_string_view(
154 std::declval<S>()))::value_type,
155 FMT_ENABLE_IF(detail::is_exotic_char<Char>::value &&
156 !is_tuple_like<Range>::value)>
157 auto join(Range&& range, S&& sep)
158 -> join_view<decltype(std::begin(range)), decltype(std::end(range)), Char> {
159 return {std::begin(range), std::end(range), detail::to_string_view(sep)};
160 }
161
162 template <typename T>
163 FMT_DEPRECATED auto join(std::initializer_list<T> list, wstring_view sep)
164 -> join_view<const T*, const T*, wchar_t> {
165 return join(std::begin(list), std::end(list), sep);
166 }
167
168 template <typename Tuple, typename S,
169 typename Char = typename decltype(detail::to_string_view(
170 std::declval<S>()))::value_type,
171 FMT_ENABLE_IF(detail::is_exotic_char<Char>::value&&
172 is_tuple_like<Tuple>::value)>
173 auto join(const Tuple& tuple, S&& sep) -> tuple_join_view<Tuple, Char> {
174 return {tuple, detail::to_string_view(sep)};
175 }
176
177 template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
178 auto vformat(basic_string_view<Char> fmt,
179 basic_format_args<buffered_context<Char>> args)
180 -> std::basic_string<Char> {
181 auto buf = basic_memory_buffer<Char>();
182 detail::vformat_to(buf, fmt, args);
183 return {buf.data(), buf.size()};
184 }
185
186 template <typename... T>
187 auto format(wformat_string<T...> fmt, T&&... args) -> std::wstring {
188 return vformat(fmt.get(), fmt::make_wformat_args(args...));
189 }
190
191 template <typename OutputIt, typename... T>
192 auto format_to(OutputIt out, wformat_string<T...> fmt, T&&... args)
193 -> OutputIt {
194 return vformat_to(out, fmt.get(), fmt::make_wformat_args(args...));
195 }
196
197 // Pass char_t as a default template parameter instead of using
198 // std::basic_string<char_t<S>> to reduce the symbol size.
199 template <typename S, typename... T,
200 typename Char = detail::format_string_char_t<S>,
201 FMT_ENABLE_IF(!std::is_same<Char, char>::value &&
202 !std::is_same<Char, wchar_t>::value)>
203 auto format(const S& fmt, T&&... args) -> std::basic_string<Char> {
204 return vformat(detail::to_string_view(fmt),
205 fmt::make_format_args<buffered_context<Char>>(args...));
206 }
207
208 template <typename S, typename Char = detail::format_string_char_t<S>,
209 FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
210 inline auto vformat(locale_ref loc, const S& fmt,
211 basic_format_args<buffered_context<Char>> args)
212 -> std::basic_string<Char> {
213 auto buf = basic_memory_buffer<Char>();
214 detail::vformat_to(buf, detail::to_string_view(fmt), args, loc);
215 return {buf.data(), buf.size()};
216 }
217
218 template <typename S, typename... T,
219 typename Char = detail::format_string_char_t<S>,
220 FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
221 inline auto format(locale_ref loc, const S& fmt, T&&... args)
222 -> std::basic_string<Char> {
223 return vformat(loc, detail::to_string_view(fmt),
224 fmt::make_format_args<buffered_context<Char>>(args...));
225 }
226
227 template <typename OutputIt, typename S,
228 typename Char = detail::format_string_char_t<S>,
229 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
230 detail::is_exotic_char<Char>::value)>
231 auto vformat_to(OutputIt out, const S& fmt,
232 basic_format_args<buffered_context<Char>> args) -> OutputIt {
233 auto&& buf = detail::get_buffer<Char>(out);
234 detail::vformat_to(buf, detail::to_string_view(fmt), args);
235 return detail::get_iterator(buf, out);
236 }
237
238 template <typename OutputIt, typename S, typename... T,
239 typename Char = detail::format_string_char_t<S>,
240 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value &&
241 !std::is_same<Char, char>::value &&
242 !std::is_same<Char, wchar_t>::value)>
243 inline auto format_to(OutputIt out, const S& fmt, T&&... args) -> OutputIt {
244 return vformat_to(out, detail::to_string_view(fmt),
245 fmt::make_format_args<buffered_context<Char>>(args...));
246 }
247
248 template <typename S, typename OutputIt, typename... Args,
249 typename Char = detail::format_string_char_t<S>,
250 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
251 detail::is_exotic_char<Char>::value)>
252 inline auto vformat_to(OutputIt out, locale_ref loc, const S& fmt,
253 basic_format_args<buffered_context<Char>> args)
254 -> OutputIt {
255 auto&& buf = detail::get_buffer<Char>(out);
256 vformat_to(buf, detail::to_string_view(fmt), args, loc);
257 return detail::get_iterator(buf, out);
258 }
259
260 template <typename OutputIt, typename S, typename... T,
261 typename Char = detail::format_string_char_t<S>,
262 bool enable = detail::is_output_iterator<OutputIt, Char>::value &&
263 detail::is_exotic_char<Char>::value>
264 inline auto format_to(OutputIt out, locale_ref loc, const S& fmt, T&&... args)
265 -> typename std::enable_if<enable, OutputIt>::type {
266 return vformat_to(out, loc, detail::to_string_view(fmt),
267 fmt::make_format_args<buffered_context<Char>>(args...));
268 }
269
270 template <typename OutputIt, typename Char, typename... Args,
271 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
272 detail::is_exotic_char<Char>::value)>
273 inline auto vformat_to_n(OutputIt out, size_t n, basic_string_view<Char> fmt,
274 basic_format_args<buffered_context<Char>> args)
275 -> format_to_n_result<OutputIt> {
276 using traits = detail::fixed_buffer_traits;
277 auto buf = detail::iterator_buffer<OutputIt, Char, traits>(out, n);
278 detail::vformat_to(buf, fmt, args);
279 return {buf.out(), buf.count()};
280 }
281
282 template <typename OutputIt, typename... T,
283 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, wchar_t>::value)>
284 FMT_INLINE auto format_to_n(OutputIt out, size_t n, wformat_string<T...> fmt,
285 T&&... args) -> format_to_n_result<OutputIt> {
286 return vformat_to_n(out, n, fmt.get(), fmt::make_wformat_args(args...));
287 }
288
289 template <typename OutputIt, typename S, typename... T,
290 typename Char = detail::format_string_char_t<S>,
291 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value &&
292 !std::is_same<Char, char>::value &&
293 !std::is_same<Char, wchar_t>::value)>
294 inline auto format_to_n(OutputIt out, size_t n, const S& fmt, T&&... args)
295 -> format_to_n_result<OutputIt> {
296 return vformat_to_n(out, n, fmt::basic_string_view<Char>(fmt),
297 fmt::make_format_args<buffered_context<Char>>(args...));
298 }
299
300 template <typename S, typename... T,
301 typename Char = detail::format_string_char_t<S>,
302 FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
303 inline auto formatted_size(const S& fmt, T&&... args) -> size_t {
304 auto buf = detail::counting_buffer<Char>();
305 detail::vformat_to(buf, detail::to_string_view(fmt),
306 fmt::make_format_args<buffered_context<Char>>(args...));
307 return buf.count();
308 }
309
310 inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) {
311 auto buf = wmemory_buffer();
312 detail::vformat_to(buf, fmt, args);
313 buf.push_back(L'\0');
314 if (std::fputws(buf.data(), f) == -1)
315 FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
316 }
317
318 inline void vprint(wstring_view fmt, wformat_args args) {
319 vprint(stdout, fmt, args);
320 }
321
322 template <typename... T>
323 void print(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
324 return vprint(f, fmt.get(), fmt::make_wformat_args(args...));
325 }
326
327 template <typename... T> void print(wformat_string<T...> fmt, T&&... args) {
328 return vprint(fmt.get(), fmt::make_wformat_args(args...));
329 }
330
331 template <typename... T>
332 void println(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
333 return print(f, L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
334 }
335
336 template <typename... T> void println(wformat_string<T...> fmt, T&&... args) {
337 return print(L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
338 }
339
340 inline auto vformat(text_style ts, wstring_view fmt, wformat_args args)
341 -> std::wstring {
342 auto buf = wmemory_buffer();
343 detail::vformat_to(buf, ts, fmt, args);
344 return {buf.data(), buf.size()};
345 }
346
347 template <typename... T>
348 inline auto format(text_style ts, wformat_string<T...> fmt, T&&... args)
349 -> std::wstring {
350 return fmt::vformat(ts, fmt.get(), fmt::make_wformat_args(args...));
351 }
352
353 inline void vprint(std::wostream& os, wstring_view fmt, wformat_args args) {
354 auto buffer = basic_memory_buffer<wchar_t>();
355 detail::vformat_to(buffer, fmt, args);
356 detail::write_buffer(os, buffer);
357 }
358
359 template <typename... T>
360 void print(std::wostream& os, wformat_string<T...> fmt, T&&... args) {
361 vprint(os, fmt.get(),
362 fmt::make_format_args<buffered_context<wchar_t>>(args...));
363 }
364
365 template <typename... T>
366 void println(std::wostream& os, wformat_string<T...> fmt, T&&... args) {
367 print(os, L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
368 }
369
370 /// Converts `value` to `std::wstring` using the default format for type `T`.
371 template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
372 return format(FMT_STRING(L"{}"), value);
373 }
374 FMT_END_EXPORT
375 FMT_END_NAMESPACE
376
377 #endif // FMT_XCHAR_H_