From 9bacccfb71bcddf5f932cccaa265ff5687bf87f9 Mon Sep 17 00:00:00 2001 From: electria Date: Wed, 30 Sep 2026 15:18:15 -0700 Subject: [PATCH] feat: preserve leading_spaces --- src/main.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index a8f98ac..640fa6e 100644 --- a/src/main.rs +++ b/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 {