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/InspectorProtocol.h" | |
7 #include "platform/v8_inspector/V8InspectorImpl.h" | 8 #include "platform/v8_inspector/V8InspectorImpl.h" |
8 #include "platform/v8_inspector/V8InspectorSessionImpl.h" | 9 #include "platform/v8_inspector/V8InspectorSessionImpl.h" |
9 #include "platform/v8_inspector/V8Regex.h" | 10 #include "platform/v8_inspector/V8Regex.h" |
10 | 11 |
11 namespace v8_inspector { | 12 namespace v8_inspector { |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 String16 findMagicComment(const String16& content, const String16& name, bool mu ltiline) | 16 String16 findMagicComment(const String16& content, const String16& name, bool mu ltiline) |
16 { | 17 { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
160 if (string.isEmpty()) | 161 if (string.isEmpty()) |
161 return v8::String::Empty(isolate); | 162 return v8::String::Empty(isolate); |
162 return v8::String::NewFromTwoByte(isolate, reinterpret_cast<const uint16_t*> (string.characters16()), v8::NewStringType::kInternalized, string.length()).ToLo calChecked(); | 163 return v8::String::NewFromTwoByte(isolate, reinterpret_cast<const uint16_t*> (string.characters16()), v8::NewStringType::kInternalized, string.length()).ToLo calChecked(); |
163 } | 164 } |
164 | 165 |
165 v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate, const char* s tr) | 166 v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate, const char* s tr) |
166 { | 167 { |
167 return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kInternalize d).ToLocalChecked(); | 168 return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kInternalize d).ToLocalChecked(); |
168 } | 169 } |
169 | 170 |
171 v8::Local<v8::String> toV8String(v8::Isolate* isolate, const StringView& string) | |
172 { | |
173 if (!string.length()) | |
174 return v8::String::Empty(isolate); | |
esprehn
2016/08/23 17:35:39
this turns null strings into Empty(), maybe that's
dgozman
2016/08/24 00:45:00
Yes, empty v8 string is what we need here.
| |
175 if (string.is8Bit()) | |
176 return v8::String::NewFromOneByte(isolate, reinterpret_cast<const uint8_ t*>(string.characters8()), v8::NewStringType::kNormal, string.length()).ToLocalC hecked(); | |
177 return v8::String::NewFromTwoByte(isolate, reinterpret_cast<const uint16_t*> (string.characters16()), v8::NewStringType::kNormal, string.length()).ToLocalChe cked(); | |
178 } | |
179 | |
170 String16 toProtocolString(v8::Local<v8::String> value) | 180 String16 toProtocolString(v8::Local<v8::String> value) |
171 { | 181 { |
172 if (value.IsEmpty() || value->IsNull() || value->IsUndefined()) | 182 if (value.IsEmpty() || value->IsNull() || value->IsUndefined()) |
173 return String16(); | 183 return String16(); |
174 std::unique_ptr<UChar[]> buffer(new UChar[value->Length()]); | 184 std::unique_ptr<UChar[]> buffer(new UChar[value->Length()]); |
175 value->Write(reinterpret_cast<uint16_t*>(buffer.get()), 0, value->Length()); | 185 value->Write(reinterpret_cast<uint16_t*>(buffer.get()), 0, value->Length()); |
176 return String16(buffer.get(), value->Length()); | 186 return String16(buffer.get(), value->Length()); |
177 } | 187 } |
178 | 188 |
179 String16 toProtocolStringWithTypeCheck(v8::Local<v8::Value> value) | 189 String16 toProtocolStringWithTypeCheck(v8::Local<v8::Value> value) |
180 { | 190 { |
181 if (value.IsEmpty() || !value->IsString()) | 191 if (value.IsEmpty() || !value->IsString()) |
182 return String16(); | 192 return String16(); |
183 return toProtocolString(value.As<v8::String>()); | 193 return toProtocolString(value.As<v8::String>()); |
184 } | 194 } |
185 | 195 |
196 String16 toString16(const StringView& string) | |
197 { | |
198 if (!string.length()) | |
199 return String16(); | |
200 if (string.is8Bit()) | |
201 return String16(reinterpret_cast<const char*>(string.characters8()), str ing.length()); | |
202 return String16(reinterpret_cast<const UChar*>(string.characters16()), strin g.length()); | |
203 } | |
204 | |
205 StringView toStringView(const String16& string) | |
206 { | |
207 if (string.isEmpty()) | |
208 return StringView(); | |
209 return StringView(reinterpret_cast<const uint16_t*>(string.characters16()), string.length()); | |
210 } | |
211 | |
212 bool stringViewStartsWith(const StringView& string, const char* prefix) | |
213 { | |
214 if (!string.length()) | |
215 return !(*prefix); | |
216 if (string.is8Bit()) { | |
217 for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) { | |
218 if (string.characters8()[i] != prefix[j]) | |
219 return false; | |
220 } | |
221 } else { | |
222 for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) { | |
223 if (string.characters16()[i] != prefix[j]) | |
224 return false; | |
225 } | |
226 } | |
227 return true; | |
228 } | |
229 | |
230 std::unique_ptr<protocol::Value> parseJSON(const StringView& string) | |
231 { | |
232 if (!string.length()) | |
233 return nullptr; | |
234 if (string.is8Bit()) | |
235 return blink::protocol::parseJSON(string.characters8(), string.length()) ; | |
236 return blink::protocol::parseJSON(string.characters16(), string.length()); | |
237 } | |
238 | |
186 std::vector<std::unique_ptr<protocol::Debugger::SearchMatch>> searchInTextByLine sImpl(V8InspectorSession* session, const String16& text, const String16& query, const bool caseSensitive, const bool isRegex) | 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) |
187 { | 240 { |
188 std::unique_ptr<V8Regex> regex = createSearchRegex(static_cast<V8InspectorSe ssionImpl*>(session)->inspector(), query, caseSensitive, isRegex); | 241 std::unique_ptr<V8Regex> regex = createSearchRegex(static_cast<V8InspectorSe ssionImpl*>(session)->inspector(), query, caseSensitive, isRegex); |
189 std::vector<std::pair<int, String16>> matches = scriptRegexpMatchesByLines(* regex.get(), text); | 242 std::vector<std::pair<int, String16>> matches = scriptRegexpMatchesByLines(* regex.get(), text); |
190 | 243 |
191 std::vector<std::unique_ptr<protocol::Debugger::SearchMatch>> result; | 244 std::vector<std::unique_ptr<protocol::Debugger::SearchMatch>> result; |
192 for (const auto& match : matches) | 245 for (const auto& match : matches) |
193 result.push_back(buildObjectForSearchMatch(match.first, match.second)); | 246 result.push_back(buildObjectForSearchMatch(match.first, match.second)); |
194 return result; | 247 return result; |
195 } | 248 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
270 if (!propertyValue) | 323 if (!propertyValue) |
271 return nullptr; | 324 return nullptr; |
272 jsonObject->setValue(toProtocolString(propertyName), std::move(prope rtyValue)); | 325 jsonObject->setValue(toProtocolString(propertyName), std::move(prope rtyValue)); |
273 } | 326 } |
274 return std::move(jsonObject); | 327 return std::move(jsonObject); |
275 } | 328 } |
276 NOTREACHED(); | 329 NOTREACHED(); |
277 return nullptr; | 330 return nullptr; |
278 } | 331 } |
279 | 332 |
333 // static | |
334 std::unique_ptr<StringBuffer> StringBuffer::create(const StringView& string) | |
335 { | |
336 String16 owner = toString16(string); | |
337 return StringBufferImpl::adopt(owner); | |
338 } | |
339 | |
340 // static | |
341 std::unique_ptr<StringBufferImpl> StringBufferImpl::adopt(String16& string) | |
342 { | |
343 return wrapUnique(new StringBufferImpl(string)); | |
344 } | |
345 | |
346 StringBufferImpl::StringBufferImpl(String16& string) | |
347 { | |
348 m_owner.swap(string); | |
349 m_string = toStringView(m_owner); | |
350 } | |
351 | |
280 } // namespace v8_inspector | 352 } // namespace v8_inspector |
OLD | NEW |