Improve string split performance (#933)

This commit is contained in:
David Chavez 2022-07-26 03:12:25 +02:00 committed by GitHub
parent 5f718932e6
commit 8bdc4458c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 15 deletions

View File

@ -9,22 +9,18 @@
std::vector<std::string> StringHelper::Split(std::string s, const std::string& delimiter)
{
std::vector<std::string> result;
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
std::string token;
std::vector<std::string> res;
size_t pos = 0;
std::string token;
while ((pos_end = s.find(delimiter, pos_start)) != std::string::npos) {
token = s.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back(token);
}
while ((pos = s.find(delimiter)) != std::string::npos)
{
token = s.substr(0, pos);
result.push_back(token);
s.erase(0, pos + delimiter.length());
}
if (s.length() != 0)
result.push_back(s);
return result;
res.push_back(s.substr(pos_start));
return res;
}
std::string StringHelper::Strip(std::string s, const std::string& delimiter)
@ -127,4 +123,4 @@ bool StringHelper::IEquals(const std::string& a, const std::string& b)
{
return std::equal(a.begin(), a.end(), b.begin(), b.end(),
[](char a, char b) { return tolower(a) == tolower(b); });
}
}