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     /// Gennerally allocations will be avoided if all data falls into the ASCII range.
from_cp437(self) -> Self::Target10     fn from_cp437(self) -> Self::Target;
11 }
12 
13 impl<'a> FromCp437 for &'a [u8] {
14     type Target = ::std::borrow::Cow<'a, str>;
15 
from_cp437(self) -> Self::Target16     fn from_cp437(self) -> Self::Target {
17         if self.iter().all(|c| *c < 0x80) {
18             ::std::str::from_utf8(self).unwrap().into()
19         } else {
20             self.iter().map(|c| to_char(*c)).collect::<String>().into()
21         }
22     }
23 }
24 
25 impl FromCp437 for Vec<u8> {
26     type Target = String;
27 
from_cp437(self) -> Self::Target28     fn from_cp437(self) -> Self::Target {
29         if self.iter().all(|c| *c < 0x80) {
30             String::from_utf8(self).unwrap()
31         } else {
32             self.into_iter().map(to_char).collect()
33         }
34     }
35 }
36 
to_char(input: u8) -> char37 fn to_char(input: u8) -> char {
38     let output = match input {
39         0x00..=0x7f => input as u32,
40         0x80 => 0x00c7,
41         0x81 => 0x00fc,
42         0x82 => 0x00e9,
43         0x83 => 0x00e2,
44         0x84 => 0x00e4,
45         0x85 => 0x00e0,
46         0x86 => 0x00e5,
47         0x87 => 0x00e7,
48         0x88 => 0x00ea,
49         0x89 => 0x00eb,
50         0x8a => 0x00e8,
51         0x8b => 0x00ef,
52         0x8c => 0x00ee,
53         0x8d => 0x00ec,
54         0x8e => 0x00c4,
55         0x8f => 0x00c5,
56         0x90 => 0x00c9,
57         0x91 => 0x00e6,
58         0x92 => 0x00c6,
59         0x93 => 0x00f4,
60         0x94 => 0x00f6,
61         0x95 => 0x00f2,
62         0x96 => 0x00fb,
63         0x97 => 0x00f9,
64         0x98 => 0x00ff,
65         0x99 => 0x00d6,
66         0x9a => 0x00dc,
67         0x9b => 0x00a2,
68         0x9c => 0x00a3,
69         0x9d => 0x00a5,
70         0x9e => 0x20a7,
71         0x9f => 0x0192,
72         0xa0 => 0x00e1,
73         0xa1 => 0x00ed,
74         0xa2 => 0x00f3,
75         0xa3 => 0x00fa,
76         0xa4 => 0x00f1,
77         0xa5 => 0x00d1,
78         0xa6 => 0x00aa,
79         0xa7 => 0x00ba,
80         0xa8 => 0x00bf,
81         0xa9 => 0x2310,
82         0xaa => 0x00ac,
83         0xab => 0x00bd,
84         0xac => 0x00bc,
85         0xad => 0x00a1,
86         0xae => 0x00ab,
87         0xaf => 0x00bb,
88         0xb0 => 0x2591,
89         0xb1 => 0x2592,
90         0xb2 => 0x2593,
91         0xb3 => 0x2502,
92         0xb4 => 0x2524,
93         0xb5 => 0x2561,
94         0xb6 => 0x2562,
95         0xb7 => 0x2556,
96         0xb8 => 0x2555,
97         0xb9 => 0x2563,
98         0xba => 0x2551,
99         0xbb => 0x2557,
100         0xbc => 0x255d,
101         0xbd => 0x255c,
102         0xbe => 0x255b,
103         0xbf => 0x2510,
104         0xc0 => 0x2514,
105         0xc1 => 0x2534,
106         0xc2 => 0x252c,
107         0xc3 => 0x251c,
108         0xc4 => 0x2500,
109         0xc5 => 0x253c,
110         0xc6 => 0x255e,
111         0xc7 => 0x255f,
112         0xc8 => 0x255a,
113         0xc9 => 0x2554,
114         0xca => 0x2569,
115         0xcb => 0x2566,
116         0xcc => 0x2560,
117         0xcd => 0x2550,
118         0xce => 0x256c,
119         0xcf => 0x2567,
120         0xd0 => 0x2568,
121         0xd1 => 0x2564,
122         0xd2 => 0x2565,
123         0xd3 => 0x2559,
124         0xd4 => 0x2558,
125         0xd5 => 0x2552,
126         0xd6 => 0x2553,
127         0xd7 => 0x256b,
128         0xd8 => 0x256a,
129         0xd9 => 0x2518,
130         0xda => 0x250c,
131         0xdb => 0x2588,
132         0xdc => 0x2584,
133         0xdd => 0x258c,
134         0xde => 0x2590,
135         0xdf => 0x2580,
136         0xe0 => 0x03b1,
137         0xe1 => 0x00df,
138         0xe2 => 0x0393,
139         0xe3 => 0x03c0,
140         0xe4 => 0x03a3,
141         0xe5 => 0x03c3,
142         0xe6 => 0x00b5,
143         0xe7 => 0x03c4,
144         0xe8 => 0x03a6,
145         0xe9 => 0x0398,
146         0xea => 0x03a9,
147         0xeb => 0x03b4,
148         0xec => 0x221e,
149         0xed => 0x03c6,
150         0xee => 0x03b5,
151         0xef => 0x2229,
152         0xf0 => 0x2261,
153         0xf1 => 0x00b1,
154         0xf2 => 0x2265,
155         0xf3 => 0x2264,
156         0xf4 => 0x2320,
157         0xf5 => 0x2321,
158         0xf6 => 0x00f7,
159         0xf7 => 0x2248,
160         0xf8 => 0x00b0,
161         0xf9 => 0x2219,
162         0xfa => 0x00b7,
163         0xfb => 0x221a,
164         0xfc => 0x207f,
165         0xfd => 0x00b2,
166         0xfe => 0x25a0,
167         0xff => 0x00a0,
168     };
169     ::std::char::from_u32(output).unwrap()
170 }
171 
172 #[cfg(test)]
173 mod test {
174     #[test]
to_char_valid()175     fn to_char_valid() {
176         for i in 0x00_u32..0x100 {
177             super::to_char(i as u8);
178         }
179     }
180 
181     #[test]
ascii()182     fn ascii() {
183         for i in 0x00..0x80 {
184             assert_eq!(super::to_char(i), i as char);
185         }
186     }
187 
188     #[test]
example_slice()189     fn example_slice() {
190         use super::FromCp437;
191         let data = b"Cura\x87ao";
192         assert!(::std::str::from_utf8(data).is_err());
193         assert_eq!(data.from_cp437(), "Curaçao");
194     }
195 
196     #[test]
example_vec()197     fn example_vec() {
198         use super::FromCp437;
199         let data = vec![0xCC, 0xCD, 0xCD, 0xB9];
200         assert!(String::from_utf8(data.clone()).is_err());
201         assert_eq!(&data.from_cp437(), "╠══╣");
202     }
203 }
204