Line data Source code
1 : // Copyright 2016 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/inspector/string-util.h"
6 :
7 : #include "src/inspector/protocol/Protocol.h"
8 :
9 : namespace v8_inspector {
10 :
11 1293560 : v8::Local<v8::String> toV8String(v8::Isolate* isolate, const String16& string) {
12 1293560 : if (string.isEmpty()) return v8::String::Empty(isolate);
13 : DCHECK(string.length() < v8::String::kMaxLength);
14 : return v8::String::NewFromTwoByte(
15 : isolate, reinterpret_cast<const uint16_t*>(string.characters16()),
16 1258575 : v8::NewStringType::kNormal, static_cast<int>(string.length()))
17 1258575 : .ToLocalChecked();
18 : }
19 :
20 0 : v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate,
21 : const String16& string) {
22 0 : if (string.isEmpty()) return v8::String::Empty(isolate);
23 : DCHECK(string.length() < v8::String::kMaxLength);
24 : return v8::String::NewFromTwoByte(
25 : isolate, reinterpret_cast<const uint16_t*>(string.characters16()),
26 : v8::NewStringType::kInternalized,
27 0 : static_cast<int>(string.length()))
28 0 : .ToLocalChecked();
29 : }
30 :
31 12071848 : v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate,
32 : const char* str) {
33 : return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kInternalized)
34 24143696 : .ToLocalChecked();
35 : }
36 :
37 0 : v8::Local<v8::String> toV8String(v8::Isolate* isolate,
38 0 : const StringView& string) {
39 0 : if (!string.length()) return v8::String::Empty(isolate);
40 : DCHECK(string.length() < v8::String::kMaxLength);
41 0 : if (string.is8Bit())
42 : return v8::String::NewFromOneByte(
43 : isolate, reinterpret_cast<const uint8_t*>(string.characters8()),
44 0 : v8::NewStringType::kNormal, static_cast<int>(string.length()))
45 0 : .ToLocalChecked();
46 : return v8::String::NewFromTwoByte(
47 : isolate, reinterpret_cast<const uint16_t*>(string.characters16()),
48 0 : v8::NewStringType::kNormal, static_cast<int>(string.length()))
49 0 : .ToLocalChecked();
50 : }
51 :
52 66615787 : String16 toProtocolString(v8::Local<v8::String> value) {
53 133231052 : if (value.IsEmpty() || value->IsNullOrUndefined()) return String16();
54 66615265 : std::unique_ptr<UChar[]> buffer(new UChar[value->Length()]);
55 66615265 : value->Write(reinterpret_cast<uint16_t*>(buffer.get()), 0, value->Length());
56 66615265 : return String16(buffer.get(), value->Length());
57 : }
58 :
59 3782693 : String16 toProtocolStringWithTypeCheck(v8::Local<v8::Value> value) {
60 7565386 : if (value.IsEmpty() || !value->IsString()) return String16();
61 3782693 : return toProtocolString(value.As<v8::String>());
62 : }
63 :
64 1264545 : String16 toString16(const StringView& string) {
65 431485 : if (!string.length()) return String16();
66 416530 : if (string.is8Bit())
67 : return String16(reinterpret_cast<const char*>(string.characters8()),
68 1482 : string.length());
69 : return String16(reinterpret_cast<const UChar*>(string.characters16()),
70 : string.length());
71 : }
72 :
73 430396 : StringView toStringView(const String16& string) {
74 838944 : if (string.isEmpty()) return StringView();
75 : return StringView(reinterpret_cast<const uint16_t*>(string.characters16()),
76 : string.length());
77 : }
78 :
79 0 : bool stringViewStartsWith(const StringView& string, const char* prefix) {
80 0 : if (!string.length()) return !(*prefix);
81 0 : if (string.is8Bit()) {
82 0 : for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) {
83 0 : if (string.characters8()[i] != prefix[j]) return false;
84 : }
85 : } else {
86 0 : for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) {
87 0 : if (string.characters16()[i] != prefix[j]) return false;
88 : }
89 : }
90 : return true;
91 : }
92 :
93 : namespace protocol {
94 :
95 209799 : std::unique_ptr<protocol::Value> StringUtil::parseJSON(
96 629121 : const StringView& string) {
97 209799 : if (!string.length()) return nullptr;
98 209661 : if (string.is8Bit()) {
99 : return parseJSONCharacters(string.characters8(),
100 0 : static_cast<int>(string.length()));
101 : }
102 : return parseJSONCharacters(string.characters16(),
103 419322 : static_cast<int>(string.length()));
104 : }
105 :
106 172732 : std::unique_ptr<protocol::Value> StringUtil::parseJSON(const String16& string) {
107 172732 : if (!string.length()) return nullptr;
108 : return parseJSONCharacters(string.characters16(),
109 136162 : static_cast<int>(string.length()));
110 : }
111 :
112 : } // namespace protocol
113 :
114 : // static
115 408494 : std::unique_ptr<StringBuffer> StringBuffer::create(const StringView& string) {
116 408494 : String16 owner = toString16(string);
117 1225482 : return StringBufferImpl::adopt(owner);
118 : }
119 :
120 : // static
121 408548 : std::unique_ptr<StringBufferImpl> StringBufferImpl::adopt(String16& string) {
122 817096 : return std::unique_ptr<StringBufferImpl>(new StringBufferImpl(string));
123 : }
124 :
125 817096 : StringBufferImpl::StringBufferImpl(String16& string) {
126 : m_owner.swap(string);
127 408548 : m_string = toStringView(m_owner);
128 408548 : }
129 :
130 : } // namespace v8_inspector
|