Skip to content

Commit

Permalink
Fix whitespace remover
Browse files Browse the repository at this point in the history
Previously, if there was enough whitespace to fill the buffer it would
return a read length of zero, even though there is still content to
read.
  • Loading branch information
rsammelson committed Jul 29, 2024
1 parent dda0866 commit c094548
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/version_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ where
self.0.has_seen_version = true;
let mut new_buf = vec![0; buf.len() + 1];
new_buf[0] = self.0.version;
(&mut new_buf[1..]).copy_from_slice(buf);
new_buf[1..].copy_from_slice(buf);
self.0.wrapped.write(&new_buf).map(|n| n - 1)
}
}
Expand Down
27 changes: 22 additions & 5 deletions src/whitespace_remover.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(test)]
mod test;

use std::io::{Read, Result};

/// Remove all ascii whitespace from an incoming stream.
Expand All @@ -20,10 +23,24 @@ where
{
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let mut my_buf = vec![0; buf.len()];
let n = self.inner.read(&mut my_buf)?;
my_buf.truncate(n);
my_buf.retain(|d| !d.is_ascii_whitespace());
buf[..my_buf.len()].copy_from_slice(&my_buf);
Ok(my_buf.len())

Ok(loop {
let n = self.inner.read(&mut my_buf)?;
if n == 0 {
// the underlying reader is done
break 0;
}

my_buf.truncate(n);
my_buf.retain(|d| !d.is_ascii_whitespace());

// keep reading until we get at least a byte to return
if !my_buf.is_empty() {
buf[..my_buf.len()].copy_from_slice(&my_buf);
break my_buf.len();
}

my_buf.resize(buf.len(), 0);
})
}
}
33 changes: 33 additions & 0 deletions src/whitespace_remover/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::io::Read;

#[test]
fn test() {
const TEST: &str = " t/prau0INWoWUQ0LgQ\tMUdJRcCetBZP\nAyD+DCpn 01yWZT/LBo3Ogk0INwwuAtKNI ";

let mut stripped = String::new();
super::WhitespaceRemover::new(TEST.as_bytes())
.read_to_string(&mut stripped)
.unwrap();

assert_eq!(
stripped,
"t/prau0INWoWUQ0LgQMUdJRcCetBZPAyD+DCpn01yWZT/LBo3Ogk0INwwuAtKNI"
);
}

#[test]
fn long_whitespace() {
let mut test_string = "a".to_owned();
test_string.reserve_exact(161);
for _ in 0..20 {
test_string.push_str(" ");
}
test_string.push('b');

let mut stripped = String::new();
super::WhitespaceRemover::new(test_string.as_bytes())
.read_to_string(&mut stripped)
.unwrap();

assert_eq!(stripped, "ab");
}

0 comments on commit c094548

Please sign in to comment.