Commit 58fc2309f5d for nodejs
commit 58fc2309f5ddde80bc9dc7e47108b129a30e1dba
Author: James M Snell <jasnell@gmail.com>
Date: Sun Nov 15 18:07:11 2026 +0000
buffer: share search range logic between indexOf implementations
IndexOfString() and IndexOfBuffer() duplicated the computation of the
search range and the early returns for empty needles, empty haystacks,
and out-of-range offsets. Move it into a shared
GetIndexOfSearchRange() helper.
Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67495
Reviewed-By: Guilherme Araújo <arauujogui@gmail.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
diff --git a/src/node_buffer.cc b/src/node_buffer.cc
index 448ef5b6049..e7accbbe8d3 100644
--- a/src/node_buffer.cc
+++ b/src/node_buffer.cc
@@ -1013,33 +1013,23 @@ int64_t IndexOfOffset(size_t length,
}
}
-void IndexOfString(const FunctionCallbackInfo<Value>& args) {
- Environment* env = Environment::GetCurrent(args);
- Isolate* isolate = env->isolate();
-
- CHECK(args[1]->IsString());
- CHECK(args[2]->IsNumber());
- CHECK(args[3]->IsInt32());
- CHECK(args[4]->IsBoolean());
- CHECK(args[5]->IsNumber());
-
- enum encoding enc = static_cast<enum encoding>(args[3].As<Int32>()->Value());
-
- THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
- ArrayBufferViewContents<char> buffer(args[0]);
-
- Local<String> needle = args[1].As<String>();
- int64_t offset_i64 = args[2].As<Integer>()->Value();
- bool is_forward = args[4]->IsTrue();
- int64_t end_i64 = args[5].As<Integer>()->Value();
-
- const char* haystack = buffer.data();
- // Round down to the nearest multiple of 2 in case of UCS2.
- const size_t haystack_length = (enc == UCS2) ?
- buffer.length() &~ 1 : buffer.length(); // NOLINT(whitespace/operators)
-
- size_t needle_length;
- if (!StringBytes::Size(isolate, needle, enc).To(&needle_length)) return;
+// Resolves the range searched by IndexOfString() and IndexOfBuffer(). Returns
+// false if the result is known without searching, in which case
+// `*early_result` holds the value to return. Otherwise, sets `*offset` to the
+// position to start searching from and `*search_end` to the exclusive upper
+// bound of the search range. If `align_ucs2_offset` is true, forward UCS-2
+// searches start at an even byte offset.
+bool GetIndexOfSearchRange(size_t haystack_length,
+ size_t needle_length,
+ int64_t offset_i64,
+ int64_t end_i64,
+ bool is_forward,
+ enum encoding enc,
+ bool align_ucs2_offset,
+ size_t* offset_out,
+ size_t* search_end_out,
+ double* early_result) {
+ *early_result = -1;
// search_end is the exclusive upper bound of the search range.
size_t search_end = static_cast<size_t>(std::min(
@@ -1055,33 +1045,79 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
// Match String#indexOf() and String#lastIndexOf() behavior,
// but clamp to search_end.
int64_t clamped = std::min(opt_offset, static_cast<int64_t>(search_end));
- args.GetReturnValue().Set(static_cast<double>(clamped));
- return;
- }
-
- if (haystack_length == 0) {
- return args.GetReturnValue().Set(-1);
+ *early_result = static_cast<double>(clamped);
+ return false;
}
- if (opt_offset <= -1) {
- return args.GetReturnValue().Set(-1);
+ if (haystack_length == 0 || opt_offset <= -1) {
+ return false;
}
size_t offset = static_cast<size_t>(opt_offset);
// For backward search, clamp start to within the search range.
if (!is_forward && offset >= search_end) {
- if (search_end == 0) return args.GetReturnValue().Set(-1);
+ if (search_end == 0) return false;
offset = search_end - 1;
} else if (is_forward && offset >= search_end) {
- return args.GetReturnValue().Set(-1);
+ return false;
}
- if (enc == UCS2 && is_forward) {
+ if (align_ucs2_offset && enc == UCS2 && is_forward) {
offset += offset % sizeof(uint16_t);
- if (offset >= search_end) return args.GetReturnValue().Set(-1);
+ if (offset >= search_end) return false;
}
CHECK_LT(offset, haystack_length);
if ((is_forward && needle_length + offset > search_end) ||
needle_length > search_end) {
- return args.GetReturnValue().Set(-1);
+ return false;
+ }
+
+ *offset_out = offset;
+ *search_end_out = search_end;
+ return true;
+}
+
+void IndexOfString(const FunctionCallbackInfo<Value>& args) {
+ Environment* env = Environment::GetCurrent(args);
+ Isolate* isolate = env->isolate();
+
+ CHECK(args[1]->IsString());
+ CHECK(args[2]->IsNumber());
+ CHECK(args[3]->IsInt32());
+ CHECK(args[4]->IsBoolean());
+ CHECK(args[5]->IsNumber());
+
+ enum encoding enc = static_cast<enum encoding>(args[3].As<Int32>()->Value());
+
+ THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
+ ArrayBufferViewContents<char> buffer(args[0]);
+
+ Local<String> needle = args[1].As<String>();
+ int64_t offset_i64 = args[2].As<Integer>()->Value();
+ bool is_forward = args[4]->IsTrue();
+ int64_t end_i64 = args[5].As<Integer>()->Value();
+
+ const char* haystack = buffer.data();
+ // Round down to the nearest multiple of 2 in case of UCS2.
+ const size_t haystack_length =
+ (enc == UCS2) ? buffer.length() & ~1
+ : buffer.length(); // NOLINT(whitespace/operators)
+
+ size_t needle_length;
+ if (!StringBytes::Size(isolate, needle, enc).To(&needle_length)) return;
+
+ size_t offset;
+ size_t search_end;
+ double early_result;
+ if (!GetIndexOfSearchRange(haystack_length,
+ needle_length,
+ offset_i64,
+ end_i64,
+ is_forward,
+ enc,
+ true,
+ &offset,
+ &search_end,
+ &early_result)) {
+ return args.GetReturnValue().Set(early_result);
}
size_t result = search_end;
@@ -1172,43 +1208,20 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) {
const char* needle = needle_contents.data();
const size_t needle_length = needle_contents.length();
- // search_end is the exclusive upper bound of the search range.
- size_t search_end = static_cast<size_t>(std::min(
- std::max(end_i64, int64_t{0}), static_cast<int64_t>(haystack_length)));
- if (enc == UCS2) search_end &= ~static_cast<size_t>(1);
-
- int64_t opt_offset = IndexOfOffset(haystack_length,
- offset_i64,
- needle_length,
- is_forward);
-
- if (needle_length == 0) {
- // Match String#indexOf() and String#lastIndexOf() behavior,
- // but clamp to search_end.
- int64_t clamped = std::min(opt_offset, static_cast<int64_t>(search_end));
- args.GetReturnValue().Set(static_cast<double>(clamped));
- return;
- }
-
- if (haystack_length == 0) {
- return args.GetReturnValue().Set(-1);
- }
-
- if (opt_offset <= -1) {
- return args.GetReturnValue().Set(-1);
- }
- size_t offset = static_cast<size_t>(opt_offset);
- // For backward search, clamp start to within the search range.
- if (!is_forward && offset >= search_end) {
- if (search_end == 0) return args.GetReturnValue().Set(-1);
- offset = search_end - 1;
- } else if (is_forward && offset >= search_end) {
- return args.GetReturnValue().Set(-1);
- }
- CHECK_LT(offset, haystack_length);
- if ((is_forward && needle_length + offset > search_end) ||
- needle_length > search_end) {
- return args.GetReturnValue().Set(-1);
+ size_t offset;
+ size_t search_end;
+ double early_result;
+ if (!GetIndexOfSearchRange(haystack_length,
+ needle_length,
+ offset_i64,
+ end_i64,
+ is_forward,
+ enc,
+ false,
+ &offset,
+ &search_end,
+ &early_result)) {
+ return args.GetReturnValue().Set(early_result);
}
size_t result = search_end;