Title: [164845] trunk
Revision
164845
Author
[email protected]
Date
2014-02-27 17:17:37 -0800 (Thu, 27 Feb 2014)

Log Message

Improve how ContentSearchUtilities::lineEndings works by supporting the three common line endings.

https://bugs.webkit.org/show_bug.cgi?id=129458

Reviewed by Joseph Pecoraro.

Source/_javascript_Core:

* inspector/ContentSearchUtilities.cpp:
(Inspector::ContentSearchUtilities::textPositionFromOffset): Remove assumption about line ending length.
(Inspector::ContentSearchUtilities::getRegularExpressionMatchesByLines): Remove assumption about
line ending type and don't try to strip the line ending. Use size_t
(Inspector::ContentSearchUtilities::lineEndings): Use findNextLineStart to find the lines.
This will include the line ending in the lines, but that is okay.
(Inspector::ContentSearchUtilities::buildObjectForSearchMatch): Use size_t.
(Inspector::ContentSearchUtilities::searchInTextByLines): Modernize.

LayoutTests:

* inspector-protocol/debugger/resources/mac-linebreaks.js: Added.
* inspector-protocol/debugger/resources/mixed-linebreaks.js: Added.
* inspector-protocol/debugger/resources/unix-linebreaks.js: Added.
* inspector-protocol/debugger/resources/windows-linebreaks.js: Added.
* inspector-protocol/debugger/searchInContent-linebreaks-expected.txt: Added.
* inspector-protocol/debugger/searchInContent-linebreaks.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (164844 => 164845)


--- trunk/LayoutTests/ChangeLog	2014-02-28 01:03:09 UTC (rev 164844)
+++ trunk/LayoutTests/ChangeLog	2014-02-28 01:17:37 UTC (rev 164845)
@@ -1,3 +1,18 @@
+2014-02-27  Timothy Hatcher  <[email protected]>
+
+        Improve how ContentSearchUtilities::lineEndings works by supporting the three common line endings.
+
+        https://bugs.webkit.org/show_bug.cgi?id=129458
+
+        Reviewed by Joseph Pecoraro.
+
+        * inspector-protocol/debugger/resources/mac-linebreaks.js: Added.
+        * inspector-protocol/debugger/resources/mixed-linebreaks.js: Added.
+        * inspector-protocol/debugger/resources/unix-linebreaks.js: Added.
+        * inspector-protocol/debugger/resources/windows-linebreaks.js: Added.
+        * inspector-protocol/debugger/searchInContent-linebreaks-expected.txt: Added.
+        * inspector-protocol/debugger/searchInContent-linebreaks.html: Added.
+
 2014-02-27  Brian Burg  <[email protected]>
 
         Unreviewed, update test expectations after r164830.

Added: trunk/LayoutTests/inspector-protocol/debugger/resources/mac-linebreaks.js (0 => 164845)


--- trunk/LayoutTests/inspector-protocol/debugger/resources/mac-linebreaks.js	                        (rev 0)
+++ trunk/LayoutTests/inspector-protocol/debugger/resources/mac-linebreaks.js	2014-02-28 01:17:37 UTC (rev 164845)
@@ -0,0 +1,10 @@
+// test one
+// 
+//  test two
+
+function boo()
+{
+    //   test three test four
+}
+
+// test no newline
\ No newline at end of file

Added: trunk/LayoutTests/inspector-protocol/debugger/resources/mixed-linebreaks.js (0 => 164845)


--- trunk/LayoutTests/inspector-protocol/debugger/resources/mixed-linebreaks.js	                        (rev 0)
+++ trunk/LayoutTests/inspector-protocol/debugger/resources/mixed-linebreaks.js	2014-02-28 01:17:37 UTC (rev 164845)
@@ -0,0 +1,10 @@
+// test one
+
+// test two
+
+function baz()
+{
+    //   test three test four
+}
+
+// test no newline
\ No newline at end of file

Added: trunk/LayoutTests/inspector-protocol/debugger/resources/unix-linebreaks.js (0 => 164845)


--- trunk/LayoutTests/inspector-protocol/debugger/resources/unix-linebreaks.js	                        (rev 0)
+++ trunk/LayoutTests/inspector-protocol/debugger/resources/unix-linebreaks.js	2014-02-28 01:17:37 UTC (rev 164845)
@@ -0,0 +1,10 @@
+// test one
+// 
+//  test two
+
+function bar()
+{
+    // test three test four
+}
+
+// test no newline
\ No newline at end of file

Added: trunk/LayoutTests/inspector-protocol/debugger/resources/windows-linebreaks.js (0 => 164845)


--- trunk/LayoutTests/inspector-protocol/debugger/resources/windows-linebreaks.js	                        (rev 0)
+++ trunk/LayoutTests/inspector-protocol/debugger/resources/windows-linebreaks.js	2014-02-28 01:17:37 UTC (rev 164845)
@@ -0,0 +1,10 @@
+// test one
+// 
+//  test two
+
+function foo()
+{
+    // test three test four
+}
+
+// test no newline
\ No newline at end of file

