Repository navigation
🐛 fix(encoding): keep GBK alive past a pushback - #2487
Conversation
3a0edcd to
18e51be
Compare
On a four-byte GB18030 prefix whose fourth byte is invalid, the decoder
pushes the rest of the sequence back, the fourth byte included. chardetng
applies a single-byte-extension penalty at that byte and resets its decoder,
so the pushed-back bytes are never read again. turbohtml resumed at them. A
0xFF read again after a lead killed the candidate, and an ASCII byte after a
0xA0 or 0xFE lead took the penalty twice.
b"\xe26\x90\xff0\xa3\x89" chardetng: GBK before: IBM866
<GBK text> 81 36 fe ff <GBK text> chardetng: GBK before: windows-1252
Both penalty rules now resume after the byte that ends the sequence. A lead
followed directly by a 0xFF stays dead, as chardetng scores it.
18e51be to
cdd44bb
Compare
Merging this PR will improve performance by 5.61%2 warnings
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
When a GB18030 four-byte sequence fails on its third or fourth byte, the decoder pushes the unread bytes back. chardetng's GBK candidate takes the single-byte-extension penalty at that point and resets its decoder, so those bytes are never read again; turbohtml resumed at the pushed-back bytes, re-read them as a fresh lead plus an invalid byte, and could charge the penalty twice or drop the candidate. Under
.cn,b'\xe26\x90\xff0\xa3\x89'detected as IBM866 where chardetng answers GBK.Both of chardetng's GBK penalty rules (an invalid 0xFF, and a 0xA0/0xFD/0xFE third byte followed by ASCII or 0xFF) now resume after the byte that ends the sequence, as chardetng does.
Against the vendored chardetng oracle: 40,000 random GBK-shaped inputs, main matches 36,901 and this branch 36,923 (22 fixed); 30,000 inputs focused on the third-byte rule, main 29,904 and this branch 30,000 (96 fixed). No answer that matched chardetng changes. Pinned cases run one-shot and streamed at chunk sizes 1, 2, 3 and 16.