Skip to content

Commit bd8deb1

Browse files
committed
simplify more prevent trailing underscore
1 parent dfb81b1 commit bd8deb1

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/input/shared.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,22 +151,21 @@ fn strip_leading_zeros(s: &str) -> Option<&str> {
151151
// anything else is invalid, we return None
152152
_ => return None,
153153
};
154-
loop {
155-
match char_iter.next() {
156-
// continue on more leading zeros
157-
Some((_, '0')) => (),
158-
// if we get an underscore we continue - we're "within the number"
159-
Some((_, '_')) => (),
154+
for (i, c) in char_iter {
155+
match c {
156+
// continue on more leading zeros or if we get an underscore we continue - we're "within the number"
157+
'0' | '_' => (),
160158
// any other digit we return the rest of the string
161-
Some((i, c)) if ('1'..='9').contains(&c) => return Some(&s[i..]),
159+
'1'..='9' => return Some(&s[i..]),
162160
// if we get a dot we return the rest of the string but include the last zero
163-
Some((i, '.')) => return Some(&s[(i - 1)..]),
164-
// if the string is all zeros, we return a single zero
165-
None => return Some("0"),
161+
'.' => return Some(&s[(i - 1)..]),
166162
// anything else is invalid, we return None
167163
_ => return None,
168164
}
169165
}
166+
// if the string is all zeros (or underscores), we return the last character
167+
// generally this will be zero, but could be an underscore, which will fail
168+
Some(&s[s.len() - 1..])
170169
}
171170

172171
pub fn float_as_int<'py>(input: &(impl Input<'py> + ?Sized), float: f64) -> ValResult<EitherInt<'py>> {

tests/validators/test_int.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
('042', 42),
3737
('01', 1),
3838
('09', 9),
39+
('00_', Err('Input should be a valid integer, unable to parse string as an integer')),
3940
# next character after 9 is not valid
4041
('0:', Err('Input should be a valid integer, unable to parse string as an integer')),
4142
('4_2', 42),

0 commit comments

Comments
 (0)