Detect and fix KOI8-R mojibake (fixes #231) - #234
Open
sebollin wants to merge 1 commit into
Open
Conversation
The example in rspeer#231 is reported as CP437/CP866, but it is KOI8-R: in that encoding 0xD0 is 'п' and 0xD1 is 'я', the two lead bytes of Cyrillic in UTF-8, and 0x95/0x81 are '∙'/'│'. No other Cyrillic codepage round-trips it -- cp866, koi8-u, iso-8859-5 and mac-cyrillic all fail to encode it. Two changes are needed, and neither one alone is enough: - add koi8-r to CHARMAP_ENCODINGS, so the text can be re-encoded; - add lower_common/upper_common to the rule that pairs a character with a box-drawing character, so that is_bad() fires. KOI8-R mojibake of Cyrillic puts a Cyrillic letter immediately before a box-drawing character, and no existing alternative covers that pair. Adding koi8-r on its own leaves the example unchanged, because the badness heuristic never flags it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes#231.
The example in the issue is reported as CP437/CP866, but I'm fairly sure it's KOI8-R. In KOI8-R,
0xD0isпand0xD1isя— the two lead bytes of Cyrillic in UTF-8 — and0x95and0x81are∙and│. That's why the mojibake comes out as Cyrillic letters interleaved with box-drawing characters.I tried the codepages the reporter suggested and the neighbouring Cyrillic ones too:
cp866,koi8-u,iso-8859-5andmac-cyrillicall fail to encode the text, so KOI8-R is the only one that round-trips it.Two changes are needed, and neither one is enough on its own:
koi8-rinCHARMAP_ENCODINGS, so the text can be re-encoded at all.lower_commonandupper_commonin the badness alternative that pairs a character with a box-drawing character. KOI8-R mojibake of Cyrillic puts a Cyrillic letter immediately before a box-drawing character, and no existing alternative covers that pair, sois_bad()never fires.If you apply only the first one, the example still comes back unchanged. That took me a while to work out, so I mention it in case someone tries the shorter patch.
I added the example to
in-the-wild.json. The suite goes from 351 to 353 passing with the same 10 xfails, and the negative cases are untouched.