Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
R
rapidjson
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
rapidjson
Commits
5cd62c23
Commit
5cd62c23
authored
Sep 22, 2016
by
Milo Yip
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add StringBuffer::GetLength()
Fix #744
parent
0761ac12
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
1 deletion
+27
-1
stringbuffer.h
include/rapidjson/stringbuffer.h
+4
-0
stringbuffertest.cpp
test/unittest/stringbuffertest.cpp
+23
-1
No files found.
include/rapidjson/stringbuffer.h
View file @
5cd62c23
...
...
@@ -78,8 +78,12 @@ public:
return
stack_
.
template
Bottom
<
Ch
>
();
}
//! Get the size of string in bytes in the string buffer.
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
;
mutable
internal
::
Stack
<
Allocator
>
stack_
;
...
...
test/unittest/stringbuffertest.cpp
View file @
5cd62c23
...
...
@@ -26,6 +26,7 @@ using namespace rapidjson;
TEST
(
StringBuffer
,
InitialSize
)
{
StringBuffer
buffer
;
EXPECT_EQ
(
0u
,
buffer
.
GetSize
());
EXPECT_EQ
(
0u
,
buffer
.
GetLength
());
EXPECT_STREQ
(
""
,
buffer
.
GetString
());
}
...
...
@@ -34,14 +35,17 @@ TEST(StringBuffer, Put) {
buffer
.
Put
(
'A'
);
EXPECT_EQ
(
1u
,
buffer
.
GetSize
());
EXPECT_EQ
(
1u
,
buffer
.
GetLength
());
EXPECT_STREQ
(
"A"
,
buffer
.
GetString
());
}
TEST
(
StringBuffer
,
PutN_Issue672
)
{
GenericStringBuffer
<
UTF8
<>
,
MemoryPoolAllocator
<>
>
buffer
;
EXPECT_EQ
(
0
,
buffer
.
GetSize
());
EXPECT_EQ
(
0
,
buffer
.
GetLength
());
rapidjson
::
PutN
(
buffer
,
' '
,
1
);
EXPECT_EQ
(
1
,
buffer
.
GetSize
());
EXPECT_EQ
(
1
,
buffer
.
GetLength
());
}
TEST
(
StringBuffer
,
Clear
)
{
...
...
@@ -52,6 +56,7 @@ TEST(StringBuffer, Clear) {
buffer
.
Clear
();
EXPECT_EQ
(
0u
,
buffer
.
GetSize
());
EXPECT_EQ
(
0u
,
buffer
.
GetLength
());
EXPECT_STREQ
(
""
,
buffer
.
GetString
());
}
...
...
@@ -60,6 +65,7 @@ TEST(StringBuffer, Push) {
buffer
.
Push
(
5
);
EXPECT_EQ
(
5u
,
buffer
.
GetSize
());
EXPECT_EQ
(
5u
,
buffer
.
GetLength
());
// Causes sudden expansion to make the stack's capacity equal to size
buffer
.
Push
(
65536u
);
...
...
@@ -76,9 +82,19 @@ TEST(StringBuffer, Pop) {
buffer
.
Pop
(
3
);
EXPECT_EQ
(
2u
,
buffer
.
GetSize
());
EXPECT_EQ
(
2u
,
buffer
.
GetLength
());
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 0 // Many old compiler does not support these. Turn it off temporaily.
...
...
@@ -130,18 +146,23 @@ TEST(StringBuffer, MoveConstructor) {
x
.
Put
(
'D'
);
EXPECT_EQ
(
4u
,
x
.
GetSize
());
EXPECT_EQ
(
4u
,
x
.
GetLength
());
EXPECT_STREQ
(
"ABCD"
,
x
.
GetString
());
// StringBuffer y(x); // does not compile (!is_copy_constructible)
StringBuffer
y
(
std
::
move
(
x
));
EXPECT_EQ
(
0u
,
x
.
GetSize
());
EXPECT_EQ
(
0u
,
x
.
GetLength
());
EXPECT_EQ
(
4u
,
y
.
GetSize
());
EXPECT_EQ
(
4u
,
y
.
GetLength
());
EXPECT_STREQ
(
"ABCD"
,
y
.
GetString
());
// StringBuffer z = y; // does not compile (!is_copy_assignable)
StringBuffer
z
=
std
::
move
(
y
);
EXPECT_EQ
(
0u
,
y
.
GetSize
());
EXPECT_EQ
(
0u
,
y
.
GetLength
());
EXPECT_EQ
(
4u
,
z
.
GetSize
());
EXPECT_EQ
(
4u
,
z
.
GetLength
());
EXPECT_STREQ
(
"ABCD"
,
z
.
GetString
());
}
...
...
@@ -153,13 +174,14 @@ TEST(StringBuffer, MoveAssignment) {
x
.
Put
(
'D'
);
EXPECT_EQ
(
4u
,
x
.
GetSize
());
EXPECT_EQ
(
4u
,
x
.
GetLength
());
EXPECT_STREQ
(
"ABCD"
,
x
.
GetString
());
StringBuffer
y
;
// y = x; // does not compile (!is_copy_assignable)
y
=
std
::
move
(
x
);
EXPECT_EQ
(
0u
,
x
.
GetSize
());
EXPECT_EQ
(
4u
,
y
.
Get
Size
());
EXPECT_EQ
(
4u
,
y
.
Get
Length
());
EXPECT_STREQ
(
"ABCD"
,
y
.
GetString
());
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment