diff --git a/taglib/toolkit/tbytevector.cpp b/taglib/toolkit/tbytevector.cpp index 7eaa57ff..4a51fed8 100644 --- a/taglib/toolkit/tbytevector.cpp +++ b/taglib/toolkit/tbytevector.cpp @@ -420,6 +420,37 @@ bool ByteVector::endsWith(const ByteVector &pattern) const return containsAt(pattern, size() - pattern.size()); } +ByteVector &ByteVector::replace(const ByteVector &pattern, const ByteVector &with) +{ + if(pattern.size() == 0 || pattern.size() > size()) + return *this; + + const int patternSize = pattern.size(); + const int withSize = with.size(); + + int offset = find(pattern); + + while(offset >= 0) + { + const int originalSize = size(); + + if(withSize > patternSize) + resize(originalSize + withSize - patternSize); + + if(patternSize != withSize) + ::memcpy(data() + offset + withSize, mid(offset + patternSize).data(), originalSize - offset - patternSize); + + if(withSize < patternSize) + resize(originalSize + withSize - patternSize); + + ::memcpy(data() + offset, with.data(), withSize); + + offset = find(pattern, offset + withSize); + } + + return *this; +} + int ByteVector::endsWithPartialMatch(const ByteVector &pattern) const { if(pattern.size() > size()) diff --git a/taglib/toolkit/tbytevector.h b/taglib/toolkit/tbytevector.h index 97ef277f..66a424ea 100644 --- a/taglib/toolkit/tbytevector.h +++ b/taglib/toolkit/tbytevector.h @@ -162,6 +162,12 @@ namespace TagLib { */ bool endsWith(const ByteVector &pattern) const; + /*! + * Replaces \a pattern with \a with and returns a reference to the ByteVector + * after the operation. This \e does modify the vector. + */ + ByteVector &replace(const ByteVector &pattern, const ByteVector &with); + /*! * Checks for a partial match of \a pattern at the end of the vector. It * returns the offset of the partial match within the vector, or -1 if the