[WTF] Add more StringView / ASCIILiteral helper functions and add ICUDeleter
authorysuzuki@apple.com <ysuzuki@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 29 Jun 2020 17:23:27 +0000 (17:23 +0000)
committerysuzuki@apple.com <ysuzuki@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 29 Jun 2020 17:23:27 +0000 (17:23 +0000)
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()):

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

Source/WTF/ChangeLog
Source/WTF/WTF.xcodeproj/project.pbxproj
Source/WTF/wtf/CMakeLists.txt
Source/WTF/wtf/text/ASCIILiteral.h
Source/WTF/wtf/text/StringView.cpp
Source/WTF/wtf/text/StringView.h
Source/WTF/wtf/unicode/icu/ICUHelpers.cpp [new file with mode: 0644]
Source/WTF/wtf/unicode/icu/ICUHelpers.h

index 718ad38..259449e 100644 (file)
@@ -1,3 +1,31 @@
+2020-06-29  Yusuke Suzuki  <ysuzuki@apple.com>
+
+        [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()):
+
 2020-06-28  Geoffrey Garen  <ggaren@apple.com>
 
         Rename initializeThreading to initialize
index 71263b5..8456e30 100644 (file)
                E38D6E271F5522E300A75CC4 /* StringBuilderJSON.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E38D6E261F5522E300A75CC4 /* StringBuilderJSON.cpp */; };
                E392FA2722E92BFF00ECDC73 /* ResourceUsageCocoa.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E392FA2622E92BFF00ECDC73 /* ResourceUsageCocoa.cpp */; };
                E3A32BC41FC830E2007D7E76 /* JSValueMalloc.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E3A32BC21FC830E2007D7E76 /* JSValueMalloc.cpp */; };
+               E3BE09A724A5854D009DF2B4 /* ICUHelpers.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E3BE09A624A58545009DF2B4 /* ICUHelpers.cpp */; };
                E4A0AD391A96245500536DF6 /* WorkQueue.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4A0AD371A96245500536DF6 /* WorkQueue.cpp */; };
                E4A0AD3D1A96253C00536DF6 /* WorkQueueCocoa.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4A0AD3C1A96253C00536DF6 /* WorkQueueCocoa.cpp */; };
                EB61EDC72409CCC1001EFE36 /* SystemTracingCocoa.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EB61EDC62409CCC0001EFE36 /* SystemTracingCocoa.cpp */; };
                E392FA2622E92BFF00ECDC73 /* ResourceUsageCocoa.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ResourceUsageCocoa.cpp; sourceTree = "<group>"; };
                E3A32BC21FC830E2007D7E76 /* JSValueMalloc.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSValueMalloc.cpp; sourceTree = "<group>"; };
                E3A32BC31FC830E2007D7E76 /* JSValueMalloc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSValueMalloc.h; sourceTree = "<group>"; };
+               E3BE09A624A58545009DF2B4 /* ICUHelpers.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ICUHelpers.cpp; sourceTree = "<group>"; };
                E3CF76902115D6BA0091DE48 /* CompactPointerTuple.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CompactPointerTuple.h; sourceTree = "<group>"; };
                E3E158251EADA53C004A079D /* SystemFree.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SystemFree.h; sourceTree = "<group>"; };
                E3E64F0B22813428001E55B4 /* Nonmovable.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Nonmovable.h; sourceTree = "<group>"; };
                                0F7075F31FBF537A00489AF0 /* TimingScope.h */,
                                553071C91C40427200384898 /* TinyLRUCache.h */,
                                0FED67B51B22D4D80066CE15 /* TinyPtrSet.h */,
-                               2D1F2F462498F73300C63A83 /* TranslatedProcess.h */,
                                521CC6B324A277C2004377D6 /* TranslatedProcess.cpp */,
