From 03e457516a08b08f7cbe7aabd35d0792737a5a7a Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Fri, 15 Apr 2022 20:33:53 +0200 Subject: [PATCH] Fixed an issue where the StringUtil::replace function did not remove repeating occurances. --- es-core/src/utils/StringUtil.cpp | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/es-core/src/utils/StringUtil.cpp b/es-core/src/utils/StringUtil.cpp index 0a86e65c4..cb3d1cc3b 100644 --- a/es-core/src/utils/StringUtil.cpp +++ b/es-core/src/utils/StringUtil.cpp @@ -586,17 +586,28 @@ namespace Utils const std::string& from, const std::string& to) { - std::string replaced; - size_t lastPos {0}; - size_t findPos {0}; + std::string result {stringArg}; - while ((findPos = stringArg.find(from, lastPos)) != std::string::npos) { - replaced.append(stringArg, lastPos, findPos - lastPos).append(to); - lastPos = findPos + from.length(); + // The outer loop makes sure that we're eliminating all repeating occurances + // of the 'from' value. + while (result.find(from) != std::string::npos) { + // Prevent endless loops. + if (from == to) + break; + + std::string replaced; + size_t lastPos {0}; + size_t findPos {0}; + + while ((findPos = result.find(from, lastPos)) != std::string::npos) { + replaced.append(result, lastPos, findPos - lastPos).append(to); + lastPos = findPos + from.length(); + } + + replaced.append(result.substr(lastPos)); + result = replaced; } - - replaced.append(stringArg.substr(lastPos)); - return replaced; + return result; } std::wstring stringToWideString(const std::string& stringArg)