diff options
author | 2021-02-08 17:46:15 -0800 | |
---|---|---|
committer | 2021-02-10 01:39:40 +0000 | |
commit | 60117aeeffda3d01a5314984694ae3d6d4588fc1 (patch) | |
tree | 12f5062c456de8b50418a8fb74c801c8ccc5b690 /libartbase/base/utils.cc | |
parent | c7ac91b21d1a15c14e29d69ff02b48c485962b0d (diff) |
Improve string splitting
String splitting is something that we often have to do but our support
code for doing so is not the best. Add support for using
std::string_view in many circumstances and add support for making an
iterator of splits without allocation.
Test: ./test.py --host
Change-Id: I1b56b7e10926a064b64011326b508dd4af707df9
Diffstat (limited to 'libartbase/base/utils.cc')
-rw-r--r-- | libartbase/base/utils.cc | 49 |
1 files changed, 37 insertions, 12 deletions
diff --git a/libartbase/base/utils.cc b/libartbase/base/utils.cc index 492e737f4e..ba62f30bdc 100644 --- a/libartbase/base/utils.cc +++ b/libartbase/base/utils.cc @@ -25,11 +25,13 @@ #include <fstream> #include <memory> +#include <string> #include "android-base/file.h" #include "android-base/stringprintf.h" #include "android-base/strings.h" +#include "base/stl_util.h" #include "bit_utils.h" #include "os.h" @@ -230,22 +232,45 @@ std::string PrettySize(uint64_t byte_count) { byte_count / kBytesPerUnit[i], kUnitStrings[i]); } -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)); +template <typename StrIn, typename Str> +void Split(const StrIn& s, char separator, std::vector<Str>* out_result) { + auto split = SplitString(std::string_view(s), separator); + for (std::string_view p : split) { + if (p.empty()) { + continue; } + out_result->push_back(Str(p)); } } +template void Split(const char *const& s, char separator, std::vector<std::string>* out_result); +template void Split(const std::string& s, char separator, std::vector<std::string>* out_result); +template void Split(const char *const& s, char separator, std::vector<std::string_view>* out_result); +template void Split(const std::string_view& s, + char separator, + std::vector<std::string_view>* out_result); + +template <typename Str> +void Split(const Str& s, char separator, size_t len, Str* out_result) { + Str* last = out_result + len; + auto split = SplitString(std::string_view(s), separator); + for (std::string_view p : split) { + if (p.empty()) { + continue; + } + if (out_result == last) { + return; + } + *out_result++ = Str(p); + } +} + +template void Split(const std::string& s, char separator, size_t len, std::string* out_result); +template void Split(const std::string_view& s, + char separator, + size_t len, + std::string_view* out_result); + void SetThreadName(const char* thread_name) { bool hasAt = false; bool hasDot = false; |