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_WCHAR_H_
9 #define FMT_WCHAR_H_
10 
11 #include <cwchar>
12 #include <tuple>
13 
14 #include "format.h"
15 
17 namespace detail {
18 template <typename T>
20 }
21 
23 
29 
30 #if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
31 // Workaround broken conversion on older gcc.
32 template <typename... Args> using wformat_string = wstring_view;
33 #else
34 template <typename... Args>
36 #endif
37 
38 template <> struct is_char<wchar_t> : std::true_type {};
39 template <> struct is_char<detail::char8_type> : std::true_type {};
40 template <> struct is_char<char16_t> : std::true_type {};
41 template <> struct is_char<char32_t> : std::true_type {};
42 
43 template <typename... Args>
45  const Args&... args) {
46  return {args...};
47 }
48 
49 inline namespace literals {
50 constexpr auto operator"" _format(const wchar_t* s, size_t n)
51  -> detail::udl_formatter<wchar_t> {
52  return {{s, n}};
53 }
54 
55 #if FMT_USE_USER_DEFINED_LITERALS && !FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
56 constexpr detail::udl_arg<wchar_t> operator"" _a(const wchar_t* s, size_t) {
57  return {s};
58 }
59 #endif
60 } // namespace literals
61 
62 template <typename It, typename Sentinel>
63 auto join(It begin, Sentinel end, wstring_view sep)
65  return {begin, end, sep};
66 }
67 
68 template <typename Range>
69 auto join(Range&& range, wstring_view sep)
71  wchar_t> {
72  return join(std::begin(range), std::end(range), sep);
73 }
74 
75 template <typename T>
76 auto join(std::initializer_list<T> list, wstring_view sep)
78  return join(std::begin(list), std::end(list), sep);
79 }
80 
81 template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
84  -> std::basic_string<Char> {
86  detail::vformat_to(buffer, format_str, args);
87  return to_string(buffer);
88 }
89 
90 // Pass char_t as a default template parameter instead of using
91 // std::basic_string<char_t<S>> to reduce the symbol size.
92 template <typename S, typename... Args, typename Char = char_t<S>,
93  FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
94 auto format(const S& format_str, Args&&... args) -> std::basic_string<Char> {
95  const auto& vargs = fmt::make_args_checked<Args...>(format_str, args...);
96  return vformat(to_string_view(format_str), vargs);
97 }
98 
99 template <typename Locale, typename S, typename Char = char_t<S>,
100  FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
101  detail::is_exotic_char<Char>::value)>
102 inline auto vformat(
103  const Locale& loc, const S& format_str,
105  -> std::basic_string<Char> {
106  return detail::vformat(loc, to_string_view(format_str), args);
107 }
108 
109 template <typename Locale, typename S, typename... Args,
110  typename Char = char_t<S>,
113 inline auto format(const Locale& loc, const S& format_str, Args&&... args)
114  -> std::basic_string<Char> {
115  return detail::vformat(loc, to_string_view(format_str),
116  fmt::make_args_checked<Args...>(format_str, args...));
117 }
118 
119 template <typename OutputIt, typename S, typename Char = char_t<S>,
120  FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
121  detail::is_exotic_char<Char>::value)>
122 auto vformat_to(OutputIt out, const S& format_str,
124  -> OutputIt {
125  auto&& buf = detail::get_buffer<Char>(out);
126  detail::vformat_to(buf, to_string_view(format_str), args);
127  return detail::get_iterator(buf);
128 }
129 
130 template <typename OutputIt, typename S, typename... Args,
131  typename Char = char_t<S>,
132  FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
134 inline auto format_to(OutputIt out, const S& fmt, Args&&... args) -> OutputIt {
135  const auto& vargs = fmt::make_args_checked<Args...>(fmt, args...);
136  return vformat_to(out, to_string_view(fmt), vargs);
137 }
138 
139 template <typename S, typename... Args, typename Char, size_t SIZE,
140  typename Allocator, FMT_ENABLE_IF(detail::is_string<S>::value)>
142  const S& format_str, Args&&... args) ->
144  const auto& vargs = fmt::make_args_checked<Args...>(format_str, args...);
145  detail::vformat_to(buf, to_string_view(format_str), vargs);
146  return detail::buffer_appender<Char>(buf);
147 }
148 
149 template <typename Locale, typename S, typename OutputIt, typename... Args,
150  typename Char = char_t<S>,
151  FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
154 inline auto vformat_to(
155  OutputIt out, const Locale& loc, const S& format_str,
157  auto&& buf = detail::get_buffer<Char>(out);
158  vformat_to(buf, to_string_view(format_str), args, detail::locale_ref(loc));
159  return detail::get_iterator(buf);
160 }
161 
162 template <
163  typename OutputIt, typename Locale, typename S, typename... Args,
164  typename Char = char_t<S>,
165  bool enable = detail::is_output_iterator<OutputIt, Char>::value&&
167 inline auto format_to(OutputIt out, const Locale& loc, const S& format_str,
168  Args&&... args) ->
170  const auto& vargs = fmt::make_args_checked<Args...>(format_str, args...);
171  return vformat_to(out, loc, to_string_view(format_str), vargs);
172 }
173 
174 template <typename OutputIt, typename Char, typename... Args,
175  FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
177 inline auto vformat_to_n(
178  OutputIt out, size_t n, basic_string_view<Char> format_str,
181  detail::iterator_buffer<OutputIt, Char, detail::fixed_buffer_traits> buf(out,
182  n);
183  detail::vformat_to(buf, format_str, args);
184  return {buf.out(), buf.count()};
185 }
186 
187 template <typename OutputIt, typename S, typename... Args,
188  typename Char = char_t<S>,
189  FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
191 inline auto format_to_n(OutputIt out, size_t n, const S& fmt,
192  const Args&... args) -> format_to_n_result<OutputIt> {
193  const auto& vargs = fmt::make_args_checked<Args...>(fmt, args...);
194  return vformat_to_n(out, n, to_string_view(fmt), vargs);
195 }
196 
197 template <typename S, typename... Args, typename Char = char_t<S>,
199 inline auto formatted_size(const S& fmt, Args&&... args) -> size_t {
200  detail::counting_buffer<Char> buf;
201  const auto& vargs = fmt::make_args_checked<Args...>(fmt, args...);
202  detail::vformat_to(buf, to_string_view(fmt), vargs);
203  return buf.count();
204 }
205 
206 inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) {
208  detail::vformat_to(buffer, fmt, args);
209  buffer.push_back(L'\0');
210  if (std::fputws(buffer.data(), f) == -1)
211  FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
212 }
213 
215  vprint(stdout, fmt, args);
216 }
217 
218 template <typename... T>
219 void print(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
220  return vprint(f, wstring_view(fmt), make_wformat_args(args...));
221 }
222 
223 template <typename... T> void print(wformat_string<T...> fmt, T&&... args) {
224  return vprint(wstring_view(fmt), make_wformat_args(args...));
225 }
226 
230 template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
231  return format(FMT_STRING(L"{}"), value);
232 }
235 
236 #endif // FMT_WCHAR_H_
#define FMT_MODULE_EXPORT_END
Definition: core.h:242
#define FMT_STRING(s)
Definition: format.h:2152
#define FMT_ENABLE_IF(...)
Definition: core.h:341
basic_string_view< wchar_t > wstring_view
Definition: xchar.h:24
typename detail::char_t_impl< S >::type char_t
Definition: core.h:621
#define FMT_DEPRECATED
Definition: format.h:120
auto join(It begin, Sentinel end, wstring_view sep) -> join_view< It, Sentinel, wchar_t >
Definition: xchar.h:63
auto formatted_size(const S &fmt, Args &&... args) -> size_t
Definition: xchar.h:199
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:82
XmlRpcServer s
char8_type
Definition: format.h:425
auto format_to(OutputIt out, const S &fmt, Args &&... args) -> OutputIt
Definition: xchar.h:134
decltype(std::end(std::declval< T & >())) sentinel_t
Definition: format.h:327
bool_constant<!std::is_same< T, char >::value > is_exotic_char
Definition: xchar.h:19
FMT_INLINE auto make_args_checked(const S &fmt, const remove_reference_t< Args > &... args) -> format_arg_store< buffer_context< Char >, remove_reference_t< Args >... >
Definition: format.h:769
buffer_context< wchar_t > wformat_context
Definition: xchar.h:26
#define S(x)
Definition: luac.c:667
#define FMT_END_NAMESPACE
Definition: core.h:229
#define FMT_THROW(x)
Definition: format.h:93
type
Definition: core.h:1059
auto format_to_n(OutputIt out, size_t n, const S &fmt, const Args &... args) -> format_to_n_result< OutputIt >
Definition: xchar.h:191
auto get_iterator(Buffer &buf) -> decltype(buf.out())
Definition: core.h:965
Definition: core.h:760
void vprint(std::FILE *f, wstring_view fmt, wformat_args args)
Definition: xchar.h:206
FMT_INLINE auto to_string_view(const Char *s) -> basic_string_view< Char >
Definition: core.h:545
Definition: args.h:19
FMT_FUNC void print(std::FILE *f, string_view text)
Definition: format-inl.h:2579
integral_constant< bool, true > true_type
Definition: span.hpp:611
auto vformat_to(OutputIt out, const S &format_str, basic_format_args< buffer_context< type_identity_t< Char >>> args) -> OutputIt
Definition: xchar.h:122
constexpr format_arg_store< wformat_context, Args... > make_wformat_args(const Args &... args)
Definition: xchar.h:44
typename type_identity< T >::type type_identity_t
Definition: core.h:329
std::enable_if_t< all< Args... >::value, enable_t > enable
Definition: sol.hpp:2244
OutputIt iterator
Definition: core.h:1604
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:177
Definition: core.h:1131
#define FMT_BEGIN_NAMESPACE
Definition: core.h:234
auto system_error(int error_code, format_string< T... > fmt, T &&... args) -> std::system_error
Definition: format.h:2240
auto format(const S &format_str, Args &&... args) -> std::basic_string< Char >
Definition: xchar.h:94
#define FMT_MODULE_EXPORT_BEGIN
Definition: core.h:241
std::integral_constant< bool, B > bool_constant
Definition: core.h:323
Definition: core.h:525
auto to_wstring(const T &value) -> std::wstring
Definition: xchar.h:230


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Jun 19 2023 03:12:53