V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
string-util.cc
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 v8::Local<v8::String> toV8String(v8::Isolate* isolate, const String16& string) {
14  if (string.isEmpty()) return v8::String::Empty(isolate);
15  DCHECK_GT(v8::String::kMaxLength, string.length());
17  isolate, reinterpret_cast<const uint16_t*>(string.characters16()),
18  v8::NewStringType::kNormal, static_cast<int>(string.length()))
19  .ToLocalChecked();
20 }
21 
22 v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate,
23  const String16& string) {
24  if (string.isEmpty()) return v8::String::Empty(isolate);
25  DCHECK_GT(v8::String::kMaxLength, string.length());
27  isolate, reinterpret_cast<const uint16_t*>(string.characters16()),
29  static_cast<int>(string.length()))
30  .ToLocalChecked();
31 }
32 
33 v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate,
34  const char* str) {
36  .ToLocalChecked();
37 }
38 
39 v8::Local<v8::String> toV8String(v8::Isolate* isolate,
40  const StringView& string) {
41  if (!string.length()) return v8::String::Empty(isolate);
42  DCHECK_GT(v8::String::kMaxLength, string.length());
43  if (string.is8Bit())
45  isolate, reinterpret_cast<const uint8_t*>(string.characters8()),
46  v8::NewStringType::kNormal, static_cast<int>(string.length()))
47  .ToLocalChecked();
49  isolate, reinterpret_cast<const uint16_t*>(string.characters16()),
50  v8::NewStringType::kNormal, static_cast<int>(string.length()))
51  .ToLocalChecked();
52 }
53 
54 String16 toProtocolString(v8::Isolate* isolate, v8::Local<v8::String> value) {
55  if (value.IsEmpty() || value->IsNullOrUndefined()) return String16();
56  std::unique_ptr<UChar[]> buffer(new UChar[value->Length()]);
57  value->Write(isolate, reinterpret_cast<uint16_t*>(buffer.get()), 0,
58  value->Length());
59  return String16(buffer.get(), value->Length());
60 }
61 
62 String16 toProtocolStringWithTypeCheck(v8::Isolate* isolate,
63  v8::Local<v8::Value> value) {
64  if (value.IsEmpty() || !value->IsString()) return String16();
65  return toProtocolString(isolate, value.As<v8::String>());
66 }
67 
68 String16 toString16(const StringView& string) {
69  if (!string.length()) return String16();
70  if (string.is8Bit())
71  return String16(reinterpret_cast<const char*>(string.characters8()),
72  string.length());
73  return String16(reinterpret_cast<const UChar*>(string.characters16()),
74  string.length());
75 }
76 
77 StringView toStringView(const String16& string) {
78  if (string.isEmpty()) return StringView();
79  return StringView(reinterpret_cast<const uint16_t*>(string.characters16()),
80  string.length());
81 }
82 
83 bool stringViewStartsWith(const StringView& string, const char* prefix) {
84  if (!string.length()) return !(*prefix);
85  if (string.is8Bit()) {
86  for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) {
87  if (string.characters8()[i] != prefix[j]) return false;
88  }
89  } else {
90  for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) {
91  if (string.characters16()[i] != prefix[j]) return false;
92  }
93  }
94  return true;
95 }
96 
97 namespace protocol {
98 
99 // static
100 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  double result = v8::internal::StringToDouble(s, flags);
104  *isOk = !std::isnan(result);
105  return result;
106 }
107 
108 std::unique_ptr<protocol::Value> StringUtil::parseJSON(
109  const StringView& string) {
110  if (!string.length()) return nullptr;
111  if (string.is8Bit()) {
112  return parseJSONCharacters(string.characters8(),
113  static_cast<int>(string.length()));
114  }
115  return parseJSONCharacters(string.characters16(),
116  static_cast<int>(string.length()));
117 }
118 
119 std::unique_ptr<protocol::Value> StringUtil::parseJSON(const String16& string) {
120  if (!string.length()) return nullptr;
121  return parseJSONCharacters(string.characters16(),
122  static_cast<int>(string.length()));
123 }
124 
125 // static
126 void StringUtil::builderAppendQuotedString(StringBuilder& builder,
127  const String& str) {
128  builder.append('"');
129  if (!str.isEmpty()) {
130  escapeWideStringForJSON(
131  reinterpret_cast<const uint16_t*>(str.characters16()),
132  static_cast<int>(str.length()), &builder);
133  }
134  builder.append('"');
135 }
136 
137 } // namespace protocol
138 
139 // static
140 std::unique_ptr<StringBuffer> StringBuffer::create(const StringView& string) {
141  String16 owner = toString16(string);
142  return StringBufferImpl::adopt(owner);
143 }
144 
145 // static
146 std::unique_ptr<StringBufferImpl> StringBufferImpl::adopt(String16& string) {
147  return std::unique_ptr<StringBufferImpl>(new StringBufferImpl(string));
148 }
149 
150 StringBufferImpl::StringBufferImpl(String16& string) {
151  m_owner.swap(string);
152  m_string = toStringView(m_owner);
153 }
154 
155 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  debuggerId.first, debuggerId.second);
161  return String16(buffer);
162 }
163 
164 String16 stackTraceIdToString(uintptr_t id) {
165  String16Builder builder;
166  builder.appendNumber(static_cast<size_t>(id));
167  return builder.toString();
168 }
169 
170 } // namespace v8_inspector
static V8_WARN_UNUSED_RESULT MaybeLocal< String > NewFromOneByte(Isolate *isolate, const uint8_t *data, v8::NewStringType type, int length=-1)
Definition: api.cc:6528
static V8_WARN_UNUSED_RESULT MaybeLocal< String > NewFromTwoByte(Isolate *isolate, const uint16_t *data, v8::NewStringType type, int length=-1)
Definition: api.cc:6535
static V8_INLINE Local< String > Empty(Isolate *isolate)
Definition: v8.h:9913
static V8_WARN_UNUSED_RESULT MaybeLocal< String > NewFromUtf8(Isolate *isolate, const char *data, v8::NewStringType type, int length=-1)
Definition: api.cc:6511