LCOV - code coverage report
Current view: top level - src/inspector - string-util.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 57 83 68.7 %
Date: 2019-04-17 Functions: 16 22 72.7 %

          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 <cmath>
       8             : 
       9             : #include "src/base/platform/platform.h"
      10             : #include "src/conversions.h"
      11             : #include "src/inspector/protocol/Protocol.h"
      12             : 
      13             : namespace v8_inspector {
      14             : 
      15      882022 : v8::Local<v8::String> toV8String(v8::Isolate* isolate, const String16& string) {
      16      882022 :   if (string.isEmpty()) return v8::String::Empty(isolate);
      17             :   DCHECK_GT(v8::String::kMaxLength, string.length());
      18      878272 :   return v8::String::NewFromTwoByte(
      19             :              isolate, reinterpret_cast<const uint16_t*>(string.characters16()),
      20      878272 :              v8::NewStringType::kNormal, static_cast<int>(string.length()))
      21             :       .ToLocalChecked();
      22             : }
      23             : 
      24           0 : v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate,
      25             :                                              const String16& string) {
      26           0 :   if (string.isEmpty()) return v8::String::Empty(isolate);
      27             :   DCHECK_GT(v8::String::kMaxLength, string.length());
      28           0 :   return v8::String::NewFromTwoByte(
      29             :              isolate, reinterpret_cast<const uint16_t*>(string.characters16()),
      30             :              v8::NewStringType::kInternalized,
      31           0 :              static_cast<int>(string.length()))
      32             :       .ToLocalChecked();
      33             : }
      34             : 
      35       19659 : v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate,
      36             :                                              const char* str) {
      37       19659 :   return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kInternalized)
      38       19659 :       .ToLocalChecked();
      39             : }
      40             : 
      41           0 : v8::Local<v8::String> toV8String(v8::Isolate* isolate,
      42             :                                  const StringView& string) {
      43           0 :   if (!string.length()) return v8::String::Empty(isolate);
      44             :   DCHECK_GT(v8::String::kMaxLength, string.length());
      45           0 :   if (string.is8Bit())
      46           0 :     return v8::String::NewFromOneByte(
      47             :                isolate, reinterpret_cast<const uint8_t*>(string.characters8()),
      48           0 :                v8::NewStringType::kNormal, static_cast<int>(string.length()))
      49             :         .ToLocalChecked();
      50           0 :   return v8::String::NewFromTwoByte(
      51             :              isolate, reinterpret_cast<const uint16_t*>(string.characters16()),
      52           0 :              v8::NewStringType::kNormal, static_cast<int>(string.length()))
      53             :       .ToLocalChecked();
      54             : }
      55             : 
      56     9050489 : String16 toProtocolString(v8::Isolate* isolate, v8::Local<v8::String> value) {
      57    18100379 :   if (value.IsEmpty() || value->IsNullOrUndefined()) return String16();
      58     9049890 :   std::unique_ptr<UChar[]> buffer(new UChar[value->Length()]);
      59     9049890 :   value->Write(isolate, reinterpret_cast<uint16_t*>(buffer.get()), 0,
      60     9049890 :                value->Length());
      61     9049890 :   return String16(buffer.get(), value->Length());
      62             : }
      63             : 
      64     2286883 : String16 toProtocolStringWithTypeCheck(v8::Isolate* isolate,
      65             :                                        v8::Local<v8::Value> value) {
      66     4573766 :   if (value.IsEmpty() || !value->IsString()) return String16();
      67     2059591 :   return toProtocolString(isolate, value.As<v8::String>());
      68             : }
      69             : 
      70      351830 : String16 toString16(const StringView& string) {
      71      351830 :   if (!string.length()) return String16();
      72      338513 :   if (string.is8Bit())
      73             :     return String16(reinterpret_cast<const char*>(string.characters8()),
      74        1905 :                     string.length());
      75             :   return String16(reinterpret_cast<const UChar*>(string.characters16()),
      76      336608 :                   string.length());
      77             : }
      78             : 
      79      414550 : StringView toStringView(const String16& string) {
      80      744673 :   if (string.isEmpty()) return StringView();
      81             :   return StringView(reinterpret_cast<const uint16_t*>(string.characters16()),
      82             :                     string.length());
      83             : }
      84             : 
      85           0 : bool stringViewStartsWith(const StringView& string, const char* prefix) {
      86           0 :   if (!string.length()) return !(*prefix);
      87           0 :   if (string.is8Bit()) {
      88           0 :     for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) {
      89           0 :       if (string.characters8()[i] != prefix[j]) return false;
      90             :     }
      91             :   } else {
      92           0 :     for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) {
      93           0 :       if (string.characters16()[i] != prefix[j]) return false;
      94             :     }
      95             :   }
      96             :   return true;
      97             : }
      98             : 
      99             : namespace protocol {
     100             : 
     101             : // static
     102      349870 : double StringUtil::toDouble(const char* s, size_t len, bool* isOk) {
     103             :   int flags = v8::internal::ALLOW_HEX | v8::internal::ALLOW_OCTAL |
     104             :               v8::internal::ALLOW_BINARY;
     105      349870 :   double result = v8::internal::StringToDouble(s, flags);
     106      349870 :   *isOk = !std::isnan(result);
     107      349870 :   return result;
     108             : }
     109             : 
     110      155138 : std::unique_ptr<protocol::Value> StringUtil::parseJSON(
     111             :     const StringView& string) {
     112      155138 :   if (!string.length()) return nullptr;
     113      155018 :   if (string.is8Bit()) {
     114             :     return parseJSONCharacters(string.characters8(),
     115           0 :                                static_cast<int>(string.length()));
     116             :   }
     117             :   return parseJSONCharacters(string.characters16(),
     118      155018 :                              static_cast<int>(string.length()));
     119             : }
     120             : 
     121      150065 : std::unique_ptr<protocol::Value> StringUtil::parseJSON(const String16& string) {
     122      150065 :   if (!string.length()) return nullptr;
     123             :   return parseJSONCharacters(string.characters16(),
     124       93976 :                              static_cast<int>(string.length()));
     125             : }
     126             : 
     127             : // static
     128           0 : std::unique_ptr<protocol::Value> StringUtil::parseProtocolMessage(
     129             :     const ProtocolMessage& message) {
     130           0 :   return parseJSON(message.json);
     131             : }
     132             : 
     133             : // static
     134           0 : ProtocolMessage StringUtil::jsonToMessage(String message) {
     135             :   ProtocolMessage result;
     136             :   result.json = std::move(message);
     137           0 :   return result;
     138             : }
     139             : 
     140             : // static
     141           0 : ProtocolMessage StringUtil::binaryToMessage(std::vector<uint8_t> message) {
     142             :   ProtocolMessage result;
     143           0 :   result.binary = std::move(message);
     144           0 :   return result;
     145             : }
     146             : 
     147             : // static
     148    49683508 : void StringUtil::builderAppendQuotedString(StringBuilder& builder,
     149             :                                            const String& str) {
     150    49683508 :   builder.append('"');
     151    49683508 :   if (!str.isEmpty()) {
     152    49492013 :     escapeWideStringForJSON(
     153             :         reinterpret_cast<const uint16_t*>(str.characters16()),
     154    49492013 :         static_cast<int>(str.length()), &builder);
     155             :   }
     156    49683508 :   builder.append('"');
     157    49683508 : }
     158             : 
     159             : }  // namespace protocol
     160             : 
     161             : // static
     162      330003 : std::unique_ptr<StringBuffer> StringBuffer::create(const StringView& string) {
     163      330003 :   String16 owner = toString16(string);
     164      330003 :   return StringBufferImpl::adopt(owner);
     165             : }
     166             : 
     167             : // static
     168         120 : std::unique_ptr<StringBufferImpl> StringBufferImpl::adopt(String16& string) {
     169      330243 :   return std::unique_ptr<StringBufferImpl>(new StringBufferImpl(string));
     170             : }
     171             : 
     172      660246 : StringBufferImpl::StringBufferImpl(String16& string) {
     173             :   m_owner.swap(string);
     174      330123 :   m_string = toStringView(m_owner);
     175      330123 : }
     176             : 
     177        6892 : String16 debuggerIdToString(const std::pair<int64_t, int64_t>& debuggerId) {
     178             :   const size_t kBufferSize = 35;
     179             : 
     180             :   char buffer[kBufferSize];
     181             :   v8::base::OS::SNPrintF(buffer, kBufferSize, "(%08" PRIX64 "%08" PRIX64 ")",
     182        6892 :                          debuggerId.first, debuggerId.second);
     183        6892 :   return String16(buffer);
     184             : }
     185             : 
     186         250 : String16 stackTraceIdToString(uintptr_t id) {
     187         250 :   String16Builder builder;
     188         250 :   builder.appendNumber(static_cast<size_t>(id));
     189         500 :   return builder.toString();
     190             : }
     191             : 
     192      122004 : }  // namespace v8_inspector

Generated by: LCOV version 1.10