Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
dralley committed Aug 14, 2022
1 parent 380826e commit 8b39067
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 85 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ license = "MIT"
[dependencies]
document-features = { version = "0.2", optional = true }
encoding_rs = { version = "0.8", optional = true }
encoding_rs_io = { version = "0.1", optional = true }
serde = { version = "1.0", optional = true }
memchr = "2.5"

Expand Down Expand Up @@ -47,7 +48,7 @@ default = []
## crate, that satisfied the restriction above.
##
## [standard compliant]: https://www.w3.org/TR/xml11/#charencoding
encoding = ["encoding_rs"]
encoding = ["encoding_rs", "encoding_rs_io"]

## This feature enables support for deserializing lists where tags are overlapped
## with tags that do not correspond to the list.
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ Note that despite not focusing on performance (there are several unnecessary cop
Benchmarking is hard and the results depend on your input file and your machine.

Here on my particular file, quick-xml is around **50 times faster** than [xml-rs](https://crates.io/crates/xml-rs) crate.
_(measurements was done while this crate named quick-xml)_

```
// quick-xml benches
Expand Down
7 changes: 2 additions & 5 deletions examples/read_texts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ fn main() {
reader.trim_text(true);

let mut txt = Vec::new();
let mut buf = Vec::new();

loop {
match reader.read_event_into(&mut buf) {
match reader.read_event() {
Ok(Event::Start(ref e)) if e.name().as_ref() == b"tag2" => {
txt.push(
reader
.read_text_into(QName(b"tag2"), &mut Vec::new())
.read_text(QName(b"tag2"))
.expect("Cannot decode text value"),
);
println!("{:?}", txt);
Expand All @@ -26,6 +24,5 @@ fn main() {
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
_ => (), // There are several other `Event`s we do not consider here
}
buf.clear();
}
}
4 changes: 2 additions & 2 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ mod var;

pub use crate::errors::serialize::DeError;
use crate::{
encoding::Decoder,
encoding::{Decoder, DecodingReader},
errors::Error,
events::{BytesCData, BytesEnd, BytesStart, BytesText, Event},
name::QName,
Expand Down Expand Up @@ -697,7 +697,7 @@ impl<'de> Deserializer<'de, SliceReader<'de>> {
}
}

impl<'de, R> Deserializer<'de, IoReader<R>>
impl<'de, R> Deserializer<'de, IoReader<DecodingReader<R>>>
where
R: BufRead,
{
Expand Down
51 changes: 51 additions & 0 deletions src/encoding.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,65 @@
//! A module for wrappers that encode / decode data.

use std::borrow::Cow;
use std::io;

#[cfg(feature = "encoding")]
use encoding_rs::{Encoding, UTF_16BE, UTF_16LE, UTF_8};
#[cfg(feature = "encoding")]
use encoding_rs_io::{DecodeReaderBytes, DecodeReaderBytesBuilder};

#[cfg(feature = "encoding")]
use crate::Error;
use crate::Result;

/// A struct for transparently decoding / validating bytes to known-valid UTF-8.
#[derive(Debug)]
pub struct DecodingReader<R> {
#[cfg(feature = "encoding")]
reader: io::BufReader<DecodeReaderBytes<R, Vec<u8>>>,
#[cfg(not(feature = "encoding"))]
reader: io::BufReader<R>,
}

impl<R: io::Read> DecodingReader<R> {
/// Build a new DecodingReader which decodes a stream of bytes into valid UTF-8.
#[cfg(feature = "encoding")]
pub fn new(reader: R) -> Self {
let decoder = DecodeReaderBytesBuilder::new()
.encoding(Some(UTF_8))
.bom_override(true)
.build(reader);

Self {
reader: io::BufReader::new(decoder),
}
}

/// Build a new DecodingReader which only validates UTF-8.
#[cfg(not(feature = "encoding"))]
pub fn new(reader: R) -> Self {
Self {
reader: io::BufReader::new(reader),
}
}
}

impl<R: io::Read> io::Read for DecodingReader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.reader.read(buf)
}
}

impl<R: io::Read> io::BufRead for DecodingReader<R> {
fn fill_buf(&mut self) -> io::Result<&[u8]> {
self.reader.fill_buf()
}

fn consume(&mut self, amt: usize) {
self.reader.consume(amt)
}
}

/// Decoder of byte slices into strings.
///
/// If feature `encoding` is enabled, this encoding taken from the `"encoding"`
Expand Down
13 changes: 7 additions & 6 deletions src/reader/buffered_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
//! underlying byte stream.

use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::io;
use std::path::Path;

use memchr;

use crate::encoding::DecodingReader;
use crate::errors::{Error, Result};
use crate::events::Event;
use crate::name::QName;
use crate::reader::{is_whitespace, BangType, ReadElementState, Reader, XmlSource};

/// This is an implementation of [`Reader`] for reading from a [`BufRead`] as
/// underlying byte stream.
impl<R: BufRead> Reader<R> {
impl<R: io::BufRead> Reader<R> {
/// Reads the next `Event`.
///
/// This is the main entry point for reading XML `Event`s.
Expand Down Expand Up @@ -217,20 +218,19 @@ impl<R: BufRead> Reader<R> {
}
}

impl Reader<BufReader<File>> {
impl Reader<DecodingReader<File>> {
/// Creates an XML reader from a file path.
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
let file = File::open(path).map_err(Error::Io)?;
let reader = BufReader::new(file);
Ok(Self::from_reader(reader))
Ok(Self::from_reader(file))
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////

/// Implementation of `XmlSource` for any `BufRead` reader using a user-given
/// `Vec<u8>` as buffer that will be borrowed by events.
impl<'b, R: BufRead> XmlSource<'b, &'b mut Vec<u8>> for R {
impl<'b, R: io::BufRead> XmlSource<'b, &'b mut Vec<u8>> for R {
#[inline]
fn read_bytes_until(
&mut self,
Expand Down Expand Up @@ -443,6 +443,7 @@ mod test {

/// Checks that encoding is detected by BOM and changed after XML declaration
#[test]
#[ignore = "dalley fixme"]
fn bom_detected() {
let mut reader =
Reader::from_reader(b"\xFF\xFE<?xml encoding='windows-1251'?>".as_ref());
Expand Down
66 changes: 8 additions & 58 deletions src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#[cfg(feature = "encoding")]
use encoding_rs::Encoding;

use crate::encoding::Decoder;
use std::io::Read;

use crate::encoding::{Decoder, DecodingReader};
use crate::errors::{Error, Result};
use crate::events::Event;
use crate::reader::parser::Parser;
Expand Down Expand Up @@ -289,73 +291,19 @@ pub struct Reader<R> {
}

/// Builder methods
impl<R> Reader<R> {
impl<R: Read> Reader<DecodingReader<R>> {
/// Creates a `Reader` that reads from a given reader.
pub fn from_reader(reader: R) -> Self {
Self {
reader,
reader: DecodingReader::new(reader),
parser: Parser::default(),
}
}

configure_methods!();
}

/// Getters
impl<R> Reader<R> {
/// Consumes `Reader` returning the underlying reader
///
/// Can be used to compute line and column of a parsing error position
///
/// # Examples
///
/// ```
/// # use pretty_assertions::assert_eq;
/// use std::{str, io::Cursor};
/// use quick_xml::Reader;
/// use quick_xml::events::Event;
///
/// let xml = r#"<tag1 att1 = "test">
/// <tag2><!--Test comment-->Test</tag2>
/// <tag3>Test 2</tag3>
/// </tag1>"#;
/// let mut reader = Reader::from_reader(Cursor::new(xml.as_bytes()));
/// let mut buf = Vec::new();
///
/// fn into_line_and_column(reader: Reader<Cursor<&[u8]>>) -> (usize, usize) {
/// let end_pos = reader.buffer_position();
/// let mut cursor = reader.into_inner();
/// let s = String::from_utf8(cursor.into_inner()[0..end_pos].to_owned())
/// .expect("can't make a string");
/// let mut line = 1;
/// let mut column = 0;
/// for c in s.chars() {
/// if c == '\n' {
/// line += 1;
/// column = 0;
/// } else {
/// column += 1;
/// }
/// }
/// (line, column)
/// }
///
/// loop {
/// match reader.read_event_into(&mut buf) {
/// Ok(Event::Start(ref e)) => match e.name().as_ref() {
/// b"tag1" | b"tag2" => (),
/// tag => {
/// assert_eq!(b"tag3", tag);
/// assert_eq!((3, 22), into_line_and_column(reader));
/// break;
/// }
/// },
/// Ok(Event::Eof) => unreachable!(),
/// _ => (),
/// }
/// buf.clear();
/// }
/// ```
/// TODO
pub fn into_inner(self) -> R {
self.reader
}
Expand Down Expand Up @@ -394,6 +342,8 @@ impl<R> Reader<R> {
pub fn decoder(&self) -> Decoder {
self.parser.decoder()
}

configure_methods!();
}

/// Private sync reading methods
Expand Down
14 changes: 8 additions & 6 deletions src/reader/ns_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
//! [expanded names]: https://www.w3.org/TR/xml-names11/#dt-expname

use std::fs::File;
use std::io::{BufRead, BufReader};
use std::io;
use std::ops::Deref;
use std::path::Path;

use crate::encoding::DecodingReader;
use crate::errors::Result;
use crate::events::Event;
use crate::name::{LocalName, NamespaceResolver, QName, ResolveResult};
use crate::reader::{Reader, XmlSource};

/// A low level encoding-agnostic XML event reader that performs namespace resolution.
///
/// Consumes a [`BufRead`] and streams XML `Event`s.
Expand All @@ -32,7 +32,7 @@ pub struct NsReader<R> {
}

/// Builder methods
impl<R> NsReader<R> {
impl<R: io::Read> NsReader<DecodingReader<R>> {
/// Creates a `NsReader` that reads from a reader.
#[inline]
pub fn from_reader(reader: R) -> Self {
Expand Down Expand Up @@ -298,7 +298,7 @@ impl<R> NsReader<R> {
}
}

impl<R: BufRead> NsReader<R> {
impl<R: io::BufRead> NsReader<R> {
/// Reads the next event into given buffer.
///
/// This method manages namespaces but doesn't resolve them automatically.
Expand Down Expand Up @@ -509,14 +509,14 @@ impl<R: BufRead> NsReader<R> {
/// [`read_to_end()`]: Self::read_to_end
/// [`BytesStart::to_end()`]: crate::events::BytesStart::to_end
#[inline]
pub fn read_to_end_into(&mut self, end: QName, buf: &mut Vec<u8>) -> Result<()> {
pub fn read_to_end_into<'b>(&mut self, end: QName, buf: &'b mut Vec<u8>) -> Result<()> {
// According to the https://www.w3.org/TR/xml11/#dt-etag, end name should
// match literally the start name. See `Self::check_end_names` documentation
self.reader.read_to_end_into(end, buf)
}
}

impl NsReader<BufReader<File>> {
impl NsReader<DecodingReader<File>> {
/// Creates an XML reader from a file path.
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
Ok(Self::new(Reader::from_file(path)?))
Expand All @@ -530,6 +530,8 @@ impl<'i> NsReader<&'i [u8]> {
Self::new(Reader::from_str(s))
}

configure_methods!(reader);

/// Reads the next event, borrow its content from the input buffer.
///
/// This method manages namespaces but doesn't resolve them automatically.
Expand Down
Loading

0 comments on commit 8b39067

Please sign in to comment.