Title: [131655] trunk/Source/WTF
Revision
131655
Author
msab...@apple.com
Date
2012-10-17 15:16:12 -0700 (Wed, 17 Oct 2012)

Log Message

StringImpl::findIgnoringCase() and reverseFindIgnoringCase() don't optimally handle a mix of 8 and 16 bit strings
https://bugs.webkit.org/show_bug.cgi?id=99224

Reviewed by Geoffrey Garen.

Added helper templated functions and all four combinations similar to find() and reverseFind().

(WTF::findIgnoringCaseInner):
(WTF::StringImpl::findIgnoringCase):
(WTF::reverseFindIgnoringCaseInner):
(WTF::StringImpl::reverseFindIgnoringCase):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (131654 => 131655)


--- trunk/Source/WTF/ChangeLog	2012-10-17 22:14:41 UTC (rev 131654)
+++ trunk/Source/WTF/ChangeLog	2012-10-17 22:16:12 UTC (rev 131655)
@@ -1,5 +1,19 @@
 2012-10-17  Michael Saboff  <msab...@apple.com>
 
+        StringImpl::findIgnoringCase() and reverseFindIgnoringCase() don't optimally handle a mix of 8 and 16 bit strings
+        https://bugs.webkit.org/show_bug.cgi?id=99224
+
+        Reviewed by Geoffrey Garen.
+
+        Added helper templated functions and all four combinations similar to find() and reverseFind().
+
+        (WTF::findIgnoringCaseInner):
+        (WTF::StringImpl::findIgnoringCase):
+        (WTF::reverseFindIgnoringCaseInner):
+        (WTF::StringImpl::reverseFindIgnoringCase):
+
+2012-10-17  Michael Saboff  <msab...@apple.com>
+
         AtomicString::HashAndUTF8CharactersTranslator::equal() doesn't optimally handle 8 bit strings
         https://bugs.webkit.org/show_bug.cgi?id=99223
 

Modified: trunk/Source/WTF/wtf/text/StringImpl.cpp (131654 => 131655)


--- trunk/Source/WTF/wtf/text/StringImpl.cpp	2012-10-17 22:14:41 UTC (rev 131654)
+++ trunk/Source/WTF/wtf/text/StringImpl.cpp	2012-10-17 22:16:12 UTC (rev 131655)
@@ -1048,6 +1048,22 @@
     return findInner(characters16() + index, matchString->characters16(), index, searchLength, matchLength);
 }
 
+template <typename SearchCharacterType, typename MatchCharacterType>
+ALWAYS_INLINE static size_t findIgnoringCaseInner(const SearchCharacterType* searchCharacters, const MatchCharacterType* matchCharacters, unsigned index, unsigned searchLength, unsigned matchLength)
+{
+    // delta is the number of additional times to test; delta == 0 means test only once.
+    unsigned delta = searchLength - matchLength;
+
+    unsigned i = 0;
+    // keep looping until we match
+    while (!equalIgnoringCase(searchCharacters + i, matchCharacters, matchLength)) {
+        if (i == delta)
+            return notFound;
+        ++i;
+    }
+    return index + i;
+}
+
 size_t StringImpl::findIgnoringCase(StringImpl* matchString, unsigned index)
 {
     // Check for null or empty string to match against
@@ -1063,20 +1079,17 @@
     unsigned searchLength = length() - index;
     if (matchLength > searchLength)
         return notFound;
-    // delta is the number of additional times to test; delta == 0 means test only once.
-    unsigned delta = searchLength - matchLength;
 
-    const UChar* searchCharacters = characters() + index;
-    const UChar* matchCharacters = matchString->characters();
+    if (is8Bit()) {
+        if (matchString->is8Bit())
+            return findIgnoringCaseInner(characters8() + index, matchString->characters8(), index, length(), matchLength);
+        return findIgnoringCaseInner(characters8() + index, matchString->characters16(), index, length(), matchLength);
+    }
 
-    unsigned i = 0;
-    // keep looping until we match
-    while (!equalIgnoringCase(searchCharacters + i, matchCharacters, matchLength)) {
-        if (i == delta)
-            return notFound;
-        ++i;
-    }
-    return index + i;
+    if (matchString->is8Bit())
+        return findIgnoringCaseInner(characters16() + index, matchString->characters8(), index, length(), matchLength);
+
+    return findIgnoringCaseInner(characters16() + index, matchString->characters16(), index, length(), matchLength);
 }
 
 size_t StringImpl::reverseFind(UChar c, unsigned index)
@@ -1146,44 +1159,45 @@
     return reverseFindInner(characters16(), matchString->characters16(), index, ourLength, matchLength);
 }
 
+template <typename SearchCharacterType, typename MatchCharacterType>
+ALWAYS_INLINE static size_t reverseFindIgnoringCaseInner(const SearchCharacterType* searchCharacters, const MatchCharacterType* matchCharacters, unsigned index, unsigned length, unsigned matchLength)
+{
+    // delta is the number of additional times to test; delta == 0 means test only once.
+    unsigned delta = min(index, length - matchLength);
+
+    // keep looping until we match
+    while (!equalIgnoringCase(searchCharacters + delta, matchCharacters, matchLength)) {
+        if (!delta)
+            return notFound;
+        delta--;
+    }
+    return delta;
+}
+
 size_t StringImpl::reverseFindIgnoringCase(StringImpl* matchString, unsigned index)
 {
     // Check for null or empty string to match against
     if (!matchString)
         return notFound;
     unsigned matchLength = matchString->length();
+    unsigned ourLength = length();
     if (!matchLength)
-        return min(index, length());
+        return min(index, ourLength);
 
     // Check index & matchLength are in range.
-    if (matchLength > length())
+    if (matchLength > ourLength)
         return notFound;
-    // delta is the number of additional times to test; delta == 0 means test only once.
-    unsigned delta = min(index, length() - matchLength);
 
-    if (is8Bit() && matchString->is8Bit()) {
-        const LChar *searchCharacters = characters8();
-        const LChar *matchCharacters = matchString->characters8();
-
-        // keep looping until we match
-        while (!equalIgnoringCase(searchCharacters + delta, matchCharacters, matchLength)) {
-            if (!delta)
-                return notFound;
-            delta--;
-        }
-        return delta;
+    if (is8Bit()) {
+        if (matchString->is8Bit())
+            return reverseFindIgnoringCaseInner(characters8(), matchString->characters8(), index, ourLength, matchLength);
+        return reverseFindIgnoringCaseInner(characters8(), matchString->characters16(), index, ourLength, matchLength);
     }
 
-    const UChar *searchCharacters = characters();
-    const UChar *matchCharacters = matchString->characters();
+    if (matchString->is8Bit())
+        return reverseFindIgnoringCaseInner(characters16(), matchString->characters8(), index, ourLength, matchLength);
 
-    // keep looping until we match
-    while (!equalIgnoringCase(searchCharacters + delta, matchCharacters, matchLength)) {
-        if (!delta)
-            return notFound;
-        delta--;
-    }
-    return delta;
+    return reverseFindIgnoringCaseInner(characters16(), matchString->characters16(), index, ourLength, matchLength);
 }
 
 ALWAYS_INLINE static bool equalInner(const StringImpl* stringImpl, unsigned startOffset, const char* matchString, unsigned matchLength, bool caseSensitive)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to