From 2f13d41e30faed6e0393d66cf3f93e240ce9b518 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 7 Feb 2018 07:08:14 -0800 Subject: [PATCH] Add to_wstring --- include/fmt/format.h | 22 ++++++++++++++++++---- test/format-test.cc | 4 ++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 04850fd2..56574934 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1327,10 +1327,16 @@ class arg_formatter_base { FMT_DISALLOW_COPY_AND_ASSIGN(arg_formatter_base); - void write_char(char_type value) { - writer_.write_padded(1, specs_, [value](auto &&it) { + struct char_writer { + char_type value; + template + void operator()(It &&it) const { *it++ = internal::char_traits::cast(value); - }); + } + }; + + void write_char(char_type value) { + writer_.write_padded(1, specs_, char_writer{value}); } void write_pointer(const void *p) { @@ -2172,11 +2178,11 @@ template class basic_writer { public: using char_type = typename Range::value_type; + using iterator = decltype(std::declval().begin()); using format_specs = basic_format_specs; private: // Output iterator. - using iterator = decltype(std::declval().begin()); iterator out_; std::unique_ptr locale_; @@ -3145,6 +3151,14 @@ std::string to_string(const T &value) { return str; } +template +std::wstring to_wstring(const T &value) { + std::wstring str; + internal::container_buffer buf(str); + wwriter(buf).write(value); + return str; +} + template std::basic_string to_string(const basic_memory_buffer &buffer) { return std::basic_string(buffer.data(), buffer.size()); diff --git a/test/format-test.cc b/test/format-test.cc index 6de47bb3..792b4014 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1608,6 +1608,10 @@ TEST(FormatTest, ToString) { EXPECT_EQ("42", fmt::to_string(42)); } +TEST(FormatTest, ToWString) { + EXPECT_EQ(L"42", fmt::to_wstring(42)); +} + TEST(FormatTest, OutputIterators) { std::list out; fmt::format_to(std::back_inserter(out), "{}", 42);