WTF should have a similar function as equalLettersIgnoringASCIICase to match beginnin...
authoryouenn.fablet@crf.canon.fr <youenn.fablet@crf.canon.fr@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 11 Mar 2016 15:10:18 +0000 (15:10 +0000)
committeryouenn.fablet@crf.canon.fr <youenn.fablet@crf.canon.fr@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 11 Mar 2016 15:10:18 +0000 (15:10 +0000)
https://bugs.webkit.org/show_bug.cgi?id=153419

Reviewed by Darin Adler.

Source/WebCore:

Covered by added unint tests.

* Modules/fetch/FetchHeaders.cpp:
(WebCore::isForbiddenHeaderName): Using startsWithLettersIgnoringASCIICase.

Source/WTF:

Introducing startsWithLettersIgnoringASCIICase, to check the beginning of a string.
Moving some code from WTF::equalLettersIgnoringASCIICaseCommonWithoutLength in
WTF::hasPrefixWithLettersIgnoringASCIICaseCommon to enable reuse in
WTF::startsWithLettersIgnoringASCIICaseCommon.

* wtf/text/StringCommon.h:
(WTF::hasPrefixWithLettersIgnoringASCIICaseCommon):
(WTF::equalLettersIgnoringASCIICaseCommonWithoutLength):
(WTF::startsWithLettersIgnoringASCIICaseCommonWithoutLength):
(WTF::startsWithLettersIgnoringASCIICaseCommon):
* wtf/text/StringImpl.h:
(WTF::startsWithLettersIgnoringASCIICase):
* wtf/text/WTFString.h:
(WTF::startsWithLettersIgnoringASCIICase):

Tools:

* TestWebKitAPI/Tests/WTF/StringOperators.cpp:
(TestWebKitAPI::TEST): Adding test case for startsWithLettersIgnoringASCIICase.

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

Source/WTF/ChangeLog
Source/WTF/wtf/text/StringCommon.h
Source/WTF/wtf/text/StringImpl.h
Source/WTF/wtf/text/WTFString.h
Source/WebCore/ChangeLog
Source/WebCore/Modules/fetch/FetchHeaders.cpp
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WTF/StringImpl.cpp

index 6d81e49..71a2ab8 100644 (file)
@@ -1,3 +1,25 @@
+2016-03-11  Youenn Fablet  <youenn.fablet@crf.canon.fr>
+
+        WTF should have a similar function as equalLettersIgnoringASCIICase to match beginning of strings
+        https://bugs.webkit.org/show_bug.cgi?id=153419
+
+        Reviewed by Darin Adler.
+
+        Introducing startsWithLettersIgnoringASCIICase, to check the beginning of a string.
+        Moving some code from WTF::equalLettersIgnoringASCIICaseCommonWithoutLength in
+        WTF::hasPrefixWithLettersIgnoringASCIICaseCommon to enable reuse in
+        WTF::startsWithLettersIgnoringASCIICaseCommon.
+
+        * wtf/text/StringCommon.h:
+        (WTF::hasPrefixWithLettersIgnoringASCIICaseCommon):
+        (WTF::equalLettersIgnoringASCIICaseCommonWithoutLength):
+        (WTF::startsWithLettersIgnoringASCIICaseCommonWithoutLength):
+        (WTF::startsWithLettersIgnoringASCIICaseCommon):
+        * wtf/text/StringImpl.h:
+        (WTF::startsWithLettersIgnoringASCIICase):
+        * wtf/text/WTFString.h:
+        (WTF::startsWithLettersIgnoringASCIICase):
+
 2016-03-10  Frederic Wang  <fwang@igalia.com>
 
         [GTK] Add support for WOFF2
index 22529c0..9e36558 100644 (file)
@@ -590,22 +590,39 @@ template<typename CharacterType, unsigned lowercaseLettersLength> inline bool eq
     return charactersLength == lowercaseLettersStringLength && equalLettersIgnoringASCIICase(characters, lowercaseLetters, lowercaseLettersStringLength);
 }
 
