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 std::string_view);
68 std::unique_ptr<Value> parseJSON(const std::string_view);
69 std::unique_ptr<Value> parseJSON(v8_inspector::StringView view);
70 
71 std::unique_ptr<Value> parseMessage(const std::string_view message,
72                                     bool binary);
73 ProtocolMessage jsonToMessage(String message);
74 ProtocolMessage binaryToMessage(std::vector<uint8_t> message);
75 String fromUTF8(const uint8_t* data, size_t length);
76 String fromUTF16(const uint16_t* data, size_t length);
77 const uint8_t* CharactersUTF8(const std::string_view s);
78 size_t CharacterCount(const std::string_view s);
79 
80 // Unimplemented. The generated code will fall back to CharactersUTF8().
CharactersLatin1(const std::string_view s)81 inline uint8_t* CharactersLatin1(const std::string_view s) {
82   return nullptr;
83 }
CharactersUTF16(const std::string_view s)84 inline const uint16_t* CharactersUTF16(const std::string_view s) {
85   return nullptr;
86 }
87 
88 extern size_t kNotFound;
89 }  // namespace StringUtil
90 
91 // A read-only sequence of uninterpreted bytes with reference-counted storage.
92 // Though the templates for generating the protocol bindings reference
93 // this type, js_protocol.pdl doesn't have a field of type 'binary', so
94 // therefore it's unnecessary to provide an implementation here.
95 class Binary {
96  public:
data() const97   const uint8_t* data() const { UNREACHABLE(); }
size() const98   size_t size() const { UNREACHABLE(); }
toBase64() const99   String toBase64() const { UNREACHABLE(); }
fromBase64(const std::string_view base64, bool* success)100   static Binary fromBase64(const std::string_view base64, bool* success) {
101     UNREACHABLE();
102   }
fromSpan(const uint8_t* data, size_t size)103   static Binary fromSpan(const uint8_t* data, size_t size) { UNREACHABLE(); }
104 };
105 
106 }  // namespace protocol
107 }  // namespace inspector
108 }  // namespace node
109 
110 #endif  // SRC_INSPECTOR_NODE_STRING_H_
111