add a replace function to ByteVector

git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@771464 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
This commit is contained in:
Scott Wheeler 2008-02-06 04:50:34 +00:00
parent 88a9ae32f8
commit 5542dbb94b
2 changed files with 37 additions and 0 deletions

View File

@ -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())

View File

@ -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