@@ -728,9 +728,7 @@ Buffer.prototype.fill = function fill(val, start, end, encoding) {
728728Buffer.prototype.write = function(string, offset, length, encoding) {
729729 // Buffer#write(string);
730730 if (offset === undefined) {
731- encoding = 'utf8';
732- length = this.length;
733- offset = 0;
731+ return this.utf8Write(string, 0, this.length);
734732
735733 // Buffer#write(string, encoding)
736734 } else if (length === undefined && typeof offset === 'string') {
@@ -743,12 +741,17 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
743741 offset = offset >>> 0;
744742 if (isFinite(length)) {
745743 length = length >>> 0;
746- if (encoding === undefined)
747- encoding = 'utf8';
748744 } else {
749745 encoding = length;
750746 length = undefined;
751747 }
748+
749+ var remaining = this.length - offset;
750+ if (length === undefined || length > remaining)
751+ length = remaining;
752+
753+ if (string.length > 0 && (length < 0 || offset < 0))
754+ throw new RangeError('Attempt to write outside buffer bounds');
752755 } else {
753756 // if someone is still calling the obsolete form of write(), tell them.
754757 // we don't want eg buf.write("foo", "utf8", 10) to silently turn into
@@ -757,50 +760,51 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
757760 'is no longer supported');
758761 }
759762
760- var remaining = this.length - offset;
761- if (length === undefined || length > remaining)
762- length = remaining;
763-
764- if (string.length > 0 && (length < 0 || offset < 0))
765- throw new RangeError('Attempt to write outside buffer bounds');
766-
767- if (!encoding)
768- encoding = 'utf8';
769-
770- var loweredCase = false;
771- for (;;) {
772- switch (encoding) {
773- case 'hex':
774- return this.hexWrite(string, offset, length);
775-
776- case 'utf8':
777- case 'utf-8':
778- return this.utf8Write(string, offset, length);
779-
780- case 'ascii':
781- return this.asciiWrite(string, offset, length);
782-
783- case 'latin1':
784- case 'binary':
763+ if (!encoding) return this.utf8Write(string, offset, length);
764+
765+ encoding += '';
766+ switch (encoding.length) {
767+ case 4:
768+ if (encoding === 'utf8') return this.utf8Write(string, offset, length);
769+ if (encoding === 'ucs2') return this.ucs2Write(string, offset, length);
770+ encoding = encoding.toLowerCase();
771+ if (encoding === 'utf8') return this.utf8Write(string, offset, length);
772+ if (encoding === 'ucs2') return this.ucs2Write(string, offset, length);
773+ break;
774+ case 5:
775+ if (encoding === 'utf-8') return this.utf8Write(string, offset, length);
776+ if (encoding === 'ascii') return this.asciiWrite(string, offset, length);
777+ if (encoding === 'ucs-2') return this.ucs2Write(string, offset, length);
778+ encoding = encoding.toLowerCase();
779+ if (encoding === 'utf-8') return this.utf8Write(string, offset, length);
780+ if (encoding === 'ascii') return this.asciiWrite(string, offset, length);
781+ if (encoding === 'ucs-2') return this.ucs2Write(string, offset, length);
782+ break;
783+ case 7:
784+ if (encoding === 'utf16le' || encoding.toLowerCase() === 'utf16le')
785+ return this.ucs2Write(string, offset, length);
786+ break;
787+ case 8:
788+ if (encoding === 'utf-16le' || encoding.toLowerCase() === 'utf-16le')
789+ return this.ucs2Write(string, offset, length);
790+ break;
791+ case 6:
792+ if (encoding === 'latin1' || encoding === 'binary')
785793 return this.latin1Write(string, offset, length);
786-
787- case 'base64':
788- // Warning: maxLength not taken into account in base64Write
794+ if (encoding === 'base64')
789795 return this.base64Write(string, offset, length);
790-
791- case 'ucs2':
792- case 'ucs-2':
793- case 'utf16le':
794- case 'utf-16le':
795- return this.ucs2Write(string, offset, length);
796-
797- default:
798- if (loweredCase)
799- throw new TypeError('Unknown encoding: ' + encoding);
800- encoding = ('' + encoding).toLowerCase();
801- loweredCase = true;
802- }
796+ encoding = encoding.toLowerCase();
797+ if (encoding === 'latin1' || encoding === 'binary')
798+ return this.latin1Write(string, offset, length);
799+ if (encoding === 'base64')
800+ return this.base64Write(string, offset, length);
801+ break;
802+ case 3:
803+ if (encoding === 'hex' || encoding.toLowerCase() === 'hex')
804+ return this.hexWrite(string, offset, length);
805+ break;
803806 }
807+ throw new TypeError('Unknown encoding: ' + encoding);
804808};
805809
806810
0 commit comments