#[cfg(test)]
mod tests;
#[cfg(not(feature = "si-units"))]
const SUFFIX: [&str; 9] = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
#[cfg(feature = "si-units")]
const SUFFIX: [&str; 9] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
#[cfg(not(feature = "si-units"))]
const UNIT: f64 = 1000.0;
#[cfg(feature = "si-units")]
const UNIT: f64 = 1024.0;
pub fn human_bytes<T: Into<f64>>(bytes: T) -> String {
let size = bytes.into();
if size <= 0.0 {
return "0 B".to_string();
}
let base = size.log10() / UNIT.log10();
#[cfg(feature = "fast")]
{
let mut buffer = ryu::Buffer::new();
let result = buffer
.format((UNIT.powf(base - base.floor()) * 10.0).round() / 10.0)
.trim_end_matches(".0");
[result, SUFFIX[base.floor() as usize]].join(" ")
}
#[cfg(not(feature = "fast"))]
{
let result = format!("{:.1}", UNIT.powf(base - base.floor()),)
.trim_end_matches(".0")
.to_owned();
[&result, SUFFIX[base.floor() as usize]].join(" ")
}
}