Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/encoding_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -356,20 +356,32 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
const char* data = buffer.data();
size_t length = buffer.length();

if (!ignore_bom && length >= 3) {
if (memcmp(data, "\xEF\xBB\xBF", 3) == 0) {
data += 3;
length -= 3;
}
}

if (has_fatal) {
// Are we perhaps ASCII? Then we won't have to check for UTF-8
if (!simdutf::validate_ascii_with_errors(data, length).error) {
Local<Value> ret;
if (StringBytes::Encode(env->isolate(), data, length, LATIN1)
.ToLocal(&ret)) {
args.GetReturnValue().Set(ret);
}
return;
}

auto result = simdutf::validate_utf8_with_errors(data, length);

if (result.error) {
return node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA(
env->isolate(), "The encoded data was not valid for encoding utf-8");
}
}

if (!ignore_bom && length >= 3) {
if (memcmp(data, "\xEF\xBB\xBF", 3) == 0) {
data += 3;
length -= 3;
}
// TODO(chalker): save on utf8 validity recheck in StringBytes::Encode()
}

if (length == 0) return args.GetReturnValue().SetEmptyString();
Expand Down
18 changes: 18 additions & 0 deletions src/string_bytes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,24 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,

case UTF8: {
buflen = keep_buflen_in_range(buflen);

// ASCII fast path
// TODO(chalker): remove when String::NewFromUtf8 is fast enough itself
// This is cheap compared to the benefits though
if (!simdutf::validate_ascii_with_errors(buf, buflen).error) {
return ExternOneByteString::NewFromCopy(isolate, buf, buflen);
}

if (simdutf::validate_utf8(buf, buflen)) {
// We know that we are non-ASCII (and are unlikely Latin1), use 2-byte
// In the most likely case of valid UTF-8, we can use this fast impl
size_t u16size = simdutf::utf16_length_from_utf8(buf, buflen);
uint16_t* dst = node::UncheckedMalloc<uint16_t>(u16size);
size_t utf16len = simdutf::convert_valid_utf8_to_utf16(
buf, buflen, reinterpret_cast<char16_t*>(dst));
return ExternTwoByteString::New(isolate, dst, utf16len);
}

val =
String::NewFromUtf8(isolate, buf, v8::NewStringType::kNormal, buflen);
Local<String> str;
Expand Down