1
0
Fork 0

fix endless loop in StringReplace (#3658)

* Fixed a recursive loop where the replacement would again be searched for the needle.
* Skip if the needle is empty. Find(needle) always matches if needle is empty.
This commit is contained in:
Marvin Kopf 2017-04-01 13:57:51 +02:00 committed by worktycho
parent 62ffa37bf1
commit 974c054bc9
1 changed files with 7 additions and 1 deletions

View File

@ -374,11 +374,17 @@ size_t RateCompareString(const AString & s1, const AString & s2)
void ReplaceString(AString & iHayStack, const AString & iNeedle, const AString & iReplaceWith)
{
// find always returns the current position for an empty needle; prevent endless loop
if (iNeedle.empty())
{
return;
}
size_t pos1 = iHayStack.find(iNeedle);
while (pos1 != AString::npos)
{
iHayStack.replace( pos1, iNeedle.size(), iReplaceWith);
pos1 = iHayStack.find(iNeedle, pos1);
pos1 = iHayStack.find(iNeedle, pos1 + iReplaceWith.size());
}
}