1 // Bridges V8 Inspector generated code with the std::string used by the Node
2 // Compare to V8 counterpart - deps/v8/src/inspector/string-util.h
3 #ifndef SRC_INSPECTOR_NODE_STRING_H_
4 #define SRC_INSPECTOR_NODE_STRING_H_
5
6 #include "util.h"
7 #include "v8-inspector.h"
8
9 #include <cstring>
10 #include <sstream>
11 #include <string>
12
13 namespace node {
14 namespace inspector {
15 namespace protocol {
16
17 class Value;
18
19 using String = std::string;
20 using StringBuilder = std::ostringstream;
21 using ProtocolMessage = std::string;
22
23 namespace StringUtil {
24 // NOLINTNEXTLINE(runtime/references) This is V8 API...
builderAppend(StringBuilder & builder,char c)25 inline void builderAppend(StringBuilder& builder, char c) {
26 builder.put(c);
27 }
28
29 // NOLINTNEXTLINE(runtime/references)
builderAppend(StringBuilder & builder,const char * value,size_t length)30 inline void builderAppend(StringBuilder& builder, const char* value,
31 size_t length) {
32 builder.write(value, length);
33 }
34
35 // NOLINTNEXTLINE(runtime/references)
builderAppend(StringBuilder & builder,const char * value)36 inline void builderAppend(StringBuilder& builder, const char* value) {
37 builderAppend(builder, value, std::strlen(value));
38 }
39
40 // NOLINTNEXTLINE(runtime/references)
builderAppend(StringBuilder & builder,const String & string)41 inline void builderAppend(StringBuilder& builder, const String& string) {
42 builder << string;
43 }
44
45 // NOLINTNEXTLINE(runtime/references)
builderReserve(StringBuilder & builder,size_t)46 inline void builderReserve(StringBuilder& builder, size_t) {
47 // ostringstream does not have a counterpart
48 }
substring(const String & string,size_t start,size_t count)49 inline String substring(const String& string, size_t start, size_t count) {
50 return string.substr(start, count);
51 }
fromInteger(int n)52 inline String fromInteger(int n) {
53 return std::to_string(n);
54 }
builderToString(const StringBuilder & builder)55 inline String builderToString(const StringBuilder& builder) {
56 return builder.str();
57 }
find(const String & string,const char * substring)58 inline size_t find(const String& string, const char* substring) {
59 return string.find(substring);
60 }
61 String fromDouble(double d);
62 double toDouble(const char* buffer, size_t length, bool* ok);
63
64 String StringViewToUtf8(v8_inspector::StringView view);
65
66 // NOLINTNEXTLINE(runtime/references)
67 void builderAppendQuotedString(StringBuilder& builder, const String&);
68 std::unique_ptr<Value> parseJSON(const String&);
69 std::unique_ptr<Value> parseJSON(v8_inspector::StringView view);
70
71 std::unique_ptr<Value> parseMessage(const std::string& message, bool binary);
72 ProtocolMessage jsonToMessage(String message);
73 ProtocolMessage binaryToMessage(std::vector<uint8_t> message);
74 String fromUTF8(const uint8_t* data, size_t length);
75 String fromUTF16(const uint16_t* data, size_t length);
76 const uint8_t* CharactersUTF8(const String& s);
77 size_t CharacterCount(const String& s);
78
79 // Unimplemented. The generated code will fall back to CharactersUTF8().
CharactersLatin1(const String & s)80 inline uint8_t* CharactersLatin1(const String& s) { return nullptr; }
CharactersUTF16(const String & s)81 inline const uint16_t* CharactersUTF16(const String& s) { return nullptr; }
82
83 extern size_t kNotFound;
84 } // namespace StringUtil
85
86 // A read-only sequence of uninterpreted bytes with reference-counted storage.
87 // Though the templates for generating the protocol bindings reference
88 // this type, js_protocol.pdl doesn't have a field of type 'binary', so
89 // therefore it's unnecessary to provide an implementation here.
90 class Binary {
91 public:
data()92 const uint8_t* data() const { UNREACHABLE(); }
size()93 size_t size() const { UNREACHABLE(); }
toBase64()94 String toBase64() const { UNREACHABLE(); }
fromBase64(const String & base64,bool * success)95 static Binary fromBase64(const String& base64, bool* success) {
96 UNREACHABLE();
97 }
fromSpan(const uint8_t * data,size_t size)98 static Binary fromSpan(const uint8_t* data, size_t size) { UNREACHABLE(); }
99 };
100
101 } // namespace protocol
102 } // namespace inspector
103 } // namespace node
104
105 #endif // SRC_INSPECTOR_NODE_STRING_H_
106