Skip to main content

uchardet_git/
encoding.rs

1// MIT License
2//
3// Copyright (c) 2026 worksoup <https://github.com/worksoup/>
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23pub fn to_standard(encoding: &str) -> Option<&'static encoding_rs::Encoding> {
24    match encoding {
25        "MAC-CENTRALEUROPE" => Some(encoding_rs::X_MAC_CYRILLIC),
26        "MAC-CYRILLIC" => Some(encoding_rs::X_MAC_CYRILLIC),
27        "HZ-GB-2312" => None,
28        "ISO-2022-CN" => None,
29        "ISO-2022-KR" => None,
30        _ => encoding_rs::Encoding::for_label_no_replacement(encoding.as_bytes()),
31    }
32}
33#[cfg(test)]
34mod tests {
35
36    macro_rules! assert_encoding_for_name {
37        ($encoding:ident, $name:expr) => {{
38            let expected = encoding_rs::$encoding;
39            let actual =
40                encoding_rs::Encoding::for_label($name.as_bytes()).expect("Expected an encoding");
41            assert_eq!(expected.name(), actual.name());
42        }};
43    }
44
45    macro_rules! assert_no_encoding_for_name {
46        ($name:expr) => {{
47            assert_eq!(encoding_rs::Encoding::for_label($name.as_bytes()), None);
48        }};
49    }
50
51    #[test]
52    fn test_encoding_for_label_handles_uchardet_names_as_expected() {
53        // Note that we may not want to map "ASCII" to 1252 at higher levels.
54        assert_encoding_for_name!(WINDOWS_1252, "ASCII");
55        assert_encoding_for_name!(BIG5, "BIG5");
56        assert_encoding_for_name!(EUC_JP, "EUC-JP");
57        assert_encoding_for_name!(EUC_KR, "EUC-KR");
58        assert_encoding_for_name!(GB18030, "GB18030");
59        assert_encoding_for_name!(IBM866, "IBM866");
60        assert_encoding_for_name!(ISO_2022_JP, "ISO-2022-JP");
61        assert_encoding_for_name!(WINDOWS_1252, "ISO-8859-1"); // 浏览器应按 ‌Windows-1252‌ 解析,需确认 uchardet 是否也是一样的处理。
62        assert_encoding_for_name!(ISO_8859_2, "ISO-8859-2");
63        assert_encoding_for_name!(ISO_8859_3, "ISO-8859-3");
64        assert_encoding_for_name!(ISO_8859_4, "ISO-8859-4");
65        assert_encoding_for_name!(ISO_8859_5, "ISO-8859-5");
66        assert_encoding_for_name!(ISO_8859_6, "ISO-8859-6");
67        assert_encoding_for_name!(ISO_8859_7, "ISO-8859-7");
68        assert_encoding_for_name!(ISO_8859_8, "ISO-8859-8");
69        assert_encoding_for_name!(WINDOWS_1254, "ISO-8859-9"); // 需确认。
70        assert_encoding_for_name!(ISO_8859_10, "ISO-8859-10");
71        assert_encoding_for_name!(WINDOWS_874, "ISO-8859-11");
72        assert_encoding_for_name!(ISO_8859_13, "ISO-8859-13");
73        assert_encoding_for_name!(ISO_8859_15, "ISO-8859-15");
74        assert_encoding_for_name!(ISO_8859_16, "ISO-8859-16");
75        assert_encoding_for_name!(WINDOWS_874, "TIS-620");
76        assert_encoding_for_name!(KOI8_R, "KOI8-R");
77        assert_encoding_for_name!(SHIFT_JIS, "SHIFT_JIS");
78        assert_encoding_for_name!(UTF_8, "UTF-8");
79        // This maps to UTF_16LE because that's the most common, but the
80        // decoder can actually decode either in the default mode by detecting
81        // byte order marks, which is the only way `uchardet` can detect
82        // either, so it's not a problem.
83        assert_encoding_for_name!(UTF_16LE, "UTF-16");
84        // This is not supported by the encoding standard, because it appears
85        // to be rare in the wild.
86        assert_encoding_for_name!(WINDOWS_1250, "Windows-1250");
87        assert_encoding_for_name!(WINDOWS_1251, "Windows-1251");
88        assert_encoding_for_name!(WINDOWS_1252, "Windows-1252");
89        assert_encoding_for_name!(WINDOWS_1253, "Windows-1253");
90        assert_encoding_for_name!(WINDOWS_1255, "Windows-1255");
91        assert_encoding_for_name!(WINDOWS_1256, "Windows-1256");
92        assert_encoding_for_name!(WINDOWS_1257, "Windows-1257");
93        assert_encoding_for_name!(WINDOWS_1258, "Windows-1258");
94
95        // X-*
96        assert_no_encoding_for_name!("MAC-CENTRALEUROPE");
97        assert_no_encoding_for_name!("MAC-CYRILLIC");
98
99        // REPLACEMENT
100        assert_encoding_for_name!(REPLACEMENT, "HZ-GB-2312");
101        assert_encoding_for_name!(REPLACEMENT, "ISO-2022-CN");
102        assert_encoding_for_name!(REPLACEMENT, "ISO-2022-KR");
103
104        assert_no_encoding_for_name!("IBM737");
105        assert_no_encoding_for_name!("CP737");
106        // This does not appear to be supported by the Encoding Standard.
107        assert_no_encoding_for_name!("EUC-TW");
108        assert_no_encoding_for_name!("GEORGIAN-ACADEMY");
109        assert_no_encoding_for_name!("GEORGIAN-PS");
110        assert_no_encoding_for_name!("IBM852");
111        assert_no_encoding_for_name!("IBM855");
112        assert_no_encoding_for_name!("IBM862");
113        assert_no_encoding_for_name!("IBM865");
114        assert_no_encoding_for_name!("Johab");
115        // superset of EUC-KR.
116        assert_no_encoding_for_name!("UHC");
117        assert_no_encoding_for_name!("UTF-32BE");
118        assert_no_encoding_for_name!("UTF-32LE");
119        assert_no_encoding_for_name!("VISCII");
120        assert_no_encoding_for_name!("X-ISO-10646-UCS-4-34121");
121        assert_no_encoding_for_name!("X-ISO-10646-UCS-4-21431");
122    }
123}