Better string split implementation

This commit is contained in:
topjohnwu 2023-04-03 18:50:36 -07:00
parent 41b77e4f25
commit 762b70ba9d
3 changed files with 11 additions and 11 deletions

View File

@ -217,14 +217,14 @@ string &replace_all(string &str, string_view from, string_view to) {
return str; return str;
} }
template <class T> template <typename T>
static auto split_impl(T s, T delims) { static auto split_impl(string_view s, string_view delims) {
vector<std::decay_t<T>> result; vector<T> result;
size_t base = 0; size_t base = 0;
size_t found; size_t found;
while (true) { while (true) {
found = s.find_first_of(delims, base); found = s.find_first_of(delims, base);
result.push_back(s.substr(base, found - base)); result.emplace_back(s.substr(base, found - base));
if (found == string::npos) if (found == string::npos)
break; break;
base = found + 1; base = found + 1;
@ -232,11 +232,11 @@ static auto split_impl(T s, T delims) {
return result; return result;
} }
vector<string> split(const string &s, const string &delims) { vector<string> split(string_view s, string_view delims) {
return split_impl<const string&>(s, delims); return split_impl<string>(s, delims);
} }
vector<string_view> split_ro(string_view s, string_view delims) { vector<string_view> split_view(string_view s, string_view delims) {
return split_impl<string_view>(s, delims); return split_impl<string_view>(s, delims);
} }

View File

@ -163,8 +163,8 @@ int switch_mnt_ns(int pid);
std::mt19937_64 &get_rand(const void *seed_buf = nullptr); std::mt19937_64 &get_rand(const void *seed_buf = nullptr);
int gen_rand_str(char *buf, int len, bool varlen = true); int gen_rand_str(char *buf, int len, bool varlen = true);
std::string &replace_all(std::string &str, std::string_view from, std::string_view to); std::string &replace_all(std::string &str, std::string_view from, std::string_view to);
std::vector<std::string> split(const std::string &s, const std::string &delims); std::vector<std::string> split(std::string_view s, std::string_view delims);
std::vector<std::string_view> split_ro(std::string_view, std::string_view delims); std::vector<std::string_view> split_view(std::string_view, std::string_view delims);
// Similar to vsnprintf, but the return value is the written number of bytes // Similar to vsnprintf, but the return value is the written number of bytes
int vssprintf(char *dest, size_t size, const char *fmt, va_list ap); int vssprintf(char *dest, size_t size, const char *fmt, va_list ap);

View File

@ -71,7 +71,7 @@ static void mount_mirrors() {
bool mounted = false; bool mounted = false;
for (const auto &info: self_mount_info) { for (const auto &info: self_mount_info) {
if (info.root == "/" && info.device == preinit_dev) { if (info.root == "/" && info.device == preinit_dev) {
auto flags = split_ro(info.fs_option, ","); auto flags = split_view(info.fs_option, ",");
auto rw = std::any_of(flags.begin(), flags.end(), [](const auto &flag) { auto rw = std::any_of(flags.begin(), flags.end(), [](const auto &flag) {
return flag == "rw"sv; return flag == "rw"sv;
}); });
@ -137,7 +137,7 @@ string find_preinit_device() {
continue; continue;
if (info.type != "ext4" && info.type != "f2fs") if (info.type != "ext4" && info.type != "f2fs")
continue; continue;
auto flags = split_ro(info.fs_option, ","); auto flags = split_view(info.fs_option, ",");
auto rw = std::any_of(flags.begin(), flags.end(), [](const auto &flag) { auto rw = std::any_of(flags.begin(), flags.end(), [](const auto &flag) {
return flag == "rw"sv; return flag == "rw"sv;
}); });