Skip to content

Buffer's indexOf does not seem to handle numbers correctly #7591

Closed
@Sha-Bang

Description

Node seems to behave oddly when using indexOf (or includes) on a buffer

It seems that when a string or buffer is the parameter of indexOf it returns the index of the first byte that matches it; however, when a number is passed in it returns the index of the last byte that matched it, unless there are duplicates, then it behaves very oddly.

$ node
> buf = new Buffer('this is a test');
 <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>
> buf.indexOf('is') // pass
2
> buf.indexOf(new Buffer('is')) // pass
2
> buf.indexOf(new Buffer('istest')) // pass
-1
> buf.indexOf('istest') // pass
-1
> buf.indexOf(0x6973) // odd 
3
> buf.indexOf(0x697320) // appears to return the index of the last byte that matched the number given
4
> buf.indexOf(0x69732069) // unless there are duplicates, then it does weird things
2
> buf.indexOf(0x697374657374) 
0
> buf.indexOf(0x69737374)
0
> buf.indexOf(0x69737465)
11

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions