Changeset 263665 in webkit


Ignore:
Timestamp:
Jun 29, 2020 10:23:27 AM (4 years ago)
Author:
ysuzuki@apple.com
Message:

[WTF] Add more StringView / ASCIILiteral helper functions and add ICUDeleter
https://bugs.webkit.org/show_bug.cgi?id=209774

Reviewed by Ross Kirsling and Darin Adler.

Add ICUDeleter for cleaner ICU object deletion.
Add ICU::majorVersion and ICU::minorVersion to switch the skeleton string in Intl.NumberFormat.
Add more features for ASCIILiteral.

This patch is part of https://bugs.webkit.org/show_bug.cgi?id=209774. I land it separately from
Intl.NumberFormat change to unlock the further Intl implementations using ICUDeleter.

  • WTF.xcodeproj/project.pbxproj:
  • wtf/CMakeLists.txt:
  • wtf/text/ASCIILiteral.h:
  • wtf/text/StringView.cpp:

(WTF::codePointCompare):

  • wtf/text/StringView.h:

(WTF::StringView::StringView):

  • wtf/unicode/icu/ICUHelpers.cpp: Copied from Source/WTF/wtf/text/ASCIILiteral.h.

(WTF::ICU::version):
(WTF::ICU::majorVersion):
(WTF::ICU::minorVersion):

  • wtf/unicode/icu/ICUHelpers.h:

(WTF::ICUDeleter::operator()):

