diff options
author | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:22:18 +0200 |
---|---|---|
committer | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:22:18 +0200 |
commit | bcc5cc28c6dc5178e8f4fd0cc521034ae5def388 (patch) | |
tree | bacc60d27b435d32172f97643576c5e4e953177d /zen/string_tools.h | |
parent | 5.9 (diff) | |
download | FreeFileSync-bcc5cc28c6dc5178e8f4fd0cc521034ae5def388.tar.gz FreeFileSync-bcc5cc28c6dc5178e8f4fd0cc521034ae5def388.tar.bz2 FreeFileSync-bcc5cc28c6dc5178e8f4fd0cc521034ae5def388.zip |
5.10
Diffstat (limited to 'zen/string_tools.h')
-rw-r--r-- | zen/string_tools.h | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/zen/string_tools.h b/zen/string_tools.h index 32d12119..c0bb1039 100644 --- a/zen/string_tools.h +++ b/zen/string_tools.h @@ -286,34 +286,45 @@ template <class S, class T, class U> inline S replaceCpy(const S& str, const T& oldTerm, const U& newTerm, bool replaceAll) { assert_static(IsStringLike<T>::value && IsStringLike<U>::value); - typedef typename GetCharType<S>::Type CharType; const size_t oldLen = strLength(oldTerm); - const size_t newLen = strLength(newTerm); - - S output; + if (oldLen == 0) + { + assert(false); + return str; + } const CharType* strPos = strBegin(str); const CharType* const strEnd = strPos + strLength(str); const CharType* const oldBegin = strBegin(oldTerm); + const CharType* const oldEnd = oldBegin + oldLen; + + //optimize "oldTerm not found" + const CharType* strMatch = std::search(strPos, strEnd, + oldBegin, oldEnd); + if (strMatch == strEnd) + return str; + + const size_t newLen = strLength(newTerm); const CharType* const newBegin = strBegin(newTerm); + S output; for (;;) { - const CharType* ptr = std::search(strPos, strEnd, - oldBegin, oldBegin + oldLen); - if (ptr == strEnd) - break; - - implementation::stringAppend(output, strPos, ptr - strPos); + implementation::stringAppend(output, strPos, strMatch - strPos); implementation::stringAppend(output, newBegin, newLen); - strPos = ptr + oldLen; + strPos = strMatch + oldLen; if (!replaceAll) break; + + strMatch = std::search(strPos, strEnd, + oldBegin, oldEnd); + if (strMatch == strEnd) + break; } implementation::stringAppend(output, strPos, strEnd - strPos); |