Added: trunk/LayoutTests/inspector-protocol/debugger/searchInContent-linebreaks-expected.txt (0 => 164845)


--- trunk/LayoutTests/inspector-protocol/debugger/searchInContent-linebreaks-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/inspector-protocol/debugger/searchInContent-linebreaks-expected.txt	2014-02-28 01:17:37 UTC (rev 164845)
@@ -0,0 +1,32 @@
+Verify that Debugger.searchInContent works with scripts that have varying line endings.
+Scripts searched: 4
+
+Script: mac-linebreaks.js
+Results found: 4
+Line 0: "// test one\r"
+Line 2: "//  test two\r"
+Line 6: "    //   test three test four\r"
+Line 9: "// test no newline"
+
+Script: mixed-linebreaks.js
+Results found: 4
+Line 0: "// test one\n"
+Line 2: "// test two\r"
+Line 6: "    //   test three test four\r\n"
+Line 9: "// test no newline"
+
+Script: unix-linebreaks.js
+Results found: 4
+Line 0: "// test one\n"
+Line 2: "//  test two\n"
+Line 6: "    // test three test four\n"
+Line 9: "// test no newline"
+
+Script: windows-linebreaks.js
+Results found: 4
+Line 0: "// test one\r\n"
+Line 2: "//  test two\r\n"
+Line 6: "    // test three test four\r\n"
+Line 9: "// test no newline"
+
+

Added: trunk/LayoutTests/inspector-protocol/debugger/searchInContent-linebreaks.html (0 => 164845)


--- trunk/LayoutTests/inspector-protocol/debugger/searchInContent-linebreaks.html	                        (rev 0)
+++ trunk/LayoutTests/inspector-protocol/debugger/searchInContent-linebreaks.html	2014-02-28 01:17:37 UTC (rev 164845)
@@ -0,0 +1,67 @@
+<html>
+<head>
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script>
+function test()
+{
+    const expectedSearchCount = 4;
+    var completedSearchCount = 0;
+
+    var testResults = [];
+
+    InspectorTest.sendCommand("Debugger.enable", {});
+
+    InspectorTest.eventHandler["Debugger.scriptParsed"] = function(messageObject)
+    {
+        if (/-linebreaks\.js$/.test(messageObject.params.url)) {
+            InspectorTest.sendCommand("Debugger.searchInContent", {"scriptId": messageObject.params.scriptId, "query": "test"}, function(url, messageObject) {
+                var results = messageObject.result.result;
+
+                var testResult = {url: url, count: results.length, matches: []};
+                for (var result of results)
+                    testResult.matches.push(result);
+
+                testResults.push(testResult);
+
+                if (++completedSearchCount >= expectedSearchCount)
+                    outputResults();
+            }.bind(null, messageObject.params.url));
+        }
+    }
+
+    function outputResults()
+    {
+        function compareResultURLs(a, b)
+        {
+            return a.url.localeCompare(b.url);
+        }
+
+        var sortedTestResults = testResults.sort(compareResultURLs);
+
+        InspectorTest.log("Scripts searched: " + sortedTestResults.length);
+        InspectorTest.log("");
+
+        for (var testResult of sortedTestResults) {
+            var fileName = /([\w-]+\.js)$/.exec(testResult.url)[1];
+            InspectorTest.log("Script: " + fileName);
+            InspectorTest.log("Results found: " + testResult.count);
+    
+            for (var match of testResult.matches)
+                InspectorTest.log("Line " + match.lineNumber + ": " + JSON.stringify(match.lineContent));    
+
+            InspectorTest.log("");
+        }
+
+        InspectorTest.completeTest();
+    }
+}
+</script>
+</head>
+<body _onload_="runTest()">
+Verify that Debugger.searchInContent works with scripts that have varying line endings.
+</body>
+</html>

Modified: trunk/Source/_javascript_Core/ChangeLog (164844 => 164845)


--- trunk/Source/_javascript_Core/ChangeLog	2014-02-28 01:03:09 UTC (rev 164844)
+++ trunk/Source/_javascript_Core/ChangeLog	2014-02-28 01:17:37 UTC (rev 164845)
@@ -1,3 +1,20 @@
+2014-02-27  Timothy Hatcher  <[email protected]>
+
+        Improve how ContentSearchUtilities::lineEndings works by supporting the three common line endings.
+
+        https://bugs.webkit.org/show_bug.cgi?id=129458
+
+        Reviewed by Joseph Pecoraro.
+
+        * inspector/ContentSearchUtilities.cpp:
+        (Inspector::ContentSearchUtilities::textPositionFromOffset): Remove assumption about line ending length.
+        (Inspector::ContentSearchUtilities::getRegularExpressionMatchesByLines): Remove assumption about
+        line ending type and don't try to strip the line ending. Use size_t
+        (Inspector::ContentSearchUtilities::lineEndings): Use findNextLineStart to find the lines.
+        This will include the line ending in the lines, but that is okay.
+        (Inspector::ContentSearchUtilities::buildObjectForSearchMatch): Use size_t.
+        (Inspector::ContentSearchUtilities::searchInTextByLines): Modernize.
+
 2014-02-27  Joseph Pecoraro  <[email protected]>
 
         [Mac] Warning: Multiple build commands for output file GCSegmentedArray and InspectorAgent

