Skip to content

Commit 201785b

Browse files
committed
Optimize ucs2Write
1 parent 01abbf8 commit 201785b

1 file changed

Lines changed: 14 additions & 17 deletions

File tree

index.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,20 @@ function base64Write (buf, string, offset, length) {
885885
}
886886

887887
function ucs2Write (buf, string, offset, length) {
888-
return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
888+
length >>>= 1
889+
890+
if (length > string.length) {
891+
length = string.length
892+
}
893+
894+
for (let i = 0; i < length; i++) {
895+
const ch = string.charCodeAt(i)
896+
897+
buf[offset + i * 2 + 0] = ch >> 0
898+
buf[offset + i * 2 + 1] = ch >> 8
899+
}
900+
901+
return length * 2
889902
}
890903

891904
Buffer.prototype.write = function write (string, offset, length, encoding) {
@@ -2058,22 +2071,6 @@ function utf8ToBytes (string, units) {
20582071
return bytes
20592072
}
20602073

2061-
function utf16leToBytes (str, units) {
2062-
let c, hi, lo
2063-
const byteArray = []
2064-
for (let i = 0; i < str.length; ++i) {
2065-
if ((units -= 2) < 0) break
2066-
2067-
c = str.charCodeAt(i)
2068-
hi = c >> 8
2069-
lo = c % 256
2070-
byteArray.push(lo)
2071-
byteArray.push(hi)
2072-
}
2073-
2074-
return byteArray
2075-
}
2076-
20772074
function base64ToBytes (str) {
20782075
return base64.toByteArray(base64clean(str))
20792076
}

0 commit comments

Comments
 (0)