Add support for null StringViews
authorweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 12 Jan 2014 01:54:22 +0000 (01:54 +0000)
committerweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 12 Jan 2014 01:54:22 +0000 (01:54 +0000)
https://bugs.webkit.org/show_bug.cgi?id=126827

Reviewed by Anders Carlsson.

Source/WTF:

- Do some style cleanup while in the neighborhood.

* wtf/text/StringView.h:
(WTF::StringView::StringView):
Add default constructor to create a null StringView.

(WTF::StringView::isNull):
Add isNull(), matching the semantics of String.

(WTF::StringView::operator bool):
Add an explicit operator bool() to allow use in if-statements.

Tools:

Add new test for StringView.

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WTF/StringView.cpp: Added.
(TestWebKitAPI::TEST):

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

Source/WTF/ChangeLog
Source/WTF/wtf/text/StringView.h
Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/WTF/StringView.cpp [new file with mode: 0644]

index 4ce951e..2045038 100644 (file)
@@ -1,5 +1,24 @@
 2014-01-11  Sam Weinig  <sam@webkit.org>
 
+        Add support for null StringViews
+        https://bugs.webkit.org/show_bug.cgi?id=126827
+
+        Reviewed by Anders Carlsson.
+
+        - Do some style cleanup while in the neighborhood.
+
+        * wtf/text/StringView.h:
+        (WTF::StringView::StringView):
+        Add default constructor to create a null StringView.
+
+        (WTF::StringView::isNull):
+        Add isNull(), matching the semantics of String.
+
+        (WTF::StringView::operator bool):
+        Add an explicit operator bool() to allow use in if-statements. 
+
+2014-01-11  Sam Weinig  <sam@webkit.org>
+
         Clean up iterator filter / iterator range code
         https://bugs.webkit.org/show_bug.cgi?id=126817
 
index 4c854e1..70f4eb0 100644 (file)
@@ -37,12 +37,18 @@ namespace WTF {
 
 class StringView {
 public:
-    StringView(const LChar *characters, unsigned length)
+    StringView()
+        : m_characters(nullptr)
+        , m_length(0)
+    {
+    }
+
+    StringView(const LChar* characters, unsigned length)
     {
         initialize(characters, length);
     }
 
-    StringView(const UChar *characters, unsigned length)
+    StringView(const UCharcharacters, unsigned length)
     {
         initialize(characters, length);
     }
@@ -79,9 +85,12 @@ public:
         return static_cast<const UChar*>(m_characters);
     }
 
+    bool isNull() const { return !m_characters; }
     bool isEmpty() const { return !length(); }
     unsigned length() const { return m_length & ~is16BitStringFlag; }
 
+    explicit operator bool() const { return !isNull(); }
+
     bool is8Bit() const { return !(m_length & is16BitStringFlag); }
 
     StringView substring(unsigned start, unsigned length = std::numeric_limits<unsigned>::max()) const
@@ -119,7 +128,7 @@ public:
     }
 
 private:
-    void initialize(const LChar *characters, unsigned length)
+    void initialize(const LCharcharacters, unsigned length)
     {
         ASSERT(!(length & is16BitStringFlag));
         
@@ -127,7 +136,7 @@ private:
         m_length = length;
     }
 
-    void initialize(const UChar *characters, unsigned length)
+    void initialize(const UCharcharacters, unsigned length)
     {
         ASSERT(!(length & is16BitStringFlag));
         
index 92cb94e..54cb0c3 100644 (file)
@@ -1,3 +1,16 @@
+2014-01-11  Sam Weinig  <sam@webkit.org>
+
+        Add support for null StringViews
+        https://bugs.webkit.org/show_bug.cgi?id=126827
+
+        Reviewed by Anders Carlsson.
+
+        Add new test for StringView.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WTF/StringView.cpp: Added.
+        (TestWebKitAPI::TEST):
+
 2014-01-10  Benjamin Poulain  <bpoulain@apple.com>
 
         Remove the BlackBerry port from trunk
index fcc477d..1e1e5f6 100644 (file)
                76E182DA1547550100F1FADD /* WillSendSubmitEvent.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76E182D91547550100F1FADD /* WillSendSubmitEvent.cpp */; };
                76E182DD1547569100F1FADD /* WillSendSubmitEvent_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76E182DC1547569100F1FADD /* WillSendSubmitEvent_Bundle.cpp */; };
                76E182DF154767E600F1FADD /* auto-submitting-form.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 76E182DE15475A8300F1FADD /* auto-submitting-form.html */; };