Modified: trunk/Source/_javascript_Core/inspector/ContentSearchUtilities.cpp (164844 => 164845)


--- trunk/Source/_javascript_Core/inspector/ContentSearchUtilities.cpp	2014-02-28 01:03:09 UTC (rev 164844)
+++ trunk/Source/_javascript_Core/inspector/ContentSearchUtilities.cpp	2014-02-28 01:17:37 UTC (rev 164845)
@@ -67,36 +67,36 @@
 
 TextPosition textPositionFromOffset(size_t offset, const Vector<size_t>& lineEndings)
 {
-    const size_t* foundLineEnding = approximateBinarySearch<size_t, size_t>(lineEndings, lineEndings.size(), offset, sizetExtractor);
-    size_t lineIndex = foundLineEnding - &lineEndings.at(0);
-    if (offset > *foundLineEnding)
+    const size_t* foundNextStart = approximateBinarySearch<size_t, size_t>(lineEndings, lineEndings.size(), offset, sizetExtractor);
+    size_t lineIndex = foundNextStart - &lineEndings.at(0);
+    if (offset >= *foundNextStart)
         ++lineIndex;
-    size_t lineStartOffset = lineIndex > 0 ? lineEndings.at(lineIndex - 1) + 1 : 0;
+    size_t lineStartOffset = lineIndex > 0 ? lineEndings.at(lineIndex - 1) : 0;
     size_t column = offset - lineStartOffset;
     return TextPosition(OrdinalNumber::fromZeroBasedInt(lineIndex), OrdinalNumber::fromZeroBasedInt(column));
 }
 
-static Vector<std::pair<int, String>> getRegularExpressionMatchesByLines(const JSC::Yarr::RegularExpression& regex, const String& text)
+static Vector<std::pair<size_t, String>> getRegularExpressionMatchesByLines(const JSC::Yarr::RegularExpression& regex, const String& text)
 {
-    Vector<std::pair<int, String>> result;
+    Vector<std::pair<size_t, String>> result;
     if (text.isEmpty())
         return result;
 
     std::unique_ptr<Vector<size_t>> endings(lineEndings(text));
     size_t size = endings->size();
-    unsigned start = 0;
+    size_t start = 0;
+
     for (size_t lineNumber = 0; lineNumber < size; ++lineNumber) {
-        size_t lineEnd = endings->at(lineNumber);
-        String line = text.substring(start, lineEnd - start);
-        if (line.endsWith('\r'))
-            line = line.left(line.length() - 1);
+        size_t nextStart = endings->at(lineNumber);
+        String line = text.substring(start, nextStart - start);
 
         int matchLength;
         if (regex.match(line, 0, &matchLength) != -1)
-            result.append(std::pair<int, String>(lineNumber, line));
+            result.append(std::pair<size_t, String>(lineNumber, line));
 
-        start = lineEnd + 1;
+        start = nextStart;
     }
+
     return result;
 }
 
@@ -104,21 +104,24 @@
 {
     auto result = std::make_unique<Vector<size_t>>();
 
-    unsigned start = 0;
+    size_t start = 0;
     while (start < text.length()) {
-        size_t lineEnd = text.find('\n', start);
-        if (lineEnd == notFound)
+        size_t nextStart = text.findNextLineStart(start);
+        if (nextStart == notFound) {
+            result->append(text.length());
             break;
+        }
 
-        result->append(lineEnd);
-        start = lineEnd + 1;
+        result->append(nextStart);
+        start = nextStart;
     }
+
     result->append(text.length());
 
     return result;
 }
 
-static PassRefPtr<Inspector::TypeBuilder::GenericTypes::SearchMatch> buildObjectForSearchMatch(int lineNumber, const String& lineContent)
+static PassRefPtr<Inspector::TypeBuilder::GenericTypes::SearchMatch> buildObjectForSearchMatch(size_t lineNumber, const String& lineContent)
 {
     return Inspector::TypeBuilder::GenericTypes::SearchMatch::create()
         .setLineNumber(lineNumber)
@@ -156,10 +159,10 @@
     RefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::GenericTypes::SearchMatch>> result = Inspector::TypeBuilder::Array<Inspector::TypeBuilder::GenericTypes::SearchMatch>::create();
 
     JSC::Yarr::RegularExpression regex = ContentSearchUtilities::createSearchRegex(query, caseSensitive, isRegex);
-    Vector<std::pair<int, String>> matches = getRegularExpressionMatchesByLines(regex, text);
+    Vector<std::pair<size_t, String>> matches = getRegularExpressionMatchesByLines(regex, text);
 
-    for (Vector<std::pair<int, String>>::const_iterator it = matches.begin(); it != matches.end(); ++it)
-        result->addItem(buildObjectForSearchMatch(it->first, it->second));
+    for (const auto& match : matches)
+        result->addItem(buildObjectForSearchMatch(match.first, match.second));
 
     return result;
 }
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to