-// This is intentionally not marked inline because it's used often and is not speed-critical enough to want it inlined everywhere.
-template<typename StringClass> bool equalLettersIgnoringASCIICaseCommonWithoutLength(const StringClass& string, const char* lowercaseLetters)
+template<typename StringClass> bool inline hasPrefixWithLettersIgnoringASCIICaseCommon(const StringClass& string, const char* lowercaseLetters, unsigned length)
 {
 #if !ASSERT_DISABLED
     ASSERT(*lowercaseLetters);
     for (const char* letter = lowercaseLetters; *letter; ++letter)
         ASSERT(toASCIILowerUnchecked(*letter) == *letter);
 #endif
-    unsigned length = string.length();
-    if (length != strlen(lowercaseLetters))
-        return false;
+    ASSERT(string.length() >= length);
+
     if (string.is8Bit())
         return equalLettersIgnoringASCIICase(string.characters8(), lowercaseLetters, length);
     return equalLettersIgnoringASCIICase(string.characters16(), lowercaseLetters, length);
 }
 
+// This is intentionally not marked inline because it's used often and is not speed-critical enough to want it inlined everywhere.
+template<typename StringClass> bool equalLettersIgnoringASCIICaseCommonWithoutLength(const StringClass& string, const char* lowercaseLetters)
+{
+    unsigned length = string.length();
+    if (length != strlen(lowercaseLetters))
+        return false;
+    return hasPrefixWithLettersIgnoringASCIICaseCommon(string, lowercaseLetters, length);
+}
+
+template<typename StringClass> bool startsWithLettersIgnoringASCIICaseCommonWithoutLength(const StringClass& string, const char* lowercaseLetters)
+{
+    size_t prefixLength = strlen(lowercaseLetters);
+    if (!prefixLength)
+        return true;
+    if (string.length() < prefixLength)
+        return false;
+    return hasPrefixWithLettersIgnoringASCIICaseCommon(string, lowercaseLetters, prefixLength);
+}
+
 template<typename StringClass, unsigned length> inline bool equalLettersIgnoringASCIICaseCommon(const StringClass& string, const char (&lowercaseLetters)[length])
 {
     // Don't actually use the length; we are choosing code size over speed.
@@ -614,6 +631,12 @@ template<typename StringClass, unsigned length> inline bool equalLettersIgnoring
     return equalLettersIgnoringASCIICaseCommonWithoutLength(string, pointer);
 }
 
+template<typename StringClass, unsigned length> inline bool startsWithLettersIgnoringASCIICaseCommon(const StringClass& string, const char (&lowercaseLetters)[length])
+{
+    const char* pointer = lowercaseLetters;
+    return startsWithLettersIgnoringASCIICaseCommonWithoutLength(string, pointer);
+}
+
 }
 
 using WTF::equalIgnoringASCIICase;
index 4dd924c..2823434 100644 (file)
@@ -1181,6 +1181,16 @@ inline bool equalIgnoringASCIICase(const StringImpl* a, const char* b)
     return a && equalIgnoringASCIICase(*a, b);
 }
 
+template<unsigned length> inline bool startsWithLettersIgnoringASCIICase(const StringImpl& string, const char (&lowercaseLetters)[length])
+{
+    return startsWithLettersIgnoringASCIICaseCommon(string, lowercaseLetters);
+}
+
+template<unsigned length> inline bool startsWithLettersIgnoringASCIICase(const StringImpl* string, const char (&lowercaseLetters)[length])
+{
+    return string && startsWithLettersIgnoringASCIICase(*string, lowercaseLetters);
+}
+
 template<unsigned length> inline bool equalLettersIgnoringASCIICase(const StringImpl& string, const char (&lowercaseLetters)[length])
 {
     return equalLettersIgnoringASCIICaseCommon(string, lowercaseLetters);
index 3c55908..d10ca72 100644 (file)
@@ -503,6 +503,7 @@ bool equalIgnoringASCIICase(const String&, const String&);
 bool equalIgnoringASCIICase(const String&, const char*);
 
 template<unsigned length> bool equalLettersIgnoringASCIICase(const String&, const char (&lowercaseLetters)[length]);
+template<unsigned length> bool startsWithLettersIgnoringASCIICase(const String&, const char (&lowercaseLetters)[length]);
 
 inline bool equalIgnoringNullity(const String& a, const String& b) { return equalIgnoringNullity(a.impl(), b.impl()); }
 template<size_t inlineCapacity> inline bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>& a, const String& b) { return equalIgnoringNullity(a, b.impl()); }
