Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: simplify code #25151

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 16 additions & 19 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,13 @@ Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) {
// If --zero-fill-buffers command line argument is set, a zero-filled
// buffer is returned.
function SlowBuffer(length) {
const len = +length;
// eslint-disable-next-line eqeqeq
if (+length != length)
if (len != length)
length = 0;
assertSize(+length);
return createUnsafeBuffer(+length);
else
assertSize(len);
return createUnsafeBuffer(len);
}

Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
Expand All @@ -317,9 +319,8 @@ function allocate(size) {
poolOffset += size;
alignPool();
return b;
} else {
return createUnsafeBuffer(size);
}
return createUnsafeBuffer(size);
}

function fromString(string, encoding) {
Expand Down Expand Up @@ -637,21 +638,18 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: can this also be indented correctly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems correct? It's indented by exactly two spaces.


const len = this.length;
if (len === 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this optimization removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be detected below with the if (end <= start) check and it seemed an unnecessary check for the average case without hurting much in the empty buffer case.

return '';

if (!start || start < 0)
if (start <= 0)
start = 0;
else if (start >= len)
return '';
else
start |= 0;
addaleax marked this conversation as resolved.
Show resolved Hide resolved

if (end === undefined || end > len)
end = len;
else if (end <= 0)
return '';

start |= 0;
end |= 0;
else
end |= 0;

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

// Override how buffers are presented by util.inspect().
Buffer.prototype[customInspectSymbol] = function inspect() {
var str = '';
var max = exports.INSPECT_MAX_BYTES;
str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim();
Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
const max = exports.INSPECT_MAX_BYTES;
const actualMax = Math.min(max, this.length);
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
const remaining = this.length - max;
if (remaining > 0)
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
Expand Down Expand Up @@ -975,9 +973,8 @@ Buffer.prototype.toJSON = function toJSON() {
for (var i = 0; i < this.length; ++i)
data[i] = this[i];
return { type: 'Buffer', data };
} else {
return { type: 'Buffer', data: [] };
}
return { type: 'Buffer', data: [] };
};

function adjustOffset(offset, length) {
Expand Down