From 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c Mon Sep 17 00:00:00 2001 From: Lorry Tar Creator Date: Tue, 27 Jun 2017 06:07:23 +0000 Subject: webkitgtk-2.16.5 --- .../inspector/ContentSearchUtilities.cpp | 107 +++++++++------------ 1 file changed, 47 insertions(+), 60 deletions(-) (limited to 'Source/JavaScriptCore/inspector/ContentSearchUtilities.cpp') diff --git a/Source/JavaScriptCore/inspector/ContentSearchUtilities.cpp b/Source/JavaScriptCore/inspector/ContentSearchUtilities.cpp index 8f1f91cf1..7f4504235 100644 --- a/Source/JavaScriptCore/inspector/ContentSearchUtilities.cpp +++ b/Source/JavaScriptCore/inspector/ContentSearchUtilities.cpp @@ -29,14 +29,13 @@ #include "config.h" #include "ContentSearchUtilities.h" -#if ENABLE(INSPECTOR) - -#include "InspectorJSTypeBuilders.h" #include "InspectorValues.h" #include "RegularExpression.h" #include "Yarr.h" +#include "YarrInterpreter.h" #include #include +#include using namespace JSC::Yarr; @@ -47,17 +46,16 @@ static const char regexSpecialCharacters[] = "[](){}+-*.,?\\^$|"; static String createSearchRegexSource(const String& text) { - String result; - const UChar* characters = text.deprecatedCharacters(); - String specials(regexSpecialCharacters); + StringBuilder result; for (unsigned i = 0; i < text.length(); i++) { - if (specials.find(characters[i]) != notFound) - result.append("\\"); - result.append(characters[i]); + UChar character = text[i]; + if (isASCII(character) && strchr(regexSpecialCharacters, character)) + result.append('\\'); + result.append(character); } - return result; + return result.toString(); } static inline size_t sizetExtractor(const size_t* value) @@ -67,60 +65,64 @@ static inline size_t sizetExtractor(const size_t* value) TextPosition textPositionFromOffset(size_t offset, const Vector& lineEndings) { - const size_t* foundLineEnding = approximateBinarySearch(lineEndings, lineEndings.size(), offset, sizetExtractor); - size_t lineIndex = foundLineEnding - &lineEndings.at(0); - if (offset > *foundLineEnding) + const size_t* foundNextStart = approximateBinarySearch(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> getRegularExpressionMatchesByLines(const JSC::Yarr::RegularExpression& regex, const String& text) +static Vector> getRegularExpressionMatchesByLines(const JSC::Yarr::RegularExpression& regex, const String& text) { - Vector> result; + Vector> result; if (text.isEmpty()) return result; - OwnPtr> endings(lineEndings(text)); + std::unique_ptr> 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(lineNumber, line)); + result.append(std::pair(lineNumber, line)); - start = lineEnd + 1; + start = nextStart; } + return result; } -PassOwnPtr> lineEndings(const String& text) +std::unique_ptr> lineEndings(const String& text) { - OwnPtr> result(adoptPtr(new Vector())); + auto result = std::make_unique>(); - 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.find('\n', start); + if (nextStart == notFound || nextStart == (text.length() - 1)) { + result->append(text.length()); break; + } - result->append(lineEnd); - start = lineEnd + 1; + nextStart += 1; + result->append(nextStart); + start = nextStart; } + result->append(text.length()); - return result.release(); + return result; } -static PassRefPtr buildObjectForSearchMatch(int lineNumber, const String& lineContent) +static Ref buildObjectForSearchMatch(size_t lineNumber, const String& lineContent) { - return Inspector::TypeBuilder::GenericTypes::SearchMatch::create() + return Inspector::Protocol::GenericTypes::SearchMatch::create() .setLineNumber(lineNumber) .setLineContent(lineContent) .release(); @@ -151,25 +153,21 @@ int countRegularExpressionMatches(const JSC::Yarr::RegularExpression& regex, con return result; } -PassRefPtr> searchInTextByLines(const String& text, const String& query, const bool caseSensitive, const bool isRegex) +Ref> searchInTextByLines(const String& text, const String& query, const bool caseSensitive, const bool isRegex) { - RefPtr> result = Inspector::TypeBuilder::Array::create(); + Ref> result = Inspector::Protocol::Array::create(); JSC::Yarr::RegularExpression regex = ContentSearchUtilities::createSearchRegex(query, caseSensitive, isRegex); - Vector> matches = getRegularExpressionMatchesByLines(regex, text); + Vector> matches = getRegularExpressionMatchesByLines(regex, text); - for (Vector>::const_iterator it = matches.begin(); it != matches.end(); ++it) - result->addItem(buildObjectForSearchMatch(it->first, it->second)); + for (const auto& match : matches) { + Ref matchObject = buildObjectForSearchMatch(match.first, match.second); + result->addItem(WTFMove(matchObject)); + } return result; } -static String scriptCommentPattern(const String& name) -{ - // "//# =" and deprecated "//@" - return "//[#@][\040\t]" + name + "=[\040\t]*([^\\s\'\"]*)[\040\t]*$"; -} - static String stylesheetCommentPattern(const String& name) { // "/*# = */" and deprecated "/*@" @@ -178,11 +176,12 @@ 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); + JSC::Yarr::YarrPattern pattern(patternString, JSC::RegExpFlags::FlagMultiline, &error); ASSERT(!error); BumpPointerAllocator regexAllocator; - OwnPtr bytecodePattern = JSC::Yarr::byteCompile(pattern, ®exAllocator); + auto bytecodePattern = JSC::Yarr::byteCompile(pattern, ®exAllocator); ASSERT(bytecodePattern); ASSERT(pattern.m_numSubpatterns == 1); @@ -196,22 +195,10 @@ 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 WebCore - -#endif // ENABLE(INSPECTOR) +} // namespace Inspector -- cgit v1.2.1