pbs-api-types: fix HumanByte::auto_scale

This commit is contained in:
Dietmar Maurer 2021-11-21 09:13:02 +01:00
parent 0eadfdf670
commit c94723062c
1 changed files with 7 additions and 6 deletions

View File

@ -44,13 +44,13 @@ impl SizeUnit {
/// 'binary' specifies if IEC (base 2) units should be used or SI (base 10) ones /// 'binary' specifies if IEC (base 2) units should be used or SI (base 10) ones
pub fn auto_scale(size: f64, binary: bool) -> SizeUnit { pub fn auto_scale(size: f64, binary: bool) -> SizeUnit {
if binary { if binary {
let bits = 63 - (size as u64).leading_zeros(); let bits = 64 - (size as u64).leading_zeros();
match bits { match bits {
50.. => SizeUnit::Pebi, 51.. => SizeUnit::Pebi,
40..=49 => SizeUnit::Tebi, 41..=50 => SizeUnit::Tebi,
30..=39 => SizeUnit::Gibi, 31..=40 => SizeUnit::Gibi,
20..=29 => SizeUnit::Mebi, 21..=30 => SizeUnit::Mebi,
10..=19 => SizeUnit::Kibi, 11..=20 => SizeUnit::Kibi,
_ => SizeUnit::Byte, _ => SizeUnit::Byte,
} }
} else { } else {
@ -319,6 +319,7 @@ fn test_human_byte_auto_unit_binary() {
fn convert(b: u64) -> String { fn convert(b: u64) -> String {
HumanByte::from(b).to_string() HumanByte::from(b).to_string()
} }
assert_eq!(convert(0), "0 B");
assert_eq!(convert(987), "987 B"); assert_eq!(convert(987), "987 B");
assert_eq!(convert(1022), "1022 B"); assert_eq!(convert(1022), "1022 B");
assert_eq!(convert(9_000), "8.789 KiB"); assert_eq!(convert(9_000), "8.789 KiB");