Add a String literal that returns a String
authordino@apple.com <dino@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 8 Nov 2018 18:51:59 +0000 (18:51 +0000)
committerdino@apple.com <dino@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 8 Nov 2018 18:51:59 +0000 (18:51 +0000)
https://bugs.webkit.org/show_bug.cgi?id=191425
<rdar://problem/45914556>

Reviewed by Sam Weinig.

Source/WebCore:

Use _str where possible.

API Test in WPT.

* Modules/fetch/FetchRequest.cpp:
(WebCore::computeReferrer):
* Modules/indexeddb/IDBKeyPath.cpp:
(WebCore::loggingString):
* Modules/webdatabase/OriginLock.cpp:
(WebCore::OriginLock::lockFileNameForPath):
* css/CSSBasicShapes.cpp:
(WebCore::updateCornerRadiusWidthAndHeight):
* html/canvas/WebGL2RenderingContext.cpp:
(WebCore::WebGL2RenderingContext::getParameter):
* html/canvas/WebGLRenderingContext.cpp:
(WebCore::WebGLRenderingContext::getParameter):
* loader/LinkHeader.cpp:
(WebCore::parseParameterValue):
* loader/LinkLoader.cpp:
(WebCore::LinkLoader::preloadIfNeeded):
* page/NavigatorBase.cpp:
(WebCore::NavigatorBase::platform):
* platform/DateComponents.cpp:
(WebCore::DateComponents::toString const):
* platform/mac/PlatformEventFactoryMac.mm:
(WebCore::keyIdentifierForKeyEvent):
* rendering/RenderListMarker.cpp:
(WebCore::RenderListMarker::suffix const):
* rendering/RenderMenuList.cpp:
(RenderMenuList::setText):
* testing/InternalSettings.cpp:
(WebCore::InternalSettings::userInterfaceDirectionPolicy):
(WebCore::InternalSettings::systemLayoutDirection):
* testing/Internals.cpp:
(WebCore::Internals::shadowRootType const):
(WebCore::Internals::getCurrentCursorInfo):

Source/WTF:

Add a new String literal, _str, that will return a String type.
This is useful when ""_s won't work, such as for things that
don't take an ASCIILiteral directly e.g. ExceptionOr<String>
or Variants.

* wtf/text/WTFString.h:
(WTF::StringLiterals::operator _str): Added.

Tools:

Test _str.

* TestWebKitAPI/Tests/WTF/WTFString.cpp:
(TestWebKitAPI::TEST):

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

20 files changed:
Source/WTF/ChangeLog
Source/WTF/wtf/text/WTFString.h
Source/WebCore/ChangeLog
Source/WebCore/Modules/fetch/FetchRequest.cpp
Source/WebCore/Modules/indexeddb/IDBKeyPath.cpp
Source/WebCore/Modules/webdatabase/OriginLock.cpp
Source/WebCore/css/CSSBasicShapes.cpp
Source/WebCore/html/canvas/WebGL2RenderingContext.cpp
Source/WebCore/html/canvas/WebGLRenderingContext.cpp
Source/WebCore/loader/LinkHeader.cpp
Source/WebCore/loader/LinkLoader.cpp
Source/WebCore/page/NavigatorBase.cpp
Source/WebCore/platform/DateComponents.cpp
Source/WebCore/platform/mac/PlatformEventFactoryMac.mm
Source/WebCore/rendering/RenderListMarker.cpp
Source/WebCore/rendering/RenderMenuList.cpp
Source/WebCore/testing/InternalSettings.cpp
Source/WebCore/testing/Internals.cpp
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WTF/WTFString.cpp

index 49b09ef..aeb2e32 100644 (file)
@@ -1,3 +1,19 @@
+2018-11-08  Dean Jackson  <dino@apple.com>
+
+        Add a String literal that returns a String
+        https://bugs.webkit.org/show_bug.cgi?id=191425
+        <rdar://problem/45914556>
+
+        Reviewed by Sam Weinig.
+
+        Add a new String literal, _str, that will return a String type.
+        This is useful when ""_s won't work, such as for things that
+        don't take an ASCIILiteral directly e.g. ExceptionOr<String>
+        or Variants.
+
+        * wtf/text/WTFString.h:
+        (WTF::StringLiterals::operator _str): Added.
+
 2018-11-06  Justin Fan  <justin_fan@apple.com>
 
         [WebGPU] Experimental prototype for WebGPURenderPipeline and WebGPUSwapChain
