Title: [221250] releases/WebKitGTK/webkit-2.18
Revision
221250
Author
[email protected]
Date
2017-08-28 06:22:47 -0700 (Mon, 28 Aug 2017)

Log Message

Merge r220982 - StringView could use a function to strip leading/trailing characters without allocation
https://bugs.webkit.org/show_bug.cgi?id=175757

Reviewed by Darin Adler.

Source/WTF:

There are many places in WebCore/WebKit that we call functions like,
WebCore::stripLeadingAndTrailingHTMLSpaces,  or String::stripWhiteSpace() only to use
the allocated String as a temporary for either another transformation or a comparison.
Now that we have StringView, we can avoid that extra allocation, by having returning a
StringView  substring in these scenarios.

For instance, the check (from ScriptElement.cpp:287):

if (!stripLeadingAndTrailingHTMLSpaces(sourceURL).isEmpty()) {
    ...
}

currently allocates a string just to make this check. With a new
stripLeadingAndTrailingHTMLSpaces such as:

StringView stripLeadingAndTrailingHTMLSpaces(StringView stringView)
{
    return stringView.stripLeadingAndTrailingMatchedCharacters([] (auto c) {
        return isHTMLSpace(c);
    });
}

We could instead have exact same code from ScriptElement.cpp now avoid an allocation.

* wtf/text/StringView.h:
(WTF::StringView::stripLeadingAndTrailingMatchedCharacters):

Tools:

* TestWebKitAPI/Tests/WTF/StringView.cpp:
Add tests for StringView::stripLeadingAndTrailingMatchedCharacters().

Modified Paths

Diff

Modified: releases/WebKitGTK/webkit-2.18/Source/WTF/ChangeLog (221249 => 221250)


--- releases/WebKitGTK/webkit-2.18/Source/WTF/ChangeLog	2017-08-28 12:46:19 UTC (rev 221249)
+++ releases/WebKitGTK/webkit-2.18/Source/WTF/ChangeLog	2017-08-28 13:22:47 UTC (rev 221250)
@@ -1,3 +1,37 @@
+2017-08-20  Sam Weinig  <[email protected]>
+
+        StringView could use a function to strip leading/trailing characters without allocation
+        https://bugs.webkit.org/show_bug.cgi?id=175757
+
+        Reviewed by Darin Adler.
+
+        There are many places in WebCore/WebKit that we call functions like, 
+        WebCore::stripLeadingAndTrailingHTMLSpaces,  or String::stripWhiteSpace() only to use 
+        the allocated String as a temporary for either another transformation or a comparison.
+        Now that we have StringView, we can avoid that extra allocation, by having returning a
+        StringView  substring in these scenarios.
+
+        For instance, the check (from ScriptElement.cpp:287):
+
+        if (!stripLeadingAndTrailingHTMLSpaces(sourceURL).isEmpty()) {
+            ...
+        }
+
+        currently allocates a string just to make this check. With a new 
+        stripLeadingAndTrailingHTMLSpaces such as:
+
+        StringView stripLeadingAndTrailingHTMLSpaces(StringView stringView)
+        {
+            return stringView.stripLeadingAndTrailingMatchedCharacters([] (auto c) {
+                return isHTMLSpace(c);
+            });
+        }
+
+        We could instead have exact same code from ScriptElement.cpp now avoid an allocation.
+
+        * wtf/text/StringView.h:
+        (WTF::StringView::stripLeadingAndTrailingMatchedCharacters):
+
 2017-08-14  Simon Fraser  <[email protected]>
 
         Remove Proximity Events and related code

Modified: releases/WebKitGTK/webkit-2.18/Source/WTF/wtf/text/StringView.h (221249 => 221250)


--- releases/WebKitGTK/webkit-2.18/Source/WTF/wtf/text/StringView.h	2017-08-28 12:46:19 UTC (rev 221249)
+++ releases/WebKitGTK/webkit-2.18/Source/WTF/wtf/text/StringView.h	2017-08-28 13:22:47 UTC (rev 221250)
@@ -119,6 +119,9 @@
     StringView left(unsigned len) const { return substring(0, len); }
     StringView right(unsigned len) const { return substring(length() - len, len); }
 
+    template<typename MatchedCharacterPredicate>
+    StringView stripLeadingAndTrailingMatchedCharacters(const MatchedCharacterPredicate&);
+
     class SplitResult;
     SplitResult split(UChar) const;
 
@@ -157,6 +160,9 @@
     void initialize(const LChar*, unsigned length);
     void initialize(const UChar*, unsigned length);
 
+    template<typename CharacterType, typename MatchedCharacterPredicate>
+    StringView stripLeadingAndTrailingMatchedCharacters(const CharacterType*, const MatchedCharacterPredicate&);
+
 #if CHECK_STRINGVIEW_LIFETIME
     WTF_EXPORT_STRING_API bool underlyingStringIsValid() const;
     WTF_EXPORT_STRING_API void setUnderlyingString(const StringImpl*);
