summaryrefslogtreecommitdiff
path: root/Source/JavaScriptCore/inspector/ContentSearchUtilities.cpp
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@lorry>2016-04-10 09:28:39 +0000
committerLorry Tar Creator <lorry-tar-importer@lorry>2016-04-10 09:28:39 +0000
commit32761a6cee1d0dee366b885b7b9c777e67885688 (patch)
treed6bec92bebfb216f4126356e55518842c2f476a1 /Source/JavaScriptCore/inspector/ContentSearchUtilities.cpp
parenta4e969f4965059196ca948db781e52f7cfebf19e (diff)
downloadWebKitGtk-tarball-32761a6cee1d0dee366b885b7b9c777e67885688.tar.gz
webkitgtk-2.4.11webkitgtk-2.4.11
Diffstat (limited to 'Source/JavaScriptCore/inspector/ContentSearchUtilities.cpp')
-rw-r--r--Source/JavaScriptCore/inspector/ContentSearchUtilities.cpp103
1 files changed, 59 insertions, 44 deletions
diff --git a/Source/JavaScriptCore/inspector/ContentSearchUtilities.cpp b/Source/JavaScriptCore/inspector/ContentSearchUtilities.cpp
index f3515f4e7..8f1f91cf1 100644
--- a/Source/JavaScriptCore/inspector/ContentSearchUtilities.cpp
+++ b/Source/JavaScriptCore/inspector/ContentSearchUtilities.cpp
@@ -29,12 +29,14 @@
#include "config.h"
#include "ContentSearchUtilities.h"
+#if ENABLE(INSPECTOR)
+
+#include "InspectorJSTypeBuilders.h"
#include "InspectorValues.h"
#include "RegularExpression.h"
#include "Yarr.h"
#include <wtf/BumpPointerAllocator.h>
#include <wtf/StdLibExtras.h>
-#include <wtf/text/StringBuilder.h>
using namespace JSC::Yarr;
@@ -45,16 +47,17 @@ static const char regexSpecialCharacters[] = "[](){}+-*.,?\\^$|";
static String createSearchRegexSource(const String& text)
{
- StringBuilder result;
+ String result;
+ const UChar* characters = text.deprecatedCharacters();
+ String specials(regexSpecialCharacters);
for (unsigned i = 0; i < text.length(); i++) {
- UChar character = text[i];
- if (isASCII(character) && strchr(regexSpecialCharacters, character))
- result.append('\\');
- result.append(character);
+ if (specials.find(characters[i]) != notFound)
+ result.append("\\");
+ result.append(characters[i]);
}
- return result.toString();
+ return result;
}
static inline size_t sizetExtractor(const size_t* value)
@@ -64,63 +67,60 @@ static inline size_t sizetExtractor(const size_t* value)
TextPosition textPositionFromOffset(size_t offset, const Vector<size_t>& lineEndings)
{
- const size_t* foundNextStart = approximateBinarySearch<size_t, size_t>(lineEndings, lineEndings.size(), offset, sizetExtractor);
- size_t lineIndex = foundNextStart - &lineEndings.at(0);
- if (offset >= *foundNextStart)
+ const size_t* foundLineEnding = approximateBinarySearch<size_t, size_t>(lineEndings, lineEndings.size(), offset, sizetExtractor);
+ size_t lineIndex = foundLineEnding - &lineEndings.at(0);
+ if (offset > *foundLineEnding)
++lineIndex;
- size_t lineStartOffset = lineIndex > 0 ? lineEndings.at(lineIndex - 1) : 0;
+ size_t lineStartOffset = lineIndex > 0 ? lineEndings.at(lineIndex - 1) + 1 : 0;
size_t column = offset - lineStartOffset;
return TextPosition(OrdinalNumber::fromZeroBasedInt(lineIndex), OrdinalNumber::fromZeroBasedInt(column));
}
-static Vector<std::pair<size_t, String>> getRegularExpressionMatchesByLines(const JSC::Yarr::RegularExpression& regex, const String& text)
+static Vector<std::pair<int, String>> getRegularExpressionMatchesByLines(const JSC::Yarr::RegularExpression& regex, const String& text)
{
- Vector<std::pair<size_t, String>> result;
+ Vector<std::pair<int, String>> result;
if (text.isEmpty())
return result;
- std::unique_ptr<Vector<size_t>> endings(lineEndings(text));
+ OwnPtr<Vector<size_t>> endings(lineEndings(text));
size_t size = endings->size();
- size_t start = 0;
-
+ unsigned start = 0;
for (size_t lineNumber = 0; lineNumber < size; ++lineNumber) {
- size_t nextStart = endings->at(lineNumber);
- String line = text.substring(start, nextStart - start);
+ size_t lineEnd = endings->at(lineNumber);
+ String line = text.substring(start, lineEnd - start);
+ if (line.endsWith('\r'))
+ line = line.left(line.length() - 1);
int matchLength;
if (regex.match(line, 0, &matchLength) != -1)
- result.append(std::pair<size_t, String>(lineNumber, line));
+ result.append(std::pair<int, String>(lineNumber, line));
- start = nextStart;
+ start = lineEnd + 1;
}
-
return result;
}
-std::unique_ptr<Vector<size_t>> lineEndings(const String& text)
+PassOwnPtr<Vector<size_t>> lineEndings(const String& text)
{
- auto result = std::make_unique<Vector<size_t>>();
+ OwnPtr<Vector<size_t>> result(adoptPtr(new Vector<size_t>()));
- size_t start = 0;
+ unsigned start = 0;
while (start < text.length()) {
- size_t nextStart = text.findNextLineStart(start);
- if (nextStart == notFound) {
- result->append(text.length());
+ size_t lineEnd = text.find('\n', start);
+ if (lineEnd == notFound)
break;
- }
- result->append(nextStart);
- start = nextStart;
+ result->append(lineEnd);
+ start = lineEnd + 1;
}
-
result->append(text.length());
- return result;
+ return result.release();
}
-static Ref<Inspector::Protocol::GenericTypes::SearchMatch> buildObjectForSearchMatch(size_t lineNumber, const String& lineContent)
+static PassRefPtr<Inspector::TypeBuilder::GenericTypes::SearchMatch> buildObjectForSearchMatch(int lineNumber, const String& lineContent)
{
- return Inspector::Protocol::GenericTypes::SearchMatch::create()
+ return Inspector::TypeBuilder::GenericTypes::SearchMatch::create()
.setLineNumber(lineNumber)
.setLineContent(lineContent)
.release();
@@ -151,21 +151,25 @@ int countRegularExpressionMatches(const JSC::Yarr::RegularExpression& regex, con
return result;
}
-Ref<Inspector::Protocol::Array<Inspector::Protocol::GenericTypes::SearchMatch>> searchInTextByLines(const String& text, const String& query, const bool caseSensitive, const bool isRegex)
+PassRefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::GenericTypes::SearchMatch>> searchInTextByLines(const String& text, const String& query, const bool caseSensitive, const bool isRegex)
{
- Ref<Inspector::Protocol::Array<Inspector::Protocol::GenericTypes::SearchMatch>> result = Inspector::Protocol::Array<Inspector::Protocol::GenericTypes::SearchMatch>::create();
+ 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<size_t, String>> matches = getRegularExpressionMatchesByLines(regex, text);
+ Vector<std::pair<int, String>> matches = getRegularExpressionMatchesByLines(regex, text);
- for (const auto& match : matches) {
- Ref<Inspector::Protocol::GenericTypes::SearchMatch> matchObject = buildObjectForSearchMatch(match.first, match.second);
- result->addItem(WTFMove(matchObject));
- }
+ for (Vector<std::pair<int, String>>::const_iterator it = matches.begin(); it != matches.end(); ++it)
+ result->addItem(buildObjectForSearchMatch(it->first, it->second));
return result;
}
+static String scriptCommentPattern(const String& name)
+{
+ // "//# <name>=<value>" and deprecated "//@"
+ return "//[#@][\040\t]" + name + "=[\040\t]*([^\\s\'\"]*)[\040\t]*$";
+}
+
static String stylesheetCommentPattern(const String& name)
{
// "/*# <name>=<value> */" and deprecated "/*@"
@@ -174,12 +178,11 @@ static String stylesheetCommentPattern(const String& name)
static String findMagicComment(const String& content, const String& patternString)
{
- ASSERT(!content.isNull());
const char* error = nullptr;
JSC::Yarr::YarrPattern pattern(patternString, false, true, &error);
ASSERT(!error);
BumpPointerAllocator regexAllocator;
- auto bytecodePattern = JSC::Yarr::byteCompile(pattern, &regexAllocator);
+ OwnPtr<JSC::Yarr::BytecodePattern> bytecodePattern = JSC::Yarr::byteCompile(pattern, &regexAllocator);
ASSERT(bytecodePattern);
ASSERT(pattern.m_numSubpatterns == 1);
@@ -193,10 +196,22 @@ static String findMagicComment(const String& content, const String& patternStrin
return content.substring(matches[2], matches[3] - matches[2]);
}
+String findScriptSourceURL(const String& content)
+{
+ return findMagicComment(content, scriptCommentPattern(ASCIILiteral("sourceURL")));
+}
+
+String findScriptSourceMapURL(const String& content)
+{
+ return findMagicComment(content, scriptCommentPattern(ASCIILiteral("sourceMappingURL")));
+}
+
String findStylesheetSourceMapURL(const String& content)
{
return findMagicComment(content, stylesheetCommentPattern(ASCIILiteral("sourceMappingURL")));
}
} // namespace ContentSearchUtilities
-} // namespace Inspector
+} // namespace WebCore
+
+#endif // ENABLE(INSPECTOR)