Skip to content

Commit

Permalink
improve stringwrapper bytestring unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Oct 1, 2024
1 parent 924b27e commit f29f4fe
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/workerd/jsg/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -468,18 +468,19 @@ class StringWrapper {
kj::Maybe<v8::Local<v8::Object>> parentObject) {
// TODO(cleanup): Move to a HeaderStringWrapper in the api directory.
v8::Local<v8::String> str = check(handle->ToString(context));
auto result =
ByteString(KJ_ASSERT_NONNULL(tryUnwrap(context, str, (kj::String*)nullptr, parentObject)));
if (str->ContainsOnlyOneByte()) {
auto buf = kj::heapArray<kj::byte>(str->Length() + 1);
str->WriteOneByte(context->GetIsolate(), buf.begin());
for (auto b: buf) {
if (b >= 128) {
result.warning = ByteString::Warning::CONTAINS_EXTENDED_ASCII;
break;
}
}
auto result = ByteString(KJ_ASSERT_NONNULL(
tryUnwrap(context, str, static_cast<kj::String*>(nullptr), parentObject)));

v8::String::ValueView str_view(context->GetIsolate(), str);

if (str_view.is_one_byte()) [[likely]] {
// Storage is one-byte. We don't need to scan.
} else if (str->ContainsOnlyOneByte()) {
// Storage is two-byte, but it contains only one byte.
// No need to scan the string for extended ascii.
result.warning = ByteString::Warning::CONTAINS_EXTENDED_ASCII;
} else {
// Storage is two-bytes, and it doesn't contain "only one-bytes"
result.warning = ByteString::Warning::CONTAINS_UNICODE;
}
return kj::mv(result);
Expand Down

0 comments on commit f29f4fe

Please sign in to comment.