enum_name
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#include <cwchar>
12
13#include "format.h"
14
15#ifndef FMT_STATIC_THOUSANDS_SEPARATOR
16# include <locale>
17#endif
18
19FMT_BEGIN_NAMESPACE
20namespace detail {
21
22template <typename T>
23using is_exotic_char = bool_constant<!std::is_same<T, char>::value>;
24
25inline auto write_loc(std::back_insert_iterator<detail::buffer<wchar_t>> out,
26 loc_value value, const format_specs<wchar_t>& specs,
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
40FMT_BEGIN_EXPORT
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>
55inline auto runtime(wstring_view s) -> runtime_format_string<wchar_t> {
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>
66constexpr auto make_wformat_args(const T&... args)
68 return {args...};
69}
70
71inline namespace literals {
72#if FMT_USE_USER_DEFINED_LITERALS && !FMT_USE_NONTYPE_TEMPLATE_ARGS
73constexpr auto operator""_a(const wchar_t* s, size_t)
74 -> detail::udl_arg<wchar_t> {
75 return {s};
76}
77#endif
78} // namespace literals
79
80template <typename It, typename Sentinel>
81auto join(It begin, Sentinel end, wstring_view sep)
83 return {begin, end, sep};
84}
85
86template <typename Range>
87auto join(Range&& range, wstring_view sep)
88 -> join_view<detail::iterator_t<Range>, detail::sentinel_t<Range>,
89 wchar_t> {
90 return join(std::begin(range), std::end(range), sep);
91}
92
93template <typename T>
94auto join(std::initializer_list<T> list, wstring_view sep)
96 return join(std::begin(list), std::end(list), sep);
97}
98
99template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
100auto vformat(basic_string_view<Char> format_str,
101 basic_format_args<buffer_context<type_identity_t<Char>>> args)
102 -> std::basic_string<Char> {
103 auto buf = basic_memory_buffer<Char>();
104 detail::vformat_to(buf, format_str, args);
105 return to_string(buf);
106}
107
108template <typename... T>
109auto format(wformat_string<T...> fmt, T&&... args) -> std::wstring {
110 return vformat(fmt::wstring_view(fmt), fmt::make_wformat_args(args...));
111}
112
113// Pass char_t as a default template parameter instead of using
114// std::basic_string<char_t<S>> to reduce the symbol size.
115template <typename S, typename... T, typename Char = char_t<S>,
116 FMT_ENABLE_IF(!std::is_same<Char, char>::value &&
117 !std::is_same<Char, wchar_t>::value)>
118auto format(const S& format_str, T&&... args) -> std::basic_string<Char> {
119 return vformat(detail::to_string_view(format_str),
120 fmt::make_format_args<buffer_context<Char>>(args...));
121}
122
123template <typename Locale, typename S, typename Char = char_t<S>,
124 FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
125 detail::is_exotic_char<Char>::value)>
126inline auto vformat(
127 const Locale& loc, const S& format_str,
128 basic_format_args<buffer_context<type_identity_t<Char>>> args)
129 -> std::basic_string<Char> {
130 return detail::vformat(loc, detail::to_string_view(format_str), args);
131}
132
133template <typename Locale, typename S, typename... T, typename Char = char_t<S>,
134 FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
135 detail::is_exotic_char<Char>::value)>
136inline auto format(const Locale& loc, const S& format_str, T&&... args)
137 -> std::basic_string<Char> {
138 return detail::vformat(loc, detail::to_string_view(format_str),
139 fmt::make_format_args<buffer_context<Char>>(args...));
140}
141
142template <typename OutputIt, typename S, typename Char = char_t<S>,
143 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
144 detail::is_exotic_char<Char>::value)>
145auto vformat_to(OutputIt out, const S& format_str,
146 basic_format_args<buffer_context<type_identity_t<Char>>> args)
147 -> OutputIt {
148 auto&& buf = detail::get_buffer<Char>(out);
149 detail::vformat_to(buf, detail::to_string_view(format_str), args);
150 return detail::get_iterator(buf, out);
151}
152
153template <typename OutputIt, typename S, typename... T,
154 typename Char = char_t<S>,
156 detail::is_exotic_char<Char>::value)>
157inline auto format_to(OutputIt out, const S& fmt, T&&... args) -> OutputIt {
158 return vformat_to(out, detail::to_string_view(fmt),
159 fmt::make_format_args<buffer_context<Char>>(args...));
160}
161
162template <typename Locale, typename S, typename OutputIt, typename... Args,
163 typename Char = char_t<S>,
165 detail::is_locale<Locale>::value&&
166 detail::is_exotic_char<Char>::value)>
167inline auto vformat_to(
168 OutputIt out, const Locale& loc, const S& format_str,
169 basic_format_args<buffer_context<type_identity_t<Char>>> args) -> OutputIt {
170 auto&& buf = detail::get_buffer<Char>(out);
171 vformat_to(buf, detail::to_string_view(format_str), args,
172 detail::locale_ref(loc));
173 return detail::get_iterator(buf, out);
174}
175
176template <typename OutputIt, typename Locale, typename S, typename... T,
177 typename Char = char_t<S>,
179 detail::is_locale<Locale>::value &&
180 detail::is_exotic_char<Char>::value>
181inline auto format_to(OutputIt out, const Locale& loc, const S& format_str,
182 T&&... args) ->
183 typename std::enable_if<enable, OutputIt>::type {
184 return vformat_to(out, loc, detail::to_string_view(format_str),
185 fmt::make_format_args<buffer_context<Char>>(args...));
186}
187
188template <typename OutputIt, typename Char, typename... Args,
190 detail::is_exotic_char<Char>::value)>
191inline auto vformat_to_n(
192 OutputIt out, size_t n, basic_string_view<Char> format_str,
193 basic_format_args<buffer_context<type_identity_t<Char>>> args)
195 using traits = detail::fixed_buffer_traits;
197 detail::vformat_to(buf, format_str, args);
198 return {buf.out(), buf.count()};
199}
200
201template <typename OutputIt, typename S, typename... T,
202 typename Char = char_t<S>,
204 detail::is_exotic_char<Char>::value)>
205inline auto format_to_n(OutputIt out, size_t n, const S& fmt, T&&... args)
207 return vformat_to_n(out, n, detail::to_string_view(fmt),
208 fmt::make_format_args<buffer_context<Char>>(args...));
209}
210
211template <typename S, typename... T, typename Char = char_t<S>,
212 FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
213inline auto formatted_size(const S& fmt, T&&... args) -> size_t {
215 detail::vformat_to(buf, detail::to_string_view(fmt),
216 fmt::make_format_args<buffer_context<Char>>(args...));
217 return buf.count();
218}
219
220inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) {
221 auto buf = wmemory_buffer();
222 detail::vformat_to(buf, fmt, args);
223 buf.push_back(L'\0');
224 if (std::fputws(buf.data(), f) == -1)
225 FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
226}
227
228inline void vprint(wstring_view fmt, wformat_args args) {
229 vprint(stdout, fmt, args);
230}
231
232template <typename... T>
233void print(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
234 return vprint(f, wstring_view(fmt), fmt::make_wformat_args(args...));
235}
236
237template <typename... T> void print(wformat_string<T...> fmt, T&&... args) {
238 return vprint(wstring_view(fmt), fmt::make_wformat_args(args...));
239}
240
241template <typename... T>
242void println(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
243 return print(f, L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
244}
245
246template <typename... T> void println(wformat_string<T...> fmt, T&&... args) {
247 return print(L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
248}
249
253template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
254 return format(FMT_STRING(L"{}"), value);
255}
256FMT_END_EXPORT
257FMT_END_NAMESPACE
258
259#endif // FMT_XCHAR_H_
Definition core.h:1891
Definition core.h:1739
Definition core.h:674
Definition core.h:2768
Definition core.h:415
Definition core.h:816
Definition core.h:1038
Definition core.h:907
Definition core.h:924
Definition core.h:1553
Definition core.h:1813
Definition format.h:1045
Definition core.h:1538
Definition core.h:2076
Definition core.h:2866
Definition core.h:522
Definition format.h:4230
Definition xchar-test.cc:451
Definition core.h:2763