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: little improve for Buffer.concat method #1437

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
10 changes: 5 additions & 5 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ Buffer.concat = function(list, length) {
if (!Array.isArray(list))
throw new TypeError('list argument must be an Array of Buffers.');

if (list.length === 0)
return new Buffer(0);
else if (list.length === 1)
return list[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

By the way, should we follow the below assertions:

assert.equal(Buffer.cancat(anyArray, len).length, len);

however

assert.equal(Buffer.cancat([], 1).length, 1); // this line would fail
assert.equal(Buffer.concat([new Buffer(20)]).length, 10); this line would fail as well and importantly i think we need to cut the buffer to 10.

@JacksonTian I know this should not be related to your patch, but it would be great if this is able to be fixed(bug? unsure) :p though cc @trevnorris

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @yorkie for bringing that up. Let's address that issue in another PR and keep this one simple.


if (length === undefined) {
length = 0;
for (var i = 0; i < list.length; i++)
Expand All @@ -255,11 +260,6 @@ Buffer.concat = function(list, length) {
length = length >>> 0;
}

if (list.length === 0)
return new Buffer(0);
else if (list.length === 1)
return list[0];

var buffer = new Buffer(length);
var pos = 0;
for (var i = 0; i < list.length; i++) {
Expand Down