Location:
trunk/Source/WTF
Files:
7 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r263635 r263665  
     12020-06-29  Yusuke Suzuki  <ysuzuki@apple.com>
     2
     3        [WTF] Add more StringView / ASCIILiteral helper functions and add ICUDeleter
     4        https://bugs.webkit.org/show_bug.cgi?id=209774
     5
     6        Reviewed by Ross Kirsling and Darin Adler.
     7
     8        Add ICUDeleter for cleaner ICU object deletion.
     9        Add ICU::majorVersion and ICU::minorVersion to switch the skeleton string in Intl.NumberFormat.
     10        Add more features for ASCIILiteral.
     11
     12        This patch is part of https://bugs.webkit.org/show_bug.cgi?id=209774. I land it separately from
     13        Intl.NumberFormat change to unlock the further Intl implementations using ICUDeleter.
     14
     15        * WTF.xcodeproj/project.pbxproj:
     16        * wtf/CMakeLists.txt:
     17        * wtf/text/ASCIILiteral.h:
     18        * wtf/text/StringView.cpp:
     19        (WTF::codePointCompare):
     20        * wtf/text/StringView.h:
     21        (WTF::StringView::StringView):
     22        * wtf/unicode/icu/ICUHelpers.cpp: Copied from Source/WTF/wtf/text/ASCIILiteral.h.
     23        (WTF::ICU::version):
     24        (WTF::ICU::majorVersion):
     25        (WTF::ICU::minorVersion):
     26        * wtf/unicode/icu/ICUHelpers.h:
     27        (WTF::ICUDeleter::operator()):
     28
    1292020-06-28  Geoffrey Garen  <ggaren@apple.com>
    230
  • trunk/Source/WTF/WTF.xcodeproj/project.pbxproj

    r263529 r263665  
    181181                E392FA2722E92BFF00ECDC73 /* ResourceUsageCocoa.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E392FA2622E92BFF00ECDC73 /* ResourceUsageCocoa.cpp */; };
    182182                E3A32BC41FC830E2007D7E76 /* JSValueMalloc.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E3A32BC21FC830E2007D7E76 /* JSValueMalloc.cpp */; };
     183                E3BE09A724A5854D009DF2B4 /* ICUHelpers.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E3BE09A624A58545009DF2B4 /* ICUHelpers.cpp */; };
    183184                E4A0AD391A96245500536DF6 /* WorkQueue.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4A0AD371A96245500536DF6 /* WorkQueue.cpp */; };
    184185                E4A0AD3D1A96253C00536DF6 /* WorkQueueCocoa.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4A0AD3C1A96253C00536DF6 /* WorkQueueCocoa.cpp */; };
     
    732733                E3A32BC21FC830E2007D7E76 /* JSValueMalloc.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSValueMalloc.cpp; sourceTree = "<group>"; };
    733734                E3A32BC31FC830E2007D7E76 /* JSValueMalloc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSValueMalloc.h; sourceTree = "<group>"; };
     735                E3BE09A624A58545009DF2B4 /* ICUHelpers.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ICUHelpers.cpp; sourceTree = "<group>"; };
    734736                E3CF76902115D6BA0091DE48 /* CompactPointerTuple.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CompactPointerTuple.h; sourceTree = "<group>"; };
    735737                E3E158251EADA53C004A079D /* SystemFree.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SystemFree.h; sourceTree = "<group>"; };
     
    12741276                                553071C91C40427200384898 /* TinyLRUCache.h */,
    12751277                                0FED67B51B22D4D80066CE15 /* TinyPtrSet.h */,
     1278                                521CC6B324A277C2004377D6 /* TranslatedProcess.cpp */,
    12761279                                2D1F2F462498F73300C63A83 /* TranslatedProcess.h */,
    1277                                 521CC6B324A277C2004377D6 /* TranslatedProcess.cpp */,
    12781280                                149EF16216BBFE0D000A4331 /* TriState.h */,
    12791281                                83FBA93119DF459700F30ADB /* TypeCasts.h */,
     
    14531455                        children = (
    14541456                                A8A47350151A825B004123FF /* CollatorICU.cpp */,
     1457                                E3BE09A624A58545009DF2B4 /* ICUHelpers.cpp */,
    14551458                                52B228C12458DC8200753D91 /* ICUHelpers.h */,
    14561459                        );
     
    16771680                                A8A473D8151A825B004123FF /* HashTable.cpp in Sources */,
    16781681                                93B5B44E2213D616004B7AA7 /* HexNumber.cpp in Sources */,
     1682                                E3BE09A724A5854D009DF2B4 /* ICUHelpers.cpp in Sources */,
    16791683                                7A05093F1FB9DCC500B33FB8 /* JSONValues.cpp in Sources */,
    16801684                                E3A32BC41FC830E2007D7E76 /* JSValueMalloc.cpp in Sources */,
     
    17481752                                0FDDBFA71666DFA300C55FEF /* StringPrintStream.cpp in Sources */,
    17491753                                93F1993E19D7958D00C2390B /* StringView.cpp in Sources */,
    1750                                 521CC6B424A27809004377D6 /* TranslatedProcess.cpp in Sources */,
    17511754                                93934BD518A1F16900D0D6A1 /* StringViewCF.cpp in Sources */,
    17521755                                93934BD318A1E8C300D0D6A1 /* StringViewCocoa.mm in Sources */,
     
    17651768                                0F66B2901DC97BAB004A1D3F /* TimeWithDynamicClockType.cpp in Sources */,
    17661769                                0F7075F51FBF53CD00489AF0 /* TimingScope.cpp in Sources */,
     1770                                521CC6B424A27809004377D6 /* TranslatedProcess.cpp in Sources */,
    17671771                                0FA6F39520CCACE900A03DCD /* UniqueArray.cpp in Sources */,
    17681772                                5CC0EE7621629F1900A1A842 /* URL.cpp in Sources */,
  • trunk/Source/WTF/wtf/CMakeLists.txt

    r263529 r263665  
    500500
    501501    unicode/icu/CollatorICU.cpp
     502    unicode/icu/ICUHelpers.cpp
    502503)
    503504
  • trunk/Source/WTF/wtf/text/ASCIILiteral.h

    r263341 r263665  
    2727
    2828#include <wtf/ASCIICType.h>
     29#include <wtf/StdLibExtras.h>
    2930
    3031namespace WTF {
     
    4849    }
    4950
    50     const char* characters() const { return m_characters; }
     51    constexpr const char* characters() const { return m_characters; }
     52    const LChar* characters8() const { return bitwise_cast<const LChar*>(m_characters); }
     53    size_t length() const { return strlen(m_characters); }
    5154
    5255private:
  • trunk/Source/WTF/wtf/text/StringView.cpp

    r261661 r263665  
    339339{
    340340    return find(string) != notFound;
     341}
     342
     343int codePointCompare(StringView lhs, StringView rhs)
     344{
     345    bool lhsIs8Bit = lhs.is8Bit();
     346    bool rhsIs8Bit = rhs.is8Bit();
     347    if (lhsIs8Bit) {
     348        if (rhsIs8Bit)
     349            return codePointCompare(lhs.characters8(), lhs.length(), rhs.characters8(), rhs.length());
     350        return codePointCompare(lhs.characters8(), lhs.length(), rhs.characters16(), rhs.length());
     351    }
     352    if (rhsIs8Bit)
     353        return codePointCompare(lhs.characters16(), lhs.length(), rhs.characters8(), rhs.length());
     354    return codePointCompare(lhs.characters16(), lhs.length(), rhs.characters16(), rhs.length());
    341355}
    342356
  • trunk/Source/WTF/wtf/text/StringView.h

    r260881 r263665  
    3232#include <wtf/RetainPtr.h>
    3333#include <wtf/Vector.h>
     34#include <wtf/text/ASCIILiteral.h>
    3435#include <wtf/text/CString.h>
    3536#include <wtf/text/ConversionMode.h>
     
    6970    StringView(const char*);
    7071    StringView(const char*, unsigned length);
     72    explicit StringView(ASCIILiteral);
    7173
    7274    static StringView empty();
     
    355357{
    356358    initialize(reinterpret_cast<const LChar*>(characters), length);
     359}
     360
     361inline StringView::StringView(ASCIILiteral string)
     362{
     363    initialize(string.characters8(), string.length());
    357364}
    358365
     
    10671074}
    10681075
     1076WTF_EXPORT_PRIVATE int codePointCompare(StringView, StringView);
     1077
    10691078} // namespace WTF
    10701079
  • trunk/Source/WTF/wtf/unicode/icu/ICUHelpers.cpp

    r263663 r263665  
    11/*
    2  * Copyright (C) 2018 Yusuke Suzuki <utatane.tea@gmail.com>
     2 * Copyright (C) 2020 Apple Inc. All Rights Reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    2424 */
    2525
    26 #pragma once
     26#include "config.h"
     27#include <wtf/unicode/icu/ICUHelpers.h>
    2728
    28 #include <wtf/ASCIICType.h>
     29#include <mutex>
     30#include <unicode/uvernum.h>
    2931
    3032namespace WTF {
     33namespace ICU {
    3134
    32 class PrintStream;
    33 
    34 class ASCIILiteral final {
    35 public:
    36     operator const char*() const { return m_characters; }
    37 
    38     static constexpr ASCIILiteral fromLiteralUnsafe(const char* string)
    39     {
    40         return ASCIILiteral { string };
    41     }
    42 
    43     WTF_EXPORT_PRIVATE void dump(PrintStream& out) const;
    44 
    45     static constexpr ASCIILiteral null()
    46     {
    47         return ASCIILiteral { nullptr };
    48     }
    49 
    50     const char* characters() const { return m_characters; }
    51 
    52 private:
    53     constexpr explicit ASCIILiteral(const char* characters) : m_characters(characters) { }
    54 
    55     const char* m_characters;
    56 };
    57 
    58 inline namespace StringLiterals {
    59 
    60 constexpr ASCIILiteral operator"" _s(const char* characters, size_t n)
     35static const UVersionInfo& version()
    6136{
    62 #if ASSERT_ENABLED
    63     for (size_t i = 0; i < n; ++i)
    64         ASSERT_UNDER_CONSTEXPR_CONTEXT(isASCII(characters[i]));
    65 #else
    66     UNUSED_PARAM(n);
    67 #endif
    68     return ASCIILiteral::fromLiteralUnsafe(characters);
     37    static UVersionInfo versions { };
     38    static std::once_flag onceKey;
     39    std::call_once(onceKey, [&] {
     40        u_getVersion(versions);
     41    });
     42    return versions;
    6943}
    7044
    71 } // inline StringLiterals
     45unsigned majorVersion()
     46{
     47    static_assert(0 < U_MAX_VERSION_LENGTH);
     48    return version()[0];
     49}
    7250
    73 } // namespace WTF
     51unsigned minorVersion()
     52{
     53    static_assert(1 < U_MAX_VERSION_LENGTH);
     54    return version()[1];
     55}
    7456
    75 using namespace WTF::StringLiterals;
    76 using WTF::ASCIILiteral;
     57} } // namespace WTF::ICU
  • trunk/Source/WTF/wtf/unicode/icu/ICUHelpers.h

    r261386 r263665  
    2929#include <unicode/utypes.h>
    3030#include <wtf/Forward.h>
     31#include <wtf/FunctionTraits.h>
    3132
    3233namespace WTF {
     
    108109}
    109110
    110 }
     111template<auto deleteFunction>
     112struct ICUDeleter {
     113    void operator()(typename FunctionTraits<decltype(deleteFunction)>::template ArgumentType<0> pointer)
     114    {
     115        if (pointer)
     116            deleteFunction(pointer);
     117    }
     118};
     119
     120namespace ICU {
     121
     122WTF_EXPORT_PRIVATE unsigned majorVersion();
     123WTF_EXPORT_PRIVATE unsigned minorVersion();
     124
     125} // namespace ICU
     126} // namespace WTF
    111127
    112128using WTF::callBufferProducingFunction;
    113129using WTF::needsToGrowToProduceCString;
    114130using WTF::needsToGrowToProduceBuffer;
     131using WTF::ICUDeleter;
Note: See TracChangeset for help on using the changeset viewer.