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.cpp b/strings.cpp
index 40b2bf2..8f3c7d9 100644
--- a/strings.cpp
+++ b/strings.cpp
@@ -46,6 +46,23 @@
return result;
}
+std::vector<std::string> Tokenize(const std::string& s, const std::string& delimiters) {
+ CHECK_NE(delimiters.size(), 0U);
+
+ std::vector<std::string> result;
+ size_t end = 0;
+
+ while (true) {
+ size_t base = s.find_first_not_of(delimiters, end);
+ if (base == s.npos) {
+ break;
+ }
+ end = s.find_first_of(delimiters, base);
+ result.push_back(s.substr(base, end - base));
+ }
+ return result;
+}
+
std::string Trim(const std::string& s) {
std::string result;