-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
buffer: fix indexOf for empty searches #13024
Conversation
Q: |
Documentation update to include this info? MDN mentions the behavior in its docs for String.prototype.lastIndexOf() and String.prototype.indexOf() and we should probably follow suit. |
test/parallel/test-buffer-indexof.js
Outdated
assert.strictEqual(b.indexOf('', b.length + 1), -1); | ||
assert.strictEqual(b.indexOf('', Infinity), -1); | ||
assert.strictEqual(b.indexOf(''), 0); | ||
assert.strictEqual(b.indexOf('', 1), 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To match the behavior of String.prototype.indexOf()
, this should return 1
rather than 0
.
test/parallel/test-buffer-indexof.js
Outdated
assert.strictEqual(b.indexOf('', Infinity), -1); | ||
assert.strictEqual(b.indexOf(''), 0); | ||
assert.strictEqual(b.indexOf('', 1), 0); | ||
assert.strictEqual(b.indexOf('', b.length + 1), 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To match the behavior of String.prototype.indexOf()
, this should return b.length
, not 0
.
test/parallel/test-buffer-indexof.js
Outdated
assert.strictEqual(b.indexOf(''), 0); | ||
assert.strictEqual(b.indexOf('', 1), 0); | ||
assert.strictEqual(b.indexOf('', b.length + 1), 0); | ||
assert.strictEqual(b.indexOf('', Infinity), 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To match the behavior of String.prototype.indexOf(), this should return b.length, not 0.
ce70e2d
to
8bcee3f
Compare
@Trott updated! |
doc/api/buffer.md
Outdated
@@ -1340,6 +1340,10 @@ console.log(b.indexOf('b', null)); | |||
console.log(b.indexOf('b', [])); | |||
``` | |||
|
|||
If `value` is an empty string or `Buffer` and `byteOffset` is less |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: empty string or empty Buffer
would avoid confusion over whether or not the Buffer
must be empty.
Might be even simpler to replace is an empty string or Buffer
with is empty
or has a length of zero
?
If
value
is empty andbyteOffset
is less
or
If
value
has a length of zero and byteOffset is less
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, makes sense. I went with the former because is empty
sounds a bid odd to me (like empty/undefined variables?), and has a length of zero
doesn’t make sense for integer input (and users can figure that out, but, you know), so I chose the imho explicit version.
doc/api/buffer.md
Outdated
@@ -1450,6 +1454,8 @@ console.log(b.lastIndexOf('b', null)); | |||
console.log(b.lastIndexOf('b', [])); | |||
``` | |||
|
|||
If `value` is an empty string or `Buffer`, `byteOffset` will be returned. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above comment.
Make searches for empty subsequences do exactly what `String.prototype.indexOf()` does. Fixes: nodejs#13023
8bcee3f
to
bb875a5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM to me if CI is green and doc nits are addressed. Not sure about the semver-ness here, but maybe CITGM run results would help make the case for patch vs. major?
That’s fair: CITGM: https://ci.nodejs.org/view/Node.js-citgm/job/citgm-smoker/784/ |
Assuming previous Windows fails was infra. Rerunning: https://ci.nodejs.org/job/node-test-commit-windows-fanned/9084/ |
// Valid positive offset. | ||
return offset_i64; | ||
} else if (needle_length == 0) { | ||
// Out of buffer bounds, but empty needle: point to end of buffer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Either ref - http://www.ecma-international.org/ecma-262/6.0/#sec-string.prototype.indexof
Or state Same semantics as String.prototype.indexof
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@refack Ack, added comments (at the GetReturnValue()
sites)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
I was feeling it missing here (you explain "what" but not "why")
Landed in 28ddac2 |
Make searches for empty subsequences do exactly what `String.prototype.indexOf()` does. Fixes: nodejs#13023 PR-URL: nodejs#13024 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
Make searches for empty subsequences do exactly what `String.prototype.indexOf()` does. Fixes: nodejs#13023 PR-URL: nodejs#13024 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
@addaleax is this applicable for v6.x? |
I'm going to opt to not land this on lts as it does change behavior of buffer. While it might be "more correct", I'd rather not pull the rug under people /cc @addaleax @nodejs/ctc |
I’m okay with that. |
I'm ok as well. |
Make searches for empty subsequences return0
forbuffer.indexOf()
and
buffer.length
forbuffer.lastIndexOf()
, because thoseare the indices of the first and last occurrence of an empty
subsequence, respectively.
Make searches for empty subsequences do exactly what
String.prototype.indexOf()
does.Fixes: #13023
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
buffer
/cc @nodejs/buffer and because people might to consider this a semver-major change (I would not) @nodejs/ctc
CI: https://ci.nodejs.org/job/node-test-commit/9880/