pub mod add;
pub mod control;
pub mod div;
pub mod max;
pub mod min;
pub mod mul;
pub mod sub;
pub fn to_int(s: &str) -> i64 {
let mut s = String::from(s);
s = s.trim_start().to_string();
let negative = if s.starts_with("-") {
s = s.split_off(1);
-1
} else {
1
};
let mut num_str = String::from("");
for i in s.chars() {
match i {
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => num_str.push(i),
_ => break,
}
}
if !num_str.is_empty() {
let Ok(num) = num_str.parse::<i64>() else {
return 0;
};
return num * negative;
}
0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_int() {
assert_eq!(0, to_int("c3po"));
assert_eq!(4, to_int("4.8"));
assert_eq!(-12, to_int("-12"));
assert_eq!(0, to_int("- 12"));
assert_eq!(4, to_int(" 4.8"));
}
}