-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdecoder.rs
161 lines (136 loc) · 4.73 KB
/
decoder.rs
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
//! Decoder utilities.
use std::str::from_utf8;
use crate::character::{Category, RichChar, Type, CHARACTER_FILL, CHARACTER_UNKNOWN};
struct LossyASCIIDecoder<'a> {
bytes: &'a [u8],
cursor: usize,
}
impl<'a> From<&'a [u8]> for LossyASCIIDecoder<'a> {
fn from(bytes: &'a [u8]) -> Self {
Self { bytes, cursor: 0 }
}
}
impl<'a> Iterator for LossyASCIIDecoder<'a> {
type Item = (char, Type);
fn next(&mut self) -> Option<Self::Item> {
if self.cursor < self.bytes.len() {
let byte = self.bytes[self.cursor];
self.cursor += 1;
if byte.is_ascii() {
Some((byte as char, Type::Ascii))
} else {
Some((CHARACTER_UNKNOWN, Type::Unknown))
}
} else {
None
}
}
}
struct LossyUTF8Decoder<'a> {
bytes: &'a [u8],
cursor: usize,
}
impl<'a> From<&'a [u8]> for LossyUTF8Decoder<'a> {
fn from(bytes: &'a [u8]) -> Self {
LossyUTF8Decoder { bytes, cursor: 0 }
}
}
impl<'a> Iterator for LossyUTF8Decoder<'a> {
type Item = (char, Type);
fn next(&mut self) -> Option<Self::Item> {
if self.cursor < self.bytes.len() {
let typ = match self.bytes[self.cursor] {
0x00..=0x7F => Type::Ascii,
0xC0..=0xDF => Type::Unicode(2),
0xE0..=0xEF => Type::Unicode(3),
0xF0..=0xF7 => Type::Unicode(4),
_ => {
self.cursor += 1;
return Some((CHARACTER_UNKNOWN, Type::Unknown));
}
};
let new_cursor = self.bytes.len().min(self.cursor + typ.size());
let chunk = &self.bytes[self.cursor..new_cursor];
if let Ok(mut chars) = from_utf8(chunk).map(str::chars) {
let char = chars.next().expect("the string must contain exactly one character");
debug_assert!(
chars.next().is_none(),
"the string must contain exactly one character"
);
self.cursor += typ.size();
Some((char, typ))
} else {
self.cursor += 1;
Some((CHARACTER_UNKNOWN, Type::Unknown))
}
} else {
None
}
}
}
#[derive(Copy, Clone, Debug)]
pub enum Encoding {
Ascii,
Utf8,
}
pub(crate) struct ByteAlignedDecoder<D: Iterator<Item = (char, Type)>> {
decoder: D,
to_fill: usize,
}
type BoxedDecoder<'a> = Box<dyn Iterator<Item = (char, Type)> + 'a>;
impl<'a> ByteAlignedDecoder<BoxedDecoder<'a>> {
pub(crate) fn new(bytes: &'a [u8], encoding: Encoding) -> Self {
match encoding {
Encoding::Ascii => Box::new(LossyASCIIDecoder::from(bytes)) as BoxedDecoder,
Encoding::Utf8 => Box::new(LossyUTF8Decoder::from(bytes)) as BoxedDecoder,
}
.into()
}
}
impl<D: Iterator<Item = (char, Type)>> From<D> for ByteAlignedDecoder<D> {
fn from(decoder: D) -> Self {
Self { decoder, to_fill: 0 }
}
}
impl<D: Iterator<Item = (char, Type)>> Iterator for ByteAlignedDecoder<D> {
type Item = RichChar;
fn next(&mut self) -> Option<Self::Item> {
if self.to_fill == 0 {
let (character, typ) = self.decoder.next()?;
let category = match typ {
Type::Unknown => Category::Unknown,
_ => Category::from(&character),
};
self.to_fill = typ.size() - 1;
Some(RichChar::new(character, category))
} else {
self.to_fill -= 1;
Some(RichChar::new(CHARACTER_FILL, Category::Fill))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const TEST_BYTES: &[u8] = b"text, controls \n \r\n, space \t, unicode \xC3\xA4h \xC3\xA0 la \xF0\x9F\x92\xA9, null \x00, invalid \xC0\xF8\xEE";
#[test]
fn test_decoder_ascii() {
let decoder = ByteAlignedDecoder::new(TEST_BYTES, Encoding::Ascii);
let characters: Vec<_> = decoder.collect();
assert_eq!(TEST_BYTES.len(), characters.len());
assert_eq!(
characters.iter().map(RichChar::escape).map(char::from).collect::<String>(),
"text, controls _ __, space _, unicode ��h �� la ����, null 0, invalid ���"
);
}
#[test]
fn test_decoder_utf8() {
let decoder = ByteAlignedDecoder::new(TEST_BYTES, Encoding::Utf8);
let characters: Vec<_> = decoder.collect();
assert_eq!(TEST_BYTES.len(), characters.len());
assert_eq!(
characters.iter().map(RichChar::escape).map(char::from).collect::<String>(),
"text, controls _ __, space _, unicode ä•h à• la 💩•••, null 0, invalid ���"
);
}
}