-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Skip whitespace in base64 encoded inputs given to the CLI
- Loading branch information
1 parent
6c7ecd1
commit cdbed82
Showing
3 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
pub mod decode; | ||
pub mod encode; | ||
pub mod guess; | ||
mod skip_whitespace; | ||
pub mod types; | ||
mod version; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use std::io::Read; | ||
|
||
/// Forwards read operations to the wrapped object, skipping over any whitespace | ||
/// in what is written to buf when the function returns. | ||
pub struct SkipWhitespace<R: Read> { | ||
inner: R, | ||
} | ||
|
||
impl<R: Read> SkipWhitespace<R> { | ||
pub fn new(inner: R) -> Self { | ||
SkipWhitespace { inner } | ||
} | ||
} | ||
|
||
impl<R: Read> Read for SkipWhitespace<R> { | ||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { | ||
let n = self.inner.read(buf)?; | ||
|
||
let mut written = 0; | ||
for read in 0..n { | ||
if !buf[read].is_ascii_whitespace() { | ||
buf[written] = buf[read]; | ||
written += 1; | ||
} | ||
} | ||
|
||
Ok(written) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test() { | ||
struct Test { | ||
input: &'static [u8], | ||
output: &'static [u8], | ||
} | ||
let tests = [ | ||
Test { | ||
input: b"", | ||
output: b"", | ||
}, | ||
Test { | ||
input: b" \n\t\r", | ||
output: b"", | ||
}, | ||
Test { | ||
input: b"a c", | ||
output: b"ac", | ||
}, | ||
Test { | ||
input: b"ab cd", | ||
output: b"abcd", | ||
}, | ||
Test { | ||
input: b" ab \n cd ", | ||
output: b"abcd", | ||
}, | ||
]; | ||
for (i, t) in tests.iter().enumerate() { | ||
let mut skip = SkipWhitespace::new(t.input); | ||
let mut output = Vec::new(); | ||
skip.read_to_end(&mut output).unwrap(); | ||
assert_eq!(output, t.output, "#{i}"); | ||
} | ||
} | ||
} |