Add an overload of ByteVector::replace() which takes chars.

Currently, this is only way of using ByteVector::replace().
This commit is contained in:
Tsuda Kageyu 2016-02-18 03:47:02 +09:00
parent a9acca5d81
commit 07d95e0dc0
5 changed files with 29 additions and 4 deletions

View File

@ -54,7 +54,7 @@ bool Mod::FileBase::readString(String &s, unsigned long size)
{
data.resize(index);
}
data.replace((char) 0xff, ' ');
data.replace('\xff', ' ');
s = data;
return true;

View File

@ -475,18 +475,32 @@ bool ByteVector::endsWith(const ByteVector &pattern) const
return containsAt(pattern, size() - pattern.size());
}
ByteVector &ByteVector::replace(char oldByte, char newByte)
{
detach();
for(ByteVector::Iterator it = begin(); it != end(); ++it) {
if(*it == oldByte)
*it = newByte;
}
return *this;
}
ByteVector &ByteVector::replace(const ByteVector &pattern, const ByteVector &with)
{
if(pattern.size() == 0 || pattern.size() > size())
return *this;
if(pattern.size() == 1 && with.size() == 1)
return replace(pattern[0], with[0]);
const size_t withSize = with.size();
const size_t patternSize = pattern.size();
const ptrdiff_t diff = withSize - patternSize;
size_t offset = 0;
while (true)
{
while (true) {
offset = find(pattern, offset);
if(offset == static_cast<size_t>(-1)) // Use npos in taglib2.
break;

View File

@ -179,6 +179,12 @@ namespace TagLib {
*/
bool endsWith(const ByteVector &pattern) const;
/*!
* Replaces \a oldByte with \a newByte and returns a reference to the
* ByteVector after the operation. This \e does modify the vector.
*/
ByteVector &replace(char oldByte, char newByte);
/*!
* Replaces \a pattern with \a with and returns a reference to the ByteVector
* after the operation. This \e does modify the vector.

View File

@ -134,7 +134,7 @@ public:
if(index > -1) {
data.resize(index);
}
data.replace((char) 0xff, ' ');
data.replace('\xff', ' ');
value = data;
return count;
}

View File

@ -266,6 +266,11 @@ public:
a.replace(ByteVector("a"), ByteVector("x"));
CPPUNIT_ASSERT_EQUAL(ByteVector("xbcdxbf"), a);
}
{
ByteVector a("abcdabf");
a.replace('a', 'x');
CPPUNIT_ASSERT_EQUAL(ByteVector("xbcdxbf"), a);
}
{
ByteVector a("abcdabf");
a.replace(ByteVector("ab"), ByteVector("xy"));