refactor(formatting): use helper function

this is probably much more efficient,
since `"0".repeat(n)` allocates a new string
(which is then immediately deallocated)
This commit is contained in:
electria 2026-11-16 15:08:56 -08:00
commit aef4b4727a
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -55,17 +55,17 @@ pub fn pretty_eval(input: &str, max_digits: usize) -> Result<String, Error> {
} else {
let amount_of_zeros = uexp - (changing_string.len() - dot_pos - 1);
changing_string.remove(dot_pos);
changing_string += &"0".repeat(amount_of_zeros);
changing_string.push_n('0', amount_of_zeros);
}
} else {
changing_string += &"0".repeat(uexp);
changing_string.push_n('0', uexp);
}
} else {
let uexp = exponent.unsigned_abs();
if let Some(dot_pos) = changing_string.find('.') {
changing_string.remove(dot_pos);
}
changing_string.insert_str(0, &("0.".to_owned() + &"0".repeat(uexp - 1)));
changing_string.insert_str(0, &format!("0.{}", "0".repeat(uexp - 1)));
}
}
@ -89,6 +89,17 @@ impl StrExt for str {
.unwrap_or(("", "", self))
}
}
trait StringExt {
fn push_n(&mut self, c: char, n: usize);
}
impl StringExt for String {
fn push_n(&mut self, c: char, n: usize) {
self.reserve(n);
for _ in 0..n {
self.push(c);
}
}
}
pub fn get_significant_digits(n: &Float) -> usize {
let raw_string = n.to_string().replace(['.', '-'], "");