diff --git a/include/fmt/core.h b/include/fmt/core.h index 06deb520..0ec3504c 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -1340,23 +1340,14 @@ template struct is_contiguous >: std::true_type {}; /** Formats a string and writes the output to ``out``. */ -template +template typename std::enable_if< is_contiguous::value, std::back_insert_iterator>::type vformat_to(std::back_insert_iterator out, - string_view format_str, format_args args) { + const S &format_str, + basic_format_args::type> args) { internal::container_buffer buf(internal::get_container(out)); - vformat_to(buf, format_str, args); - return out; -} - -template -typename std::enable_if< - is_contiguous::value, std::back_insert_iterator>::type - vformat_to(std::back_insert_iterator out, - wstring_view format_str, wformat_args args) { - internal::container_buffer buf(internal::get_container(out)); - vformat_to(buf, format_str, args); + vformat_to(buf, internal::to_string_view(format_str), args); return out; } @@ -1366,8 +1357,8 @@ inline typename std::enable_if< std::back_insert_iterator>::type format_to(std::back_insert_iterator out, const S &format_str, const Args &... args) { - return vformat_to(out, internal::to_string_view(format_str), - internal::checked_args(format_str, args...)); + internal::checked_args ca(format_str, args...); + return vformat_to(out, internal::to_string_view(format_str), *ca); } template diff --git a/include/fmt/format.h b/include/fmt/format.h index 17d2a3da..b5089128 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3465,12 +3465,6 @@ inline typename buffer_context::type::iterator vformat_to( buf, basic_string_view(format_str), args); } -inline wformat_context::iterator vformat_to( - internal::wbuffer &buf, wstring_view format_str, wformat_args args) { - typedef back_insert_range range; - return vformat_to>(buf, format_str, args); -} - template < typename String, typename... Args, std::size_t SIZE = inline_buffer_size, @@ -3534,23 +3528,23 @@ struct format_to_n_result { std::size_t size; }; -template +template using format_to_n_context = typename fmt::format_context_t< - fmt::internal::truncating_iterator>::type; + fmt::internal::truncating_iterator, Char>::type; -template -using format_to_n_args = fmt::basic_format_args>; +template +using format_to_n_args = fmt::basic_format_args>; -template -inline format_arg_store, Args...> +template +inline format_arg_store, Args...> make_format_to_n_args(const Args &... args) { - return format_arg_store, Args...>(args...); + return format_arg_store, Args...>(args...); } -template +template inline format_to_n_result vformat_to_n( - OutputIt out, std::size_t n, string_view format_str, - format_to_n_args args) { + OutputIt out, std::size_t n, basic_string_view format_str, + format_to_n_args args) { typedef internal::truncating_iterator It; auto it = vformat_to(It(out, n), format_str, args); return {it.base(), it.count()}; @@ -3563,20 +3557,16 @@ inline format_to_n_result vformat_to_n( end of the output range. \endrst */ -template -inline format_to_n_result format_to_n( - OutputIt out, std::size_t n, string_view format_str, const Args &... args) { - return vformat_to_n( - out, n, format_str, make_format_to_n_args(args...)); -} -template -inline format_to_n_result format_to_n( - OutputIt out, std::size_t n, wstring_view format_str, - const Args &... args) { - typedef internal::truncating_iterator It; - auto it = vformat_to(It(out, n), format_str, - make_format_args::type>(args...)); - return {it.base(), it.count()}; +template +inline typename std::enable_if< + internal::is_format_string::value, + format_to_n_result>::type format_to_n( + OutputIt out, std::size_t n, const String &format_str, const Args &... args) { + internal::check_format_string(format_str); + typedef FMT_CHAR(String) Char; + format_arg_store, Args...> as{ args... }; + return vformat_to_n(out, n, internal::to_string_view(format_str), + format_to_n_args(as)); } template diff --git a/test/format-test.cc b/test/format-test.cc index 18f8ad12..766befd2 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -2401,6 +2401,26 @@ TEST(FormatTest, FormatStringErrors) { "cannot switch from automatic to manual argument indexing", int, int); } + +TEST(FormatTest, VFormatTo) { + typedef fmt::format_context context; + fmt::basic_format_arg arg = fmt::internal::make_arg(42); + fmt::basic_format_args args(&arg, 1); + std::string s; + fmt::vformat_to(std::back_inserter(s), "{}", args); + EXPECT_EQ("42", s); + s.clear(); + fmt::vformat_to(std::back_inserter(s), FMT_STRING("{}"), args); + EXPECT_EQ("42", s); + + typedef fmt::wformat_context wcontext; + fmt::basic_format_arg warg = fmt::internal::make_arg(42); + fmt::basic_format_args wargs(&warg, 1); + std::wstring w; + fmt::vformat_to(std::back_inserter(w), L"{}", wargs); + EXPECT_EQ(L"42", w); +} + #endif // FMT_USE_CONSTEXPR TEST(FormatTest, ConstructU8StringViewFromCString) {