bashkit 0.14.2

Awesomely fast virtual sandbox with bash and file system
Documentation
// GENERATED by bashkit-coreutils-port. DO NOT EDIT.
//
// Source: uutils/coreutils@4988134fa src/uucore/src/lib/features/format/human.rs
// Regenerate: cargo run -p bashkit-coreutils-port -- port-module <UUTILS_DIR> format <REV>
//
// Original uutils licensed MIT; see THIRD_PARTY_LICENSES.

//! `human`-size formatting
//!
//! Format sizes like gnulibs human_readable() would
use unit_prefix::NumberPrefix;
#[derive(Copy, Clone, PartialEq)]
pub enum SizeFormat {
    Bytes,
    Binary,
    Decimal,
}
fn format_prefixed(prefixed: &NumberPrefix<f64>) -> String {
    match prefixed {
        NumberPrefix::Standalone(bytes) => bytes.to_string(),
        NumberPrefix::Prefixed(prefix, bytes) => {
            let prefix_str = prefix.symbol().trim_end_matches('i');
            if (10.0 * bytes).ceil() >= 100.0 {
                format!("{:.0}{prefix_str}", bytes.ceil())
            } else {
                let number = format!("{:.1}", (10.0 * bytes).ceil() / 10.0);
                format!("{}{prefix_str}", localize_decimal(number))
            }
        }
    }
}
fn localize_decimal(s: String) -> String {
    #[cfg(feature = "i18n-decimal")]
    {
        let sep = crate::i18n::decimal::locale_decimal_separator();
        if sep == "." {
            s
        } else {
            s.replacen('.', sep, 1)
        }
    }
    #[cfg(not(feature = "i18n-decimal"))]
    {
        s
    }
}
pub fn human_readable(size: u64, sfmt: SizeFormat) -> String {
    match sfmt {
        SizeFormat::Binary => format_prefixed(&NumberPrefix::binary(size as f64)),
        SizeFormat::Decimal => format_prefixed(&NumberPrefix::decimal(size as f64)),
        SizeFormat::Bytes => size.to_string(),
    }
}