Skip to content

Commit ab3d03c

Browse files
committed
fixup! inspector: roll inspector_protocol
1 parent 06a47f9 commit ab3d03c

File tree

2 files changed

+16
-70
lines changed

2 files changed

+16
-70
lines changed

src/inspector/node_string.cc

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,32 +59,20 @@ String StringUtil::StringViewToUtf8(v8_inspector::StringView view) {
5959
return std::string(reinterpret_cast<const char*>(view.characters8()),
6060
view.length());
6161
}
62-
return fromUTF16LE(view.characters16(), view.length());
63-
}
64-
65-
String StringUtil::fromDouble(double d) {
66-
std::ostringstream stream;
67-
stream.imbue(std::locale::classic()); // Ignore current locale
68-
stream << std::fixed << d;
69-
return stream.str();
70-
}
71-
72-
double StringUtil::toDouble(const char* buffer, size_t length, bool* ok) {
73-
std::istringstream stream(std::string(buffer, length));
74-
stream.imbue(std::locale::classic()); // Ignore current locale
75-
double d;
76-
stream >> d;
77-
*ok = !stream.fail();
78-
return d;
79-
}
80-
81-
ProtocolMessage StringUtil::jsonToMessage(String message) {
82-
return message;
83-
}
84-
85-
ProtocolMessage StringUtil::binaryToMessage(std::vector<uint8_t> message) {
86-
return std::string(reinterpret_cast<const char*>(message.data()),
87-
message.size());
62+
const char16_t* source =
63+
reinterpret_cast<const char16_t*>(view.characters16());
64+
size_t expected_utf8_length =
65+
simdutf::utf8_length_from_utf16(source, view.length());
66+
MaybeStackBuffer<char> buffer(expected_utf8_length);
67+
// convert_utf16_to_utf8 returns zero in case of error.
68+
size_t utf8_length =
69+
simdutf::convert_utf16_to_utf8(source, view.length(), buffer.out());
70+
// We have that utf8_length == expected_utf8_length if and only
71+
// if the input was a valid UTF-16 string. Otherwise, utf8_length
72+
// must be zero.
73+
CHECK(utf8_length == 0 || utf8_length == expected_utf8_length);
74+
// An invalid UTF-16 input will generate the empty string:
75+
return String(buffer.out(), utf8_length);
8876
}
8977

9078
String StringUtil::fromUTF8(const uint8_t* data, size_t length) {

src/inspector/node_string.h

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,56 +31,14 @@ using String = std::string;
3131
using StringBuilder = std::ostringstream;
3232
using ProtocolMessage = std::string;
3333

34+
// Implements StringUtil methods used in `inspector_protocol/lib/`.
3435
struct StringUtil {
35-
// NOLINTNEXTLINE(runtime/references) This is V8 API...
36-
inline static void builderAppend(StringBuilder& builder, char c) {
37-
builder.put(c);
38-
}
39-
40-
// NOLINTNEXTLINE(runtime/references)
41-
inline static void builderAppend(StringBuilder& builder,
42-
const char* value,
43-
size_t length) {
44-
builder.write(value, length);
45-
}
46-
47-
// NOLINTNEXTLINE(runtime/references)
48-
inline static void builderAppend(StringBuilder& builder, const char* value) {
49-
builderAppend(builder, value, std::strlen(value));
50-
}
51-
52-
// NOLINTNEXTLINE(runtime/references)
53-
inline static void builderAppend(StringBuilder& builder,
54-
const String& string) {
55-
builder << string;
56-
}
57-
58-
// NOLINTNEXTLINE(runtime/references)
59-
inline static void builderReserve(StringBuilder& builder, size_t) {
60-
// ostringstream does not have a counterpart
61-
}
62-
inline static String substring(const String& string,
63-
size_t start,
64-
size_t count) {
65-
return string.substr(start, count);
66-
}
67-
inline static String fromInteger(int n) { return std::to_string(n); }
68-
inline static String builderToString(const StringBuilder& builder) {
69-
return builder.str();
70-
}
71-
inline static size_t find(const String& string, const char* substring) {
72-
return string.find(substring);
73-
}
74-
static String fromDouble(double d);
75-
static double toDouble(const char* buffer, size_t length, bool* ok);
76-
36+
// Convert Utf16 in local endianness to Utf8 if needed.
7737
static String StringViewToUtf8(v8_inspector::StringView view);
7838

7939
static std::unique_ptr<Value> parseJSON(const std::string_view);
8040
static std::unique_ptr<Value> parseJSON(v8_inspector::StringView view);
8141

82-
static ProtocolMessage jsonToMessage(String message);
83-
static ProtocolMessage binaryToMessage(std::vector<uint8_t> message);
8442
static String fromUTF8(const uint8_t* data, size_t length);
8543
static String fromUTF16LE(const uint16_t* data, size_t length);
8644
static const uint8_t* CharactersUTF8(const std::string_view s);

0 commit comments

Comments
 (0)