From ed2dfe512422c7473759618228f71f67019475b5 Mon Sep 17 00:00:00 2001 From: vitaut Date: Tue, 9 Jun 2015 08:20:44 -0700 Subject: [PATCH] Implement writing narrow strings into a wide writer --- format.h | 12 ++++++++++-- test/format-test.cc | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/format.h b/format.h index 2e558edc..bee4cf78 100644 --- a/format.h +++ b/format.h @@ -368,14 +368,16 @@ class Buffer { } /** Appends data to the end of the buffer. */ - void append(const T *begin, const T *end); + template + void append(const U *begin, const U *end); T &operator[](std::size_t index) { return ptr_[index]; } const T &operator[](std::size_t index) const { return ptr_[index]; } }; template -void Buffer::append(const T *begin, const T *end) { +template +void Buffer::append(const U *begin, const U *end) { std::ptrdiff_t num_elements = end - begin; if (size_ + num_elements > capacity_) grow(size_ + num_elements); @@ -1923,6 +1925,12 @@ class BasicWriter { return *this; } + BasicWriter &operator<<(typename internal::WCharHelper::Supported value) { + const char *str = value.c_str(); + buffer_.append(str, str + value.size()); + return *this; + } + template BasicWriter &operator<<(IntFormatSpec spec) { internal::CharTraits::convert(FillChar()); diff --git a/test/format-test.cc b/test/format-test.cc index 8660b945..11d5425b 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -325,6 +325,7 @@ TEST(WriterTest, WriteWideChar) { TEST(WriterTest, WriteString) { CHECK_WRITE_CHAR("abc"); + CHECK_WRITE_WCHAR("abc"); // The following line shouldn't compile: //MemoryWriter() << L"abc"; }