diff --git a/src/input/shared.rs b/src/input/shared.rs index b72fd409c..771f16ed9 100644 --- a/src/input/shared.rs +++ b/src/input/shared.rs @@ -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> { diff --git a/tests/validators/test_int.py b/tests/validators/test_int.py index abad10f95..83e527514 100644 --- a/tests/validators/test_int.py +++ b/tests/validators/test_int.py @@ -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),