]> granicus.if.org Git - taglib/commitdiff
add a replace function to ByteVector
authorScott Wheeler <wheeler@kde.org>
Wed, 6 Feb 2008 04:50:34 +0000 (04:50 +0000)
committerScott Wheeler <wheeler@kde.org>
Wed, 6 Feb 2008 04:50:34 +0000 (04:50 +0000)
git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@771464 283d02a7-25f6-0310-bc7c-ecb5cbfe19da

taglib/toolkit/tbytevector.cpp
taglib/toolkit/tbytevector.h

index 7eaa57fff69c46eb569c6315386312ef811c465a..4a51fed8e8af9b052ed93b4bb37fa828da7f9812 100644 (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())
index 97ef277f5651b300a8bd24795d8055697840204e..66a424eac7bd4d40c53b8507474a1a0b9b2effe3 100644 (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