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/V8StringUtil.h" |
6 | 6 |
| 7 #include "platform/inspector_protocol/String16.h" |
7 #include "platform/v8_inspector/V8DebuggerImpl.h" | 8 #include "platform/v8_inspector/V8DebuggerImpl.h" |
8 #include "platform/v8_inspector/V8Regex.h" | 9 #include "platform/v8_inspector/V8Regex.h" |
9 #include "platform/v8_inspector/public/V8ContentSearchUtil.h" | 10 #include "platform/v8_inspector/public/V8ContentSearchUtil.h" |
10 #include "wtf/text/StringBuilder.h" | |
11 | 11 |
12 namespace blink { | 12 namespace blink { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 String findMagicComment(const String& content, const String& name, bool multilin
e, bool* deprecated) | 16 String16 findMagicComment(const String16& content, const String16& name, bool mu
ltiline, bool* deprecated) |
17 { | 17 { |
18 ASSERT(name.find("=") == kNotFound); | 18 ASSERT(name.find("=") == kNotFound); |
19 if (deprecated) | 19 if (deprecated) |
20 *deprecated = false; | 20 *deprecated = false; |
21 | 21 |
22 unsigned length = content.length(); | 22 unsigned length = content.length(); |
23 unsigned nameLength = name.length(); | 23 unsigned nameLength = name.length(); |
24 | 24 |
25 size_t pos = length; | 25 size_t pos = length; |
26 size_t equalSignPos = 0; | 26 size_t equalSignPos = 0; |
27 size_t closingCommentPos = 0; | 27 size_t closingCommentPos = 0; |
28 while (true) { | 28 while (true) { |
29 pos = content.reverseFind(name, pos); | 29 pos = content.reverseFind(name, pos); |
30 if (pos == kNotFound) | 30 if (pos == kNotFound) |
31 return String(); | 31 return String16(); |
32 | 32 |
33 // Check for a /\/[\/*][@#][ \t]/ regexp (length of 4) before found name
. | 33 // Check for a /\/[\/*][@#][ \t]/ regexp (length of 4) before found name
. |
34 if (pos < 4) | 34 if (pos < 4) |
35 return String(); | 35 return String16(); |
36 pos -= 4; | 36 pos -= 4; |
37 if (content[pos] != '/') | 37 if (content[pos] != '/') |
38 continue; | 38 continue; |
39 if ((content[pos + 1] != '/' || multiline) | 39 if ((content[pos + 1] != '/' || multiline) |
40 && (content[pos + 1] != '*' || !multiline)) | 40 && (content[pos + 1] != '*' || !multiline)) |
41 continue; | 41 continue; |
42 if (content[pos + 2] != '#' && content[pos + 2] != '@') | 42 if (content[pos + 2] != '#' && content[pos + 2] != '@') |
43 continue; | 43 continue; |
44 if (content[pos + 3] != ' ' && content[pos + 3] != '\t') | 44 if (content[pos + 3] != ' ' && content[pos + 3] != '\t') |
45 continue; | 45 continue; |
46 equalSignPos = pos + 4 + nameLength; | 46 equalSignPos = pos + 4 + nameLength; |
47 if (equalSignPos < length && content[equalSignPos] != '=') | 47 if (equalSignPos < length && content[equalSignPos] != '=') |
48 continue; | 48 continue; |
49 if (multiline) { | 49 if (multiline) { |
50 closingCommentPos = content.find("*/", equalSignPos + 1); | 50 closingCommentPos = content.find("*/", equalSignPos + 1); |
51 if (closingCommentPos == kNotFound) | 51 if (closingCommentPos == kNotFound) |
52 return String(); | 52 return String16(); |
53 } | 53 } |
54 | 54 |
55 break; | 55 break; |
56 } | 56 } |
57 | 57 |
58 if (deprecated && content[pos + 2] == '@') | 58 if (deprecated && content[pos + 2] == '@') |
59 *deprecated = true; | 59 *deprecated = true; |
60 | 60 |
61 ASSERT(equalSignPos); | 61 ASSERT(equalSignPos); |
62 ASSERT(!multiline || closingCommentPos); | 62 ASSERT(!multiline || closingCommentPos); |
63 size_t urlPos = equalSignPos + 1; | 63 size_t urlPos = equalSignPos + 1; |
64 String match = multiline | 64 String16 match = multiline |
65 ? content.substring(urlPos, closingCommentPos - urlPos) | 65 ? content.substring(urlPos, closingCommentPos - urlPos) |
66 : content.substring(urlPos); | 66 : content.substring(urlPos); |
67 | 67 |
68 size_t newLine = match.find("\n"); | 68 size_t newLine = match.find("\n"); |
69 if (newLine != kNotFound) | 69 if (newLine != kNotFound) |
70 match = match.substring(0, newLine); | 70 match = match.substring(0, newLine); |
71 match = match.stripWhiteSpace(); | 71 match = match.stripWhiteSpace(); |
72 | 72 |
73 String disallowedChars("\"' \t"); | 73 String16 disallowedChars("\"' \t"); |
74 for (unsigned i = 0; i < match.length(); ++i) { | 74 for (unsigned i = 0; i < match.length(); ++i) { |
75 if (disallowedChars.find(match[i]) != kNotFound) | 75 if (disallowedChars.find(match[i]) != kNotFound) |
76 return ""; | 76 return ""; |
77 } | 77 } |
78 | 78 |
79 return match; | 79 return match; |
80 } | 80 } |
81 | 81 |
82 String createSearchRegexSource(const String& text) | 82 String16 createSearchRegexSource(const String16& text) |
83 { | 83 { |
84 StringBuilder result; | 84 String16Builder result; |
85 String specials("[](){}+-*.,?\\^$|"); | 85 String16 specials("[](){}+-*.,?\\^$|"); |
86 | 86 |
87 for (unsigned i = 0; i < text.length(); i++) { | 87 for (unsigned i = 0; i < text.length(); i++) { |
88 if (specials.find(text[i]) != kNotFound) | 88 if (specials.find(text[i]) != kNotFound) |
89 result.append('\\'); | 89 result.append('\\'); |
90 result.append(text[i]); | 90 result.append(text[i]); |
91 } | 91 } |
92 | 92 |
93 return result.toString(); | 93 return result.toString(); |
94 } | 94 } |
95 | 95 |
96 PassOwnPtr<protocol::Vector<unsigned>> lineEndings(const String& text) | 96 PassOwnPtr<protocol::Vector<unsigned>> lineEndings(const String16& text) |
97 { | 97 { |
98 OwnPtr<protocol::Vector<unsigned>> result(adoptPtr(new protocol::Vector<unsi
gned>())); | 98 OwnPtr<protocol::Vector<unsigned>> result(adoptPtr(new protocol::Vector<unsi
gned>())); |
99 | 99 |
100 unsigned start = 0; | 100 unsigned start = 0; |
101 while (start < text.length()) { | 101 while (start < text.length()) { |
102 size_t lineEnd = text.find('\n', start); | 102 size_t lineEnd = text.find('\n', start); |
103 if (lineEnd == kNotFound) | 103 if (lineEnd == kNotFound) |
104 break; | 104 break; |
105 | 105 |
106 result->append(static_cast<unsigned>(lineEnd)); | 106 result->append(static_cast<unsigned>(lineEnd)); |
107 start = lineEnd + 1; | 107 start = lineEnd + 1; |
108 } | 108 } |
109 result->append(text.length()); | 109 result->append(text.length()); |
110 | 110 |
111 return result.release(); | 111 return result.release(); |
112 } | 112 } |
113 | 113 |
114 protocol::Vector<std::pair<int, String>> scriptRegexpMatchesByLines(const V8Rege
x& regex, const String& text) | 114 protocol::Vector<std::pair<int, String16>> scriptRegexpMatchesByLines(const V8Re
gex& regex, const String16& text) |
115 { | 115 { |
116 protocol::Vector<std::pair<int, String>> result; | 116 protocol::Vector<std::pair<int, String16>> result; |
117 if (text.isEmpty()) | 117 if (text.isEmpty()) |
118 return result; | 118 return result; |
119 | 119 |
120 OwnPtr<protocol::Vector<unsigned>> endings(lineEndings(text)); | 120 OwnPtr<protocol::Vector<unsigned>> endings(lineEndings(text)); |
121 unsigned size = endings->size(); | 121 unsigned size = endings->size(); |
122 unsigned start = 0; | 122 unsigned start = 0; |
123 for (unsigned lineNumber = 0; lineNumber < size; ++lineNumber) { | 123 for (unsigned lineNumber = 0; lineNumber < size; ++lineNumber) { |
124 unsigned lineEnd = endings->at(lineNumber); | 124 unsigned lineEnd = endings->at(lineNumber); |
125 String line = text.substring(start, lineEnd - start); | 125 String16 line = text.substring(start, lineEnd - start); |
126 if (line.endsWith('\r')) | 126 if (line.endsWith('\r')) |
127 line = line.left(line.length() - 1); | 127 line = line.left(line.length() - 1); |
128 | 128 |
129 int matchLength; | 129 int matchLength; |
130 if (regex.match(line, 0, &matchLength) != -1) | 130 if (regex.match(line, 0, &matchLength) != -1) |
131 result.append(std::pair<int, String>(lineNumber, line)); | 131 result.append(std::pair<int, String16>(lineNumber, line)); |
132 | 132 |
133 start = lineEnd + 1; | 133 start = lineEnd + 1; |
134 } | 134 } |
135 return result; | 135 return result; |
136 } | 136 } |
137 | 137 |
138 PassOwnPtr<protocol::Debugger::SearchMatch> buildObjectForSearchMatch(int lineNu
mber, const String& lineContent) | 138 PassOwnPtr<protocol::Debugger::SearchMatch> buildObjectForSearchMatch(int lineNu
mber, const String16& lineContent) |
139 { | 139 { |
140 return protocol::Debugger::SearchMatch::create() | 140 return protocol::Debugger::SearchMatch::create() |
141 .setLineNumber(lineNumber) | 141 .setLineNumber(lineNumber) |
142 .setLineContent(lineContent) | 142 .setLineContent(lineContent) |
143 .build(); | 143 .build(); |
144 } | 144 } |
145 | 145 |
146 PassOwnPtr<V8Regex> createSearchRegex(V8DebuggerImpl* debugger, const String& qu
ery, bool caseSensitive, bool isRegex) | 146 PassOwnPtr<V8Regex> createSearchRegex(V8DebuggerImpl* debugger, const String16&
query, bool caseSensitive, bool isRegex) |
147 { | 147 { |
148 String regexSource = isRegex ? query : createSearchRegexSource(query); | 148 String16 regexSource = isRegex ? query : createSearchRegexSource(query); |
149 return adoptPtr(new V8Regex(debugger, regexSource, caseSensitive ? TextCaseS
ensitive : TextCaseInsensitive)); | 149 return adoptPtr(new V8Regex(debugger, regexSource, caseSensitive)); |
150 } | 150 } |
151 | 151 |
152 } // namespace | 152 } // namespace |
153 | 153 |
154 v8::Local<v8::String> toV8String(v8::Isolate* isolate, const String& string) | 154 v8::Local<v8::String> toV8String(v8::Isolate* isolate, const String16& string) |
155 { | 155 { |
156 return v8::String::NewFromUtf8(isolate, string.utf8().data(), v8::NewStringT
ype::kNormal).ToLocalChecked(); | 156 if (string.isNull()) |
| 157 return v8::String::Empty(isolate); |
| 158 if (string.is8Bit()) |
| 159 return v8::String::NewFromOneByte(isolate, string.characters8(), v8::New
StringType::kNormal, string.length()).ToLocalChecked(); |
| 160 return v8::String::NewFromTwoByte(isolate, string.characters16(), v8::NewStr
ingType::kNormal, string.length()).ToLocalChecked(); |
157 } | 161 } |
158 | 162 |
159 v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate, const String&
string) | 163 v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate, const String1
6& string) |
160 { | 164 { |
161 return v8::String::NewFromUtf8(isolate, string.utf8().data(), v8::NewStringT
ype::kInternalized).ToLocalChecked(); | 165 if (string.isNull()) |
| 166 return v8::String::Empty(isolate); |
| 167 if (string.is8Bit()) |
| 168 return v8::String::NewFromOneByte(isolate, string.characters8(), v8::New
StringType::kInternalized, string.length()).ToLocalChecked(); |
| 169 return v8::String::NewFromTwoByte(isolate, string.characters16(), v8::NewStr
ingType::kInternalized, string.length()).ToLocalChecked(); |
162 } | 170 } |
163 | 171 |
164 String toWTFString(v8::Local<v8::String> value) | 172 String16 toProtocolString(v8::Local<v8::String> value) |
165 { | 173 { |
166 if (value.IsEmpty() || value->IsNull() || value->IsUndefined()) | 174 if (value.IsEmpty() || value->IsNull() || value->IsUndefined()) |
167 return String(); | 175 return String16(); |
168 UChar* buffer; | 176 UChar* buffer; |
169 String result = String::createUninitialized(value->Length(), buffer); | 177 String16 result = String16::createUninitialized(value->Length(), buffer); |
170 value->Write(reinterpret_cast<uint16_t*>(buffer), 0, value->Length()); | 178 value->Write(reinterpret_cast<uint16_t*>(buffer), 0, value->Length()); |
171 return result; | 179 return result; |
172 } | 180 } |
173 | 181 |
174 String toWTFStringWithTypeCheck(v8::Local<v8::Value> value) | 182 String16 toProtocolStringWithTypeCheck(v8::Local<v8::Value> value) |
175 { | 183 { |
176 if (value.IsEmpty() || !value->IsString()) | 184 if (value.IsEmpty() || !value->IsString()) |
177 return String(); | 185 return String16(); |
178 return toWTFString(value.As<v8::String>()); | 186 return toProtocolString(value.As<v8::String>()); |
179 } | 187 } |
180 | 188 |
181 namespace V8ContentSearchUtil { | 189 namespace V8ContentSearchUtil { |
182 | 190 |
183 String findSourceURL(const String& content, bool multiline, bool* deprecated) | 191 String16 findSourceURL(const String16& content, bool multiline, bool* deprecated
) |
184 { | 192 { |
185 return findMagicComment(content, "sourceURL", multiline, deprecated); | 193 return findMagicComment(content, "sourceURL", multiline, deprecated); |
186 } | 194 } |
187 | 195 |
188 String findSourceMapURL(const String& content, bool multiline, bool* deprecated) | 196 String16 findSourceMapURL(const String16& content, bool multiline, bool* depreca
ted) |
189 { | 197 { |
190 return findMagicComment(content, "sourceMappingURL", multiline, deprecated); | 198 return findMagicComment(content, "sourceMappingURL", multiline, deprecated); |
191 } | 199 } |
192 | 200 |
193 PassOwnPtr<protocol::Array<protocol::Debugger::SearchMatch>> searchInTextByLines
(V8Debugger* debugger, const String& text, const String& query, const bool caseS
ensitive, const bool isRegex) | 201 PassOwnPtr<protocol::Array<protocol::Debugger::SearchMatch>> searchInTextByLines
(V8Debugger* debugger, const String16& text, const String16& query, const bool c
aseSensitive, const bool isRegex) |
194 { | 202 { |
195 OwnPtr<protocol::Array<protocol::Debugger::SearchMatch>> result = protocol::
Array<protocol::Debugger::SearchMatch>::create(); | 203 OwnPtr<protocol::Array<protocol::Debugger::SearchMatch>> result = protocol::
Array<protocol::Debugger::SearchMatch>::create(); |
196 OwnPtr<V8Regex> regex = createSearchRegex(static_cast<V8DebuggerImpl*>(debug
ger), query, caseSensitive, isRegex); | 204 OwnPtr<V8Regex> regex = createSearchRegex(static_cast<V8DebuggerImpl*>(debug
ger), query, caseSensitive, isRegex); |
197 protocol::Vector<std::pair<int, String>> matches = scriptRegexpMatchesByLine
s(*regex.get(), text); | 205 protocol::Vector<std::pair<int, String16>> matches = scriptRegexpMatchesByLi
nes(*regex.get(), text); |
198 | 206 |
199 for (const auto& match : matches) | 207 for (const auto& match : matches) |
200 result->addItem(buildObjectForSearchMatch(match.first, match.second)); | 208 result->addItem(buildObjectForSearchMatch(match.first, match.second)); |
201 | 209 |
202 return result.release(); | 210 return result.release(); |
203 } | 211 } |
204 | 212 |
205 } // namespace V8ContentSearchUtil | 213 } // namespace V8ContentSearchUtil |
206 | 214 |
207 } // namespace blink | 215 } // namespace blink |
OLD | NEW |