]> granicus.if.org Git - taglib/commitdiff
Add an overload of ByteVector::replace() which takes chars.
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Wed, 17 Feb 2016 18:47:02 +0000 (03:47 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Wed, 17 Feb 2016 18:47:02 +0000 (03:47 +0900)
Currently, this is only way of using ByteVector::replace().

taglib/mod/modfilebase.cpp
taglib/toolkit/tbytevector.cpp
taglib/toolkit/tbytevector.h
taglib/xm/xmfile.cpp
tests/test_bytevector.cpp

index e6a3114fbb579c9534d187c6c108ca1a10a22c42..142f669ff0ef48686ada1a68ec7779d2aaa77e22 100644 (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;
index 47c9dd3186e38b4e5d4b91428958d5631d24cf24..22ce2d4fdbaf383ad71caaac7bf6cb8ce552a620 100644 (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;
index e897307e303689e4528a11840d9cf90607d00ae8..e1549bb9c29b93c51f20dff82783ffc7be983148 100644 (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.
index e0c8db575a7a810178d182d99202c8dd8c203dbb..9192e9bf2a039891fbc55f38897708ded8a41f2e 100644 (file)
@@ -134,7 +134,7 @@ public:
     if(index > -1) {
       data.resize(index);
     }
-    data.replace((char) 0xff, ' ');
+    data.replace('\xff', ' ');
     value = data;
     return count;
   }
index f5e93d5f67ad048ad6e341017af88569aea4c736..f7c9b6ee804f4d6fada0d1b2bd56e36c92162e06 100644 (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"));