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
use self::super::Cp437Dialect;
use std::iter::FromIterator;
use std::borrow::Cow;
use std::str;
pub trait FromCp437<T: Sized> {
fn from_cp437(cp437: T, dialect: &Cp437Dialect) -> Self;
}
macro_rules! from_cp437_slice_impl {
($($l:expr)*) => ($(
impl FromCp437<[u8; $l]> for String {
fn from_cp437(cp437: [u8; $l], dialect: &Cp437Dialect) -> Self {
from_cp437_slice_impl(&cp437, dialect)
}
}
)*)
}
impl FromCp437<Vec<u8>> for String {
fn from_cp437(cp437: Vec<u8>, dialect: &Cp437Dialect) -> Self {
if cp437.iter().all(|&b| dialect.overlap_cp437(b)) {
String::from_utf8(cp437).unwrap()
} else {
String::from_iter(cp437.into_iter().map(|b| dialect.decode(b)))
}
}
}
impl FromCp437<[u8; 0]> for String {
fn from_cp437(_: [u8; 0], _: &Cp437Dialect) -> Self {
String::new()
}
}
from_cp437_slice_impl!( 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);
fn from_cp437_slice_impl(cp437: &[u8], dialect: &Cp437Dialect) -> String {
if cp437.iter().all(|&b| dialect.overlap_cp437(b)) {
String::from_utf8(cp437.to_vec()).unwrap()
} else {
String::from_iter(cp437.iter().map(|&b| dialect.decode(b)))
}
}
pub trait BorrowFromCp437<'c, T: ?Sized> {
fn borrow_from_cp437(cp437: &'c T, dialect: &Cp437Dialect) -> Self;
}
impl<'c, T: AsRef<[u8]> + ?Sized> BorrowFromCp437<'c, T> for Cow<'c, str> {
fn borrow_from_cp437(cp437: &'c T, dialect: &Cp437Dialect) -> Self {
borrow_from_cp437_cow_slice_impl(cp437.as_ref(), dialect)
}
}
impl<'c, T: AsRef<[u8]> + ?Sized> BorrowFromCp437<'c, T> for String {
fn borrow_from_cp437(cp437: &'c T, dialect: &Cp437Dialect) -> Self {
borrow_from_cp437_string_slice_impl(cp437.as_ref(), dialect)
}
}
fn borrow_from_cp437_cow_slice_impl<'c>(cp437: &'c [u8], dialect: &Cp437Dialect) -> Cow<'c, str> {
if cp437.iter().all(|&b| dialect.overlap_cp437(b)) {
Cow::Borrowed(str::from_utf8(&cp437[..]).unwrap())
} else {
Cow::Owned(String::from_iter(cp437.iter().map(|&b| dialect.decode(b))))
}
}
fn borrow_from_cp437_string_slice_impl(cp437: &[u8], dialect: &Cp437Dialect) -> String {
if cp437.iter().all(|&b| dialect.overlap_cp437(b)) {
String::from_utf8(cp437.to_vec()).unwrap()
} else {
String::from_iter(cp437.iter().map(|&b| dialect.decode(b)))
}
}