Skip to content

Commit 18aadc9

Browse files
committed
Fix parsing logic after first whitespace
The way the parse was implemented accepted additional numeric characters or `.` after the first valid `f64` literal but ignored them. This permitted more strings that are invalid following a strict grammar for byte sizes and silently ignores the further symbols without error. ``` 1.0 ...KB 1.0 42.0 B ``` This change makes those illegal. `1 000 B` was also subject to the bad `skip_while` ignoring the following `000`. In this version of the fix whitespace is not accepted as a digit separator. So it will raise an error if the user doesn't explicitly strip the whitespace instead of reporting a wrong value.
1 parent b7b7988 commit 18aadc9

File tree

1 file changed

+1
-3
lines changed

1 file changed

+1
-3
lines changed

src/parse.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ impl std::str::FromStr for ByteSize {
1010
let number = take_while(value, |c| c.is_ascii_digit() || c == '.');
1111
match number.parse::<f64>() {
1212
Ok(v) => {
13-
let suffix = skip_while(value, |c| {
14-
c.is_whitespace() || c.is_ascii_digit() || c == '.'
15-
});
13+
let suffix = skip_while(&value[number.len()..], char::is_whitespace);
1614
match suffix.parse::<Unit>() {
1715
Ok(u) => Ok(Self((v * u) as u64)),
1816
Err(error) => Err(format!(

0 commit comments

Comments
 (0)