Skip to content

Commit a7105a8

Browse files
committed
test,doc: clarify buf.indexOf(num) input range
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 <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent cbbddc4 commit a7105a8

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

doc/api/buffer.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,8 @@ Operates similar to [`Array#indexOf()`][] in that it returns either the
982982
starting index position of `value` in Buffer or `-1` if the Buffer does not
983983
contain `value`. The `value` can be a String, Buffer or Number. Strings are by
984984
default interpreted as UTF8. Buffers will use the entire Buffer (to compare a
985-
partial Buffer use [`buf.slice()`][]). Numbers can range from 0 to 255.
985+
partial Buffer use [`buf.slice()`][]). Numbers will be interpreted as unsigned 8-bit
986+
integer values between `0` and `255`.
986987

987988
```js
988989
const buf = Buffer.from('this is a buffer');
@@ -1021,7 +1022,8 @@ added: v5.3.0
10211022
Operates similar to [`Array#includes()`][]. The `value` can be a String, Buffer
10221023
or Number. Strings are interpreted as UTF8 unless overridden with the
10231024
`encoding` argument. Buffers will use the entire Buffer (to compare a partial
1024-
Buffer use [`buf.slice()`][]). Numbers can range from 0 to 255.
1025+
Buffer use [`buf.slice()`][]). Numbers will be interpreted as unsigned 8-bit
1026+
integer values between `0` and `255`.
10251027

10261028
The `byteOffset` indicates the index in `buf` where searching begins.
10271029

test/parallel/test-buffer-includes.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,19 @@ assert.throws(function() {
264264
assert.throws(function() {
265265
b.includes([]);
266266
});
267+
268+
// test truncation of Number arguments to uint8
269+
{
270+
const buf = Buffer.from('this is a test');
271+
assert.ok(buf.includes(0x6973));
272+
assert.ok(buf.includes(0x697320));
273+
assert.ok(buf.includes(0x69732069));
274+
assert.ok(buf.includes(0x697374657374));
275+
assert.ok(buf.includes(0x69737374));
276+
assert.ok(buf.includes(0x69737465));
277+
assert.ok(buf.includes(0x69737465));
278+
assert.ok(buf.includes(-140));
279+
assert.ok(buf.includes(-152));
280+
assert.ok(!buf.includes(0xff));
281+
assert.ok(!buf.includes(0xffff));
282+
}

test/parallel/test-buffer-indexof.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,3 +470,19 @@ pattern = reallyLong.slice(0, 1000000); // First 1/5th.
470470
assert.equal(3932160, reallyLong.lastIndexOf(pattern));
471471
pattern = reallyLong.slice(0, 2000000); // first 2/5ths.
472472
assert.equal(0, reallyLong.lastIndexOf(pattern));
473+
474+
// test truncation of Number arguments to uint8
475+
{
476+
const buf = Buffer.from('this is a test');
477+
assert.strictEqual(buf.indexOf(0x6973), 3);
478+
assert.strictEqual(buf.indexOf(0x697320), 4);
479+
assert.strictEqual(buf.indexOf(0x69732069), 2);
480+
assert.strictEqual(buf.indexOf(0x697374657374), 0);
481+
assert.strictEqual(buf.indexOf(0x69737374), 0);
482+
assert.strictEqual(buf.indexOf(0x69737465), 11);
483+
assert.strictEqual(buf.indexOf(0x69737465), 11);
484+
assert.strictEqual(buf.indexOf(-140), 0);
485+
assert.strictEqual(buf.indexOf(-152), 1);
486+
assert.strictEqual(buf.indexOf(0xff), -1);
487+
assert.strictEqual(buf.indexOf(0xffff), -1);
488+
}

0 commit comments

Comments
 (0)