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‎

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

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

486490
if(typeofval==='string'){

‎test/parallel/test-buffer-indexof.js‎

Lines changed: 5 additions & 0 deletions
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(newBuffer('aaaa0').indexOf('30','hex'),4);
116+
assert.equal(newBuffer('aaaa00a').indexOf('3030','hex'),4);
117+
118+
114119
// test usc2 encoding
115120
vartwoByteString=newBuffer('\u039a\u0391\u03a3\u03a3\u0395','ucs2');
116121

0 commit comments

Comments
(0)