Merge remote-tracking branch 'origin/topic/timw/strreplace-endless-loop'

* origin/topic/timw/strreplace-endless-loop:
  Fix potential endless loop in util::strreplace
This commit is contained in:
Tim Wojtulewicz 2024-09-12 08:59:20 +02:00
commit 923ca7e817

View file

@ -1540,18 +1540,22 @@ TEST_CASE("util strreplace") {
string s = "this is not a string";
CHECK(strreplace(s, "not", "really") == "this is really a string");
CHECK(strreplace(s, "not ", "") == "this is a string");
CHECK(strreplace("\"abc\"", "\"", "\\\"") == "\\\"abc\\\"");
CHECK(strreplace("\\\"abc\\\"", "\\\"", "\"") == "\"abc\"");
}
string strreplace(const string& s, const string& o, const string& n) {
string r = s;
size_t i = 0;
while ( true ) {
size_t i = r.find(o);
i = r.find(o, i);
if ( i == std::string::npos )
break;
r.replace(i, o.size(), n);
i += n.size();
}
return r;