Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
brpc
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
brpc
Commits
b60143ea
Commit
b60143ea
authored
Apr 16, 2019
by
zhujiashun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support_kv_pair_splitter: make tests more robust & refine name
parent
55f18449
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
36 additions
and
39 deletions
+36
-39
string_splitter.h
src/butil/string_splitter.h
+9
-9
string_splitter_inl.h
src/butil/string_splitter_inl.h
+3
-3
string_splitter_unittest.cpp
test/string_splitter_unittest.cpp
+24
-27
No files found.
src/butil/string_splitter.h
View file @
b60143ea
...
...
@@ -169,10 +169,10 @@ private:
// This class can also handle some exceptional cases.
// 1. consecutive key_value_pair_delimiter are omitted, for example,
// suppose key_value_delimiter is '=' and key_value_pair_delimiter
// is '&', then
k1=v1&&&k2=v2 is normalized to k1=k2&k2=v2
.
// 2. key or value can be empty or both can be empty
// is '&', then
'k1=v1&&&k2=v2' is normalized to 'k1=k2&k2=v2'
.
// 2. key or value can be empty or both can be empty
.
// 3. consecutive key_value_delimiter are not omitted, for example,
// suppose input is
k1===v2
and key_value_delimiter is '=', then
// suppose input is
'k1===v2'
and key_value_delimiter is '=', then
// key() returns 'k1', value() returns '==v2'.
class
KeyValuePairsSplitter
{
public
:
...
...
@@ -181,8 +181,8 @@ public:
char
key_value_delimiter
,
char
key_value_pair_delimiter
)
:
_sp
(
str_begin
,
str_end
,
key_value_pair_delimiter
)
,
_deli_pos
(
StringPiece
::
npos
)
,
_key_value_delim
iter
(
key_value_delimiter
)
{
,
_deli
m
_pos
(
StringPiece
::
npos
)
,
_key_value_delim
(
key_value_delimiter
)
{
UpdateDelimiterPosition
();
}
...
...
@@ -199,11 +199,11 @@ public:
key_value_delimiter
,
key_value_pair_delimiter
)
{}
inline
StringPiece
key
()
{
return
key_and_value
().
substr
(
0
,
_deli_pos
);
return
key_and_value
().
substr
(
0
,
_deli
m
_pos
);
}
inline
StringPiece
value
()
{
return
key_and_value
().
substr
(
_deli_pos
+
1
);
return
key_and_value
().
substr
(
_deli
m
_pos
+
1
);
}
// Get the current value of key and value
...
...
@@ -232,8 +232,8 @@ private:
private
:
StringSplitter
_sp
;
StringPiece
::
size_type
_deli_pos
;
const
char
_key_value_delim
iter
;
StringPiece
::
size_type
_deli
m
_pos
;
const
char
_key_value_delim
;
};
}
// namespace butil
...
...
src/butil/string_splitter_inl.h
View file @
b60143ea
...
...
@@ -318,9 +318,9 @@ int StringMultiSplitter::to_double(double* pv) const {
void
KeyValuePairsSplitter
::
UpdateDelimiterPosition
()
{
const
StringPiece
key_value_pair
(
key_and_value
());
_deli
_pos
=
key_value_pair
.
find
(
_key_value_delimiter
);
if
(
_deli_pos
==
StringPiece
::
npos
)
{
_deli_pos
=
key_value_pair
.
length
();
_deli
m_pos
=
key_value_pair
.
find
(
_key_value_delim
);
if
(
_deli
m
_pos
==
StringPiece
::
npos
)
{
_deli
m
_pos
=
key_value_pair
.
length
();
}
}
...
...
test/string_splitter_unittest.cpp
View file @
b60143ea
...
...
@@ -338,9 +338,20 @@ TEST_F(StringSplitterTest, split_limit_len) {
}
TEST_F
(
StringSplitterTest
,
key_value_pairs_splitter_sanity
)
{
std
::
string
kvstr
=
"key1=value1&&&key2=value2&key3=value3"
;
{
butil
::
KeyValuePairsSplitter
splitter
(
kvstr
,
'='
,
'&'
);
std
::
string
kvstr
=
"key1=value1&&&key2=value2&key3=value3&===&key4=&=&=value5"
;
for
(
int
i
=
0
;
i
<
3
;
++
i
)
{
// Test three constructors
butil
::
KeyValuePairsSplitter
*
psplitter
=
NULL
;
if
(
i
==
0
)
{
psplitter
=
new
butil
::
KeyValuePairsSplitter
(
kvstr
,
'='
,
'&'
);
}
else
if
(
i
==
1
)
{
psplitter
=
new
butil
::
KeyValuePairsSplitter
(
kvstr
.
data
(),
kvstr
.
data
()
+
kvstr
.
size
(),
'='
,
'&'
);
}
else
if
(
i
==
2
)
{
psplitter
=
new
butil
::
KeyValuePairsSplitter
(
kvstr
.
c_str
(),
'='
,
'&'
);
}
butil
::
KeyValuePairsSplitter
&
splitter
=
*
psplitter
;
ASSERT_TRUE
(
splitter
);
ASSERT_EQ
(
splitter
.
key
(),
"key1"
);
ASSERT_EQ
(
splitter
.
value
(),
"value1"
);
...
...
@@ -353,39 +364,25 @@ TEST_F(StringSplitterTest, key_value_pairs_splitter_sanity) {
ASSERT_EQ
(
splitter
.
key
(),
"key3"
);
ASSERT_EQ
(
splitter
.
value
(),
"value3"
);
++
splitter
;
ASSERT_FALSE
(
splitter
);
}
{
butil
::
KeyValuePairsSplitter
splitter
(
kvstr
.
data
(),
kvstr
.
data
()
+
kvstr
.
size
(),
'='
,
'&'
);
ASSERT_TRUE
(
splitter
);
ASSERT_EQ
(
splitter
.
key
(),
"key1"
);
ASSERT_EQ
(
splitter
.
value
(),
"value1"
);
++
splitter
;
ASSERT_TRUE
(
splitter
);
ASSERT_EQ
(
splitter
.
key
(),
"key2"
);
ASSERT_EQ
(
splitter
.
value
(),
"value2"
);
++
splitter
;
ASSERT_TRUE
(
splitter
);
ASSERT_EQ
(
splitter
.
key
(),
"
key3
"
);
ASSERT_EQ
(
splitter
.
value
(),
"
value3
"
);
ASSERT_EQ
(
splitter
.
key
(),
""
);
ASSERT_EQ
(
splitter
.
value
(),
"
==
"
);
++
splitter
;
ASSERT_FALSE
(
splitter
);
}
{
butil
::
KeyValuePairsSplitter
splitter
(
kvstr
.
c_str
(),
'='
,
'&'
);
ASSERT_TRUE
(
splitter
);
ASSERT_EQ
(
splitter
.
key
(),
"key
1
"
);
ASSERT_EQ
(
splitter
.
value
(),
"
value1
"
);
ASSERT_EQ
(
splitter
.
key
(),
"key
4
"
);
ASSERT_EQ
(
splitter
.
value
(),
""
);
++
splitter
;
ASSERT_TRUE
(
splitter
);
ASSERT_EQ
(
splitter
.
key
(),
"
key2
"
);
ASSERT_EQ
(
splitter
.
value
(),
"
value2
"
);
ASSERT_EQ
(
splitter
.
key
(),
""
);
ASSERT_EQ
(
splitter
.
value
(),
""
);
++
splitter
;
ASSERT_TRUE
(
splitter
);
ASSERT_EQ
(
splitter
.
key
(),
"
key3
"
);
ASSERT_EQ
(
splitter
.
value
(),
"value
3
"
);
ASSERT_EQ
(
splitter
.
key
(),
""
);
ASSERT_EQ
(
splitter
.
value
(),
"value
5
"
);
++
splitter
;
ASSERT_FALSE
(
splitter
);
delete
psplitter
;
}
}
...
...
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