format.rs 634 B

123456789101112131415161718192021222324
  1. static METRIC_PREFIXES: &'static [&'static str] = &[
  2. "", "K", "M", "G", "T", "P", "E", "Z", "Y"
  3. ];
  4. static IEC_PREFIXES: &'static [&'static str] = &[
  5. "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"
  6. ];
  7. fn formatBytes(mut amount: u64, kilo: u64, prefixes: &[&str]) -> StrBuf {
  8. let mut prefix = 0;
  9. while amount > kilo {
  10. amount /= kilo;
  11. prefix += 1;
  12. }
  13. format!("{}{}", amount, prefixes[prefix])
  14. }
  15. pub fn formatBinaryBytes(amount: u64) -> StrBuf {
  16. formatBytes(amount, 1024, IEC_PREFIXES)
  17. }
  18. pub fn formatDecimalBytes(amount: u64) -> StrBuf {
  19. formatBytes(amount, 1000, METRIC_PREFIXES)
  20. }