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
use std::str;
use std::io::Write;
use {
Stream,
StrSpan,
};
const BUF_END: usize = 4;
#[derive(Clone, Copy, PartialEq, Debug)]
#[allow(missing_docs)]
pub enum XmlSpace {
Default,
Preserve,
}
pub struct TextUnescape<'a> {
stream: Stream<'a>,
buf: [u8; BUF_END],
buf_idx: usize,
preserve_spaces: bool,
prev: u8,
}
impl<'a> From<&'a str> for TextUnescape<'a> {
fn from(text: &'a str) -> Self {
Self::from(StrSpan::from(text))
}
}
impl<'a> From<StrSpan<'a>> for TextUnescape<'a> {
fn from(span: StrSpan<'a>) -> Self {
TextUnescape {
stream: Stream::from(span),
buf: [0xFF; BUF_END],
buf_idx: BUF_END,
preserve_spaces: false,
prev: 0,
}
}
}
impl<'a> TextUnescape<'a> {
pub fn unescape(text: &str, space: XmlSpace) -> String {
let mut v = Vec::new();
let mut t = TextUnescape::from(text);
t.set_xml_space(space);
for c in t {
v.push(c);
}
str::from_utf8(&v).unwrap().to_owned()
}
pub fn set_xml_space(&mut self, kind: XmlSpace) {
self.preserve_spaces = kind == XmlSpace::Preserve;
}
}
impl<'a> Iterator for TextUnescape<'a> {
type Item = u8;
fn next(&mut self) -> Option<Self::Item> {
if self.buf_idx != BUF_END {
let c = self.buf[self.buf_idx];
if c != 0xFF {
self.buf_idx += 1;
return Some(c);
} else {
self.buf_idx = BUF_END;
}
}
if self.stream.at_end() {
return None;
}
let mut c = self.stream.curr_byte().unwrap();
if c == b'&' {
if let Some(ch) = self.stream.try_consume_char_reference() {
self.buf = [0xFF; 4];
write!(&mut self.buf[..], "{}", ch).unwrap();
c = self.buf[0];
self.buf_idx = 1;
} else {
self.stream.advance(1);
}
} else {
self.stream.advance(1);
}
c = match c {
b'\n' | b'\t' => b' ',
_ => c,
};
if c == b'\r' {
return self.next();
}
if !self.preserve_spaces && c == b' ' && c == self.prev {
return self.next();
}
self.prev = c;
Some(c)
}
}