ctb_formats_encoding/
standard.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Single-byte character mappings backed by `encoding_rs`.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use crate::mapping::SingleByteMapping;
30use std::collections::HashMap;
31use std::sync::LazyLock;
32
33/// Builds a `SingleByteMapping` table from an `encoding_rs::Encoding`.
34pub(crate) fn build_mapping_from_encoding_rs(
35    encoding: &'static encoding_rs::Encoding,
36) -> SingleByteMapping {
37    let mut decode_table = ['\0'; 256];
38    let mut encode_table = HashMap::new();
39    for i in 0u8..=255 {
40        let byte_slice = [i];
41        let (cow, _, _) = encoding.decode(&byte_slice);
42        if let Some(ch) = cow.chars().next() {
43            if let Some(slot) = decode_table.get_mut(usize::from(i)) {
44                *slot = ch;
45            }
46            encode_table.entry(ch).or_insert(i);
47        }
48    }
49    SingleByteMapping::from_raw(decode_table, encode_table)
50}
51
52/// Pre-initialized `SingleByteMapping` for Mac OS Roman encoding.
53pub(crate) static MACROMAN_MAPPING: LazyLock<SingleByteMapping> =
54    LazyLock::new(|| build_mapping_from_encoding_rs(encoding_rs::MACINTOSH));
55
56/// Pre-initialized `SingleByteMapping` for Windows-1252 (ANSI) encoding.
57pub(crate) static WINDOWS_1252_MAPPING: LazyLock<SingleByteMapping> =
58    LazyLock::new(|| build_mapping_from_encoding_rs(encoding_rs::WINDOWS_1252));
59
60/// Pre-initialized `SingleByteMapping` for ISO 8859-1 (Latin-1) encoding.
61pub(crate) static ISO_8859_1_MAPPING: LazyLock<SingleByteMapping> =
62    LazyLock::new(|| {
63        let mut decode_table = ['\0'; 256];
64        let mut encode_table = HashMap::new();
65        for i in 0u8..=255 {
66            let ch = char::from(i);
67            if let Some(slot) = decode_table.get_mut(usize::from(i)) {
68                *slot = ch;
69            }
70            encode_table.insert(ch, i);
71        }
72        SingleByteMapping::from_raw(decode_table, encode_table)
73    });
74
75/// Pre-initialized `SingleByteMapping` for standard 7-bit ASCII encoding.
76pub(crate) static ASCII_MAPPING: LazyLock<SingleByteMapping> =
77    LazyLock::new(|| {
78        let mut decode_table = ['\0'; 256];
79        let mut encode_table = HashMap::new();
80        for i in 0u8..=127 {
81            let ch = char::from(i);
82            if let Some(slot) = decode_table.get_mut(usize::from(i)) {
83                *slot = ch;
84            }
85            encode_table.insert(ch, i);
86        }
87        SingleByteMapping::from_raw(decode_table, encode_table)
88    });
89
90#[cfg(test)]
91#[allow(
92    clippy::panic,
93    clippy::expect_used,
94    clippy::unwrap_used,
95    clippy::unwrap_in_result,
96    clippy::panic_in_result_fn,
97    clippy::indexing_slicing,
98    clippy::arithmetic_side_effects,
99    reason = "Standard repository test boilerplate"
100)]
101mod tests {
102    use super::*;
103    use ctb_formats_utilities::encoding::CharEncoding;
104
105    #[crate::ctb_test]
106    fn test_macroman_encoding() {
107        let enc = CharEncoding::mac_roman();
108        let original = "Hello, World! ñ ü á";
109        let encoded = crate::encode(enc, original).unwrap();
110        let decoded = crate::decode(enc, &encoded).unwrap();
111        assert_eq!(original, decoded);
112        assert_eq!(
113            crate::encode(enc, "caf\u{e9}").unwrap(),
114            vec![99, 97, 102, 142]
115        );
116    }
117
118    #[crate::ctb_test]
119    fn test_windows_1252_encoding() {
120        let enc = CharEncoding::windows_1252();
121        let original = "Hello, World! ñ ü á";
122        let encoded = crate::encode(enc, original).unwrap();
123        let decoded = crate::decode(enc, &encoded).unwrap();
124        assert_eq!(original, decoded);
125    }
126
127    #[crate::ctb_test]
128    fn test_iso88591_encoding() {
129        let enc = CharEncoding::iso_8859_1();
130        let original = "Hello, World! ñ ü á caf\u{e9}";
131        let encoded = crate::encode(enc, original).unwrap();
132        let decoded = crate::decode(enc, &encoded).unwrap();
133        assert_eq!(original, decoded);
134        assert_eq!(
135            crate::encode(enc, "caf\u{e9}").unwrap(),
136            vec![99, 97, 102, 0xe9]
137        );
138    }
139
140    #[crate::ctb_test]
141    fn test_ascii_encoding() {
142        let enc = CharEncoding::ascii();
143        let original = "Hello, World! 123";
144        let encoded = crate::encode(enc, original).unwrap();
145        let decoded = crate::decode(enc, &encoded).unwrap();
146        assert_eq!(original, decoded);
147    }
148}