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-11-24 02:01:08 +00:00
|
|
|
fn format_bytes<'s>(mut amount: f64, kilo: f64, prefixes: &[&'s str]) -> (String, &'s str) {
|
2014-05-04 20:33:14 +00:00
|
|
|
let mut prefix = 0;
|
2014-06-28 10:32:29 +00:00
|
|
|
while amount >= kilo {
|
2014-05-04 20:33:14 +00:00
|
|
|
amount /= kilo;
|
|
|
|
prefix += 1;
|
|
|
|
}
|
2014-06-28 10:32:29 +00:00
|
|
|
|
|
|
|
if amount < 10.0 && prefix != 0 {
|
2014-11-24 02:01:08 +00:00
|
|
|
(format!("{:.1}", amount), prefixes[prefix])
|
2014-06-28 10:32:29 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-11-24 02:01:08 +00:00
|
|
|
(format!("{:.0}", amount), prefixes[prefix])
|
2014-06-28 10:32:29 +00:00
|
|
|
}
|
2014-05-04 20:33:14 +00:00
|
|
|
}
|
|
|
|
|
2014-09-22 16:03:47 +00:00
|
|
|
#[allow(non_snake_case)]
|
2014-11-24 02:01:08 +00:00
|
|
|
pub fn format_IEC_bytes<'s>(amount: u64) -> (String, &'s str) {
|
2014-06-28 10:32:29 +00:00
|
|
|
format_bytes(amount as f64, 1024.0, IEC_PREFIXES)
|
2014-05-04 20:33:14 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 02:01:08 +00:00
|
|
|
pub fn format_metric_bytes<'s>(amount: u64) -> (String, &'s str) {
|
2014-06-28 10:32:29 +00:00
|
|
|
format_bytes(amount as f64, 1000.0, METRIC_PREFIXES)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_0() {
|
|
|
|
let kk = format_metric_bytes(0);
|
2014-11-24 02:01:08 +00:00
|
|
|
assert!(kk == ("0".to_string(), ""));
|
2014-06-28 10:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_999() {
|
|
|
|
let kk = format_metric_bytes(999);
|
2014-11-24 02:01:08 +00:00
|
|
|
assert!(kk == ("999".to_string(), ""));
|
2014-06-28 10:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_1000() {
|
|
|
|
let kk = format_metric_bytes(1000);
|
2014-11-24 02:01:08 +00:00
|
|
|
assert!(kk == ("1.0".to_string(), "K"));
|
2014-06-28 10:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_1030() {
|
|
|
|
let kk = format_metric_bytes(1030);
|
2014-11-24 02:01:08 +00:00
|
|
|
assert!(kk == ("1.0".to_string(), "K"));
|
2014-06-28 10:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_1100() {
|
|
|
|
let kk = format_metric_bytes(1100);
|
2014-11-24 02:01:08 +00:00
|
|
|
assert!(kk == ("1.1".to_string(), "K"));
|
2014-06-28 10:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_1111() {
|
|
|
|
let kk = format_metric_bytes(1111);
|
2014-11-24 02:01:08 +00:00
|
|
|
assert!(kk == ("1.1".to_string(), "K"));
|
2014-05-04 20:33:14 +00:00
|
|
|
}
|
2014-06-28 10:32:29 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_104857() {
|
|
|
|
let kk = format_IEC_bytes(126456);
|
2014-11-24 02:01:08 +00:00
|
|
|
assert!(kk == ("123".to_string(), "Ki"));
|
2014-06-28 10:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_1048576() {
|
|
|
|
let kk = format_IEC_bytes(1048576);
|
2014-11-24 02:01:08 +00:00
|
|
|
assert!(kk == ("1.0".to_string(), "Mi"));
|
2014-06-28 10:32:29 +00:00
|
|
|
}
|
|
|
|
|