The Wayback Machine - https://web.archive.org/web/20241226182114/https://github.com/nodejs/node/commit/ea72e9f143
Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Commit

Permalink
test,doc: clarify buf.indexOf(num) input range
Browse files Browse the repository at this point in the history
Hopefully clarify the behaviour of `buffer.indexOf()` and
`buffer.includes()` for numbers in that they will be
truncated to uint8s.

Add tests for that behaviour.

Fixes: #7591
PR-URL: #7611
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
addaleax authored and MylesBorins committed Oct 26, 2016
1 parent 26e695c commit ea72e9f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion 3 doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,8 @@ Operates similar to [`Array#indexOf()`][] in that it returns either the
starting index position of `value` in Buffer or `-1` if the Buffer does not
contain `value`. The `value` can be a String, Buffer or Number. Strings are by
default interpreted as UTF8. Buffers will use the entire Buffer (to compare a
partial Buffer use [`buf.slice()`][]). Numbers can range from 0 to 255.
partial Buffer use [`buf.slice()`][]). Numbers will be interpreted as unsigned 8-bit
integer values between `0` and `255`.

```js
const buf = new Buffer('this is a buffer');
Expand Down
16 changes: 16 additions & 0 deletions 16 test/parallel/test-buffer-indexof.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,19 @@ assert.throws(function() {
assert.throws(function() {
b.indexOf([]);
});

// test truncation of Number arguments to uint8
{
const buf = Buffer.from('this is a test');
assert.strictEqual(buf.indexOf(0x6973), 3);
assert.strictEqual(buf.indexOf(0x697320), 4);
assert.strictEqual(buf.indexOf(0x69732069), 2);
assert.strictEqual(buf.indexOf(0x697374657374), 0);
assert.strictEqual(buf.indexOf(0x69737374), 0);
assert.strictEqual(buf.indexOf(0x69737465), 11);
assert.strictEqual(buf.indexOf(0x69737465), 11);
assert.strictEqual(buf.indexOf(-140), 0);
assert.strictEqual(buf.indexOf(-152), 1);
assert.strictEqual(buf.indexOf(0xff), -1);
assert.strictEqual(buf.indexOf(0xffff), -1);
}

0 comments on commit ea72e9f

Please sign in to comment.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.