Skip to content

Commit

Permalink
fixup! inspector: roll inspector_protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Jan 24, 2025
1 parent 06a47f9 commit ab3d03c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 70 deletions.
40 changes: 14 additions & 26 deletions src/inspector/node_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,20 @@ String StringUtil::StringViewToUtf8(v8_inspector::StringView view) {
return std::string(reinterpret_cast<const char*>(view.characters8()),
view.length());
}
return fromUTF16LE(view.characters16(), view.length());
}

String StringUtil::fromDouble(double d) {
std::ostringstream stream;
stream.imbue(std::locale::classic()); // Ignore current locale
stream << std::fixed << d;
return stream.str();
}

double StringUtil::toDouble(const char* buffer, size_t length, bool* ok) {
std::istringstream stream(std::string(buffer, length));
stream.imbue(std::locale::classic()); // Ignore current locale
double d;
stream >> d;
*ok = !stream.fail();
return d;
}

ProtocolMessage StringUtil::jsonToMessage(String message) {
return message;
}

ProtocolMessage StringUtil::binaryToMessage(std::vector<uint8_t> message) {
return std::string(reinterpret_cast<const char*>(message.data()),
message.size());
const char16_t* source =
reinterpret_cast<const char16_t*>(view.characters16());
size_t expected_utf8_length =
simdutf::utf8_length_from_utf16(source, view.length());
MaybeStackBuffer<char> buffer(expected_utf8_length);
// convert_utf16_to_utf8 returns zero in case of error.
size_t utf8_length =
simdutf::convert_utf16_to_utf8(source, view.length(), buffer.out());
// We have that utf8_length == expected_utf8_length if and only
// if the input was a valid UTF-16 string. Otherwise, utf8_length
// must be zero.
CHECK(utf8_length == 0 || utf8_length == expected_utf8_length);
// An invalid UTF-16 input will generate the empty string:
return String(buffer.out(), utf8_length);
}

String StringUtil::fromUTF8(const uint8_t* data, size_t length) {
Expand Down
46 changes: 2 additions & 44 deletions src/inspector/node_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,56 +31,14 @@ using String = std::string;
using StringBuilder = std::ostringstream;
using ProtocolMessage = std::string;

// Implements StringUtil methods used in `inspector_protocol/lib/`.
struct StringUtil {
// NOLINTNEXTLINE(runtime/references) This is V8 API...
inline static void builderAppend(StringBuilder& builder, char c) {
builder.put(c);
}

// NOLINTNEXTLINE(runtime/references)
inline static void builderAppend(StringBuilder& builder,
const char* value,
size_t length) {
builder.write(value, length);
}

// NOLINTNEXTLINE(runtime/references)
inline static void builderAppend(StringBuilder& builder, const char* value) {
builderAppend(builder, value, std::strlen(value));
}

// NOLINTNEXTLINE(runtime/references)
inline static void builderAppend(StringBuilder& builder,
const String& string) {
builder << string;
}

// NOLINTNEXTLINE(runtime/references)
inline static void builderReserve(StringBuilder& builder, size_t) {
// ostringstream does not have a counterpart
}
inline static String substring(const String& string,
size_t start,
size_t count) {
return string.substr(start, count);
}
inline static String fromInteger(int n) { return std::to_string(n); }
inline static String builderToString(const StringBuilder& builder) {
return builder.str();
}
inline static size_t find(const String& string, const char* substring) {
return string.find(substring);
}
static String fromDouble(double d);
static double toDouble(const char* buffer, size_t length, bool* ok);

// Convert Utf16 in local endianness to Utf8 if needed.
static String StringViewToUtf8(v8_inspector::StringView view);

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

static ProtocolMessage jsonToMessage(String message);
static ProtocolMessage binaryToMessage(std::vector<uint8_t> message);
static String fromUTF8(const uint8_t* data, size_t length);
static String fromUTF16LE(const uint16_t* data, size_t length);
static const uint8_t* CharactersUTF8(const std::string_view s);
Expand Down

0 comments on commit ab3d03c

Please sign in to comment.