human_format_next/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
//! Human-format-next
use std::fmt;
#[derive(Debug, Clone, Copy)]
/// Entry point to the lib. Use this to handle your formatting needs.
///
/// - `BASE`: the base
/// - `DECIMALS`: target decimal places (if not keeping the original number)
pub struct Formatter<const BASE: usize = 0, const DECIMALS: usize = 2> {
/// Separator between numbers and units.
///
/// Defaults to be " " (space)
separator: &'static str,
/// The abbreviated number's units.
///
/// If the number is too large and no corresponding unit is found, the
/// scientific notation like `3.0e99` will be used.
units: &'static [&'static str],
/// The custom unit attached after the abbreviated number's unit.
custom_unit: Option<&'static str>,
}
impl Formatter {
/// SI units (western format).
pub const SI: Formatter<1000, 2> = Formatter::new(&["K", "M", "G", "T", "P", "E", "Z", "Y"]);
/// Binary units (western format).
pub const BINARY: Formatter<1024, 2> =
Formatter::new(&["Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"]);
/// Chinese units.
pub const CHINESE: Formatter<10000, 2> =
Formatter::new(&["万", "亿", "兆", "京", "垓", "秭", "穰", "沟"]);
}
impl<const BASE: usize, const DECIMALS: usize> Formatter<BASE, DECIMALS> {
#[inline]
/// Create a new [`Formatter`] with given `BASE`, `DECIMALS` and units.
pub const fn new(units: &'static [&'static str]) -> Self {
Self {
separator: " ",
units,
custom_unit: None,
}
}
#[inline]
/// Set the separator between numbers and units.
pub const fn with_separator(self, separator: &'static str) -> Self {
Self { separator, ..self }
}
#[inline]
/// Set scales, including base and the abbreviated number's units.
pub const fn with_scales<const N_BASE: usize>(
self,
units: &'static [&'static str],
) -> Formatter<N_BASE, DECIMALS> {
// wait for feature `generic_const_exprs`
debug_assert!(BASE > 0, "BASE CANNOT BE 0");
Formatter {
separator: self.separator,
units,
custom_unit: self.custom_unit,
}
}
#[inline]
/// Set custom unit attached after the abbreviated number's unit.
pub const fn with_custom_unit(self, custom_unit: &'static str) -> Self {
Self {
custom_unit: Some(custom_unit),
..self
}
}
#[inline]
/// Set the decimal places to keep.
pub const fn with_decimals<const N_DECIMALS: usize>(self) -> Formatter<BASE, N_DECIMALS> {
// wait for feature `generic_const_exprs`
debug_assert!(
N_DECIMALS <= f64::DIGITS as usize,
"DECIMALS too large, for RELEASE profile will make use of f64::DIGITS",
);
Formatter {
separator: self.separator,
units: self.units,
custom_unit: self.custom_unit,
}
}
#[inline]
/// Formats the given `number` into a human-readable string using the
/// specified units and separator.
///
/// See [`NumberT`] for all types we accept as param.
///
/// # Notice
///
/// For better performance (may be so), you may need
/// [`format_int`](Self::format_int) or [`format_uint`](Self::format_uint).
///
/// # Limitation
///
/// `f64` can only handle 15 decimal places at most. We may introduce
/// `macro_toolset` for large number formatting.
pub fn format(&self, number: impl NumberT) -> FormatResult<DECIMALS> {
if let Some(integer) = number.integer() {
self.format_general(integer, number.fraction())
.set_result_is_negative(number.is_negative())
} else {
self.format_float(
number
.fraction()
.expect("must be floating number which is too large"),
)
}
}
#[inline]
/// Formats the given `number` into a human-readable string using the
/// specified units and separator.
///
/// We accept any number that fits into `isize`. For `i128`, see
/// [`format_large_int`](Self::format_large_int).
pub fn format_int(&self, number: impl Into<i128>) -> FormatResult<DECIMALS> {
let number: i128 = number.into();
self.format_general(number.unsigned_abs(), None)
.set_result_is_negative(number.is_negative())
}
#[inline]
/// Formats the given `number` into a human-readable string using the
/// specified units and separator.
pub fn format_uint(&self, number: impl Into<u128>) -> FormatResult<DECIMALS> {
self.format_general(number.into(), None)
}
/// Formats the given `number` into a human-readable string using the
/// specified units and separator.
///
/// # Params
///
/// - `integer`: the integer part of the number.
/// - For float, [`f32::trunc`] or [`f64::trunc`] may helps you.
/// - `fraction`: the fractional part of the number.
/// - For float, [`f32::fract`] or [`f64::fract`] may helps you.
/// - For integer, leave it `None`.
///
/// # Notice
///
/// It's NOT recommended that you use this directly, use
/// [`format`](Self::format) instead unless you know exactly what you do.
pub fn format_general(&self, integer: u128, fraction: Option<f64>) -> FormatResult<DECIMALS> {
let base = BASE as u128;
if integer < base {
return FormatType::General {
integer,
fraction,
unit: None,
}
.formatter_result(self);
}
let mut index: usize = 0;
let mut value = integer;
while value >= base {
value /= base;
index += 1;
}
match self.units.get(index - 1) {
Some(&unit) => {
let leftover = {
let leftover_exp = (base).pow(index as u32);
(integer - value * leftover_exp) as f64 / leftover_exp as f64
};
// fraction may be larger than 1.
let leftover_fraction = fraction.unwrap_or(0.0) + leftover.fract();
FormatType::General {
integer: value + leftover.trunc() as u128 + leftover_fraction.trunc() as u128,
fraction: Some(leftover_fraction.fract()),
unit: Some(unit),
}
}
None => {
let mut exponent: usize = 0;
let mut value = integer;
// Safe: have checked DECIMALS <= u32::MAX
let target_len = 10usize.pow((DECIMALS as u32).min(f64::DIGITS) + 1);
loop {
value /= 10;
exponent += 1;
if value < target_len as _ {
break;
}
}
// calc the leftover
{
let mut value = value;
loop {
value /= 10;
exponent += 1;
if value < 10 {
break;
}
}
}
FormatType::Scientific {
coefficient: value as f64 / (target_len / 10) as f64,
exponent,
}
}
}
.formatter_result(self)
}
/// Formats the given `number` into a human-readable string using the
/// specified units and separator.
///
/// # Notice
///
/// It's NOT recommended that you use this directly, floating point
/// calculation often slower than integer arithmetic. Use
/// [`format`](Self::format) instead unless you know exactly what you do.
pub fn format_float(&self, number: f64) -> FormatResult<DECIMALS> {
let base = BASE as f64;
if number < base {
return FormatType::Float { number, unit: None }.formatter_result(self);
}
let mut index: usize = 0;
let mut value = number;
while value >= base {
value /= base;
index += 1;
}
match self.units.get(index - 1) {
Some(&unit) => {
let leftover = {
let leftover_exp = base.powi(index as i32);
(number - value * leftover_exp) / leftover_exp
};
FormatType::Float {
number: value + leftover,
unit: Some(unit),
}
}
None => {
let value = number.log10();
FormatType::Scientific {
coefficient: 10.0f64.powf(value.fract()),
exponent: value.trunc() as _,
}
}
}
.formatter_result(self)
}
}
#[allow(private_bounds)]
/// Sealed trait for number that can be formatted, including:
///
/// - `u8`
/// - `u16`
/// - `u32`
/// - `u64`
/// - `u128`
/// - `i8`
/// - `i16`
/// - `i32`
/// - `i64`
/// - `i128`
/// - `f32`
/// - `f64`
pub trait NumberT: number_sealed::NumberT {}
impl<T: number_sealed::NumberT> NumberT for T {}
mod number_sealed {
pub(super) trait NumberT: Copy {
fn is_negative(self) -> bool;
fn integer(self) -> Option<u128>;
#[inline]
fn fraction(self) -> Option<f64> {
None
}
}
macro_rules! impl_number_trait {
(UINT: $($ty:ident),+) => {
$(
impl NumberT for $ty {
#[inline]
fn is_negative(self) -> bool {
false
}
#[inline]
fn integer(self) -> Option<u128> {
Some(self as _)
}
}
)+
};
(INT: $($ty:ident),+) => {
$(
impl NumberT for $ty {
#[inline]
fn is_negative(self) -> bool {
self < 0
}
#[inline]
fn integer(self) -> Option<u128> {
Some(self.unsigned_abs() as _)
}
}
)+
};
}
impl_number_trait!(UINT: u8, u16, u32, u64, usize, u128);
impl_number_trait!(INT: i8, i16, i32, i64, isize, i128);
impl NumberT for f32 {
#[inline]
fn is_negative(self) -> bool {
self < 0.0
}
#[inline]
fn integer(self) -> Option<u128> {
Some(self.trunc() as _)
}
#[inline]
fn fraction(self) -> Option<f64> {
Some(self.fract() as _)
}
}
impl NumberT for f64 {
#[inline]
fn is_negative(self) -> bool {
self < 0.0
}
#[inline]
fn integer(self) -> Option<u128> {
if self < 3.40282366920938e+38 {
Some(self.trunc() as _)
} else {
None
}
}
#[inline]
fn fraction(self) -> Option<f64> {
if self < 3.40282366920938e+38 {
Some(self.fract() as _)
} else {
Some(self as _)
}
}
}
}
#[derive(Debug, Clone, Copy)]
/// Format result
///
/// This implements [`Display`](fmt::Display) and `to_string` is supported.
pub struct FormatResult<const DECIMALS: usize> {
result: FormatType<DECIMALS>,
result_is_negative: bool,
separator: &'static str,
custom_unit: Option<&'static str>,
}
impl<const DECIMALS: usize> fmt::Display for FormatResult<DECIMALS> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.result_is_negative {
write!(f, "-")?;
}
let custom_unit = self.custom_unit.unwrap_or_default();
match &self.result {
FormatType::General {
integer,
fraction,
unit,
} => {
// Keep 15, f64::DIGITS
let full_fraction = fraction.map(|fraction| format!("{fraction:.15}"));
let fraction = full_fraction
.as_ref()
.map(|full_fraction| {
let digits = (f64::DIGITS as usize).min(DECIMALS);
&full_fraction[1..digits + 2]
})
.unwrap_or_default();
let separator_before_unit = if (*unit).is_some() {
self.separator
} else {
""
};
let unit = (*unit).unwrap_or_default();
write!(
f,
"{integer}{fraction}{separator_before_unit}{unit}{custom_unit}",
)
}
FormatType::Float { number, unit } => {
// Keep 15, f64::DIGITS
let number = format!("{number:.15}");
let digits = (f64::DIGITS as usize).min(DECIMALS);
let number = &number[1..digits + 2];
let separator_before_unit = if (*unit).is_some() {
self.separator
} else {
""
};
let unit = (*unit).unwrap_or_default();
write!(f, "{number}{separator_before_unit}{unit}{custom_unit}",)
}
FormatType::Scientific {
coefficient: value,
exponent,
} => {
let separator_before_custom_unit = if self.custom_unit.is_some() {
self.separator
} else {
""
};
write!(
f,
"{value}e{exponent}{separator_before_custom_unit}{custom_unit}",
)
}
}
}
}
impl<const DECIMALS: usize> FormatResult<DECIMALS> {
#[inline]
const fn set_result_is_negative(self, result_is_negative: bool) -> Self {
Self {
result_is_negative,
..self
}
}
}
#[derive(Debug, Clone, Copy)]
enum FormatType<const DECIMALS: usize> {
/// General
General {
/// The integer part.
integer: u128,
/// The fractional part.
fraction: Option<f64>,
/// The abbreviated number's unit.
unit: Option<&'static str>,
},
/// General
Float {
/// The integer part.
number: f64,
/// The abbreviated number's unit.
unit: Option<&'static str>,
},
/// Scientific notation
Scientific {
/// The coefficient part, must be within `1.0` ~ `9.99...`
coefficient: f64,
/// The exponent part, must be a positive integer
exponent: usize,
},
}
impl<const DECIMALS: usize> FormatType<DECIMALS> {
#[inline]
const fn formatter_result<const BASE: usize>(
self,
formatter: &Formatter<BASE, DECIMALS>,
) -> FormatResult<DECIMALS> {
FormatResult {
result: self,
result_is_negative: false,
separator: formatter.separator,
custom_unit: formatter.custom_unit,
}
}
}