RavEngine
Loading...
Searching...
No Matches
xchar.h
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#ifndef FMT_IMPORT_STD
12# include <cwchar>
13#endif
14
15#include "color.h"
16#include "format.h"
17#include "ranges.h"
18
19#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR) && !defined(FMT_IMPORT_STD)
20# include <locale>
21#endif
22
23FMT_BEGIN_NAMESPACE
24namespace detail {
25
26template <typename T>
27using is_exotic_char = bool_constant<!std::is_same<T, char>::value>;
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>
38struct format_string_char<S, enable_if_t<is_compile_string<S>::value>> {
39 using type = typename S::char_type;
40};
41
42template <typename S>
43using format_string_char_t = typename format_string_char<S>::type;
44
45inline auto write_loc(basic_appender<wchar_t> out, loc_value value,
46 const format_specs& specs, locale_ref loc) -> bool {
47#ifndef FMT_STATIC_THOUSANDS_SEPARATOR
48 auto& numpunct =
49 std::use_facet<std::numpunct<wchar_t>>(loc.get<std::locale>());
50 auto separator = std::wstring();
51 auto grouping = numpunct.grouping();
52 if (!grouping.empty()) separator = std::wstring(1, numpunct.thousands_sep());
53 return value.visit(loc_writer<wchar_t>{out, specs, separator, grouping, {}});
54#endif
55 return false;
56}
57} // namespace detail
58
59FMT_BEGIN_EXPORT
60
63using wformat_context = buffered_context<wchar_t>;
66
67#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
68// Workaround broken conversion on older gcc.
69template <typename... Args> using wformat_string = wstring_view;
70inline auto runtime(wstring_view s) -> wstring_view { return s; }
71#else
72template <typename... Args>
74inline auto runtime(wstring_view s) -> runtime_format_string<wchar_t> {
75 return {{s}};
76}
77#endif
78
79template <> struct is_char<wchar_t> : std::true_type {};
80template <> struct is_char<char16_t> : std::true_type {};
81template <> struct is_char<char32_t> : std::true_type {};
82
83#ifdef __cpp_char8_t
84template <>
85struct is_char<char8_t> : bool_constant<detail::is_utf8_enabled()> {};
86#endif
87
88template <typename... T>
89constexpr auto make_wformat_args(T&... args)
90 -> decltype(fmt::make_format_args<wformat_context>(args...)) {
91 return fmt::make_format_args<wformat_context>(args...);
92}
93
94inline namespace literals {
95#if FMT_USE_USER_DEFINED_LITERALS && !FMT_USE_NONTYPE_TEMPLATE_ARGS
96constexpr auto operator""_a(const wchar_t* s, size_t)
97 -> detail::udl_arg<wchar_t> {
98 return {s};
99}
100#endif
101} // namespace literals
102
103template <typename It, typename Sentinel>
104auto join(It begin, Sentinel end, wstring_view sep)
106 return {begin, end, sep};
107}
108
109template <typename Range>
110auto join(Range&& range, wstring_view sep)
111 -> join_view<detail::iterator_t<Range>, detail::sentinel_t<Range>,
112 wchar_t> {
113 return join(std::begin(range), std::end(range), sep);
114}
115
116template <typename T>
117auto join(std::initializer_list<T> list, wstring_view sep)
119 return join(std::begin(list), std::end(list), sep);
120}
121
122template <typename... T>
123auto join(const std::tuple<T...>& tuple, basic_string_view<wchar_t> sep)
124 -> tuple_join_view<wchar_t, T...> {
125 return {tuple, sep};
126}
127
128template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
129auto vformat(basic_string_view<Char> format_str,
131 -> std::basic_string<Char> {
132 auto buf = basic_memory_buffer<Char>();
133 detail::vformat_to(buf, format_str, args);
134 return to_string(buf);
135}
136
137template <typename... T>
138auto format(wformat_string<T...> fmt, T&&... args) -> std::wstring {
139 return vformat(fmt::wstring_view(fmt), fmt::make_wformat_args(args...));
140}
141
142template <typename OutputIt, typename... T>
143auto format_to(OutputIt out, wformat_string<T...> fmt, T&&... args)
144 -> OutputIt {
145 return vformat_to(out, fmt::wstring_view(fmt),
146 fmt::make_wformat_args(args...));
147}
148
149// Pass char_t as a default template parameter instead of using
150// std::basic_string<char_t<S>> to reduce the symbol size.
151template <typename S, typename... T,
152 typename Char = detail::format_string_char_t<S>,
153 FMT_ENABLE_IF(!std::is_same<Char, char>::value &&
154 !std::is_same<Char, wchar_t>::value)>
155auto format(const S& format_str, T&&... args) -> std::basic_string<Char> {
156 return vformat(detail::to_string_view(format_str),
157 fmt::make_format_args<buffered_context<Char>>(args...));
158}
159
160template <typename Locale, typename S,
161 typename Char = detail::format_string_char_t<S>,
162 FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
163 detail::is_exotic_char<Char>::value)>
164inline auto vformat(const Locale& loc, const S& format_str,
166 -> std::basic_string<Char> {
167 return detail::vformat(loc, detail::to_string_view(format_str), args);
168}
169
170template <typename Locale, typename S, typename... T,
171 typename Char = detail::format_string_char_t<S>,
172 FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
173 detail::is_exotic_char<Char>::value)>
174inline auto format(const Locale& loc, const S& format_str, T&&... args)
175 -> std::basic_string<Char> {
176 return detail::vformat(
177 loc, detail::to_string_view(format_str),
178 fmt::make_format_args<buffered_context<Char>>(args...));
179}
180
181template <typename OutputIt, typename S,
182 typename Char = detail::format_string_char_t<S>,
184 detail::is_exotic_char<Char>::value)>
185auto vformat_to(OutputIt out, const S& format_str,
186 typename detail::vformat_args<Char>::type args) -> OutputIt {
187 auto&& buf = detail::get_buffer<Char>(out);
188 detail::vformat_to(buf, detail::to_string_view(format_str), args);
189 return detail::get_iterator(buf, out);
190}
191
192template <typename OutputIt, typename S, typename... T,
193 typename Char = detail::format_string_char_t<S>,
195 !std::is_same<Char, char>::value &&
196 !std::is_same<Char, wchar_t>::value)>
197inline auto format_to(OutputIt out, const S& fmt, T&&... args) -> OutputIt {
198 return vformat_to(out, detail::to_string_view(fmt),
199 fmt::make_format_args<buffered_context<Char>>(args...));
200}
201
202template <typename Locale, typename S, typename OutputIt, typename... Args,
203 typename Char = detail::format_string_char_t<S>,
205 detail::is_locale<Locale>::value&&
206 detail::is_exotic_char<Char>::value)>
207inline auto vformat_to(OutputIt out, const Locale& loc, const S& format_str,
209 -> OutputIt {
210 auto&& buf = detail::get_buffer<Char>(out);
211 vformat_to(buf, detail::to_string_view(format_str), args,
212 detail::locale_ref(loc));
213 return detail::get_iterator(buf, out);
214}
215
216template <typename OutputIt, typename Locale, typename S, typename... T,
217 typename Char = detail::format_string_char_t<S>,
219 detail::is_locale<Locale>::value &&
220 detail::is_exotic_char<Char>::value>
221inline auto format_to(OutputIt out, const Locale& loc, const S& format_str,
222 T&&... args) ->
223 typename std::enable_if<enable, OutputIt>::type {
224 return vformat_to(out, loc, detail::to_string_view(format_str),
225 fmt::make_format_args<buffered_context<Char>>(args...));
226}
227
228template <typename OutputIt, typename Char, typename... Args,
230 detail::is_exotic_char<Char>::value)>
231inline auto vformat_to_n(OutputIt out, size_t n,
232 basic_string_view<Char> format_str,
235 using traits = detail::fixed_buffer_traits;
237 detail::vformat_to(buf, format_str, args);
238 return {buf.out(), buf.count()};
239}
240
241template <typename OutputIt, typename S, typename... T,
242 typename Char = detail::format_string_char_t<S>,
244 detail::is_exotic_char<Char>::value)>
245inline auto format_to_n(OutputIt out, size_t n, const S& fmt, T&&... args)
247 return vformat_to_n(out, n, fmt::basic_string_view<Char>(fmt),
248 fmt::make_format_args<buffered_context<Char>>(args...));
249}
250
251template <typename S, typename... T,
252 typename Char = detail::format_string_char_t<S>,
253 FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
254inline auto formatted_size(const S& fmt, T&&... args) -> size_t {
256 detail::vformat_to(buf, detail::to_string_view(fmt),
257 fmt::make_format_args<buffered_context<Char>>(args...));
258 return buf.count();
259}
260
261inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) {
262 auto buf = wmemory_buffer();
263 detail::vformat_to(buf, fmt, args);
264 buf.push_back(L'\0');
265 if (std::fputws(buf.data(), f) == -1)
266 FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
267}
268
269inline void vprint(wstring_view fmt, wformat_args args) {
270 vprint(stdout, fmt, args);
271}
272
273template <typename... T>
274void print(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
275 return vprint(f, wstring_view(fmt), fmt::make_wformat_args(args...));
276}
277
278template <typename... T> void print(wformat_string<T...> fmt, T&&... args) {
279 return vprint(wstring_view(fmt), fmt::make_wformat_args(args...));
280}
281
282template <typename... T>
283void println(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
284 return print(f, L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
285}
286
287template <typename... T> void println(wformat_string<T...> fmt, T&&... args) {
288 return print(L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
289}
290
291inline auto vformat(const text_style& ts, wstring_view fmt, wformat_args args)
292 -> std::wstring {
293 auto buf = wmemory_buffer();
294 detail::vformat_to(buf, ts, fmt, args);
295 return fmt::to_string(buf);
296}
297
298template <typename... T>
299inline auto format(const text_style& ts, wformat_string<T...> fmt, T&&... args)
300 -> std::wstring {
301 return fmt::vformat(ts, fmt, fmt::make_wformat_args(args...));
302}
303
304template <typename... T>
305FMT_DEPRECATED void print(std::FILE* f, const text_style& ts,
306 wformat_string<T...> fmt, const T&... args) {
307 vprint(f, ts, fmt, fmt::make_wformat_args(args...));
308}
309
310template <typename... T>
311FMT_DEPRECATED void print(const text_style& ts, wformat_string<T...> fmt,
312 const T&... args) {
313 return print(stdout, ts, fmt, args...);
314}
315
319template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
320 return format(FMT_STRING(L"{}"), value);
321}
322FMT_END_EXPORT
323FMT_END_NAMESPACE
324
325#endif // FMT_XCHAR_H_
Definition base.h:1143
Definition base.h:1827
Definition base.h:727
Definition base.h:2857
Definition base.h:484
Definition base.h:1082
Definition base.h:945
Definition base.h:962
Definition base.h:1561
Definition base.h:1310
Definition format.h:1045
Definition color.h:231
Definition Common3D.hpp:137
Definition xchar.h:29
Definition base.h:1551
Definition format.h:2146
Definition base.h:2152
Definition base.h:2942
Definition base.h:586
Definition ranges.h:606
Definition base.h:2852
Definition ranges.h:688