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/conversions.h"
8 : #include "src/inspector/protocol/Protocol.h"
9 : #include "src/unicode-cache.h"
10 :
11 : namespace v8_inspector {
12 :
13 1133208 : v8::Local<v8::String> toV8String(v8::Isolate* isolate, const String16& string) {
14 1133208 : 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 1101472 : v8::NewStringType::kNormal, static_cast<int>(string.length()))
19 1101472 : .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 7090269 : v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate,
34 : const char* str) {
35 : return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kInternalized)
36 14180538 : .ToLocalChecked();
37 : }
38 :
39 2944 : v8::Local<v8::String> toV8String(v8::Isolate* isolate,
40 8832 : const StringView& string) {
41 2944 : if (!string.length()) return v8::String::Empty(isolate);
42 : DCHECK_GT(v8::String::kMaxLength, string.length());
43 2944 : if (string.is8Bit())
44 : return v8::String::NewFromOneByte(
45 : isolate, reinterpret_cast<const uint8_t*>(string.characters8()),
46 2944 : v8::NewStringType::kNormal, static_cast<int>(string.length()))
47 2944 : .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 41157435 : String16 toProtocolString(v8::Local<v8::String> value) {
55 82314351 : if (value.IsEmpty() || value->IsNullOrUndefined()) return String16();
56 41156916 : std::unique_ptr<UChar[]> buffer(new UChar[value->Length()]);
57 41156916 : value->Write(reinterpret_cast<uint16_t*>(buffer.get()), 0, value->Length());
58 41156916 : return String16(buffer.get(), value->Length());
59 : }
60 :
61 2630286 : String16 toProtocolStringWithTypeCheck(v8::Local<v8::Value> value) {
62 5260572 : if (value.IsEmpty() || !value->IsString()) return String16();
63 2630286 : return toProtocolString(value.As<v8::String>());
64 : }
65 :
66 853942 : String16 toString16(const StringView& string) {
67 292296 : if (!string.length()) return String16();
68 280823 : if (string.is8Bit())
69 : return String16(reinterpret_cast<const char*>(string.characters8()),
70 1465 : string.length());
71 : return String16(reinterpret_cast<const UChar*>(string.characters16()),
72 279358 : string.length());
73 : }
74 :
75 292823 : StringView toStringView(const String16& string) {
76 566458 : if (string.isEmpty()) return StringView();
77 : return StringView(reinterpret_cast<const uint16_t*>(string.characters16()),
78 : string.length());
79 : }
80 :
81 0 : bool stringViewStartsWith(const StringView& string, const char* prefix) {
82 0 : if (!string.length()) return !(*prefix);
83 0 : if (string.is8Bit()) {
84 0 : for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) {
85 0 : if (string.characters8()[i] != prefix[j]) return false;
86 : }
87 : } else {
88 0 : for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) {
89 0 : if (string.characters16()[i] != prefix[j]) return false;
90 : }
91 : }
92 : return true;
93 : }
94 :
95 : namespace protocol {
96 :
97 : // static
98 323431 : double StringUtil::toDouble(const char* s, size_t len, bool* isOk) {
99 323431 : v8::internal::UnicodeCache unicode_cache;
100 : int flags = v8::internal::ALLOW_HEX | v8::internal::ALLOW_OCTAL |
101 : v8::internal::ALLOW_BINARY;
102 323431 : double result = StringToDouble(&unicode_cache, s, flags);
103 323431 : *isOk = !std::isnan(result);
104 323431 : return result;
105 : }
106 :
107 140528 : std::unique_ptr<protocol::Value> StringUtil::parseJSON(
108 421354 : const StringView& string) {
109 140528 : if (!string.length()) return nullptr;
110 140413 : if (string.is8Bit()) {
111 : return parseJSONCharacters(string.characters8(),
112 0 : static_cast<int>(string.length()));
113 : }
114 : return parseJSONCharacters(string.characters16(),
115 280826 : static_cast<int>(string.length()));
116 : }
117 :
118 114127 : std::unique_ptr<protocol::Value> StringUtil::parseJSON(const String16& string) {
119 114127 : if (!string.length()) return nullptr;
120 : return parseJSONCharacters(string.characters16(),
121 88162 : static_cast<int>(string.length()));
122 : }
123 :
124 : // static
125 44353129 : void StringUtil::builderAppendQuotedString(StringBuilder& builder,
126 : const String& str) {
127 44353129 : builder.append('"');
128 44353129 : if (!str.isEmpty()) {
129 : escapeWideStringForJSON(
130 : reinterpret_cast<const uint16_t*>(str.characters16()),
131 44224640 : static_cast<int>(str.length()), &builder);
132 : }
133 44353129 : builder.append('"');
134 44353129 : }
135 :
136 : } // namespace protocol
137 :
138 : // static
139 273530 : std::unique_ptr<StringBuffer> StringBuffer::create(const StringView& string) {
140 273530 : String16 owner = toString16(string);
141 820590 : return StringBufferImpl::adopt(owner);
142 : }
143 :
144 : // static
145 273635 : std::unique_ptr<StringBufferImpl> StringBufferImpl::adopt(String16& string) {
146 547270 : return std::unique_ptr<StringBufferImpl>(new StringBufferImpl(string));
147 : }
148 :
149 547270 : StringBufferImpl::StringBufferImpl(String16& string) {
150 : m_owner.swap(string);
151 273635 : m_string = toStringView(m_owner);
152 273635 : }
153 :
154 : } // namespace v8_inspector
|