@@ -463,17 +463,17 @@ void StringSlice(const FunctionCallbackInfo<Value>& args) {
463
463
Isolate* isolate = env->isolate ();
464
464
465
465
THROW_AND_RETURN_UNLESS_BUFFER (env, args.This ());
466
- SPREAD_BUFFER_ARG (args.This (), ts_obj );
466
+ ArrayBufferViewContents< char > buffer (args.This ());
467
467
468
- if (ts_obj_length == 0 )
468
+ if (buffer. length () == 0 )
469
469
return args.GetReturnValue ().SetEmptyString ();
470
470
471
- SLICE_START_END (env, args[0 ], args[1 ], ts_obj_length )
471
+ SLICE_START_END (env, args[0 ], args[1 ], buffer. length () )
472
472
473
473
Local<Value> error;
474
474
MaybeLocal<Value> ret =
475
475
StringBytes::Encode (isolate,
476
- ts_obj_data + start,
476
+ buffer. data () + start,
477
477
length,
478
478
encoding,
479
479
&error);
@@ -492,9 +492,8 @@ void Copy(const FunctionCallbackInfo<Value> &args) {
492
492
493
493
THROW_AND_RETURN_UNLESS_BUFFER (env, args[0 ]);
494
494
THROW_AND_RETURN_UNLESS_BUFFER (env, args[1 ]);
495
- Local<Object> buffer_obj = args[0 ]. As <Object>( );
495
+ ArrayBufferViewContents< char > source ( args[0 ]);
496
496
Local<Object> target_obj = args[1 ].As <Object>();
497
- SPREAD_BUFFER_ARG (buffer_obj, ts_obj);
498
497
SPREAD_BUFFER_ARG (target_obj, target);
499
498
500
499
size_t target_start = 0 ;
@@ -503,14 +502,14 @@ void Copy(const FunctionCallbackInfo<Value> &args) {
503
502
504
503
THROW_AND_RETURN_IF_OOB (ParseArrayIndex (env, args[2 ], 0 , &target_start));
505
504
THROW_AND_RETURN_IF_OOB (ParseArrayIndex (env, args[3 ], 0 , &source_start));
506
- THROW_AND_RETURN_IF_OOB (ParseArrayIndex (env, args[4 ], ts_obj_length ,
505
+ THROW_AND_RETURN_IF_OOB (ParseArrayIndex (env, args[4 ], source. length () ,
507
506
&source_end));
508
507
509
508
// Copy 0 bytes; we're done
510
509
if (target_start >= target_length || source_start >= source_end)
511
510
return args.GetReturnValue ().Set (0 );
512
511
513
- if (source_start > ts_obj_length )
512
+ if (source_start > source. length () )
514
513
return THROW_ERR_OUT_OF_RANGE (
515
514
env, " The value of \" sourceStart\" is out of range." );
516
515
@@ -519,9 +518,9 @@ void Copy(const FunctionCallbackInfo<Value> &args) {
519
518
520
519
uint32_t to_copy = std::min (
521
520
std::min (source_end - source_start, target_length - target_start),
522
- ts_obj_length - source_start);
521
+ source. length () - source_start);
523
522
524
- memmove (target_data + target_start, ts_obj_data + source_start, to_copy);
523
+ memmove (target_data + target_start, source. data () + source_start, to_copy);
525
524
args.GetReturnValue ().Set (to_copy);
526
525
}
527
526
@@ -689,8 +688,8 @@ void CompareOffset(const FunctionCallbackInfo<Value> &args) {
689
688
690
689
THROW_AND_RETURN_UNLESS_BUFFER (env, args[0 ]);
691
690
THROW_AND_RETURN_UNLESS_BUFFER (env, args[1 ]);
692
- SPREAD_BUFFER_ARG (args[0 ], ts_obj );
693
- SPREAD_BUFFER_ARG (args[1 ], target );
691
+ ArrayBufferViewContents< char > source (args[0 ]);
692
+ ArrayBufferViewContents< char > target (args[1 ]);
694
693
695
694
size_t target_start = 0 ;
696
695
size_t source_start = 0 ;
@@ -699,15 +698,15 @@ void CompareOffset(const FunctionCallbackInfo<Value> &args) {
699
698
700
699
THROW_AND_RETURN_IF_OOB (ParseArrayIndex (env, args[2 ], 0 , &target_start));
701
700
THROW_AND_RETURN_IF_OOB (ParseArrayIndex (env, args[3 ], 0 , &source_start));
702
- THROW_AND_RETURN_IF_OOB (ParseArrayIndex (env, args[4 ], target_length ,
701
+ THROW_AND_RETURN_IF_OOB (ParseArrayIndex (env, args[4 ], target. length () ,
703
702
&target_end));
704
- THROW_AND_RETURN_IF_OOB (ParseArrayIndex (env, args[5 ], ts_obj_length ,
703
+ THROW_AND_RETURN_IF_OOB (ParseArrayIndex (env, args[5 ], source. length () ,
705
704
&source_end));
706
705
707
- if (source_start > ts_obj_length )
706
+ if (source_start > source. length () )
708
707
return THROW_ERR_OUT_OF_RANGE (
709
708
env, " The value of \" sourceStart\" is out of range." );
710
- if (target_start > target_length )
709
+ if (target_start > target. length () )
711
710
return THROW_ERR_OUT_OF_RANGE (
712
711
env, " The value of \" targetStart\" is out of range." );
713
712
@@ -716,11 +715,11 @@ void CompareOffset(const FunctionCallbackInfo<Value> &args) {
716
715
717
716
size_t to_cmp =
718
717
std::min (std::min (source_end - source_start, target_end - target_start),
719
- ts_obj_length - source_start);
718
+ source. length () - source_start);
720
719
721
720
int val = normalizeCompareVal (to_cmp > 0 ?
722
- memcmp (ts_obj_data + source_start,
723
- target_data + target_start,
721
+ memcmp (source. data () + source_start,
722
+ target. data () + target_start,
724
723
to_cmp) : 0 ,
725
724
source_end - source_start,
726
725
target_end - target_start);
@@ -733,14 +732,14 @@ void Compare(const FunctionCallbackInfo<Value> &args) {
733
732
734
733
THROW_AND_RETURN_UNLESS_BUFFER (env, args[0 ]);
735
734
THROW_AND_RETURN_UNLESS_BUFFER (env, args[1 ]);
736
- SPREAD_BUFFER_ARG (args[0 ], obj_a );
737
- SPREAD_BUFFER_ARG (args[1 ], obj_b );
735
+ ArrayBufferViewContents< char > a (args[0 ]);
736
+ ArrayBufferViewContents< char > b (args[1 ]);
738
737
739
- size_t cmp_length = std::min (obj_a_length, obj_b_length );
738
+ size_t cmp_length = std::min (a. length (), b. length () );
740
739
741
740
int val = normalizeCompareVal (cmp_length > 0 ?
742
- memcmp (obj_a_data, obj_b_data , cmp_length) : 0 ,
743
- obj_a_length, obj_b_length );
741
+ memcmp (a. data (), b. data () , cmp_length) : 0 ,
742
+ a. length (), b. length () );
744
743
args.GetReturnValue ().Set (val);
745
744
}
746
745
@@ -792,16 +791,16 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
792
791
enum encoding enc = ParseEncoding (isolate, args[3 ], UTF8);
793
792
794
793
THROW_AND_RETURN_UNLESS_BUFFER (env, args[0 ]);
795
- SPREAD_BUFFER_ARG (args[0 ], ts_obj );
794
+ ArrayBufferViewContents< char > buffer (args[0 ]);
796
795
797
796
Local<String> needle = args[1 ].As <String>();
798
797
int64_t offset_i64 = args[2 ].As <Integer>()->Value ();
799
798
bool is_forward = args[4 ]->IsTrue ();
800
799
801
- const char * haystack = ts_obj_data ;
800
+ const char * haystack = buffer. data () ;
802
801
// Round down to the nearest multiple of 2 in case of UCS2.
803
802
const size_t haystack_length = (enc == UCS2) ?
804
- ts_obj_length &~ 1 : ts_obj_length ; // NOLINT(whitespace/operators)
803
+ buffer. length () &~ 1 : buffer. length () ; // NOLINT(whitespace/operators)
805
804
806
805
size_t needle_length;
807
806
if (!StringBytes::Size (isolate, needle, enc).To (&needle_length)) return ;
@@ -909,15 +908,15 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) {
909
908
910
909
THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[0 ]);
911
910
THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[1 ]);
912
- SPREAD_BUFFER_ARG (args[0 ], ts_obj );
913
- SPREAD_BUFFER_ARG (args[1 ], buf );
911
+ ArrayBufferViewContents< char > haystack_contents (args[0 ]);
912
+ ArrayBufferViewContents< char > needle_contents (args[1 ]);
914
913
int64_t offset_i64 = args[2 ].As <Integer>()->Value ();
915
914
bool is_forward = args[4 ]->IsTrue ();
916
915
917
- const char * haystack = ts_obj_data ;
918
- const size_t haystack_length = ts_obj_length ;
919
- const char * needle = buf_data ;
920
- const size_t needle_length = buf_length ;
916
+ const char * haystack = haystack_contents. data () ;
917
+ const size_t haystack_length = haystack_contents. length () ;
918
+ const char * needle = needle_contents. data () ;
919
+ const size_t needle_length = needle_contents. length () ;
921
920
922
921
int64_t opt_offset = IndexOfOffset (haystack_length,
923
922
offset_i64,
@@ -978,27 +977,28 @@ void IndexOfNumber(const FunctionCallbackInfo<Value>& args) {
978
977
CHECK (args[3 ]->IsBoolean ());
979
978
980
979
THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[0 ]);
981
- SPREAD_BUFFER_ARG (args[0 ], ts_obj );
980
+ ArrayBufferViewContents< char > buffer (args[0 ]);
982
981
983
982
uint32_t needle = args[1 ].As <Uint32>()->Value ();
984
983
int64_t offset_i64 = args[2 ].As <Integer>()->Value ();
985
984
bool is_forward = args[3 ]->IsTrue ();
986
985
987
- int64_t opt_offset = IndexOfOffset (ts_obj_length, offset_i64, 1 , is_forward);
988
- if (opt_offset <= -1 || ts_obj_length == 0 ) {
986
+ int64_t opt_offset =
987
+ IndexOfOffset (buffer.length (), offset_i64, 1 , is_forward);
988
+ if (opt_offset <= -1 || buffer.length () == 0 ) {
989
989
return args.GetReturnValue ().Set (-1 );
990
990
}
991
991
size_t offset = static_cast <size_t >(opt_offset);
992
- CHECK_LT (offset, ts_obj_length );
992
+ CHECK_LT (offset, buffer. length () );
993
993
994
994
const void * ptr;
995
995
if (is_forward) {
996
- ptr = memchr (ts_obj_data + offset, needle, ts_obj_length - offset);
996
+ ptr = memchr (buffer. data () + offset, needle, buffer. length () - offset);
997
997
} else {
998
- ptr = node::stringsearch::MemrchrFill (ts_obj_data , needle, offset + 1 );
998
+ ptr = node::stringsearch::MemrchrFill (buffer. data () , needle, offset + 1 );
999
999
}
1000
1000
const char * ptr_char = static_cast <const char *>(ptr);
1001
- args.GetReturnValue ().Set (ptr ? static_cast <int >(ptr_char - ts_obj_data )
1001
+ args.GetReturnValue ().Set (ptr ? static_cast <int >(ptr_char - buffer. data () )
1002
1002
: -1 );
1003
1003
}
1004
1004
0 commit comments