Skip to content

Commit

Permalink
simplify more prevent trailing underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Apr 13, 2024
1 parent dfb81b1 commit bd8deb1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/input/shared.rs
Expand Up @@ -151,22 +151,21 @@ fn strip_leading_zeros(s: &str) -> Option<&str> {
// anything else is invalid, we return None
_ => return None,
};
loop {
match char_iter.next() {
// continue on more leading zeros
Some((_, '0')) => (),
// if we get an underscore we continue - we're "within the number"
Some((_, '_')) => (),
for (i, c) in char_iter {
match c {
// continue on more leading zeros or if we get an underscore we continue - we're "within the number"
'0' | '_' => (),
// any other digit we return the rest of the string
Some((i, c)) if ('1'..='9').contains(&c) => return Some(&s[i..]),
'1'..='9' => return Some(&s[i..]),
// if we get a dot we return the rest of the string but include the last zero
Some((i, '.')) => return Some(&s[(i - 1)..]),
// if the string is all zeros, we return a single zero
None => return Some("0"),
'.' => return Some(&s[(i - 1)..]),
// anything else is invalid, we return None
_ => return None,
}
}
// if the string is all zeros (or underscores), we return the last character
// generally this will be zero, but could be an underscore, which will fail
Some(&s[s.len() - 1..])
}

pub fn float_as_int<'py>(input: &(impl Input<'py> + ?Sized), float: f64) -> ValResult<EitherInt<'py>> {
Expand Down
1 change: 1 addition & 0 deletions tests/validators/test_int.py
Expand Up @@ -36,6 +36,7 @@
('042', 42),
('01', 1),
('09', 9),
('00_', Err('Input should be a valid integer, unable to parse string as an integer')),
# next character after 9 is not valid
('0:', Err('Input should be a valid integer, unable to parse string as an integer')),
('4_2', 42),
Expand Down

0 comments on commit bd8deb1

Please sign in to comment.