Skip to content

Commit 7240ad4

Browse files
committed
buffer: allow encoding param to collapse
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 <[email protected]>
1 parent 6712a1f commit 7240ad4

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/buffer.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,14 @@ function slowIndexOf(buffer, val, byteOffset, encoding) {
477477
}
478478

479479
Buffer.prototype.indexOf = function indexOf(val, byteOffset, encoding) {
480-
if (byteOffset > 0x7fffffff)
480+
if (typeof byteOffset === 'string') {
481+
encoding = byteOffset;
482+
byteOffset = 0;
483+
} else if (byteOffset > 0x7fffffff) {
481484
byteOffset = 0x7fffffff;
482-
else if (byteOffset < -0x80000000)
485+
} else if (byteOffset < -0x80000000) {
483486
byteOffset = -0x80000000;
487+
}
484488
byteOffset >>= 0;
485489

486490
if (typeof val === 'string') {

test/parallel/test-buffer-indexof.js

+5
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ assert.equal(
111111
.indexOf(Buffer('d', 'binary'), 0, 'binary'), 3);
112112

113113

114+
// test optional offset with passed encoding
115+
assert.equal(new Buffer('aaaa0').indexOf('30', 'hex'), 4);
116+
assert.equal(new Buffer('aaaa00a').indexOf('3030', 'hex'), 4);
117+
118+
114119
// test usc2 encoding
115120
var twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
116121

0 commit comments

Comments
 (0)