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