@@ -731,51 +731,55 @@ const encodingOps = {
731731 byteLength : ( string ) => string . length ,
732732 write : asciiWrite ,
733733 slice : asciiSlice ,
734- indexOf : ( buf , val , byteOffset , dir ) =>
734+ indexOf : ( buf , val , byteOffset , dir , byteLength = buf . byteLength - byteOffset ) =>
735735 indexOfBuffer ( buf ,
736736 fromStringFast ( val , encodingOps . ascii ) ,
737737 byteOffset ,
738738 encodingsMap . ascii ,
739- dir ) ,
739+ dir ,
740+ byteLength ) ,
740741 } ,
741742 base64 : {
742743 encoding : 'base64' ,
743744 encodingVal : encodingsMap . base64 ,
744745 byteLength : ( string ) => base64ByteLength ( string , string . length ) ,
745746 write : base64Write ,
746747 slice : base64Slice ,
747- indexOf : ( buf , val , byteOffset , dir ) =>
748+ indexOf : ( buf , val , byteOffset , dir , byteLength = buf . byteLength - byteOffset ) =>
748749 indexOfBuffer ( buf ,
749750 fromStringFast ( val , encodingOps . base64 ) ,
750751 byteOffset ,
751752 encodingsMap . base64 ,
752- dir ) ,
753+ dir ,
754+ byteLength ) ,
753755 } ,
754756 base64url : {
755757 encoding : 'base64url' ,
756758 encodingVal : encodingsMap . base64url ,
757759 byteLength : ( string ) => base64ByteLength ( string , string . length ) ,
758760 write : base64urlWrite ,
759761 slice : base64urlSlice ,
760- indexOf : ( buf , val , byteOffset , dir ) =>
762+ indexOf : ( buf , val , byteOffset , dir , byteLength = buf . byteLength - byteOffset ) =>
761763 indexOfBuffer ( buf ,
762764 fromStringFast ( val , encodingOps . base64url ) ,
763765 byteOffset ,
764766 encodingsMap . base64url ,
765- dir ) ,
767+ dir ,
768+ byteLength ) ,
766769 } ,
767770 hex : {
768771 encoding : 'hex' ,
769772 encodingVal : encodingsMap . hex ,
770773 byteLength : ( string ) => string . length >>> 1 ,
771774 write : hexWrite ,
772775 slice : hexSlice ,
773- indexOf : ( buf , val , byteOffset , dir ) =>
776+ indexOf : ( buf , val , byteOffset , dir , byteLength = buf . byteLength - byteOffset ) =>
774777 indexOfBuffer ( buf ,
775778 fromStringFast ( val , encodingOps . hex ) ,
776779 byteOffset ,
777780 encodingsMap . hex ,
778- dir ) ,
781+ dir ,
782+ byteLength ) ,
779783 } ,
780784} ;
781785function getEncodingOps ( encoding ) {
@@ -1031,7 +1035,7 @@ Buffer.prototype.compare = function compare(target,
10311035// - byteOffset - an index into `buffer`; will be clamped to an int32
10321036// - encoding - an optional encoding, relevant if val is a string
10331037// - dir - true for indexOf, false for lastIndexOf
1034- function bidirectionalIndexOf ( buffer , val , byteOffset , encoding , dir ) {
1038+ function bidirectionalIndexOf ( buffer , val , byteOffset , byteLength , encoding , dir ) {
10351039 validateBuffer ( buffer ) ;
10361040
10371041 if ( typeof byteOffset === 'string' ) {
@@ -1051,7 +1055,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10511055 dir = ! ! dir ; // Cast to bool.
10521056
10531057 if ( typeof val === 'number' )
1054- return indexOfNumber ( buffer , val >>> 0 , byteOffset , dir ) ;
1058+ return indexOfNumber ( buffer , val >>> 0 , byteOffset , dir , byteLength ) ;
10551059
10561060 let ops ;
10571061 if ( encoding === undefined )
@@ -1062,30 +1066,42 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10621066 if ( typeof val === 'string' ) {
10631067 if ( ops === undefined )
10641068 throw new ERR_UNKNOWN_ENCODING ( encoding ) ;
1065- return ops . indexOf ( buffer , val , byteOffset , dir ) ;
1069+ return ops . indexOf ( buffer , val , byteOffset , dir , byteLength ) ;
10661070 }
10671071
10681072 if ( isUint8Array ( val ) ) {
10691073 const encodingVal =
10701074 ( ops === undefined ? encodingsMap . utf8 : ops . encodingVal ) ;
1071- return indexOfBuffer ( buffer , val , byteOffset , encodingVal , dir ) ;
1075+ return indexOfBuffer ( buffer , val , byteOffset , encodingVal , dir , byteLength ) ;
10721076 }
10731077
10741078 throw new ERR_INVALID_ARG_TYPE (
10751079 'value' , [ 'number' , 'string' , 'Buffer' , 'Uint8Array' ] , val ,
10761080 ) ;
10771081}
10781082
1079- Buffer . prototype . indexOf = function indexOf ( val , byteOffset , encoding ) {
1080- return bidirectionalIndexOf ( this , val , byteOffset , encoding , true ) ;
1083+ Buffer . prototype . indexOf = function indexOf ( val , offset , end , encoding ) {
1084+ if ( typeof end === 'string' ) {
1085+ encoding = end ;
1086+ end = val . byteLength ;
1087+ }
1088+ return bidirectionalIndexOf ( this , val , offset , end - offset , encoding , true ) ;
10811089} ;
10821090
1083- Buffer . prototype . lastIndexOf = function lastIndexOf ( val , byteOffset , encoding ) {
1084- return bidirectionalIndexOf ( this , val , byteOffset , encoding , false ) ;
1091+ Buffer . prototype . lastIndexOf = function lastIndexOf ( val , offset , byteLength , encoding ) {
1092+ if ( typeof end === 'string' ) {
1093+ encoding = end ;
1094+ end = val . byteLength ;
1095+ }
1096+ return bidirectionalIndexOf ( this , val , offset , end - offset , encoding , false ) ;
10851097} ;
10861098
1087- Buffer . prototype . includes = function includes ( val , byteOffset , encoding ) {
1088- return bidirectionalIndexOf ( this , val , byteOffset , encoding , true ) !== - 1 ;
1099+ Buffer . prototype . includes = function includes ( val , offset , end , encoding ) {
1100+ if ( typeof end === 'string' ) {
1101+ encoding = end ;
1102+ end = val . byteLength ;
1103+ }
1104+ return bidirectionalIndexOf ( this , val , offset , end - offset , encoding , true ) !== - 1 ;
10891105} ;
10901106
10911107// Usage:
0 commit comments