Skip to content

Commit

Permalink
Improve string split performance (HarbourMasters#933)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz authored Jul 26, 2022
1 parent 5f71893 commit 8bdc445
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions ZAPDTR/ZAPDUtils/Utils/StringHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 0;
std::string 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;
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
std::string token;
std::vector<std::string> res;

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);
}

res.push_back(s.substr(pos_start));
return res;
}

std::string StringHelper::Strip(std::string s, const std::string& delimiter)
Expand Down Expand Up @@ -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); });
}
}

0 comments on commit 8bdc445

Please sign in to comment.