Agora  1.2.0
Agora project
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 #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>
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>,
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>,
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>,
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>,
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,
177 inline auto vformat_to_n(
178  OutputIt out, size_t n, basic_string_view<Char> format_str,
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>,
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 {
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) {
207  wmemory_buffer buffer;
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 
214 inline void vprint(wstring_view fmt, wformat_args args) {
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), 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), 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_XCHAR_H_
spdlog::level::to_string_view
const SPDLOG_INLINE string_view_t & to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
Definition: common-inl.h:23
fmt::v8::char_t
typename detail::char_t_impl< S >::type char_t
Definition: core.h:623
fmt::v8::basic_format_parse_context
Definition: core.h:633
fmt::v8::detail::counting_buffer
Definition: core.h:1002
fmt::v8::join
auto join(std::initializer_list< T > list, wstring_view sep) -> join_view< const T *, const T *, wchar_t >
Definition: xchar.h:76
fmt::v8::detail::buffer::push_back
void push_back(const T &value)
Definition: core.h:849
fmt::v8::basic_format_args
Definition: core.h:702
Catch::Generators::value
GeneratorWrapper< T > value(T &&value)
Definition: catch.hpp:3999
format.h
fmt::v8::detail::locale_ref
Definition: core.h:1669
fmt::v8::basic_string_view
Definition: core.h:448
fmt::v8::detail::counting_buffer::count
auto count() -> size_t
Definition: core.h:1018
detail
Definition: fmt.cpp:13
fmt::v8::wstring_view
basic_string_view< wchar_t > wstring_view
Definition: xchar.h:24
Catch::Generators::range
GeneratorWrapper< T > range(T const &start, T const &end, T const &step)
Definition: catch.hpp:4693
FMT_STRING
#define FMT_STRING(s)
Definition: format.h:2383
fmt::v8::vprint
void vprint(wstring_view fmt, wformat_args args)
Definition: xchar.h:214
T
T
Definition: simulate_performance.m:4
fmt::v8::vformat_to
auto vformat_to(OutputIt out, const Locale &loc, const S &format_str, basic_format_args< buffer_context< type_identity_t< Char >>> args) -> OutputIt
Definition: xchar.h:154
fmt::v8::basic_memory_buffer
Definition: format.h:677
fmt::v8::basic_format_context
Definition: core.h:1742
FMT_ENABLE_IF
#define FMT_ENABLE_IF(...)
Definition: core.h:344
fmt::v8::detail::iterator_buffer::count
auto count() const -> size_t
Definition: core.h:917
fmt::v8::join_view
Definition: format.h:2772
fmt::v8::format_to_n
auto format_to_n(OutputIt out, size_t n, const S &fmt, const Args &... args) -> format_to_n_result< OutputIt >
Definition: xchar.h:191
fmt::v8::basic_format_context::iterator
OutputIt iterator
Definition: core.h:1753
fmt::v8::bool_constant
std::integral_constant< bool, B > bool_constant
Definition: core.h:324
nlohmann::json_v3_11_1NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON::detail2::begin
begin_tag begin(T &&...)
fmt::v8::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:177
FMT_END_NAMESPACE
#define FMT_END_NAMESPACE
Definition: core.h:240
fmt::v8::detail::iterator_buffer
Definition: core.h:889
fmt::v8::detail::is_exotic_char
bool_constant<!std::is_same< T, char >::value > is_exotic_char
Definition: xchar.h:19
fmt::v8::wformat_context
buffer_context< wchar_t > wformat_context
Definition: xchar.h:26
Range
A range type with an inclusive start bound and an exclusive end bound.
Definition: utils.h:146
fmt::v8::make_args_checked
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:835
fmt::v8::detail::char8_type
char8_type
Definition: format.h:465
fmt::v8::format_to_n_result
Definition: core.h:3151
s
s
Definition: simulate_performance.m:3
fmt::v8::to_wstring
auto to_wstring(const T &value) -> std::wstring
Definition: xchar.h:230
n
n
Definition: simulate_performance.m:1
fmt::v8::detail::buffer::data
auto data() -> T *
Definition: core.h:826
fmt::v8::make_wformat_args
constexpr format_arg_store< wformat_context, Args... > make_wformat_args(const Args &... args)
Definition: xchar.h:44
fmt::v8::system_error
auto system_error(int error_code, format_string< T... > fmt, T &&... args) -> std::system_error
Definition: format.h:2471
FMT_MODULE_EXPORT_END
#define FMT_MODULE_EXPORT_END
Definition: core.h:248
FMT_MODULE_EXPORT_BEGIN
#define FMT_MODULE_EXPORT_BEGIN
Definition: core.h:247
fmt::v8::detail::iterator_buffer::out
auto out() -> OutputIt
Definition: core.h:913
fmt
Definition: bin_to_hex.h:102
fmt::v8::vformat
auto vformat(const Locale &loc, const S &format_str, basic_format_args< buffer_context< type_identity_t< Char >>> args) -> std::basic_string< Char >
Definition: xchar.h:102
FMT_BEGIN_NAMESPACE
#define FMT_BEGIN_NAMESPACE
Definition: core.h:237
to_string
std::string to_string() const
Definition: eth_common.h:64
fmt::v8::type_identity_t
typename type_identity< T >::type type_identity_t
Definition: core.h:332
fmt::v8::detail::sentinel_t
decltype(std::end(std::declval< T & >())) sentinel_t
Definition: format.h:364
FMT_DEPRECATED
#define FMT_DEPRECATED
Definition: core.h:231
fmt::v8::print
void print(wformat_string< T... > fmt, T &&... args)
Definition: xchar.h:223
fmt::v8::detail::get_iterator
auto get_iterator(Buffer &buf) -> decltype(buf.out())
Definition: core.h:1032
fmt::v8::format_to
auto format_to(OutputIt out, const Locale &loc, const S &format_str, Args &&... args) -> typename std::enable_if< enable, OutputIt >::type
Definition: xchar.h:167
fmt::v8::formatted_size
auto formatted_size(const S &fmt, Args &&... args) -> size_t
Definition: xchar.h:199
fmt::v8::basic_format_string
Definition: core.h:3049
fmt::v8::format_arg_store
Definition: core.h:1819
fmt::v8::is_char
Definition: core.h:543
fmt::v8::detail::buffer_appender
conditional_t< std::is_same< T, char >::value, appender, std::back_insert_iterator< buffer< T > >> buffer_appender
Definition: core.h:1023
fmt::v8::format
auto format(const Locale &loc, const S &format_str, Args &&... args) -> std::basic_string< Char >
Definition: xchar.h:113
nlohmann::json_v3_11_1NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON::detail2::end
end_tag end(T &&...)
fmt::v8::detail::type
type
Definition: core.h:1131
FMT_THROW
#define FMT_THROW(x)
Definition: format.h:95