Skip to content

Commit

Permalink
More things converted to CompactString
Browse files Browse the repository at this point in the history
  • Loading branch information
rben01 committed Oct 12, 2024
1 parent 86ade91 commit 4b2a528
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 90 deletions.
20 changes: 13 additions & 7 deletions numbat/src/interpreter/assert_eq_3.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{quantity::Quantity, span::Span};
use compact_str::{format_compact, CompactString};
use std::fmt::Display;
use thiserror::Error;

Expand Down Expand Up @@ -32,7 +33,7 @@ impl AssertEq3Error {
}

/// Returns the comparand quantities formatted as strings for pretty error message display.
pub fn fmt_comparands(&self) -> (String, String) {
pub fn fmt_comparands(&self) -> (CompactString, CompactString) {
let (lhs_converted_len, _) =
get_float_part_lengths(&self.lhs_converted.unsafe_value_as_string());
let (rhs_converted_len, _) =
Expand All @@ -46,7 +47,12 @@ impl AssertEq3Error {
(lhs, rhs)
}

fn fmt_comparand(&self, converted: &Quantity, original: &Quantity, width: usize) -> String {
fn fmt_comparand(
&self,
converted: &Quantity,
original: &Quantity,
width: usize,
) -> CompactString {
let pretty_converted_str = left_pad_integer_part(
converted
.pretty_print_with_precision(self.eps_precision())
Expand All @@ -58,7 +64,7 @@ impl AssertEq3Error {
if converted.unit() == original.unit() {
pretty_converted_str
} else {
format!("{} ({})", pretty_converted_str, original)
format_compact!("{} ({})", pretty_converted_str, original)
}
}
}
Expand Down Expand Up @@ -91,7 +97,7 @@ fn get_float_part_lengths(number: &str) -> (usize, usize) {

/// Returns the input number padded with 0s until the integer part width (number of characters) is exactly integer_part_width
/// The input number should be a float plainly formatted as a string as with `to_string`.
fn left_pad_integer_part(number: &str, integer_part_width: usize) -> String {
fn left_pad_integer_part(number: &str, integer_part_width: usize) -> CompactString {
let (integer_part, fractional_part) = get_float_parts(number);
let integer_part_len = integer_part.len();
let is_negative = integer_part.starts_with('-');
Expand All @@ -111,7 +117,7 @@ fn left_pad_integer_part(number: &str, integer_part_width: usize) -> String {
};

// Pad integer part with 0s
let integer_part_abs_padded = format!(
let integer_part_abs_padded = format_compact!(
"{:0>width$}",
integer_part_abs,
width = padding_needed + integer_part_abs.len()
Expand All @@ -121,12 +127,12 @@ fn left_pad_integer_part(number: &str, integer_part_width: usize) -> String {
let padded_str = if fractional_part.is_empty() {
integer_part_abs_padded
} else {
format!("{}.{}", integer_part_abs_padded, fractional_part)
format_compact!("{}.{}", integer_part_abs_padded, fractional_part)
};

// Add the negative sign if necessary
if is_negative {
format!("-{}", padded_str)
format_compact!("-{}", padded_str)
} else {
padded_str
}
Expand Down
162 changes: 82 additions & 80 deletions numbat/src/prefix.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use compact_str::{format_compact, CompactString};

use crate::number::Number;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -102,91 +104,91 @@ impl Prefix {
matches!(self, Prefix::Binary(_))
}

pub fn as_string_short(&self) -> String {
pub fn as_string_short(&self) -> CompactString {
match self {
Prefix::Metric(-30) => "q".into(),
Prefix::Metric(-27) => "r".into(),
Prefix::Metric(-24) => "y".into(),
Prefix::Metric(-21) => "z".into(),
Prefix::Metric(-18) => "a".into(),
Prefix::Metric(-15) => "f".into(),
Prefix::Metric(-12) => "p".into(),
Prefix::Metric(-9) => "n".into(),
Prefix::Metric(-6) => "µ".into(),
Prefix::Metric(-3) => "m".into(),
Prefix::Metric(-2) => "c".into(),
Prefix::Metric(-1) => "d".into(),
Prefix::Metric(0) => "".into(),
Prefix::Metric(1) => "da".into(),
Prefix::Metric(2) => "h".into(),
Prefix::Metric(3) => "k".into(),
Prefix::Metric(6) => "M".into(),
Prefix::Metric(9) => "G".into(),
Prefix::Metric(12) => "T".into(),
Prefix::Metric(15) => "P".into(),
Prefix::Metric(18) => "E".into(),
Prefix::Metric(21) => "Z".into(),
Prefix::Metric(24) => "Y".into(),
Prefix::Metric(27) => "R".into(),
Prefix::Metric(30) => "Q".into(),

Prefix::Metric(n) => format!("<prefix 10^{n}>"),

Prefix::Binary(0) => "".into(),
Prefix::Binary(10) => "Ki".into(),
Prefix::Binary(20) => "Mi".into(),
Prefix::Binary(30) => "Gi".into(),
Prefix::Binary(40) => "Ti".into(),
Prefix::Binary(50) => "Pi".into(),
Prefix::Binary(60) => "Ei".into(),
Prefix::Binary(70) => "Zi".into(),
Prefix::Binary(80) => "Yi".into(),

Prefix::Binary(n) => format!("<prefix 2^{n}>"),
Prefix::Metric(-30) => CompactString::const_new("q"),
Prefix::Metric(-27) => CompactString::const_new("r"),
Prefix::Metric(-24) => CompactString::const_new("y"),
Prefix::Metric(-21) => CompactString::const_new("z"),
Prefix::Metric(-18) => CompactString::const_new("a"),
Prefix::Metric(-15) => CompactString::const_new("f"),
Prefix::Metric(-12) => CompactString::const_new("p"),
Prefix::Metric(-9) => CompactString::const_new("n"),
Prefix::Metric(-6) => CompactString::const_new("µ"),
Prefix::Metric(-3) => CompactString::const_new("m"),
Prefix::Metric(-2) => CompactString::const_new("c"),
Prefix::Metric(-1) => CompactString::const_new("d"),
Prefix::Metric(0) => CompactString::const_new(""),
Prefix::Metric(1) => CompactString::const_new("da"),
Prefix::Metric(2) => CompactString::const_new("h"),
Prefix::Metric(3) => CompactString::const_new("k"),
Prefix::Metric(6) => CompactString::const_new("M"),
Prefix::Metric(9) => CompactString::const_new("G"),
Prefix::Metric(12) => CompactString::const_new("T"),
Prefix::Metric(15) => CompactString::const_new("P"),
Prefix::Metric(18) => CompactString::const_new("E"),
Prefix::Metric(21) => CompactString::const_new("Z"),
Prefix::Metric(24) => CompactString::const_new("Y"),
Prefix::Metric(27) => CompactString::const_new("R"),
Prefix::Metric(30) => CompactString::const_new("Q"),

Prefix::Metric(n) => format_compact!("<prefix 10^{n}>"),

Prefix::Binary(0) => CompactString::const_new(""),
Prefix::Binary(10) => CompactString::const_new("Ki"),
Prefix::Binary(20) => CompactString::const_new("Mi"),
Prefix::Binary(30) => CompactString::const_new("Gi"),
Prefix::Binary(40) => CompactString::const_new("Ti"),
Prefix::Binary(50) => CompactString::const_new("Pi"),
Prefix::Binary(60) => CompactString::const_new("Ei"),
Prefix::Binary(70) => CompactString::const_new("Zi"),
Prefix::Binary(80) => CompactString::const_new("Yi"),

Prefix::Binary(n) => format_compact!("<prefix 2^{n}>"),
}
}

pub fn as_string_long(&self) -> String {
pub fn as_string_long(&self) -> CompactString {
match self {
Prefix::Metric(-30) => "quecto".into(),
Prefix::Metric(-27) => "ronto".into(),
Prefix::Metric(-24) => "yocto".into(),
Prefix::Metric(-21) => "zepto".into(),
Prefix::Metric(-18) => "atto".into(),
Prefix::Metric(-15) => "femto".into(),
Prefix::Metric(-12) => "pico".into(),
Prefix::Metric(-9) => "nano".into(),
Prefix::Metric(-6) => "micro".into(),
Prefix::Metric(-3) => "milli".into(),
Prefix::Metric(-2) => "centi".into(),
Prefix::Metric(-1) => "deci".into(),
Prefix::Metric(0) => "".into(),
Prefix::Metric(1) => "deca".into(),
Prefix::Metric(2) => "hecto".into(),
Prefix::Metric(3) => "kilo".into(),
Prefix::Metric(6) => "mega".into(),
Prefix::Metric(9) => "giga".into(),
Prefix::Metric(12) => "tera".into(),
Prefix::Metric(15) => "peta".into(),
Prefix::Metric(18) => "exa".into(),
Prefix::Metric(21) => "zetta".into(),
Prefix::Metric(24) => "yotta".into(),
Prefix::Metric(27) => "ronna".into(),
Prefix::Metric(30) => "quetta".into(),

Prefix::Metric(n) => format!("<prefix 10^{n}>"),

Prefix::Binary(0) => "".into(),
Prefix::Binary(10) => "kibi".into(),
Prefix::Binary(20) => "mebi".into(),
Prefix::Binary(30) => "gibi".into(),
Prefix::Binary(40) => "tebi".into(),
Prefix::Binary(50) => "pebi".into(),
Prefix::Binary(60) => "exbi".into(),
Prefix::Binary(70) => "zebi".into(),
Prefix::Binary(80) => "yobi".into(),

Prefix::Binary(n) => format!("<prefix 2^{n}>"),
Prefix::Metric(-30) => CompactString::const_new("quecto"),
Prefix::Metric(-27) => CompactString::const_new("ronto"),
Prefix::Metric(-24) => CompactString::const_new("yocto"),
Prefix::Metric(-21) => CompactString::const_new("zepto"),
Prefix::Metric(-18) => CompactString::const_new("atto"),
Prefix::Metric(-15) => CompactString::const_new("femto"),
Prefix::Metric(-12) => CompactString::const_new("pico"),
Prefix::Metric(-9) => CompactString::const_new("nano"),
Prefix::Metric(-6) => CompactString::const_new("micro"),
Prefix::Metric(-3) => CompactString::const_new("milli"),
Prefix::Metric(-2) => CompactString::const_new("centi"),
Prefix::Metric(-1) => CompactString::const_new("deci"),
Prefix::Metric(0) => CompactString::const_new(""),
Prefix::Metric(1) => CompactString::const_new("deca"),
Prefix::Metric(2) => CompactString::const_new("hecto"),
Prefix::Metric(3) => CompactString::const_new("kilo"),
Prefix::Metric(6) => CompactString::const_new("mega"),
Prefix::Metric(9) => CompactString::const_new("giga"),
Prefix::Metric(12) => CompactString::const_new("tera"),
Prefix::Metric(15) => CompactString::const_new("peta"),
Prefix::Metric(18) => CompactString::const_new("exa"),
Prefix::Metric(21) => CompactString::const_new("zetta"),
Prefix::Metric(24) => CompactString::const_new("yotta"),
Prefix::Metric(27) => CompactString::const_new("ronna"),
Prefix::Metric(30) => CompactString::const_new("quetta"),

Prefix::Metric(n) => format_compact!("<prefix 10^{n}>"),

Prefix::Binary(0) => CompactString::const_new(""),
Prefix::Binary(10) => CompactString::const_new("kibi"),
Prefix::Binary(20) => CompactString::const_new("mebi"),
Prefix::Binary(30) => CompactString::const_new("gibi"),
Prefix::Binary(40) => CompactString::const_new("tebi"),
Prefix::Binary(50) => CompactString::const_new("pebi"),
Prefix::Binary(60) => CompactString::const_new("exbi"),
Prefix::Binary(70) => CompactString::const_new("zebi"),
Prefix::Binary(80) => CompactString::const_new("yobi"),

Prefix::Binary(n) => format_compact!("<prefix 2^{n}>"),
}
}
}
6 changes: 3 additions & 3 deletions numbat/src/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::number::Number;
use crate::pretty_print::PrettyPrint;
use crate::unit::{is_multiple_of, Unit, UnitFactor};

use compact_str::format_compact;
use compact_str::{format_compact, CompactString, ToCompactString};
use itertools::Itertools;
use num_rational::Ratio;
use num_traits::{FromPrimitive, Zero};
Expand Down Expand Up @@ -378,8 +378,8 @@ impl Quantity {
self.pretty_print_with_options(Some(options))
}

pub fn unsafe_value_as_string(&self) -> String {
self.unsafe_value().to_string()
pub fn unsafe_value_as_string(&self) -> CompactString {
self.unsafe_value().to_compact_string()
}
}

Expand Down

0 comments on commit 4b2a528

Please sign in to comment.