Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
dralley committed Jul 25, 2022
1 parent b16c52e commit 488c9c7
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ impl EncodingRef {
}

#[cfg(feature = "encoding")]

struct DecodingBufReader<R> {
pub(crate) struct DecodingBufReader { //<R> {
// // The buffer
// buffer: String,
// // How many bytes in the buffer currently hold significant data.
Expand All @@ -243,44 +242,45 @@ struct DecodingBufReader<R> {
// /// Track whether we see errors.
// encoding: Option<Encoding>,

inner: R,
// inner: R,
decoded_buffer: Vec<u8>,
current_pos: usize,

decoder: ExtDecoder,
encoding: EncodingRef,
}

#[cfg(feature = "encoding")]
impl<R: BufRead> BufRead for DecodingBufReader<R> {
fn fill_buf(&mut self) -> io::Result<&[u8]> {
self.shuffle();
let data = self.inner.fill_buf()?;
// #[cfg(feature = "encoding")]
// impl<R: BufRead> BufRead for DecodingBufReader<R> {
// fn fill_buf(&mut self) -> io::Result<&[u8]> {
// self.shuffle();
// let data = self.inner.fill_buf()?;

let amount_read_from_inner = self.feed(data)?;
self.inner.consume(amount_read_from_inner);
// let amount_read_from_inner = self.feed(data)?;
// self.inner.consume(amount_read_from_inner);

Ok(data)
}
// Ok(data)
// }

fn consume(&mut self, amt: usize) {
self.current_pos = std::cmp::min(self.current_pos + amt, self.decoded_buffer.capacity());
}
}
// fn consume(&mut self, amt: usize) {
// self.current_pos = std::cmp::min(self.current_pos + amt, self.decoded_buffer.capacity());
// }
// }


#[cfg(feature = "encoding")]
impl<R: Read> Read for DecodingBufReader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}
}
// #[cfg(feature = "encoding")]
// impl<R: Read> Read for DecodingBufReader<R> {
// fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
// self.inner.read(buf)
// }
// }

#[cfg(feature = "encoding")]
impl<R: BufRead> DecodingBufReader<R> {
fn new(inner: R) -> Self {
// impl<R: BufRead> DecodingBufReader<R> {
impl DecodingBufReader {
fn new() -> Self {
DecodingBufReader {
inner: inner,
// inner: inner,
decoded_buffer: Vec::new(),
current_pos: 0,

Expand All @@ -289,8 +289,12 @@ impl<R: BufRead> DecodingBufReader<R> {
}
}

fn get_raw_buffer(&mut self) -> io::Result<&[u8]> {
self.inner.fill_buf()
// fn get_raw_buffer(&mut self) -> io::Result<&[u8]> {
// self.inner.fill_buf()
// }

fn buffer(&self) -> &[u8] {
&self.decoded_buffer
}

/// Move unconsumed data to the front of the buffer and reset the length
Expand Down Expand Up @@ -342,7 +346,15 @@ impl<R: BufRead> DecodingBufReader<R> {

#[cfg(test)]
mod tests {
#[cfg(feature = "encoding")]
#[test]
fn basic() {
use super::DecodingBufReader;

let mut reader = DecodingBufReader::new();
reader.feed(b"mary had a little lamb").unwrap();
panic!("{:?}", reader.decoded_buffer);
}
}


Expand Down

0 comments on commit 488c9c7

Please sign in to comment.