@@ -714,83 +714,91 @@ const encodingOps = {
714714byteLength : byteLengthUtf8 ,
715715write : utf8Write ,
716716slice : utf8Slice ,
717- indexOf : ( buf , val , byteOffset , dir ) =>
718- indexOfString ( buf , val , byteOffset , encodingsMap . utf8 , dir ) ,
717+ indexOf : ( buf , val , byteOffset , dir , end ) =>
718+ indexOfString ( buf , val , byteOffset , encodingsMap . utf8 , dir , end ) ,
719719} ,
720720ucs2 : {
721721encoding : 'ucs2' ,
722722encodingVal : encodingsMap . utf16le ,
723723byteLength : ( string ) => string . length * 2 ,
724724write : ucs2Write ,
725725slice : ucs2Slice ,
726- indexOf : ( buf , val , byteOffset , dir ) =>
727- indexOfString ( buf , val , byteOffset , encodingsMap . utf16le , dir ) ,
726+ indexOf : ( buf , val , byteOffset , dir , end ) =>
727+ indexOfString ( buf , val , byteOffset , encodingsMap . utf16le , dir , end ) ,
728728} ,
729729utf16le : {
730730encoding : 'utf16le' ,
731731encodingVal : encodingsMap . utf16le ,
732732byteLength : ( string ) => string . length * 2 ,
733733write : ucs2Write ,
734734slice : ucs2Slice ,
735- indexOf : ( buf , val , byteOffset , dir ) =>
736- indexOfString ( buf , val , byteOffset , encodingsMap . utf16le , dir ) ,
735+ indexOf : ( buf , val , byteOffset , dir , end ) =>
736+ indexOfString ( buf , val , byteOffset , encodingsMap . utf16le , dir , end ) ,
737737} ,
738738latin1 : {
739739encoding : 'latin1' ,
740740encodingVal : encodingsMap . latin1 ,
741741byteLength : ( string ) => string . length ,
742742write : latin1Write ,
743743slice : latin1Slice ,
744- indexOf : ( buf , val , byteOffset , dir ) =>
745- indexOfString ( buf , val , byteOffset , encodingsMap . latin1 , dir ) ,
744+ indexOf : ( buf , val , byteOffset , dir , end ) =>
745+ indexOfString ( buf , val , byteOffset , encodingsMap . latin1 , dir , end ) ,
746746} ,
747747ascii : {
748748encoding : 'ascii' ,
749749encodingVal : encodingsMap . ascii ,
750750byteLength : ( string ) => string . length ,
751751write : asciiWrite ,
752752slice : asciiSlice ,
753- indexOf : ( buf , val , byteOffset , dir ) =>
754- indexOfString ( buf , val , byteOffset , encodingsMap . ascii , dir ) ,
753+ indexOf : ( buf , val , byteOffset , dir , end ) =>
754+ indexOfBuffer ( buf ,
755+ fromStringFast ( val , encodingOps . ascii ) ,
756+ byteOffset ,
757+ encodingsMap . ascii ,
758+ dir ,
759+ end ) ,
755760} ,
756761base64 : {
757762encoding : 'base64' ,
758763encodingVal : encodingsMap . base64 ,
759764byteLength : ( string ) => base64ByteLength ( string , string . length ) ,
760765write : base64Write ,
761766slice : base64Slice ,
762- indexOf : ( buf , val , byteOffset , dir ) =>
767+ indexOf : ( buf , val , byteOffset , dir , end ) =>
763768indexOfBuffer ( buf ,
764769fromStringFast ( val , encodingOps . base64 ) ,
765770byteOffset ,
766771encodingsMap . base64 ,
767- dir ) ,
772+ dir ,
773+ end ) ,
768774} ,
769775base64url : {
770776encoding : 'base64url' ,
771777encodingVal : encodingsMap . base64url ,
772778byteLength : ( string ) => base64ByteLength ( string , string . length ) ,
773779write : base64urlWrite ,
774780slice : base64urlSlice ,
775- indexOf : ( buf , val , byteOffset , dir ) =>
781+ indexOf : ( buf , val , byteOffset , dir , end ) =>
776782indexOfBuffer ( buf ,
777783fromStringFast ( val , encodingOps . base64url ) ,
778784byteOffset ,
779785encodingsMap . base64url ,
780- dir ) ,
786+ dir ,
787+ end ) ,
781788} ,
782789hex : {
783790encoding : 'hex' ,
784791encodingVal : encodingsMap . hex ,
785792byteLength : ( string ) => string . length >>> 1 ,
786793write : hexWrite ,
787794slice : hexSlice ,
788- indexOf : ( buf , val , byteOffset , dir ) =>
795+ indexOf : ( buf , val , byteOffset , dir , end ) =>
789796indexOfBuffer ( buf ,
790797fromStringFast ( val , encodingOps . hex ) ,
791798byteOffset ,
792799encodingsMap . hex ,
793- dir ) ,
800+ dir ,
801+ end ) ,
794802} ,
795803} ;
796804function getEncodingOps ( encoding ) {
@@ -1044,9 +1052,10 @@ Buffer.prototype.compare = function compare(target,
10441052// - buffer - a Buffer to search
10451053// - val - a string, Buffer, or number
10461054// - byteOffset - an index into `buffer`; will be clamped to an int32
1055+ // - end - absolute exclusive end of the search range
10471056// - encoding - an optional encoding, relevant if val is a string
10481057// - dir - true for indexOf, false for lastIndexOf
1049- function bidirectionalIndexOf ( buffer , val , byteOffset , encoding , dir ) {
1058+ function bidirectionalIndexOf ( buffer , val , byteOffset , end , encoding , dir ) {
10501059validateBuffer ( buffer ) ;
10511060
10521061if ( typeof byteOffset === 'string' ) {
@@ -1066,7 +1075,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10661075dir = ! ! dir ; // Cast to bool.
10671076
10681077if ( typeof val === 'number' )
1069- return indexOfNumber ( buffer , val >>> 0 , byteOffset , dir ) ;
1078+ return indexOfNumber ( buffer , val >>> 0 , byteOffset , dir , end ) ;
10701079
10711080let ops ;
10721081if ( encoding === undefined )
@@ -1077,30 +1086,48 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10771086if ( typeof val === 'string' ) {
10781087if ( ops === undefined )
10791088throw new ERR_UNKNOWN_ENCODING ( encoding ) ;
1080- return ops . indexOf ( buffer , val , byteOffset , dir ) ;
1089+ return ops . indexOf ( buffer , val , byteOffset , dir , end ) ;
10811090}
10821091
10831092if ( isUint8Array ( val ) ) {
10841093const encodingVal =
10851094( ops === undefined ? encodingsMap . utf8 : ops . encodingVal ) ;
1086- return indexOfBuffer ( buffer , val , byteOffset , encodingVal , dir ) ;
1095+ return indexOfBuffer ( buffer , val , byteOffset , encodingVal , dir , end ) ;
10871096}
10881097
10891098throw new ERR_INVALID_ARG_TYPE (
10901099'value' , [ 'number' , 'string' , 'Buffer' , 'Uint8Array' ] , val ,
10911100) ;
10921101}
10931102
1094- Buffer . prototype . indexOf = function indexOf ( val , byteOffset , encoding ) {
1095- return bidirectionalIndexOf ( this , val , byteOffset , encoding , true ) ;
1103+ Buffer . prototype . indexOf = function indexOf ( val , offset , end , encoding ) {
1104+ if ( typeof end === 'string' ) {
1105+ encoding = end ;
1106+ end = this . length ;
1107+ } else if ( end === undefined ) {
1108+ end = this . length ;
1109+ }
1110+ return bidirectionalIndexOf ( this , val , offset , end , encoding , true ) ;
10961111} ;
10971112
1098- Buffer . prototype . lastIndexOf = function lastIndexOf ( val , byteOffset , encoding ) {
1099- return bidirectionalIndexOf ( this , val , byteOffset , encoding , false ) ;
1113+ Buffer . prototype . lastIndexOf = function lastIndexOf ( val , offset , end , encoding ) {
1114+ if ( typeof end === 'string' ) {
1115+ encoding = end ;
1116+ end = this . length ;
1117+ } else if ( end === undefined ) {
1118+ end = this . length ;
1119+ }
1120+ return bidirectionalIndexOf ( this , val , offset , end , encoding , false ) ;
11001121} ;
11011122
1102- Buffer . prototype . includes = function includes ( val , byteOffset , encoding ) {
1103- return bidirectionalIndexOf ( this , val , byteOffset , encoding , true ) !== - 1 ;
1123+ Buffer . prototype . includes = function includes ( val , offset , end , encoding ) {
1124+ if ( typeof end === 'string' ) {
1125+ encoding = end ;
1126+ end = this . length ;
1127+ } else if ( end === undefined ) {
1128+ end = this . length ;
1129+ }
1130+ return bidirectionalIndexOf ( this , val , offset , end , encoding , true ) !== - 1 ;
11041131} ;
11051132
11061133// Usage:
0 commit comments