LCOV - code coverage report
Current view: top level - src/inspector - string-util.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 55 82 67.1 %
Date: 2019-02-19 Functions: 17 23 73.9 %

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

Generated by: LCOV version 1.10