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-
76namespace node {
87namespace inspector {
98namespace protocol {
@@ -12,27 +11,34 @@ namespace StringUtil {
1211size_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
3844std::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
7370String 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
111109String 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}
0 commit comments