diff --git a/include/fmt/core.h b/include/fmt/core.h index 2f270047..1adca5ae 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -1281,9 +1281,11 @@ template struct format_string_traits> : format_string_traits_base {}; -template -struct format_string_traits> : - format_string_traits_base {}; +template +struct format_string_traits< + S, typename std::enable_if, S>::value>::type> : + format_string_traits_base {}; template struct is_format_string : @@ -1300,6 +1302,11 @@ typename std::enable_if::value>::type template typename std::enable_if::value>::type check_format_string(S); + +template +std::basic_string vformat( + basic_string_view format_str, + basic_format_args::type> args); } // namespace internal format_context::iterator vformat_to( @@ -1354,8 +1361,15 @@ inline typename std::enable_if< make_format_args(args...)); } -std::string vformat(string_view format_str, format_args args); -std::wstring vformat(wstring_view format_str, wformat_args args); +template < + typename String, + typename Char = typename internal::format_string_traits::char_type> +inline std::basic_string vformat( + const String &format_str, + basic_format_args::type> args) { + // Convert format string to string_view to reduce the number of overloads. + return internal::vformat(basic_string_view(format_str), args); +} /** \rst @@ -1376,8 +1390,11 @@ inline std::basic_string< // return vformat(format_str, make_format_args(args...)); // but gcc has trouble optimizing the latter, so break it down. typedef typename internal::format_string_traits::char_type char_t; - format_arg_store::type, Args...> as{args...}; - return vformat(basic_string_view(format_str), as); + typedef typename buffer_context::type context_t; + format_arg_store as{args...}; + return internal::vformat( + basic_string_view(format_str), + basic_format_args(as)); } FMT_API void vprint(std::FILE *f, string_view format_str, format_args args); diff --git a/include/fmt/format.h b/include/fmt/format.h index d4fa94d2..ce309988 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -490,13 +490,14 @@ struct char8_t { } }; -// A UTF-8 string. +// A UTF-8 string view. class u8string_view : public basic_string_view { private: - typedef basic_string_view base; + typedef basic_string_view base; public: using basic_string_view::basic_string_view; + using basic_string_view::char_type; u8string_view(const char *s) : base(reinterpret_cast(s)) {} @@ -3617,18 +3618,15 @@ inline format_to_n_result format_to_n( return {it.base(), it.count()}; } -inline std::string vformat(string_view format_str, format_args args) { - memory_buffer buffer; +template +inline std::basic_string internal::vformat( + basic_string_view format_str, + basic_format_args::type> args) { + basic_memory_buffer buffer; vformat_to(buffer, format_str, args); return fmt::to_string(buffer); } -inline std::wstring vformat(wstring_view format_str, wformat_args args) { - wmemory_buffer buffer; - vformat_to(buffer, format_str, args); - return to_string(buffer); -} - template inline typename std::enable_if::value>::type print(String format_str, const Args & ... args) { diff --git a/src/format.cc b/src/format.cc index f973c385..e387dded 100644 --- a/src/format.cc +++ b/src/format.cc @@ -8,12 +8,6 @@ #include "fmt/format-inl.h" FMT_BEGIN_NAMESPACE -namespace internal { -// Force linking of inline functions into the library. -std::string (*vformat_ref)(string_view, format_args) = vformat; -std::wstring (*vformat_wref)(wstring_view, wformat_args) = vformat; -} - template struct internal::basic_data; // Explicit instantiations for char.