⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 242353 in webkit


Ignore:
Timestamp:
Mar 4, 2019, 7:18:42 AM (7 years ago)
Author:
Michael Catanzaro
Message:

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):

Location:
trunk/Source/WTF
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r242330 r242353  
     12019-03-04  Michael Catanzaro  <mcatanzaro@igalia.com>
     2
     3        URLHelpers should use unorm2_quickCheck before converting to NFC
     4        https://bugs.webkit.org/show_bug.cgi?id=194272
     5
     6        Reviewed by Darin Adler.
     7
     8        If the string is already in normalization form C, don't try to normalize it.
     9
     10        * wtf/URLHelpers.cpp:
     11        (WTF::URLHelpers::toNormalizationFormC):
     12
    1132019-03-02  Darin Adler  <darin@apple.com>
    214
  • trunk/Source/WTF/wtf/URLHelpers.cpp

    r241998 r242353  
    776776static String toNormalizationFormC(const String& string)
    777777{
    778     auto sourceBuffer = string.charactersWithNullTermination();
     778    Vector<UChar> sourceBuffer = string.charactersWithNullTermination();
    779779    ASSERT(sourceBuffer.last() == '\0');
    780780    sourceBuffer.removeLast();
    781781
    782     String result;
     782    UErrorCode uerror = U_ZERO_ERROR;
     783    const UNormalizer2* normalizer = unorm2_getNFCInstance(&uerror);
     784    if (U_FAILURE(uerror))
     785        return { };
     786
     787    UNormalizationCheckResult checkResult = unorm2_quickCheck(normalizer, sourceBuffer.data(), sourceBuffer.size(), &uerror);
     788    if (U_FAILURE(uerror))
     789        return { };
     790
     791    // No need to normalize if already normalized.
     792    if (checkResult == UNORM_YES)
     793        return string;
     794
    783795    Vector<UChar, urlBytesBufferLength> normalizedCharacters(sourceBuffer.size());
    784     UErrorCode uerror = U_ZERO_ERROR;
    785     int32_t normalizedLength = 0;
    786     const UNormalizer2* normalizer = unorm2_getNFCInstance(&uerror);
    787     if (!U_FAILURE(uerror)) {
    788         normalizedLength = unorm2_normalize(normalizer, sourceBuffer.data(), sourceBuffer.size(), normalizedCharacters.data(), normalizedCharacters.size(), &uerror);
    789         if (uerror == U_BUFFER_OVERFLOW_ERROR) {
    790             uerror = U_ZERO_ERROR;
    791             normalizedCharacters.resize(normalizedLength);
    792             normalizedLength = unorm2_normalize(normalizer, sourceBuffer.data(), sourceBuffer.size(), normalizedCharacters.data(), normalizedLength, &uerror);
    793         }
    794         if (!U_FAILURE(uerror))
    795             result = String(normalizedCharacters.data(), normalizedLength);
    796     }
    797 
    798     return result;
     796    auto normalizedLength = unorm2_normalize(normalizer, sourceBuffer.data(), sourceBuffer.size(), normalizedCharacters.data(), normalizedCharacters.size(), &uerror);
     797    if (uerror == U_BUFFER_OVERFLOW_ERROR) {
     798        uerror = U_ZERO_ERROR;
     799        normalizedCharacters.resize(normalizedLength);
     800        normalizedLength = unorm2_normalize(normalizer, sourceBuffer.data(), sourceBuffer.size(), normalizedCharacters.data(), normalizedLength, &uerror);
     801    }
     802    if (U_FAILURE(uerror))
     803        return { };
     804
     805    return String(normalizedCharacters.data(), normalizedLength);
    799806}
    800807
Note: See TracChangeset for help on using the changeset viewer.