@@ -786,9 +786,7 @@ Buffer.prototype.fill = function fill(val, start, end, encoding) {
786786Buffer . prototype . write = function ( string , offset , length , encoding ) {
787787 // Buffer#write(string);
788788 if ( offset === undefined ) {
789- encoding = 'utf8' ;
790- length = this . length ;
791- offset = 0 ;
789+ return this . utf8Write ( string , 0 , this . length ) ;
792790
793791 // Buffer#write(string, encoding)
794792 } else if ( length === undefined && typeof offset === 'string' ) {
@@ -801,12 +799,17 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
801799 offset = offset >>> 0 ;
802800 if ( isFinite ( length ) ) {
803801 length = length >>> 0 ;
804- if ( encoding === undefined )
805- encoding = 'utf8' ;
806802 } else {
807803 encoding = length ;
808804 length = undefined ;
809805 }
806+
807+ var remaining = this . length - offset ;
808+ if ( length === undefined || length > remaining )
809+ length = remaining ;
810+
811+ if ( string . length > 0 && ( length < 0 || offset < 0 ) )
812+ throw new RangeError ( 'Attempt to write outside buffer bounds' ) ;
810813 } else {
811814 // if someone is still calling the obsolete form of write(), tell them.
812815 // we don't want eg buf.write("foo", "utf8", 10) to silently turn into
@@ -815,50 +818,51 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
815818 'is no longer supported' ) ;
816819 }
817820
818- var remaining = this . length - offset ;
819- if ( length === undefined || length > remaining )
820- length = remaining ;
821-
822- if ( string . length > 0 && ( length < 0 || offset < 0 ) )
823- throw new RangeError ( 'Attempt to write outside buffer bounds' ) ;
824-
825- if ( ! encoding )
826- encoding = 'utf8' ;
827-
828- var loweredCase = false ;
829- for ( ; ; ) {
830- switch ( encoding ) {
831- case 'hex' :
832- return this . hexWrite ( string , offset , length ) ;
833-
834- case 'utf8' :
835- case 'utf-8' :
836- return this . utf8Write ( string , offset , length ) ;
837-
838- case 'ascii' :
839- return this . asciiWrite ( string , offset , length ) ;
821+ if ( ! encoding ) return this . utf8Write ( string , offset , length ) ;
840822
841- case 'latin1' :
842- case 'binary' :
823+ encoding += '' ;
824+ switch ( encoding . length ) {
825+ case 4 :
826+ if ( encoding === 'utf8' ) return this . utf8Write ( string , offset , length ) ;
827+ if ( encoding === 'ucs2' ) return this . ucs2Write ( string , offset , length ) ;
828+ encoding = encoding . toLowerCase ( ) ;
829+ if ( encoding === 'utf8' ) return this . utf8Write ( string , offset , length ) ;
830+ if ( encoding === 'ucs2' ) return this . ucs2Write ( string , offset , length ) ;
831+ break ;
832+ case 5 :
833+ if ( encoding === 'utf-8' ) return this . utf8Write ( string , offset , length ) ;
834+ if ( encoding === 'ascii' ) return this . asciiWrite ( string , offset , length ) ;
835+ if ( encoding === 'ucs-2' ) return this . ucs2Write ( string , offset , length ) ;
836+ encoding = encoding . toLowerCase ( ) ;
837+ if ( encoding === 'utf-8' ) return this . utf8Write ( string , offset , length ) ;
838+ if ( encoding === 'ascii' ) return this . asciiWrite ( string , offset , length ) ;
839+ if ( encoding === 'ucs-2' ) return this . ucs2Write ( string , offset , length ) ;
840+ break ;
841+ case 7 :
842+ if ( encoding === 'utf16le' || encoding . toLowerCase ( ) === 'utf16le' )
843+ return this . ucs2Write ( string , offset , length ) ;
844+ break ;
845+ case 8 :
846+ if ( encoding === 'utf-16le' || encoding . toLowerCase ( ) === 'utf-16le' )
847+ return this . ucs2Write ( string , offset , length ) ;
848+ break ;
849+ case 6 :
850+ if ( encoding === 'latin1' || encoding === 'binary' )
843851 return this . latin1Write ( string , offset , length ) ;
844-
845- case 'base64' :
846- // Warning: maxLength not taken into account in base64Write
852+ if ( encoding === 'base64' )
847853 return this . base64Write ( string , offset , length ) ;
848-
849- case 'ucs2' :
850- case 'ucs-2' :
851- case 'utf16le' :
852- case 'utf-16le' :
853- return this . ucs2Write ( string , offset , length ) ;
854-
855- default :
856- if ( loweredCase )
857- throw new TypeError ( 'Unknown encoding: ' + encoding ) ;
858- encoding = ( '' + encoding ) . toLowerCase ( ) ;
859- loweredCase = true ;
860- }
854+ encoding = encoding . toLowerCase ( ) ;
855+ if ( encoding === 'latin1' || encoding === 'binary' )
856+ return this . latin1Write ( string , offset , length ) ;
857+ if ( encoding === 'base64' )
858+ return this . base64Write ( string , offset , length ) ;
859+ break ;
860+ case 3 :
861+ if ( encoding === 'hex' || encoding . toLowerCase ( ) === 'hex' )
862+ return this . hexWrite ( string , offset , length ) ;
863+ break ;
861864 }
865+ throw new TypeError ( 'Unknown encoding: ' + encoding ) ;
862866} ;
863867
864868
0 commit comments