buffer: simplify code

This refactors some code for simplicity. It also removes a call
indirection used in the buffers custom inspect function.

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Ruben Bridgewater 2018-12-20 03:40:40 +01:00
parent e7e26b2119
commit 65d8179b0d
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762

View File

@ -296,11 +296,13 @@ Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) {
// If --zero-fill-buffers command line argument is set, a zero-filled // If --zero-fill-buffers command line argument is set, a zero-filled
// buffer is returned. // buffer is returned.
function SlowBuffer(length) { function SlowBuffer(length) {
const len = +length;
// eslint-disable-next-line eqeqeq // eslint-disable-next-line eqeqeq
if (+length != length) if (len != length)
length = 0; length = 0;
assertSize(+length); else
return createUnsafeBuffer(+length); assertSize(len);
return createUnsafeBuffer(len);
} }
Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype); Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
@ -317,9 +319,8 @@ function allocate(size) {
poolOffset += size; poolOffset += size;
alignPool(); alignPool();
return b; return b;
} else {
return createUnsafeBuffer(size);
} }
return createUnsafeBuffer(size);
} }
function fromString(string, encoding) { function fromString(string, encoding) {
@ -637,21 +638,18 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
} }
const len = this.length; const len = this.length;
if (len === 0)
return '';
if (!start || start < 0) if (start <= 0)
start = 0; start = 0;
else if (start >= len) else if (start >= len)
return ''; return '';
else
start |= 0;
if (end === undefined || end > len) if (end === undefined || end > len)
end = len; end = len;
else if (end <= 0) else
return ''; end |= 0;
start |= 0;
end |= 0;
if (end <= start) if (end <= start)
return ''; return '';
@ -670,10 +668,10 @@ Buffer.prototype.equals = function equals(otherBuffer) {
}; };
// Override how buffers are presented by util.inspect(). // Override how buffers are presented by util.inspect().
Buffer.prototype[customInspectSymbol] = function inspect() { Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
var str = ''; const max = exports.INSPECT_MAX_BYTES;
var max = exports.INSPECT_MAX_BYTES; const actualMax = Math.min(max, this.length);
str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim(); let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
const remaining = this.length - max; const remaining = this.length - max;
if (remaining > 0) if (remaining > 0)
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`; str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
@ -975,9 +973,8 @@ Buffer.prototype.toJSON = function toJSON() {
for (var i = 0; i < this.length; ++i) for (var i = 0; i < this.length; ++i)
data[i] = this[i]; data[i] = this[i];
return { type: 'Buffer', data }; return { type: 'Buffer', data };
} else {
return { type: 'Buffer', data: [] };
} }
return { type: 'Buffer', data: [] };
}; };
function adjustOffset(offset, length) { function adjustOffset(offset, length) {