1
1
#include " node_string.h"
2
2
#include " node/inspector/protocol/Protocol.h"
3
3
#include " node_util.h"
4
+ #if !defined(_WIN32) || !defined(_M_ARM64)
4
5
#include " simdutf.h"
6
+ #endif
7
+
8
+ #if defined(_WIN32) && defined(_M_ARM64)
9
+ #include < unicode/unistr.h>
10
+ #endif
5
11
6
12
namespace node {
7
13
namespace inspector {
@@ -15,6 +21,13 @@ void builderAppendQuotedString(StringBuilder& builder,
15
21
const std::string_view string) {
16
22
builder.put (' "' );
17
23
if (!string.empty ()) {
24
+ #if defined(_WIN32) && defined(_M_ARM64)
25
+ icu::UnicodeString utf16 = icu::UnicodeString::fromUTF8 (
26
+ icu::StringPiece (string.data (), string.length ()));
27
+ escapeWideStringForJSON (
28
+ reinterpret_cast <const uint16_t *>(utf16.getBuffer ()), utf16.length (),
29
+ &builder);
30
+ #else
18
31
size_t expected_utf16_length =
19
32
simdutf::utf16_length_from_utf8 (string.data (), string.length ());
20
33
MaybeStackBuffer<char16_t > buffer (expected_utf16_length);
@@ -24,13 +37,21 @@ void builderAppendQuotedString(StringBuilder& builder,
24
37
escapeWideStringForJSON (reinterpret_cast <const uint16_t *>(buffer.out ()),
25
38
utf16_length,
26
39
&builder);
40
+ #endif
27
41
}
28
42
builder.put (' "' );
29
43
}
30
44
31
45
std::unique_ptr<Value> parseJSON (const std::string_view string) {
32
46
if (string.empty ())
33
47
return nullptr ;
48
+ #if defined(_WIN32) && defined(_M_ARM64)
49
+ icu::UnicodeString utf16 =
50
+ icu::UnicodeString::fromUTF8 (icu::StringPiece (string.data (),
51
+ string.length ()));
52
+ return parseJSONCharacters (
53
+ reinterpret_cast <const uint16_t *>(utf16.getBuffer ()), utf16.length ());
54
+ #else
34
55
size_t expected_utf16_length =
35
56
simdutf::utf16_length_from_utf8 (string.data (), string.length ());
36
57
MaybeStackBuffer<char16_t > buffer (expected_utf16_length);
@@ -39,6 +60,7 @@ std::unique_ptr<Value> parseJSON(const std::string_view string) {
39
60
CHECK_EQ (expected_utf16_length, utf16_length);
40
61
return parseJSONCharacters (reinterpret_cast <const uint16_t *>(buffer.out ()),
41
62
utf16_length);
63
+ #endif
42
64
}
43
65
44
66
std::unique_ptr<Value> parseJSON (v8_inspector::StringView string) {
@@ -56,6 +78,26 @@ String StringViewToUtf8(v8_inspector::StringView view) {
56
78
return std::string (reinterpret_cast <const char *>(view.characters8 ()),
57
79
view.length ());
58
80
}
81
+ #if defined(_WIN32) && defined(_M_ARM64)
82
+ const uint16_t * source = view.characters16 ();
83
+ const UChar* unicodeSource = reinterpret_cast <const UChar*>(source);
84
+ static_assert (sizeof (*source) == sizeof (*unicodeSource),
85
+ " sizeof(*source) == sizeof(*unicodeSource)" );
86
+
87
+ size_t result_length = view.length () * sizeof (*source);
88
+ std::string result (result_length, ' \0 ' );
89
+ icu::UnicodeString utf16 (unicodeSource, view.length ());
90
+ // ICU components for std::string compatibility are not enabled in build...
91
+ bool done = false ;
92
+ while (!done) {
93
+ icu::CheckedArrayByteSink sink (&result[0 ], result_length);
94
+ utf16.toUTF8 (sink);
95
+ result_length = sink.NumberOfBytesAppended ();
96
+ result.resize (result_length);
97
+ done = !sink.Overflowed ();
98
+ }
99
+ return result;
100
+ #else
59
101
const char16_t * source =
60
102
reinterpret_cast <const char16_t *>(view.characters16 ());
61
103
size_t expected_utf8_length =
@@ -65,6 +107,7 @@ String StringViewToUtf8(v8_inspector::StringView view) {
65
107
simdutf::convert_utf16_to_utf8 (source, view.length (), buffer.out ());
66
108
CHECK_EQ (expected_utf8_length, utf8_length);
67
109
return String (buffer.out (), utf8_length);
110
+ #endif
68
111
}
69
112
70
113
String fromDouble (double d) {
@@ -107,6 +150,11 @@ String fromUTF8(const uint8_t* data, size_t length) {
107
150
}
108
151
109
152
String fromUTF16 (const uint16_t * data, size_t length) {
153
+ #if defined(_WIN32) && defined(_M_ARM64)
154
+ icu::UnicodeString utf16 (reinterpret_cast <const char16_t *>(data), length);
155
+ std::string result;
156
+ return utf16.toUTF8String (result);
157
+ #else
110
158
auto casted_data = reinterpret_cast <const char16_t *>(data);
111
159
size_t expected_utf8_length =
112
160
simdutf::utf8_length_from_utf16 (casted_data, length);
@@ -115,15 +163,22 @@ String fromUTF16(const uint16_t* data, size_t length) {
115
163
simdutf::convert_utf16_to_utf8 (casted_data, length, buffer.out ());
116
164
CHECK_EQ (expected_utf8_length, utf8_length);
117
165
return String (buffer.out (), utf8_length);
166
+ #endif
118
167
}
119
168
120
169
const uint8_t * CharactersUTF8 (const std::string_view s) {
121
170
return reinterpret_cast <const uint8_t *>(s.data ());
122
171
}
123
172
124
173
size_t CharacterCount (const std::string_view s) {
174
+ #if defined(_WIN32) && defined(_M_ARM64)
175
+ icu::UnicodeString utf16 =
176
+ icu::UnicodeString::fromUTF8 (icu::StringPiece (s.data (), s.length ()));
177
+ return utf16.countChar32 ();
178
+ #else
125
179
// TODO(@anonrig): Test to make sure CharacterCount returns correctly.
126
180
return simdutf::utf32_length_from_utf8 (s.data (), s.length ());
181
+ #endif
127
182
}
128
183
129
184
} // namespace StringUtil
0 commit comments