Skip to content

Commit 4b8b7e9

Browse files
committed
buffer: refactor slowToString
Since slowToString only has one callsite, refactor to eliminate the use of call. PR-URL: #11358 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 1e21d05 commit 4b8b7e9

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

lib/buffer.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,10 @@ Object.defineProperty(Buffer.prototype, 'offset', {
433433
});
434434

435435

436-
function slowToString(encoding, start, end) {
436+
function slowToString(buf, encoding, start, end) {
437437
var loweredCase = false;
438438

439-
// No need to verify that "this.length <= MAX_UINT32" since it's a read-only
439+
// No need to verify that "buf.length <= MAX_UINT32" since it's a read-only
440440
// property of a typed array.
441441

442442
// This behaves neither like String nor Uint8Array in that we set start/end
@@ -445,13 +445,13 @@ function slowToString(encoding, start, end) {
445445
// Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
446446
if (start === undefined || start < 0)
447447
start = 0;
448-
// Return early if start > this.length. Done here to prevent potential uint32
448+
// Return early if start > buf.length. Done here to prevent potential uint32
449449
// coercion fail below.
450-
if (start > this.length)
450+
if (start > buf.length)
451451
return '';
452452

453-
if (end === undefined || end > this.length)
454-
end = this.length;
453+
if (end === undefined || end > buf.length)
454+
end = buf.length;
455455

456456
if (end <= 0)
457457
return '';
@@ -468,27 +468,27 @@ function slowToString(encoding, start, end) {
468468
while (true) {
469469
switch (encoding) {
470470
case 'hex':
471-
return this.hexSlice(start, end);
471+
return buf.hexSlice(start, end);
472472

473473
case 'utf8':
474474
case 'utf-8':
475-
return this.utf8Slice(start, end);
475+
return buf.utf8Slice(start, end);
476476

477477
case 'ascii':
478-
return this.asciiSlice(start, end);
478+
return buf.asciiSlice(start, end);
479479

480480
case 'latin1':
481481
case 'binary':
482-
return this.latin1Slice(start, end);
482+
return buf.latin1Slice(start, end);
483483

484484
case 'base64':
485-
return this.base64Slice(start, end);
485+
return buf.base64Slice(start, end);
486486

487487
case 'ucs2':
488488
case 'ucs-2':
489489
case 'utf16le':
490490
case 'utf-16le':
491-
return this.ucs2Slice(start, end);
491+
return buf.ucs2Slice(start, end);
492492

493493
default:
494494
if (loweredCase)
@@ -508,7 +508,7 @@ Buffer.prototype.toString = function(encoding, start, end) {
508508
if (arguments.length === 0) {
509509
result = this.utf8Slice(0, this.length);
510510
} else {
511-
result = slowToString.call(this, encoding, start, end);
511+
result = slowToString(this, encoding, start, end);
512512
}
513513
if (result === undefined)
514514
throw new Error('"toString()" failed');

0 commit comments

Comments
 (0)