Commit 5cd62c23 authored by Milo Yip's avatar Milo Yip

Add StringBuffer::GetLength()

Fix #744
parent 0761ac12
...@@ -78,8 +78,12 @@ public: ...@@ -78,8 +78,12 @@ public:
return stack_.template Bottom<Ch>(); return stack_.template Bottom<Ch>();
} }
//! Get the size of string in bytes in the string buffer.
size_t GetSize() const { return stack_.GetSize(); } size_t GetSize() const { return stack_.GetSize(); }
//! Get the length of string in Ch in the string buffer.
size_t GetLength() const { return stack_.GetSize() / sizeof(Ch); }
static const size_t kDefaultCapacity = 256; static const size_t kDefaultCapacity = 256;
mutable internal::Stack<Allocator> stack_; mutable internal::Stack<Allocator> stack_;
......
...@@ -26,6 +26,7 @@ using namespace rapidjson; ...@@ -26,6 +26,7 @@ using namespace rapidjson;
TEST(StringBuffer, InitialSize) { TEST(StringBuffer, InitialSize) {
StringBuffer buffer; StringBuffer buffer;
EXPECT_EQ(0u, buffer.GetSize()); EXPECT_EQ(0u, buffer.GetSize());
EXPECT_EQ(0u, buffer.GetLength());
EXPECT_STREQ("", buffer.GetString()); EXPECT_STREQ("", buffer.GetString());
} }
...@@ -34,14 +35,17 @@ TEST(StringBuffer, Put) { ...@@ -34,14 +35,17 @@ TEST(StringBuffer, Put) {
buffer.Put('A'); buffer.Put('A');
EXPECT_EQ(1u, buffer.GetSize()); EXPECT_EQ(1u, buffer.GetSize());
EXPECT_EQ(1u, buffer.GetLength());
EXPECT_STREQ("A", buffer.GetString()); EXPECT_STREQ("A", buffer.GetString());
} }
TEST(StringBuffer, PutN_Issue672) { TEST(StringBuffer, PutN_Issue672) {
GenericStringBuffer<UTF8<>, MemoryPoolAllocator<> > buffer; GenericStringBuffer<UTF8<>, MemoryPoolAllocator<> > buffer;
EXPECT_EQ(0, buffer.GetSize()); EXPECT_EQ(0, buffer.GetSize());
EXPECT_EQ(0, buffer.GetLength());
rapidjson::PutN(buffer, ' ', 1); rapidjson::PutN(buffer, ' ', 1);
EXPECT_EQ(1, buffer.GetSize()); EXPECT_EQ(1, buffer.GetSize());
EXPECT_EQ(1, buffer.GetLength());
} }
TEST(StringBuffer, Clear) { TEST(StringBuffer, Clear) {
...@@ -52,6 +56,7 @@ TEST(StringBuffer, Clear) { ...@@ -52,6 +56,7 @@ TEST(StringBuffer, Clear) {
buffer.Clear(); buffer.Clear();
EXPECT_EQ(0u, buffer.GetSize()); EXPECT_EQ(0u, buffer.GetSize());
EXPECT_EQ(0u, buffer.GetLength());
EXPECT_STREQ("", buffer.GetString()); EXPECT_STREQ("", buffer.GetString());
} }
...@@ -60,6 +65,7 @@ TEST(StringBuffer, Push) { ...@@ -60,6 +65,7 @@ TEST(StringBuffer, Push) {
buffer.Push(5); buffer.Push(5);
EXPECT_EQ(5u, buffer.GetSize()); EXPECT_EQ(5u, buffer.GetSize());
EXPECT_EQ(5u, buffer.GetLength());
// Causes sudden expansion to make the stack's capacity equal to size // Causes sudden expansion to make the stack's capacity equal to size
buffer.Push(65536u); buffer.Push(65536u);
...@@ -76,9 +82,19 @@ TEST(StringBuffer, Pop) { ...@@ -76,9 +82,19 @@ TEST(StringBuffer, Pop) {
buffer.Pop(3); buffer.Pop(3);
EXPECT_EQ(2u, buffer.GetSize()); EXPECT_EQ(2u, buffer.GetSize());
EXPECT_EQ(2u, buffer.GetLength());
EXPECT_STREQ("AB", buffer.GetString()); EXPECT_STREQ("AB", buffer.GetString());
} }
TEST(StringBuffer, GetLength_Issue744) {
GenericStringBuffer<UTF16<wchar_t> > buffer;
buffer.Put('A');
buffer.Put('B');
buffer.Put('C');
EXPECT_EQ(3u * sizeof(wchar_t), buffer.GetSize());
EXPECT_EQ(3u, buffer.GetLength());
}
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
#if 0 // Many old compiler does not support these. Turn it off temporaily. #if 0 // Many old compiler does not support these. Turn it off temporaily.
...@@ -130,18 +146,23 @@ TEST(StringBuffer, MoveConstructor) { ...@@ -130,18 +146,23 @@ TEST(StringBuffer, MoveConstructor) {
x.Put('D'); x.Put('D');
EXPECT_EQ(4u, x.GetSize()); EXPECT_EQ(4u, x.GetSize());
EXPECT_EQ(4u, x.GetLength());
EXPECT_STREQ("ABCD", x.GetString()); EXPECT_STREQ("ABCD", x.GetString());
// StringBuffer y(x); // does not compile (!is_copy_constructible) // StringBuffer y(x); // does not compile (!is_copy_constructible)
StringBuffer y(std::move(x)); StringBuffer y(std::move(x));
EXPECT_EQ(0u, x.GetSize()); EXPECT_EQ(0u, x.GetSize());
EXPECT_EQ(0u, x.GetLength());
EXPECT_EQ(4u, y.GetSize()); EXPECT_EQ(4u, y.GetSize());
EXPECT_EQ(4u, y.GetLength());
EXPECT_STREQ("ABCD", y.GetString()); EXPECT_STREQ("ABCD", y.GetString());
// StringBuffer z = y; // does not compile (!is_copy_assignable) // StringBuffer z = y; // does not compile (!is_copy_assignable)
StringBuffer z = std::move(y); StringBuffer z = std::move(y);
EXPECT_EQ(0u, y.GetSize()); EXPECT_EQ(0u, y.GetSize());
EXPECT_EQ(0u, y.GetLength());
EXPECT_EQ(4u, z.GetSize()); EXPECT_EQ(4u, z.GetSize());
EXPECT_EQ(4u, z.GetLength());
EXPECT_STREQ("ABCD", z.GetString()); EXPECT_STREQ("ABCD", z.GetString());
} }
...@@ -153,13 +174,14 @@ TEST(StringBuffer, MoveAssignment) { ...@@ -153,13 +174,14 @@ TEST(StringBuffer, MoveAssignment) {
x.Put('D'); x.Put('D');
EXPECT_EQ(4u, x.GetSize()); EXPECT_EQ(4u, x.GetSize());
EXPECT_EQ(4u, x.GetLength());
EXPECT_STREQ("ABCD", x.GetString()); EXPECT_STREQ("ABCD", x.GetString());
StringBuffer y; StringBuffer y;
// y = x; // does not compile (!is_copy_assignable) // y = x; // does not compile (!is_copy_assignable)
y = std::move(x); y = std::move(x);
EXPECT_EQ(0u, x.GetSize()); EXPECT_EQ(0u, x.GetSize());
EXPECT_EQ(4u, y.GetSize()); EXPECT_EQ(4u, y.GetLength());
EXPECT_STREQ("ABCD", y.GetString()); EXPECT_STREQ("ABCD", y.GetString());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment