diff --git a/lib/buffer.js b/lib/buffer.js index 6514d0eb6dcf5d..881bdc2e1183d1 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -477,10 +477,14 @@ function slowIndexOf(buffer, val, byteOffset, encoding) { } Buffer.prototype.indexOf = function indexOf(val, byteOffset, encoding) { - if (byteOffset > 0x7fffffff) + if (typeof byteOffset === 'string') { + encoding = byteOffset; + byteOffset = 0; + } else if (byteOffset > 0x7fffffff) { byteOffset = 0x7fffffff; - else if (byteOffset < -0x80000000) + } else if (byteOffset < -0x80000000) { byteOffset = -0x80000000; + } byteOffset >>= 0; if (typeof val === 'string') { diff --git a/test/parallel/test-buffer-indexof.js b/test/parallel/test-buffer-indexof.js index aac5b18f718cb3..83776be80a37ee 100644 --- a/test/parallel/test-buffer-indexof.js +++ b/test/parallel/test-buffer-indexof.js @@ -111,6 +111,11 @@ assert.equal( .indexOf(Buffer('d', 'binary'), 0, 'binary'), 3); +// test optional offset with passed encoding +assert.equal(new Buffer('aaaa0').indexOf('30', 'hex'), 4); +assert.equal(new Buffer('aaaa00a').indexOf('3030', 'hex'), 4); + + // test usc2 encoding var twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');