-
Notifications
You must be signed in to change notification settings - Fork 42
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
Convert writingBuf to Buffer to compute the original string length #188
Convert writingBuf to Buffer to compute the original string length #188
Conversation
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.
lgtm
Seems a good fix. I'll need to bench it.
index.js
Outdated
} | ||
function releaseWritingBuf (writingBuf, len, n) { |
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.
} | |
function releaseWritingBuf (writingBuf, len, n) { | |
} | |
/** | |
* Please add a jsdoc block here. | |
*/ | |
function releaseWritingBuf (writingBuf, len, n) { |
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.
Added
index.js
Outdated
if (typeof writingBuf === 'string' && writingBuf.length !== n) { | ||
// Since the fs.write callback parameter `n` means how many bytes the passed of string | ||
// We calculate the original string length for avoiding the multi-byte character issue | ||
n = Buffer.from(writingBuf).subarray(0, n).toString().length | ||
} | ||
len = Math.max(len - n, 0) | ||
writingBuf = writingBuf.slice(n) | ||
return { writingBuf, len } |
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.
Could most of this be solved by comparing theBuffer.byteLength
instead of theBuffer.length
?
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.
Thank you. Using .byteLength
is more sensible to compare byteLength.
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.
lgtm
benchmarks are ok.
Fixes #139
Although #139 (comment) suggested converting
this._writingBuf
to a buffer, I found that it might reduce performance if we convert everythis._writingBuf
which just simply writes string.Therefore, I only handle cases when multi-byte characters exist.