Skip to content

Commit c6bfa66

Browse files
BridgeARMylesBorins
authored andcommitted
buffer: simplify code
This refactors some code for simplicity. It also removes a call indirection used in the buffers custom inspect function. PR-URL: #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 db54531 commit c6bfa66

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
@@ -298,11 +298,13 @@ Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) {
298298
// If --zero-fill-buffers command line argument is set, a zero-filled
299299
// buffer is returned.
300300
function SlowBuffer(length) {
301+
const len = +length;
301302
// eslint-disable-next-line eqeqeq
302-
if (+length != length)
303+
if (len != length)
303304
length = 0;
304-
assertSize(+length);
305-
return createUnsafeBuffer(+length);
305+
else
306+
assertSize(len);
307+
return createUnsafeBuffer(len);
306308
}
307309

308310
Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
@@ -319,9 +321,8 @@ function allocate(size) {
319321
poolOffset += size;
320322
alignPool();
321323
return b;
322-
} else {
323-
return createUnsafeBuffer(size);
324324
}
325+
return createUnsafeBuffer(size);
325326
}
326327

327328
function fromString(string, encoding) {
@@ -639,21 +640,18 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
639640
}
640641

641642
const len = this.length;
642-
if (len === 0)
643-
return '';
644643

645-
if (!start || start < 0)
644+
if (start <= 0)
646645
start = 0;
647646
else if (start >= len)
648647
return '';
648+
else
649+
start |= 0;
649650

650651
if (end === undefined || end > len)
651652
end = len;
652-
else if (end <= 0)
653-
return '';
654-
655-
start |= 0;
656-
end |= 0;
653+
else
654+
end |= 0;
657655

658656
if (end <= start)
659657
return '';
@@ -672,10 +670,10 @@ Buffer.prototype.equals = function equals(otherBuffer) {
672670
};
673671

674672
// Override how buffers are presented by util.inspect().
675-
Buffer.prototype[customInspectSymbol] = function inspect() {
676-
var str = '';
677-
var max = exports.INSPECT_MAX_BYTES;
678-
str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim();
673+
Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
674+
const max = exports.INSPECT_MAX_BYTES;
675+
const actualMax = Math.min(max, this.length);
676+
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
679677
const remaining = this.length - max;
680678
if (remaining > 0)
681679
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
@@ -977,9 +975,8 @@ Buffer.prototype.toJSON = function toJSON() {
977975
for (var i = 0; i < this.length; ++i)
978976
data[i] = this[i];
979977
return { type: 'Buffer', data };
980-
} else {
981-
return { type: 'Buffer', data: [] };
982978
}
979+
return { type: 'Buffer', data: [] };
983980
};
984981

985982
function adjustOffset(offset, length) {

0 commit comments

Comments
 (0)