Implement writing narrow strings into a wide writer

This commit is contained in:
vitaut 2015-06-09 08:20:44 -07:00
parent 8949a2e941
commit ed2dfe5124
2 changed files with 11 additions and 2 deletions

View File

@ -368,14 +368,16 @@ class Buffer {
} }
/** Appends data to the end of the buffer. */ /** Appends data to the end of the buffer. */
void append(const T *begin, const T *end); template <typename U>
void append(const U *begin, const U *end);
T &operator[](std::size_t index) { return ptr_[index]; } T &operator[](std::size_t index) { return ptr_[index]; }
const T &operator[](std::size_t index) const { return ptr_[index]; } const T &operator[](std::size_t index) const { return ptr_[index]; }
}; };
template <typename T> template <typename T>
void Buffer<T>::append(const T *begin, const T *end) { template <typename U>
void Buffer<T>::append(const U *begin, const U *end) {
std::ptrdiff_t num_elements = end - begin; std::ptrdiff_t num_elements = end - begin;
if (size_ + num_elements > capacity_) if (size_ + num_elements > capacity_)
grow(size_ + num_elements); grow(size_ + num_elements);
@ -1923,6 +1925,12 @@ class BasicWriter {
return *this; return *this;
} }
BasicWriter &operator<<(typename internal::WCharHelper<StringRef, Char>::Supported value) {
const char *str = value.c_str();
buffer_.append(str, str + value.size());
return *this;
}
template <typename T, typename Spec, typename FillChar> template <typename T, typename Spec, typename FillChar>
BasicWriter &operator<<(IntFormatSpec<T, Spec, FillChar> spec) { BasicWriter &operator<<(IntFormatSpec<T, Spec, FillChar> spec) {
internal::CharTraits<Char>::convert(FillChar()); internal::CharTraits<Char>::convert(FillChar());

View File

@ -325,6 +325,7 @@ TEST(WriterTest, WriteWideChar) {
TEST(WriterTest, WriteString) { TEST(WriterTest, WriteString) {
CHECK_WRITE_CHAR("abc"); CHECK_WRITE_CHAR("abc");
CHECK_WRITE_WCHAR("abc");
// The following line shouldn't compile: // The following line shouldn't compile:
//MemoryWriter() << L"abc"; //MemoryWriter() << L"abc";
} }