Fix handling of wide strings in StringWriter

This commit is contained in:
Victor Zverovich 2016-07-18 08:47:11 -07:00
parent c110c6eca7
commit 65cd664195
2 changed files with 12 additions and 4 deletions

View File

@ -20,7 +20,7 @@ namespace internal {
template <typename Char> template <typename Char>
class StringBuffer : public Buffer<Char> { class StringBuffer : public Buffer<Char> {
private: private:
std::string data_; std::basic_string<Char> data_;
protected: protected:
virtual void grow(std::size_t size) { virtual void grow(std::size_t size) {
@ -31,7 +31,7 @@ class StringBuffer : public Buffer<Char> {
public: public:
// Moves the data to ``str`` clearing the buffer. // Moves the data to ``str`` clearing the buffer.
void move_to(std::string &str) { void move_to(std::basic_string<Char> &str) {
data_.resize(this->size_); data_.resize(this->size_);
str.swap(data_); str.swap(data_);
this->capacity_ = this->size_ = 0; this->capacity_ = this->size_ = 0;
@ -82,14 +82,14 @@ class BasicStringWriter : public BasicWriter<Char> {
Constructs a :class:`fmt::BasicStringWriter` object. Constructs a :class:`fmt::BasicStringWriter` object.
\endrst \endrst
*/ */
BasicStringWriter() : Writer(buffer_) {} BasicStringWriter() : BasicWriter<Char>(buffer_) {}
/** /**
\rst \rst
Moves the buffer content to *str* clearing the buffer. Moves the buffer content to *str* clearing the buffer.
\endrst \endrst
*/ */
void move_to(std::string &str) { void move_to(std::basic_string<Char> &str) {
buffer_.move_to(str); buffer_.move_to(str);
} }
}; };

View File

@ -67,6 +67,14 @@ TEST(StringWriterTest, MoveTo) {
EXPECT_EQ(0, out.size()); EXPECT_EQ(0, out.size());
} }
TEST(StringWriterTest, WString) {
fmt::WStringWriter out;
out << "The answer is " << 42 << "\n";
std::wstring s;
out.move_to(s);
EXPECT_EQ(L"The answer is 42\n", s);
}
TEST(StringTest, ToString) { TEST(StringTest, ToString) {
EXPECT_EQ("42", fmt::to_string(42)); EXPECT_EQ("42", fmt::to_string(42));
} }