From 07d95e0dc008e49356650ce0fec06c96d28b77f0 Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Thu, 18 Feb 2016 03:47:02 +0900 Subject: [PATCH] Add an overload of ByteVector::replace() which takes chars. Currently, this is only way of using ByteVector::replace(). --- taglib/mod/modfilebase.cpp | 2 +- taglib/toolkit/tbytevector.cpp | 18 ++++++++++++++++-- taglib/toolkit/tbytevector.h | 6 ++++++ taglib/xm/xmfile.cpp | 2 +- tests/test_bytevector.cpp | 5 +++++ 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/taglib/mod/modfilebase.cpp b/taglib/mod/modfilebase.cpp index e6a3114f..142f669f 100644 --- a/taglib/mod/modfilebase.cpp +++ b/taglib/mod/modfilebase.cpp @@ -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; diff --git a/taglib/toolkit/tbytevector.cpp b/taglib/toolkit/tbytevector.cpp index 47c9dd31..22ce2d4f 100644 --- a/taglib/toolkit/tbytevector.cpp +++ b/taglib/toolkit/tbytevector.cpp @@ -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(-1)) // Use npos in taglib2. break; diff --git a/taglib/toolkit/tbytevector.h b/taglib/toolkit/tbytevector.h index e897307e..e1549bb9 100644 --- a/taglib/toolkit/tbytevector.h +++ b/taglib/toolkit/tbytevector.h @@ -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. diff --git a/taglib/xm/xmfile.cpp b/taglib/xm/xmfile.cpp index e0c8db57..9192e9bf 100644 --- a/taglib/xm/xmfile.cpp +++ b/taglib/xm/xmfile.cpp @@ -134,7 +134,7 @@ public: if(index > -1) { data.resize(index); } - data.replace((char) 0xff, ' '); + data.replace('\xff', ' '); value = data; return count; } diff --git a/tests/test_bytevector.cpp b/tests/test_bytevector.cpp index f5e93d5f..f7c9b6ee 100644 --- a/tests/test_bytevector.cpp +++ b/tests/test_bytevector.cpp @@ -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"));