forked from untitaker/html5gum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.rs
400 lines (362 loc) · 12.3 KB
/
reader.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
use std::cmp::min;
use std::convert::Infallible;
use std::fs::File;
use std::io::{self, Read};
/// An object that provides characters to the tokenizer.
///
/// See [`crate::Tokenizer::new`] for more information.
pub trait Reader {
/// The error returned by this reader.
type Error: std::error::Error;
/// Return a new byte from the input stream.
///
/// The input stream does **not** have to be preprocessed in any way, it can contain standalone
/// surrogates and have inconsistent newlines.
fn read_byte(&mut self) -> Result<Option<u8>, Self::Error>;
/// Attempt to read an entire string at once, either case-insensitively or not.
///
/// `case_sensitive=false` means that characters of the input stream should be compared while
/// ignoring ASCII-casing.
///
/// It can be assumed that this function is never called with a string that contains `\r` or
/// `\n`.
///
/// If the next characters equal to `s`, this function consumes the respective characters from
/// the input stream and returns `true`. If not, it does nothing and returns `false`.
fn try_read_string(&mut self, s: &[u8], case_sensitive: bool) -> Result<bool, Self::Error>;
/// Read an arbitrary amount of characters up until and including the next character that
/// matches an array entry in `needle`.
///
/// Return either:
///
/// 1. A chunk of consumed characters that does not contain any characters from `needle`. The chunk can be arbitrarily large or small.
/// 2. If the next character is included in `needle`, a string with just that character and nothing else.
///
/// In other words, case 1 means "we didn't find the needle yet, but here's some read data",
/// while case 2 means "we have found the needle".
///
/// The default implementation simply reads one character and calls `read_cb` with that
/// character, ignoring the needle entirely. It is recommended to manually implement
/// `read_until` if there is any sort of in-memory buffer where some sort of efficient string
/// search (see `memchr` or `jetscii` crate) can be run on.
///
/// The return value is usually borrowed from underlying buffers. If that's not possible, a
/// small buffer is provided as `char_buf` to put a single character into.
///
/// # Example
///
/// Here is how [`StringReader`] behaves:
///
/// ```rust
/// use html5gum::{Reader, Readable};
///
/// let mut reader = "hello world".to_reader();
/// let mut eof = false;
/// let mut chunks = Vec::new();
/// while !eof {
/// let mut char_buf = [0; 4];
/// let xs = reader.read_until(&[b' ', b'r'], &mut char_buf).unwrap();
/// if let Some(xs) = xs {
/// chunks.push(std::str::from_utf8(xs).unwrap().to_owned());
/// } else {
/// eof = true;
/// }
/// }
///
/// assert_eq!(chunks, &["hello", " ", "wo", "r", "ld"]);
/// ```
///
/// The inefficient default implementation produces:
///
/// ```text
/// ["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"]
/// ```
fn read_until<'b>(
&'b mut self,
needle: &[u8],
char_buf: &'b mut [u8; 4],
) -> Result<Option<&'b [u8]>, Self::Error> {
let _ = needle;
match self.read_byte()? {
Some(x) => {
char_buf[0] = x;
Ok(Some(&char_buf[..1]))
}
None => Ok(None),
}
}
}
/// An object that can be converted into a [`crate::Reader`].
///
/// For example, any utf8-string can be converted into a `StringReader`, such that
/// `Tokenizer::new("mystring")` and `Tokenizer::new(&String::new("foo"))` work.
pub trait Readable<'a> {
/// The reader type to which this type should be converted.
type Reader: Reader + 'a;
/// Convert self to some sort of reader.
fn to_reader(self) -> Self::Reader;
}
impl<'a, R: 'a + Reader> Readable<'a> for R {
type Reader = Self;
fn to_reader(self) -> Self::Reader {
self
}
}
/// A helper struct to seek forwards and backwards in strings. Used by the tokenizer to read HTML
/// from strings.
///
/// Example:
///
/// ```rust
/// use std::fmt::Write;
/// use html5gum::{Tokenizer, Token};
///
/// let html = "<title >hello world</title>";
/// let mut new_html = String::new();
///
/// for token in Tokenizer::new(html).infallible() {
/// match token {
/// Token::StartTag(tag) => {
/// write!(new_html, "<{}>", String::from_utf8_lossy(&tag.name)).unwrap();
/// }
/// Token::String(hello_world) => {
/// write!(new_html, "{}", String::from_utf8_lossy(&hello_world)).unwrap();
/// }
/// Token::EndTag(tag) => {
/// write!(new_html, "</{}>", String::from_utf8_lossy(&tag.name)).unwrap();
/// }
/// _ => panic!("unexpected input"),
/// }
/// }
///
/// assert_eq!(new_html, "<title>hello world</title>");
/// ```
#[derive(Debug)]
pub struct StringReader<'a> {
input: &'a [u8],
}
impl<'a> StringReader<'a> {
fn new(input: &'a [u8]) -> Self {
StringReader { input }
}
}
impl<'a> Reader for StringReader<'a> {
type Error = Infallible;
fn read_byte(&mut self) -> Result<Option<u8>, Self::Error> {
if self.input.is_empty() {
Ok(None)
} else {
let rv = self.input[0];
self.input = &self.input[1..];
Ok(Some(rv))
}
}
fn read_until<'b>(
&'b mut self,
needle: &[u8],
_: &'b mut [u8; 4],
) -> Result<Option<&'b [u8]>, Self::Error> {
if self.input.is_empty() {
return Ok(None);
}
if let Some(needle_pos) = fast_find(needle, self.input) {
if needle_pos == 0 {
let (rv, new_input) = self.input.split_at(1);
self.input = new_input;
Ok(Some(rv))
} else {
let (rv, new_input) = self.input.split_at(needle_pos);
self.input = new_input;
Ok(Some(rv))
}
} else {
let rv = self.input;
self.input = b"";
Ok(Some(rv))
}
}
fn try_read_string(&mut self, s1: &[u8], case_sensitive: bool) -> Result<bool, Self::Error> {
// we do not need to call validate_char here because `s` hopefully does not contain invalid
// characters
if let Some(s2) = self.input.get(..s1.len()) {
if s1 == s2 || (!case_sensitive && s1.eq_ignore_ascii_case(s2)) {
self.input = &self.input[s1.len()..];
return Ok(true);
}
}
Ok(false)
}
}
impl<'a> Readable<'a> for &'a str {
type Reader = StringReader<'a>;
fn to_reader(self) -> Self::Reader {
StringReader::new(self.as_bytes())
}
}
impl<'a> Readable<'a> for &'a String {
type Reader = StringReader<'a>;
fn to_reader(self) -> Self::Reader {
StringReader::new(self.as_bytes())
}
}
impl<'a> Readable<'a> for &'a Vec<u8> {
type Reader = StringReader<'a>;
fn to_reader(self) -> Self::Reader {
StringReader::new(self.as_slice())
}
}
impl<'a> Readable<'a> for &'a [u8] {
type Reader = StringReader<'a>;
fn to_reader(self) -> Self::Reader {
StringReader::new(self)
}
}
/// A [`IoReader`] can be used to construct a tokenizer from any type that implements
/// `std::io::Read`.
///
/// Because of trait impl conflicts, `IoReader` needs to be explicitly constructed. The exception
/// to that is `File`, which can be directly passed to `Tokenizer::new`.
///
/// When passing `Read`-types into html5gum, no I/O buffering is required. html5gum maintains its
/// own read-buffer (16kb, heap-allocated) such that it can be accessed directly. Put more simply,
/// it's wasteful to wrap your `File` in a `std::io::BufReader` before passing it to html5gum.
///
/// Example:
///
/// ```rust
/// use std::fmt::Write;
/// use html5gum::{Token, IoReader, Tokenizer};
///
/// let tokenizer = Tokenizer::new(IoReader::new("<title>hello world</title>".as_bytes()));
/// // more realistically: Tokenizer::new(File::open("index.html")?)
/// // long-form: Tokenizer::new(IoReader::new(File::open("index.html")?))
///
/// let mut new_html = String::new();
///
/// for token in tokenizer {
/// let token = token.unwrap();
///
/// match token {
/// Token::StartTag(tag) => {
/// write!(new_html, "<{}>", String::from_utf8_lossy(&tag.name)).unwrap();
/// }
/// Token::String(hello_world) => {
/// write!(new_html, "{}", String::from_utf8_lossy(&hello_world)).unwrap();
/// }
/// Token::EndTag(tag) => {
/// write!(new_html, "</{}>", String::from_utf8_lossy(&tag.name)).unwrap();
/// }
/// _ => panic!("unexpected input"),
/// }
///
/// }
///
/// assert_eq!(new_html, "<title>hello world</title>");
/// ```
#[derive(Debug)]
pub struct IoReader<R: Read> {
buf: Box<[u8; BUF_SIZE]>,
buf_offset: usize,
buf_len: usize,
reader: R,
}
const BUF_SIZE: usize = 16 * 1024;
impl<R: Read> IoReader<R> {
/// Construct a new `BufReadReader` from any type that implements `Read`.
pub fn new(reader: R) -> Self {
IoReader {
buf: Box::new([0; BUF_SIZE]),
buf_offset: 0,
buf_len: 0,
reader,
}
}
fn prepare_buf(&mut self, min_len: usize) -> Result<(), io::Error> {
let mut len = self.buf_len - self.buf_offset;
debug_assert!(min_len < self.buf.len());
debug_assert!(len < self.buf.len());
if len < min_len {
let mut raw_buf = &mut self.buf[..];
raw_buf.copy_within(self.buf_offset..self.buf_len, 0);
raw_buf = &mut raw_buf[len..];
while len < min_len {
let n = self.reader.read(raw_buf)?;
if n == 0 {
break;
}
len += n;
raw_buf = &mut raw_buf[n..];
}
self.buf_len = len;
self.buf_offset = 0;
}
Ok(())
}
}
impl<R: Read> Reader for IoReader<R> {
type Error = io::Error;
fn read_byte(&mut self) -> Result<Option<u8>, Self::Error> {
self.prepare_buf(1)?;
if self.buf_offset == self.buf_len {
return Ok(None);
}
let rv = self.buf.get(self.buf_offset).copied();
if rv.is_some() {
self.buf_offset += 1;
}
Ok(rv)
}
fn try_read_string(&mut self, s1: &[u8], case_sensitive: bool) -> Result<bool, Self::Error> {
debug_assert!(!s1.contains(&b'\r'));
debug_assert!(!s1.contains(&b'\n'));
self.prepare_buf(s1.len())?;
let s2 = &self.buf[self.buf_offset..min(self.buf_offset + s1.len(), self.buf_len)];
if s1 == s2 || (!case_sensitive && s1.eq_ignore_ascii_case(s2)) {
self.buf_offset += s1.len();
Ok(true)
} else {
Ok(false)
}
}
fn read_until<'b>(
&'b mut self,
needle: &[u8],
_: &'b mut [u8; 4],
) -> Result<Option<&'b [u8]>, Self::Error> {
self.prepare_buf(4)?;
let buf = &self.buf[self.buf_offset..self.buf_len];
if buf.is_empty() {
Ok(None)
} else if let Some(needle_pos) = fast_find(needle, buf) {
if needle_pos == 0 {
self.buf_offset += 1;
Ok(Some(&buf[..1]))
} else {
self.buf_offset += needle_pos;
Ok(Some(&buf[..needle_pos]))
}
} else {
self.buf_offset += buf.len();
Ok(Some(buf))
}
}
}
impl<'a> Readable<'a> for File {
type Reader = IoReader<File>;
fn to_reader(self) -> Self::Reader {
IoReader::new(self)
}
}
#[inline]
fn fast_find(needle: &[u8], haystack: &[u8]) -> Option<usize> {
#[cfg(feature = "jetscii")]
{
debug_assert!(needle.len() <= 16);
let mut needle_arr = [0; 16];
needle_arr[..needle.len()].copy_from_slice(needle);
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
jetscii::Bytes::new(needle_arr, needle.len() as i32, |b| needle.contains(&b)).find(haystack)
}
#[cfg(not(feature = "jetscii"))]
haystack.iter().position(|b| needle.contains(b))
}