5 #include "src/inspector/string-util.h" 7 #include "src/base/platform/platform.h" 8 #include "src/conversions.h" 9 #include "src/inspector/protocol/Protocol.h" 15 DCHECK_GT(v8::String::kMaxLength,
string.length());
17 isolate, reinterpret_cast<const uint16_t*>(
string.characters16()),
23 const String16&
string) {
25 DCHECK_GT(v8::String::kMaxLength,
string.length());
27 isolate, reinterpret_cast<const uint16_t*>(
string.characters16()),
29 static_cast<int>(
string.length()))
40 const StringView&
string) {
42 DCHECK_GT(v8::String::kMaxLength,
string.length());
45 isolate, reinterpret_cast<const uint8_t*>(
string.characters8()),
49 isolate, reinterpret_cast<const uint16_t*>(
string.characters16()),
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,
59 return String16(buffer.get(), value->Length());
62 String16 toProtocolStringWithTypeCheck(v8::Isolate* isolate,
64 if (value.IsEmpty() || !value->IsString())
return String16();
65 return toProtocolString(isolate, value.As<
v8::String>());
68 String16 toString16(
const StringView&
string) {
69 if (!
string.length())
return String16();
71 return String16(reinterpret_cast<const char*>(
string.characters8()),
73 return String16(reinterpret_cast<const UChar*>(
string.characters16()),
77 StringView toStringView(
const String16&
string) {
78 if (
string.isEmpty())
return StringView();
79 return StringView(reinterpret_cast<const uint16_t*>(
string.characters16()),
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;
90 for (
size_t i = 0, j = 0; prefix[j] &&
i <
string.length(); ++
i, ++j) {
91 if (
string.characters16()[
i] != prefix[j])
return false;
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);
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()));
115 return parseJSONCharacters(
string.characters16(),
116 static_cast<int>(
string.length()));
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()));
126 void StringUtil::builderAppendQuotedString(StringBuilder& builder,
129 if (!str.isEmpty()) {
130 escapeWideStringForJSON(
131 reinterpret_cast<const uint16_t*>(str.characters16()),
132 static_cast<int>(str.length()), &builder);
140 std::unique_ptr<StringBuffer> StringBuffer::create(
const StringView&
string) {
141 String16 owner = toString16(
string);
142 return StringBufferImpl::adopt(owner);
146 std::unique_ptr<StringBufferImpl> StringBufferImpl::adopt(String16&
string) {
147 return std::unique_ptr<StringBufferImpl>(
new StringBufferImpl(
string));
150 StringBufferImpl::StringBufferImpl(String16&
string) {
151 m_owner.swap(
string);
152 m_string = toStringView(m_owner);
155 String16 debuggerIdToString(
const std::pair<int64_t, int64_t>& debuggerId) {
156 const size_t kBufferSize = 35;
158 char buffer[kBufferSize];
159 v8::base::OS::SNPrintF(buffer, kBufferSize,
"(%08" PRIX64
"%08" PRIX64
")",
160 debuggerId.first, debuggerId.second);
161 return String16(buffer);
164 String16 stackTraceIdToString(
uintptr_t id) {
165 String16Builder builder;
166 builder.appendNumber(static_cast<size_t>(
id));
167 return builder.toString();
static V8_WARN_UNUSED_RESULT MaybeLocal< String > NewFromOneByte(Isolate *isolate, const uint8_t *data, v8::NewStringType type, int length=-1)
static V8_WARN_UNUSED_RESULT MaybeLocal< String > NewFromTwoByte(Isolate *isolate, const uint16_t *data, v8::NewStringType type, int length=-1)
static V8_INLINE Local< String > Empty(Isolate *isolate)
static V8_WARN_UNUSED_RESULT MaybeLocal< String > NewFromUtf8(Isolate *isolate, const char *data, v8::NewStringType type, int length=-1)