feat: ignore and add back single equal sign
more than one is likely a user mistake and a panic still makes sense should also add back '&' in the same way, if applicable
This commit is contained in:
parent
0dc140909f
commit
704ef81816
1 changed files with 25 additions and 1 deletions
26
src/main.rs
26
src/main.rs
|
|
@ -18,9 +18,33 @@ fn main() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
println!("{}", parse_and_calc_humanreadable(input));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_and_calc_humanreadable(mut input: String) -> String {
|
||||||
|
let should_add_back_equals = if let Some((i, _)) = input
|
||||||
|
.chars()
|
||||||
|
.enumerate()
|
||||||
|
.filter(|(_, c)| !matches!(c, ' ' | '&' | '\n'))
|
||||||
|
.find(|(_, c)| *c == '=')
|
||||||
|
{
|
||||||
|
input.remove(i);
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
|
||||||
let result = parse_and_calc(&input);
|
let result = parse_and_calc(&input);
|
||||||
|
|
||||||
println!("{result:.0$}", get_significant_digits(&result));
|
format!(
|
||||||
|
"{0}{result:.1$}",
|
||||||
|
if should_add_back_equals { "= " } else { "" },
|
||||||
|
get_significant_digits(&result),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_equals_readdition() {
|
||||||
|
assert_eq!(parse_and_calc_humanreadable(" = 11".into()), "= 11");
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_and_calc(s: &str) -> Float {
|
pub fn parse_and_calc(s: &str) -> Float {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue