fix(string_util): Ensure find != replace

This commit is contained in:
Michael Carlberg 2016-10-18 06:29:25 +02:00
parent d7d953d40a
commit 334dc7731d

View file

@ -46,7 +46,7 @@ namespace string_util {
inline auto replace(const string& haystack, string needle, string replacement) { inline auto replace(const string& haystack, string needle, string replacement) {
string str(haystack); string str(haystack);
string::size_type pos; string::size_type pos;
if ((pos = str.find(needle)) != string::npos) if (needle != replacement && (pos = str.find(needle)) != string::npos)
str = str.replace(pos, needle.length(), replacement); str = str.replace(pos, needle.length(), replacement);
return str; return str;
} }
@ -56,7 +56,7 @@ namespace string_util {
*/ */
inline auto replace_all(const string& haystack, string needle, string replacement) { inline auto replace_all(const string& haystack, string needle, string replacement) {
auto result = haystack; auto result = haystack;
while (result.find(needle) != string::npos) while (needle != replacement && result.find(needle) != string::npos)
result = replace(result, needle, replacement); result = replace(result, needle, replacement);
return result; return result;
} }