Title: [127928] trunk/Source/WTF
Revision
127928
Author
msab...@apple.com
Date
2012-09-07 15:25:24 -0700 (Fri, 07 Sep 2012)

Log Message

StringImpl::find(StringImpl*) doesn't handle cases where search and match strings are different bitness
https://bugs.webkit.org/show_bug.cgi?id=96125

Reviewed by Benjamin Poulain.

Changed findInner and reverseFindInner to be templated on both search and match character types.
Changed both find's and reverseFind to use all four bitness combinations of findInner and
reverseFindInner.

* wtf/text/StringImpl.cpp:
(WTF::findInner):
(WTF::StringImpl::find):
(WTF::reverseFindInner):
(WTF::StringImpl::reverseFind):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (127927 => 127928)


--- trunk/Source/WTF/ChangeLog	2012-09-07 22:22:04 UTC (rev 127927)
+++ trunk/Source/WTF/ChangeLog	2012-09-07 22:25:24 UTC (rev 127928)
@@ -1,5 +1,22 @@
 2012-09-07  Michael Saboff  <msab...@apple.com>
 
+        StringImpl::find(StringImpl*) doesn't handle cases where search and match strings are different bitness
+        https://bugs.webkit.org/show_bug.cgi?id=96125
+
+        Reviewed by Benjamin Poulain.
+
+        Changed findInner and reverseFindInner to be templated on both search and match character types.
+        Changed both find's and reverseFind to use all four bitness combinations of findInner and
+        reverseFindInner.
+
+        * wtf/text/StringImpl.cpp:
+        (WTF::findInner):
+        (WTF::StringImpl::find):
+        (WTF::reverseFindInner):
+        (WTF::StringImpl::reverseFind):
+
+2012-09-07  Michael Saboff  <msab...@apple.com>
+
         equalIgnoringCase of two StringImpls doesn't handle 8 bit strings
         https://bugs.webkit.org/show_bug.cgi?id=96028
 

Modified: trunk/Source/WTF/wtf/text/StringImpl.cpp (127927 => 127928)


--- trunk/Source/WTF/wtf/text/StringImpl.cpp	2012-09-07 22:22:04 UTC (rev 127927)
+++ trunk/Source/WTF/wtf/text/StringImpl.cpp	2012-09-07 22:25:24 UTC (rev 127928)
@@ -890,7 +890,7 @@
     const UChar* searchCharacters = characters() + index;
 
     // Optimization 2: keep a running hash of the strings,
-    // only call memcmp if the hashes match.
+    // only call equal if the hashes match.
     unsigned searchHash = 0;
     unsigned matchHash = 0;
     for (unsigned i = 0; i < matchLength; ++i) {
@@ -943,11 +943,11 @@
     return index + i;
 }
 
