mirror of
https://github.com/taglib/taglib.git
synced 2025-06-03 00:58:12 -04:00
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:
parent
88a9ae32f8
commit
5542dbb94b
@ -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())
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user