-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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: refactor to remove some duplicated code in fromObject. #4948
Closed
Closed
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
88f2a9f
buffer: refactor to remove some duplicated code in fromObject.
entertainyou cdaa4ea
fixup! buffer: refactor to remove some duplicated code in fromObject.
entertainyou c5a886b
fixup! buffer: refactor to remove some duplicated code in fromObject.
entertainyou e97323d
fixup! buffer: refactor to remove some duplicated code in fromObject.
entertainyou 980f208
fixup! fixup! buffer: refactor to remove some duplicated code in from…
entertainyou ab15735
fixup! fixup! fixup! buffer: refactor to remove some duplicated code …
entertainyou e16e88b
fixup! fixup! fixup! fixup! buffer: refactor to remove some duplicate…
entertainyou b274fe1
fixup! fixup! fixup! fixup! fixup! buffer: refactor to remove some du…
entertainyou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,6 +122,13 @@ function fromString(string, encoding) { | |
return b; | ||
} | ||
|
||
function fromArrayLike(obj) { | ||
const length = obj.length; | ||
const b = allocate(length); | ||
for (let i = 0; i < length; i++) | ||
b[i] = obj[i] & 255; | ||
return b; | ||
} | ||
|
||
function fromObject(obj) { | ||
if (obj instanceof Buffer) { | ||
|
@@ -134,14 +141,6 @@ function fromObject(obj) { | |
return b; | ||
} | ||
|
||
if (Array.isArray(obj)) { | ||
const length = obj.length; | ||
const b = allocate(length); | ||
for (let i = 0; i < length; i++) | ||
b[i] = obj[i] & 255; | ||
return b; | ||
} | ||
|
||
if (obj == null) { | ||
throw new TypeError('Must start with number, buffer, array or string'); | ||
} | ||
|
@@ -151,30 +150,20 @@ function fromObject(obj) { | |
} | ||
|
||
if (obj.buffer instanceof ArrayBuffer || obj.length) { | ||
let length; | ||
if (typeof obj.length !== 'number' || obj.length !== obj.length) | ||
length = 0; | ||
else | ||
length = obj.length; | ||
const b = allocate(length); | ||
for (let i = 0; i < length; i++) { | ||
b[i] = obj[i] & 255; | ||
if (typeof obj.length !== 'number' || obj.length !== obj.length) { | ||
return allocate(0); | ||
} else { | ||
return fromArrayLike(obj); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just an aside, all typed arrays could be more quickly handled in C++, but that doesn't concern this PR. at least now it's centralized and will be easier to make the change. :) |
||
} | ||
return b; | ||
} | ||
|
||
if (obj.type === 'Buffer' && Array.isArray(obj.data)) { | ||
var array = obj.data; | ||
const b = allocate(array.length); | ||
for (let i = 0; i < array.length; i++) | ||
b[i] = array[i] & 255; | ||
return b; | ||
return fromArrayLike(obj.data); | ||
} | ||
|
||
throw new TypeError('Must start with number, buffer, array or string'); | ||
} | ||
|
||
|
||
// Static methods | ||
|
||
Buffer.isBuffer = function isBuffer(b) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad. We are missing a corner case here. If the length of the array is zero we will throw an error. Perhaps we can change this to
'length' in obj
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about performance impact though. That said, if our tests didn't catch this case, may I ask you to update the test with empty array case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not very familiar with node/js internal, but I think the in operator should not be very slow compare to Array.isArray?
PR updated according to your comments