WPILibC++ 2023.4.3-108-ge5452e3
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 {
17template <typename T>
19}
20
22
28
29#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
30// Workaround broken conversion on older gcc.
31template <typename... Args> using wformat_string = wstring_view;
32inline auto runtime(wstring_view s) -> wstring_view { return s; }
33#else
34template <typename... Args>
36inline auto runtime(wstring_view s) -> basic_runtime<wchar_t> { return {{s}}; }
37#endif
38
39template <> struct is_char<wchar_t> : std::true_type {};
40template <> struct is_char<detail::char8_type> : std::true_type {};
41template <> struct is_char<char16_t> : std::true_type {};
42template <> struct is_char<char32_t> : std::true_type {};
43
44template <typename... Args>
46 const Args&... args) {
47 return {args...};
48}
49
50inline namespace literals {
51#if FMT_USE_USER_DEFINED_LITERALS && !FMT_USE_NONTYPE_TEMPLATE_ARGS
52constexpr detail::udl_arg<wchar_t> operator"" _a(const wchar_t* s, size_t) {
53 return {s};
54}
55#endif
56} // namespace literals
57
58template <typename It, typename Sentinel>
59auto join(It begin, Sentinel end, wstring_view sep)
61 return {begin, end, sep};
62}
63
64template <typename Range>
65auto join(Range&& range, wstring_view sep)
67 wchar_t> {
68 return join(std::begin(range), std::end(range), sep);
69}
70
71template <typename T>
72auto join(std::initializer_list<T> list, wstring_view sep)
74 return join(std::begin(list), std::end(list), sep);
75}
76
77template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
80 -> std::basic_string<Char> {
82 detail::vformat_to(buffer, format_str, args);
83 return to_string(buffer);
84}
85
86template <typename... T>
87auto format(wformat_string<T...> fmt, T&&... args) -> std::wstring {
89}
90
91// Pass char_t as a default template parameter instead of using
92// std::basic_string<char_t<S>> to reduce the symbol size.
93template <typename S, typename... Args, typename Char = char_t<S>,
94 FMT_ENABLE_IF(!std::is_same<Char, char>::value &&
95 !std::is_same<Char, wchar_t>::value)>
96auto format(const S& format_str, Args&&... args) -> std::basic_string<Char> {
97 return vformat(detail::to_string_view(format_str),
99}
100
101template <typename Locale, typename S, typename Char = char_t<S>,
102 FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
103 detail::is_exotic_char<Char>::value)>
104inline auto vformat(
105 const Locale& loc, const S& format_str,
107 -> std::basic_string<Char> {
108 return detail::vformat(loc, detail::to_string_view(format_str), args);
109}
110
111template <typename Locale, typename S, typename... Args,
112 typename Char = char_t<S>,
113 FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
115inline auto format(const Locale& loc, const S& format_str, Args&&... args)
116 -> std::basic_string<Char> {
117 return detail::vformat(loc, detail::to_string_view(format_str),
119}
120
121template <typename OutputIt, typename S, typename Char = char_t<S>,
122 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
123 detail::is_exotic_char<Char>::value)>
124auto vformat_to(OutputIt out, const S& format_str,
126 -> OutputIt {
127 auto&& buf = detail::get_buffer<Char>(out);
128 detail::vformat_to(buf, detail::to_string_view(format_str), args);
129 return detail::get_iterator(buf);
130}
131
132template <typename OutputIt, typename S, typename... Args,
133 typename Char = char_t<S>,
134 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
136inline auto format_to(OutputIt out, const S& fmt, Args&&... args) -> OutputIt {
137 return vformat_to(out, detail::to_string_view(fmt),
139}
140
141template <typename Locale, typename S, typename OutputIt, typename... Args,
142 typename Char = char_t<S>,
143 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
144 detail::is_locale<Locale>::value&&
146inline auto vformat_to(
147 OutputIt out, const Locale& loc, const S& format_str,
149 auto&& buf = detail::get_buffer<Char>(out);
150 vformat_to(buf, detail::to_string_view(format_str), args,
151 detail::locale_ref(loc));
152 return detail::get_iterator(buf);
153}
154
155template <
156 typename OutputIt, typename Locale, typename S, typename... Args,
157 typename Char = char_t<S>,
158 bool enable = detail::is_output_iterator<OutputIt, Char>::value&&
159 detail::is_locale<Locale>::value&& detail::is_exotic_char<Char>::value>
160inline auto format_to(OutputIt out, const Locale& loc, const S& format_str,
161 Args&&... args) ->
163 return vformat_to(out, loc, to_string_view(format_str),
165}
166
167template <typename OutputIt, typename Char, typename... Args,
168 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
170inline auto vformat_to_n(
171 OutputIt out, size_t n, basic_string_view<Char> format_str,
174 detail::iterator_buffer<OutputIt, Char, detail::fixed_buffer_traits> buf(out,
175 n);
176 detail::vformat_to(buf, format_str, args);
177 return {buf.out(), buf.count()};
178}
179
180template <typename OutputIt, typename S, typename... Args,
181 typename Char = char_t<S>,
182 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
184inline auto format_to_n(OutputIt out, size_t n, const S& fmt,
185 const Args&... args) -> format_to_n_result<OutputIt> {
186 return vformat_to_n(out, n, detail::to_string_view(fmt),
188}
189
190template <typename S, typename... Args, typename Char = char_t<S>,
192inline auto formatted_size(const S& fmt, Args&&... args) -> size_t {
193 detail::counting_buffer<Char> buf;
196 return buf.count();
197}
198
199inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) {
201 detail::vformat_to(buffer, fmt, args);
202 buffer.push_back(L'\0');
203 std::fputws(buffer.data(), f);
204}
205
206inline void vprint(wstring_view fmt, wformat_args args) {
207 vprint(stdout, fmt, args);
208}
209
210template <typename... T>
211void print(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
212 return vprint(f, wstring_view(fmt), fmt::make_wformat_args(args...));
213}
214
215template <typename... T> void print(wformat_string<T...> fmt, T&&... args) {
216 return vprint(wstring_view(fmt), fmt::make_wformat_args(args...));
217}
218
219/**
220 Converts *value* to ``std::wstring`` using the default format for type *T*.
221 */
222template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
223 return format(FMT_STRING(L"{}"), value);
224}
227
228#endif // FMT_XCHAR_H_
\rst A view of a collection of formatting arguments.
Definition: core.h:1955
Definition: core.h:1795
\rst Parsing context consisting of a format string range being parsed and an argument counter for aut...
Definition: core.h:654
A compile-time format string.
Definition: core.h:3147
An implementation of std::basic_string_view for pre-C++17.
Definition: core.h:430
\rst A contiguous memory buffer with an optional growing ability.
Definition: core.h:862
FMT_CONSTEXPR20 void push_back(const T &value)
Definition: core.h:931
FMT_CONSTEXPR auto data() noexcept -> T *
Returns a pointer to the buffer data.
Definition: core.h:908
\rst An array of references to arguments.
Definition: core.h:1877
Definition: core.h:1240
std::integral_constant< bool, B > bool_constant
Definition: core.h:301
typename detail::char_t_impl< S >::type char_t
String's character type.
Definition: core.h:644
#define FMT_MODULE_EXPORT_BEGIN
Definition: core.h:224
constexpr auto make_format_args(Args &&... args) -> format_arg_store< Context, remove_cvref_t< Args >... >
\rst Constructs a ~fmtformat_arg_store object that contains references to arguments and can be implic...
Definition: core.h:1923
type
Definition: core.h:575
#define FMT_BEGIN_NAMESPACE
Definition: core.h:214
#define FMT_ENABLE_IF(...)
Definition: core.h:335
auto get_iterator(Buffer &buf) -> decltype(buf.out())
Definition: core.h:1115
FMT_INLINE auto to_string_view(const Char *s) -> basic_string_view< Char >
Definition: core.h:536
typename type_identity< T >::type type_identity_t
Definition: core.h:309
#define FMT_END_NAMESPACE
Definition: core.h:217
#define FMT_MODULE_EXPORT_END
Definition: core.h:225
#define FMT_STRING(s)
\rst Constructs a compile-time format string from a string literal s.
Definition: format.h:1780
static EIGEN_DEPRECATED const end_t end
Definition: IndexedViewHelper.h:181
Definition: format-inl.h:32
decltype(std::end(std::declval< T & >())) sentinel_t
Definition: format.h:476
bool_constant<!std::is_same< T, char >::value > is_exotic_char
Definition: xchar.h:18
char8_type
Definition: format.h:577
Definition: xchar.h:50
std::string to_string(const T &t)
Definition: base.h:93
cubed< length::millimeter > L
Definition: volume.h:49
Definition: core.h:3144
Definition: core.h:3245
Specifies if T is a character type.
Definition: core.h:523
Definition: format.h:3937
#define S(label, offset, message)
Definition: Errors.h:119
auto format(wformat_string< T... > fmt, T &&... args) -> std::wstring
Definition: xchar.h:87
auto vformat_to(OutputIt out, const S &format_str, basic_format_args< buffer_context< type_identity_t< Char > > > args) -> OutputIt
Definition: xchar.h:124
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:78
auto format_to(OutputIt out, const S &fmt, Args &&... args) -> OutputIt
Definition: xchar.h:136
basic_string_view< wchar_t > wstring_view
Definition: xchar.h:23
constexpr format_arg_store< wformat_context, Args... > make_wformat_args(const Args &... args)
Definition: xchar.h:45
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:170
auto format_to_n(OutputIt out, size_t n, const S &fmt, const Args &... args) -> format_to_n_result< OutputIt >
Definition: xchar.h:184
auto formatted_size(const S &fmt, Args &&... args) -> size_t
Definition: xchar.h:192
auto to_wstring(const T &value) -> std::wstring
Converts value to std::wstring using the default format for type T.
Definition: xchar.h:222
auto join(It begin, Sentinel end, wstring_view sep) -> join_view< It, Sentinel, wchar_t >
Definition: xchar.h:59
void print(std::FILE *f, wformat_string< T... > fmt, T &&... args)
Definition: xchar.h:211
void vprint(std::FILE *f, wstring_view fmt, wformat_args args)
Definition: xchar.h:199
buffer_context< wchar_t > wformat_context
Definition: xchar.h:25
auto runtime(wstring_view s) -> basic_runtime< wchar_t >
Definition: xchar.h:36