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 as_whatwg(encoding: &str) -> Option<&'static encoding_rs::Encoding> {
24    if encoding.eq_ignore_ascii_case("MAC-CYRILLIC") {
25        return Some(encoding_rs::X_MAC_CYRILLIC);
26    }
27    if encoding.eq_ignore_ascii_case("HZ-GB-2312")
28        || encoding.eq_ignore_ascii_case("ISO-2022-CN")
29        || encoding.eq_ignore_ascii_case("ISO-2022-KR")
30    {
31        return None;
32    }
33    encoding_rs::Encoding::for_label_no_replacement(encoding.as_bytes())
34}
35
36#[cfg(test)]
37mod tests {
38
39    macro_rules! assert_encoding_for_name {
40        ($encoding:ident, $name:expr) => {{
41            let expected = encoding_rs::$encoding;
42            let actual =
43                encoding_rs::Encoding::for_label($name.as_bytes()).expect("Expected an encoding");
44            assert_eq!(expected.name(), actual.name());
45        }};
46    }
47
48    macro_rules! assert_no_encoding_for_name {
49        ($name:expr) => {{
50            assert_eq!(encoding_rs::Encoding::for_label($name.as_bytes()), None);
51        }};
52    }
53
54    #[test]
55    fn test_encoding_for_label_handles_uchardet_names_as_expected() {
56        // Note that we may not want to map "ASCII" to 1252 at higher levels.
57        assert_encoding_for_name!(WINDOWS_1252, "ASCII");
58        assert_encoding_for_name!(BIG5, "BIG5");
59        assert_encoding_for_name!(EUC_JP, "EUC-JP");
60        assert_encoding_for_name!(EUC_KR, "EUC-KR");
61        assert_encoding_for_name!(GB18030, "GB18030");
62        assert_encoding_for_name!(IBM866, "IBM866");
63        assert_encoding_for_name!(ISO_2022_JP, "ISO-2022-JP");
64        assert_encoding_for_name!(WINDOWS_1252, "ISO-8859-1"); // 浏览器应按 ‌Windows-1252‌ 解析,需确认 uchardet 是否也是一样的处理。
65        assert_encoding_for_name!(ISO_8859_2, "ISO-8859-2");
66        assert_encoding_for_name!(ISO_8859_3, "ISO-8859-3");
67        assert_encoding_for_name!(ISO_8859_4, "ISO-8859-4");
68        assert_encoding_for_name!(ISO_8859_5, "ISO-8859-5");
69        assert_encoding_for_name!(ISO_8859_6, "ISO-8859-6");
70        assert_encoding_for_name!(ISO_8859_7, "ISO-8859-7");
71        assert_encoding_for_name!(ISO_8859_8, "ISO-8859-8");
72        assert_encoding_for_name!(WINDOWS_1254, "ISO-8859-9"); // 需确认。
73        assert_encoding_for_name!(ISO_8859_10, "ISO-8859-10");
74        assert_encoding_for_name!(WINDOWS_874, "ISO-8859-11");
75        assert_encoding_for_name!(ISO_8859_13, "ISO-8859-13");
76        assert_encoding_for_name!(ISO_8859_15, "ISO-8859-15");
77        assert_encoding_for_name!(ISO_8859_16, "ISO-8859-16");
78        assert_encoding_for_name!(WINDOWS_874, "TIS-620");
79        assert_encoding_for_name!(KOI8_R, "KOI8-R");
80        assert_encoding_for_name!(SHIFT_JIS, "SHIFT_JIS");
81        assert_encoding_for_name!(UTF_8, "UTF-8");
82        // This maps to UTF_16LE because that's the most common, but the
83        // decoder can actually decode either in the default mode by detecting
84        // byte order marks, which is the only way `uchardet` can detect
85        // either, so it's not a problem.
86        assert_encoding_for_name!(UTF_16LE, "UTF-16");
87        // This is not supported by the encoding standard, because it appears
88        // to be rare in the wild.
89        assert_encoding_for_name!(WINDOWS_1250, "Windows-1250");
90        assert_encoding_for_name!(WINDOWS_1251, "Windows-1251");
91        assert_encoding_for_name!(WINDOWS_1252, "Windows-1252");
92        assert_encoding_for_name!(WINDOWS_1253, "Windows-1253");
93        assert_encoding_for_name!(WINDOWS_1255, "Windows-1255");
94        assert_encoding_for_name!(WINDOWS_1256, "Windows-1256");
95        assert_encoding_for_name!(WINDOWS_1257, "Windows-1257");
96        assert_encoding_for_name!(WINDOWS_1258, "Windows-1258");
97
98        // X-*
99        assert_no_encoding_for_name!("MAC-CENTRALEUROPE");
100        assert_no_encoding_for_name!("MAC-CYRILLIC");
101        // X-*
102        assert_no_encoding_for_name!("X-MAC-CENTRALEUROPE");
103        assert_encoding_for_name!(X_MAC_CYRILLIC, "X-MAC-CYRILLIC");
104
105        // REPLACEMENT
106        assert_encoding_for_name!(REPLACEMENT, "HZ-GB-2312");
107        assert_encoding_for_name!(REPLACEMENT, "ISO-2022-CN");
108        assert_encoding_for_name!(REPLACEMENT, "ISO-2022-KR");
109
110        assert_no_encoding_for_name!("IBM737");
111        assert_no_encoding_for_name!("CP737");
112        // This does not appear to be supported by the Encoding Standard.
113        assert_no_encoding_for_name!("EUC-TW");
114        assert_no_encoding_for_name!("GEORGIAN-ACADEMY");
115        assert_no_encoding_for_name!("GEORGIAN-PS");
116        assert_no_encoding_for_name!("IBM852");
117        assert_no_encoding_for_name!("IBM855");
118        assert_no_encoding_for_name!("IBM862");
119        assert_no_encoding_for_name!("IBM865");
120        assert_no_encoding_for_name!("Johab");
121        // superset of EUC-KR.
122        assert_no_encoding_for_name!("UHC");
123        assert_no_encoding_for_name!("UTF-32BE");
124        assert_no_encoding_for_name!("UTF-32LE");
125        assert_no_encoding_for_name!("VISCII");
126        assert_no_encoding_for_name!("X-ISO-10646-UCS-4-34121");
127        assert_no_encoding_for_name!("X-ISO-10646-UCS-4-21431");
128    }
129}