From: Mathias Panzenböck <grosser.meister.morti@gmx.net> Date: Mon, 1 Aug 2011 13:14:58 +0000 (+0200) Subject: ByteVector::replace: forgot detach() and opt. when pattern not found X-Git-Tag: v1.8beta~52^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3b14dc3e94d9dbade686563ceda5429e3ff84628;p=taglib ByteVector::replace: forgot detach() and opt. when pattern not found --- diff --git a/taglib/toolkit/tbytevector.cpp b/taglib/toolkit/tbytevector.cpp index d90f464f..623a8c37 100644 --- a/taglib/toolkit/tbytevector.cpp +++ b/taglib/toolkit/tbytevector.cpp @@ -436,25 +436,29 @@ ByteVector &ByteVector::replace(const ByteVector &pattern, const ByteVector &wit int offset = 0; if(withSize == patternSize) { - // I think this case might be common enough to optimize it - offset = find(pattern); - while(offset >= 0) { - ::memcpy(data() + offset, with.data(), withSize); - offset = find(pattern, offset + withSize); - } - return *this; + // I think this case might be common enough to optimize it + detach(); + offset = find(pattern); + while(offset >= 0) { + ::memcpy(data() + offset, with.data(), withSize); + offset = find(pattern, offset + withSize); + } + return *this; } // calculate new size: uint newSize = 0; for(;;) { - int next = find(pattern, offset); - if(next < 0) { - newSize += size() - offset; - break; - } - newSize += (next - offset) + withSize; - offset = next + patternSize; + int next = find(pattern, offset); + if(next < 0) { + if(offset == 0) + // pattern not found, do nothing: + return *this; + newSize += size() - offset; + break; + } + newSize += (next - offset) + withSize; + offset = next + patternSize; } // new private data of appropriate size: @@ -467,8 +471,8 @@ ByteVector &ByteVector::replace(const ByteVector &pattern, const ByteVector &wit for(;;) { int next = find(pattern, offset); if(next < 0) { - ::memcpy(target, source + offset, size() - offset); - break; + ::memcpy(target, source + offset, size() - offset); + break; } int chunkSize = next - offset; ::memcpy(target, source + offset, chunkSize);