Skip to content

Commit 7a39172

Browse files
authored
Merge pull request Tencent#746 from miloyip/issue744_stringbuffergetlength
Add StringBuffer::GetLength()
2 parents 0761ac1 + 5cd62c2 commit 7a39172

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

include/rapidjson/stringbuffer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,12 @@ class GenericStringBuffer {
7878
return stack_.template Bottom<Ch>();
7979
}
8080

81+
//! Get the size of string in bytes in the string buffer.
8182
size_t GetSize() const { return stack_.GetSize(); }
8283

84+
//! Get the length of string in Ch in the string buffer.
85+
size_t GetLength() const { return stack_.GetSize() / sizeof(Ch); }
86+
8387
static const size_t kDefaultCapacity = 256;
8488
mutable internal::Stack<Allocator> stack_;
8589

test/unittest/stringbuffertest.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ using namespace rapidjson;
2626
TEST(StringBuffer, InitialSize) {
2727
StringBuffer buffer;
2828
EXPECT_EQ(0u, buffer.GetSize());
29+
EXPECT_EQ(0u, buffer.GetLength());
2930
EXPECT_STREQ("", buffer.GetString());
3031
}
3132

@@ -34,14 +35,17 @@ TEST(StringBuffer, Put) {
3435
buffer.Put('A');
3536

3637
EXPECT_EQ(1u, buffer.GetSize());
38+
EXPECT_EQ(1u, buffer.GetLength());
3739
EXPECT_STREQ("A", buffer.GetString());
3840
}
3941

4042
TEST(StringBuffer, PutN_Issue672) {
4143
GenericStringBuffer<UTF8<>, MemoryPoolAllocator<> > buffer;
4244
EXPECT_EQ(0, buffer.GetSize());
45+
EXPECT_EQ(0, buffer.GetLength());
4346
rapidjson::PutN(buffer, ' ', 1);
4447
EXPECT_EQ(1, buffer.GetSize());
48+
EXPECT_EQ(1, buffer.GetLength());
4549
}
4650

4751
TEST(StringBuffer, Clear) {
@@ -52,6 +56,7 @@ TEST(StringBuffer, Clear) {
5256
buffer.Clear();
5357

5458
EXPECT_EQ(0u, buffer.GetSize());
59+
EXPECT_EQ(0u, buffer.GetLength());
5560
EXPECT_STREQ("", buffer.GetString());
5661
}
5762

@@ -60,6 +65,7 @@ TEST(StringBuffer, Push) {
6065
buffer.Push(5);
6166

6267
EXPECT_EQ(5u, buffer.GetSize());
68+
EXPECT_EQ(5u, buffer.GetLength());
6369

6470
// Causes sudden expansion to make the stack's capacity equal to size
6571
buffer.Push(65536u);
@@ -76,9 +82,19 @@ TEST(StringBuffer, Pop) {
7682
buffer.Pop(3);
7783

7884
EXPECT_EQ(2u, buffer.GetSize());
85+
EXPECT_EQ(2u, buffer.GetLength());
7986
EXPECT_STREQ("AB", buffer.GetString());
8087
}
8188

89+
TEST(StringBuffer, GetLength_Issue744) {
90+
GenericStringBuffer<UTF16<wchar_t> > buffer;
91+
buffer.Put('A');
92+
buffer.Put('B');
93+
buffer.Put('C');
94+
EXPECT_EQ(3u * sizeof(wchar_t), buffer.GetSize());
95+
EXPECT_EQ(3u, buffer.GetLength());
96+
}
97+
8298
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
8399

84100
#if 0 // Many old compiler does not support these. Turn it off temporaily.
@@ -130,18 +146,23 @@ TEST(StringBuffer, MoveConstructor) {
130146
x.Put('D');
131147

132148
EXPECT_EQ(4u, x.GetSize());
149+
EXPECT_EQ(4u, x.GetLength());
133150
EXPECT_STREQ("ABCD", x.GetString());
134151

135152
// StringBuffer y(x); // does not compile (!is_copy_constructible)
136153
StringBuffer y(std::move(x));
137154
EXPECT_EQ(0u, x.GetSize());
155+
EXPECT_EQ(0u, x.GetLength());
138156
EXPECT_EQ(4u, y.GetSize());
157+
EXPECT_EQ(4u, y.GetLength());
139158
EXPECT_STREQ("ABCD", y.GetString());
140159

141160
// StringBuffer z = y; // does not compile (!is_copy_assignable)
142161
StringBuffer z = std::move(y);
143162
EXPECT_EQ(0u, y.GetSize());
163+
EXPECT_EQ(0u, y.GetLength());
144164
EXPECT_EQ(4u, z.GetSize());
165+
EXPECT_EQ(4u, z.GetLength());
145166
EXPECT_STREQ("ABCD", z.GetString());
146167
}
147168

@@ -153,13 +174,14 @@ TEST(StringBuffer, MoveAssignment) {
153174
x.Put('D');
154175

155176
EXPECT_EQ(4u, x.GetSize());
177+
EXPECT_EQ(4u, x.GetLength());
156178
EXPECT_STREQ("ABCD", x.GetString());
157179

158180
StringBuffer y;
159181
// y = x; // does not compile (!is_copy_assignable)
160182
y = std::move(x);
161183
EXPECT_EQ(0u, x.GetSize());
162-
EXPECT_EQ(4u, y.GetSize());
184+
EXPECT_EQ(4u, y.GetLength());
163185
EXPECT_STREQ("ABCD", y.GetString());
164186
}
165187

0 commit comments

Comments
 (0)