1 |
|
|
#include "node_string.h" |
2 |
|
|
#include "node/inspector/protocol/Protocol.h" |
3 |
|
|
|
4 |
|
|
#include <unicode/unistr.h> |
5 |
|
|
|
6 |
|
|
namespace node { |
7 |
|
|
namespace inspector { |
8 |
|
|
namespace protocol { |
9 |
|
|
namespace StringUtil { |
10 |
|
|
|
11 |
|
|
size_t kNotFound = std::string::npos; |
12 |
|
|
|
13 |
|
|
// NOLINTNEXTLINE(runtime/references) V8 API requirement |
14 |
|
4450 |
void builderAppendQuotedString(StringBuilder& builder, const String& string) { |
15 |
|
4450 |
builder.put('"'); |
16 |
✓✓ |
4450 |
if (!string.empty()) { |
17 |
|
|
icu::UnicodeString utf16 = icu::UnicodeString::fromUTF8( |
18 |
|
8898 |
icu::StringPiece(string.data(), string.length())); |
19 |
|
4449 |
escapeWideStringForJSON( |
20 |
|
4449 |
reinterpret_cast<const uint16_t*>(utf16.getBuffer()), utf16.length(), |
21 |
|
|
&builder); |
22 |
|
|
} |
23 |
|
4450 |
builder.put('"'); |
24 |
|
4450 |
} |
25 |
|
|
|
26 |
|
19720 |
std::unique_ptr<Value> parseJSON(const String& string) { |
27 |
✗✓ |
19720 |
if (string.empty()) |
28 |
|
|
return nullptr; |
29 |
|
|
|
30 |
|
|
icu::UnicodeString utf16 = |
31 |
|
|
icu::UnicodeString::fromUTF8(icu::StringPiece(string.data(), |
32 |
|
39440 |
string.length())); |
33 |
|
|
return parseJSONCharacters( |
34 |
|
19720 |
reinterpret_cast<const uint16_t*>(utf16.getBuffer()), utf16.length()); |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
std::unique_ptr<Value> parseJSON(v8_inspector::StringView string) { |
38 |
|
|
if (string.length() == 0) |
39 |
|
|
return nullptr; |
40 |
|
|
if (string.is8Bit()) |
41 |
|
|
return parseJSONCharacters(string.characters8(), string.length()); |
42 |
|
|
return parseJSONCharacters(string.characters16(), string.length()); |
43 |
|
|
} |
44 |
|
|
|
45 |
|
694749 |
String StringViewToUtf8(v8_inspector::StringView view) { |
46 |
✓✓ |
694749 |
if (view.length() == 0) |
47 |
|
564 |
return ""; |
48 |
✓✓ |
694185 |
if (view.is8Bit()) { |
49 |
|
18337 |
return std::string(reinterpret_cast<const char*>(view.characters8()), |
50 |
|
36674 |
view.length()); |
51 |
|
|
} |
52 |
|
675848 |
const uint16_t* source = view.characters16(); |
53 |
|
675848 |
const UChar* unicodeSource = reinterpret_cast<const UChar*>(source); |
54 |
|
|
static_assert(sizeof(*source) == sizeof(*unicodeSource), |
55 |
|
|
"sizeof(*source) == sizeof(*unicodeSource)"); |
56 |
|
|
|
57 |
|
675848 |
size_t result_length = view.length() * sizeof(*source); |
58 |
|
1351696 |
std::string result(result_length, '\0'); |
59 |
|
1351696 |
icu::UnicodeString utf16(unicodeSource, view.length()); |
60 |
|
|
// ICU components for std::string compatibility are not enabled in build... |
61 |
|
675848 |
bool done = false; |
62 |
✓✓ |
1351698 |
while (!done) { |
63 |
|
675850 |
icu::CheckedArrayByteSink sink(&result[0], result_length); |
64 |
|
675850 |
utf16.toUTF8(sink); |
65 |
|
675850 |
result_length = sink.NumberOfBytesAppended(); |
66 |
|
675850 |
result.resize(result_length); |
67 |
|
675850 |
done = !sink.Overflowed(); |
68 |
|
|
} |
69 |
|
675848 |
return result; |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
String fromDouble(double d) { |
73 |
|
|
std::ostringstream stream; |
74 |
|
|
stream.imbue(std::locale::classic()); // Ignore current locale |
75 |
|
|
stream << d; |
76 |
|
|
return stream.str(); |
77 |
|
|
} |
78 |
|
|
|
79 |
|
20703 |
double toDouble(const char* buffer, size_t length, bool* ok) { |
80 |
|
41406 |
std::istringstream stream(std::string(buffer, length)); |
81 |
|
20703 |
stream.imbue(std::locale::classic()); // Ignore current locale |
82 |
|
|
double d; |
83 |
|
20703 |
stream >> d; |
84 |
|
20703 |
*ok = !stream.fail(); |
85 |
|
20703 |
return d; |
86 |
|
|
} |
87 |
|
|
|
88 |
|
19720 |
std::unique_ptr<Value> parseMessage(const std::string& message, bool binary) { |
89 |
✗✓ |
19720 |
if (binary) { |
90 |
|
|
return Value::parseBinary( |
91 |
|
|
reinterpret_cast<const uint8_t*>(message.data()), |
92 |
|
|
message.length()); |
93 |
|
|
} |
94 |
|
19720 |
return parseJSON(message); |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
ProtocolMessage jsonToMessage(String message) { |
98 |
|
|
return message; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
ProtocolMessage binaryToMessage(std::vector<uint8_t> message) { |
102 |
|
|
return std::string(reinterpret_cast<const char*>(message.data()), |
103 |
|
|
message.size()); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
String fromUTF8(const uint8_t* data, size_t length) { |
107 |
|
|
return std::string(reinterpret_cast<const char*>(data), length); |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
String fromUTF16(const uint16_t* data, size_t length) { |
111 |
|
|
icu::UnicodeString utf16(reinterpret_cast<const char16_t*>(data), length); |
112 |
|
|
std::string result; |
113 |
|
|
return utf16.toUTF8String(result); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
const uint8_t* CharactersUTF8(const String& s) { |
117 |
|
|
return reinterpret_cast<const uint8_t*>(s.data()); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
size_t CharacterCount(const String& s) { |
121 |
|
|
icu::UnicodeString utf16 = |
122 |
|
|
icu::UnicodeString::fromUTF8(icu::StringPiece(s.data(), s.length())); |
123 |
|
|
return utf16.countChar32(); |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
} // namespace StringUtil |
127 |
|
|
} // namespace protocol |
128 |
|
|
} // namespace inspector |
129 |
|
|
} // namespace node |
130 |
|
|
|