Skip to content

Commit

Permalink
Merge pull request #11 from coriolinus/whitespace-remover
Browse files Browse the repository at this point in the history
Whitespace remover
  • Loading branch information
coriolinus authored Aug 12, 2024
2 parents 69b7c07 + aba3ed5 commit c12d359
Show file tree
Hide file tree
Showing 2 changed files with 58 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
62 changes: 57 additions & 5 deletions src/whitespace_remover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,62 @@ 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())

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

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

// ensure we got at least a byte to return
if !my_buf.is_empty() {
buf[..my_buf.len()].copy_from_slice(&my_buf);
return Ok(my_buf.len());
}

// otherwise reset and reread
my_buf.resize(buf.len(), 0);
}
}
}

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

#[test]
fn functionality() {
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 c12d359

Please sign in to comment.