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

Drop base64-js dependency, and optimise Buffer.write #349

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Optimize asciiWrite
  • Loading branch information
chjj committed Feb 5, 2024
commit b5742b6eae2f3e65e0c5d95d0e6bcf6b279975e6
19 changes: 9 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,15 @@ function utf8Write (buf, string, offset, length) {
}

function asciiWrite (buf, string, offset, length) {
return blitBuffer(asciiToBytes(string), buf, offset, length)
if (length > string.length) {
length = string.length
}

for (let i = 0; i < length; i++) {
buf[offset + i] = string.charCodeAt(i)
}

return length
}

function base64Write (buf, string, offset, length) {
Expand Down Expand Up @@ -2049,15 +2057,6 @@ function utf8ToBytes (string, units) {
return bytes
}

function asciiToBytes (str) {
const byteArray = []
for (let i = 0; i < str.length; ++i) {
// Node's code seems to be doing this and not & 0x7F..
byteArray.push(str.charCodeAt(i) & 0xFF)
}
return byteArray
}

function utf16leToBytes (str, units) {
let c, hi, lo
const byteArray = []
Expand Down