From b765094a2949c0d735e4b87aefb6c88917b0e337 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Fri, 23 Dec 2016 04:40:35 +0100 Subject: [PATCH] refactor(string_util): Faster replace_all --- src/utils/string.cpp | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/src/utils/string.cpp b/src/utils/string.cpp index e96c8a3a..d44f3dac 100644 --- a/src/utils/string.cpp +++ b/src/utils/string.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -45,13 +46,13 @@ namespace string_util { /** * Replace first occurence of needle in haystack */ - string replace(const string& haystack, const string& needle, const string& reply, size_t start, size_t end) { + string replace(const string& haystack, const string& needle, const string& replacement, size_t start, size_t end) { string str(haystack); string::size_type pos; - if (needle != reply && (pos = str.find(needle, start)) != string::npos) { + if (needle != replacement && (pos = str.find(needle, start)) != string::npos) { if (end == string::npos || pos < end) { - str = str.replace(pos, needle.length(), reply); + str = str.replace(pos, needle.length(), replacement); } } @@ -61,27 +62,16 @@ namespace string_util { /** * Replace all occurences of needle in haystack */ - string replace_all(const string& haystack, const string& needle, const string& reply, size_t start, size_t end) { - string replaced; - - if (end == string::npos) { - end = haystack.length(); + string replace_all( + const string& haystack, const string& needle, const string& replacement, size_t start, size_t end) { + string result{haystack}; + string::size_type pos; + while ((pos = result.find(needle, start)) != string::npos && pos < result.length() && + (end == string::npos || pos + needle.length() <= end)) { + result.replace(pos, needle.length(), replacement); + start = pos + replacement.length(); } - - for (size_t i = 0; i < haystack.length(); i++) { - if (i < start) { - replaced += haystack[i]; - } else if (i + needle.length() > end) { - replaced += haystack[i]; - } else if (haystack.compare(i, needle.length(), needle) == 0) { - replaced += reply; - i += needle.length() - 1; - } else { - replaced += haystack[i]; - } - } - - return replaced; + return result; } /**