-template <typename CharType>
-ALWAYS_INLINE static size_t findInner(const CharType* searchCharacters, const CharType* matchCharacters, unsigned index, unsigned searchLength, unsigned matchLength)
+template <typename SearchCharacterType, typename MatchCharacterType>
+ALWAYS_INLINE static size_t findInner(const SearchCharacterType* searchCharacters, const MatchCharacterType* matchCharacters, unsigned index, unsigned searchLength, unsigned matchLength)
 {
     // Optimization: keep a running hash of the strings,
-    // only call memcmp if the hashes match.
+    // only call equal() if the hashes match.
 
     // delta is the number of additional times to test; delta == 0 means test only once.
     unsigned delta = searchLength - matchLength;
@@ -962,7 +962,7 @@
 
     unsigned i = 0;
     // keep looping until we match
-    while (searchHash != matchHash || memcmp(searchCharacters + i, matchCharacters, matchLength * sizeof(CharType))) {
+    while (searchHash != matchHash || !equal(searchCharacters + i, matchCharacters, matchLength)) {
         if (i == delta)
             return notFound;
         searchHash += searchCharacters[i + matchLength];
@@ -999,10 +999,16 @@
     if (UNLIKELY(!matchLength))
         return 0;
 
-    if (is8Bit() && matchString->is8Bit())
-        return findInner(characters8(), matchString->characters8(), 0, length(), matchLength);
+    if (is8Bit()) {
+        if (matchString->is8Bit())
+            return findInner(characters8(), matchString->characters8(), 0, length(), matchLength);
+        return findInner(characters8(), matchString->characters16(), 0, length(), matchLength);
+    }
 
-    return findInner(characters(), matchString->characters(), 0, length(), matchLength);
+    if (matchString->is8Bit())
+        return findInner(characters16(), matchString->characters8(), 0, length(), matchLength);
+
+    return findInner(characters16(), matchString->characters16(), 0, length(), matchLength);
 }
 
 size_t StringImpl::find(StringImpl* matchString, unsigned index)
@@ -1035,10 +1041,16 @@
     if (matchLength > searchLength)
         return notFound;
 
-    if (is8Bit() && matchString->is8Bit())
-        return findInner(characters8() + index, matchString->characters8(), index, searchLength, matchLength);
+    if (is8Bit()) {
+        if (matchString->is8Bit())
+            return findInner(characters8() + index, matchString->characters8(), index, searchLength, matchLength);
+        return findInner(characters8() + index, matchString->characters16(), index, searchLength, matchLength);
+    }
 
-    return findInner(characters() + index, matchString->characters(), index, searchLength, matchLength);
+    if (matchString->is8Bit())
+        return findInner(characters16() + index, matchString->characters8(), index, searchLength, matchLength);
+
+    return findInner(characters16() + index, matchString->characters16(), index, searchLength, matchLength);
 }
 
 size_t StringImpl::findIgnoringCase(StringImpl* matchString, unsigned index)
@@ -1079,11 +1091,11 @@
     return WTF::reverseFind(characters16(), m_length, c, index);
 }
 
-template <typename CharType>
-ALWAYS_INLINE static size_t reverseFindInner(const CharType* searchCharacters, const CharType* matchCharacters, unsigned index, unsigned length, unsigned matchLength)
+template <typename SearchCharacterType, typename MatchCharacterType>
+ALWAYS_INLINE static size_t reverseFindInner(const SearchCharacterType* searchCharacters, const MatchCharacterType* matchCharacters, unsigned index, unsigned length, unsigned matchLength)
 {
     // Optimization: keep a running hash of the strings,
-    // only call memcmp if the hashes match.
+    // only call equal if the hashes match.
 
     // delta is the number of additional times to test; delta == 0 means test only once.
     unsigned delta = min(index, length - matchLength);
@@ -1096,7 +1108,7 @@
     }
 
     // keep looping until we match
-    while (searchHash != matchHash || memcmp(searchCharacters + delta, matchCharacters, matchLength * sizeof(CharType))) {
+    while (searchHash != matchHash || !equal(searchCharacters + delta, matchCharacters, matchLength)) {
         if (!delta)
             return notFound;
         delta--;
@@ -1127,10 +1139,16 @@
     if (matchLength > ourLength)
         return notFound;
 
-    if (is8Bit() && matchString->is8Bit())
-        return reverseFindInner(characters8(), matchString->characters8(), index, ourLength, matchLength);
+    if (is8Bit()) {
+        if (matchString->is8Bit())
+            return reverseFindInner(characters8(), matchString->characters8(), index, ourLength, matchLength);
+        return reverseFindInner(characters8(), matchString->characters16(), index, ourLength, matchLength);
+    }
+    
+    if (matchString->is8Bit())
+        return reverseFindInner(characters16(), matchString->characters8(), index, ourLength, matchLength);
 
-    return reverseFindInner(characters(), matchString->characters(), index, ourLength, matchLength);
+    return reverseFindInner(characters16(), matchString->characters16(), index, ourLength, matchLength);
 }
 
 size_t StringImpl::reverseFindIgnoringCase(StringImpl* matchString, unsigned index)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to