index 9a60774..60bb16e 100644 (file)
@@ -631,6 +631,15 @@ template<unsigned length> inline bool startsWithLettersIgnoringASCIICase(const S
     return startsWithLettersIgnoringASCIICase(string.impl(), lowercaseLetters);
 }
 
+inline namespace StringLiterals {
+
+inline String operator"" _str(const char* characters, size_t)
+{
+    return ASCIILiteral::fromLiteralUnsafe(characters);
+}
+
+} // inline StringLiterals
+
 } // namespace WTF
 
 using WTF::KeepTrailingZeros;
index 218c090..42e28ce 100644 (file)
@@ -1,3 +1,48 @@
+2018-11-08  Dean Jackson  <dino@apple.com>
+
+        Add a String literal that returns a String
+        https://bugs.webkit.org/show_bug.cgi?id=191425
+        <rdar://problem/45914556>
+
+        Reviewed by Sam Weinig.
+
+        Use _str where possible.
+
+        API Test in WPT.
+
+        * Modules/fetch/FetchRequest.cpp:
+        (WebCore::computeReferrer):
+        * Modules/indexeddb/IDBKeyPath.cpp:
+        (WebCore::loggingString):
+        * Modules/webdatabase/OriginLock.cpp:
+        (WebCore::OriginLock::lockFileNameForPath):
+        * css/CSSBasicShapes.cpp:
+        (WebCore::updateCornerRadiusWidthAndHeight):
+        * html/canvas/WebGL2RenderingContext.cpp:
+        (WebCore::WebGL2RenderingContext::getParameter):
+        * html/canvas/WebGLRenderingContext.cpp:
+        (WebCore::WebGLRenderingContext::getParameter):
+        * loader/LinkHeader.cpp:
+        (WebCore::parseParameterValue):
+        * loader/LinkLoader.cpp:
+        (WebCore::LinkLoader::preloadIfNeeded):
+        * page/NavigatorBase.cpp:
+        (WebCore::NavigatorBase::platform):
+        * platform/DateComponents.cpp:
+        (WebCore::DateComponents::toString const):
+        * platform/mac/PlatformEventFactoryMac.mm:
+        (WebCore::keyIdentifierForKeyEvent):
+        * rendering/RenderListMarker.cpp:
+        (WebCore::RenderListMarker::suffix const):
+        * rendering/RenderMenuList.cpp:
+        (RenderMenuList::setText):
+        * testing/InternalSettings.cpp:
+        (WebCore::InternalSettings::userInterfaceDirectionPolicy):
+        (WebCore::InternalSettings::systemLayoutDirection):
+        * testing/Internals.cpp:
+        (WebCore::Internals::shadowRootType const):
+        (WebCore::Internals::getCurrentCursorInfo):
+
 2018-11-08  Jonathan Hammer  <jonathan@e3software.com>
 
         Plain text drag in contenteditable is always DragOperationCopy, never DragOperationMove
