• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 FMT_BEGIN_NAMESPACE
20 namespace detail {
21 
22 template <typename T>
23 using is_exotic_char = bool_constant<!std::is_same<T, char>::value>;
24 
25 inline 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 
40 FMT_BEGIN_EXPORT
41 
42 using wstring_view = basic_string_view<wchar_t>;
43 using wformat_parse_context = basic_format_parse_context<wchar_t>;
44 using wformat_context = buffer_context<wchar_t>;
45 using wformat_args = basic_format_args<wformat_context>;
46 using wmemory_buffer = basic_memory_buffer<wchar_t>;
47 
48 #if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
49 // Workaround broken conversion on older gcc.
50 template <typename... Args> using wformat_string = wstring_view;
51 inline auto runtime(wstring_view s) -> wstring_view { return s; }
52 #else
53 template <typename... Args>
54 using wformat_string = basic_format_string<wchar_t, type_identity_t<Args>...>;
55 inline auto runtime(wstring_view s) -> runtime_format_string<wchar_t> {
56   return {{s}};
57 }
58 #endif
59 
60 template <> struct is_char<wchar_t> : std::true_type {};
61 template <> struct is_char<detail::char8_type> : std::true_type {};
62 template <> struct is_char<char16_t> : std::true_type {};
63 template <> struct is_char<char32_t> : std::true_type {};
64 
65 template <typename... T>
66 constexpr auto make_wformat_args(const T&... args)
67     -> format_arg_store<wformat_context, T...> {
68   return {args...};
69 }
70 
71 inline namespace literals {
72 #if FMT_USE_USER_DEFINED_LITERALS && !FMT_USE_NONTYPE_TEMPLATE_ARGS
73 constexpr 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 
80 template <typename It, typename Sentinel>
81 auto join(It begin, Sentinel end, wstring_view sep)
82     -> join_view<It, Sentinel, wchar_t> {
83   return {begin, end, sep};
84 }
85 
86 template <typename Range>
87 auto 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 
93 template <typename T>
94 auto join(std::initializer_list<T> list, wstring_view sep)
95     -> join_view<const T*, const T*, wchar_t> {
96   return join(std::begin(list), std::end(list), sep);
97 }
98 
99 template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
100 auto 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 
108 template <typename... T>
109 auto 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.
115 template <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)>
118 auto 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 
123 template <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)>
126 inline 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 
133 template <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)>
136 inline 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 
142 template <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)>
145 auto 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 
153 template <typename OutputIt, typename S, typename... T,
154           typename Char = char_t<S>,
155           FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
156                             detail::is_exotic_char<Char>::value)>
157 inline 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 
162 template <typename Locale, typename S, typename OutputIt, typename... Args,
163           typename Char = char_t<S>,
164           FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
165                             detail::is_locale<Locale>::value&&
166                                 detail::is_exotic_char<Char>::value)>
167 inline 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 
176 template <typename OutputIt, typename Locale, typename S, typename... T,
177           typename Char = char_t<S>,
178           bool enable = detail::is_output_iterator<OutputIt, Char>::value &&
179                         detail::is_locale<Locale>::value &&
180                         detail::is_exotic_char<Char>::value>
181 inline 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 
188 template <typename OutputIt, typename Char, typename... Args,
189           FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
190                             detail::is_exotic_char<Char>::value)>
191 inline 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)
194     -> format_to_n_result<OutputIt> {
195   using traits = detail::fixed_buffer_traits;
196   auto buf = detail::iterator_buffer<OutputIt, Char, traits>(out, n);
197   detail::vformat_to(buf, format_str, args);
198   return {buf.out(), buf.count()};
199 }
200 
201 template <typename OutputIt, typename S, typename... T,
202           typename Char = char_t<S>,
203           FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
204                             detail::is_exotic_char<Char>::value)>
205 inline auto format_to_n(OutputIt out, size_t n, const S& fmt, T&&... args)
206     -> format_to_n_result<OutputIt> {
207   return vformat_to_n(out, n, detail::to_string_view(fmt),
208                       fmt::make_format_args<buffer_context<Char>>(args...));
209 }
210 
211 template <typename S, typename... T, typename Char = char_t<S>,
212           FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
213 inline auto formatted_size(const S& fmt, T&&... args) -> size_t {
214   auto buf = detail::counting_buffer<Char>();
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 
220 inline 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 
228 inline void vprint(wstring_view fmt, wformat_args args) {
229   vprint(stdout, fmt, args);
230 }
231 
232 template <typename... T>
233 void print(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
234   return vprint(f, wstring_view(fmt), fmt::make_wformat_args(args...));
235 }
236 
237 template <typename... T> void print(wformat_string<T...> fmt, T&&... args) {
238   return vprint(wstring_view(fmt), fmt::make_wformat_args(args...));
239 }
240 
241 template <typename... T>
242 void 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 
246 template <typename... T> void println(wformat_string<T...> fmt, T&&... args) {
247   return print(L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
248 }
249 
250 /**
251   Converts *value* to ``std::wstring`` using the default format for type *T*.
252  */
253 template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
254   return format(FMT_STRING(L"{}"), value);
255 }
256 FMT_END_EXPORT
257 FMT_END_NAMESPACE
258 
259 #endif  // FMT_XCHAR_H_
260