Commit cd02be84 authored by wanglun's avatar wanglun

StringMultiSplitter not supporting '\0' separator

parent f4f7fc39
...@@ -115,15 +115,11 @@ public: ...@@ -115,15 +115,11 @@ public:
// longer than this utility. // longer than this utility.
inline StringMultiSplitter(const char* input, const char* separators, inline StringMultiSplitter(const char* input, const char* separators,
EmptyFieldAction action = SKIP_EMPTY_FIELD); EmptyFieldAction action = SKIP_EMPTY_FIELD);
// Allows containing embedded '\0' characters, if str_end is not NULL. // Allows containing embedded '\0' characters if str_end is not NULL.
// NOTE: `separators` cannot contain embedded '\0' character.
inline StringMultiSplitter(const char* str_begin, const char* str_end, inline StringMultiSplitter(const char* str_begin, const char* str_end,
const char* separators, const char* separators,
EmptyFieldAction action = SKIP_EMPTY_FIELD); EmptyFieldAction action = SKIP_EMPTY_FIELD);
// Allows separators containing '\0', if str_end and separators_end are
// both not NULL.
inline StringMultiSplitter(const char* str_begin, const char* str_end,
const char* seps_begin, const char* seps_end,
EmptyFieldAction action = SKIP_EMPTY_FIELD);
// Move splitter forward. // Move splitter forward.
inline StringMultiSplitter& operator++(); inline StringMultiSplitter& operator++();
...@@ -160,7 +156,6 @@ private: ...@@ -160,7 +156,6 @@ private:
const char* _tail; const char* _tail;
const char* _str_tail; const char* _str_tail;
const char* const _seps; const char* const _seps;
const char* const _seps_tail;
const EmptyFieldAction _empty_field_action; const EmptyFieldAction _empty_field_action;
}; };
......
...@@ -167,7 +167,6 @@ StringMultiSplitter::StringMultiSplitter ( ...@@ -167,7 +167,6 @@ StringMultiSplitter::StringMultiSplitter (
: _head(str) : _head(str)
, _str_tail(NULL) , _str_tail(NULL)
, _seps(seps) , _seps(seps)
, _seps_tail(NULL)
, _empty_field_action(action) { , _empty_field_action(action) {
init(); init();
} }
...@@ -178,18 +177,6 @@ StringMultiSplitter::StringMultiSplitter ( ...@@ -178,18 +177,6 @@ StringMultiSplitter::StringMultiSplitter (
: _head(str_begin) : _head(str_begin)
, _str_tail(str_end) , _str_tail(str_end)
, _seps(seps) , _seps(seps)
, _seps_tail(NULL)
, _empty_field_action(action) {
init();
}
StringMultiSplitter::StringMultiSplitter (
const char* str_begin, const char* str_end,
const char* seps_begin, const char* seps_end, EmptyFieldAction action)
: _head(str_begin)
, _str_tail(str_end)
, _seps(seps_begin)
, _seps_tail(seps_end)
, _empty_field_action(action) { , _empty_field_action(action) {
init(); init();
} }
...@@ -226,8 +213,7 @@ StringMultiSplitter StringMultiSplitter::operator++(int) { ...@@ -226,8 +213,7 @@ StringMultiSplitter StringMultiSplitter::operator++(int) {
} }
bool StringMultiSplitter::is_sep(char c) const { bool StringMultiSplitter::is_sep(char c) const {
for (const char* p = _seps; for (const char* p = _seps; *p != '\0'; ++p) {
(_seps_tail == NULL) ? (*p != '\0') : (p != _seps_tail); ++p) {
if (c == *p) { if (c == *p) {
return true; return true;
} }
......
...@@ -227,44 +227,6 @@ TEST_F(StringSplitterTest, number_list) { ...@@ -227,44 +227,6 @@ TEST_F(StringSplitterTest, number_list) {
ASSERT_FALSE(ss2); ASSERT_FALSE(ss2);
ASSERT_EQ(0ul, ss2.length()); ASSERT_EQ(0ul, ss2.length());
ASSERT_EQ(ss2.field(), str + str_len); ASSERT_EQ(ss2.field(), str + str_len);
// separators contains '\0'
const char* seps = ", \0";
const size_t seps_len = 3;
butil::StringMultiSplitter ss3(str, str + str_len, seps, seps + seps_len);
ASSERT_TRUE(ss3);
ASSERT_EQ(3ul, ss3.length());
ASSERT_FALSE(strncmp(ss3.field(), "123", ss3.length()));
ss3++;
ASSERT_TRUE(ss3);
ASSERT_EQ(2ul, ss3.length());
ASSERT_FALSE(strncmp(ss3.field(), "12", ss3.length()));
ss3++;
ASSERT_TRUE(ss3);
ASSERT_EQ(1ul, ss3.length());
ASSERT_FALSE(strncmp(ss3.field(), "1", ss3.length()));
ss3++;
ASSERT_TRUE(ss3);
ASSERT_EQ(2ul, ss3.length());
ASSERT_FALSE(strncmp(ss3.field(), "21", ss3.length()));
ss3++;
ASSERT_TRUE(ss3);
ASSERT_EQ(4ul, ss3.length());
ASSERT_FALSE(strncmp(ss3.field(), "4321", ss3.length()));
ss3++;
ASSERT_TRUE(ss3);
ASSERT_EQ(2ul, ss3.length());
ASSERT_FALSE(strncmp(ss3.field(), "56", ss3.length()));
++ss3;
ASSERT_FALSE(ss3);
ASSERT_EQ(0ul, ss3.length());
ASSERT_EQ(ss3.field(), str + str_len);
} }
TEST_F(StringSplitterTest, cast_type) { TEST_F(StringSplitterTest, cast_type) {
......
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