index c37ac69..e3ee5d9 100644 (file)
@@ -48,7 +48,7 @@ static std::optional<Exception> setMethod(ResourceRequest& request, const String
 static ExceptionOr<String> computeReferrer(ScriptExecutionContext& context, const String& referrer)
 {
     if (referrer.isEmpty())
-        return String { "no-referrer"_s };
+        return "no-referrer"_str;
 
     // FIXME: Tighten the URL parsing algorithm according https://url.spec.whatwg.org/#concept-url-parser.
     URL referrerURL = context.completeURL(referrer);
@@ -56,10 +56,10 @@ static ExceptionOr<String> computeReferrer(ScriptExecutionContext& context, cons
         return Exception { TypeError, "Referrer is not a valid URL."_s };
 
     if (referrerURL.protocolIs("about") && referrerURL.path() == "client")
-        return String { "client"_s };
+        return "client"_str;
 
     if (!(context.securityOrigin() && context.securityOrigin()->canRequest(referrerURL)))
-        return String { "client"_s };
+        return "client"_str;
 
     return String { referrerURL.string() };
 }
index 76ebeb3..13bc60b 100644 (file)
@@ -230,7 +230,7 @@ String loggingString(const IDBKeyPath& path)
         return makeString("< ", string, " >");
     }, [](const Vector<String>& strings) {
         if (strings.isEmpty())
-            return String("< >");
+            return "< >"_str;
 
         StringBuilder builder;
         builder.append("< ");
index 8f1d0ea..423b2e6 100644 (file)
@@ -32,7 +32,7 @@ namespace WebCore {
 
 String OriginLock::lockFileNameForPath(String originPath)
 {
-    return FileSystem::pathByAppendingComponent(originPath, String(".lock"));
+    return FileSystem::pathByAppendingComponent(originPath, ".lock"_s);
 }
 
 OriginLock::OriginLock(String originPath)
index 8a9cf66..e1eaf94 100644 (file)
@@ -386,7 +386,7 @@ static inline void updateCornerRadiusWidthAndHeight(CSSPrimitiveValue* corner, S
         return;
 
     Pair* radius = corner->pairValue();
-    width = radius->first() ? radius->first()->cssText() : String("0");
+    width = radius->first() ? radius->first()->cssText() : "0"_str;
     if (radius->second())
         height = radius->second()->cssText();
 }
index b56eca5..45cdeee 100644 (file)
@@ -1870,7 +1870,7 @@ WebGLAny WebGL2RenderingContext::getParameter(GC3Denum pname)
     case GraphicsContext3D::RENDERBUFFER_BINDING:
         return m_renderbufferBinding;
     case GraphicsContext3D::RENDERER:
-        return String { "WebKit WebGL"_s };
+        return "WebKit WebGL"_str;
     case GraphicsContext3D::SAMPLE_BUFFERS:
         return getIntParameter(pname);
     case GraphicsContext3D::SAMPLE_COVERAGE_INVERT:
@@ -1936,15 +1936,15 @@ WebGLAny WebGL2RenderingContext::getParameter(GC3Denum pname)
     case GraphicsContext3D::UNPACK_COLORSPACE_CONVERSION_WEBGL:
         return m_unpackColorspaceConversion;
     case GraphicsContext3D::VENDOR:
-        return String { "WebKit" };
+        return "WebKit"_str;
     case GraphicsContext3D::VERSION:
-        return String { "WebGL 2.0" };
+        return "WebGL 2.0"_str;
     case GraphicsContext3D::VIEWPORT:
         return getWebGLIntArrayParameter(pname);
     case WebGLDebugRendererInfo::UNMASKED_RENDERER_WEBGL:
         if (m_webglDebugRendererInfo) {
 #if PLATFORM(IOS_FAMILY)
-            return String { "Apple GPU" };
+            return "Apple GPU"_str;
 #else
             return m_context->getString(GraphicsContext3D::RENDERER);
 #endif
index fbcc663..b7a989a 100644 (file)
@@ -520,7 +520,7 @@ WebGLAny WebGLRenderingContext::getParameter(GC3Denum pname)
     case GraphicsContext3D::RENDERBUFFER_BINDING:
         return m_renderbufferBinding;
     case GraphicsContext3D::RENDERER:
-        return String { "WebKit WebGL"_s };
+        return "WebKit WebGL"_str;
     case GraphicsContext3D::SAMPLE_BUFFERS:
         return getIntParameter(pname);
     case GraphicsContext3D::SAMPLE_COVERAGE_INVERT:
@@ -586,9 +586,9 @@ WebGLAny WebGLRenderingContext::getParameter(GC3Denum pname)
     case GraphicsContext3D::UNPACK_COLORSPACE_CONVERSION_WEBGL:
         return m_unpackColorspaceConversion;
     case GraphicsContext3D::VENDOR:
-        return String { "WebKit" };
+        return "WebKit"_str;
     case GraphicsContext3D::VERSION:
-        return String { "WebGL 1.0" };
+        return "WebGL 1.0"_str;
     case GraphicsContext3D::VIEWPORT:
         return getWebGLIntArrayParameter(pname);
     case Extensions3D::FRAGMENT_SHADER_DERIVATIVE_HINT_OES: // OES_standard_derivatives
@@ -599,7 +599,7 @@ WebGLAny WebGLRenderingContext::getParameter(GC3Denum pname)
     case WebGLDebugRendererInfo::UNMASKED_RENDERER_WEBGL:
         if (m_webglDebugRendererInfo) {
 #if PLATFORM(IOS_FAMILY)
-            return String { "Apple GPU" };
+            return "Apple GPU"_str;
 #else
             return m_context->getString(GraphicsContext3D::RENDERER);
 #endif
index e2546d6..bc0ff64 100644 (file)
@@ -252,7 +252,7 @@ static bool parseParameterValue(CharacterType*& position, CharacterType* const e
     valueEnd = position;
     skipWhile<CharacterType, isSpaceOrTab>(position, end);
     if ((!completeQuotes && valueStart == valueEnd) || (position != end && !isParameterValueEnd(*position))) {
-        value = String("");
+        value = emptyString();
         return false;
     }
     if (hasQuotes)
index be5db4d..62be7b9 100644 (file)
@@ -233,12 +233,12 @@ std::unique_ptr<LinkPreloadResourceClient> LinkLoader::preloadIfNeeded(const Lin
 
     ASSERT(RuntimeEnabledFeatures::sharedFeatures().linkPreloadEnabled());
     if (!href.isValid()) {
-        document.addConsoleMessage(MessageSource::Other, MessageLevel::Error, String("<link rel=preload> has an invalid `href` value"));
+        document.addConsoleMessage(MessageSource::Other, MessageLevel::Error, "<link rel=preload> has an invalid `href` value"_s);
         return nullptr;
     }
     auto type = LinkLoader::resourceTypeFromAsAttribute(as);
     if (!type) {
-        document.addConsoleMessage(MessageSource::Other, MessageLevel::Error, String("<link rel=preload> must have a valid `as` value"));
+        document.addConsoleMessage(MessageSource::Other, MessageLevel::Error, "<link rel=preload> must have a valid `as` value"_s);
         return nullptr;
     }
     if (!MediaQueryEvaluator::mediaAttributeMatches(document, media))
index 5f69c08..0e47f75 100644 (file)
@@ -105,7 +105,7 @@ String NavigatorBase::platform()
     if (!String(WEBCORE_NAVIGATOR_PLATFORM).isEmpty())
         return WEBCORE_NAVIGATOR_PLATFORM;
     struct utsname osname;
-    static NeverDestroyed<String> platformName(uname(&osname) >= 0 ? String(osname.sysname) + String(" ") + String(osname.machine) : emptyString());
+    static NeverDestroyed<String> platformName(uname(&osname) >= 0 ? String(osname.sysname) + " "_str + String(osname.machine) : emptyString());
     return platformName;
 #else
     return WEBCORE_NAVIGATOR_PLATFORM;
index db70788..dd5adcb 100644 (file)
@@ -708,7 +708,7 @@ String DateComponents::toString(SecondFormat format) const
         return String::format("%04d-%02d-%02d", m_year, m_month + 1, m_monthDay);
     case DateTime:
         return String::format("%04d-%02d-%02dT", m_year, m_month + 1, m_monthDay)
-            + toStringForTime(format) + String("Z");
+            + toStringForTime(format) + "Z"_str;
     case DateTimeLocal:
         return String::format("%04d-%02d-%02dT", m_year, m_month + 1, m_monthDay)
             + toStringForTime(format);
@@ -722,7 +722,7 @@ String DateComponents::toString(SecondFormat format) const
         break;
     }
     ASSERT_NOT_REACHED();
-    return String("(Invalid DateComponents)");
+    return "(Invalid DateComponents)"_str;
 }
 
 } // namespace WebCore
index ebad07b..e399b62 100644 (file)
@@ -492,22 +492,22 @@ String keyIdentifierForKeyEvent(NSEvent* event)
         switch ([event keyCode]) {
         case 54: // Right Command
         case 55: // Left Command
-            return String("Meta");
+            return "Meta"_str;
 
         case 57: // Capslock
-            return String("CapsLock");
+            return "CapsLock"_str;
 
         case 56: // Left Shift
         case 60: // Right Shift
-            return String("Shift");
+            return "Shift"_str;
 
         case 58: // Left Alt
         case 61: // Right Alt
-            return String("Alt");
+            return "Alt"_str;
 
         case 59: // Left Ctrl
         case 62: // Right Ctrl
-            return String("Control");
+            return "Control"_str;
 
         default:
             ASSERT_NOT_REACHED();
index da10419..b423796 100644 (file)
@@ -1750,7 +1750,7 @@ String RenderListMarker::suffix() const
     const UChar suffix = listMarkerSuffix(type, m_listItem.value());
 
     if (suffix == ' ')
-        return String(" ");
+        return " "_str;
 
     // If the suffix is not ' ', an extra space is needed
     UChar data[2];
index 9409330..89312de 100644 (file)
@@ -270,7 +270,7 @@ void RenderMenuList::setTextFromOption(int optionIndex)
 
 void RenderMenuList::setText(const String& s)
 {
-    String textToUse = s.isEmpty() ? String("\n"_s) : s;
+    String textToUse = s.isEmpty() ? "\n"_str : s;
 
     if (m_buttonText)
         m_buttonText->setText(textToUse.impl(), true);
index e7ce18b..6f7e519 100644 (file)
@@ -801,9 +801,9 @@ ExceptionOr<String> InternalSettings::userInterfaceDirectionPolicy()
         return Exception { InvalidAccessError };
     switch (settings().userInterfaceDirectionPolicy()) {
     case UserInterfaceDirectionPolicy::Content:
-        return String { "Content"_s };
+        return "Content"_str;
     case UserInterfaceDirectionPolicy::System:
-        return String { "View"_s };
+        return "View"_str;
     }
     ASSERT_NOT_REACHED();
     return Exception { InvalidAccessError };
@@ -830,9 +830,9 @@ ExceptionOr<String> InternalSettings::systemLayoutDirection()
         return Exception { InvalidAccessError };
     switch (settings().systemLayoutDirection()) {
     case TextDirection::LTR:
-        return String { "LTR"_s };
+        return "LTR"_str;
     case TextDirection::RTL:
-        return String { "RTL"_s };
+        return "RTL"_str;
     }
     ASSERT_NOT_REACHED();
     return Exception { InvalidAccessError };
index e73e7a9..70b2ba6 100644 (file)
@@ -1134,14 +1134,14 @@ ExceptionOr<String> Internals::shadowRootType(const Node& root) const
 
     switch (downcast<ShadowRoot>(root).mode()) {
     case ShadowRootMode::UserAgent:
-        return String("UserAgentShadowRoot");
+        return "UserAgentShadowRoot"_str;
     case ShadowRootMode::Closed:
-        return String("ClosedShadowRoot");
+        return "ClosedShadowRoot"_str;
     case ShadowRootMode::Open:
-        return String("OpenShadowRoot");
+        return "OpenShadowRoot"_str;
     default:
         ASSERT_NOT_REACHED();
-        return String("Unknown");
+        return "Unknown"_str;
     }
 }
 
@@ -3187,7 +3187,7 @@ ExceptionOr<String> Internals::getCurrentCursorInfo()
 #endif
     return result.toString();
 #else
-    return String { "FAIL: Cursor details not available on this platform." };
+    return "FAIL: Cursor details not available on this platform."_str;
 #endif
 }
 
index 362f539..22749cd 100644 (file)
@@ -1,3 +1,16 @@
+2018-11-08  Dean Jackson  <dino@apple.com>
+
+        Add a String literal that returns a String
+        https://bugs.webkit.org/show_bug.cgi?id=191425
+        <rdar://problem/45914556>
+
+        Reviewed by Sam Weinig.
+
+        Test _str.
+
+        * TestWebKitAPI/Tests/WTF/WTFString.cpp:
+        (TestWebKitAPI::TEST):
+
 2018-11-08  Jonathan Bedard  <jbedard@apple.com>
 
         webkitpy: Generalize trailing SDK specifier (Part 2)
index e1219d7..a06a671 100644 (file)
@@ -35,11 +35,17 @@ namespace TestWebKitAPI {
 
 TEST(WTF, StringCreationFromLiteral)
 {
-    String stringFromLiteral("Explicit construction syntax"_s);
-    EXPECT_EQ(strlen("Explicit construction syntax"), stringFromLiteral.length());
-    EXPECT_EQ("Explicit construction syntax", stringFromLiteral);
+    String stringFromLiteralViaASCII("Explicit construction syntax"_s);
+    EXPECT_EQ(strlen("Explicit construction syntax"), stringFromLiteralViaASCII.length());
+    EXPECT_EQ("Explicit construction syntax", stringFromLiteralViaASCII);
+    EXPECT_TRUE(stringFromLiteralViaASCII.is8Bit());
+    EXPECT_EQ(String("Explicit construction syntax"), stringFromLiteralViaASCII);
+
+    String stringFromLiteral = "String Literal"_str;
+    EXPECT_EQ(strlen("String Literal"), stringFromLiteral.length());
+    EXPECT_EQ("String Literal", stringFromLiteral);
     EXPECT_TRUE(stringFromLiteral.is8Bit());
-    EXPECT_EQ(String("Explicit construction syntax"), stringFromLiteral);
+    EXPECT_EQ(String("String Literal"), stringFromLiteral);
 
     String stringWithTemplate("Template Literal", String::ConstructFromLiteral);
     EXPECT_EQ(strlen("Template Literal"), stringWithTemplate.length());