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 v8::Local<v8::String> toV8String(v8::Isolate* isolate, const String16& string) { 12 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 v8::NewStringType::kNormal, static_cast<int>(string.length())) 17 .ToLocalChecked(); 18 } 19 20 v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate, 21 const String16& string) { 22 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 static_cast<int>(string.length())) 28 .ToLocalChecked(); 29 } 30 31 v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate, 32 const char* str) { 33 return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kInternalized) 34 .ToLocalChecked(); 35 } 36 37 v8::Local<v8::String> toV8String(v8::Isolate* isolate, 38 const StringView& string) { 39 if (!string.length()) return v8::String::Empty(isolate); 40 DCHECK(string.length() < v8::String::kMaxLength); 41 if (string.is8Bit()) 42 return v8::String::NewFromOneByte( 43 isolate, reinterpret_cast<const uint8_t*>(string.characters8()), 44 v8::NewStringType::kNormal, static_cast<int>(string.length())) 45 .ToLocalChecked(); 46 return v8::String::NewFromTwoByte( 47 isolate, reinterpret_cast<const uint16_t*>(string.characters16()), 48 v8::NewStringType::kNormal, static_cast<int>(string.length())) 49 .ToLocalChecked(); 50 } 51 52 String16 toProtocolString(v8::Local<v8::String> value) { 53 if (value.IsEmpty() || value->IsNull() || value->IsUndefined()) 54 return String16(); 55 std::unique_ptr<UChar[]> buffer(new UChar[value->Length()]); 56 value->Write(reinterpret_cast<uint16_t*>(buffer.get()), 0, value->Length()); 57 return String16(buffer.get(), value->Length()); 58 } 59 60 String16 toProtocolStringWithTypeCheck(v8::Local<v8::Value> value) { 61 if (value.IsEmpty() || !value->IsString()) return String16(); 62 return toProtocolString(value.As<v8::String>()); 63 } 64 65 String16 toString16(const StringView& string) { 66 if (!string.length()) return String16(); 67 if (string.is8Bit()) 68 return String16(reinterpret_cast<const char*>(string.characters8()), 69 string.length()); 70 return String16(reinterpret_cast<const UChar*>(string.characters16()), 71 string.length()); 72 } 73 74 StringView toStringView(const String16& string) { 75 if (string.isEmpty()) return StringView(); 76 return StringView(reinterpret_cast<const uint16_t*>(string.characters16()), 77 string.length()); 78 } 79 80 bool stringViewStartsWith(const StringView& string, const char* prefix) { 81 if (!string.length()) return !(*prefix); 82 if (string.is8Bit()) { 83 for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) { 84 if (string.characters8()[i] != prefix[j]) return false; 85 } 86 } else { 87 for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) { 88 if (string.characters16()[i] != prefix[j]) return false; 89 } 90 } 91 return true; 92 } 93 94 namespace protocol { 95 96 std::unique_ptr<protocol::Value> parseJSON(const StringView& string) { 97 if (!string.length()) return nullptr; 98 if (string.is8Bit()) { 99 return protocol::parseJSON(string.characters8(), 100 static_cast<int>(string.length())); 101 } 102 return protocol::parseJSON(string.characters16(), 103 static_cast<int>(string.length())); 104 } 105 106 std::unique_ptr<protocol::Value> parseJSON(const String16& string) { 107 if (!string.length()) return nullptr; 108 return protocol::parseJSON(string.characters16(), 109 static_cast<int>(string.length())); 110 } 111 112 } // namespace protocol 113 114 // static 115 std::unique_ptr<StringBuffer> StringBuffer::create(const StringView& string) { 116 String16 owner = toString16(string); 117 return StringBufferImpl::adopt(owner); 118 } 119 120 // static 121 std::unique_ptr<StringBufferImpl> StringBufferImpl::adopt(String16& string) { 122 return wrapUnique(new StringBufferImpl(string)); 123 } 124 125 StringBufferImpl::StringBufferImpl(String16& string) { 126 m_owner.swap(string); 127 m_string = toStringView(m_owner); 128 } 129 130 } // namespace v8_inspector 131