Title: [161785] trunk
Revision
161785
Author
wei...@apple.com
Date
2014-01-11 17:54:22 -0800 (Sat, 11 Jan 2014)

Log Message

Add support for null StringViews
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):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (161784 => 161785)


--- trunk/Source/WTF/ChangeLog	2014-01-12 00:54:27 UTC (rev 161784)
+++ trunk/Source/WTF/ChangeLog	2014-01-12 01:54:22 UTC (rev 161785)
@@ -1,5 +1,24 @@
 2014-01-11  Sam Weinig  <s...@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  <s...@webkit.org>
+
         Clean up iterator filter / iterator range code
         https://bugs.webkit.org/show_bug.cgi?id=126817
 

Modified: trunk/Source/WTF/wtf/text/StringView.h (161784 => 161785)


--- trunk/Source/WTF/wtf/text/StringView.h	2014-01-12 00:54:27 UTC (rev 161784)
+++ trunk/Source/WTF/wtf/text/StringView.h	2014-01-12 01:54:22 UTC (rev 161785)
@@ -37,12 +37,18 @@
 
 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 UChar* characters, unsigned length)
     {
         initialize(characters, length);
     }
@@ -79,9 +85,12 @@
         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 @@
     }
 
 private:
-    void initialize(const LChar *characters, unsigned length)
+    void initialize(const LChar* characters, unsigned length)
     {
         ASSERT(!(length & is16BitStringFlag));
         
@@ -127,7 +136,7 @@
         m_length = length;
     }
 
-    void initialize(const UChar *characters, unsigned length)
+    void initialize(const UChar* characters, unsigned length)
     {
         ASSERT(!(length & is16BitStringFlag));
         

Modified: trunk/Tools/ChangeLog (161784 => 161785)


--- trunk/Tools/ChangeLog	2014-01-12 00:54:27 UTC (rev 161784)
+++ trunk/Tools/ChangeLog	2014-01-12 01:54:22 UTC (rev 161785)
@@ -1,3 +1,16 @@
+2014-01-11  Sam Weinig  <s...@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  <bpoul...@apple.com>
 
         Remove the BlackBerry port from trunk

Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (161784 => 161785)


--- trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2014-01-12 00:54:27 UTC (rev 161784)
+++ trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2014-01-12 01:54:22 UTC (rev 161785)
@@ -109,6 +109,7 @@
 		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 */; };
@@ -432,6 +433,7 @@
 		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>"; };
@@ -848,6 +850,7 @@
 				93ABA80816DDAB91002DB2FA /* StringHasher.cpp */,
 				26F1B44315CA434F00D1E4BF /* StringImpl.cpp */,
 				C01363C713C3997300EF3964 /* StringOperators.cpp */,
+				7C74D42D188228F300E5ED57 /* StringView.cpp */,
 				0BCD85691485C98B00EA2003 /* TemporaryChange.cpp */,
 				BC55F5F814AD78EE00484BE1 /* Vector.cpp */,
 				265AF54F15D1E48A00B0CB4A /* WTFString.cpp */,
@@ -1162,6 +1165,7 @@
 				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 */,

Added: trunk/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp (0 => 161785)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp	2014-01-12 01:54:22 UTC (rev 161785)
@@ -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
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to