Skip to content

Commit 65d8179

Browse files
committed
buffer: simplify code
This refactors some code for simplicity. It also removes a call indirection used in the buffers custom inspect function. PR-URL: nodejs#25151 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Masashi Hirano <shisama07@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent e7e26b2 commit 65d8179

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

lib/buffer.js

+16-19
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,13 @@ Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) {
296296
// If --zero-fill-buffers command line argument is set, a zero-filled
297297
// buffer is returned.
298298
function SlowBuffer(length) {
299+
const len = +length;
299300
// eslint-disable-next-line eqeqeq
300-
if (+length != length)
301+
if (len != length)
301302
length = 0;
302-
assertSize(+length);
303-
return createUnsafeBuffer(+length);
303+
else
304+
assertSize(len);
305+
return createUnsafeBuffer(len);
304306
}
305307

306308
Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
@@ -317,9 +319,8 @@ function allocate(size) {
317319
poolOffset += size;
318320
alignPool();
319321
return b;
320-
} else {
321-
return createUnsafeBuffer(size);
322322
}
323+
return createUnsafeBuffer(size);
323324
}
324325

325326
function fromString(string, encoding) {
@@ -637,21 +638,18 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
637638
}
638639

639640
const len = this.length;
640-
if (len === 0)
641-
return '';
642641

643-
if (!start || start < 0)
642+
if (start <= 0)
644643
start = 0;
645644
else if (start >= len)
646645
return '';
646+
else
647+
start |= 0;
647648

648649
if (end === undefined || end > len)
649650
end = len;
650-
else if (end <= 0)
651-
return '';
652-
653-
start |= 0;
654-
end |= 0;
651+
else
652+
end |= 0;
655653

656654
if (end <= start)
657655
return '';
@@ -670,10 +668,10 @@ Buffer.prototype.equals = function equals(otherBuffer) {
670668
};
671669

672670
// Override how buffers are presented by util.inspect().
673-
Buffer.prototype[customInspectSymbol] = function inspect() {
674-
var str = '';
675-
var max = exports.INSPECT_MAX_BYTES;
676-
str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim();
671+
Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
672+
const max = exports.INSPECT_MAX_BYTES;
673+
const actualMax = Math.min(max, this.length);
674+
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
677675
const remaining = this.length - max;
678676
if (remaining > 0)
679677
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
@@ -975,9 +973,8 @@ Buffer.prototype.toJSON = function toJSON() {
975973
for (var i = 0; i < this.length; ++i)
976974
data[i] = this[i];
977975
return { type: 'Buffer', data };
978-
} else {
979-
return { type: 'Buffer', data: [] };
980976
}
977+
return { type: 'Buffer', data: [] };
981978
};
982979

983980
function adjustOffset(offset, length) {

0 commit comments

Comments
 (0)