URLHelpers should use unorm2_quickCheck before converting to NFC
authormcatanzaro@igalia.com <mcatanzaro@igalia.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 4 Mar 2019 15:18:42 +0000 (15:18 +0000)
committermcatanzaro@igalia.com <mcatanzaro@igalia.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 4 Mar 2019 15:18:42 +0000 (15:18 +0000)
https://bugs.webkit.org/show_bug.cgi?id=194272

Reviewed by Darin Adler.

If the string is already in normalization form C, don't try to normalize it.

* wtf/URLHelpers.cpp:
(WTF::URLHelpers::toNormalizationFormC):

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

Source/WTF/ChangeLog
Source/WTF/wtf/URLHelpers.cpp

index f0d8f82..33fda98 100644 (file)
@@ -1,3 +1,15 @@
+2019-03-04  Michael Catanzaro  <mcatanzaro@igalia.com>
+
+        URLHelpers should use unorm2_quickCheck before converting to NFC
+        https://bugs.webkit.org/show_bug.cgi?id=194272
+
+        Reviewed by Darin Adler.
+
+        If the string is already in normalization form C, don't try to normalize it.
+
+        * wtf/URLHelpers.cpp:
+        (WTF::URLHelpers::toNormalizationFormC):
+
 2019-03-02  Darin Adler  <darin@apple.com>
 
         Retire legacy dtoa function and DecimalNumber class
index 4fb287a..c943e33 100644 (file)
@@ -775,27 +775,34 @@ static String createStringWithEscapedUnsafeCharacters(const String& sourceBuffer
 
 static String toNormalizationFormC(const String& string)
 {
-    auto sourceBuffer = string.charactersWithNullTermination();
+    Vector<UChar> sourceBuffer = string.charactersWithNullTermination();
     ASSERT(sourceBuffer.last() == '\0');
     sourceBuffer.removeLast();
 
-    String result;
-    Vector<UChar, urlBytesBufferLength> normalizedCharacters(sourceBuffer.size());
     UErrorCode uerror = U_ZERO_ERROR;
-    int32_t normalizedLength = 0;
     const UNormalizer2* normalizer = unorm2_getNFCInstance(&uerror);
-    if (!U_FAILURE(uerror)) {
-        normalizedLength = unorm2_normalize(normalizer, sourceBuffer.data(), sourceBuffer.size(), normalizedCharacters.data(), normalizedCharacters.size(), &uerror);
-        if (uerror == U_BUFFER_OVERFLOW_ERROR) {
-            uerror = U_ZERO_ERROR;
-            normalizedCharacters.resize(normalizedLength);
-            normalizedLength = unorm2_normalize(normalizer, sourceBuffer.data(), sourceBuffer.size(), normalizedCharacters.data(), normalizedLength, &uerror);
-        }
-        if (!U_FAILURE(uerror))
-            result = String(normalizedCharacters.data(), normalizedLength);
+    if (U_FAILURE(uerror))
+        return { };
+
+    UNormalizationCheckResult checkResult = unorm2_quickCheck(normalizer, sourceBuffer.data(), sourceBuffer.size(), &uerror);
+    if (U_FAILURE(uerror))
+        return { };
+
+    // No need to normalize if already normalized.
+    if (checkResult == UNORM_YES)
+        return string;
+
+    Vector<UChar, urlBytesBufferLength> normalizedCharacters(sourceBuffer.size());
+    auto normalizedLength = unorm2_normalize(normalizer, sourceBuffer.data(), sourceBuffer.size(), normalizedCharacters.data(), normalizedCharacters.size(), &uerror);
+    if (uerror == U_BUFFER_OVERFLOW_ERROR) {
+        uerror = U_ZERO_ERROR;
+        normalizedCharacters.resize(normalizedLength);
+        normalizedLength = unorm2_normalize(normalizer, sourceBuffer.data(), sourceBuffer.size(), normalizedCharacters.data(), normalizedLength, &uerror);
     }
+    if (U_FAILURE(uerror))
+        return { };
 
-    return result;
+    return String(normalizedCharacters.data(), normalizedLength);
 }
 
 String userVisibleURL(const CString& url)