Debian Codegraph @

debian/rust-vendor/zip/src/cp437.rs

raw
1 //! Convert a string in IBM codepage 437 to UTF-8
2
3 /// Trait to convert IBM codepage 437 to the target type
4 pub trait FromCp437 {
5 /// Target type
6 type Target;
7
8 /// Function that does the conversion from cp437.
9 /// Generally allocations will be avoided if all data falls into the ASCII range.
10 #[allow(clippy::wrong_self_convention)]
11 fn from_cp437(self) -> Self::Target;
12 }
13
14 impl<'a> FromCp437 for &'a [u8] {
15 type Target = Result<std::borrow::Cow<'a, str>, std::io::Error>;
16
17 fn from_cp437(self) -> Self::Target {
18 let target = if self.iter().any(|c| *c >= 0x80) {
19 let s = self.iter().copied().map(to_char).collect::<String>();
20 std::borrow::Cow::Owned(s)
21 } else {
22 let s = std::str::from_utf8(self).map_err(|e| {
23 std::io::Error::new(
24 std::io::ErrorKind::InvalidData,
25 format!("Cannot translate path from cp437: {e}"),
26 )
27 })?;
28 std::borrow::Cow::Borrowed(s)
29 };
30 Ok(target)
31 }
32 }
33
34 #[allow(clippy::too_many_lines)]
35 fn to_char(input: u8) -> char {
36 match input {
37 0x00..=0x7f => input as char,
38 0x80 => '\u{00c7}',
39 0x81 => '\u{00fc}',
40 0x82 => '\u{00e9}',
41 0x83 => '\u{00e2}',
42 0x84 => '\u{00e4}',
43 0x85 => '\u{00e0}',
44 0x86 => '\u{00e5}',
45 0x87 => '\u{00e7}',
46 0x88 => '\u{00ea}',
47 0x89 => '\u{00eb}',
48 0x8a => '\u{00e8}',
49 0x8b => '\u{00ef}',
50 0x8c => '\u{00ee}',
51 0x8d => '\u{00ec}',
52 0x8e => '\u{00c4}',
53 0x8f => '\u{00c5}',
54 0x90 => '\u{00c9}',
55 0x91 => '\u{00e6}',
56 0x92 => '\u{00c6}',
57 0x93 => '\u{00f4}',
58 0x94 => '\u{00f6}',
59 0x95 => '\u{00f2}',
60 0x96 => '\u{00fb}',
61 0x97 => '\u{00f9}',
62 0x98 => '\u{00ff}',
63 0x99 => '\u{00d6}',
64 0x9a => '\u{00dc}',
65 0x9b => '\u{00a2}',
66 0x9c => '\u{00a3}',
67 0x9d => '\u{00a5}',
68 0x9e => '\u{20a7}',
69 0x9f => '\u{0192}',
70 0xa0 => '\u{00e1}',
71 0xa1 => '\u{00ed}',
72 0xa2 => '\u{00f3}',
73 0xa3 => '\u{00fa}',
74 0xa4 => '\u{00f1}',
75 0xa5 => '\u{00d1}',
76 0xa6 => '\u{00aa}',
77 0xa7 => '\u{00ba}',
78 0xa8 => '\u{00bf}',
79 0xa9 => '\u{2310}',
80 0xaa => '\u{00ac}',
81 0xab => '\u{00bd}',
82 0xac => '\u{00bc}',
83 0xad => '\u{00a1}',
84 0xae => '\u{00ab}',
85 0xaf => '\u{00bb}',
86 0xb0 => '\u{2591}',
87 0xb1 => '\u{2592}',
88 0xb2 => '\u{2593}',
89 0xb3 => '\u{2502}',
90 0xb4 => '\u{2524}',
91 0xb5 => '\u{2561}',
92 0xb6 => '\u{2562}',
93 0xb7 => '\u{2556}',
94 0xb8 => '\u{2555}',
95 0xb9 => '\u{2563}',
96 0xba => '\u{2551}',
97 0xbb => '\u{2557}',
98 0xbc => '\u{255d}',
99 0xbd => '\u{255c}',
100 0xbe => '\u{255b}',
101 0xbf => '\u{2510}',
102 0xc0 => '\u{2514}',
103 0xc1 => '\u{2534}',
104 0xc2 => '\u{252c}',
105 0xc3 => '\u{251c}',
106 0xc4 => '\u{2500}',
107 0xc5 => '\u{253c}',
108 0xc6 => '\u{255e}',
109 0xc7 => '\u{255f}',
110 0xc8 => '\u{255a}',
111 0xc9 => '\u{2554}',
112 0xca => '\u{2569}',
113 0xcb => '\u{2566}',
114 0xcc => '\u{2560}',
115 0xcd => '\u{2550}',
116 0xce => '\u{256c}',
117 0xcf => '\u{2567}',
118 0xd0 => '\u{2568}',
119 0xd1 => '\u{2564}',
120 0xd2 => '\u{2565}',
121 0xd3 => '\u{2559}',
122 0xd4 => '\u{2558}',
123 0xd5 => '\u{2552}',
124 0xd6 => '\u{2553}',
125 0xd7 => '\u{256b}',
126 0xd8 => '\u{256a}',
127 0xd9 => '\u{2518}',
128 0xda => '\u{250c}',
129 0xdb => '\u{2588}',
130 0xdc => '\u{2584}',
131 0xdd => '\u{258c}',
132 0xde => '\u{2590}',
133 0xdf => '\u{2580}',
134 0xe0 => '\u{03b1}',
135 0xe1 => '\u{00df}',
136 0xe2 => '\u{0393}',
137 0xe3 => '\u{03c0}',
138 0xe4 => '\u{03a3}',
139 0xe5 => '\u{03c3}',
140 0xe6 => '\u{00b5}',
141 0xe7 => '\u{03c4}',
142 0xe8 => '\u{03a6}',
143 0xe9 => '\u{0398}',
144 0xea => '\u{03a9}',
145 0xeb => '\u{03b4}',
146 0xec => '\u{221e}',
147 0xed => '\u{03c6}',
148 0xee => '\u{03b5}',
149 0xef => '\u{2229}',
150 0xf0 => '\u{2261}',
151 0xf1 => '\u{00b1}',
152 0xf2 => '\u{2265}',
153 0xf3 => '\u{2264}',
154 0xf4 => '\u{2320}',
155 0xf5 => '\u{2321}',
156 0xf6 => '\u{00f7}',
157 0xf7 => '\u{2248}',
158 0xf8 => '\u{00b0}',
159 0xf9 => '\u{2219}',
160 0xfa => '\u{00b7}',
161 0xfb => '\u{221a}',
162 0xfc => '\u{207f}',
163 0xfd => '\u{00b2}',
164 0xfe => '\u{25a0}',
165 0xff => '\u{00a0}',
166 }
167 }
168
169 #[cfg(test)]
170 mod tests {
171 #[test]
172 fn to_char_valid() {
173 for i in u8::MIN..=u8::MAX {
174 super::to_char(i);
175 }
176 }
177
178 #[test]
179 fn ascii() {
180 for i in 0x00..0x80 {
181 assert_eq!(super::to_char(i), i as char);
182 }
183 }
184
185 #[test]
186 #[allow(invalid_from_utf8)]
187 fn example_slice() {
188 use super::FromCp437;
189 let data = b"Cura\x87ao";
190 assert!(std::str::from_utf8(data).is_err());
191 let converted = &(*data.from_cp437().unwrap());
192 assert_eq!(converted, "Curaçao");
193 }
194
195 #[test]
196 fn example_vec() {
197 use super::FromCp437;
198 let data = vec![0xCC, 0xCD, 0xCD, 0xB9];
199 assert!(String::from_utf8(data.clone()).is_err());
200 let converted = &(*data.from_cp437().unwrap());
201 assert_eq!(converted, "╠══╣");
202 }
203 }