Make Split() function work consistently with empty strings
Split() would return an array of one string if the original string
had no delimters. However in case the input is an empty string, the
result will be a vector with zero elements, even if purge_empty_strings
is set to false.
Change-Id: If0cd2cd07d21b89c647d6d5b2985796cc78370af
Reviewed-on: https://weave-review.googlesource.com/1765
Reviewed-by: Vitaly Buka <vitalybuka@google.com>
diff --git a/src/string_utils.cc b/src/string_utils.cc
index b5e71b8..d4386f0 100644
--- a/src/string_utils.cc
+++ b/src/string_utils.cc
@@ -45,9 +45,6 @@
bool trim_whitespaces,
bool purge_empty_strings) {
std::vector<std::string> tokens;
- if (str.empty())
- return tokens;
-
for (std::string::size_type i = 0;;) {
const std::string::size_type pos =
delimiter.empty() ? (i + 1) : str.find(delimiter, i);
diff --git a/src/string_utils_unittest.cc b/src/string_utils_unittest.cc
index 3966921..7b76d35 100644
--- a/src/string_utils_unittest.cc
+++ b/src/string_utils_unittest.cc
@@ -17,6 +17,10 @@
std::vector<std::string> parts;
parts = Split("", ",", false, false);
+ EXPECT_EQ(1u, parts.size());
+ EXPECT_EQ("", parts[0]);
+
+ parts = Split("", ",", false, true);
EXPECT_EQ(0u, parts.size());
parts = Split("abc", ",", false, false);