Skip to content

Commit 922b89e

Browse files
committed
Optimize ucs2Write
1 parent b5742b6 commit 922b89e

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

index.js

+14-17
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,20 @@ function base64Write (buf, string, offset, length) {
890890
}
891891

892892
function ucs2Write (buf, string, offset, length) {
893-
return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
893+
length >>>= 1
894+
895+
if (length > string.length) {
896+
length = string.length
897+
}
898+
899+
for (let i = 0; i < length; i++) {
900+
const ch = string.charCodeAt(i)
901+
902+
buf[offset + i * 2 + 0] = ch >> 0
903+
buf[offset + i * 2 + 1] = ch >> 8
904+
}
905+
906+
return length * 2
894907
}
895908

896909
Buffer.prototype.write = function write (string, offset, length, encoding) {
@@ -2057,22 +2070,6 @@ function utf8ToBytes (string, units) {
20572070
return bytes
20582071
}
20592072

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

0 commit comments

Comments
 (0)