From 0508bbc7ae4d1509b6588e1a536bd86c0694edf6 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 13 Jun 2018 08:24:32 +0200 Subject: [PATCH] Add wchar_t overload of format_to_n (#764) --- include/fmt/format.h | 17 ++++++++++++++++- test/format-test.cc | 9 +++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index b11abdc3..26b81e07 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3526,6 +3526,13 @@ inline OutputIt vformat_to(OutputIt out, string_view format_str, typedef output_range range; return vformat_to>(range(out), format_str, args); } +template +inline OutputIt vformat_to( + OutputIt out, wstring_view format_str, + typename format_args_t::type args) { + typedef output_range range; + return vformat_to>(range(out), format_str, args); +} /** \rst @@ -3578,12 +3585,20 @@ struct format_to_n_result { */ template inline format_to_n_result format_to_n( - OutputIt out, std::size_t n, string_view format_str, const Args & ... args) { + OutputIt out, std::size_t n, string_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 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()}; +} inline std::string vformat(string_view format_str, format_args args) { memory_buffer buffer; diff --git a/test/format-test.cc b/test/format-test.cc index e0cddd14..5af837da 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1525,6 +1525,15 @@ TEST(FormatTest, FormatToN) { EXPECT_EQ("foox", fmt::string_view(buffer, 4)); } +TEST(FormatTest, WideFormatToN) { + wchar_t buffer[4]; + buffer[3] = L'x'; + auto result = fmt::format_to_n(buffer, 3, L"{}", 12345); + EXPECT_EQ(5u, result.size); + EXPECT_EQ(buffer + 3, result.out); + EXPECT_EQ(L"123x", fmt::wstring_view(buffer, 4)); +} + #if FMT_USE_CONSTEXPR struct test_arg_id_handler { enum result { NONE, EMPTY, INDEX, NAME, ERROR };