@@ -1013,33 +1013,23 @@ int64_t IndexOfOffset(size_t length, |
1013 | 1013 | } |
1014 | 1014 | } |
1015 | 1015 |
|
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; |
1043 | 1033 |
|
1044 | 1034 | // search_end is the exclusive upper bound of the search range. |
1045 | 1035 | size_t search_end = static_cast<size_t>(std::min( |
@@ -1055,33 +1045,79 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) { |
1055 | 1045 | // Match String#indexOf() and String#lastIndexOf() behavior, |
1056 | 1046 | // but clamp to search_end. |
1057 | 1047 | 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; |
1064 | 1050 | } |
1065 | 1051 |
|
1066 | | - if (opt_offset <= -1) { |
1067 | | - return args.GetReturnValue().Set(-1); |
| 1052 | + if (haystack_length == 0 || opt_offset <= -1) { |
| 1053 | + return false; |
1068 | 1054 | } |
1069 | 1055 | size_t offset = static_cast<size_t>(opt_offset); |
1070 | 1056 | // For backward search, clamp start to within the search range. |
1071 | 1057 | if (!is_forward && offset >= search_end) { |
1072 | | - if (search_end == 0) return args.GetReturnValue().Set(-1); |
| 1058 | + if (search_end == 0) return false; |
1073 | 1059 | offset = search_end - 1; |
1074 | 1060 | } else if (is_forward && offset >= search_end) { |
1075 | | - return args.GetReturnValue().Set(-1); |
| 1061 | + return false; |
1076 | 1062 | } |
1077 | | - if (enc == UCS2 && is_forward) { |
| 1063 | + if (align_ucs2_offset && enc == UCS2 && is_forward) { |
1078 | 1064 | offset += offset % sizeof(uint16_t); |
1079 | | - if (offset >= search_end) return args.GetReturnValue().Set(-1); |
| 1065 | + if (offset >= search_end) return false; |
1080 | 1066 | } |
1081 | 1067 | CHECK_LT(offset, haystack_length); |
1082 | 1068 | if ((is_forward && needle_length + offset > search_end) || |
1083 | 1069 | 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); |
1085 | 1121 | } |
1086 | 1122 |
|
1087 | 1123 | size_t result = search_end; |
@@ -1172,43 +1208,20 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) { |
1172 | 1208 | const char* needle = needle_contents.data(); |
1173 | 1209 | const size_t needle_length = needle_contents.length(); |
1174 | 1210 |
|
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); |
1212 | 1225 | } |
1213 | 1226 |
|
1214 | 1227 | size_t result = search_end; |
|