Reviewed by Maciej
authorbeidson <beidson@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 28 Aug 2006 05:59:56 +0000 (05:59 +0000)
committerbeidson <beidson@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 28 Aug 2006 05:59:56 +0000 (05:59 +0000)
        Rewrote StringImpl::replace(UChar, StringImpl*)

        * platform/StringImpl.cpp:
        (WebCore::StringImpl::replace):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@16069 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog
WebCore/platform/StringImpl.cpp

index d55d2770c44cf1e046549b1d6683efc9f8773d16..85de92d689299bb88ab87ce8f2460fe1d89cd47c 100644 (file)
@@ -1,3 +1,12 @@
+2006-08-27  Brady Eidson  <beidson@apple.com>
+
+        Reviewed by Maciej
+
+        Rewrote StringImpl::replace(UChar, StringImpl*)
+
+        * platform/StringImpl.cpp:
+        (WebCore::StringImpl::replace):
+
 2006-08-27  Sam Weinig  <sam.weinig@gmail.com>
 
         Reviewed by Tim H.
index cfb075c0b200872fde4adb50a929434b3591e022..f408c33b230784d95762e58acf1fc23c59d338b5 100644 (file)
@@ -758,20 +758,45 @@ StringImpl* StringImpl::replace(unsigned index, unsigned len, const StringImpl*
     return m_data;
 }
 
-StringImpl* StringImpl::replace(UChar pattern, const StringImpl* str)
-{
-    int slen = str ? str->length() : 0;
-    int index = 0;
-    StringImpl* oldResult = this;
-    StringImpl* newResult;
-    while ((index = oldResult->find(pattern, index)) >= 0) {
-        newResult = oldResult->replace(index, 1, str);
-        if (oldResult != this)
-            delete oldResult;
-        oldResult = newResult;
-        index += slen;
-    }
-    return oldResult;
+StringImpl* StringImpl::replace(UChar pattern, const StringImpl* replacement)
+{
+    if (!replacement)
+        return this;
+        
+    int repStrLength = replacement->length();
+    int srcSegmentStart = 0;
+    int matchCount = 0;
+    
+    // Count the matches
+    while ((srcSegmentStart = find(pattern, srcSegmentStart)) >= 0) {
+        ++matchCount;
+        ++srcSegmentStart;
+    }
+    
+    // Create the new StringImpl;
+    StringImpl* dst = new StringImpl();
+    dst->m_length = m_length - matchCount + (matchCount * repStrLength);
+    dst->m_data = newUCharVector(dst->m_length);
+    
+    // Construct the new data
+    int srcSegmentEnd;
+    int srcSegmentLength;
+    srcSegmentStart = 0;
+    int dstOffset = 0;
+    
+    while ((srcSegmentEnd = find(pattern, srcSegmentStart)) >= 0) {
+        srcSegmentLength = srcSegmentEnd - srcSegmentStart;
+        memcpy(dst->m_data + dstOffset, m_data + srcSegmentStart, srcSegmentLength * sizeof(UChar));
+        dstOffset += srcSegmentLength;
+        memcpy(dst->m_data + dstOffset, replacement->m_data, repStrLength * sizeof(UChar));
+        dstOffset += repStrLength;
+        srcSegmentStart = srcSegmentEnd + 1;
+    }
+
+    srcSegmentLength = m_length - srcSegmentStart;
+    memcpy(dst->m_data + dstOffset, m_data + srcSegmentStart, srcSegmentLength * sizeof(UChar));
+    
+    return dst;
 }
 
 bool equal(const StringImpl* a, const StringImpl* b)