Skip to content

Commit

Permalink
buffer: allow encoding param to collapse
Browse files Browse the repository at this point in the history
Currently the signature is indexOf(val[, byteOffset[, encoding]])
Instead allow indexOf(val[, byteOffset][, encoding])
so that byteOffset does not need to be passed.

PR-URL: #4803
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
trevnorris authored and rvagg committed Feb 9, 2016
1 parent 18d24e6 commit 4c67d74
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,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') {
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-buffer-indexof.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ assert.equal(
Buffer(b.toString('binary'), 'binary')
.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
const twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
Expand Down

0 comments on commit 4c67d74

Please sign in to comment.