From e3fd788e66d9a29fb265ab944e856bfa1db09eb3 Mon Sep 17 00:00:00 2001 From: DC Date: Tue, 6 Dec 2016 23:28:06 -0800 Subject: [PATCH 1/4] test: update Buffer.lastIndexOf Test type coercion for non-number offset arguments. Verify that Buffer and String behave the same way. --- test/parallel/test-buffer-indexof.js | 31 +++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-buffer-indexof.js b/test/parallel/test-buffer-indexof.js index 208fed08cdf45c..ffec5e0a661300 100644 --- a/test/parallel/test-buffer-indexof.js +++ b/test/parallel/test-buffer-indexof.js @@ -413,14 +413,39 @@ assert.strictEqual(b.lastIndexOf(0x61, Infinity), 0); assert.strictEqual(b.lastIndexOf(0x0), -1); // Test weird offset arguments. -// Behaviour should match String.lastIndexOf: -assert.strictEqual(b.lastIndexOf('b', 0), -1); +// The following offsets coerce to NaN, searching the whole Buffer (or String) assert.strictEqual(b.lastIndexOf('b', undefined), 1); -assert.strictEqual(b.lastIndexOf('b', null), -1); assert.strictEqual(b.lastIndexOf('b', {}), 1); + +// The following offsets coerce to 0 +assert.strictEqual(b.lastIndexOf('b', 0), -1); +assert.strictEqual(b.lastIndexOf('b', null), -1); assert.strictEqual(b.lastIndexOf('b', []), -1); + +// The following offset coerces to 2, in other words +[2] === 2 assert.strictEqual(b.lastIndexOf('b', [2]), 1); +// Behavior should match String.lastIndexOf() +var s = 'abcdef'; +assert.strictEqual( + b.lastIndexOf('b', undefined), + s.lastIndexOf('b', undefined)); +assert.strictEqual( + b.lastIndexOf('b', {}), + s.lastIndexOf('b', {})); +assert.strictEqual( + b.lastIndexOf('b', 0), + s.lastIndexOf('b', 0)); +assert.strictEqual( + b.lastIndexOf('b', null), + s.lastIndexOf('b', null)); +assert.strictEqual( + b.lastIndexOf('b', []), + s.lastIndexOf('b', [])); +assert.strictEqual( + b.lastIndexOf('b', [2]), + s.lastIndexOf('b', [2])); + // Test needles longer than the haystack. assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'ucs2'), -1); assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'utf8'), -1); From 75af3ef4233c7274a77f77c278fd7cffd6e99256 Mon Sep 17 00:00:00 2001 From: DC Date: Wed, 7 Dec 2016 00:36:16 -0800 Subject: [PATCH 2/4] doc: clarify Buffer.indexOf/lastIndexOf edge cases --- doc/api/buffer.md | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index f4ef0b5bd15be7..5d39edbe1321a5 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -1164,6 +1164,30 @@ console.log(utf16Buffer.indexOf('\u03a3', 0, 'ucs2')); console.log(utf16Buffer.indexOf('\u03a3', -4, 'ucs2')); ``` +If `value` is not a string, number, or `Buffer`, this method will throw a +`TypeError`. If `value` is a number, it will be coerced to a valid byte value, +an integer between 0 and 255. + +If `byteOffset` is not a number, it will be coerced to a number. Any arguments +that coerce to `NaN` or 0, like `{}`, `[]`, `null` or `undefined`, will search +the whole buffer. This behavior matches [`String#indexOf()`]. + +```js +const b = Buffer.from('abcdef'); + +// Passing a value that's a number, but not a valid byte +// Prints: 2, equivalent to searching for 99 or 'c' +console.log(b.indexOf(99.9)); +console.log(b.indexOf(256 + 99)); + +// Passing a byteOffset that coerces to NaN or 0 +// Prints: 1, searching the whole buffer +console.log(b.indexOf('b', undefined)); +console.log(b.indexOf('b', {})); +console.log(b.indexOf('b', null)); +console.log(b.indexOf('b', [])); +``` + ### buf.includes(value[, byteOffset][, encoding])