+               7C74D42F188228F300E5ED57 /* StringView.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7C74D42D188228F300E5ED57 /* StringView.cpp */; };
                7C8DDAAB1735DEEE00EA5AC0 /* CloseThenTerminate.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7C8DDAA91735DE1D00EA5AC0 /* CloseThenTerminate.cpp */; };
                7C9ED98B17A19F4B00E4DC33 /* attributedStringStrikethrough.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 7C9ED98A17A19D0600E4DC33 /* attributedStringStrikethrough.html */; };
                7CFBCADF1743234F00B2BFCF /* WillLoad.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CFBCADD1743234F00B2BFCF /* WillLoad.cpp */; };
                76E182D91547550100F1FADD /* WillSendSubmitEvent.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WillSendSubmitEvent.cpp; sourceTree = "<group>"; };
                76E182DC1547569100F1FADD /* WillSendSubmitEvent_Bundle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WillSendSubmitEvent_Bundle.cpp; sourceTree = "<group>"; };
                76E182DE15475A8300F1FADD /* auto-submitting-form.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "auto-submitting-form.html"; sourceTree = "<group>"; };
+               7C74D42D188228F300E5ED57 /* StringView.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StringView.cpp; sourceTree = "<group>"; };
                7C8DDAA91735DE1D00EA5AC0 /* CloseThenTerminate.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = CloseThenTerminate.cpp; sourceTree = "<group>"; };
                7C9ED98A17A19D0600E4DC33 /* attributedStringStrikethrough.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = attributedStringStrikethrough.html; sourceTree = "<group>"; };
                7CFBCADD1743234F00B2BFCF /* WillLoad.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WillLoad.cpp; sourceTree = "<group>"; };
                                93ABA80816DDAB91002DB2FA /* StringHasher.cpp */,
                                26F1B44315CA434F00D1E4BF /* StringImpl.cpp */,
                                C01363C713C3997300EF3964 /* StringOperators.cpp */,
+                               7C74D42D188228F300E5ED57 /* StringView.cpp */,
                                0BCD85691485C98B00EA2003 /* TemporaryChange.cpp */,
                                BC55F5F814AD78EE00484BE1 /* Vector.cpp */,
                                265AF54F15D1E48A00B0CB4A /* WTFString.cpp */,
                                1A02C84F125D4A8400E3F4BD /* Find.cpp in Sources */,
                                C51AFB99169F49FF009CCF66 /* FindMatches.mm in Sources */,
                                1ADBEFAE130C689C00D61D19 /* ForceRepaint.cpp in Sources */,
+                               7C74D42F188228F300E5ED57 /* StringView.cpp in Sources */,
                                BCBD3710125AA2EB00D2C29F /* FrameMIMETypeHTML.cpp in Sources */,
                                BCBD3761125ABCFE00D2C29F /* FrameMIMETypePNG.cpp in Sources */,
                                CE32C7C818184C4900CD8C28 /* WillPerformClientRedirectToURLCrash.mm in Sources */,
diff --git a/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp b/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp
new file mode 100644 (file)
index 0000000..38bd278
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2014 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. AND ITS CONTRIBUTORS ``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 ITS 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"
+
+#import <wtf/text/StringView.h>
+
+namespace TestWebKitAPI {
+
+TEST(WTF, StringViewEmptyVsNull)
+{
+    StringView nullView;
+    EXPECT_TRUE(nullView.isNull());
+    EXPECT_TRUE(nullView.isEmpty());
+
+    // Test in a boolean context to test operator bool().
+    if (nullView)
+        FAIL();
+    else
+        SUCCEED();
+
+    if (!nullView)
+        SUCCEED();
+    else
+        FAIL();
+
+    StringView emptyView = StringView::empty();
+    EXPECT_FALSE(emptyView.isNull());
+    EXPECT_TRUE(emptyView.isEmpty());
+
+    // Test in a boolean context to test operator bool().
+    if (emptyView)
+        SUCCEED();
+    else
+        FAIL();
+
+    if (!emptyView)
+        FAIL();
+    else
+        SUCCEED();
+
+    StringView viewWithCharacters(String("hello"));
+    EXPECT_FALSE(viewWithCharacters.isNull());
+    EXPECT_FALSE(viewWithCharacters.isEmpty());
+
+    // Test in a boolean context to test operator bool().
+    if (viewWithCharacters)
+        SUCCEED();
+    else
+        FAIL();
+
+    if (!viewWithCharacters)
+        FAIL();
+    else
+        SUCCEED();
+}
+
+} // namespace TestWebKitAPI