Skip to content

Commit c590fdf

Browse files
authored
Merge pull request #438 from dralley/split-readers
Add example for buffered access when reading from a file
2 parents d8ae1c3 + 33b6e6d commit c590fdf

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

examples/read_buffered.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// This example demonstrates how a reader (for example when reading from a file)
2+
// can be buffered. In that case, data read from the file is written to a supplied
3+
// buffer and returned XML events borrow from that buffer.
4+
// That way, allocations can be kept to a minimum.
5+
6+
fn main() -> Result<(), quick_xml::Error> {
7+
use quick_xml::events::Event;
8+
use quick_xml::Reader;
9+
10+
let mut reader = Reader::from_file("tests/documents/document.xml")?;
11+
reader.trim_text(true);
12+
13+
let mut buf = Vec::new();
14+
15+
let mut count = 0;
16+
17+
loop {
18+
match reader.read_event_into(&mut buf) {
19+
Ok(Event::Start(ref e)) => {
20+
let name = e.name();
21+
let name = reader.decoder().decode(name.as_ref())?;
22+
println!("read start event {:?}", name.as_ref());
23+
count += 1;
24+
}
25+
Ok(Event::Eof) => break, // exits the loop when reaching end of file
26+
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
27+
_ => (), // There are several other `Event`s we do not consider here
28+
}
29+
}
30+
31+
println!("read {} start events in total", count);
32+
33+
Ok(())
34+
}

src/reader/buffered_reader.rs

+6
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ impl<'b, R: BufRead> XmlSource<'b, &'b mut Vec<u8>> for R {
238238
buf: &'b mut Vec<u8>,
239239
position: &mut usize,
240240
) -> Result<Option<&'b [u8]>> {
241+
// search byte must be within the ascii range
242+
debug_assert!(byte.is_ascii());
243+
241244
let mut read = 0;
242245
let mut done = false;
243246
let start = buf.len();
@@ -397,6 +400,9 @@ impl<'b, R: BufRead> XmlSource<'b, &'b mut Vec<u8>> for R {
397400
/// Consume and discard one character if it matches the given byte. Return
398401
/// true if it matched.
399402
fn skip_one(&mut self, byte: u8, position: &mut usize) -> Result<bool> {
403+
// search byte must be within the ascii range
404+
debug_assert!(byte.is_ascii());
405+
400406
match self.peek_one()? {
401407
Some(b) if b == byte => {
402408
*position += 1;

src/reader/slice_reader.rs

+4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] {
147147
_buf: (),
148148
position: &mut usize,
149149
) -> Result<Option<&'a [u8]>> {
150+
// search byte must be within the ascii range
151+
debug_assert!(byte.is_ascii());
150152
if self.is_empty() {
151153
return Ok(None);
152154
}
@@ -217,6 +219,8 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] {
217219
}
218220

219221
fn skip_one(&mut self, byte: u8, position: &mut usize) -> Result<bool> {
222+
// search byte must be within the ascii range
223+
debug_assert!(byte.is_ascii());
220224
if self.first() == Some(&byte) {
221225
*self = &self[1..];
222226
*position += 1;

0 commit comments

Comments
 (0)