Title: [293491] trunk/Source
Revision
293491
Author
cdu...@apple.com
Date
2022-04-26 18:15:17 -0700 (Tue, 26 Apr 2022)

Log Message

Avoid iterating the input string several times setInnerText() / setOuterText()
https://bugs.webkit.org/show_bug.cgi?id=239783

Reviewed by Geoff Garen.

Avoid iterating the input string several times setInnerText() / setOuterText(), to look for new lines
and carriage return characters. Instead of calling `contains('\n')` and then `contains('\r')`, thus iterating
over the string twice, we now call `contains([](UChar c) { return c == '\n' || c == '\r'; })`, achieving
the same thing by traversing the input string only once.

* Source/WTF/wtf/text/WTFString.h:
* Source/WebCore/editing/InsertTextCommand.cpp:
(WebCore::InsertTextCommand::performTrivialReplace):
* Source/WebCore/html/HTMLElement.cpp:
(WebCore::HTMLElement::setInnerText):
(WebCore::HTMLElement::setOuterText):

Canonical link: https://commits.webkit.org/250023@main

Modified Paths

Diff

Modified: trunk/Source/WTF/wtf/text/WTFString.h (293490 => 293491)


--- trunk/Source/WTF/wtf/text/WTFString.h	2022-04-27 01:14:29 UTC (rev 293490)
+++ trunk/Source/WTF/wtf/text/WTFString.h	2022-04-27 01:15:17 UTC (rev 293491)
@@ -177,6 +177,8 @@
     bool contains(UChar character) const { return find(character) != notFound; }
     bool contains(const LChar* string) const { return find(string) != notFound; }
     bool contains(StringView) const;
+    template<typename CodeUnitMatchFunction, std::enable_if_t<std::is_invocable_r_v<bool, CodeUnitMatchFunction, UChar>>* = nullptr>
+    bool contains(CodeUnitMatchFunction matchFunction) const { return find(matchFunction, 0) != notFound; }
     bool containsIgnoringASCIICase(StringView) const;
     bool containsIgnoringASCIICase(StringView, unsigned start) const;
 

Modified: trunk/Source/WebCore/editing/InsertTextCommand.cpp (293490 => 293491)


--- trunk/Source/WebCore/editing/InsertTextCommand.cpp	2022-04-27 01:14:29 UTC (rev 293490)
+++ trunk/Source/WebCore/editing/InsertTextCommand.cpp	2022-04-27 01:15:17 UTC (rev 293491)
@@ -92,7 +92,7 @@
     if (!endingSelection().isRange())
         return false;
 
-    if (text.contains('\t') || text.contains(' ') || text.contains('\n'))
+    if (text.contains([](UChar c) { return c == '\t' || c == ' ' || c == '\n'; }))
         return false;
 
     Position start = endingSelection().start();

Modified: trunk/Source/WebCore/html/HTMLElement.cpp (293490 => 293491)


--- trunk/Source/WebCore/html/HTMLElement.cpp	2022-04-27 01:14:29 UTC (rev 293490)
+++ trunk/Source/WebCore/html/HTMLElement.cpp	2022-04-27 01:15:17 UTC (rev 293491)
@@ -439,7 +439,7 @@
 {
     // FIXME: This doesn't take whitespace collapsing into account at all.
 
-    if (!text.contains('\n') && !text.contains('\r')) {
+    if (!text.contains([](UChar c) { return c == '\n' || c == '\r'; })) {
         stringReplaceAll(WTFMove(text));
         return { };
     }
@@ -473,7 +473,7 @@
     RefPtr<Node> newChild;
 
     // Convert text to fragment with <br> tags instead of linebreaks if needed.
-    if (text.contains('\r') || text.contains('\n'))
+    if (text.contains([](UChar c) { return c == '\n' || c == '\r'; }))
         newChild = textToFragment(document(), WTFMove(text));
     else
         newChild = Text::create(document(), WTFMove(text));
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to