spdlog
Loading...
Searching...
No Matches
bundled/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
17namespace detail {
18template <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> using wformat_string = wstring_view;
33#else
34template <typename... Args>
36#endif
37
38template <> struct is_char<wchar_t> : std::true_type {};
39template <> struct is_char<detail::char8_type> : std::true_type {};
40template <> struct is_char<char16_t> : std::true_type {};
41template <> struct is_char<char32_t> : std::true_type {};
42
43template <typename... Args>
45 const Args&... args) {
46 return {args...};
47}
48
49inline namespace literals {
50constexpr 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
56constexpr detail::udl_arg<wchar_t> operator"" _a(const wchar_t* s, size_t) {
57 return {s};
58}
59#endif
60} // namespace literals
61
62template <typename It, typename Sentinel>
63auto join(It begin, Sentinel end, wstring_view sep)
65 return {begin, end, sep};
66}
67
68template <typename Range>
69auto join(Range&& range, wstring_view sep)
71 wchar_t> {
72 return join(std::begin(range), std::end(range), sep);
73}
74
75template <typename T>
80
81template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
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.
92template <typename S, typename... Args, typename Char = char_t<S>,
94auto 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
99template <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)>
102inline auto vformat(
103 const Locale& loc, const S& format_str,
106 return detail::vformat(loc, to_string_view(format_str), args);
107}
108
109template <typename Locale, typename S, typename... Args,
110 typename Char = char_t<S>,
113inline auto format(const Locale& loc, const S& format_str, Args&&... args)
115 return detail::vformat(loc, to_string_view(format_str),
116 fmt::make_args_checked<Args...>(format_str, args...));
117}
118
119template <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)>
122auto 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
130template <typename OutputIt, typename S, typename... Args,
131 typename Char = char_t<S>,
132 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
134inline 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
139template <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
149template <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&&
154inline 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
162template <
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&&
167inline 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
174template <typename OutputIt, typename Char, typename... Args,
175 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
177inline 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
187template <typename OutputIt, typename S, typename... Args,
188 typename Char = char_t<S>,
189 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
191inline 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
197template <typename S, typename... Args, typename Char = char_t<S>,
199inline 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
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
218template <typename... T>
220 return vprint(f, wstring_view(fmt), make_wformat_args(args...));
221}
222
223template <typename... T> void print(wformat_string<T...> fmt, T&&... args) {
224 return vprint(wstring_view(fmt), make_wformat_args(args...));
225}
226
227/**
228 Converts *value* to ``std::wstring`` using the default format for type *T*.
229 */
230template <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_
T begin(T... args)
auto vformat_to(OutputIt out, const S &format_str, basic_format_args< buffer_context< type_identity_t< Char > > > args) -> OutputIt
auto vformat(basic_string_view< Char > format_str, basic_format_args< buffer_context< type_identity_t< Char > > > args) -> std::basic_string< Char >
auto format_to(OutputIt out, const S &fmt, Args &&... args) -> OutputIt
basic_string_view< wchar_t > wstring_view
constexpr format_arg_store< wformat_context, Args... > make_wformat_args(const Args &... args)
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 >
auto format_to_n(OutputIt out, size_t n, const S &fmt, const Args &... args) -> format_to_n_result< OutputIt >
auto format(const S &format_str, Args &&... args) -> std::basic_string< Char >
auto formatted_size(const S &fmt, Args &&... args) -> size_t
auto to_wstring(const T &value) -> std::wstring
auto join(It begin, Sentinel end, wstring_view sep) -> join_view< It, Sentinel, wchar_t >
void print(std::FILE *f, wformat_string< T... > fmt, T &&... args)
void vprint(std::FILE *f, wstring_view fmt, wformat_args args)
buffer_context< wchar_t > wformat_context
OutputIt iterator
Definition core.h:1593
Definition core.h:749
void push_back(const T &value)
Definition core.h:820
auto data() FMT_NOEXCEPT -> T *
Definition core.h:797
Definition core.h:1120
typename detail::char_t_impl< S >::type char_t
Definition core.h:610
#define FMT_MODULE_EXPORT_BEGIN
Definition core.h:242
#define FMT_BEGIN_NAMESPACE
Definition core.h:235
#define FMT_ENABLE_IF(...)
Definition core.h:347
typename type_identity< T >::type type_identity_t
Definition core.h:330
T
Definition core.h:320
#define FMT_END_NAMESPACE
Definition core.h:230
#define FMT_MODULE_EXPORT_END
Definition core.h:243
T end(T... args)
auto system_error(int error_code, format_string< T... > fmt, T &&... args) -> std::system_error
Definition format.h:2243
#define FMT_STRING(s)
Definition format.h:2155
#define FMT_DEPRECATED
Definition format.h:120
auto to_string(const T &value) -> std::string
Definition format.h:2600
#define FMT_THROW(x)
Definition format.h:96
T fputws(T... args)
Definition args.h:19
decltype(std::end(std::declval< T & >())) sentinel_t
Definition format.h:327