Home | History | Annotate | Download | only in inspector
      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->IsNullOrUndefined()) return String16();
     54   std::unique_ptr<UChar[]> buffer(new UChar[value->Length()]);
     55   value->Write(reinterpret_cast<uint16_t*>(buffer.get()), 0, value->Length());
     56   return String16(buffer.get(), value->Length());
     57 }
     58 
     59 String16 toProtocolStringWithTypeCheck(v8::Local<v8::Value> value) {
     60   if (value.IsEmpty() || !value->IsString()) return String16();
     61   return toProtocolString(value.As<v8::String>());
     62 }
     63 
     64 String16 toString16(const StringView& string) {
     65   if (!string.length()) return String16();
     66   if (string.is8Bit())
     67     return String16(reinterpret_cast<const char*>(string.characters8()),
     68                     string.length());
     69   return String16(reinterpret_cast<const UChar*>(string.characters16()),
     70                   string.length());
     71 }
     72 
     73 StringView toStringView(const String16& string) {
     74   if (string.isEmpty()) return StringView();
     75   return StringView(reinterpret_cast<const uint16_t*>(string.characters16()),
     76                     string.length());
     77 }
     78 
     79 bool stringViewStartsWith(const StringView& string, const char* prefix) {
     80   if (!string.length()) return !(*prefix);
     81   if (string.is8Bit()) {
     82     for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) {
     83       if (string.characters8()[i] != prefix[j]) return false;
     84     }
     85   } else {
     86     for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) {
     87       if (string.characters16()[i] != prefix[j]) return false;
     88     }
     89   }
     90   return true;
     91 }
     92 
     93 namespace protocol {
     94 
     95 std::unique_ptr<protocol::Value> StringUtil::parseJSON(
     96     const StringView& string) {
     97   if (!string.length()) return nullptr;
     98   if (string.is8Bit()) {
     99     return parseJSONCharacters(string.characters8(),
    100                                static_cast<int>(string.length()));
    101   }
    102   return parseJSONCharacters(string.characters16(),
    103                              static_cast<int>(string.length()));
    104 }
    105 
    106 std::unique_ptr<protocol::Value> StringUtil::parseJSON(const String16& string) {
    107   if (!string.length()) return nullptr;
    108   return parseJSONCharacters(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 std::unique_ptr<StringBufferImpl>(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