Coverage Report

Created: 2026-02-14 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zip/src/cp437.rs
Line
Count
Source
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 = ::std::borrow::Cow<'a, str>;
16
17
0
    fn from_cp437(self) -> Self::Target {
18
0
        if self.iter().all(|c| *c < 0x80) {
19
0
            ::std::str::from_utf8(self).unwrap().into()
20
        } else {
21
0
            self.iter().map(|c| to_char(*c)).collect::<String>().into()
22
        }
23
0
    }
24
}
25
26
impl FromCp437 for Vec<u8> {
27
    type Target = String;
28
29
428k
    fn from_cp437(self) -> Self::Target {
30
14.8M
        if self.iter().all(|c| *c < 0x80) {
31
304k
            String::from_utf8(self).unwrap()
32
        } else {
33
123k
            self.into_iter().map(to_char).collect()
34
        }
35
428k
    }
36
}
37
38
4.25M
fn to_char(input: u8) -> char {
39
4.25M
    let output = match input {
40
4.25M
        0x00..=0x7f => input as u32,
41
12.4k
        0x80 => 0x00c7,
42
36.5k
        0x81 => 0x00fc,
43
3.15k
        0x82 => 0x00e9,
44
430
        0x83 => 0x00e2,
45
4.58k
        0x84 => 0x00e4,
46
13.4k
        0x85 => 0x00e0,
47
4.07k
        0x86 => 0x00e5,
48
8.25k
        0x87 => 0x00e7,
49
610
        0x88 => 0x00ea,
50
2.62k
        0x89 => 0x00eb,
51
6.66k
        0x8a => 0x00e8,
52
595
        0x8b => 0x00ef,
53
4.58k
        0x8c => 0x00ee,
54
1.02k
        0x8d => 0x00ec,
55
1.05k
        0x8e => 0x00c4,
56
677
        0x8f => 0x00c5,
57
1.12k
        0x90 => 0x00c9,
58
1.01k
        0x91 => 0x00e6,
59
385
        0x92 => 0x00c6,
60
802
        0x93 => 0x00f4,
61
838
        0x94 => 0x00f6,
62
1.88k
        0x95 => 0x00f2,
63
5.08k
        0x96 => 0x00fb,
64
906
        0x97 => 0x00f9,
65
979
        0x98 => 0x00ff,
66
2.82k
        0x99 => 0x00d6,
67
20.2k
        0x9a => 0x00dc,
68
1.06k
        0x9b => 0x00a2,
69
2.30k
        0x9c => 0x00a3,
70
1.10k
        0x9d => 0x00a5,
71
1.80k
        0x9e => 0x20a7,
72
700
        0x9f => 0x0192,
73
5.47k
        0xa0 => 0x00e1,
74
526
        0xa1 => 0x00ed,
75
14.4k
        0xa2 => 0x00f3,
76
831
        0xa3 => 0x00fa,
77
6.27k
        0xa4 => 0x00f1,
78
986
        0xa5 => 0x00d1,
79
3.47k
        0xa6 => 0x00aa,
80
2.69k
        0xa7 => 0x00ba,
81
774
        0xa8 => 0x00bf,
82
4.67k
        0xa9 => 0x2310,
83
2.95k
        0xaa => 0x00ac,
84
2.12k
        0xab => 0x00bd,
85
10.7k
        0xac => 0x00bc,
86
1.90k
        0xad => 0x00a1,
87
5.84k
        0xae => 0x00ab,
88
5.93k
        0xaf => 0x00bb,
89
3.41k
        0xb0 => 0x2591,
90
3.83k
        0xb1 => 0x2592,
91
3.14k
        0xb2 => 0x2593,
92
1.56k
        0xb3 => 0x2502,
93
9.68k
        0xb4 => 0x2524,
94
3.76k
        0xb5 => 0x2561,
95
1.78k
        0xb6 => 0x2562,
96
1.37k
        0xb7 => 0x2556,
97
637
        0xb8 => 0x2555,
98
87.1k
        0xb9 => 0x2563,
99
929
        0xba => 0x2551,
100
1.45k
        0xbb => 0x2557,
101
44.4k
        0xbc => 0x255d,
102
10.4k
        0xbd => 0x255c,
103
1.08k
        0xbe => 0x255b,
104
10.6k
        0xbf => 0x2510,
105
3.62k
        0xc0 => 0x2514,
106
3.08k
        0xc1 => 0x2534,
107
4.89k
        0xc2 => 0x252c,
108
13.2k
        0xc3 => 0x251c,
109
9.12k
        0xc4 => 0x2500,
110
1.47k
        0xc5 => 0x253c,
111
174k
        0xc6 => 0x255e,
112
903
        0xc7 => 0x255f,
113
1.24k
        0xc8 => 0x255a,
114
2.01k
        0xc9 => 0x2554,
115
868
        0xca => 0x2569,
116
11.8k
        0xcb => 0x2566,
117
1.57k
        0xcc => 0x2560,
118
10.0k
        0xcd => 0x2550,
119
6.47k
        0xce => 0x256c,
120
2.47k
        0xcf => 0x2567,
121
1.79k
        0xd0 => 0x2568,
122
420k
        0xd1 => 0x2564,
123
1.30k
        0xd2 => 0x2565,
124
4.72k
        0xd3 => 0x2559,
125
6.40k
        0xd4 => 0x2558,
126
1.63k
        0xd5 => 0x2552,
127
1.77k
        0xd6 => 0x2553,
128
1.29k
        0xd7 => 0x256b,
129
728
        0xd8 => 0x256a,
130
2.44k
        0xd9 => 0x2518,
131
3.58k
        0xda => 0x250c,
132
2.05k
        0xdb => 0x2588,
133
915
        0xdc => 0x2584,
134
824
        0xdd => 0x258c,
135
763
        0xde => 0x2590,
136
2.34k
        0xdf => 0x2580,
137
4.23k
        0xe0 => 0x03b1,
138
939
        0xe1 => 0x00df,
139
2.70k
        0xe2 => 0x0393,
140
611
        0xe3 => 0x03c0,
141
974
        0xe4 => 0x03a3,
142
1.05k
        0xe5 => 0x03c3,
143
8.67k
        0xe6 => 0x00b5,
144
712
        0xe7 => 0x03c4,
145
2.17k
        0xe8 => 0x03a6,
146
1.14k
        0xe9 => 0x0398,
147
6.43k
        0xea => 0x03a9,
148
657
        0xeb => 0x03b4,
149
654
        0xec => 0x221e,
150
1.20k
        0xed => 0x03c6,
151
756
        0xee => 0x03b5,
152
8.92k
        0xef => 0x2229,
153
982
        0xf0 => 0x2261,
154
4.04k
        0xf1 => 0x00b1,
155
984
        0xf2 => 0x2265,
156
11.4k
        0xf3 => 0x2264,
157
13.3k
        0xf4 => 0x2320,
158
6.14k
        0xf5 => 0x2321,
159
3.06k
        0xf6 => 0x00f7,
160
9.00k
        0xf7 => 0x2248,
161
8.07k
        0xf8 => 0x00b0,
162
9.00k
        0xf9 => 0x2219,
163
10.4k
        0xfa => 0x00b7,
164
8.88k
        0xfb => 0x221a,
165
6.91k
        0xfc => 0x207f,
166
12.1k
        0xfd => 0x00b2,
167
18.8k
        0xfe => 0x25a0,
168
398k
        0xff => 0x00a0,
169
    };
170
4.25M
    ::std::char::from_u32(output).unwrap()
171
4.25M
}
172
173
#[cfg(test)]
174
mod test {
175
    #[test]
176
    fn to_char_valid() {
177
        for i in 0x00_u32..0x100 {
178
            super::to_char(i as u8);
179
        }
180
    }
181
182
    #[test]
183
    fn ascii() {
184
        for i in 0x00..0x80 {
185
            assert_eq!(super::to_char(i), i as char);
186
        }
187
    }
188
189
    #[test]
190
    fn example_slice() {
191
        use super::FromCp437;
192
        let data = b"Cura\x87ao";
193
        assert!(::std::str::from_utf8(data).is_err());
194
        assert_eq!(data.from_cp437(), "Curaçao");
195
    }
196
197
    #[test]
198
    fn example_vec() {
199
        use super::FromCp437;
200
        let data = vec![0xCC, 0xCD, 0xCD, 0xB9];
201
        assert!(String::from_utf8(data.clone()).is_err());
202
        assert_eq!(&data.from_cp437(), "╠══╣");
203
    }
204
}