Photon 1.0.0
Loading...
Searching...
No Matches
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
16namespace detail
17{
18 template <typename T>
20}
21
23
29
30#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
31// Workaround broken conversion on older gcc.
32template <typename... Args>
34inline auto runtime(wstring_view s) -> wstring_view
35{
36 return s;
37}
38#else
39template <typename... Args>
42{
43 return {{s}};
44}
45#endif
46
47template <>
48struct is_char<wchar_t> : std::true_type
49{
50};
51template <>
52struct is_char<detail::char8_type> : std::true_type
53{
54};
55template <>
56struct is_char<char16_t> : std::true_type
57{
58};
59template <>
60struct is_char<char32_t> : std::true_type
61{
62};
63
64template <typename... Args>
66 const Args&... args)
67{
68 return {args...};
69}
70
71inline namespace literals
72{
73#if FMT_USE_USER_DEFINED_LITERALS && !FMT_USE_NONTYPE_TEMPLATE_ARGS
74 constexpr detail::udl_arg<wchar_t> operator"" _a(const wchar_t * s, size_t)
75 {
76 return {s};
77 }
78#endif
79} // namespace literals
80
81template <typename It, typename Sentinel>
82auto join(It begin, Sentinel end, wstring_view sep)
84{
85 return {begin, end, sep};
86}
87
88template <typename Range>
89auto join(Range&& range, wstring_view sep)
91{
92 return join(std::begin(range), std::end(range), sep);
93}
94
95template <typename T>
96auto join(std::initializer_list<T> list, wstring_view sep)
98{
99 return join(std::begin(list), std::end(list), sep);
100}
101
102template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
105 -> std::basic_string<Char>
106{
108 detail::vformat_to(buffer, format_str, args);
109 return to_string(buffer);
110}
111
112template <typename... T>
113auto format(wformat_string<T...> fmt, T&&... args) -> std::wstring
114{
115 return vformat(fmt::wstring_view(fmt), fmt::make_wformat_args(args...));
116}
117
118// Pass char_t as a default template parameter instead of using
119// std::basic_string<char_t<S>> to reduce the symbol size.
120template <typename S, typename... Args, typename Char = char_t<S>, FMT_ENABLE_IF(!std::is_same<Char, char>::value && !std::is_same<Char, wchar_t>::value)>
121auto format(const S& format_str, Args&&... args) -> std::basic_string<Char>
122{
123 return vformat(detail::to_string_view(format_str),
124 fmt::make_format_args<buffer_context<Char>>(args...));
125}
126
127template <typename Locale, typename S, typename Char = char_t<S>, FMT_ENABLE_IF(detail::is_locale<Locale>::value&& detail::is_exotic_char<Char>::value)>
128inline auto vformat(
129 const Locale& loc, const S& format_str, basic_format_args<buffer_context<type_identity_t<Char>>> args)
130 -> std::basic_string<Char>
131{
132 return detail::vformat(loc, detail::to_string_view(format_str), args);
133}
134
135template <typename Locale, typename S, typename... Args, typename Char = char_t<S>, FMT_ENABLE_IF(detail::is_locale<Locale>::value&& detail::is_exotic_char<Char>::value)>
136inline auto format(const Locale& loc, const S& format_str, Args&&... args)
137 -> std::basic_string<Char>
138{
139 return detail::vformat(loc, detail::to_string_view(format_str),
140 fmt::make_format_args<buffer_context<Char>>(args...));
141}
142
143template <typename OutputIt, typename S, typename Char = char_t<S>, FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&& detail::is_exotic_char<Char>::value)>
144auto vformat_to(OutputIt out, const S& format_str, basic_format_args<buffer_context<type_identity_t<Char>>> args)
145 -> OutputIt
146{
147 auto&& buf = detail::get_buffer<Char>(out);
148 detail::vformat_to(buf, detail::to_string_view(format_str), args);
149 return detail::get_iterator(buf);
150}
151
152template <typename OutputIt, typename S, typename... Args, typename Char = char_t<S>, FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&& detail::is_exotic_char<Char>::value)>
153inline auto format_to(OutputIt out, const S& fmt, Args&&... args) -> OutputIt
154{
155 return vformat_to(out, detail::to_string_view(fmt),
156 fmt::make_format_args<buffer_context<Char>>(args...));
157}
158
159template <typename Locale, typename S, typename OutputIt, typename... Args, typename Char = char_t<S>, FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&& detail::is_locale<Locale>::value&& detail::is_exotic_char<Char>::value)>
160inline auto vformat_to(
161 OutputIt out, const Locale& loc, const S& format_str, basic_format_args<buffer_context<type_identity_t<Char>>> args) -> OutputIt
162{
163 auto&& buf = detail::get_buffer<Char>(out);
164 vformat_to(buf, detail::to_string_view(format_str), args,
165 detail::locale_ref(loc));
166 return detail::get_iterator(buf);
167}
168
169template <
170 typename OutputIt,
171 typename Locale,
172 typename S,
173 typename... Args,
174 typename Char = char_t<S>,
175 bool enable = detail::is_output_iterator<OutputIt, Char>::value &&
176 detail::is_locale<Locale>::value && detail::is_exotic_char<Char>::value>
177inline auto format_to(OutputIt out, const Locale& loc, const S& format_str, Args&&... args) ->
178 typename std::enable_if<enable, OutputIt>::type
179{
180 return vformat_to(out, loc, to_string_view(format_str),
181 fmt::make_format_args<buffer_context<Char>>(args...));
182}
183
184template <typename OutputIt, typename Char, typename... Args, FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&& detail::is_exotic_char<Char>::value)>
185inline auto vformat_to_n(
188{
189 detail::iterator_buffer<OutputIt, Char, detail::fixed_buffer_traits> buf(out,
190 n);
191 detail::vformat_to(buf, format_str, args);
192 return {buf.out(), buf.count()};
193}
194
195template <typename OutputIt, typename S, typename... Args, typename Char = char_t<S>, FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&& detail::is_exotic_char<Char>::value)>
196inline auto format_to_n(OutputIt out, size_t n, const S& fmt, const Args&... args) -> format_to_n_result<OutputIt>
197{
198 return vformat_to_n(out, n, detail::to_string_view(fmt),
199 fmt::make_format_args<buffer_context<Char>>(args...));
200}
201
202template <typename S, typename... Args, typename Char = char_t<S>, FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
203inline auto formatted_size(const S& fmt, Args&&... args) -> size_t
204{
205 detail::counting_buffer<Char> buf;
206 detail::vformat_to(buf, detail::to_string_view(fmt),
207 fmt::make_format_args<buffer_context<Char>>(args...));
208 return buf.count();
209}
210
211inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args)
212{
214 detail::vformat_to(buffer, fmt, args);
215 buffer.push_back(L'\0');
216 if (std::fputws(buffer.data(), f) == -1)
217 FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
218}
219
221{
222 vprint(stdout, fmt, args);
223}
224
225template <typename... T>
226void print(std::FILE* f, wformat_string<T...> fmt, T&&... args)
227{
228 return vprint(f, wstring_view(fmt), fmt::make_wformat_args(args...));
229}
230
231template <typename... T>
233{
234 return vprint(wstring_view(fmt), fmt::make_wformat_args(args...));
235}
236
240template <typename T>
241inline auto to_wstring(const T& value) -> std::wstring
242{
243 return format(FMT_STRING(L"{}"), value);
244}
247
248#endif // FMT_XCHAR_H_
auto format(wformat_string< T... > fmt, T &&... args) -> std::wstring
Definition xchar.h:113
auto vformat_to(OutputIt out, const S &format_str, basic_format_args< buffer_context< type_identity_t< Char > > > args) -> OutputIt
Definition xchar.h:144
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:103
auto format_to(OutputIt out, const S &fmt, Args &&... args) -> OutputIt
Definition xchar.h:153
basic_string_view< wchar_t > wstring_view
Definition xchar.h:24
basic_format_string< wchar_t, type_identity_t< Args >... > wformat_string
Definition xchar.h:40
constexpr format_arg_store< wformat_context, Args... > make_wformat_args(const Args &... args)
Definition xchar.h:65
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:185
auto format_to_n(OutputIt out, size_t n, const S &fmt, const Args &... args) -> format_to_n_result< OutputIt >
Definition xchar.h:196
basic_format_parse_context< wchar_t > wformat_parse_context
Definition xchar.h:25
auto formatted_size(const S &fmt, Args &&... args) -> size_t
Definition xchar.h:203
auto to_wstring(const T &value) -> std::wstring
Definition xchar.h:241
auto join(It begin, Sentinel end, wstring_view sep) -> join_view< It, Sentinel, wchar_t >
Definition xchar.h:82
void print(std::FILE *f, wformat_string< T... > fmt, T &&... args)
Definition xchar.h:226
void vprint(std::FILE *f, wstring_view fmt, wformat_args args)
Definition xchar.h:211
buffer_context< wchar_t > wformat_context
Definition xchar.h:26
auto runtime(wstring_view s) -> basic_runtime< wchar_t >
Definition xchar.h:41
basic_format_args< wformat_context > wformat_args
Definition xchar.h:27
Definition core.h:2524
Definition core.h:2338
Definition core.h:770
Definition core.h:4010
Definition core.h:483
Definition core.h:1032
FMT_CONSTEXPR20 void push_back(const T &value)
Definition core.h:1137
FMT_CONSTEXPR auto data() noexcept -> T *
Definition core.h:1102
Definition core.h:2442
Definition core.h:1598
std::integral_constant< bool, B > bool_constant
Definition core.h:306
typename detail::char_t_impl< S >::type char_t
Definition core.h:759
#define FMT_MODULE_EXPORT_BEGIN
Definition core.h:226
#define FMT_BEGIN_NAMESPACE
Definition core.h:214
#define FMT_ENABLE_IF(...)
Definition core.h:364
FMT_INLINE auto to_string_view(const Char *s) -> basic_string_view< Char >
Definition core.h:630
typename type_identity< T >::type type_identity_t
Definition core.h:319
#define FMT_END_NAMESPACE
Definition core.h:219
#define FMT_MODULE_EXPORT_END
Definition core.h:227
#define out
Definition encodings.cpp:5
auto system_error(int error_code, format_string< T... > fmt, T &&... args) -> std::system_error
Definition format.h:4326
#define FMT_STRING(s)
Definition format.h:2155
auto to_string(const T &value) -> std::string
Definition format.h:4827
#define FMT_THROW(x)
Definition format.h:99
Definition args.h:20
decltype(std::end(std::declval< T & >())) sentinel_t
Definition format.h:556
bool_constant<!std::is_same< T, char >::value > is_exotic_char
Definition xchar.h:19
Definition bin_to_hex.h:111
Definition xchar.h:72
Definition core.h:4003
Definition core.h:4128
Definition core.h:609
Definition format.h:4709
s
Definition tag_strings.h:47