2014-05-25 14:52:36 +00:00
|
|
|
static METRIC_PREFIXES: &'static [&'static str] = &[
|
|
|
|
"", "K", "M", "G", "T", "P", "E", "Z", "Y"
|
|
|
|
];
|
|
|
|
|
|
|
|
static IEC_PREFIXES: &'static [&'static str] = &[
|
|
|
|
"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"
|
|
|
|
];
|
|
|
|
|
2014-06-03 20:20:17 +00:00
|
|
|
fn format_bytes(mut amount: u64, kilo: u64, prefixes: &[&str]) -> (String, String) {
|
2014-05-04 20:33:14 +00:00
|
|
|
let mut prefix = 0;
|
|
|
|
while amount > kilo {
|
|
|
|
amount /= kilo;
|
|
|
|
prefix += 1;
|
|
|
|
}
|
2014-06-03 20:20:17 +00:00
|
|
|
return (format!("{}", amount), prefixes[prefix].to_string());
|
2014-05-04 20:33:14 +00:00
|
|
|
}
|
|
|
|
|
2014-06-16 19:32:23 +00:00
|
|
|
#[allow(non_snake_case_functions)]
|
2014-06-03 20:20:17 +00:00
|
|
|
pub fn format_IEC_bytes(amount: u64) -> (String, String) {
|
2014-06-02 20:02:06 +00:00
|
|
|
format_bytes(amount, 1024, IEC_PREFIXES)
|
2014-05-04 20:33:14 +00:00
|
|
|
}
|
|
|
|
|
2014-06-03 20:20:17 +00:00
|
|
|
pub fn format_metric_bytes(amount: u64) -> (String, String) {
|
2014-06-02 20:02:06 +00:00
|
|
|
format_bytes(amount, 1000, METRIC_PREFIXES)
|
2014-05-04 20:33:14 +00:00
|
|
|
}
|