Skip to content

Commit 38e0eba

Browse files
committed
perf: slightly faster gb18030 decoding
1 parent 97459ad commit 38e0eba

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

fallback/multi-byte.js

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -357,30 +357,34 @@ const mappers = {
357357
// Same as the full loop, but without EOF handling
358358
while (i < end || pushback.length > 0) {
359359
const b = pushback.length > 0 ? pushback.pop() : arr[i++]
360-
if (g3) {
361-
if (b < 0x30 || b > 0x39) {
362-
pushback.push(b, g3, g2)
363-
g1 = g2 = g3 = 0
364-
res += String.fromCharCode(err())
365-
} else {
366-
const p = index((g1 - 0x81) * 12_600 + (g2 - 0x30) * 1260 + (g3 - 0x81) * 10 + b - 0x30)
367-
g1 = g2 = g3 = 0
368-
if (p === undefined) {
369-
res += String.fromCharCode(err())
360+
if (g1) {
361+
// g2 can be set only when g1 is set, g3 can be set only when g2 is set
362+
// hence, 3 checks for g3 is faster than 3 checks for g1
363+
if (g2) {
364+
if (g3) {
365+
if (b < 0x30 || b > 0x39) {
366+
pushback.push(b, g3, g2)
367+
g1 = g2 = g3 = 0
368+
res += String.fromCharCode(err())
369+
} else {
370+
const p = index(
371+
(g1 - 0x81) * 12_600 + (g2 - 0x30) * 1260 + (g3 - 0x81) * 10 + b - 0x30
372+
)
373+
g1 = g2 = g3 = 0
374+
if (p === undefined) {
375+
res += String.fromCharCode(err())
376+
} else {
377+
res += String.fromCodePoint(p) // Can validly return replacement
378+
}
379+
}
380+
} else if (b >= 0x81 && b <= 0xfe) {
381+
g3 = b
370382
} else {
371-
res += String.fromCodePoint(p) // Can validly return replacement
383+
pushback.push(b, g2)
384+
g1 = g2 = 0
385+
res += String.fromCharCode(err())
372386
}
373-
}
374-
} else if (g2) {
375-
if (b >= 0x81 && b <= 0xfe) {
376-
g3 = b
377-
} else {
378-
pushback.push(b, g2)
379-
g1 = g2 = 0
380-
res += String.fromCharCode(err())
381-
}
382-
} else if (g1) {
383-
if (b >= 0x30 && b <= 0x39) {
387+
} else if (b >= 0x30 && b <= 0x39) {
384388
g2 = b
385389
} else {
386390
let cp

0 commit comments

Comments
 (0)