string-util.h 5.14 KB
Newer Older
1 2 3 4
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef V8_INSPECTOR_STRING_UTIL_H_
#define V8_INSPECTOR_STRING_UTIL_H_
7

8
#include <stdint.h>
9 10 11
#include <memory>

#include "src/base/logging.h"
12
#include "src/base/macros.h"
13
#include "src/inspector/string-16.h"
14

15
#include "include/v8-inspector.h"
16 17 18 19 20 21 22 23 24

namespace v8_inspector {

namespace protocol {

class Value;

using String = v8_inspector::String16;
using StringBuilder = v8_inspector::String16Builder;
25 26 27 28
struct ProtocolMessage {
  String json;
  std::vector<uint8_t> binary;
};
29 30 31

class StringUtil {
 public:
32
  static String substring(const String& s, size_t pos, size_t len) {
33 34 35
    return s.substring(pos, len);
  }
  static String fromInteger(int number) { return String::fromInteger(number); }
36 37 38
  static String fromInteger(size_t number) {
    return String::fromInteger(number);
  }
39
  static String fromDouble(double number) { return String::fromDouble(number); }
40
  static double toDouble(const char* s, size_t len, bool* isOk);
41 42 43 44 45 46
  static size_t find(const String& s, const char* needle) {
    return s.find(needle);
  }
  static size_t find(const String& s, const String& needle) {
    return s.find(needle);
  }
47
  static const size_t kNotFound = String::kNotFound;
48 49 50
  static void builderAppend(
      StringBuilder& builder,  // NOLINT(runtime/references)
      const String& s) {
51 52
    builder.append(s);
  }
53 54 55
  static void builderAppend(
      StringBuilder& builder,  // NOLINT(runtime/references)
      UChar c) {
56 57
    builder.append(c);
  }
58 59 60
  static void builderAppend(
      StringBuilder& builder,  // NOLINT(runtime/references)
      const char* s, size_t len) {
61 62
    builder.append(s, len);
  }
63
  static void builderAppendQuotedString(StringBuilder&, const String&);
64 65 66
  static void builderReserve(
      StringBuilder& builder,  // NOLINT(runtime/references)
      size_t capacity) {
67 68
    builder.reserveCapacity(capacity);
  }
69 70
  static String builderToString(
      StringBuilder& builder) {  // NOLINT(runtime/references)
71 72
    return builder.toString();
  }
73 74
  static std::unique_ptr<protocol::Value> parseJSON(const String16& json);
  static std::unique_ptr<protocol::Value> parseJSON(const StringView& json);
75 76
  static ProtocolMessage jsonToMessage(String message);
  static ProtocolMessage binaryToMessage(std::vector<uint8_t> message);
77 78 79 80

  static String fromUTF8(const uint8_t* data, size_t length) {
    return String16::fromUTF8(reinterpret_cast<const char*>(data), length);
  }
81 82 83 84 85

  static String fromUTF16(const uint16_t* data, size_t length) {
    return String16(data, length);
  }

86 87 88 89
  static String fromUTF16LE(const uint16_t* data, size_t length) {
    return String16::fromUTF16LE(data, length);
  }

90 91 92 93
  static const uint8_t* CharactersLatin1(const String& s) { return nullptr; }
  static const uint8_t* CharactersUTF8(const String& s) { return nullptr; }
  static const uint16_t* CharactersUTF16(const String& s) {
    return s.characters16();
94
  }
95
  static size_t CharacterCount(const String& s) { return s.length(); }
96 97
};

98 99 100 101 102 103 104 105 106
// A read-only sequence of uninterpreted bytes with reference-counted storage.
// Though the templates for generating the protocol bindings reference
// this type, js_protocol.pdl doesn't have a field of type 'binary', so
// therefore it's unnecessary to provide an implementation here.
class Binary {
 public:
  const uint8_t* data() const { UNIMPLEMENTED(); }
  size_t size() const { UNIMPLEMENTED(); }
  String toBase64() const { UNIMPLEMENTED(); }
107
  static Binary fromBase64(const String& base64, bool* success) {
108 109
    UNIMPLEMENTED();
  }
110
  static Binary fromSpan(const uint8_t* data, size_t size) { UNIMPLEMENTED(); }
111
};
112 113 114 115 116 117 118
}  // namespace protocol

v8::Local<v8::String> toV8String(v8::Isolate*, const String16&);
v8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const String16&);
v8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const char*);
v8::Local<v8::String> toV8String(v8::Isolate*, const StringView&);
// TODO(dgozman): rename to toString16.
119 120
String16 toProtocolString(v8::Isolate*, v8::Local<v8::String>);
String16 toProtocolStringWithTypeCheck(v8::Isolate*, v8::Local<v8::Value>);
121 122 123 124 125 126 127 128 129 130 131 132 133 134
String16 toString16(const StringView&);
StringView toStringView(const String16&);
bool stringViewStartsWith(const StringView&, const char*);

class StringBufferImpl : public StringBuffer {
 public:
  // Destroys string's content.
  static std::unique_ptr<StringBufferImpl> adopt(String16&);
  const StringView& string() override { return m_string; }

 private:
  explicit StringBufferImpl(String16&);
  String16 m_owner;
  StringView m_string;
135 136

  DISALLOW_COPY_AND_ASSIGN(StringBufferImpl);
137 138
};

139 140 141 142 143 144 145 146 147 148 149 150 151
class BinaryStringBuffer : public StringBuffer {
 public:
  explicit BinaryStringBuffer(std::vector<uint8_t> data)
      : m_data(std::move(data)), m_string(m_data.data(), m_data.size()) {}
  const StringView& string() override { return m_string; }

 private:
  std::vector<uint8_t> m_data;
  StringView m_string;

  DISALLOW_COPY_AND_ASSIGN(BinaryStringBuffer);
};

152 153 154
String16 debuggerIdToString(const std::pair<int64_t, int64_t>& debuggerId);
String16 stackTraceIdToString(uintptr_t id);

155 156
}  //  namespace v8_inspector

157
#endif  // V8_INSPECTOR_STRING_UTIL_H_