Skip to content

Commit 715f756

Browse files
lemiretpoisseau
authored andcommitted
src: simplify string_bytes with views
PR-URL: nodejs#54876 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
1 parent 8545f2d commit 715f756

File tree

1 file changed

+26
-57
lines changed

1 file changed

+26
-57
lines changed

src/string_bytes.cc

Lines changed: 26 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ size_t StringBytes::Write(Isolate* isolate,
248248

249249
CHECK(val->IsString() == true);
250250
Local<String> str = val.As<String>();
251+
String::ValueView input_view(isolate, str);
251252

252253
int flags = String::HINT_MANY_WRITES_EXPECTED |
253254
String::NO_NULL_TERMINATION |
@@ -256,10 +257,9 @@ size_t StringBytes::Write(Isolate* isolate,
256257
switch (encoding) {
257258
case ASCII:
258259
case LATIN1:
259-
if (str->IsExternalOneByte()) {
260-
auto ext = str->GetExternalOneByteStringResource();
261-
nbytes = std::min(buflen, ext->length());
262-
memcpy(buf, ext->data(), nbytes);
260+
if (input_view.is_one_byte()) {
261+
nbytes = std::min(buflen, static_cast<size_t>(input_view.length()));
262+
memcpy(buf, input_view.data8(), nbytes);
263263
} else {
264264
uint8_t* const dst = reinterpret_cast<uint8_t*>(buf);
265265
nbytes = str->WriteOneByte(isolate, dst, 0, buflen, flags);
@@ -284,31 +284,11 @@ size_t StringBytes::Write(Isolate* isolate,
284284
}
285285

286286
case BASE64URL:
287-
if (str->IsExternalOneByte()) { // 8-bit case
288-
auto ext = str->GetExternalOneByteStringResource();
287+
if (input_view.is_one_byte()) { // 8-bit case
289288
size_t written_len = buflen;
290289
auto result = simdutf::base64_to_binary_safe(
291-
ext->data(), ext->length(), buf, written_len, simdutf::base64_url);
292-
if (result.error == simdutf::error_code::SUCCESS) {
293-
nbytes = written_len;
294-
} else {
295-
// The input does not follow the WHATWG forgiving-base64 specification
296-
// adapted for base64url
297-
// https://infra.spec.whatwg.org/#forgiving-base64-decode
298-
nbytes =
299-
nbytes::Base64Decode(buf, buflen, ext->data(), ext->length());
300-
}
301-
} else if (str->IsOneByte()) {
302-
MaybeStackBuffer<uint8_t> stack_buf(str->Length());
303-
str->WriteOneByte(isolate,
304-
stack_buf.out(),
305-
0,
306-
str->Length(),
307-
String::NO_NULL_TERMINATION);
308-
size_t written_len = buflen;
309-
auto result = simdutf::base64_to_binary_safe(
310-
reinterpret_cast<const char*>(*stack_buf),
311-
stack_buf.length(),
290+
reinterpret_cast<const char*>(input_view.data8()),
291+
input_view.length(),
312292
buf,
313293
written_len,
314294
simdutf::base64_url);
@@ -318,8 +298,11 @@ size_t StringBytes::Write(Isolate* isolate,
318298
// The input does not follow the WHATWG forgiving-base64 specification
319299
// (adapted for base64url with + and / replaced by - and _).
320300
// https://infra.spec.whatwg.org/#forgiving-base64-decode
321-
nbytes =
322-
nbytes::Base64Decode(buf, buflen, *stack_buf, stack_buf.length());
301+
nbytes = nbytes::Base64Decode(
302+
buf,
303+
buflen,
304+
reinterpret_cast<const char*>(input_view.data8()),
305+
input_view.length());
323306
}
324307
} else {
325308
String::Value value(isolate, str);
@@ -342,40 +325,23 @@ size_t StringBytes::Write(Isolate* isolate,
342325
break;
343326

344327
case BASE64: {
345-
if (str->IsExternalOneByte()) { // 8-bit case
346-
auto ext = str->GetExternalOneByteStringResource();
328+
if (input_view.is_one_byte()) { // 8-bit case
347329
size_t written_len = buflen;
348330
auto result = simdutf::base64_to_binary_safe(
349-
ext->data(), ext->length(), buf, written_len);
350-
if (result.error == simdutf::error_code::SUCCESS) {
351-
nbytes = written_len;
352-
} else {
353-
// The input does not follow the WHATWG forgiving-base64 specification
354-
// https://infra.spec.whatwg.org/#forgiving-base64-decode
355-
nbytes =
356-
nbytes::Base64Decode(buf, buflen, ext->data(), ext->length());
357-
}
358-
} else if (str->IsOneByte()) {
359-
MaybeStackBuffer<uint8_t> stack_buf(str->Length());
360-
str->WriteOneByte(isolate,
361-
stack_buf.out(),
362-
0,
363-
str->Length(),
364-
String::NO_NULL_TERMINATION);
365-
size_t written_len = buflen;
366-
auto result = simdutf::base64_to_binary_safe(
367-
reinterpret_cast<const char*>(*stack_buf),
368-
stack_buf.length(),
331+
reinterpret_cast<const char*>(input_view.data8()),
332+
input_view.length(),
369333
buf,
370334
written_len);
371335
if (result.error == simdutf::error_code::SUCCESS) {
372336
nbytes = written_len;
373337
} else {
374338
// The input does not follow the WHATWG forgiving-base64 specification
375-
// (adapted for base64url with + and / replaced by - and _).
376339
// https://infra.spec.whatwg.org/#forgiving-base64-decode
377-
nbytes =
378-
nbytes::Base64Decode(buf, buflen, *stack_buf, stack_buf.length());
340+
nbytes = nbytes::Base64Decode(
341+
buf,
342+
buflen,
343+
reinterpret_cast<const char*>(input_view.data8()),
344+
input_view.length());
379345
}
380346
} else {
381347
String::Value value(isolate, str);
@@ -396,9 +362,12 @@ size_t StringBytes::Write(Isolate* isolate,
396362
break;
397363
}
398364
case HEX:
399-
if (str->IsExternalOneByte()) {
400-
auto ext = str->GetExternalOneByteStringResource();
401-
nbytes = nbytes::HexDecode(buf, buflen, ext->data(), ext->length());
365+
if (input_view.is_one_byte()) {
366+
nbytes =
367+
nbytes::HexDecode(buf,
368+
buflen,
369+
reinterpret_cast<const char*>(input_view.data8()),
370+
input_view.length());
402371
} else {
403372
String::Value value(isolate, str);
404373
nbytes = nbytes::HexDecode(buf, buflen, *value, value.length());

0 commit comments

Comments
 (0)