Browse Source

buffer: refactor slowToString

Since slowToString only has one callsite, refactor to eliminate
the use of call.

PR-URL: https://github.com/nodejs/node/pull/11358
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
v6
James M Snell 8 years ago
parent
commit
4b8b7e9d2b
  1. 26
      lib/buffer.js

26
lib/buffer.js

@ -433,10 +433,10 @@ Object.defineProperty(Buffer.prototype, 'offset', {
}); });
function slowToString(encoding, start, end) { function slowToString(buf, encoding, start, end) {
var loweredCase = false; var loweredCase = false;
// No need to verify that "this.length <= MAX_UINT32" since it's a read-only // No need to verify that "buf.length <= MAX_UINT32" since it's a read-only
// property of a typed array. // property of a typed array.
// This behaves neither like String nor Uint8Array in that we set start/end // This behaves neither like String nor Uint8Array in that we set start/end
@ -445,13 +445,13 @@ function slowToString(encoding, start, end) {
// Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization. // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
if (start === undefined || start < 0) if (start === undefined || start < 0)
start = 0; start = 0;
// Return early if start > this.length. Done here to prevent potential uint32 // Return early if start > buf.length. Done here to prevent potential uint32
// coercion fail below. // coercion fail below.
if (start > this.length) if (start > buf.length)
return ''; return '';
if (end === undefined || end > this.length) if (end === undefined || end > buf.length)
end = this.length; end = buf.length;
if (end <= 0) if (end <= 0)
return ''; return '';
@ -468,27 +468,27 @@ function slowToString(encoding, start, end) {
while (true) { while (true) {
switch (encoding) { switch (encoding) {
case 'hex': case 'hex':
return this.hexSlice(start, end); return buf.hexSlice(start, end);
case 'utf8': case 'utf8':
case 'utf-8': case 'utf-8':
return this.utf8Slice(start, end); return buf.utf8Slice(start, end);
case 'ascii': case 'ascii':
return this.asciiSlice(start, end); return buf.asciiSlice(start, end);
case 'latin1': case 'latin1':
case 'binary': case 'binary':
return this.latin1Slice(start, end); return buf.latin1Slice(start, end);
case 'base64': case 'base64':
return this.base64Slice(start, end); return buf.base64Slice(start, end);
case 'ucs2': case 'ucs2':
case 'ucs-2': case 'ucs-2':
case 'utf16le': case 'utf16le':
case 'utf-16le': case 'utf-16le':
return this.ucs2Slice(start, end); return buf.ucs2Slice(start, end);
default: default:
if (loweredCase) if (loweredCase)
@ -508,7 +508,7 @@ Buffer.prototype.toString = function(encoding, start, end) {
if (arguments.length === 0) { if (arguments.length === 0) {
result = this.utf8Slice(0, this.length); result = this.utf8Slice(0, this.length);
} else { } else {
result = slowToString.call(this, encoding, start, end); result = slowToString(this, encoding, start, end);
} }
if (result === undefined) if (result === undefined)
throw new Error('"toString()" failed'); throw new Error('"toString()" failed');

Loading…
Cancel
Save