Add Tokenize() that splits string tokens
Add Tokenize(std::string, std::string) which is like Split() but only
return nonempty string pieces. In other words, works like strtok().
Bug: 177977370
Test: atest libbase_test:strings
Test: atest --host libbase_test:strings
Change-Id: Ie422f97280a0d4c30897b2941b2f58a79de04460
diff --git a/strings_test.cpp b/strings_test.cpp
index 5ae3094..7a57489 100644
--- a/strings_test.cpp
+++ b/strings_test.cpp
@@ -83,6 +83,52 @@
ASSERT_EQ("bar", parts[2]);
}
+TEST(strings, tokenize_empty) {
+ std::vector<std::string> parts = android::base::Tokenize("", " ");
+ ASSERT_EQ(0U, parts.size());
+}
+
+TEST(strings, tokenize_all_delimiter) {
+ std::vector<std::string> parts = android::base::Tokenize(" \t ", " \t");
+ ASSERT_EQ(0U, parts.size());
+}
+
+TEST(strings, tokenize_trivial) {
+ std::vector<std::string> parts = android::base::Tokenize("foo", "\t");
+ ASSERT_EQ(1U, parts.size());
+ ASSERT_EQ("foo", parts[0]);
+}
+
+TEST(strings, tokenize_single) {
+ std::vector<std::string> parts = android::base::Tokenize("foo\t", "\t");
+ ASSERT_EQ(1U, parts.size());
+ ASSERT_EQ("foo", parts[0]);
+}
+
+TEST(strings, tokenize_simple) {
+ std::vector<std::string> parts = android::base::Tokenize("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, tokenize_any) {
+ std::vector<std::string> parts = android::base::Tokenize("foo \tbar\t\t baz", " \t");
+ ASSERT_EQ(3U, parts.size());
+ ASSERT_EQ("foo", parts[0]);
+ ASSERT_EQ("bar", parts[1]);
+ ASSERT_EQ("baz", parts[2]);
+}
+
+TEST(strings, tokenize_beginning_trailing_delimiters) {
+ std::vector<std::string> parts = android::base::Tokenize(" foo bar baz \t", " \t");
+ ASSERT_EQ(3U, parts.size());
+ ASSERT_EQ("foo", parts[0]);
+ ASSERT_EQ("bar", parts[1]);
+ ASSERT_EQ("baz", parts[2]);
+}
+
TEST(strings, trim_empty) {
ASSERT_EQ("", android::base::Trim(""));
}