// Formatting library for C++ - optional wchar_t and exotic character support // // Copyright (c) 2012 - present, Victor Zverovich // All rights reserved. // // For the license information refer to format.h. #ifndef FMT_XCHAR_H_ #define FMT_XCHAR_H_ #include "color.h" #include "format.h" #include "ostream.h" #include "ranges.h" #ifndef FMT_MODULE # include # if FMT_USE_LOCALE # include # endif #endif FMT_BEGIN_NAMESPACE namespace detail { template using is_exotic_char = bool_constant::value>; template struct format_string_char {}; template struct format_string_char< S, void_t())))>> { using type = char_t; }; template struct format_string_char< S, enable_if_t::value>> { using type = typename S::char_type; }; template using format_string_char_t = typename format_string_char::type; inline auto write_loc(basic_appender out, loc_value value, const format_specs& specs, locale_ref loc) -> bool { #if FMT_USE_LOCALE auto& numpunct = std::use_facet>(loc.get()); auto separator = std::wstring(); auto grouping = numpunct.grouping(); if (!grouping.empty()) separator = std::wstring(1, numpunct.thousands_sep()); return value.visit(loc_writer{out, specs, separator, grouping, {}}); #endif return false; } } // namespace detail FMT_BEGIN_EXPORT using wstring_view = basic_string_view; using wformat_parse_context = parse_context; using wformat_context = buffered_context; using wformat_args = basic_format_args; using wmemory_buffer = basic_memory_buffer; template struct basic_fstring { private: basic_string_view str_; static constexpr int num_static_named_args = detail::count_static_named_args(); using checker = detail::format_string_checker< Char, static_cast(sizeof...(T)), num_static_named_args, num_static_named_args != detail::count_named_args()>; using arg_pack = detail::arg_pack; public: using t = basic_fstring; template >::value)> FMT_CONSTEVAL FMT_ALWAYS_INLINE basic_fstring(const S& s) : str_(s) { if (FMT_USE_CONSTEVAL) detail::parse_format_string(s, checker(s, arg_pack())); } template ::value&& std::is_same::value)> FMT_ALWAYS_INLINE basic_fstring(const S&) : str_(S()) { FMT_CONSTEXPR auto sv = basic_string_view(S()); FMT_CONSTEXPR int ignore = (parse_format_string(sv, checker(sv, arg_pack())), 0); detail::ignore_unused(ignore); } basic_fstring(runtime_format_string fmt) : str_(fmt.str) {} operator basic_string_view() const { return str_; } auto get() const -> basic_string_view { return str_; } }; template using basic_format_string = basic_fstring; template using wformat_string = typename basic_format_string::t; inline auto runtime(wstring_view s) -> runtime_format_string { return {{s}}; } template <> struct is_char : std::true_type {}; template <> struct is_char : std::true_type {}; template <> struct is_char : std::true_type {}; #ifdef __cpp_char8_t template <> struct is_char : bool_constant {}; #endif template constexpr auto make_wformat_args(T&... args) -> decltype(fmt::make_format_args(args...)) { return fmt::make_format_args(args...); } #if !FMT_USE_NONTYPE_TEMPLATE_ARGS inline namespace literals { inline auto operator""_a(const wchar_t* s, size_t) -> detail::udl_arg { return {s}; } } // namespace literals #endif template auto join(It begin, Sentinel end, wstring_view sep) -> join_view { return {begin, end, sep}; } template ::value)> auto join(Range&& range, wstring_view sep) -> join_view { return join(std::begin(range), std::end(range), sep); } template auto join(std::initializer_list list, wstring_view sep) -> join_view { return join(std::begin(list), std::end(list), sep); } template ::value)> auto join(const Tuple& tuple, basic_string_view sep) -> tuple_join_view { return {tuple, sep}; } template ::value)> auto vformat(basic_string_view fmt, typename detail::vformat_args::type args) -> std::basic_string { auto buf = basic_memory_buffer(); detail::vformat_to(buf, fmt, args); return {buf.data(), buf.size()}; } template auto format(wformat_string fmt, T&&... args) -> std::wstring { return vformat(fmt::wstring_view(fmt), fmt::make_wformat_args(args...)); } template auto format_to(OutputIt out, wformat_string fmt, T&&... args) -> OutputIt { return vformat_to(out, fmt::wstring_view(fmt), fmt::make_wformat_args(args...)); } // Pass char_t as a default template parameter instead of using // std::basic_string> to reduce the symbol size. template , FMT_ENABLE_IF(!std::is_same::value && !std::is_same::value)> auto format(const S& fmt, T&&... args) -> std::basic_string { return vformat(detail::to_string_view(fmt), fmt::make_format_args>(args...)); } template , FMT_ENABLE_IF(detail::is_exotic_char::value)> inline auto vformat(detail::locale_ref loc, const S& fmt, typename detail::vformat_args::type args) -> std::basic_string { auto buf = basic_memory_buffer(); detail::vformat_to(buf, detail::to_string_view(fmt), args, detail::locale_ref(loc)); return {buf.data(), buf.size()}; } template , FMT_ENABLE_IF(detail::is_exotic_char::value)> inline auto format(detail::locale_ref loc, const S& fmt, T&&... args) -> std::basic_string { return vformat(loc, detail::to_string_view(fmt), fmt::make_format_args>(args...)); } template , FMT_ENABLE_IF(detail::is_output_iterator::value&& detail::is_exotic_char::value)> auto vformat_to(OutputIt out, const S& fmt, typename detail::vformat_args::type args) -> OutputIt { auto&& buf = detail::get_buffer(out); detail::vformat_to(buf, detail::to_string_view(fmt), args); return detail::get_iterator(buf, out); } template , FMT_ENABLE_IF(detail::is_output_iterator::value && !std::is_same::value && !std::is_same::value)> inline auto format_to(OutputIt out, const S& fmt, T&&... args) -> OutputIt { return vformat_to(out, detail::to_string_view(fmt), fmt::make_format_args>(args...)); } template , FMT_ENABLE_IF(detail::is_output_iterator::value&& detail::is_exotic_char::value)> inline auto vformat_to(OutputIt out, detail::locale_ref loc, const S& fmt, typename detail::vformat_args::type args) -> OutputIt { auto&& buf = detail::get_buffer(out); vformat_to(buf, detail::to_string_view(fmt), args, detail::locale_ref(loc)); return detail::get_iterator(buf, out); } template , bool enable = detail::is_output_iterator::value && detail::is_exotic_char::value> inline auto format_to(OutputIt out, detail::locale_ref loc, const S& fmt, T&&... args) -> typename std::enable_if::type { return vformat_to(out, loc, detail::to_string_view(fmt), fmt::make_format_args>(args...)); } template ::value&& detail::is_exotic_char::value)> inline auto vformat_to_n(OutputIt out, size_t n, basic_string_view fmt, typename detail::vformat_args::type args) -> format_to_n_result { using traits = detail::fixed_buffer_traits; auto buf = detail::iterator_buffer(out, n); detail::vformat_to(buf, fmt, args); return {buf.out(), buf.count()}; } template , FMT_ENABLE_IF(detail::is_output_iterator::value&& detail::is_exotic_char::value)> inline auto format_to_n(OutputIt out, size_t n, const S& fmt, T&&... args) -> format_to_n_result { return vformat_to_n(out, n, fmt::basic_string_view(fmt), fmt::make_format_args>(args...)); } template , FMT_ENABLE_IF(detail::is_exotic_char::value)> inline auto formatted_size(const S& fmt, T&&... args) -> size_t { auto buf = detail::counting_buffer(); detail::vformat_to(buf, detail::to_string_view(fmt), fmt::make_format_args>(args...)); return buf.count(); } inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) { auto buf = wmemory_buffer(); detail::vformat_to(buf, fmt, args); buf.push_back(L'\0'); if (std::fputws(buf.data(), f) == -1) FMT_THROW(system_error(errno, FMT_STRING("cannot write to file"))); } inline void vprint(wstring_view fmt, wformat_args args) { vprint(stdout, fmt, args); } template void print(std::FILE* f, wformat_string fmt, T&&... args) { return vprint(f, wstring_view(fmt), fmt::make_wformat_args(args...)); } template void print(wformat_string fmt, T&&... args) { return vprint(wstring_view(fmt), fmt::make_wformat_args(args...)); } template void println(std::FILE* f, wformat_string fmt, T&&... args) { return print(f, L"{}\n", fmt::format(fmt, std::forward(args)...)); } template void println(wformat_string fmt, T&&... args) { return print(L"{}\n", fmt::format(fmt, std::forward(args)...)); } inline auto vformat(const text_style& ts, wstring_view fmt, wformat_args args) -> std::wstring { auto buf = wmemory_buffer(); detail::vformat_to(buf, ts, fmt, args); return {buf.data(), buf.size()}; } template inline auto format(const text_style& ts, wformat_string fmt, T&&... args) -> std::wstring { return fmt::vformat(ts, fmt, fmt::make_wformat_args(args...)); } template FMT_DEPRECATED void print(std::FILE* f, const text_style& ts, wformat_string fmt, const T&... args) { vprint(f, ts, fmt, fmt::make_wformat_args(args...)); } template FMT_DEPRECATED void print(const text_style& ts, wformat_string fmt, const T&... args) { return print(stdout, ts, fmt, args...); } inline void vprint(std::wostream& os, wstring_view fmt, wformat_args args) { auto buffer = basic_memory_buffer(); detail::vformat_to(buffer, fmt, args); detail::write_buffer(os, buffer); } template void print(std::wostream& os, wformat_string fmt, T&&... args) { vprint(os, fmt, fmt::make_format_args>(args...)); } template void println(std::wostream& os, wformat_string fmt, T&&... args) { print(os, L"{}\n", fmt::format(fmt, std::forward(args)...)); } /// Converts `value` to `std::wstring` using the default format for type `T`. template inline auto to_wstring(const T& value) -> std::wstring { return format(FMT_STRING(L"{}"), value); } FMT_END_EXPORT FMT_END_NAMESPACE #endif // FMT_XCHAR_H_