Skip to content

Commit f68c38d

Browse files
committed
refactor: simplify utf-16 unfinishedBytes
1 parent 6d89df0 commit f68c38d

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

fallback/encoding.util.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,11 @@ export function unfinishedBytes(u, len, enc) {
2020
case 'utf-16le':
2121
case 'utf-16be': {
2222
// 0-3
23-
let p = 0
24-
if (len % 2 !== 0) p++ // uneven bytes
23+
const p = len % 2 // uneven byte length adds 1
2524
const l = len - p - 1
26-
if (len - p >= 2) {
27-
const last = enc === 'utf-16le' ? (u[l] << 8) ^ u[l - 1] : (u[l - 1] << 8) ^ u[l]
28-
if (last >= 0xd8_00 && last < 0xdc_00) p += 2 // lone lead
29-
}
30-
31-
return p
25+
if (l < 1) return p
26+
const last = enc === 'utf-16le' ? (u[l] << 8) ^ u[l - 1] : (u[l - 1] << 8) ^ u[l]
27+
return last >= 0xd8_00 && last < 0xdc_00 ? p + 2 : p // lone lead
3228
}
3329
}
3430

0 commit comments

Comments
 (0)