From 3b14dc3e94d9dbade686563ceda5429e3ff84628 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mathias=20Panzenb=C3=B6ck?= Date: Mon, 1 Aug 2011 15:14:58 +0200 Subject: [PATCH] ByteVector::replace: forgot detach() and opt. when pattern not found --- taglib/toolkit/tbytevector.cpp | 36 +++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 16 deletions(-) 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); -- 2.40.0