@@ -1013,33 +1013,23 @@ int64_t IndexOfOffset(size_t length,
10131013 }
10141014}
10151015
1016- void IndexOfString (const FunctionCallbackInfo<Value>& args) {
1017- Environment* env = Environment::GetCurrent (args);
1018- Isolate* isolate = env->isolate ();
1019-
1020- CHECK (args[1 ]->IsString ());
1021- CHECK (args[2 ]->IsNumber ());
1022- CHECK (args[3 ]->IsInt32 ());
1023- CHECK (args[4 ]->IsBoolean ());
1024- CHECK (args[5 ]->IsNumber ());
1025-
1026- enum encoding enc = static_cast <enum encoding>(args[3 ].As <Int32>()->Value ());
1027-
1028- THROW_AND_RETURN_UNLESS_BUFFER (env, args[0 ]);
1029- ArrayBufferViewContents<char > buffer (args[0 ]);
1030-
1031- Local<String> needle = args[1 ].As <String>();
1032- int64_t offset_i64 = args[2 ].As <Integer>()->Value ();
1033- bool is_forward = args[4 ]->IsTrue ();
1034- int64_t end_i64 = args[5 ].As <Integer>()->Value ();
1035-
1036- const char * haystack = buffer.data ();
1037- // Round down to the nearest multiple of 2 in case of UCS2.
1038- const size_t haystack_length = (enc == UCS2 ) ?
1039- buffer.length () &~ 1 : buffer.length (); // NOLINT(whitespace/operators)
1040-
1041- size_t needle_length;
1042- if (!StringBytes::Size (isolate, needle, enc).To (&needle_length)) return ;
1016+ // Resolves the range searched by IndexOfString() and IndexOfBuffer(). Returns
1017+ // false if the result is known without searching, in which case
1018+ // `*early_result` holds the value to return. Otherwise, sets `*offset` to the
1019+ // position to start searching from and `*search_end` to the exclusive upper
1020+ // bound of the search range. If `align_ucs2_offset` is true, forward UCS-2
1021+ // searches start at an even byte offset.
1022+ bool GetIndexOfSearchRange (size_t haystack_length,
1023+ size_t needle_length,
1024+ int64_t offset_i64,
1025+ int64_t end_i64,
1026+ bool is_forward,
1027+ enum encoding enc,
1028+ bool align_ucs2_offset,
1029+ size_t * offset_out,
1030+ size_t * search_end_out,
1031+ double * early_result) {
1032+ *early_result = -1 ;
10431033
10441034 // search_end is the exclusive upper bound of the search range.
10451035 size_t search_end = static_cast <size_t >(std::min (
@@ -1055,33 +1045,79 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
10551045 // Match String#indexOf() and String#lastIndexOf() behavior,
10561046 // but clamp to search_end.
10571047 int64_t clamped = std::min (opt_offset, static_cast <int64_t >(search_end));
1058- args.GetReturnValue ().Set (static_cast <double >(clamped));
1059- return ;
1060- }
1061-
1062- if (haystack_length == 0 ) {
1063- return args.GetReturnValue ().Set (-1 );
1048+ *early_result = static_cast <double >(clamped);
1049+ return false ;
10641050 }
10651051
1066- if (opt_offset <= -1 ) {
1067- return args. GetReturnValue (). Set (- 1 ) ;
1052+ if (haystack_length == 0 || opt_offset <= -1 ) {
1053+ return false ;
10681054 }
10691055 size_t offset = static_cast <size_t >(opt_offset);
10701056 // For backward search, clamp start to within the search range.
10711057 if (!is_forward && offset >= search_end) {
1072- if (search_end == 0 ) return args. GetReturnValue (). Set (- 1 ) ;
1058+ if (search_end == 0 ) return false ;
10731059 offset = search_end - 1 ;
10741060 } else if (is_forward && offset >= search_end) {
1075- return args. GetReturnValue (). Set (- 1 ) ;
1061+ return false ;
10761062 }
1077- if (enc == UCS2 && is_forward) {
1063+ if (align_ucs2_offset && enc == UCS2 && is_forward) {
10781064 offset += offset % sizeof (uint16_t );
1079- if (offset >= search_end) return args. GetReturnValue (). Set (- 1 ) ;
1065+ if (offset >= search_end) return false ;
10801066 }
10811067 CHECK_LT (offset, haystack_length);
10821068 if ((is_forward && needle_length + offset > search_end) ||
10831069 needle_length > search_end) {
1084- return args.GetReturnValue ().Set (-1 );
1070+ return false ;
1071+ }
1072+
1073+ *offset_out = offset;
1074+ *search_end_out = search_end;
1075+ return true ;
1076+ }
1077+
1078+ void IndexOfString (const FunctionCallbackInfo<Value>& args) {
1079+ Environment* env = Environment::GetCurrent (args);
1080+ Isolate* isolate = env->isolate ();
1081+
1082+ CHECK (args[1 ]->IsString ());
1083+ CHECK (args[2 ]->IsNumber ());
1084+ CHECK (args[3 ]->IsInt32 ());
1085+ CHECK (args[4 ]->IsBoolean ());
1086+ CHECK (args[5 ]->IsNumber ());
1087+
1088+ enum encoding enc = static_cast <enum encoding>(args[3 ].As <Int32>()->Value ());
1089+
1090+ THROW_AND_RETURN_UNLESS_BUFFER (env, args[0 ]);
1091+ ArrayBufferViewContents<char > buffer (args[0 ]);
1092+
1093+ Local<String> needle = args[1 ].As <String>();
1094+ int64_t offset_i64 = args[2 ].As <Integer>()->Value ();
1095+ bool is_forward = args[4 ]->IsTrue ();
1096+ int64_t end_i64 = args[5 ].As <Integer>()->Value ();
1097+
1098+ const char * haystack = buffer.data ();
1099+ // Round down to the nearest multiple of 2 in case of UCS2.
1100+ const size_t haystack_length =
1101+ (enc == UCS2 ) ? buffer.length () & ~1
1102+ : buffer.length (); // NOLINT(whitespace/operators)
1103+
1104+ size_t needle_length;
1105+ if (!StringBytes::Size (isolate, needle, enc).To (&needle_length)) return ;
1106+
1107+ size_t offset;
1108+ size_t search_end;
1109+ double early_result;
1110+ if (!GetIndexOfSearchRange (haystack_length,
1111+ needle_length,
1112+ offset_i64,
1113+ end_i64,
1114+ is_forward,
1115+ enc,
1116+ true ,
1117+ &offset,
1118+ &search_end,
1119+ &early_result)) {
1120+ return args.GetReturnValue ().Set (early_result);
10851121 }
10861122
10871123 size_t result = search_end;
@@ -1172,43 +1208,20 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) {
11721208 const char * needle = needle_contents.data ();
11731209 const size_t needle_length = needle_contents.length ();
11741210
1175- // search_end is the exclusive upper bound of the search range.
1176- size_t search_end = static_cast <size_t >(std::min (
1177- std::max (end_i64, int64_t {0 }), static_cast <int64_t >(haystack_length)));
1178- if (enc == UCS2 ) search_end &= ~static_cast <size_t >(1 );
1179-
1180- int64_t opt_offset = IndexOfOffset (haystack_length,
1181- offset_i64,
1182- needle_length,
1183- is_forward);
1184-
1185- if (needle_length == 0 ) {
1186- // Match String#indexOf() and String#lastIndexOf() behavior,
1187- // but clamp to search_end.
1188- int64_t clamped = std::min (opt_offset, static_cast <int64_t >(search_end));
1189- args.GetReturnValue ().Set (static_cast <double >(clamped));
1190- return ;
1191- }
1192-
1193- if (haystack_length == 0 ) {
1194- return args.GetReturnValue ().Set (-1 );
1195- }
1196-
1197- if (opt_offset <= -1 ) {
1198- return args.GetReturnValue ().Set (-1 );
1199- }
1200- size_t offset = static_cast <size_t >(opt_offset);
1201- // For backward search, clamp start to within the search range.
1202- if (!is_forward && offset >= search_end) {
1203- if (search_end == 0 ) return args.GetReturnValue ().Set (-1 );
1204- offset = search_end - 1 ;
1205- } else if (is_forward && offset >= search_end) {
1206- return args.GetReturnValue ().Set (-1 );
1207- }
1208- CHECK_LT (offset, haystack_length);
1209- if ((is_forward && needle_length + offset > search_end) ||
1210- needle_length > search_end) {
1211- return args.GetReturnValue ().Set (-1 );
1211+ size_t offset;
1212+ size_t search_end;
1213+ double early_result;
1214+ if (!GetIndexOfSearchRange (haystack_length,
1215+ needle_length,
1216+ offset_i64,
1217+ end_i64,
1218+ is_forward,
1219+ enc,
1220+ false ,
1221+ &offset,
1222+ &search_end,
1223+ &early_result)) {
1224+ return args.GetReturnValue ().Set (early_result);
12121225 }
12131226
12141227 size_t result = search_end;
0 commit comments