Skip to content

Commit f8d0291

Browse files
anonrigdanielleadams
authored andcommitted
src: remove icu usage from node_string.cc
PR-URL: #46548 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 99bfbed commit f8d0291

File tree

2 files changed

+55
-47
lines changed

2 files changed

+55
-47
lines changed

src/inspector/node_string.cc

+42-39
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#include "node_string.h"
22
#include "node/inspector/protocol/Protocol.h"
3+
#include "node_util.h"
34
#include "simdutf.h"
45

5-
#include <unicode/unistr.h>
6-
76
namespace node {
87
namespace inspector {
98
namespace protocol {
@@ -12,27 +11,34 @@ namespace StringUtil {
1211
size_t kNotFound = std::string::npos;
1312

1413
// 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) {
1616
builder.put('"');
1717
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);
2327
}
2428
builder.put('"');
2529
}
2630

27-
std::unique_ptr<Value> parseJSON(const String& string) {
31+
std::unique_ptr<Value> parseJSON(const std::string_view string) {
2832
if (string.empty())
2933
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);
3642
}
3743

3844
std::unique_ptr<Value> parseJSON(v8_inspector::StringView string) {
@@ -50,24 +56,15 @@ String StringViewToUtf8(v8_inspector::StringView view) {
5056
return std::string(reinterpret_cast<const char*>(view.characters8()),
5157
view.length());
5258
}
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);
7168
}
7269

7370
String fromDouble(double d) {
@@ -86,7 +83,8 @@ double toDouble(const char* buffer, size_t length, bool* ok) {
8683
return d;
8784
}
8885

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) {
9088
if (binary) {
9189
return Value::parseBinary(
9290
reinterpret_cast<const uint8_t*>(message.data()),
@@ -109,16 +107,21 @@ String fromUTF8(const uint8_t* data, size_t length) {
109107
}
110108

111109
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);
115118
}
116119

117-
const uint8_t* CharactersUTF8(const String& s) {
120+
const uint8_t* CharactersUTF8(const std::string_view s) {
118121
return reinterpret_cast<const uint8_t*>(s.data());
119122
}
120123

121-
size_t CharacterCount(const String& s) {
124+
size_t CharacterCount(const std::string_view s) {
122125
// TODO(@anonrig): Test to make sure CharacterCount returns correctly.
123126
return simdutf::utf32_length_from_utf8(s.data(), s.length());
124127
}

src/inspector/node_string.h

+13-8
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,26 @@ double toDouble(const char* buffer, size_t length, bool* ok);
6464
String StringViewToUtf8(v8_inspector::StringView view);
6565

6666
// NOLINTNEXTLINE(runtime/references)
67-
void builderAppendQuotedString(StringBuilder& builder, const String&);
68-
std::unique_ptr<Value> parseJSON(const String&);
67+
void builderAppendQuotedString(StringBuilder& builder, const std::string_view);
68+
std::unique_ptr<Value> parseJSON(const std::string_view);
6969
std::unique_ptr<Value> parseJSON(v8_inspector::StringView view);
7070

71-
std::unique_ptr<Value> parseMessage(const std::string& message, bool binary);
71+
std::unique_ptr<Value> parseMessage(const std::string_view message,
72+
bool binary);
7273
ProtocolMessage jsonToMessage(String message);
7374
ProtocolMessage binaryToMessage(std::vector<uint8_t> message);
7475
String fromUTF8(const uint8_t* data, size_t length);
7576
String fromUTF16(const uint16_t* data, size_t length);
76-
const uint8_t* CharactersUTF8(const String& s);
77-
size_t CharacterCount(const String& s);
77+
const uint8_t* CharactersUTF8(const std::string_view s);
78+
size_t CharacterCount(const std::string_view s);
7879

7980
// Unimplemented. The generated code will fall back to CharactersUTF8().
80-
inline uint8_t* CharactersLatin1(const String& s) { return nullptr; }
81-
inline const uint16_t* CharactersUTF16(const String& s) { return nullptr; }
81+
inline uint8_t* CharactersLatin1(const std::string_view s) {
82+
return nullptr;
83+
}
84+
inline const uint16_t* CharactersUTF16(const std::string_view s) {
85+
return nullptr;
86+
}
8287

8388
extern size_t kNotFound;
8489
} // namespace StringUtil
@@ -92,7 +97,7 @@ class Binary {
9297
const uint8_t* data() const { UNREACHABLE(); }
9398
size_t size() const { UNREACHABLE(); }
9499
String toBase64() const { UNREACHABLE(); }
95-
static Binary fromBase64(const String& base64, bool* success) {
100+
static Binary fromBase64(const std::string_view base64, bool* success) {
96101
UNREACHABLE();
97102
}
98103
static Binary fromSpan(const uint8_t* data, size_t size) { UNREACHABLE(); }

0 commit comments

Comments
 (0)