Debian Codegraph @

README.md

raw symbols SCIP
1 # chardetng
2
3 [![crates.io](https://meritbadge.herokuapp.com/chardetng)](https://crates.io/crates/chardetng)
4 [![docs.rs](https://docs.rs/chardetng/badge.svg)](https://docs.rs/chardetng/)
5 [![Apache 2 / MIT dual-licensed](https://img.shields.io/badge/license-Apache%202%20%2F%20MIT-blue.svg)](https://github.com/hsivonen/chardetng/blob/master/COPYRIGHT)
6
7 A character encoding detector for legacy Web content.
8
9 ## Licensing
10
11 Please see the file named
12 [COPYRIGHT](https://github.com/hsivonen/chardetng/blob/master/COPYRIGHT).
13
14 ## Documentation
15
16 Generated [API documentation](https://docs.rs/chardetng/) is available
17 online.
18
19 There is a [long-form write-up](https://hsivonen.fi/chardetng/) about the
20 design and motivation of the crate.
21
22 ## Purpose
23
24 The purpose of this detector is user retention for Firefox by ensuring that the long tail of the legacy Web is not more convenient to use in Chrome than in Firefox. (Chrome deployed [ced](https://github.com/google/compact_enc_det/), which left Firefox less convenient to use until the deployment of this detector.)
25
26 ## About the Name
27
28 `chardet` was the name of Mozilla's old encoding detector. I named this one `chardetng`, because this the next generation of encoding detector in Firefox. There is no code reuse from the old `chardet`.
29
30 ## Optimization Goals
31
32 This crate aims to be more accurate than ICU, more complete than `chardet`, more explainable and modifiable than `compact_enc_det` (aka. ced), and, in an application that already depends on `encoding_rs` for other reasons, smaller in added binary footprint than `compact_enc_det`.
33
34 ## Rayon support
35
36 Enabling the optional feature `multithreading` makes `chardetng` run the detectors for individual encodings in parallel. Unfortunately, the performance doesn't scale linearly with CPU cores, but it's still better than single-threaded performance in terms of wall-clock time if a single instance of `chardetng` is running. In terms of combined CPU core usage, the `multithreading` mode is quite a bit worse than the single-threaded more, so if you can find a parallelization point at some higher-level task such that you could have multiple instances of `chardetng` running in paraller each on a single thread, you'll get better results doing that.
37
38 ## `no_std` support
39
40 `chardetng` works in a `no_std` environment that does not have an allocator.
41
42 ## Principle of Operation
43
44 In general `chardetng` prefers to do negative matching (rule out possibilities from the set of plausible encodings) than to do positive matching. Since negative matching is insufficient, there is positive matching, too.
45
46 * Except for ISO-2022-JP, pairs of ASCII bytes never contribute to the detection, which has the effect of ignoring HTML syntax without an HTML-aware state machine.
47 * A single encoding error disqualifies an encoding from the set of possible outcomes. Notably, as the length of the input increases, it becomes increasingly improbable for the input to be valid according to a legacy CJK encoding without being intended as such. Also, there are single-byte encodings that have unmapped bytes in areas that are in active use by other encodings, so such bytes narrow the set of possibilities very effectively.
48 * A single occurrence of a C1 control character disqualifies an encoding from possible outcomes.
49 * The first non-ASCII character being a half-width katakana character disqualifies an encoding. (This is _very_ effective for deciding between Shift_JIS and EUC-JP.)
50 * For single-byte encodings, character pairs are given scores according to their relative frequencies in the applicable Wikipedias.
51 * There's a variety of smaller penalty rules, such as:
52 - For encodings for bicameral scripts, having an upper-case letter follow a lower-case letter is penalized.
53 - For Latin encodings, having three non-ASCII letters in a row is penalized a little and having four or more is penalized a lot.
54 - For non-Latin encodings, having a non-Latin letter right next to a Latin letter is penalized.
55 - For single-byte encodings, having a character pair (excluding pairs where both characters are ASCII) that never occurs in the Wikipedias for the applicable languages is heavily penalized.
56 - Turkish I paired with a space-like character does not get a score to avoid detecting English as Turkish.
57 - There's a dedicated state machine for giving score to windows-1252 ordinal indicators, which would otherwise be hard to give score to without breaking windows-1250 Romanian detection.
58 - 0xA0, which is no-break space in most single-byte encodings, is special-cased in IBM866 and CJK encodings to avoid misdetection.
59
60 ## Notes About Encodings
61
62 <dl>
63 <dt>UTF-8</dt>
64 <dd>Detected only if explicitly permitted by the argument to the `guess` method. It's harmful for Web browsers to detect UTF-8 without requiring user action, such as choosing a menu item, because Web developers would start relying on the detection.</dd>
65 <dt>UTF-16[BE|LE]</dt>
66 <dd>Not detected: Detecting these belongs on the BOM layer.</dd>
67 <dt>x-user-defined</dt>
68 <dd>Not detected: This encoding is for XHR. <code>&lt;meta charset=x-user-defined></code> in HTML is not unlabeled and means windows-1252.</dd>
69 <dt>Replacement</dt>
70 <dd>Not detected.</dd>
71 <dt>GB18030</dt>
72 <dd>Detected as GBK.</dd>
73 <dt>GBK</dt>
74 <dt>Big5</dt>
75 <dt>EUC-KR</dt>
76 <dt>Shift_JIS</dt>
77 <dt>windows-1250</dt>
78 <dt>windows-1251</dt>
79 <dt>windows-1252</dt>
80 <dt>windows-1253</dt>
81 <dt>windows-1254</dt>
82 <dt>windows-1255</dt>
83 <dt>windows-1256</dt>
84 <dt>windows-1257</dt>
85 <dt>windows-1258</dt>
86 <dt>windows-874</dt>
87 <dt>ISO-8859-2</dt>
88 <dt>ISO-8859-7</dt>
89 <dd>Detected: Historical locale-specific fallbacks.</dd>
90 <dt>EUC-JP</dt>
91 <dt>ISO-2022-JP</dt>
92 <dt>KOI8-U</dt>
93 <dt>ISO-8859-5</dt>
94 <dt>IBM866</dt>
95 <dd>Detected: Detected by multiple browsers past and present.</dd>
96 <dt>KOI8-R</dt>
97 <dd>Detected as KOI8-U. (Always guessing the U variant is less likely to corrupt non-box drawing characters.)</dd>
98 <dt>ISO-8859-8-I</dt>
99 <dd>Detected as windows-1255.</dd>
100 <dt>ISO-8859-4</dt>
101 <dd>Detected: Detected by IE and Chrome; in menu in IE and Firefox.</dd>
102 <dt>ISO-8859-6</dt>
103 <dd>Detected: Detected by IE and Chrome.</dd>
104 <dt>ISO-8859-8</dt>
105 <dd>Detected: Available in menu in IE and Firefox.</dd>
106 <dt>ISO-8859-13</dt>
107 <dd>Detected: Detected by Chrome. This encoding is so similar to windows-1257 that menu items for windows-1257 can be considered to accommodate this one in IE and Firefox. Due to the mechanics of this detector, if this wasn't included as a separate item, the windows-1257 detection wouldn't catch the cases that use curly quotes and are invalid as windows-1257.</dd>
108 <dt>x-mac-cyrillic</dt>
109 <dd>Not detected: Not detected by IE and Chrome. (Was previously detected by Firefox.)</dd>
110 <dt>ISO-8859-3</dt>
111 <dt>ISO-8859-10</dt>
112 <dt>ISO-8859-14</dt>
113 <dt>ISO-8859-15</dt>
114 <dt>ISO-8859-16</dt>
115 <dt>macintosh</dt>
116 <dd>Not detected: These encodings have never been a locale-specific fallback in a major browser or a menu item in IE.</dd>
117 </dl>
118
119 ## Known Problems
120
121 * GBK detection is less accurate than in ced for short titles consisting of fewer than six hanzi. This is mostly due to the design that prioritizes optimizing binary size over accuracy on very short inputs.
122 * Thai detection is inaccurate for short inputs.
123 * windows-1257 detection is very inaccurate. (This detector currently doesn't use trigrams. ced uses 8 KB of trigram data to solve this.)
124 * On non-generic domains, some encodings that are confusable with the legacy encodings native to the TLD are excluded from guesses outright unless the input is invalid according to all the TLD-native encodings.
125
126 ## Associated tools
127
128 * [traindet](https://github.com/hsivonen/traindet) tool for computing the statistics for the generated code
129 * [detector_char_classes](https://github.com/hsivonen/detector_char_classes/) classification of characters in the single-byte encodings
130 * [charcounts](https://github.com/hsivonen/charcounts) intermediate files for traindet that make it possible to rerun the code generation without rerunning the statistic gathering
131 * [testdet](https://github.com/hsivonen/testdet) testing tool
132
133 ## Roadmap
134
135 No planned improvements.
136
137 - [x] Investigate parallelizing the `feed` method using Rayon.
138 - [x] Improve windows-874 detection for short inputs.
139 - [ ] ~Improve GBK detection for short inputs.~
140 - [ ] ~Reorganize the frequency data for telling short GBK, EUC-JP, and EUC-KR inputs apart.~
141 - [ ] ~Make Lithuanian and Latvian detection on generic domains a lot more accurate (likely requires looking at trigrams).~
142 - [x] Tune Central European detection.
143 - [ ] ~Tune the penalties applied to confusable encodings on non-generic TLDs to make detection of confusable encodings possible on non-generic TLDs.~
144 - [x] Reduce the binary size by not storing the scoring for implausible-next-to-alphabetic character classes.
145 - [ ] ~Reduce the binary size by classifying ASCII algorithmically.~
146 - [ ] ~Reduce the binary size by not storing the scores for C1 controls.~
147
148 ## Release Notes
149
150 ### 0.1.17
151
152 * Handle non-space space-like bytes following a windows-1252 copyright sign.
153
154 ### 0.1.16
155
156 * Detect windows-1252 copyright sign surrounded by spaces as windows-1252.
157
158 ### 0.1.15
159
160 * Make the crate work in `no_std` without an allocator.
161
162 ### 0.1.14
163
164 * Add `guess_assess` that provides more information about the guess.
165 * Upgrade the `cfg-if` dependency to 1.0.
166
167 ### 0.1.13
168
169 * Undo the limit on CJK extra scoring (from version 0.1.10). This change never made it to Gecko and, therefore, wasn't validated with in-practice telemetry.
170 * Detect inputs that have a lot of half-width katakana.
171
172 ### 0.1.12
173
174 * Fix edge case oversights in code added in the previous release.
175
176 ### 0.1.11
177
178 * Tolerate Windows and Classic Mac OS extensions to legacy CJK encodings and tolerate JIS X 0213 extensions to Shift_JIS and EUC-JP (not ISO-2022-JP).
179
180 ### 0.1.10
181
182 * Stop computing extra scores for common CJK characters after enough extra-score-eligible characters have been seen. (Improves performance with long CJK inputs.)
183 * Add Rayon support.
184 * Avoid detecting windows-1252 euro sign as GBK.
185
186 ### 0.1.9
187
188 * Fix a bug in ASCII prefix skipping. (Was introduced in 0.1.7.)
189
190 ### 0.1.8
191
192 * Avoid detecting English with no-break spaces as GBK or EUC-KR.
193
194 ### 0.1.7
195
196 * Avoid misdetecting windows-1252 English as windows-1254.
197 * Avoid misdetecting windows-1252 English as IBM866.
198 * Improve Chinese and Japanese detection by not giving single-byte encodings score for letter next to digit.
199 * Improve Italian, Portuguese, Castilian, Catalan, and Galician detection by taking into account ordinal indicator use.
200 * Reduce lookup table size.
201
202 ### 0.1.6
203
204 * Tune Central European detection.
205
206 ### 0.1.5
207
208 * Improve Thai accuracy a lot.
209 * Improve accuracy of some languages a bit.
210 * Remove unused Hebrew ASCII table.
211
212 ### 0.1.4
213
214 * Properly take into account non-ASCII bytes at word boundaries for windows-1252. (Especially relevant for Italian and Catalan.)
215 * Move Estonian from the Baltic model to the Western model. This improves overall Estonian detection but causes š and ž encoded as windows-1257, ISO-8859-13, or ISO-8859-4 to get misdecoded. (It would be possible to add a post-processing step to adjust for š and ž, but this would cause reloads given the way chardetng is integrated with Firefox.)
216 * Properly classify letters that ISO-8859-4 has but windows-1257 doesn't have in order to avoid misdetecting non-ISO-8859-4 input as ISO-8859-4.
217 * Improve character classification of windows-1254.
218 * Avoid classifying byte 0xA1 or above as space-like.
219 * Reduce binary size by collapsing similar character classes.
220
221 ### 0.1.3
222
223 * Return TLD-affiliated encoding if UTF-8 is valid but prohibited.
224
225 ### 0.1.2
226
227 * Return UTF-8 if valid and allowed even if all-ASCII.
228 * Return windows-1252 if UTF-8 valid and prohibited, because various test cases require this.
229
230 ### 0.1.1
231
232 * Detect Visual Hebrew more often.
233
234 ### 0.1.0
235
236 * Initial release.