1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use ansi_term::Color::{Green, Red};
use codepage::to_encoding;
use lazy_static::lazy_static;
use locale_name_code_page::get_codepage;
use oem_cp::code_table::DECODING_TABLE_CP_MAP;
use oem_cp::code_table_type::TableType;
use regex::Regex;
use hfs_nfd::compose_from_hfs_nfd;
use locale_config::Locale;
pub trait IDecoder {
fn to_string_lossless(&self, input: &Vec<u8>) -> Option<String>;
fn to_string_lossy(&self, input: &Vec<u8>) -> String;
fn encoding_name(&self) -> &str;
fn color(&self) -> ansi_term::Color;
}
struct UTF8NFCDecoder {}
struct ASCIIDecoder {}
struct OEMCPDecoder {
decoder: &'static TableType,
encoding_str: String,
}
struct LegacyEncodingDecoder {
decoder: &'static encoding_rs::Encoding,
}
impl IDecoder for UTF8NFCDecoder {
fn to_string_lossless(&self, input: &Vec<u8>) -> Option<String> {
return String::from_utf8(input.to_vec())
.map(|s| compose_from_hfs_nfd(s))
.ok();
}
fn to_string_lossy(&self, input: &Vec<u8>) -> String {
return compose_from_hfs_nfd(String::from_utf8_lossy(&input));
}
fn encoding_name(&self) -> &str {
return "UTF-8";
}
fn color(&self) -> ansi_term::Color {
return Green;
}
}
impl IDecoder for ASCIIDecoder {
fn to_string_lossless(&self, input: &Vec<u8>) -> Option<String> {
if input.iter().any(|c| !c.is_ascii()) {
return None;
}
return String::from_utf8(input.to_vec()).ok();
}
fn to_string_lossy(&self, input: &Vec<u8>) -> String {
return input
.iter()
.map(|c| if c.is_ascii() { *c as char } else { '\u{FFFD}' })
.collect();
}
fn encoding_name(&self) -> &str {
return "ASCII";
}
fn color(&self) -> ansi_term::Color {
return Green;
}
}
impl OEMCPDecoder {
fn from_codepage(codepage: u16) -> Option<Self> {
return Some(Self {
decoder: DECODING_TABLE_CP_MAP.get(&codepage)?,
encoding_str: format!("CP{}", codepage),
});
}
fn fallback() -> Self {
return Self::from_codepage(437).unwrap();
}
}
impl IDecoder for OEMCPDecoder {
fn to_string_lossless(&self, input: &Vec<u8>) -> Option<String> {
return self.decoder.decode_string_checked(input);
}
fn to_string_lossy(&self, input: &Vec<u8>) -> String {
return self.decoder.decode_string_lossy(input);
}
fn encoding_name(&self) -> &str {
return &self.encoding_str;
}
fn color(&self) -> ansi_term::Color {
return Red;
}
}
impl IDecoder for LegacyEncodingDecoder {
fn to_string_lossless(&self, input: &Vec<u8>) -> Option<String> {
let (result, _, met_invalid_char) = self.decoder.decode(&input);
if met_invalid_char {
return None;
}
return Some(result.into_owned());
}
fn to_string_lossy(&self, input: &Vec<u8>) -> String {
return self.decoder.decode(&input).0.into_owned();
}
fn encoding_name(&self) -> &str {
return self.decoder.name();
}
fn color(&self) -> ansi_term::Color {
return Red;
}
}
impl dyn IDecoder {
pub fn utf8() -> Box<dyn IDecoder> {
return Box::new(UTF8NFCDecoder {});
}
pub fn ascii() -> Box<dyn IDecoder> {
return Box::new(ASCIIDecoder {});
}
pub fn native_oem_encoding() -> Box<dyn IDecoder> {
let current_locale_name_full = Locale::user_default().to_string();
if let Some(codepage) = get_codepage(current_locale_name_full) {
if let Some(encoding) = to_encoding(codepage.oem) {
return Box::new(LegacyEncodingDecoder { decoder: encoding });
}
if let Some(decoder) = OEMCPDecoder::from_codepage(codepage.oem) {
return Box::new(decoder);
}
}
return Box::new(OEMCPDecoder::fallback());
}
pub fn from_encoding_name(name: &str) -> Option<Box<dyn IDecoder>> {
lazy_static! {
static ref OEM_CP_REGEX: Regex = Regex::new(r"(?i)(?:CP|OEM ?|IBM)(\d+)").unwrap();
static ref CP437_REGEX: Regex =
Regex::new("(?i)(OEM[-_]US|PC-8|DOS[-_ ]?Latin[-_ ]?US)").unwrap();
}
if let Some(decoder) = encoding_rs::Encoding::for_label(name.as_bytes()) {
return Some(Box::new(LegacyEncodingDecoder { decoder: decoder }));
}
if let Some(decoder) = OEM_CP_REGEX
.captures(name)
.and_then(|captures| captures.get(1))
.and_then(|match_| -> Option<u16> { match_.as_str().parse().ok() })
.and_then(|codepage| OEMCPDecoder::from_codepage(codepage))
{
return Some(Box::new(decoder));
}
if CP437_REGEX.is_match(name) {
return Some(Box::new(OEMCPDecoder::fallback()));
}
return None;
}
}
pub fn decide_decoeder(
decoders: &Vec<&Box<dyn IDecoder>>,
strings: &Vec<&Vec<u8>>,
) -> Option<usize> {
for i in 0..decoders.len() {
let decoder = &decoders[i];
if strings
.iter()
.all(|subject| decoder.to_string_lossless(subject) != None)
{
return Some(i);
}
}
return None;
}