Skip to content

Commit 1576730

Browse files
addaleaxevanlucas
authored andcommitted
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 2a84da5 commit 1576730

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

doc/api/buffer.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,8 @@ Operates similar to [`Array#indexOf()`][] in that it returns either the
973973
starting index position of `value` in Buffer or `-1` if the Buffer does not
974974
contain `value`. The `value` can be a String, Buffer or Number. Strings are by
975975
default interpreted as UTF8. Buffers will use the entire Buffer (to compare a
976-
partial Buffer use [`buf.slice()`][]). Numbers can range from 0 to 255.
976+
partial Buffer use [`buf.slice()`][]). Numbers will be interpreted as unsigned 8-bit
977+
integer values between `0` and `255`.
977978

978979
```js
979980
const buf = Buffer.from('this is a buffer');
@@ -1012,7 +1013,8 @@ added: v5.3.0
10121013
Operates similar to [`Array#includes()`][]. The `value` can be a String, Buffer
10131014
or Number. Strings are interpreted as UTF8 unless overridden with the
10141015
`encoding` argument. Buffers will use the entire Buffer (to compare a partial
1015-
Buffer use [`buf.slice()`][]). Numbers can range from 0 to 255.
1016+
Buffer use [`buf.slice()`][]). Numbers will be interpreted as unsigned 8-bit
1017+
integer values between `0` and `255`.
10161018

10171019
The `byteOffset` indicates the index in `buf` where searching begins.
10181020

test/parallel/test-buffer-includes.js

+16
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,19 @@ assert.throws(function() {
256256
assert.throws(function() {
257257
b.includes([]);
258258
});
259+
260+
// test truncation of Number arguments to uint8
261+
{
262+
const buf = Buffer.from('this is a test');
263+
assert.ok(buf.includes(0x6973));
264+
assert.ok(buf.includes(0x697320));
265+
assert.ok(buf.includes(0x69732069));
266+
assert.ok(buf.includes(0x697374657374));
267+
assert.ok(buf.includes(0x69737374));
268+
assert.ok(buf.includes(0x69737465));
269+
assert.ok(buf.includes(0x69737465));
270+
assert.ok(buf.includes(-140));
271+
assert.ok(buf.includes(-152));
272+
assert.ok(!buf.includes(0xff));
273+
assert.ok(!buf.includes(0xffff));
274+
}

test/parallel/test-buffer-indexof.js

+16
Original file line numberDiff line numberDiff line change
@@ -448,3 +448,19 @@ pattern = reallyLong.slice(0, 1000000); // First 1/5th.
448448
assert.equal(3932160, reallyLong.lastIndexOf(pattern));
449449
pattern = reallyLong.slice(0, 2000000); // first 2/5ths.
450450
assert.equal(0, reallyLong.lastIndexOf(pattern));
451+
452+
// test truncation of Number arguments to uint8
453+
{
454+
const buf = Buffer.from('this is a test');
455+
assert.strictEqual(buf.indexOf(0x6973), 3);
456+
assert.strictEqual(buf.indexOf(0x697320), 4);
457+
assert.strictEqual(buf.indexOf(0x69732069), 2);
458+
assert.strictEqual(buf.indexOf(0x697374657374), 0);
459+
assert.strictEqual(buf.indexOf(0x69737374), 0);
460+
assert.strictEqual(buf.indexOf(0x69737465), 11);
461+
assert.strictEqual(buf.indexOf(0x69737465), 11);
462+
assert.strictEqual(buf.indexOf(-140), 0);
463+
assert.strictEqual(buf.indexOf(-152), 1);
464+
assert.strictEqual(buf.indexOf(0xff), -1);
465+
assert.strictEqual(buf.indexOf(0xffff), -1);
466+
}

0 commit comments

Comments
 (0)