Changeset 239970 in webkit


Ignore:
Timestamp:
Jan 14, 2019 7:26:04 PM (5 years ago)
Author:
Michael Catanzaro
Message:

Use unorm2_normalize instead of precomposedStringWithCanonicalMapping in userVisibleString
https://bugs.webkit.org/show_bug.cgi?id=192945

Reviewed by Alex Christensen.

Replace use of the nice NSString function precomposedStringWithCanonicalMapping with the ICU
API unorm2_normalize. This is to prep the code for translation to cross-platform C++. Of
course this is much worse than the preexisting code, but this is just a transitional
measure and not the final state of the code. It wouldn't make sense to do this if the code
were to remain Objective C++.

  • wtf/cocoa/NSURLExtras.mm:

(WTF::toNormalizationFormC):
(WTF::userVisibleString):

Location:
trunk/Source/WTF
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r239967 r239970  
     12019-01-14  Michael Catanzaro  <mcatanzaro@igalia.com>
     2
     3        Use unorm2_normalize instead of precomposedStringWithCanonicalMapping in userVisibleString
     4        https://bugs.webkit.org/show_bug.cgi?id=192945
     5
     6        Reviewed by Alex Christensen.
     7
     8        Replace use of the nice NSString function precomposedStringWithCanonicalMapping with the ICU
     9        API unorm2_normalize. This is to prep the code for translation to cross-platform C++. Of
     10        course this is much worse than the preexisting code, but this is just a transitional
     11        measure and not the final state of the code. It wouldn't make sense to do this if the code
     12        were to remain Objective C++.
     13
     14        * wtf/cocoa/NSURLExtras.mm:
     15        (WTF::toNormalizationFormC):
     16        (WTF::userVisibleString):
     17
    1182019-01-14  Alex Christensen  <achristensen@webkit.org>
    219
  • trunk/Source/WTF/wtf/cocoa/NSURLExtras.mm

    r239967 r239970  
    3333#import <unicode/uchar.h>
    3434#import <unicode/uidna.h>
     35#import <unicode/unorm.h>
    3536#import <unicode/uscript.h>
    3637#import <wtf/Function.h>
     
    11061107}
    11071108
     1109static String toNormalizationFormC(const String& string)
     1110{
     1111    auto sourceBuffer = string.charactersWithNullTermination();
     1112    ASSERT(sourceBuffer.last() == '\0');
     1113    sourceBuffer.removeLast();
     1114
     1115    String result;
     1116    Vector<UChar, URL_BYTES_BUFFER_LENGTH> normalizedCharacters(sourceBuffer.size());
     1117    UErrorCode uerror = U_ZERO_ERROR;
     1118    int32_t normalizedLength = 0;
     1119    const UNormalizer2 *normalizer = unorm2_getNFCInstance(&uerror);
     1120    if (!U_FAILURE(uerror)) {
     1121        normalizedLength = unorm2_normalize(normalizer, sourceBuffer.data(), sourceBuffer.size(), normalizedCharacters.data(), normalizedCharacters.size(), &uerror);
     1122        if (uerror == U_BUFFER_OVERFLOW_ERROR) {
     1123            uerror = U_ZERO_ERROR;
     1124            normalizedCharacters.resize(normalizedLength);
     1125            normalizedLength = unorm2_normalize(normalizer, sourceBuffer.data(), sourceBuffer.size(), normalizedCharacters.data(), normalizedLength, &uerror);
     1126        }
     1127        if (!U_FAILURE(uerror))
     1128            result = String(normalizedCharacters.data(), normalizedLength);
     1129    }
     1130
     1131    return result;
     1132}
     1133
    11081134NSString *userVisibleString(NSURL *URL)
    11091135{
     
    11761202    }
    11771203
    1178     result = [result precomposedStringWithCanonicalMapping];
     1204    auto wtfString = String(result.get());
     1205    auto normalized = toNormalizationFormC(wtfString);
     1206    result = static_cast<NSString *>(normalized);
    11791207    return CFBridgingRelease(createStringWithEscapedUnsafeCharacters((__bridge CFStringRef)result.get()));
    11801208}
Note: See TracChangeset for help on using the changeset viewer.