7#include "src/inspector/protocol/Protocol.h"
16String16 findMagicComment(
const String16& content,
const String16& name,
19 size_t length = content.length();
20 size_t nameLength = name.length();
23 size_t equalSignPos = 0;
24 size_t closingCommentPos = 0;
26 pos = content.reverseFind(name,
pos);
30 if (
pos < 4)
return String16();
32 if (content[
pos] !=
'/')
continue;
33 if ((content[
pos + 1] !=
'/' || multiline) &&
34 (content[
pos + 1] !=
'*' || !multiline))
36 if (content[
pos + 2] !=
'#' && content[
pos + 2] !=
'@')
continue;
37 if (content[
pos + 3] !=
' ' && content[
pos + 3] !=
'\t')
continue;
38 equalSignPos =
pos + 4 + nameLength;
39 if (equalSignPos >= length)
continue;
40 if (content[equalSignPos] !=
'=')
continue;
42 closingCommentPos = content.find(
"*/", equalSignPos + 1);
51 DCHECK(!multiline || closingCommentPos);
52 size_t urlPos = equalSignPos + 1;
53 String16 match = multiline
54 ? content.substring(urlPos, closingCommentPos - urlPos)
55 : content.substring(urlPos);
57 size_t newLine = match.find(
"\n");
59 match = match.stripWhiteSpace();
61 for (
size_t i = 0;
i < match.
length(); ++
i) {
63 if (c ==
'"' || c ==
'\'' || c ==
' ' || c ==
'\t')
return "";
69String16 createSearchRegexSource(
const String16& text) {
72 for (
size_t i = 0;
i < text.
length();
i++) {
74 if (c ==
'[' || c ==
']' || c ==
'(' || c ==
')' || c ==
'{' || c ==
'}' ||
75 c ==
'+' || c ==
'-' || c ==
'*' || c ==
'.' || c ==
',' || c ==
'?' ||
76 c ==
'\\' || c ==
'^' || c ==
'$' || c ==
'|') {
85std::unique_ptr<std::vector<size_t>> lineEndings(
const String16& text) {
86 std::unique_ptr<std::vector<size_t>>
result(
new std::vector<size_t>());
88 const String16 lineEndString =
"\n";
90 while (
start < text.length()) {
91 size_t lineEnd = text.find(lineEndString,
start);
94 result->push_back(lineEnd);
97 result->push_back(text.length());
102std::vector<std::pair<int, String16>> scriptRegexpMatchesByLines(
103 const V8Regex& regex,
const String16& text) {
104 std::vector<std::pair<int, String16>>
result;
105 if (text.isEmpty())
return result;
107 std::unique_ptr<std::vector<size_t>> endings(lineEndings(text));
108 size_t size = endings->size();
110 for (
size_t lineNumber = 0; lineNumber <
size; ++lineNumber) {
111 size_t lineEnd = endings->at(lineNumber);
112 String16 line = text.substring(
start, lineEnd -
start);
113 if (line.length() && line[line.length() - 1] ==
'\r')
114 line = line.substring(0, line.length() - 1);
117 if (regex.match(line, 0, &matchLength) != -1)
118 result.push_back(std::pair<int, String16>(lineNumber, line));
125std::unique_ptr<protocol::Debugger::SearchMatch> buildObjectForSearchMatch(
126 int lineNumber,
const String16& lineContent) {
127 return protocol::Debugger::SearchMatch::create()
128 .setLineNumber(lineNumber)
129 .setLineContent(lineContent)
133std::unique_ptr<V8Regex> createSearchRegex(V8InspectorImpl* inspector,
134 const String16& query,
135 bool caseSensitive,
bool isRegex) {
136 String16 regexSource = isRegex ? query : createSearchRegexSource(query);
137 return std::unique_ptr<V8Regex>(
138 new V8Regex(inspector, regexSource, caseSensitive));
143std::vector<std::unique_ptr<protocol::Debugger::SearchMatch>>
145 const String16& query,
const bool caseSensitive,
146 const bool isRegex) {
147 std::unique_ptr<V8Regex> regex = createSearchRegex(
149 caseSensitive, isRegex);
150 std::vector<std::pair<int, String16>> matches =
151 scriptRegexpMatchesByLines(*regex, text);
153 std::vector<std::unique_ptr<protocol::Debugger::SearchMatch>>
result;
154 result.reserve(matches.size());
155 for (
const auto& match : matches)
156 result.push_back(buildObjectForSearchMatch(match.first, match.second));
161 return findMagicComment(content,
"sourceURL", multiline);
165 return findMagicComment(content,
"sourceMappingURL", multiline);
static const size_t kNotFound
ZoneVector< RpoNumber > & result
std::vector< std::unique_ptr< protocol::Debugger::SearchMatch > > searchInTextByLinesImpl(V8InspectorSession *session, const String16 &text, const String16 &query, const bool caseSensitive, const bool isRegex)
String16 findSourceURL(const String16 &content, bool multiline)
String16 findSourceMapURL(const String16 &content, bool multiline)
#define DCHECK(condition)
#define DCHECK_LT(v1, v2)
#define DCHECK_EQ(v1, v2)