+                               2D1F2F462498F73300C63A83 /* TranslatedProcess.h */,
                                149EF16216BBFE0D000A4331 /* TriState.h */,
                                83FBA93119DF459700F30ADB /* TypeCasts.h */,
                                E360C7642127B85B00C90F0E /* UnalignedAccess.h */,
                        isa = PBXGroup;
                        children = (
                                A8A47350151A825B004123FF /* CollatorICU.cpp */,
+                               E3BE09A624A58545009DF2B4 /* ICUHelpers.cpp */,
                                52B228C12458DC8200753D91 /* ICUHelpers.h */,
                        );
                        path = icu;
                                2CCD892A15C0390200285083 /* GregorianDateTime.cpp in Sources */,
                                A8A473D8151A825B004123FF /* HashTable.cpp in Sources */,
                                93B5B44E2213D616004B7AA7 /* HexNumber.cpp in Sources */,
+                               E3BE09A724A5854D009DF2B4 /* ICUHelpers.cpp in Sources */,
                                7A05093F1FB9DCC500B33FB8 /* JSONValues.cpp in Sources */,
                                E3A32BC41FC830E2007D7E76 /* JSValueMalloc.cpp in Sources */,
                                C2BCFC401F61D13000C9222C /* Language.cpp in Sources */,
                                A5BA15F51824348000A82E69 /* StringImplCocoa.mm in Sources */,
                                0FDDBFA71666DFA300C55FEF /* StringPrintStream.cpp in Sources */,
                                93F1993E19D7958D00C2390B /* StringView.cpp in Sources */,
-                               521CC6B424A27809004377D6 /* TranslatedProcess.cpp in Sources */,
                                93934BD518A1F16900D0D6A1 /* StringViewCF.cpp in Sources */,
                                93934BD318A1E8C300D0D6A1 /* StringViewCocoa.mm in Sources */,
                                A8A473B7151A825B004123FF /* strtod.cc in Sources */,
                                5311BD5C1EA822F900525281 /* ThreadMessage.cpp in Sources */,
                                0F66B2901DC97BAB004A1D3F /* TimeWithDynamicClockType.cpp in Sources */,
                                0F7075F51FBF53CD00489AF0 /* TimingScope.cpp in Sources */,
+                               521CC6B424A27809004377D6 /* TranslatedProcess.cpp in Sources */,
                                0FA6F39520CCACE900A03DCD /* UniqueArray.cpp in Sources */,
                                5CC0EE7621629F1900A1A842 /* URL.cpp in Sources */,
                                5C1F0595216437B30039302C /* URLCF.cpp in Sources */,
index ed71eb2..d220e6a 100644 (file)
@@ -499,6 +499,7 @@ set(WTF_SOURCES
     unicode/UTF8Conversion.cpp
 
     unicode/icu/CollatorICU.cpp
+    unicode/icu/ICUHelpers.cpp
 )
 
 set(WTF_PRIVATE_INCLUDE_DIRECTORIES
index a908df3..f7a479a 100644 (file)
@@ -26,6 +26,7 @@
 #pragma once
 
 #include <wtf/ASCIICType.h>
+#include <wtf/StdLibExtras.h>
 
 namespace WTF {
 
@@ -47,7 +48,9 @@ public:
         return ASCIILiteral { nullptr };
     }
 
-    const char* characters() const { return m_characters; }
+    constexpr const char* characters() const { return m_characters; }
+    const LChar* characters8() const { return bitwise_cast<const LChar*>(m_characters); }
+    size_t length() const { return strlen(m_characters); }
 
 private:
     constexpr explicit ASCIILiteral(const char* characters) : m_characters(characters) { }
index fd79383..41fc7a6 100644 (file)
@@ -340,6 +340,20 @@ bool StringView::contains(const char* string) const
     return find(string) != notFound;
 }
 
