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 : #ifndef V8_INSPECTOR_STRING_UTIL_H_
6 : #define V8_INSPECTOR_STRING_UTIL_H_
7 :
8 : #include <stdint.h>
9 : #include <memory>
10 :
11 : #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 : namespace v8_inspector {
18 :
19 : namespace protocol {
20 :
21 : class Value;
22 :
23 : using String = v8_inspector::String16;
24 : using StringBuilder = v8_inspector::String16Builder;
25 374740 : struct ProtocolMessage {
26 : String json;
27 : std::vector<uint8_t> binary;
28 : };
29 :
30 : class StringUTF8Adapter {
31 : public:
32 0 : explicit StringUTF8Adapter(const String& string) : string_(string.utf8()) {}
33 : const char* Data() const { return string_.data(); }
34 : size_t length() const { return string_.length(); }
35 :
36 : private:
37 : std::string string_;
38 : };
39 :
40 : class StringUtil {
41 : public:
42 : static String substring(const String& s, size_t pos, size_t len) {
43 168538 : return s.substring(pos, len);
44 : }
45 1893509 : static String fromInteger(int number) { return String::fromInteger(number); }
46 : static String fromInteger(size_t number) {
47 345 : return String::fromInteger(number);
48 : }
49 151401 : static String fromDouble(double number) { return String::fromDouble(number); }
50 : static double toDouble(const char* s, size_t len, bool* isOk);
51 168538 : static size_t find(const String& s, const char* needle) {
52 337076 : return s.find(needle);
53 : }
54 : static size_t find(const String& s, const String& needle) {
55 : return s.find(needle);
56 : }
57 : static const size_t kNotFound = String::kNotFound;
58 : static void builderAppend(StringBuilder& builder, const String& s) {
59 42702945 : builder.append(s);
60 : }
61 : static void builderAppend(StringBuilder& builder, UChar c) {
62 1292468023 : builder.append(c);
63 : }
64 : static void builderAppend(StringBuilder& builder, const char* s, size_t len) {
65 7918090 : builder.append(s, len);
66 : }
67 : static void builderAppendQuotedString(StringBuilder&, const String&);
68 : static void builderReserve(StringBuilder& builder, size_t capacity) {
69 1860960 : builder.reserveCapacity(capacity);
70 : }
71 : static String builderToString(StringBuilder& builder) {
72 1861007 : return builder.toString();
73 : }
74 : static std::unique_ptr<protocol::Value> parseJSON(const String16& json);
75 : static std::unique_ptr<protocol::Value> parseJSON(const StringView& json);
76 : static std::unique_ptr<protocol::Value> parseProtocolMessage(
77 : const ProtocolMessage&);
78 : static ProtocolMessage jsonToMessage(String message);
79 : static ProtocolMessage binaryToMessage(std::vector<uint8_t> message);
80 :
81 : static String fromUTF8(const uint8_t* data, size_t length) {
82 0 : return String16::fromUTF8(reinterpret_cast<const char*>(data), length);
83 : }
84 : static void writeUTF8(const String& string, std::vector<uint8_t>* out) {
85 : // TODO(pfeldman): get rid of the copy here.
86 : std::string utf8 = string.utf8();
87 : const uint8_t* data = reinterpret_cast<const uint8_t*>(utf8.data());
88 : out->insert(out->end(), data, data + utf8.length());
89 : }
90 : };
91 :
92 : // A read-only sequence of uninterpreted bytes with reference-counted storage.
93 : // Though the templates for generating the protocol bindings reference
94 : // this type, js_protocol.pdl doesn't have a field of type 'binary', so
95 : // therefore it's unnecessary to provide an implementation here.
96 : class Binary {
97 : public:
98 : const uint8_t* data() const { UNIMPLEMENTED(); }
99 0 : size_t size() const { UNIMPLEMENTED(); }
100 0 : String toBase64() const { UNIMPLEMENTED(); }
101 : static Binary fromBase64(const String& base64, bool* success) {
102 : UNIMPLEMENTED();
103 : }
104 0 : static Binary fromSpan(const uint8_t* data, size_t size) { UNIMPLEMENTED(); }
105 : };
106 : } // namespace protocol
107 :
108 : v8::Local<v8::String> toV8String(v8::Isolate*, const String16&);
109 : v8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const String16&);
110 : v8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const char*);
111 : v8::Local<v8::String> toV8String(v8::Isolate*, const StringView&);
112 : // TODO(dgozman): rename to toString16.
113 : String16 toProtocolString(v8::Isolate*, v8::Local<v8::String>);
114 : String16 toProtocolStringWithTypeCheck(v8::Isolate*, v8::Local<v8::Value>);
115 : String16 toString16(const StringView&);
116 : StringView toStringView(const String16&);
117 : bool stringViewStartsWith(const StringView&, const char*);
118 :
119 695614 : class StringBufferImpl : public StringBuffer {
120 : public:
121 : // Destroys string's content.
122 : static std::unique_ptr<StringBufferImpl> adopt(String16&);
123 347807 : const StringView& string() override { return m_string; }
124 :
125 : private:
126 : explicit StringBufferImpl(String16&);
127 : String16 m_owner;
128 : StringView m_string;
129 :
130 : DISALLOW_COPY_AND_ASSIGN(StringBufferImpl);
131 : };
132 :
133 0 : class BinaryStringBuffer : public StringBuffer {
134 : public:
135 : explicit BinaryStringBuffer(std::vector<uint8_t> data)
136 0 : : m_data(std::move(data)), m_string(m_data.data(), m_data.size()) {}
137 0 : const StringView& string() override { return m_string; }
138 :
139 : private:
140 : std::vector<uint8_t> m_data;
141 : StringView m_string;
142 :
143 : DISALLOW_COPY_AND_ASSIGN(BinaryStringBuffer);
144 : };
145 :
146 : String16 debuggerIdToString(const std::pair<int64_t, int64_t>& debuggerId);
147 : String16 stackTraceIdToString(uintptr_t id);
148 :
149 : } // namespace v8_inspector
150 :
151 : #endif // V8_INSPECTOR_STRING_UTIL_H_
|