From a9401f2b085bbb576021ed844e941beb9bf14c0f Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 10 Apr 2022 22:26:21 +0200 Subject: Fixed Text.replace in *.* case --- FoundationEx/Text.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'FoundationEx') diff --git a/FoundationEx/Text.cpp b/FoundationEx/Text.cpp index a09f333..df828f7 100644 --- a/FoundationEx/Text.cpp +++ b/FoundationEx/Text.cpp @@ -634,22 +634,23 @@ Text::replace(const char* pattern, const char* substitution) Text result; if (rep->length && pattern && *pattern) { - int index = 0; + const char* pos = rep->data; + const char* bound = rep->data + rep->length; int skip = std::strlen(pattern); do { - const char* p = std::strstr(rep->data + index, pattern); + const char* p = std::strstr(pos, pattern); if (p) { - int len = (p - rep->data + index); - result.append(substring(index, len)); + int len = p - pos; + result.append(Text(pos, len)); result.append(substitution); - index += len + skip; + pos = p + skip; } - else if (index < rep->length) { - result.append(substring(index, rep->length-index)); - index = -1; + else if (pos < bound) { + result.append(pos); + break; } } - while (index >= 0 && index < rep->length); + while (pos < bound); } return result; -- cgit v1.1