Skip to content

Commit f19c5e8

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

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

fallback/multi-byte.js

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -357,43 +357,51 @@ 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+
}
370380
} else {
371-
res += String.fromCodePoint(p) // Can validly return replacement
381+
if (b >= 0x81 && b <= 0xfe) {
382+
g3 = b
383+
} else {
384+
pushback.push(b, g2)
385+
g1 = g2 = 0
386+
res += String.fromCharCode(err())
387+
}
372388
}
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) {
384-
g2 = b
385389
} else {
386-
let cp
387-
if (b >= 0x40 && b <= 0xfe && b !== 0x7f) {
388-
cp = gb18030[(g1 - 0x81) * 190 + b - (b < 0x7f ? 0x40 : 0x41)]
389-
}
390-
391-
g1 = 0
392-
if (cp !== undefined && cp !== REP) {
393-
res += String.fromCodePoint(cp)
390+
if (b >= 0x30 && b <= 0x39) {
391+
g2 = b
394392
} else {
395-
res += String.fromCharCode(err())
396-
if (b < 128) res += String.fromCharCode(b) // can be processed immediately
393+
let cp
394+
if (b >= 0x40 && b <= 0xfe && b !== 0x7f) {
395+
cp = gb18030[(g1 - 0x81) * 190 + b - (b < 0x7f ? 0x40 : 0x41)]
396+
}
397+
398+
g1 = 0
399+
if (cp !== undefined && cp !== REP) {
400+
res += String.fromCodePoint(cp)
401+
} else {
402+
res += String.fromCharCode(err())
403+
if (b < 128) res += String.fromCharCode(b) // can be processed immediately
404+
}
397405
}
398406
}
399407
} else if (b < 128) {

0 commit comments

Comments
 (0)