Skip to content
Open
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
26 changes: 21 additions & 5 deletions benchmark/util/text-decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,42 @@ const bench = common.createBenchmark(main, {
encoding: ['utf-8', 'windows-1252', 'iso-8859-3'],
ignoreBOM: [0, 1],
fatal: [0, 1],
type: ['SharedArrayBuffer', 'ArrayBuffer', 'Buffer'],
content: ['ascii', 'one-byte-string', 'two-byte-string'],
len: [256, 1024 * 16, 1024 * 128],
n: [1e3],
type: ['SharedArrayBuffer', 'ArrayBuffer', 'Buffer'],
});

function main({ encoding, len, n, ignoreBOM, type, fatal }) {
function buildContent(content, len) {
let base;
switch (content) {
case 'ascii': base = 'a'; break;
case 'one-byte-string': base = '\xff'; break;
case 'two-byte-string': base = 'ğ'; break;
}
const unitBytes = Buffer.byteLength(base, 'utf8');
const copies = Math.max(1, Math.floor(len / unitBytes));
return Buffer.from(base.repeat(copies));
}

function main({ encoding, len, n, ignoreBOM, type, fatal, content }) {
const decoder = new TextDecoder(encoding, { ignoreBOM, fatal });
const seed = buildContent(content, len);
let buf;

switch (type) {
case 'SharedArrayBuffer': {
buf = new SharedArrayBuffer(len);
buf = new SharedArrayBuffer(seed.length);
new Uint8Array(buf).set(seed);
break;
}
case 'ArrayBuffer': {
buf = new ArrayBuffer(len);
buf = new ArrayBuffer(seed.length);
new Uint8Array(buf).set(seed);
break;
}
case 'Buffer': {
buf = Buffer.allocUnsafe(len);
buf = seed;
break;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/encoding_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -459,14 +459,15 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
return node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA(
env->isolate(), "The encoded data was not valid for encoding utf-8");
}

// TODO(chalker): save on utf8 validity recheck in StringBytes::Encode()
}

if (length == 0) return args.GetReturnValue().SetEmptyString();

Local<Value> ret;
if (StringBytes::Encode(env->isolate(), data, length, UTF8).ToLocal(&ret)) {
v8::MaybeLocal<Value> encoded =
has_fatal ? StringBytes::EncodeValidUtf8(env->isolate(), data, length)
: StringBytes::Encode(env->isolate(), data, length, UTF8);
if (encoded.ToLocal(&ret)) {
args.GetReturnValue().Set(ret);
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/string_bytes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,40 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
}
}

MaybeLocal<Value> StringBytes::EncodeValidUtf8(Isolate* isolate,
const char* buf,
size_t buflen) {
CHECK_BUFLEN_IN_RANGE(buflen);
if (!buflen) return String::Empty(isolate);
buflen = keep_buflen_in_range(buflen);

// ASCII fast path
if (!simdutf::validate_ascii_with_errors(buf, buflen).error) {
return ExternOneByteString::NewFromCopy(isolate, buf, buflen);
}

if (buflen >= 32) {
size_t u16size = simdutf::utf16_length_from_utf8(buf, buflen);
if (u16size > static_cast<size_t>(v8::String::kMaxLength)) {
isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
return MaybeLocal<Value>();
}
return EncodeTwoByteString(
isolate, u16size, [buf, buflen, u16size](uint16_t* dst) {
size_t written = simdutf::convert_valid_utf8_to_utf16(
buf, buflen, reinterpret_cast<char16_t*>(dst));
CHECK_EQ(written, u16size);
});
}

Local<String> str;
if (!String::NewFromUtf8(isolate, buf, v8::NewStringType::kNormal, buflen)
.ToLocal(&str)) {
isolate->ThrowException(node::ERR_STRING_TOO_LONG(isolate));
}
return str;
}

MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
const uint16_t* buf,
size_t buflen) {
Expand Down
5 changes: 5 additions & 0 deletions src/string_bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class StringBytes {
size_t buflen,
enum encoding encoding);

// Like Encode(..., UTF8) but does not re-validate. Input must be valid UTF-8.
static v8::MaybeLocal<v8::Value> EncodeValidUtf8(v8::Isolate* isolate,
const char* buf,
size_t buflen);

// Warning: This reverses endianness on BE platforms, even though the
// signature using uint16_t implies that it should not.
// However, the brokenness is already public API and can't therefore
Expand Down
Loading