+int codePointCompare(StringView lhs, StringView rhs)
+{
+    bool lhsIs8Bit = lhs.is8Bit();
+    bool rhsIs8Bit = rhs.is8Bit();
+    if (lhsIs8Bit) {
+        if (rhsIs8Bit)
+            return codePointCompare(lhs.characters8(), lhs.length(), rhs.characters8(), rhs.length());
+        return codePointCompare(lhs.characters8(), lhs.length(), rhs.characters16(), rhs.length());
+    }
+    if (rhsIs8Bit)
+        return codePointCompare(lhs.characters16(), lhs.length(), rhs.characters8(), rhs.length());
+    return codePointCompare(lhs.characters16(), lhs.length(), rhs.characters16(), rhs.length());
+}
+
 #if CHECK_STRINGVIEW_LIFETIME
 
 // Manage reference count manually so UnderlyingString does not need to be defined in the header.
index 8bdc8b0..6c32179 100644 (file)
@@ -31,6 +31,7 @@
 #include <wtf/Optional.h>
 #include <wtf/RetainPtr.h>
 #include <wtf/Vector.h>
+#include <wtf/text/ASCIILiteral.h>
 #include <wtf/text/CString.h>
 #include <wtf/text/ConversionMode.h>
 #include <wtf/text/LChar.h>
@@ -68,6 +69,7 @@ public:
     StringView(const UChar*, unsigned length);
     StringView(const char*);
     StringView(const char*, unsigned length);
+    explicit StringView(ASCIILiteral);
 
     static StringView empty();
 
@@ -356,6 +358,11 @@ inline StringView::StringView(const char* characters, unsigned length)
     initialize(reinterpret_cast<const LChar*>(characters), length);
 }
 
+inline StringView::StringView(ASCIILiteral string)
+{
+    initialize(string.characters8(), string.length());
+}
+
 inline StringView::StringView(const StringImpl& string)
 {
     setUnderlyingString(&string);
@@ -1066,6 +1073,8 @@ inline bool equalIgnoringNullity(StringView a, StringView b)
     return equal(a, b);
 }
 
+WTF_EXPORT_PRIVATE int codePointCompare(StringView, StringView);
+
 } // namespace WTF
 
 using WTF::append;
diff --git a/Source/WTF/wtf/unicode/icu/ICUHelpers.cpp b/Source/WTF/wtf/unicode/icu/ICUHelpers.cpp
new file mode 100644 (file)
index 0000000..c6f3e10
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2020 Apple Inc. All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include <wtf/unicode/icu/ICUHelpers.h>
+
+#include <mutex>
+#include <unicode/uvernum.h>
+
+namespace WTF {
+namespace ICU {
+
+static const UVersionInfo& version()
+{
+    static UVersionInfo versions { };
+    static std::once_flag onceKey;
+    std::call_once(onceKey, [&] {
+        u_getVersion(versions);
+    });
+    return versions;
+}
+
+unsigned majorVersion()
+{
+    static_assert(0 < U_MAX_VERSION_LENGTH);
+    return version()[0];
+}
+
+unsigned minorVersion()
+{
+    static_assert(1 < U_MAX_VERSION_LENGTH);
+    return version()[1];
+}
+
+} } // namespace WTF::ICU
index 72a943e..9f8d3ae 100644 (file)
@@ -28,6 +28,7 @@
 #include <tuple>
 #include <unicode/utypes.h>
 #include <wtf/Forward.h>
+#include <wtf/FunctionTraits.h>
 
 namespace WTF {
 
@@ -107,8 +108,24 @@ template<typename FunctionType, typename ...ArgumentTypes> UErrorCode callBuffer
     return status;
 }
 
-}
+template<auto deleteFunction>
+struct ICUDeleter {
+    void operator()(typename FunctionTraits<decltype(deleteFunction)>::template ArgumentType<0> pointer)
+    {
+        if (pointer)
+            deleteFunction(pointer);
+    }
+};
+
+namespace ICU {
+
+WTF_EXPORT_PRIVATE unsigned majorVersion();
+WTF_EXPORT_PRIVATE unsigned minorVersion();
+
+} // namespace ICU
+} // namespace WTF
 
 using WTF::callBufferProducingFunction;
 using WTF::needsToGrowToProduceCString;
 using WTF::needsToGrowToProduceBuffer;
+using WTF::ICUDeleter;