Updated the StringUtil::replace function to avoid potential endless loops.

This commit is contained in:
Leon Styhre 2022-02-21 17:14:51 +01:00
parent 171c25917c
commit fabc18a680

View file

@ -583,17 +583,20 @@ namespace Utils
} }
std::string replace(const std::string& stringArg, std::string replace(const std::string& stringArg,
const std::string& replace, const std::string& from,
const std::string& with) const std::string& to)
{ {
std::string stringReplace = stringArg; std::string replaced;
size_t pos; size_t lastPos {0};
size_t findPos {0};
while ((pos = stringReplace.find(replace)) != std::string::npos) while ((findPos = stringArg.find(from, lastPos)) != std::string::npos) {
stringReplace = replaced.append(stringArg, lastPos, findPos - lastPos).append(to);
stringReplace.replace(pos, replace.length(), with.c_str(), with.length()); lastPos = findPos + from.length();
}
return stringReplace; replaced.append(stringArg.substr(lastPos));
return replaced;
} }
std::wstring stringToWideString(const std::string& stringArg) std::wstring stringToWideString(const std::string& stringArg)