Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix overly permissive parsing after whitespace #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ impl std::str::FromStr for ByteSize {
let number = take_while(value, |c| c.is_ascii_digit() || c == '.');
match number.parse::<f64>() {
Ok(v) => {
let suffix = skip_while(value, |c| {
c.is_whitespace() || c.is_ascii_digit() || c == '.'
});
let suffix = skip_while(&value[number.len()..], char::is_whitespace);
match suffix.parse::<Unit>() {
Ok(u) => Ok(Self((v * u) as u64)),
Err(error) => Err(format!(
Expand Down Expand Up @@ -220,6 +218,11 @@ mod tests {

assert!(parse("").is_err());
assert!(parse("a124GB").is_err());
assert!(parse("1.3 42.0 B").is_err());
assert!(parse("1.3 ... B").is_err());
// The original implementation did not account for the possibility that users may
// use whitespace to visually separate digits, thus treat it as an error
assert!(parse("1 000 B").is_err());
}

#[test]
Expand Down