Loading base/include/base/strings.h +9 −4 Original line number Diff line number Diff line Loading @@ -23,10 +23,15 @@ namespace android { namespace base { // Splits a string using the given separator character into a vector of strings. // Empty strings will be omitted. void Split(const std::string& s, char separator, std::vector<std::string>* result); // Splits a string into a vector of strings. // // The string is split at each occurence 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); // Trims whitespace off both ends of the given string. std::string Trim(const std::string& s); Loading base/logging.cpp +1 −2 Original line number Diff line number Diff line Loading @@ -108,8 +108,7 @@ void InitLogging(char* argv[]) { return; } std::vector<std::string> specs; Split(tags, ' ', &specs); std::vector<std::string> specs = Split(tags, " "); for (size_t i = 0; i < specs.size(); ++i) { // "tag-pattern:[vdiwefs]" std::string spec(specs[i]); Loading base/strings.cpp +26 −14 Original line number Diff line number Diff line Loading @@ -16,27 +16,39 @@ #include "base/strings.h" #include <stdlib.h> #include <string> #include <vector> namespace android { namespace base { void Split(const std::string& s, char separator, std::vector<std::string>* result) { const char* p = s.data(); const char* end = p + s.size(); while (p != end) { if (*p == separator) { ++p; } else { const char* start = p; while (++p != end && *p != separator) { // Skip to the next occurrence of the separator. } result->push_back(std::string(start, p - start)); #define CHECK_NE(a, b) \ if ((a) == (b)) abort(); 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; } 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)); } base = found + 1; } while (found != s.npos); return split; } std::string Trim(const std::string& s) { Loading base/strings_test.cpp +27 −8 Original line number Diff line number Diff line Loading @@ -22,21 +22,18 @@ #include <vector> TEST(strings, split_empty) { std::vector<std::string> parts; android::base::Split("", '\0', &parts); std::vector<std::string> parts = android::base::Split("", ","); ASSERT_EQ(0U, parts.size()); } TEST(strings, split_single) { std::vector<std::string> parts; android::base::Split("foo", ',', &parts); std::vector<std::string> parts = android::base::Split("foo", ","); ASSERT_EQ(1U, parts.size()); ASSERT_EQ("foo", parts[0]); } TEST(strings, split_simple) { std::vector<std::string> parts; android::base::Split("foo,bar,baz", ',', &parts); std::vector<std::string> parts = android::base::Split("foo,bar,baz", ","); ASSERT_EQ(3U, parts.size()); ASSERT_EQ("foo", parts[0]); ASSERT_EQ("bar", parts[1]); Loading @@ -44,8 +41,30 @@ TEST(strings, split_simple) { } TEST(strings, split_with_empty_part) { std::vector<std::string> parts; android::base::Split("foo,,bar", ',', &parts); std::vector<std::string> parts = android::base::Split("foo,,bar", ","); ASSERT_EQ(2U, parts.size()); ASSERT_EQ("foo", parts[0]); ASSERT_EQ("bar", parts[1]); } TEST(strings, split_null_char) { std::vector<std::string> parts = android::base::Split(std::string("foo\0bar", 7), std::string("\0", 1)); ASSERT_EQ(2U, parts.size()); ASSERT_EQ("foo", parts[0]); ASSERT_EQ("bar", parts[1]); } TEST(strings, split_any) { std::vector<std::string> parts = android::base::Split("foo:bar,baz", ",:"); ASSERT_EQ(3U, parts.size()); ASSERT_EQ("foo", parts[0]); ASSERT_EQ("bar", parts[1]); ASSERT_EQ("baz", parts[2]); } TEST(strings, split_any_with_empty_part) { std::vector<std::string> parts = android::base::Split("foo:,bar", ",:"); ASSERT_EQ(2U, parts.size()); ASSERT_EQ("foo", parts[0]); ASSERT_EQ("bar", parts[1]); Loading Loading
base/include/base/strings.h +9 −4 Original line number Diff line number Diff line Loading @@ -23,10 +23,15 @@ namespace android { namespace base { // Splits a string using the given separator character into a vector of strings. // Empty strings will be omitted. void Split(const std::string& s, char separator, std::vector<std::string>* result); // Splits a string into a vector of strings. // // The string is split at each occurence 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); // Trims whitespace off both ends of the given string. std::string Trim(const std::string& s); Loading
base/logging.cpp +1 −2 Original line number Diff line number Diff line Loading @@ -108,8 +108,7 @@ void InitLogging(char* argv[]) { return; } std::vector<std::string> specs; Split(tags, ' ', &specs); std::vector<std::string> specs = Split(tags, " "); for (size_t i = 0; i < specs.size(); ++i) { // "tag-pattern:[vdiwefs]" std::string spec(specs[i]); Loading
base/strings.cpp +26 −14 Original line number Diff line number Diff line Loading @@ -16,27 +16,39 @@ #include "base/strings.h" #include <stdlib.h> #include <string> #include <vector> namespace android { namespace base { void Split(const std::string& s, char separator, std::vector<std::string>* result) { const char* p = s.data(); const char* end = p + s.size(); while (p != end) { if (*p == separator) { ++p; } else { const char* start = p; while (++p != end && *p != separator) { // Skip to the next occurrence of the separator. } result->push_back(std::string(start, p - start)); #define CHECK_NE(a, b) \ if ((a) == (b)) abort(); 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; } 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)); } base = found + 1; } while (found != s.npos); return split; } std::string Trim(const std::string& s) { Loading
base/strings_test.cpp +27 −8 Original line number Diff line number Diff line Loading @@ -22,21 +22,18 @@ #include <vector> TEST(strings, split_empty) { std::vector<std::string> parts; android::base::Split("", '\0', &parts); std::vector<std::string> parts = android::base::Split("", ","); ASSERT_EQ(0U, parts.size()); } TEST(strings, split_single) { std::vector<std::string> parts; android::base::Split("foo", ',', &parts); std::vector<std::string> parts = android::base::Split("foo", ","); ASSERT_EQ(1U, parts.size()); ASSERT_EQ("foo", parts[0]); } TEST(strings, split_simple) { std::vector<std::string> parts; android::base::Split("foo,bar,baz", ',', &parts); std::vector<std::string> parts = android::base::Split("foo,bar,baz", ","); ASSERT_EQ(3U, parts.size()); ASSERT_EQ("foo", parts[0]); ASSERT_EQ("bar", parts[1]); Loading @@ -44,8 +41,30 @@ TEST(strings, split_simple) { } TEST(strings, split_with_empty_part) { std::vector<std::string> parts; android::base::Split("foo,,bar", ',', &parts); std::vector<std::string> parts = android::base::Split("foo,,bar", ","); ASSERT_EQ(2U, parts.size()); ASSERT_EQ("foo", parts[0]); ASSERT_EQ("bar", parts[1]); } TEST(strings, split_null_char) { std::vector<std::string> parts = android::base::Split(std::string("foo\0bar", 7), std::string("\0", 1)); ASSERT_EQ(2U, parts.size()); ASSERT_EQ("foo", parts[0]); ASSERT_EQ("bar", parts[1]); } TEST(strings, split_any) { std::vector<std::string> parts = android::base::Split("foo:bar,baz", ",:"); ASSERT_EQ(3U, parts.size()); ASSERT_EQ("foo", parts[0]); ASSERT_EQ("bar", parts[1]); ASSERT_EQ("baz", parts[2]); } TEST(strings, split_any_with_empty_part) { std::vector<std::string> parts = android::base::Split("foo:,bar", ",:"); ASSERT_EQ(2U, parts.size()); ASSERT_EQ("foo", parts[0]); ASSERT_EQ("bar", parts[1]); Loading