Merge StringWriter into StringBuffer
This commit is contained in:
parent
c2f021692f
commit
87b691d80c
106
fmt/string.h
106
fmt/string.h
@ -14,11 +14,36 @@
|
|||||||
|
|
||||||
namespace fmt {
|
namespace fmt {
|
||||||
|
|
||||||
namespace internal {
|
/**
|
||||||
|
\rst
|
||||||
|
This class template represents a character buffer backed by std::string.
|
||||||
|
|
||||||
// A buffer that stores data in ``std::string``.
|
You can use one of the following typedefs for common character types
|
||||||
template <typename Char>
|
and the standard allocator:
|
||||||
class StringBuffer : public basic_buffer<Char> {
|
|
||||||
|
+----------------+------------------------------+
|
||||||
|
| Type | Definition |
|
||||||
|
+================+==============================+
|
||||||
|
| string_buffer | basic_string_buffer<char> |
|
||||||
|
+----------------+------------------------------+
|
||||||
|
| wstring_buffer | basic_string_buffer<wchar_t> |
|
||||||
|
+----------------+------------------------------+
|
||||||
|
|
||||||
|
**Example**::
|
||||||
|
|
||||||
|
string_buffer out;
|
||||||
|
format_to(out, "The answer is {}", 42);
|
||||||
|
|
||||||
|
This will write the following output to the ``out`` object:
|
||||||
|
|
||||||
|
.. code-block:: none
|
||||||
|
|
||||||
|
The answer is 42
|
||||||
|
|
||||||
|
The output can be moved to an ``std::string`` with ``out.move_to()``.
|
||||||
|
\endrst
|
||||||
|
*/template <typename Char>
|
||||||
|
class basic_string_buffer : public basic_buffer<Char> {
|
||||||
private:
|
private:
|
||||||
std::basic_string<Char> data_;
|
std::basic_string<Char> data_;
|
||||||
|
|
||||||
@ -30,7 +55,11 @@ class StringBuffer : public basic_buffer<Char> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Moves the data to ``str`` clearing the buffer.
|
/**
|
||||||
|
\rst
|
||||||
|
Moves the buffer content to *str* clearing the buffer.
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
void move_to(std::basic_string<Char> &str) {
|
void move_to(std::basic_string<Char> &str) {
|
||||||
data_.resize(this->size_);
|
data_.resize(this->size_);
|
||||||
str.swap(data_);
|
str.swap(data_);
|
||||||
@ -38,64 +67,9 @@ class StringBuffer : public basic_buffer<Char> {
|
|||||||
this->ptr_ = 0;
|
this->ptr_ = 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} // namespace internal
|
|
||||||
|
|
||||||
/**
|
typedef basic_string_buffer<char> string_buffer;
|
||||||
\rst
|
typedef basic_string_buffer<wchar_t> wstring_buffer;
|
||||||
This class template provides operations for formatting and writing data
|
|
||||||
into a character stream. The output is stored in ``std::string`` that grows
|
|
||||||
dynamically.
|
|
||||||
|
|
||||||
You can use one of the following typedefs for common character types
|
|
||||||
and the standard allocator:
|
|
||||||
|
|
||||||
+---------------+----------------------------+
|
|
||||||
| Type | Definition |
|
|
||||||
+===============+============================+
|
|
||||||
| StringWriter | BasicStringWriter<char> |
|
|
||||||
+---------------+----------------------------+
|
|
||||||
| WStringWriter | BasicStringWriter<wchar_t> |
|
|
||||||
+---------------+----------------------------+
|
|
||||||
|
|
||||||
**Example**::
|
|
||||||
|
|
||||||
StringWriter out;
|
|
||||||
out << "The answer is " << 42 << "\n";
|
|
||||||
|
|
||||||
This will write the following output to the ``out`` object:
|
|
||||||
|
|
||||||
.. code-block:: none
|
|
||||||
|
|
||||||
The answer is 42
|
|
||||||
|
|
||||||
The output can be moved to an ``std::string`` with ``out.move_to()``.
|
|
||||||
\endrst
|
|
||||||
*/
|
|
||||||
template <typename Char>
|
|
||||||
class BasicStringWriter : public basic_writer<Char> {
|
|
||||||
private:
|
|
||||||
internal::StringBuffer<Char> buffer_;
|
|
||||||
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
\rst
|
|
||||||
Constructs a :class:`fmt::BasicStringWriter` object.
|
|
||||||
\endrst
|
|
||||||
*/
|
|
||||||
BasicStringWriter() : basic_writer<Char>(buffer_) {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
\rst
|
|
||||||
Moves the buffer content to *str* clearing the buffer.
|
|
||||||
\endrst
|
|
||||||
*/
|
|
||||||
void move_to(std::basic_string<Char> &str) {
|
|
||||||
buffer_.move_to(str);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef BasicStringWriter<char> StringWriter;
|
|
||||||
typedef BasicStringWriter<wchar_t> WStringWriter;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
\rst
|
\rst
|
||||||
@ -110,9 +84,11 @@ typedef BasicStringWriter<wchar_t> WStringWriter;
|
|||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::string to_string(const T &value) {
|
std::string to_string(const T &value) {
|
||||||
fmt::MemoryWriter w;
|
string_buffer buf;
|
||||||
w.write(value);
|
basic_writer<char>(buf).write(value);
|
||||||
return w.str();
|
std::string str;
|
||||||
|
buf.move_to(str);
|
||||||
|
return str;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,10 +10,10 @@
|
|||||||
#include "fmt/string.h"
|
#include "fmt/string.h"
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
using fmt::internal::StringBuffer;
|
using fmt::string_buffer;
|
||||||
|
|
||||||
TEST(StringBufferTest, Empty) {
|
TEST(StringBufferTest, Empty) {
|
||||||
StringBuffer<char> buffer;
|
string_buffer buffer;
|
||||||
EXPECT_EQ(0, buffer.size());
|
EXPECT_EQ(0, buffer.size());
|
||||||
EXPECT_EQ(0, buffer.capacity());
|
EXPECT_EQ(0, buffer.capacity());
|
||||||
std::string data;
|
std::string data;
|
||||||
@ -25,7 +25,7 @@ TEST(StringBufferTest, Empty) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(StringBufferTest, Reserve) {
|
TEST(StringBufferTest, Reserve) {
|
||||||
StringBuffer<char> buffer;
|
string_buffer buffer;
|
||||||
std::size_t capacity = std::string().capacity() + 10;
|
std::size_t capacity = std::string().capacity() + 10;
|
||||||
buffer.reserve(capacity);
|
buffer.reserve(capacity);
|
||||||
EXPECT_EQ(0, buffer.size());
|
EXPECT_EQ(0, buffer.size());
|
||||||
@ -36,7 +36,7 @@ TEST(StringBufferTest, Reserve) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(StringBufferTest, Resize) {
|
TEST(StringBufferTest, Resize) {
|
||||||
StringBuffer<char> buffer;
|
string_buffer buffer;
|
||||||
std::size_t size = std::string().capacity() + 10;
|
std::size_t size = std::string().capacity() + 10;
|
||||||
buffer.resize(size);
|
buffer.resize(size);
|
||||||
EXPECT_EQ(size, buffer.size());
|
EXPECT_EQ(size, buffer.size());
|
||||||
@ -47,7 +47,7 @@ TEST(StringBufferTest, Resize) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(StringBufferTest, MoveTo) {
|
TEST(StringBufferTest, MoveTo) {
|
||||||
StringBuffer<char> buffer;
|
string_buffer buffer;
|
||||||
std::size_t size = std::string().capacity() + 10;
|
std::size_t size = std::string().capacity() + 10;
|
||||||
buffer.resize(size);
|
buffer.resize(size);
|
||||||
const char *p = &buffer[0];
|
const char *p = &buffer[0];
|
||||||
@ -58,25 +58,12 @@ TEST(StringBufferTest, MoveTo) {
|
|||||||
EXPECT_EQ(0, buffer.capacity());
|
EXPECT_EQ(0, buffer.capacity());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StringWriterTest, MoveTo) {
|
TEST(StringBufferTest, WString) {
|
||||||
fmt::StringWriter out;
|
fmt::wstring_buffer out;
|
||||||
out.write("The answer is ");
|
out.push_back(L'x');
|
||||||
out.write(42);
|
|
||||||
out.write("\n");
|
|
||||||
std::string s;
|
|
||||||
out.move_to(s);
|
|
||||||
EXPECT_EQ("The answer is 42\n", s);
|
|
||||||
EXPECT_EQ(0, out.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(StringWriterTest, WString) {
|
|
||||||
fmt::WStringWriter out;
|
|
||||||
out.write("The answer is ");
|
|
||||||
out.write(42);
|
|
||||||
out.write("\n");
|
|
||||||
std::wstring s;
|
std::wstring s;
|
||||||
out.move_to(s);
|
out.move_to(s);
|
||||||
EXPECT_EQ(L"The answer is 42\n", s);
|
EXPECT_EQ(L"x", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StringTest, ToString) {
|
TEST(StringTest, ToString) {
|
||||||
|
Loading…
Reference in New Issue
Block a user