Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
wookietreiber committed Sep 5, 2018
1 parent 86cdb98 commit 6af6432
Showing 1 changed file with 68 additions and 9 deletions.
77 changes: 68 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,42 +170,66 @@ impl ByteSize {
}

#[inline(always)]
pub fn to_string_as(&self, si_unit: bool) -> String {
to_string(self.0, si_unit)
pub fn to_string_as(&self, si_unit: bool, simple: bool) -> String {
to_string(self.0, si_unit, simple)
}
}

pub fn to_string(bytes: u64, si_prefix: bool) -> String {
pub fn to_string(bytes: u64, si_prefix: bool, simple: bool) -> String {
let unit = if si_prefix { KIB } else { KB };
let unit_base = if si_prefix { LN_KIB } else { LN_KB };
let unit_prefix = if si_prefix {
UNITS_SI.as_bytes()
} else {
UNITS.as_bytes()
};
let unit_suffix = if si_prefix { "iB" } else { "B" };

let unit_suffix = if simple {
""
} else if si_prefix {
"iB"
} else {
"B"
};

if bytes < unit {
format!("{} B", bytes)
if simple {
format!("{}", bytes)
} else {
format!("{} B", bytes)
}
} else {
let size = bytes as f64;
let exp = match (size.ln() / unit_base) as usize {
e if e == 0 => 1,
e => e,
};

println!("exp: {}", exp);
println!("value: {}", (size / unit.pow(exp as u32) as f64));

let prefix = if simple {
format!("{}", unit_prefix[exp - 1] as char).to_uppercase()
} else {
format!(" {}", unit_prefix[exp - 1] as char)
};

println!("unit_prefix: {}", unit_prefix[exp - 1] as char);
println!("prefix: {}", prefix);
println!("suffix: {}", unit_suffix);

format!(
"{:.1} {}{}",
"{:.1}{}{}",
(size / unit.pow(exp as u32) as f64),
unit_prefix[exp - 1] as char,
prefix,
unit_suffix
)
}
}

impl Display for ByteSize {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "{}", to_string(self.0, false))
write!(f, "{}", to_string(self.0, false, false))
}
}

Expand Down Expand Up @@ -319,7 +343,7 @@ mod tests {
}

fn assert_to_string(expected: &str, b: ByteSize, si: bool) {
assert_eq!(expected.to_string(), b.to_string_as(si));
assert_eq!(expected.to_string(), b.to_string_as(si, false));
}

#[test]
Expand Down Expand Up @@ -362,4 +386,39 @@ mod tests {
fn test_to_string() {
assert_to_string("609.0 PB", ByteSize::pb(609), false);
}

fn assert_simple(expected: &str, b: ByteSize, si: bool) {
assert_eq!(expected.to_string(), b.to_string_as(si, true));
}

#[test]
fn test_simple() {
assert_simple("215", ByteSize::b(215), true);
assert_simple("215", ByteSize::b(215), false);

assert_simple("1.0K", ByteSize::kib(1), true);
assert_simple("1.0K", ByteSize::kib(1), false);

assert_simple("293.9K", ByteSize::kb(301), true);
assert_simple("301.0K", ByteSize::kb(301), false);

assert_simple("1.0M", ByteSize::mib(1), true);
assert_simple("1048.6K", ByteSize::mib(1), false);

// a bug case: https://github.com/flang-project/bytesize/issues/8
assert_simple("1.9G", ByteSize::mib(1907), true);
assert_simple("2.0G", ByteSize::mib(1908), false);

assert_simple("399.6M", ByteSize::mb(419), true);
assert_simple("419.0M", ByteSize::mb(419), false);

assert_simple("482.4G", ByteSize::gb(518), true);
assert_simple("518.0G", ByteSize::gb(518), false);

assert_simple("741.2T", ByteSize::tb(815), true);
assert_simple("815.0T", ByteSize::tb(815), false);

assert_simple("540.9P", ByteSize::pb(609), true);
assert_simple("609.0P", ByteSize::pb(609), false);
}
}

0 comments on commit 6af6432

Please sign in to comment.