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