Title: [210078] trunk/Source/WebCore
Revision
210078
Author
[email protected]
Date
2016-12-21 14:11:39 -0800 (Wed, 21 Dec 2016)

Log Message

Modernize findPlainText
https://bugs.webkit.org/show_bug.cgi?id=166299

Reviewed by Sam Weinig.

Modernized findPlainText by merging the static version of it into it
and extracting the main nested loop out as findPlainTextOffset.

No new tests since there should be no behavioral change.

* editing/TextIterator.cpp:
(WebCore::findPlainTextOffset):
(WebCore::findPlainText):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (210077 => 210078)


--- trunk/Source/WebCore/ChangeLog	2016-12-21 22:06:22 UTC (rev 210077)
+++ trunk/Source/WebCore/ChangeLog	2016-12-21 22:11:39 UTC (rev 210078)
@@ -1,3 +1,19 @@
+2016-12-21  Ryosuke Niwa  <[email protected]>
+
+        Modernize findPlainText
+        https://bugs.webkit.org/show_bug.cgi?id=166299
+
+        Reviewed by Sam Weinig.
+
+        Modernized findPlainText by merging the static version of it into it
+        and extracting the main nested loop out as findPlainTextOffset.
+
+        No new tests since there should be no behavioral change.
+
+        * editing/TextIterator.cpp:
+        (WebCore::findPlainTextOffset):
+        (WebCore::findPlainText):
+
 2016-12-21  John Wilander  <[email protected]>
 
         Switch to a blacklist model for restricted Accept headers in simple CORS requests

Modified: trunk/Source/WebCore/editing/TextIterator.cpp (210077 => 210078)


--- trunk/Source/WebCore/editing/TextIterator.cpp	2016-12-21 22:06:22 UTC (rev 210077)
+++ trunk/Source/WebCore/editing/TextIterator.cpp	2016-12-21 22:11:39 UTC (rev 210078)
@@ -2608,11 +2608,39 @@
     return result;
 }
 
-static size_t findPlainText(const Range& range, const String& target, FindOptions options, size_t& matchStart)
+static std::optional<std::pair<size_t, size_t>> findPlainTextOffset(SearchBuffer& buffer, CharacterIterator& findIterator, bool searchForward)
 {
-    matchStart = 0;
+    size_t matchStart = 0;
     size_t matchLength = 0;
+    while (!findIterator.atEnd()) {
+        findIterator.advance(buffer.append(findIterator.text()));
+        while (1) {
+            size_t matchStartOffset;
+            size_t newMatchLength = buffer.search(matchStartOffset);
+            if (!newMatchLength) {
+                if (findIterator.atBreak() && !buffer.atBreak()) {
+                    buffer.reachedBreak();
+                    continue;
+                }
+                break;
+            }
+            size_t lastCharacterInBufferOffset = findIterator.characterOffset();
+            ASSERT(lastCharacterInBufferOffset >= matchStartOffset);
+            matchStart = lastCharacterInBufferOffset - matchStartOffset;
+            matchLength = newMatchLength;
+            if (searchForward) // Look for the last match when searching backwards instead.
+                return std::pair<size_t, size_t> { matchStart, matchLength };
+        }
+    }
 
+    if (!matchLength)
+        return std::nullopt;
+
+    return std::pair<size_t, size_t> { matchStart, matchLength };
+}
+
+Ref<Range> findPlainText(const Range& range, const String& target, FindOptions options)
+{
     SearchBuffer buffer(target, options);
 
     if (buffer.needsMoreContext()) {
@@ -2625,47 +2653,16 @@
         }
     }
 
-    CharacterIterator findIterator(range, TextIteratorEntersTextControls | TextIteratorClipsToFrameAncestors);
+    bool searchForward = !(options & Backwards);
+    TextIteratorBehavior iteratorOptions = TextIteratorEntersTextControls | TextIteratorClipsToFrameAncestors;
 
-    while (!findIterator.atEnd()) {
-        findIterator.advance(buffer.append(findIterator.text()));
-tryAgain:
-        size_t matchStartOffset;
-        if (size_t newMatchLength = buffer.search(matchStartOffset)) {
-            // Note that we found a match, and where we found it.
-            size_t lastCharacterInBufferOffset = findIterator.characterOffset();
-            ASSERT(lastCharacterInBufferOffset >= matchStartOffset);
-            matchStart = lastCharacterInBufferOffset - matchStartOffset;
-            matchLength = newMatchLength;
-            // If searching forward, stop on the first match.
-            // If searching backward, don't stop, so we end up with the last match.
-            if (!(options & Backwards))
-                break;
-            goto tryAgain;
-        }
-        if (findIterator.atBreak() && !buffer.atBreak()) {
-            buffer.reachedBreak();
-            goto tryAgain;
-        }
-    }
+    CharacterIterator findIterator(range, iteratorOptions);
+    auto result = findPlainTextOffset(buffer, findIterator, searchForward);
+    if (!result)
+        return collapsedToBoundary(range, searchForward);
 
-    return matchLength;
+    CharacterIterator rangeComputeIterator(range, iteratorOptions);
+    return characterSubrange(range.ownerDocument(), rangeComputeIterator, result->first, result->second);
 }
 
-Ref<Range> findPlainText(const Range& range, const String& target, FindOptions options)
-{
-    // First, find the text.
-    size_t matchStart;
-    size_t matchLength;
-    {
-        matchLength = findPlainText(range, target, options, matchStart);
-        if (!matchLength)
-            return collapsedToBoundary(range, !(options & Backwards));
-    }
-
-    // Then, find the document position of the start and the end of the text.
-    CharacterIterator computeRangeIterator(range, TextIteratorEntersTextControls | TextIteratorClipsToFrameAncestors);
-    return characterSubrange(range.ownerDocument(), computeRangeIterator, matchStart, matchLength);
 }
-
-}
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to