Skip to content

Commit

Permalink
prevent trailing dots in integers (#1267)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin authored Apr 8, 2024
1 parent f636403 commit b945bcb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/input/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ fn clean_int_str(mut s: &str) -> Option<Cow<str>> {
// we don't want to parse as f64 then call `float_as_int` as it can lose precision for large ints, therefore
// we strip `.0+` manually instead
if let Some(i) = s.find('.') {
if s[i + 1..].chars().all(|c| c == '0') {
let decimal_part = &s[i + 1..];
if !decimal_part.is_empty() && decimal_part.chars().all(|c| c == '0') {
s = &s[..i];
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/validators/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@
('42', 42),
(42.0, 42),
('42.0', 42),
('42.00', 42),
('042', 42),
('4_2', 42),
('4_2.0', 42),
('04_2.0', 42),
(' 04_2.0 ', 42),
# because zeros are striped before underscores this is not allowed
(' 0_42.0 ', Err('Input should be a valid integer, unable to parse string as an integer')),
('000001', 1),
('123456789.0', 123_456_789),
('1.', Err('Input should be a valid integer, unable to parse string as an integer')),
('42.', Err('Input should be a valid integer, unable to parse string as an integer')),
('123456789123456.00001', Err('Input should be a valid integer, unable to parse string as an integer')),
(int(1e10), int(1e10)),
(i64_max, i64_max),
Expand Down

0 comments on commit b945bcb

Please sign in to comment.