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/base/platform/platform.h"
8 : #include "src/conversions.h"
9 : #include "src/inspector/protocol/Protocol.h"
10 :
11 : namespace v8_inspector {
12 :
13 881359 : v8::Local<v8::String> toV8String(v8::Isolate* isolate, const String16& string) {
14 881359 : if (string.isEmpty()) return v8::String::Empty(isolate);
15 : DCHECK_GT(v8::String::kMaxLength, string.length());
16 : return v8::String::NewFromTwoByte(
17 : isolate, reinterpret_cast<const uint16_t*>(string.characters16()),
18 877484 : v8::NewStringType::kNormal, static_cast<int>(string.length()))
19 877484 : .ToLocalChecked();
20 : }
21 :
22 0 : v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate,
23 : const String16& string) {
24 0 : if (string.isEmpty()) return v8::String::Empty(isolate);
25 : DCHECK_GT(v8::String::kMaxLength, string.length());
26 : return v8::String::NewFromTwoByte(
27 : isolate, reinterpret_cast<const uint16_t*>(string.characters16()),
28 : v8::NewStringType::kInternalized,
29 0 : static_cast<int>(string.length()))
30 0 : .ToLocalChecked();
31 : }
32 :
33 19568 : v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate,
34 : const char* str) {
35 : return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kInternalized)
36 39136 : .ToLocalChecked();
37 : }
38 :
39 0 : v8::Local<v8::String> toV8String(v8::Isolate* isolate,
40 0 : const StringView& string) {
41 0 : if (!string.length()) return v8::String::Empty(isolate);
42 : DCHECK_GT(v8::String::kMaxLength, string.length());
43 0 : if (string.is8Bit())
44 : return v8::String::NewFromOneByte(
45 : isolate, reinterpret_cast<const uint8_t*>(string.characters8()),
46 0 : v8::NewStringType::kNormal, static_cast<int>(string.length()))
47 0 : .ToLocalChecked();
48 : return v8::String::NewFromTwoByte(
49 : isolate, reinterpret_cast<const uint16_t*>(string.characters16()),
50 0 : v8::NewStringType::kNormal, static_cast<int>(string.length()))
51 0 : .ToLocalChecked();
52 : }
53 :
54 9005094 : String16 toProtocolString(v8::Isolate* isolate, v8::Local<v8::String> value) {
55 18009519 : if (value.IsEmpty() || value->IsNullOrUndefined()) return String16();
56 9004425 : std::unique_ptr<UChar[]> buffer(new UChar[value->Length()]);
57 : value->Write(isolate, reinterpret_cast<uint16_t*>(buffer.get()), 0,
58 9004425 : value->Length());
59 9004425 : return String16(buffer.get(), value->Length());
60 : }
61 :
62 2267350 : String16 toProtocolStringWithTypeCheck(v8::Isolate* isolate,
63 : v8::Local<v8::Value> value) {
64 4534700 : if (value.IsEmpty() || !value->IsString()) return String16();
65 2041354 : return toProtocolString(isolate, value.As<v8::String>());
66 : }
67 :
68 1083307 : String16 toString16(const StringView& string) {
69 369867 : if (!string.length()) return String16();
70 356720 : if (string.is8Bit())
71 : return String16(reinterpret_cast<const char*>(string.characters8()),
72 1745 : string.length());
73 : return String16(reinterpret_cast<const UChar*>(string.characters16()),
74 354975 : string.length());
75 : }
76 :
77 432121 : StringView toStringView(const String16& string) {
78 780841 : if (string.isEmpty()) return StringView();
79 : return StringView(reinterpret_cast<const uint16_t*>(string.characters16()),
80 : string.length());
81 : }
82 :
83 0 : bool stringViewStartsWith(const StringView& string, const char* prefix) {
84 0 : if (!string.length()) return !(*prefix);
85 0 : if (string.is8Bit()) {
86 0 : for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) {
87 0 : if (string.characters8()[i] != prefix[j]) return false;
88 : }
89 : } else {
90 0 : for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) {
91 0 : if (string.characters16()[i] != prefix[j]) return false;
92 : }
93 : }
94 : return true;
95 : }
96 :
97 : namespace protocol {
98 :
99 : // static
100 363027 : double StringUtil::toDouble(const char* s, size_t len, bool* isOk) {
101 : int flags = v8::internal::ALLOW_HEX | v8::internal::ALLOW_OCTAL |
102 : v8::internal::ALLOW_BINARY;
103 363027 : double result = v8::internal::StringToDouble(s, flags);
104 363027 : *isOk = !std::isnan(result);
105 363027 : return result;
106 : }
107 :
108 169424 : std::unique_ptr<protocol::Value> StringUtil::parseJSON(
109 508032 : const StringView& string) {
110 169424 : if (!string.length()) return nullptr;
111 169304 : if (string.is8Bit()) {
112 : return parseJSONCharacters(string.characters8(),
113 0 : static_cast<int>(string.length()));
114 : }
115 : return parseJSONCharacters(string.characters16(),
116 338608 : static_cast<int>(string.length()));
117 : }
118 :
119 153704 : std::unique_ptr<protocol::Value> StringUtil::parseJSON(const String16& string) {
120 153704 : if (!string.length()) return nullptr;
121 : return parseJSONCharacters(string.characters16(),
122 93281 : static_cast<int>(string.length()));
123 : }
124 :
125 : // static
126 49580378 : void StringUtil::builderAppendQuotedString(StringBuilder& builder,
127 : const String& str) {
128 49580378 : builder.append('"');
129 49580378 : if (!str.isEmpty()) {
130 : escapeWideStringForJSON(
131 : reinterpret_cast<const uint16_t*>(str.characters16()),
132 49370938 : static_cast<int>(str.length()), &builder);
133 : }
134 49580378 : builder.append('"');
135 49580378 : }
136 :
137 : } // namespace protocol
138 :
139 : // static
140 348600 : std::unique_ptr<StringBuffer> StringBuffer::create(const StringView& string) {
141 348600 : String16 owner = toString16(string);
142 1045800 : return StringBufferImpl::adopt(owner);
143 : }
144 :
145 : // static
146 348720 : std::unique_ptr<StringBufferImpl> StringBufferImpl::adopt(String16& string) {
147 697440 : return std::unique_ptr<StringBufferImpl>(new StringBufferImpl(string));
148 : }
149 :
150 697440 : StringBufferImpl::StringBufferImpl(String16& string) {
151 : m_owner.swap(string);
152 348720 : m_string = toStringView(m_owner);
153 348720 : }
154 :
155 6822 : String16 debuggerIdToString(const std::pair<int64_t, int64_t>& debuggerId) {
156 : const size_t kBufferSize = 35;
157 :
158 : char buffer[kBufferSize];
159 : v8::base::OS::SNPrintF(buffer, kBufferSize, "(%08" PRIX64 "%08" PRIX64 ")",
160 6822 : debuggerId.first, debuggerId.second);
161 6822 : return String16(buffer);
162 : }
163 :
164 235 : String16 stackTraceIdToString(uintptr_t id) {
165 235 : String16Builder builder;
166 235 : builder.appendNumber(static_cast<size_t>(id));
167 470 : return builder.toString();
168 : }
169 :
170 183867 : } // namespace v8_inspector
|