StringView could use a function to strip leading/trailing characters without allocation
authorweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 21 Aug 2017 21:10:24 +0000 (21:10 +0000)
committerweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 21 Aug 2017 21:10:24 +0000 (21:10 +0000)
commit36be2bd69d925c3fe15acd45fd004c47c81d6ea8
tree8d5f6612d759916bc03129322f403efa8334a4f9
parent20373de5010e1a7976c6534ed86362a8180b4e88
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().

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@220982 268f45cc-cd09-0410-ab3c-d52691b4dbfc
Source/WTF/ChangeLog
Source/WTF/wtf/text/StringView.h
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WTF/StringView.cpp