From 9f5977fdac71200961e5462bc0547f09677746f2 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Thu, 12 Sep 2024 12:07:08 -0400 Subject: [PATCH] src: simplify string_bytes with views PR-URL: https://github.com/nodejs/node/pull/54876 Reviewed-By: James M Snell Reviewed-By: Yagiz Nizipli Reviewed-By: Minwoo Jung --- src/string_bytes.cc | 83 ++++++++++++++------------------------------- 1 file changed, 26 insertions(+), 57 deletions(-) diff --git a/src/string_bytes.cc b/src/string_bytes.cc index 3e2b29005a2..7a5d40b89d4 100644 --- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -248,6 +248,7 @@ size_t StringBytes::Write(Isolate* isolate, CHECK(val->IsString() == true); Local str = val.As(); + String::ValueView input_view(isolate, str); int flags = String::HINT_MANY_WRITES_EXPECTED | String::NO_NULL_TERMINATION | @@ -256,10 +257,9 @@ size_t StringBytes::Write(Isolate* isolate, switch (encoding) { case ASCII: case LATIN1: - if (str->IsExternalOneByte()) { - auto ext = str->GetExternalOneByteStringResource(); - nbytes = std::min(buflen, ext->length()); - memcpy(buf, ext->data(), nbytes); + if (input_view.is_one_byte()) { + nbytes = std::min(buflen, static_cast(input_view.length())); + memcpy(buf, input_view.data8(), nbytes); } else { uint8_t* const dst = reinterpret_cast(buf); nbytes = str->WriteOneByte(isolate, dst, 0, buflen, flags); @@ -284,31 +284,11 @@ size_t StringBytes::Write(Isolate* isolate, } case BASE64URL: - if (str->IsExternalOneByte()) { // 8-bit case - auto ext = str->GetExternalOneByteStringResource(); + if (input_view.is_one_byte()) { // 8-bit case size_t written_len = buflen; auto result = simdutf::base64_to_binary_safe( - ext->data(), ext->length(), buf, written_len, simdutf::base64_url); - if (result.error == simdutf::error_code::SUCCESS) { - nbytes = written_len; - } else { - // The input does not follow the WHATWG forgiving-base64 specification - // adapted for base64url - // https://infra.spec.whatwg.org/#forgiving-base64-decode - nbytes = - nbytes::Base64Decode(buf, buflen, ext->data(), ext->length()); - } - } else if (str->IsOneByte()) { - MaybeStackBuffer stack_buf(str->Length()); - str->WriteOneByte(isolate, - stack_buf.out(), - 0, - str->Length(), - String::NO_NULL_TERMINATION); - size_t written_len = buflen; - auto result = simdutf::base64_to_binary_safe( - reinterpret_cast(*stack_buf), - stack_buf.length(), + reinterpret_cast(input_view.data8()), + input_view.length(), buf, written_len, simdutf::base64_url); @@ -318,8 +298,11 @@ size_t StringBytes::Write(Isolate* isolate, // The input does not follow the WHATWG forgiving-base64 specification // (adapted for base64url with + and / replaced by - and _). // https://infra.spec.whatwg.org/#forgiving-base64-decode - nbytes = - nbytes::Base64Decode(buf, buflen, *stack_buf, stack_buf.length()); + nbytes = nbytes::Base64Decode( + buf, + buflen, + reinterpret_cast(input_view.data8()), + input_view.length()); } } else { String::Value value(isolate, str); @@ -342,40 +325,23 @@ size_t StringBytes::Write(Isolate* isolate, break; case BASE64: { - if (str->IsExternalOneByte()) { // 8-bit case - auto ext = str->GetExternalOneByteStringResource(); + if (input_view.is_one_byte()) { // 8-bit case size_t written_len = buflen; auto result = simdutf::base64_to_binary_safe( - ext->data(), ext->length(), buf, written_len); - if (result.error == simdutf::error_code::SUCCESS) { - nbytes = written_len; - } else { - // The input does not follow the WHATWG forgiving-base64 specification - // https://infra.spec.whatwg.org/#forgiving-base64-decode - nbytes = - nbytes::Base64Decode(buf, buflen, ext->data(), ext->length()); - } - } else if (str->IsOneByte()) { - MaybeStackBuffer stack_buf(str->Length()); - str->WriteOneByte(isolate, - stack_buf.out(), - 0, - str->Length(), - String::NO_NULL_TERMINATION); - size_t written_len = buflen; - auto result = simdutf::base64_to_binary_safe( - reinterpret_cast(*stack_buf), - stack_buf.length(), + reinterpret_cast(input_view.data8()), + input_view.length(), buf, written_len); if (result.error == simdutf::error_code::SUCCESS) { nbytes = written_len; } else { // The input does not follow the WHATWG forgiving-base64 specification - // (adapted for base64url with + and / replaced by - and _). // https://infra.spec.whatwg.org/#forgiving-base64-decode - nbytes = - nbytes::Base64Decode(buf, buflen, *stack_buf, stack_buf.length()); + nbytes = nbytes::Base64Decode( + buf, + buflen, + reinterpret_cast(input_view.data8()), + input_view.length()); } } else { String::Value value(isolate, str); @@ -396,9 +362,12 @@ size_t StringBytes::Write(Isolate* isolate, break; } case HEX: - if (str->IsExternalOneByte()) { - auto ext = str->GetExternalOneByteStringResource(); - nbytes = nbytes::HexDecode(buf, buflen, ext->data(), ext->length()); + if (input_view.is_one_byte()) { + nbytes = + nbytes::HexDecode(buf, + buflen, + reinterpret_cast(input_view.data8()), + input_view.length()); } else { String::Value value(isolate, str); nbytes = nbytes::HexDecode(buf, buflen, *value, value.length());