From 8bdc4458c73898e17c738c82eb83402b26968ef0 Mon Sep 17 00:00:00 2001 From: David Chavez Date: Tue, 26 Jul 2022 03:12:25 +0200 Subject: [PATCH] Improve string split performance (#933) --- ZAPDTR/ZAPDUtils/Utils/StringHelper.cpp | 26 +++++++++++-------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/ZAPDTR/ZAPDUtils/Utils/StringHelper.cpp b/ZAPDTR/ZAPDUtils/Utils/StringHelper.cpp index 070fffa63..051e9a87d 100644 --- a/ZAPDTR/ZAPDUtils/Utils/StringHelper.cpp +++ b/ZAPDTR/ZAPDUtils/Utils/StringHelper.cpp @@ -9,22 +9,18 @@ std::vector StringHelper::Split(std::string s, const std::string& delimiter) { - std::vector result; + size_t pos_start = 0, pos_end, delim_len = delimiter.length(); + std::string token; + std::vector 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); }); -} \ No newline at end of file +}