feat: preserve leading_spaces
This commit is contained in:
parent
ca9a522d2b
commit
9bacccfb71
1 changed files with 12 additions and 10 deletions
22
src/main.rs
22
src/main.rs
|
|
@ -22,6 +22,12 @@ fn main() {
|
|||
}
|
||||
|
||||
pub fn parse_and_calc_humanreadable(mut input: String) -> String {
|
||||
// no need to remove the characters here,
|
||||
// as we ignore the them in all other logic already
|
||||
let should_add_back_alignment = input.contains('&');
|
||||
let should_add_back_backslash = input.contains('\\');
|
||||
let leading_spaces = input.chars().take_while(|c| *c == ' ').count();
|
||||
|
||||
let should_add_back_equals = input
|
||||
.find('=')
|
||||
.inspect(|i| {
|
||||
|
|
@ -29,15 +35,12 @@ pub fn parse_and_calc_humanreadable(mut input: String) -> String {
|
|||
})
|
||||
.is_some();
|
||||
|
||||
// no need to remove the characters here,
|
||||
// as we ignore the them in all other logic already
|
||||
let should_add_back_alignment = input.contains('&');
|
||||
let should_add_back_backslash = input.contains('\\');
|
||||
|
||||
let result = parse_and_calc(&input);
|
||||
let significant_digits = get_significant_digits(&result);
|
||||
|
||||
format!(
|
||||
"{0}{result:.1$}{2}",
|
||||
"{:leading_spaces$}{}{result:.significant_digits$}{}",
|
||||
' ',
|
||||
if should_add_back_equals && should_add_back_alignment {
|
||||
"& = "
|
||||
} else if should_add_back_equals {
|
||||
|
|
@ -47,15 +50,14 @@ pub fn parse_and_calc_humanreadable(mut input: String) -> String {
|
|||
} else {
|
||||
""
|
||||
},
|
||||
get_significant_digits(&result),
|
||||
if should_add_back_backslash { " \\" } else { "" }
|
||||
)
|
||||
}
|
||||
#[test]
|
||||
fn test_equals_readdition() {
|
||||
assert_eq!(parse_and_calc_humanreadable(" = 11".into()), "= 11");
|
||||
assert_eq!(parse_and_calc_humanreadable(" &= 11".into()), "& = 11");
|
||||
assert_eq!(parse_and_calc_humanreadable(" & 11".into()), "& 11");
|
||||
assert_eq!(parse_and_calc_humanreadable(" = 11".into()), " = 11");
|
||||
assert_eq!(parse_and_calc_humanreadable(" &= 11".into()), " & = 11");
|
||||
assert_eq!(parse_and_calc_humanreadable(" & 11".into()), " & 11");
|
||||
}
|
||||
|
||||
pub fn parse_and_calc(s: &str) -> Float {
|
||||
|
|
|
|||
Loading…
Reference in a new issue