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 
20 namespace detail {
21 
22 template <typename T>
24 
25 inline 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.
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>
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)
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)
83  return {begin, end, sep};
84 }
85 
86 template <typename Range>
87 auto join(Range&& range, wstring_view sep)
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)
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)>
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 {
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),
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,
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&&
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),
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,
147  -> OutputIt {
148  auto&& buf = detail::get_buffer<Char>(out);
150  return detail::get_iterator(buf, out);
151 }
152 
153 template <typename OutputIt, typename S, typename... T,
154  typename Char = char_t<S>,
157 inline auto format_to(OutputIt out, const S& fmt, T&&... args) -> OutputIt {
158  return vformat_to(out, detail::to_string_view(fmt),
160 }
161 
162 template <typename Locale, typename S, typename OutputIt, typename... Args,
163  typename Char = char_t<S>,
165  detail::is_locale<Locale>::value&&
167 inline auto vformat_to(
168  OutputIt out, const Locale& loc, const S& format_str,
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>,
179  detail::is_locale<Locale>::value &&
181 inline auto format_to(OutputIt out, const Locale& loc, const S& format_str,
182  T&&... args) ->
184  return vformat_to(out, loc, detail::to_string_view(format_str),
186 }
187 
188 template <typename OutputIt, typename Char, typename... Args,
191 inline auto vformat_to_n(
192  OutputIt out, size_t n, basic_string_view<Char> format_str,
195  using traits = detail::fixed_buffer_traits;
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>,
205 inline 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),
209 }
210 
211 template <typename S, typename... T, typename Char = char_t<S>,
213 inline auto formatted_size(const S& fmt, T&&... args) -> size_t {
214  auto buf = detail::counting_buffer<Char>();
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 
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) {
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 
253 template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
254  return format(FMT_STRING(L"{}"), value);
255 }
258 
259 #endif // FMT_XCHAR_H_
print
void print(std::FILE *f, wformat_string< T... > fmt, T &&... args)
Definition: xchar.h:233
join
auto join(It begin, Sentinel end, wstring_view sep) -> join_view< It, Sentinel, wchar_t >
Definition: xchar.h:81
format_to_n
auto format_to_n(OutputIt out, size_t n, const S &fmt, T &&... args) -> format_to_n_result< OutputIt >
Definition: xchar.h:205
format_arg_store
Definition: core.h:1808
detail::to_string_view
FMT_INLINE auto to_string_view(const Char *s) -> basic_string_view< Char >
Definition: core.h:534
detail::counting_buffer
Definition: core.h:1038
basic_memory_buffer< wchar_t >
detail::buffer< wchar_t >
vformat
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:100
is_char
Definition: core.h:522
backward::ColorMode::type
type
Definition: backward.hpp:3600
loc_value
Definition: format.h:1045
basic_string_view
Definition: core.h:415
s
XmlRpcServer s
literals
Definition: xchar.h:71
format.h
format_specs
Definition: core.h:2076
format_to
auto format_to(OutputIt out, const S &fmt, T &&... args) -> OutputIt
Definition: xchar.h:157
detail::fixed_buffer_traits
Definition: core.h:907
vformat_to
auto vformat_to(OutputIt out, const S &format_str, basic_format_args< buffer_context< type_identity_t< Char >>> args) -> OutputIt
Definition: xchar.h:145
detail::write_loc
FMT_FUNC auto write_loc(appender out, loc_value value, const format_specs<> &specs, locale_ref loc) -> bool
Definition: format-inl.h:115
format_to_n_result
Definition: core.h:2866
detail
Definition: args.h:19
runtime_format_string
Definition: core.h:2763
runtime
auto runtime(wstring_view s) -> runtime_format_string< wchar_t >
Definition: xchar.h:55
nonstd::span_lite::std11::true_type
integral_constant< bool, true > true_type
Definition: span.hpp:656
println
void println(std::FILE *f, wformat_string< T... > fmt, T &&... args)
Definition: xchar.h:242
detail::get_iterator
FMT_INLINE auto get_iterator(Buf &buf, OutputIt) -> decltype(buf.out())
Definition: core.h:1146
f
f
basic_format_args
Definition: core.h:1081
FMT_STRING
#define FMT_STRING(s)
Definition: format.h:1848
basic_format_string
Definition: core.h:2768
wformat_context
buffer_context< wchar_t > wformat_context
Definition: xchar.h:44
format
auto format(wformat_string< T... > fmt, T &&... args) -> std::wstring
Definition: xchar.h:109
FMT_ENABLE_IF
#define FMT_ENABLE_IF(...)
Definition: core.h:303
formatted_size
auto formatted_size(const S &fmt, T &&... args) -> size_t
Definition: xchar.h:213
FMT_END_NAMESPACE
#define FMT_END_NAMESPACE
Definition: core.h:180
detail::iterator_buffer
Definition: core.h:924
FMT_END_EXPORT
#define FMT_END_EXPORT
Definition: core.h:188
detail::value
Definition: core.h:1257
make_format_args
constexpr auto make_format_args(T &... args) -> format_arg_store< Context, remove_cvref_t< T >... >
Definition: core.h:1858
detail::is_output_iterator
Definition: core.h:1538
detail::loc_writer
Definition: format.h:2167
sol::meta::enable
std::enable_if_t< all< Args... >::value, enable_t > enable
Definition: sol.hpp:2244
type_identity_t
typename type_identity< T >::type type_identity_t
Definition: core.h:284
make_wformat_args
constexpr auto make_wformat_args(const T &... args) -> format_arg_store< wformat_context, T... >
Definition: xchar.h:66
basic_format_parse_context
Definition: core.h:674
detail::vformat
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:3903
vprint
void vprint(std::FILE *f, wstring_view fmt, wformat_args args)
Definition: xchar.h:220
wstring_view
basic_string_view< wchar_t > wstring_view
Definition: xchar.h:42
vformat_to_n
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:191
bool_constant
std::integral_constant< bool, B > bool_constant
Definition: core.h:274
system_error
auto system_error(int error_code, format_string< T... > fmt, T &&... args) -> std::system_error
Definition: format.h:3941
join_view
Definition: format.h:4230
detail::vformat_to
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:444
FMT_BEGIN_EXPORT
#define FMT_BEGIN_EXPORT
Definition: core.h:187
detail::locale_ref
Definition: core.h:1553
mcap::internal::to_string
std::string to_string(const std::string &arg)
Definition: internal.hpp:35
FMT_BEGIN_NAMESPACE
#define FMT_BEGIN_NAMESPACE
Definition: core.h:177
basic_format_context
Definition: core.h:1739
to_wstring
auto to_wstring(const T &value) -> std::wstring
Definition: xchar.h:253
wmemory_buffer
basic_memory_buffer< wchar_t > wmemory_buffer
Definition: xchar.h:46
char_t
typename detail::char_t_impl< S >::type char_t
Definition: core.h:664
S
#define S(x)
Definition: luac.c:667
detail::char8_type
char8_type
Definition: format.h:599
FMT_THROW
#define FMT_THROW(x)
Definition: format.h:130
detail::sentinel_t
decltype(std::end(std::declval< T & >())) sentinel_t
Definition: format.h:512
udp_client.args
args
Definition: udp_client.py:12
detail::is_exotic_char
bool_constant<!std::is_same< T, char >::value > is_exotic_char
Definition: xchar.h:23


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Aug 11 2024 02:24:26