Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
dralley committed Jul 26, 2023
1 parent daf407e commit 3dada46
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,48 @@ impl<R: io::Read> io::BufRead for Utf8BytesReader<R> {
}
}

///
#[derive(Debug)]
pub struct ValidatingReader<R> {
reader: R,
leftover_bytes_buf: [u8; 7],
len: u8,
}

impl<R: io::Read> ValidatingReader<R> {
///
pub fn new(reader: R) -> Self {
Self {
reader,
leftover_bytes_buf: [0; 7],
len: 0,
}
}
}

impl<R: io::Read> io::Read for ValidatingReader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
buf[..self.len.into()].copy_from_slice(&self.leftover_bytes_buf[..self.len.into()]);
let (_leftovers, copy_dest) = buf.split_at_mut(self.len.into());
let amt = self.reader.read(copy_dest)?;

match std::str::from_utf8(buf) {
Ok(_) => Ok(amt),
Err(err) => {
let (valid, after_valid) = buf.split_at(err.valid_up_to());
self.leftover_bytes_buf[..after_valid.len()].copy_from_slice(after_valid);
self.len = after_valid.len() as u8;
Ok(valid.len())
}
}

// error::const_io_error!(
// ErrorKind::InvalidData,
// "stream did not contain valid UTF-8"
// )
}
}

/// Decodes the provided bytes using the specified encoding.
///
/// Returns an error in case of malformed or non-representable sequences in the `bytes`.
Expand Down Expand Up @@ -126,3 +168,32 @@ pub fn detect_encoding(bytes: &[u8]) -> Option<(&'static Encoding, usize)> {
_ => None,
}
}

#[cfg(test)]
mod test {
use std::io::Read;

use super::*;

#[track_caller]
fn test_valiate_input(input: &[u8]) {
let mut reader = ValidatingReader::new(input);
let mut buf = [0; 100];
assert_eq!(reader.read(&mut buf).unwrap(), input.len());
}

mod decoding_reader {

}

mod validating_reader {

}

// #[test]
// fn test() {
// test_input(b"asdf");
// test_input(b"\x82\xA0\x82\xA2\x82\xA4");
// test_input(b"\xEF\xBB\xBFfoo\xFFbar");
// }
}
1 change: 1 addition & 0 deletions src/reader/buffered_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ mod test {
/// Checks that encoding is detected by BOM and changed after XML declaration
/// BOM indicates UTF-16LE, but XML - windows-1251
#[test]
#[ignore = "dalley fixme"]
fn bom_detected() {
let mut reader =
Reader::from_reader(b"\xFF\xFE<?xml encoding='windows-1251'?>".as_ref());
Expand Down

0 comments on commit 3dada46

Please sign in to comment.