You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
byteOffset argument in buffer indexOf / lastIndexOf has some contradictions in code vs doc vs test #9801
I agree, this certainly looks like a bug. A offset of null and undefined should result in the same behaviour. Wanna file a PR to fix it in both code and tests?
So it looks like I did that in order to match the behavior of String:
vars="abcdef"varb=newBuffer("abcdef")s.indexOf('b',null)// prints 1b.indexOf('b',null)// prints 1s.lastIndexOf('b',null)// prints -1b.lastIndexOf('b',null)// prints -1
...in other words, both String and Buffer coerce an offset of null to zero.
Introducing inconsistency between String and Buffer seems bad. I'll send a PR that fixes the documentation and makes the tests more explicit, while leaving the behavior as is. Thoughts?
After a bit more investigating, it looks like String and Buffer both coerce the offset argument to a number, then use the default offset if the result is NaN. (Using the default offset means searching the whole String or Buffer.)
The bad news is that this leads to some really weird, unintuitive behavior. The good news is that at least they're consistent with each other and with Javascript's unary + operator:
vars="abcdef"varb=newBuffer("abcdef")console.log('OFFSETS THAT COERCE TO NaN')console.log(+{})// prints NaN, so this will turn into the default offsetconsole.log(+undefined)// same here// The following statements all print 1, searching the whole String or Buffers.lastIndexOf('b')s.lastIndexOf('b',undefined)s.lastIndexOf('b',{})b.lastIndexOf('b')b.lastIndexOf('b',undefined)b.lastIndexOf('b',{})console.log('OFFSETS THAT COERCE TO 0')console.log(+null)// print 0console.log(+[])// same here// The following statements all print -1, because they search from offset 0s.lastIndexOf('b',null)s.lastIndexOf('b',[])b.lastIndexOf('b',null)b.lastIndexOf('b',[])
Activity
vsemozhetbyt commentedon Nov 26, 2016
It seems this big PR concerns all points.
silverwind commentedon Dec 4, 2016
I agree, this certainly looks like a bug. A offset of
nullandundefinedshould result in the same behaviour. Wanna file a PR to fix it in both code and tests?vsemozhetbyt commentedon Dec 4, 2016
@silverwind #4846 was a big complex PR. Maybe the author wants to do this? This way all the method system will be taken into account. ping @dcposch
vsemozhetbyt commentedon Dec 7, 2016
Maybe cc #4846 reviewers? @jasnell, @trevnorris.
dcposch commentedon Dec 7, 2016
@vsemozhetbyt @silverwind I have time to look at this today. PR coming soon!
dcposch commentedon Dec 7, 2016
So it looks like I did that in order to match the behavior of String:
...in other words, both String and Buffer coerce an offset of
nullto zero.Introducing inconsistency between String and Buffer seems bad. I'll send a PR that fixes the documentation and makes the tests more explicit, while leaving the behavior as is. Thoughts?
dcposch commentedon Dec 7, 2016
After a bit more investigating, it looks like String and Buffer both coerce the offset argument to a number, then use the default offset if the result is NaN. (Using the default offset means searching the whole String or Buffer.)
The bad news is that this leads to some really weird, unintuitive behavior. The good news is that at least they're consistent with each other and with Javascript's unary + operator:
dcposch commentedon Dec 7, 2016
Looks like my mistake in the docs was fixed here: 6050bbe
Thanks @vsemozhetbyt
vsemozhetbyt commentedon Dec 7, 2016
So, if this is the desired behavior, this comment needs to be fixed:
https://github.com/nodejs/node/blob/master/lib/buffer.js#L593
trevnorris commentedon Dec 9, 2016
@vsemozhetbyt good catch. mind commenting directly on that line of the commit that the comment should be removed?
vsemozhetbyt commentedon Dec 9, 2016
@trevnorris If I get it right, it seems the comment has been changed already.
trevnorris commentedon Dec 9, 2016
@vsemozhetbyt thanks for pointing that out.
doc: clarify Buffer.indexOf/lastIndexOf edge cases
2 remaining items
doc: clarify Buffer.indexOf/lastIndexOf edge cases
buffer: fix comments in bidirectionalIndexOf
test: update Buffer.lastIndexOf
doc: clarify Buffer.indexOf/lastIndexOf edge cases
buffer: fix comments in bidirectionalIndexOf
test: update Buffer.lastIndexOf
doc: clarify Buffer.indexOf/lastIndexOf edge cases
buffer: fix comments in bidirectionalIndexOf
test: update Buffer.lastIndexOf
doc: clarify Buffer.indexOf/lastIndexOf edge cases
buffer: fix comments in bidirectionalIndexOf
test: update Buffer.lastIndexOf
doc: clarify Buffer.indexOf/lastIndexOf edge cases
buffer: fix comments in bidirectionalIndexOf
test: update Buffer.lastIndexOf