@@ -73,6 +73,17 @@ const {
7373 kStringMaxLength,
7474 atob : _atob ,
7575 btoa : _btoa ,
76+ asciiSlice,
77+ base64Slice,
78+ base64urlSlice,
79+ latin1Slice,
80+ hexSlice,
81+ ucs2Slice,
82+ utf8Slice,
83+ base64Write,
84+ base64urlWrite,
85+ hexWrite,
86+ ucs2Write,
7687} = internalBinding ( 'buffer' ) ;
7788const {
7889 constants : {
@@ -129,6 +140,9 @@ const {
129140 markAsUntransferable,
130141 addBufferPrototypeMethods,
131142 createUnsafeBuffer,
143+ asciiWrite,
144+ latin1Write,
145+ utf8Write,
132146} = require ( 'internal/buffer' ) ;
133147
134148FastBuffer . prototype . constructor = Buffer ;
@@ -646,44 +660,44 @@ const encodingOps = {
646660 encoding : 'utf8' ,
647661 encodingVal : encodingsMap . utf8 ,
648662 byteLength : byteLengthUtf8 ,
649- write : ( buf , string , offset , len ) => buf . utf8Write ( string , offset , len ) ,
650- slice : ( buf , start , end ) => buf . utf8Slice ( start , end ) ,
663+ write : utf8Write ,
664+ slice : utf8Slice ,
651665 indexOf : ( buf , val , byteOffset , dir ) =>
652666 indexOfString ( buf , val , byteOffset , encodingsMap . utf8 , dir ) ,
653667 } ,
654668 ucs2 : {
655669 encoding : 'ucs2' ,
656670 encodingVal : encodingsMap . utf16le ,
657671 byteLength : ( string ) => string . length * 2 ,
658- write : ( buf , string , offset , len ) => buf . ucs2Write ( string , offset , len ) ,
659- slice : ( buf , start , end ) => buf . ucs2Slice ( start , end ) ,
672+ write : ucs2Write ,
673+ slice : ucs2Slice ,
660674 indexOf : ( buf , val , byteOffset , dir ) =>
661675 indexOfString ( buf , val , byteOffset , encodingsMap . utf16le , dir ) ,
662676 } ,
663677 utf16le : {
664678 encoding : 'utf16le' ,
665679 encodingVal : encodingsMap . utf16le ,
666680 byteLength : ( string ) => string . length * 2 ,
667- write : ( buf , string , offset , len ) => buf . ucs2Write ( string , offset , len ) ,
668- slice : ( buf , start , end ) => buf . ucs2Slice ( start , end ) ,
681+ write : ucs2Write ,
682+ slice : ucs2Slice ,
669683 indexOf : ( buf , val , byteOffset , dir ) =>
670684 indexOfString ( buf , val , byteOffset , encodingsMap . utf16le , dir ) ,
671685 } ,
672686 latin1 : {
673687 encoding : 'latin1' ,
674688 encodingVal : encodingsMap . latin1 ,
675689 byteLength : ( string ) => string . length ,
676- write : ( buf , string , offset , len ) => buf . latin1Write ( string , offset , len ) ,
677- slice : ( buf , start , end ) => buf . latin1Slice ( start , end ) ,
690+ write : latin1Write ,
691+ slice : latin1Slice ,
678692 indexOf : ( buf , val , byteOffset , dir ) =>
679693 indexOfString ( buf , val , byteOffset , encodingsMap . latin1 , dir ) ,
680694 } ,
681695 ascii : {
682696 encoding : 'ascii' ,
683697 encodingVal : encodingsMap . ascii ,
684698 byteLength : ( string ) => string . length ,
685- write : ( buf , string , offset , len ) => buf . asciiWrite ( string , offset , len ) ,
686- slice : ( buf , start , end ) => buf . asciiSlice ( start , end ) ,
699+ write : asciiWrite ,
700+ slice : asciiSlice ,
687701 indexOf : ( buf , val , byteOffset , dir ) =>
688702 indexOfBuffer ( buf ,
689703 fromStringFast ( val , encodingOps . ascii ) ,
@@ -695,8 +709,8 @@ const encodingOps = {
695709 encoding : 'base64' ,
696710 encodingVal : encodingsMap . base64 ,
697711 byteLength : ( string ) => base64ByteLength ( string , string . length ) ,
698- write : ( buf , string , offset , len ) => buf . base64Write ( string , offset , len ) ,
699- slice : ( buf , start , end ) => buf . base64Slice ( start , end ) ,
712+ write : base64Write ,
713+ slice : base64Slice ,
700714 indexOf : ( buf , val , byteOffset , dir ) =>
701715 indexOfBuffer ( buf ,
702716 fromStringFast ( val , encodingOps . base64 ) ,
@@ -708,9 +722,8 @@ const encodingOps = {
708722 encoding : 'base64url' ,
709723 encodingVal : encodingsMap . base64url ,
710724 byteLength : ( string ) => base64ByteLength ( string , string . length ) ,
711- write : ( buf , string , offset , len ) =>
712- buf . base64urlWrite ( string , offset , len ) ,
713- slice : ( buf , start , end ) => buf . base64urlSlice ( start , end ) ,
725+ write : base64urlWrite ,
726+ slice : base64urlSlice ,
714727 indexOf : ( buf , val , byteOffset , dir ) =>
715728 indexOfBuffer ( buf ,
716729 fromStringFast ( val , encodingOps . base64url ) ,
@@ -722,8 +735,8 @@ const encodingOps = {
722735 encoding : 'hex' ,
723736 encodingVal : encodingsMap . hex ,
724737 byteLength : ( string ) => string . length >>> 1 ,
725- write : ( buf , string , offset , len ) => buf . hexWrite ( string , offset , len ) ,
726- slice : ( buf , start , end ) => buf . hexSlice ( start , end ) ,
738+ write : hexWrite ,
739+ slice : hexSlice ,
727740 indexOf : ( buf , val , byteOffset , dir ) =>
728741 indexOfBuffer ( buf ,
729742 fromStringFast ( val , encodingOps . hex ) ,
@@ -848,7 +861,7 @@ Buffer.prototype.copy =
848861// to their upper/lower bounds if the value passed is out of range.
849862Buffer . prototype . toString = function toString ( encoding , start , end ) {
850863 if ( arguments . length === 0 ) {
851- return this . utf8Slice ( 0 , this . length ) ;
864+ return utf8Slice ( this , 0 , this . length ) ;
852865 }
853866
854867 const len = this . length ;
@@ -869,7 +882,7 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
869882 return '' ;
870883
871884 if ( encoding === undefined )
872- return this . utf8Slice ( start , end ) ;
885+ return utf8Slice ( this , start , end ) ;
873886
874887 const ops = getEncodingOps ( encoding ) ;
875888 if ( ops === undefined )
@@ -900,7 +913,7 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
900913 const actualMax = MathMin ( max , this . length ) ;
901914 const remaining = this . length - max ;
902915 let str = StringPrototypeTrim ( RegExpPrototypeSymbolReplace (
903- / ( .{ 2 } ) / g, this . hexSlice ( 0 , actualMax ) , '$1 ' ) ) ;
916+ / ( .{ 2 } ) / g, hexSlice ( this , 0 , actualMax ) , '$1 ' ) ) ;
904917 if ( remaining > 0 )
905918 str += ` ... ${ remaining } more byte${ remaining > 1 ? 's' : '' } ` ;
906919 // Inspect special properties as well, if possible.
@@ -1039,7 +1052,7 @@ Buffer.prototype.lastIndexOf = function lastIndexOf(val, byteOffset, encoding) {
10391052} ;
10401053
10411054Buffer . prototype . includes = function includes ( val , byteOffset , encoding ) {
1042- return this . indexOf ( val , byteOffset , encoding ) !== - 1 ;
1055+ return bidirectionalIndexOf ( this , val , byteOffset , encoding , true ) !== - 1 ;
10431056} ;
10441057
10451058// Usage:
@@ -1124,7 +1137,7 @@ function _fill(buf, value, offset, end, encoding) {
11241137Buffer . prototype . write = function write ( string , offset , length , encoding ) {
11251138 // Buffer#write(string);
11261139 if ( offset === undefined ) {
1127- return this . utf8Write ( string , 0 , this . length ) ;
1140+ return utf8Write ( this , string , 0 , this . length ) ;
11281141 }
11291142 // Buffer#write(string, encoding)
11301143 if ( length === undefined && typeof offset === 'string' ) {
@@ -1151,9 +1164,9 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {
11511164 }
11521165
11531166 if ( ! encoding || encoding === 'utf8' )
1154- return this . utf8Write ( string , offset , length ) ;
1167+ return utf8Write ( this , string , offset , length ) ;
11551168 if ( encoding === 'ascii' )
1156- return this . asciiWrite ( string , offset , length ) ;
1169+ return asciiWrite ( this , string , offset , length ) ;
11571170
11581171 const ops = getEncodingOps ( encoding ) ;
11591172 if ( ops === undefined )
0 commit comments