diff options
author | Elliott Hughes <enh@google.com> | 2015-04-24 23:02:00 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2015-04-28 10:55:24 -0700 |
commit | d81f75ae41b2eed4ae7b0911f250778f3e6ec9c2 (patch) | |
tree | e7a5cf561d9ed9e2cfba4bcac39d14297fecb3a0 /base | |
parent | 03a90d663363c904d5b874f2de204a055c763b3c (diff) | |
download | system_core-d81f75ae41b2eed4ae7b0911f250778f3e6ec9c2.zip system_core-d81f75ae41b2eed4ae7b0911f250778f3e6ec9c2.tar.gz system_core-d81f75ae41b2eed4ae7b0911f250778f3e6ec9c2.tar.bz2 |
Remove strtok from adb.
Also fix android::base::Split to behave like Java, Python, and google3.
(cherry picked from commit 8d5fa6da44d56511b3e173bc463cbc65ff221b4a)
Change-Id: I9388ae37ee8dd4a4a6c2a9a19f068b70d9a78353
Diffstat (limited to 'base')
-rw-r--r-- | base/include/base/strings.h | 2 | ||||
-rw-r--r-- | base/strings.cpp | 13 | ||||
-rw-r--r-- | base/strings_test.cpp | 13 |
3 files changed, 11 insertions, 17 deletions
diff --git a/base/include/base/strings.h b/base/include/base/strings.h index 3559342..5dbc5fb 100644 --- a/base/include/base/strings.h +++ b/base/include/base/strings.h @@ -27,8 +27,6 @@ namespace base { // // The string is split at each occurrence of a character in delimiters. // -// Empty splits will be omitted. I.e. Split("a,,b", ",") -> {"a", "b"} -// // The empty string is not a valid delimiter list. std::vector<std::string> Split(const std::string& s, const std::string& delimiters); diff --git a/base/strings.cpp b/base/strings.cpp index 6f698d9..d3375d9 100644 --- a/base/strings.cpp +++ b/base/strings.cpp @@ -32,24 +32,17 @@ std::vector<std::string> Split(const std::string& s, const std::string& delimiters) { CHECK_NE(delimiters.size(), 0U); - std::vector<std::string> split; - if (s.size() == 0) { - // Split("", d) returns {} rather than {""}. - return split; - } + std::vector<std::string> result; size_t base = 0; size_t found; do { found = s.find_first_of(delimiters, base); - if (found != base) { - split.push_back(s.substr(base, found - base)); - } - + result.push_back(s.substr(base, found - base)); base = found + 1; } while (found != s.npos); - return split; + return result; } std::string Trim(const std::string& s) { diff --git a/base/strings_test.cpp b/base/strings_test.cpp index 1bf07a1..46a1ab5 100644 --- a/base/strings_test.cpp +++ b/base/strings_test.cpp @@ -23,7 +23,8 @@ TEST(strings, split_empty) { std::vector<std::string> parts = android::base::Split("", ","); - ASSERT_EQ(0U, parts.size()); + ASSERT_EQ(1U, parts.size()); + ASSERT_EQ("", parts[0]); } TEST(strings, split_single) { @@ -42,9 +43,10 @@ TEST(strings, split_simple) { TEST(strings, split_with_empty_part) { std::vector<std::string> parts = android::base::Split("foo,,bar", ","); - ASSERT_EQ(2U, parts.size()); + ASSERT_EQ(3U, parts.size()); ASSERT_EQ("foo", parts[0]); - ASSERT_EQ("bar", parts[1]); + ASSERT_EQ("", parts[1]); + ASSERT_EQ("bar", parts[2]); } TEST(strings, split_null_char) { @@ -65,9 +67,10 @@ TEST(strings, split_any) { TEST(strings, split_any_with_empty_part) { std::vector<std::string> parts = android::base::Split("foo:,bar", ",:"); - ASSERT_EQ(2U, parts.size()); + ASSERT_EQ(3U, parts.size()); ASSERT_EQ("foo", parts[0]); - ASSERT_EQ("bar", parts[1]); + ASSERT_EQ("", parts[1]); + ASSERT_EQ("bar", parts[2]); } TEST(strings, trim_empty) { |