// 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_WCHAR_H_ #define FMT_WCHAR_H_ #include #include #include "format.h" FMT_BEGIN_NAMESPACE namespace detail { template using is_exotic_char = bool_constant::value>; } FMT_MODULE_EXPORT_BEGIN using wstring_view = basic_string_view; using wformat_parse_context = basic_format_parse_context; using wformat_context = buffer_context; using wformat_args = basic_format_args; using wmemory_buffer = basic_memory_buffer; #if FMT_GCC_VERSION && FMT_GCC_VERSION < 409 // Workaround broken conversion on older gcc. template using wformat_string = wstring_view; #else template using wformat_string = basic_format_string...>; #endif template <> struct is_char : std::true_type {}; template <> struct is_char : std::true_type {}; template <> struct is_char : std::true_type {}; template <> struct is_char : std::true_type {}; template constexpr format_arg_store make_wformat_args( const Args&... args) { return {args...}; } inline namespace literals { constexpr auto operator"" _format(const wchar_t* s, size_t n) -> detail::udl_formatter { return {{s, n}}; } #if FMT_USE_USER_DEFINED_LITERALS && !FMT_USE_NONTYPE_TEMPLATE_PARAMETERS constexpr detail::udl_arg operator"" _a(const wchar_t* s, size_t) { return {s}; } #endif } // namespace literals template auto join(It begin, Sentinel end, wstring_view sep) -> join_view { return {begin, end, sep}; } template auto join(Range&& range, wstring_view sep) -> join_view, detail::sentinel_t, wchar_t> { 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 vformat(basic_string_view format_str, basic_format_args>> args) -> std::basic_string { basic_memory_buffer buffer; detail::vformat_to(buffer, format_str, args); return to_string(buffer); } // 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)> auto format(const S& format_str, Args&&... args) -> std::basic_string { const auto& vargs = fmt::make_args_checked(format_str, args...); return vformat(to_string_view(format_str), vargs); } template , FMT_ENABLE_IF(detail::is_locale::value&& detail::is_exotic_char::value)> inline auto vformat( const Locale& loc, const S& format_str, basic_format_args>> args) -> std::basic_string { return detail::vformat(loc, to_string_view(format_str), args); } template , FMT_ENABLE_IF(detail::is_locale::value&& detail::is_exotic_char::value)> inline auto format(const Locale& loc, const S& format_str, Args&&... args) -> std::basic_string { return detail::vformat(loc, to_string_view(format_str), fmt::make_args_checked(format_str, args...)); } template , FMT_ENABLE_IF(detail::is_output_iterator::value&& detail::is_exotic_char::value)> auto vformat_to(OutputIt out, const S& format_str, basic_format_args>> args) -> OutputIt { auto&& buf = detail::get_buffer(out); detail::vformat_to(buf, to_string_view(format_str), args); return detail::get_iterator(buf); } template , FMT_ENABLE_IF(detail::is_output_iterator::value&& detail::is_exotic_char::value)> inline auto format_to(OutputIt out, const S& fmt, Args&&... args) -> OutputIt { const auto& vargs = fmt::make_args_checked(fmt, args...); return vformat_to(out, to_string_view(fmt), vargs); } template ::value)> FMT_DEPRECATED auto format_to(basic_memory_buffer& buf, const S& format_str, Args&&... args) -> typename buffer_context::iterator { const auto& vargs = fmt::make_args_checked(format_str, args...); detail::vformat_to(buf, to_string_view(format_str), vargs, {}); return detail::buffer_appender(buf); } template , FMT_ENABLE_IF(detail::is_output_iterator::value&& detail::is_locale::value&& detail::is_exotic_char::value)> inline auto vformat_to( OutputIt out, const Locale& loc, const S& format_str, basic_format_args>> args) -> OutputIt { auto&& buf = detail::get_buffer(out); vformat_to(buf, to_string_view(format_str), args, detail::locale_ref(loc)); return detail::get_iterator(buf); } template < typename OutputIt, typename Locale, typename S, typename... Args, typename Char = char_t, bool enable = detail::is_output_iterator::value&& detail::is_locale::value&& detail::is_exotic_char::value> inline auto format_to(OutputIt out, const Locale& loc, const S& format_str, Args&&... args) -> typename std::enable_if::type { const auto& vargs = fmt::make_args_checked(format_str, args...); return vformat_to(out, loc, to_string_view(format_str), vargs); } template ::value&& detail::is_exotic_char::value)> inline auto vformat_to_n( OutputIt out, size_t n, basic_string_view format_str, basic_format_args>> args) -> format_to_n_result { detail::iterator_buffer buf(out, n); detail::vformat_to(buf, format_str, 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, const Args&... args) -> format_to_n_result { const auto& vargs = fmt::make_args_checked(fmt, args...); return vformat_to_n(out, n, to_string_view(fmt), vargs); } template , FMT_ENABLE_IF(detail::is_exotic_char::value)> inline auto formatted_size(const S& fmt, Args&&... args) -> size_t { detail::counting_buffer buf; const auto& vargs = fmt::make_args_checked(fmt, args...); detail::vformat_to(buf, to_string_view(fmt), vargs); return buf.count(); } inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) { wmemory_buffer buffer; detail::vformat_to(buffer, fmt, args); buffer.push_back(L'\0'); if (std::fputws(buffer.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), make_wformat_args(args...)); } template void print(wformat_string fmt, T&&... args) { return vprint(wstring_view(fmt), make_wformat_args(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_MODULE_EXPORT_END FMT_END_NAMESPACE #endif // FMT_WCHAR_H_