1
1
#include " node_string.h"
2
2
#include " node/inspector/protocol/Protocol.h"
3
+ #include " node_util.h"
3
4
#include " simdutf.h"
4
5
5
- #include < unicode/unistr.h>
6
-
7
6
namespace node {
8
7
namespace inspector {
9
8
namespace protocol {
@@ -12,27 +11,34 @@ namespace StringUtil {
12
11
size_t kNotFound = std::string::npos;
13
12
14
13
// NOLINTNEXTLINE(runtime/references) V8 API requirement
15
- void builderAppendQuotedString (StringBuilder& builder, const String& string) {
14
+ void builderAppendQuotedString (StringBuilder& builder,
15
+ const std::string_view string) {
16
16
builder.put (' "' );
17
17
if (!string.empty ()) {
18
- icu::UnicodeString utf16 = icu::UnicodeString::fromUTF8 (
19
- icu::StringPiece (string.data (), string.length ()));
20
- escapeWideStringForJSON (
21
- reinterpret_cast <const uint16_t *>(utf16.getBuffer ()), utf16.length (),
22
- &builder);
18
+ size_t expected_utf16_length =
19
+ simdutf::utf16_length_from_utf8 (string.data (), string.length ());
20
+ MaybeStackBuffer<char16_t > buffer (expected_utf16_length);
21
+ size_t utf16_length = simdutf::convert_utf8_to_utf16 (
22
+ string.data (), string.length (), buffer.out ());
23
+ CHECK_EQ (expected_utf16_length, utf16_length);
24
+ escapeWideStringForJSON (reinterpret_cast <const uint16_t *>(buffer.out ()),
25
+ utf16_length,
26
+ &builder);
23
27
}
24
28
builder.put (' "' );
25
29
}
26
30
27
- std::unique_ptr<Value> parseJSON (const String& string) {
31
+ std::unique_ptr<Value> parseJSON (const std::string_view string) {
28
32
if (string.empty ())
29
33
return nullptr ;
30
-
31
- icu::UnicodeString utf16 =
32
- icu::UnicodeString::fromUTF8 (icu::StringPiece (string.data (),
33
- string.length ()));
34
- return parseJSONCharacters (
35
- reinterpret_cast <const uint16_t *>(utf16.getBuffer ()), utf16.length ());
34
+ size_t expected_utf16_length =
35
+ simdutf::utf16_length_from_utf8 (string.data (), string.length ());
36
+ MaybeStackBuffer<char16_t > buffer (expected_utf16_length);
37
+ size_t utf16_length = simdutf::convert_utf8_to_utf16 (
38
+ string.data (), string.length (), buffer.out ());
39
+ CHECK_EQ (expected_utf16_length, utf16_length);
40
+ return parseJSONCharacters (reinterpret_cast <const uint16_t *>(buffer.out ()),
41
+ utf16_length);
36
42
}
37
43
38
44
std::unique_ptr<Value> parseJSON (v8_inspector::StringView string) {
@@ -50,24 +56,15 @@ String StringViewToUtf8(v8_inspector::StringView view) {
50
56
return std::string (reinterpret_cast <const char *>(view.characters8 ()),
51
57
view.length ());
52
58
}
53
- const uint16_t * source = view.characters16 ();
54
- const UChar* unicodeSource = reinterpret_cast <const UChar*>(source);
55
- static_assert (sizeof (*source) == sizeof (*unicodeSource),
56
- " sizeof(*source) == sizeof(*unicodeSource)" );
57
-
58
- size_t result_length = view.length () * sizeof (*source);
59
- std::string result (result_length, ' \0 ' );
60
- icu::UnicodeString utf16 (unicodeSource, view.length ());
61
- // ICU components for std::string compatibility are not enabled in build...
62
- bool done = false ;
63
- while (!done) {
64
- icu::CheckedArrayByteSink sink (&result[0 ], result_length);
65
- utf16.toUTF8 (sink);
66
- result_length = sink.NumberOfBytesAppended ();
67
- result.resize (result_length);
68
- done = !sink.Overflowed ();
69
- }
70
- return result;
59
+ const char16_t * source =
60
+ reinterpret_cast <const char16_t *>(view.characters16 ());
61
+ size_t expected_utf8_length =
62
+ simdutf::utf8_length_from_utf16 (source, view.length ());
63
+ MaybeStackBuffer<char > buffer (expected_utf8_length);
64
+ size_t utf8_length =
65
+ simdutf::convert_utf16_to_utf8 (source, view.length (), buffer.out ());
66
+ CHECK_EQ (expected_utf8_length, utf8_length);
67
+ return String (buffer.out (), utf8_length);
71
68
}
72
69
73
70
String fromDouble (double d) {
@@ -86,7 +83,8 @@ double toDouble(const char* buffer, size_t length, bool* ok) {
86
83
return d;
87
84
}
88
85
89
- std::unique_ptr<Value> parseMessage (const std::string& message, bool binary) {
86
+ std::unique_ptr<Value> parseMessage (const std::string_view message,
87
+ bool binary) {
90
88
if (binary) {
91
89
return Value::parseBinary (
92
90
reinterpret_cast <const uint8_t *>(message.data ()),
@@ -109,16 +107,21 @@ String fromUTF8(const uint8_t* data, size_t length) {
109
107
}
110
108
111
109
String fromUTF16 (const uint16_t * data, size_t length) {
112
- icu::UnicodeString utf16 (reinterpret_cast <const char16_t *>(data), length);
113
- std::string result;
114
- return utf16.toUTF8String (result);
110
+ auto casted_data = reinterpret_cast <const char16_t *>(data);
111
+ size_t expected_utf8_length =
112
+ simdutf::utf8_length_from_utf16 (casted_data, length);
113
+ MaybeStackBuffer<char > buffer (expected_utf8_length);
114
+ size_t utf8_length =
115
+ simdutf::convert_utf16_to_utf8 (casted_data, length, buffer.out ());
116
+ CHECK_EQ (expected_utf8_length, utf8_length);
117
+ return String (buffer.out (), utf8_length);
115
118
}
116
119
117
- const uint8_t * CharactersUTF8 (const String& s) {
120
+ const uint8_t * CharactersUTF8 (const std::string_view s) {
118
121
return reinterpret_cast <const uint8_t *>(s.data ());
119
122
}
120
123
121
- size_t CharacterCount (const String& s) {
124
+ size_t CharacterCount (const std::string_view s) {
122
125
// TODO(@anonrig): Test to make sure CharacterCount returns correctly.
123
126
return simdutf::utf32_length_from_utf8 (s.data (), s.length ());
124
127
}
0 commit comments