File indexing completed on 2025-01-18 09:57:25
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef FMT_XCHAR_H_
0009 #define FMT_XCHAR_H_
0010
0011 #include <cwchar>
0012
0013 #include "format.h"
0014
0015 FMT_BEGIN_NAMESPACE
0016 namespace detail {
0017 template <typename T>
0018 using is_exotic_char = bool_constant<!std::is_same<T, char>::value>;
0019 }
0020
0021 FMT_MODULE_EXPORT_BEGIN
0022
0023 using wstring_view = basic_string_view<wchar_t>;
0024 using wformat_parse_context = basic_format_parse_context<wchar_t>;
0025 using wformat_context = buffer_context<wchar_t>;
0026 using wformat_args = basic_format_args<wformat_context>;
0027 using wmemory_buffer = basic_memory_buffer<wchar_t>;
0028
0029 #if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
0030
0031 template <typename... Args> using wformat_string = wstring_view;
0032 inline auto runtime(wstring_view s) -> wstring_view { return s; }
0033 #else
0034 template <typename... Args>
0035 using wformat_string = basic_format_string<wchar_t, type_identity_t<Args>...>;
0036 inline auto runtime(wstring_view s) -> basic_runtime<wchar_t> { return {{s}}; }
0037 #endif
0038
0039 template <> struct is_char<wchar_t> : std::true_type {};
0040 template <> struct is_char<detail::char8_type> : std::true_type {};
0041 template <> struct is_char<char16_t> : std::true_type {};
0042 template <> struct is_char<char32_t> : std::true_type {};
0043
0044 template <typename... Args>
0045 constexpr format_arg_store<wformat_context, Args...> make_wformat_args(
0046 const Args&... args) {
0047 return {args...};
0048 }
0049
0050 inline namespace literals {
0051 #if FMT_USE_USER_DEFINED_LITERALS && !FMT_USE_NONTYPE_TEMPLATE_ARGS
0052 constexpr detail::udl_arg<wchar_t> operator"" _a(const wchar_t* s, size_t) {
0053 return {s};
0054 }
0055 #endif
0056 }
0057
0058 template <typename It, typename Sentinel>
0059 auto join(It begin, Sentinel end, wstring_view sep)
0060 -> join_view<It, Sentinel, wchar_t> {
0061 return {begin, end, sep};
0062 }
0063
0064 template <typename Range>
0065 auto join(Range&& range, wstring_view sep)
0066 -> join_view<detail::iterator_t<Range>, detail::sentinel_t<Range>,
0067 wchar_t> {
0068 return join(std::begin(range), std::end(range), sep);
0069 }
0070
0071 template <typename T>
0072 auto join(std::initializer_list<T> list, wstring_view sep)
0073 -> join_view<const T*, const T*, wchar_t> {
0074 return join(std::begin(list), std::end(list), sep);
0075 }
0076
0077 template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
0078 auto vformat(basic_string_view<Char> format_str,
0079 basic_format_args<buffer_context<type_identity_t<Char>>> args)
0080 -> std::basic_string<Char> {
0081 basic_memory_buffer<Char> buffer;
0082 detail::vformat_to(buffer, format_str, args);
0083 return to_string(buffer);
0084 }
0085
0086 template <typename... T>
0087 auto format(wformat_string<T...> fmt, T&&... args) -> std::wstring {
0088 return vformat(fmt::wstring_view(fmt), fmt::make_wformat_args(args...));
0089 }
0090
0091
0092
0093 template <typename S, typename... Args, typename Char = char_t<S>,
0094 FMT_ENABLE_IF(!std::is_same<Char, char>::value &&
0095 !std::is_same<Char, wchar_t>::value)>
0096 auto format(const S& format_str, Args&&... args) -> std::basic_string<Char> {
0097 return vformat(detail::to_string_view(format_str),
0098 fmt::make_format_args<buffer_context<Char>>(args...));
0099 }
0100
0101 template <typename Locale, typename S, typename Char = char_t<S>,
0102 FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
0103 detail::is_exotic_char<Char>::value)>
0104 inline auto vformat(
0105 const Locale& loc, const S& format_str,
0106 basic_format_args<buffer_context<type_identity_t<Char>>> args)
0107 -> std::basic_string<Char> {
0108 return detail::vformat(loc, detail::to_string_view(format_str), args);
0109 }
0110
0111 template <typename Locale, typename S, typename... Args,
0112 typename Char = char_t<S>,
0113 FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
0114 detail::is_exotic_char<Char>::value)>
0115 inline auto format(const Locale& loc, const S& format_str, Args&&... args)
0116 -> std::basic_string<Char> {
0117 return detail::vformat(loc, detail::to_string_view(format_str),
0118 fmt::make_format_args<buffer_context<Char>>(args...));
0119 }
0120
0121 template <typename OutputIt, typename S, typename Char = char_t<S>,
0122 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
0123 detail::is_exotic_char<Char>::value)>
0124 auto vformat_to(OutputIt out, const S& format_str,
0125 basic_format_args<buffer_context<type_identity_t<Char>>> args)
0126 -> OutputIt {
0127 auto&& buf = detail::get_buffer<Char>(out);
0128 detail::vformat_to(buf, detail::to_string_view(format_str), args);
0129 return detail::get_iterator(buf);
0130 }
0131
0132 template <typename OutputIt, typename S, typename... Args,
0133 typename Char = char_t<S>,
0134 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
0135 detail::is_exotic_char<Char>::value)>
0136 inline auto format_to(OutputIt out, const S& fmt, Args&&... args) -> OutputIt {
0137 return vformat_to(out, detail::to_string_view(fmt),
0138 fmt::make_format_args<buffer_context<Char>>(args...));
0139 }
0140
0141 template <typename Locale, typename S, typename OutputIt, typename... Args,
0142 typename Char = char_t<S>,
0143 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
0144 detail::is_locale<Locale>::value&&
0145 detail::is_exotic_char<Char>::value)>
0146 inline auto vformat_to(
0147 OutputIt out, const Locale& loc, const S& format_str,
0148 basic_format_args<buffer_context<type_identity_t<Char>>> args) -> OutputIt {
0149 auto&& buf = detail::get_buffer<Char>(out);
0150 vformat_to(buf, detail::to_string_view(format_str), args,
0151 detail::locale_ref(loc));
0152 return detail::get_iterator(buf);
0153 }
0154
0155 template <
0156 typename OutputIt, typename Locale, typename S, typename... Args,
0157 typename Char = char_t<S>,
0158 bool enable = detail::is_output_iterator<OutputIt, Char>::value&&
0159 detail::is_locale<Locale>::value&& detail::is_exotic_char<Char>::value>
0160 inline auto format_to(OutputIt out, const Locale& loc, const S& format_str,
0161 Args&&... args) ->
0162 typename std::enable_if<enable, OutputIt>::type {
0163 return vformat_to(out, loc, to_string_view(format_str),
0164 fmt::make_format_args<buffer_context<Char>>(args...));
0165 }
0166
0167 template <typename OutputIt, typename Char, typename... Args,
0168 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
0169 detail::is_exotic_char<Char>::value)>
0170 inline auto vformat_to_n(
0171 OutputIt out, size_t n, basic_string_view<Char> format_str,
0172 basic_format_args<buffer_context<type_identity_t<Char>>> args)
0173 -> format_to_n_result<OutputIt> {
0174 detail::iterator_buffer<OutputIt, Char, detail::fixed_buffer_traits> buf(out,
0175 n);
0176 detail::vformat_to(buf, format_str, args);
0177 return {buf.out(), buf.count()};
0178 }
0179
0180 template <typename OutputIt, typename S, typename... Args,
0181 typename Char = char_t<S>,
0182 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
0183 detail::is_exotic_char<Char>::value)>
0184 inline auto format_to_n(OutputIt out, size_t n, const S& fmt,
0185 const Args&... args) -> format_to_n_result<OutputIt> {
0186 return vformat_to_n(out, n, detail::to_string_view(fmt),
0187 fmt::make_format_args<buffer_context<Char>>(args...));
0188 }
0189
0190 template <typename S, typename... Args, typename Char = char_t<S>,
0191 FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
0192 inline auto formatted_size(const S& fmt, Args&&... args) -> size_t {
0193 detail::counting_buffer<Char> buf;
0194 detail::vformat_to(buf, detail::to_string_view(fmt),
0195 fmt::make_format_args<buffer_context<Char>>(args...));
0196 return buf.count();
0197 }
0198
0199 inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) {
0200 wmemory_buffer buffer;
0201 detail::vformat_to(buffer, fmt, args);
0202 buffer.push_back(L'\0');
0203 if (std::fputws(buffer.data(), f) == -1)
0204 FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
0205 }
0206
0207 inline void vprint(wstring_view fmt, wformat_args args) {
0208 vprint(stdout, fmt, args);
0209 }
0210
0211 template <typename... T>
0212 void print(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
0213 return vprint(f, wstring_view(fmt), fmt::make_wformat_args(args...));
0214 }
0215
0216 template <typename... T> void print(wformat_string<T...> fmt, T&&... args) {
0217 return vprint(wstring_view(fmt), fmt::make_wformat_args(args...));
0218 }
0219
0220
0221
0222
0223 template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
0224 return format(FMT_STRING(L"{}"), value);
0225 }
0226 FMT_MODULE_EXPORT_END
0227 FMT_END_NAMESPACE
0228
0229 #endif