@@ -935,6 +941,40 @@
     return !(*this == other);
 }
 
+template<typename CharacterType, typename MatchedCharacterPredicate>
+inline StringView StringView::stripLeadingAndTrailingMatchedCharacters(const CharacterType* characters, const MatchedCharacterPredicate& predicate)
+{
+    if (!m_length)
+        return *this;
+
+    unsigned start = 0;
+    unsigned end = m_length - 1;
+    
+    while (start <= end && predicate(characters[start]))
+        ++start;
+    
+    if (start > end)
+        return StringView::empty();
+
+    while (end && predicate(characters[end]))
+        --end;
+
+    if (!start && end == m_length - 1)
+        return *this;
+
+    StringView result(characters + start, end + 1 - start);
+    result.setUnderlyingString(*this);
+    return result;
+}
+
+template<typename MatchedCharacterPredicate>
+StringView StringView::stripLeadingAndTrailingMatchedCharacters(const MatchedCharacterPredicate& predicate)
+{
+    if (is8Bit())
+        return stripLeadingAndTrailingMatchedCharacters<LChar>(characters8(), predicate);
+    return stripLeadingAndTrailingMatchedCharacters<UChar>(characters16(), predicate);
+}
+
 template<unsigned length> inline bool equalLettersIgnoringASCIICase(StringView string, const char (&lowercaseLetters)[length])
 {
     return equalLettersIgnoringASCIICaseCommon(string, lowercaseLetters);

Modified: releases/WebKitGTK/webkit-2.18/Tools/ChangeLog (221249 => 221250)


--- releases/WebKitGTK/webkit-2.18/Tools/ChangeLog	2017-08-28 12:46:19 UTC (rev 221249)
+++ releases/WebKitGTK/webkit-2.18/Tools/ChangeLog	2017-08-28 13:22:47 UTC (rev 221250)
@@ -1,3 +1,13 @@
+2017-08-20  Sam Weinig  <[email protected]>
+
+        StringView could use a function to strip leading/trailing characters without allocation
+        https://bugs.webkit.org/show_bug.cgi?id=175757
+
+        Reviewed by Darin Adler.
+
+        * TestWebKitAPI/Tests/WTF/StringView.cpp:
+        Add tests for StringView::stripLeadingAndTrailingMatchedCharacters().
+
 2017-08-17  Michael Catanzaro  <[email protected]>
 
         [GTK] Make TestContextMenu work on Fedora

Modified: releases/WebKitGTK/webkit-2.18/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp (221249 => 221250)


--- releases/WebKitGTK/webkit-2.18/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp	2017-08-28 12:46:19 UTC (rev 221249)
+++ releases/WebKitGTK/webkit-2.18/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp	2017-08-28 13:22:47 UTC (rev 221250)
@@ -925,4 +925,26 @@
     EXPECT_EQ(reference.reverseFind('c', 4), notFound);
 }
 
+TEST(WTF, StringViewStripLeadingAndTrailingMatchedCharacters)
+{
+    auto isA = [] (UChar c) { 
+        return c == 'A';
+    };
+
+    EXPECT_TRUE(stringViewFromLiteral("AAABBBAAA").stripLeadingAndTrailingMatchedCharacters(isA) == stringViewFromLiteral("BBB"));
+    EXPECT_TRUE(stringViewFromLiteral("AAABBBCCC").stripLeadingAndTrailingMatchedCharacters(isA) == stringViewFromLiteral("BBBCCC"));
+    EXPECT_TRUE(stringViewFromLiteral("CCCBBBAAA").stripLeadingAndTrailingMatchedCharacters(isA) == stringViewFromLiteral("CCCBBB"));
+    EXPECT_TRUE(stringViewFromLiteral("CCCBBBCCC").stripLeadingAndTrailingMatchedCharacters(isA) == stringViewFromLiteral("CCCBBBCCC"));
+    EXPECT_TRUE(stringViewFromLiteral("AAAAAACCC").stripLeadingAndTrailingMatchedCharacters(isA) == stringViewFromLiteral("CCC"));
+    EXPECT_TRUE(stringViewFromLiteral("BBBAAAAAA").stripLeadingAndTrailingMatchedCharacters(isA) == stringViewFromLiteral("BBB"));
+    EXPECT_TRUE(stringViewFromLiteral("CCCAAABBB").stripLeadingAndTrailingMatchedCharacters(isA) == stringViewFromLiteral("CCCAAABBB"));
+    EXPECT_TRUE(stringViewFromLiteral("AAAAAAAAA").stripLeadingAndTrailingMatchedCharacters(isA) == StringView::empty());
+
+    StringView emptyView = StringView::empty();
+    EXPECT_TRUE(emptyView.stripLeadingAndTrailingMatchedCharacters(isA) == emptyView);
+
+    StringView nullView;
+    EXPECT_TRUE(nullView.stripLeadingAndTrailingMatchedCharacters(isA) == nullView);
+}
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to