-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
indexOf empty string in buffer should return 0 #13023
Comments
Hmm - the Buffer#indexOf documentation explicitly states that it will return -1 if the value is not found. https://nodejs.org/api/buffer.html#buffer_buf_indexof_value_byteoffset_encoding
I believe this is expected behavior. |
See what @addaleax said below!The following information is wrong, see #13023 (comment)!
|
@lance I think this is a bug – it makes sense for an empty input value to say that it is found immediately, without any searching. Also, @TimothyGu Unlike This should be enough to fix: diffdiff --git a/src/node_buffer.cc b/src/node_buffer.cc
index 367af6592ff3..32a942a42c4e 100644
--- a/src/node_buffer.cc
+++ b/src/node_buffer.cc
@@ -974,7 +974,13 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
const size_t needle_length =
StringBytes::Size(args.GetIsolate(), needle, enc);
- if (needle_length == 0 || haystack_length == 0) {
+ if (needle_length == 0) {
+ const double first_index = is_forward ? 0 : haystack_length;
+ args.GetReturnValue().Set(first_index);
+ return;
+ }
+
+ if (haystack_length == 0) {
return args.GetReturnValue().Set(-1);
}
@@ -1077,7 +1083,13 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) {
const char* needle = buf_data;
const size_t needle_length = buf_length;
- if (needle_length == 0 || haystack_length == 0) {
+ if (needle_length == 0) {
+ const double first_index = is_forward ? 0 : haystack_length;
+ args.GetReturnValue().Set(first_index);
+ return;
+ }
+
+ if (haystack_length == 0) {
return args.GetReturnValue().Set(-1);
} |
Oops, read what @addaleax said :) |
@addaleax I see your point - especially given that String.prototype.indexOf works this way. Comment retracted. And your diff looks reasonable to me. |
Make searches for empty subsequences do exactly what `String.prototype.indexOf()` does. Fixes: nodejs#13023
Welcome for the changes. But I would like to explain consistency more.
There is no consistency in the first place. However, existing |
Hmm, since the spec has already defined
I would say the current behavior is more consistent..especially when we are on our way making APIs that accept |
@joyeecheung That’s what |
Make searches for empty subsequences do exactly what `String.prototype.indexOf()` does. Fixes: nodejs#13023 PR-URL: nodejs#13024 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
"abc".indexOf("")
return 0.But
Buffer.from("abc").indexOf("")
return -1.Expected result : 0
The text was updated successfully, but these errors were encountered: