Skip to content

Commit d29d2a0

Browse files
committed
improve str_as_int logic
1 parent bd0339f commit d29d2a0

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/input/shared.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,28 @@ pub fn int_as_bool<'a>(input: &'a impl Input<'a>, int: i64) -> ValResult<'a, boo
4444
}
4545

4646
pub fn str_as_int<'s, 'l>(input: &'s impl Input<'s>, str: &'l str) -> ValResult<'s, i64> {
47-
let str = strip_decimal_zeros(str);
4847
if let Ok(i) = str.parse::<i64>() {
4948
Ok(i)
49+
} else if let Some(s) = strip_decimal_zeros(str) {
50+
if let Ok(i) = s.parse::<i64>() {
51+
Ok(i)
52+
} else {
53+
Err(ValError::new(ErrorType::IntParsing, input))
54+
}
5055
} else {
5156
Err(ValError::new(ErrorType::IntParsing, input))
5257
}
5358
}
5459

5560
/// we don't want to parse as f64 then call `float_as_int` as it can loose precision for large ints, therefore
56-
/// we strip `.0+` manually
57-
fn strip_decimal_zeros(s: &str) -> &str {
61+
/// we strip `.0+` manually instead, then parse as i64
62+
fn strip_decimal_zeros(s: &str) -> Option<&str> {
5863
if let Some(i) = s.find('.') {
5964
if s[i + 1..].chars().all(|c| c == '0') {
60-
return &s[..i];
65+
return Some(&s[..i]);
6166
}
6267
}
63-
s
68+
None
6469
}
6570

6671
pub fn float_as_int<'a>(input: &'a impl Input<'a>, float: f64) -> ValResult<'a, i64> {

0 commit comments

Comments
 (0)