OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "platform/v8_inspector/V8StringUtil.h" | 5 #include "platform/v8_inspector/StringUtil.h" |
6 | 6 |
7 #include "platform/inspector_protocol/InspectorProtocol.h" | 7 #include "platform/v8_inspector/protocol/Protocol.h" |
8 #include "platform/v8_inspector/V8InspectorImpl.h" | |
9 #include "platform/v8_inspector/V8InspectorSessionImpl.h" | |
10 #include "platform/v8_inspector/V8Regex.h" | |
11 | 8 |
12 namespace v8_inspector { | 9 namespace v8_inspector { |
13 | 10 |
14 namespace { | |
15 | |
16 String16 findMagicComment(const String16& content, const String16& name, bool mu
ltiline) | |
17 { | |
18 DCHECK(name.find("=") == String16::kNotFound); | |
19 unsigned length = content.length(); | |
20 unsigned nameLength = name.length(); | |
21 | |
22 size_t pos = length; | |
23 size_t equalSignPos = 0; | |
24 size_t closingCommentPos = 0; | |
25 while (true) { | |
26 pos = content.reverseFind(name, pos); | |
27 if (pos == String16::kNotFound) | |
28 return String16(); | |
29 | |
30 // Check for a /\/[\/*][@#][ \t]/ regexp (length of 4) before found name
. | |
31 if (pos < 4) | |
32 return String16(); | |
33 pos -= 4; | |
34 if (content[pos] != '/') | |
35 continue; | |
36 if ((content[pos + 1] != '/' || multiline) | |
37 && (content[pos + 1] != '*' || !multiline)) | |
38 continue; | |
39 if (content[pos + 2] != '#' && content[pos + 2] != '@') | |
40 continue; | |
41 if (content[pos + 3] != ' ' && content[pos + 3] != '\t') | |
42 continue; | |
43 equalSignPos = pos + 4 + nameLength; | |
44 if (equalSignPos < length && content[equalSignPos] != '=') | |
45 continue; | |
46 if (multiline) { | |
47 closingCommentPos = content.find("*/", equalSignPos + 1); | |
48 if (closingCommentPos == String16::kNotFound) | |
49 return String16(); | |
50 } | |
51 | |
52 break; | |
53 } | |
54 | |
55 DCHECK(equalSignPos); | |
56 DCHECK(!multiline || closingCommentPos); | |
57 size_t urlPos = equalSignPos + 1; | |
58 String16 match = multiline | |
59 ? content.substring(urlPos, closingCommentPos - urlPos) | |
60 : content.substring(urlPos); | |
61 | |
62 size_t newLine = match.find("\n"); | |
63 if (newLine != String16::kNotFound) | |
64 match = match.substring(0, newLine); | |
65 match = match.stripWhiteSpace(); | |
66 | |
67 for (unsigned i = 0; i < match.length(); ++i) { | |
68 UChar c = match[i]; | |
69 if (c == '"' || c == '\'' || c == ' ' || c == '\t') | |
70 return ""; | |
71 } | |
72 | |
73 return match; | |
74 } | |
75 | |
76 String16 createSearchRegexSource(const String16& text) | |
77 { | |
78 String16Builder result; | |
79 | |
80 for (unsigned i = 0; i < text.length(); i++) { | |
81 UChar c = text[i]; | |
82 if (c == '[' || c == ']' || c == '(' || c == ')' || c == '{' || c == '}' | |
83 || c == '+' || c == '-' || c == '*' || c == '.' || c == ',' || c ==
'?' | |
84 || c == '\\' || c == '^' || c == '$' || c == '|') { | |
85 result.append('\\'); | |
86 } | |
87 result.append(text[i]); | |
88 } | |
89 | |
90 return result.toString(); | |
91 } | |
92 | |
93 std::unique_ptr<std::vector<unsigned>> lineEndings(const String16& text) | |
94 { | |
95 std::unique_ptr<std::vector<unsigned>> result(new std::vector<unsigned>()); | |
96 | |
97 const String16 lineEndString = "\n"; | |
98 unsigned start = 0; | |
99 while (start < text.length()) { | |
100 size_t lineEnd = text.find(lineEndString, start); | |
101 if (lineEnd == String16::kNotFound) | |
102 break; | |
103 | |
104 result->push_back(static_cast<unsigned>(lineEnd)); | |
105 start = lineEnd + 1; | |
106 } | |
107 result->push_back(text.length()); | |
108 | |
109 return result; | |
110 } | |
111 | |
112 std::vector<std::pair<int, String16>> scriptRegexpMatchesByLines(const V8Regex&
regex, const String16& text) | |
113 { | |
114 std::vector<std::pair<int, String16>> result; | |
115 if (text.isEmpty()) | |
116 return result; | |
117 | |
118 std::unique_ptr<std::vector<unsigned>> endings(lineEndings(text)); | |
119 unsigned size = endings->size(); | |
120 unsigned start = 0; | |
121 for (unsigned lineNumber = 0; lineNumber < size; ++lineNumber) { | |
122 unsigned lineEnd = endings->at(lineNumber); | |
123 String16 line = text.substring(start, lineEnd - start); | |
124 if (line.length() && line[line.length() - 1] == '\r') | |
125 line = line.substring(0, line.length() - 1); | |
126 | |
127 int matchLength; | |
128 if (regex.match(line, 0, &matchLength) != -1) | |
129 result.push_back(std::pair<int, String16>(lineNumber, line)); | |
130 | |
131 start = lineEnd + 1; | |
132 } | |
133 return result; | |
134 } | |
135 | |
136 std::unique_ptr<protocol::Debugger::SearchMatch> buildObjectForSearchMatch(int l
ineNumber, const String16& lineContent) | |
137 { | |
138 return protocol::Debugger::SearchMatch::create() | |
139 .setLineNumber(lineNumber) | |
140 .setLineContent(lineContent) | |
141 .build(); | |
142 } | |
143 | |
144 std::unique_ptr<V8Regex> createSearchRegex(V8InspectorImpl* inspector, const Str
ing16& query, bool caseSensitive, bool isRegex) | |
145 { | |
146 String16 regexSource = isRegex ? query : createSearchRegexSource(query); | |
147 return wrapUnique(new V8Regex(inspector, regexSource, caseSensitive)); | |
148 } | |
149 | |
150 } // namespace | |
151 | |
152 v8::Local<v8::String> toV8String(v8::Isolate* isolate, const String16& string) | 11 v8::Local<v8::String> toV8String(v8::Isolate* isolate, const String16& string) |
153 { | 12 { |
154 if (string.isEmpty()) | 13 if (string.isEmpty()) |
155 return v8::String::Empty(isolate); | 14 return v8::String::Empty(isolate); |
156 return v8::String::NewFromTwoByte(isolate, reinterpret_cast<const uint16_t*>
(string.characters16()), v8::NewStringType::kNormal, string.length()).ToLocalChe
cked(); | 15 return v8::String::NewFromTwoByte(isolate, reinterpret_cast<const uint16_t*>
(string.characters16()), v8::NewStringType::kNormal, string.length()).ToLocalChe
cked(); |
157 } | 16 } |
158 | 17 |
159 v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate, const String1
6& string) | 18 v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate, const String1
6& string) |
160 { | 19 { |
161 if (string.isEmpty()) | 20 if (string.isEmpty()) |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 } | 79 } |
221 } else { | 80 } else { |
222 for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) { | 81 for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) { |
223 if (string.characters16()[i] != prefix[j]) | 82 if (string.characters16()[i] != prefix[j]) |
224 return false; | 83 return false; |
225 } | 84 } |
226 } | 85 } |
227 return true; | 86 return true; |
228 } | 87 } |
229 | 88 |
| 89 namespace protocol { |
| 90 |
230 std::unique_ptr<protocol::Value> parseJSON(const StringView& string) | 91 std::unique_ptr<protocol::Value> parseJSON(const StringView& string) |
231 { | 92 { |
232 if (!string.length()) | 93 if (!string.length()) |
233 return nullptr; | 94 return nullptr; |
234 if (string.is8Bit()) | 95 if (string.is8Bit()) |
235 return blink::protocol::parseJSON(string.characters8(), string.length())
; | 96 return protocol::parseJSON(string.characters8(), string.length()); |
236 return blink::protocol::parseJSON(string.characters16(), string.length()); | 97 return protocol::parseJSON(string.characters16(), string.length()); |
237 } | 98 } |
238 | 99 |
239 std::vector<std::unique_ptr<protocol::Debugger::SearchMatch>> searchInTextByLine
sImpl(V8InspectorSession* session, const String16& text, const String16& query,
const bool caseSensitive, const bool isRegex) | 100 std::unique_ptr<protocol::Value> parseJSON(const String16& string) |
240 { | 101 { |
241 std::unique_ptr<V8Regex> regex = createSearchRegex(static_cast<V8InspectorSe
ssionImpl*>(session)->inspector(), query, caseSensitive, isRegex); | 102 if (!string.length()) |
242 std::vector<std::pair<int, String16>> matches = scriptRegexpMatchesByLines(*
regex.get(), text); | 103 return nullptr; |
243 | 104 return protocol::parseJSON(string.characters16(), string.length()); |
244 std::vector<std::unique_ptr<protocol::Debugger::SearchMatch>> result; | |
245 for (const auto& match : matches) | |
246 result.push_back(buildObjectForSearchMatch(match.first, match.second)); | |
247 return result; | |
248 } | 105 } |
249 | 106 |
250 String16 findSourceURL(const String16& content, bool multiline) | 107 } // namespace protocol |
251 { | |
252 return findMagicComment(content, "sourceURL", multiline); | |
253 } | |
254 | |
255 String16 findSourceMapURL(const String16& content, bool multiline) | |
256 { | |
257 return findMagicComment(content, "sourceMappingURL", multiline); | |
258 } | |
259 | 108 |
260 std::unique_ptr<protocol::Value> toProtocolValue(v8::Local<v8::Context> context,
v8::Local<v8::Value> value, int maxDepth) | 109 std::unique_ptr<protocol::Value> toProtocolValue(v8::Local<v8::Context> context,
v8::Local<v8::Value> value, int maxDepth) |
261 { | 110 { |
262 if (value.IsEmpty()) { | 111 if (value.IsEmpty()) { |
263 NOTREACHED(); | 112 NOTREACHED(); |
264 return nullptr; | 113 return nullptr; |
265 } | 114 } |
266 | 115 |
267 if (!maxDepth) | 116 if (!maxDepth) |
268 return nullptr; | 117 return nullptr; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 return wrapUnique(new StringBufferImpl(string)); | 192 return wrapUnique(new StringBufferImpl(string)); |
344 } | 193 } |
345 | 194 |
346 StringBufferImpl::StringBufferImpl(String16& string) | 195 StringBufferImpl::StringBufferImpl(String16& string) |
347 { | 196 { |
348 m_owner.swap(string); | 197 m_owner.swap(string); |
349 m_string = toStringView(m_owner); | 198 m_string = toStringView(m_owner); |
350 } | 199 } |
351 | 200 |
352 } // namespace v8_inspector | 201 } // namespace v8_inspector |
OLD | NEW |