@@ -726,6 +727,11 @@ inline bool equalIgnoringASCIICase(const String& a, const char* b)
     return equalIgnoringASCIICase(a.impl(), b);
 }
 
+template<unsigned length> inline bool startsWithLettersIgnoringASCIICase(const String& string, const char (&lowercaseLetters)[length])
+{
+    return startsWithLettersIgnoringASCIICase(string.impl(), lowercaseLetters);
+}
+
 }
 
 using WTF::CString;
index a926417..7efe20c 100644 (file)
@@ -1,5 +1,17 @@
 2016-03-11  Youenn Fablet  <youenn.fablet@crf.canon.fr>
 
+        WTF should have a similar function as equalLettersIgnoringASCIICase to match beginning of strings
+        https://bugs.webkit.org/show_bug.cgi?id=153419
+
+        Reviewed by Darin Adler.
+
+        Covered by added unint tests.
+
+        * Modules/fetch/FetchHeaders.cpp:
+        (WebCore::isForbiddenHeaderName): Using startsWithLettersIgnoringASCIICase.
+
+2016-03-11  Youenn Fablet  <youenn.fablet@crf.canon.fr>
+
         [Fetch API] Use DeferredWrapper directly in FetchBody promise handling
         https://bugs.webkit.org/show_bug.cgi?id=155291
 
index bd53e05..1669468 100644 (file)
@@ -67,7 +67,7 @@ static bool isForbiddenHeaderName(const String& name)
             break;
         }
     }
-    return name.startsWithIgnoringASCIICase(ASCIILiteral("Sec-")) || name.startsWithIgnoringASCIICase(ASCIILiteral("Proxy-"));
+    return startsWithLettersIgnoringASCIICase(name, "sec-") || startsWithLettersIgnoringASCIICase(name, "proxy-");
 }
 
 static bool isForbiddenResponseHeaderName(const String& name)
index d745f22..ae0866f 100644 (file)
@@ -1,3 +1,14 @@
+2016-03-11  Youenn Fablet  <youenn.fablet@crf.canon.fr>
+
+        WTF should have a similar function as equalLettersIgnoringASCIICase to match beginning of strings
+        https://bugs.webkit.org/show_bug.cgi?id=153419
+
+        Reviewed by Darin Adler.
+
+        * TestWebKitAPI/Tests/WTF/StringOperators.cpp:
+        (TestWebKitAPI::TEST): Adding test case for startsWithLettersIgnoringASCIICase.
+
+
 2016-03-10  Simon Fraser  <simon.fraser@apple.com>
 
         Font antialiasing (smoothing) changes when elements are rendered into compositing layers
index 01f36f7..1747529 100644 (file)
@@ -424,6 +424,21 @@ TEST(WTF, StringImplStartsWithIgnoringASCIICaseWithEmpty)
     ASSERT_FALSE(empty->startsWithIgnoringASCIICase(*reference.get()));
 }
 
+TEST(WTF, StartsWithLettersIgnoringASCIICase)
+{
+    String string("Test tEST");
+    ASSERT_TRUE(startsWithLettersIgnoringASCIICase(string, "test t"));
+    ASSERT_TRUE(startsWithLettersIgnoringASCIICase(string, "test te"));
+    ASSERT_TRUE(startsWithLettersIgnoringASCIICase(string, "test test"));
+    ASSERT_FALSE(startsWithLettersIgnoringASCIICase(string, "test tex"));
+
+    ASSERT_TRUE(startsWithLettersIgnoringASCIICase(string, ""));
+    ASSERT_TRUE(startsWithLettersIgnoringASCIICase(String(""), ""));
+
+    ASSERT_FALSE(startsWithLettersIgnoringASCIICase(String(), "t"));
+    ASSERT_FALSE(startsWithLettersIgnoringASCIICase(String(), ""));
+}
+
 TEST(WTF, StringImplEndsWithIgnoringASCIICaseBasic)
 {
     RefPtr<StringImpl> reference = stringFromUTF8("XÉCbA");