@@ -248,6 +248,7 @@ size_t StringBytes::Write(Isolate* isolate,
248
248
249
249
CHECK (val->IsString () == true );
250
250
Local<String> str = val.As <String>();
251
+ String::ValueView input_view (isolate, str);
251
252
252
253
int flags = String::HINT_MANY_WRITES_EXPECTED |
253
254
String::NO_NULL_TERMINATION |
@@ -256,10 +257,9 @@ size_t StringBytes::Write(Isolate* isolate,
256
257
switch (encoding) {
257
258
case ASCII:
258
259
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);
263
263
} else {
264
264
uint8_t * const dst = reinterpret_cast <uint8_t *>(buf);
265
265
nbytes = str->WriteOneByte (isolate, dst, 0 , buflen, flags);
@@ -284,31 +284,11 @@ size_t StringBytes::Write(Isolate* isolate,
284
284
}
285
285
286
286
case BASE64URL:
287
- if (str->IsExternalOneByte ()) { // 8-bit case
288
- auto ext = str->GetExternalOneByteStringResource ();
287
+ if (input_view.is_one_byte ()) { // 8-bit case
289
288
size_t written_len = buflen;
290
289
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 (),
312
292
buf,
313
293
written_len,
314
294
simdutf::base64_url);
@@ -318,8 +298,11 @@ size_t StringBytes::Write(Isolate* isolate,
318
298
// The input does not follow the WHATWG forgiving-base64 specification
319
299
// (adapted for base64url with + and / replaced by - and _).
320
300
// 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 ());
323
306
}
324
307
} else {
325
308
String::Value value (isolate, str);
@@ -342,40 +325,23 @@ size_t StringBytes::Write(Isolate* isolate,
342
325
break ;
343
326
344
327
case BASE64: {
345
- if (str->IsExternalOneByte ()) { // 8-bit case
346
- auto ext = str->GetExternalOneByteStringResource ();
328
+ if (input_view.is_one_byte ()) { // 8-bit case
347
329
size_t written_len = buflen;
348
330
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 (),
369
333
buf,
370
334
written_len);
371
335
if (result.error == simdutf::error_code::SUCCESS) {
372
336
nbytes = written_len;
373
337
} else {
374
338
// The input does not follow the WHATWG forgiving-base64 specification
375
- // (adapted for base64url with + and / replaced by - and _).
376
339
// 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 ());
379
345
}
380
346
} else {
381
347
String::Value value (isolate, str);
@@ -396,9 +362,12 @@ size_t StringBytes::Write(Isolate* isolate,
396
362
break ;
397
363
}
398
364
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 ());
402
371
} else {
403
372
String::Value value (isolate, str);
404
373
nbytes = nbytes::HexDecode (buf, buflen, *value, value.length ());
0 commit comments