-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a function to humanize bytes. Remove dependency. (#12)
- Loading branch information
Showing
6 changed files
with
54 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#![allow(clippy::cast_precision_loss)] | ||
|
||
const UNITS: [&str; 6] = ["B", "KB", "MB", "GB", "TB", "PB"]; | ||
|
||
const BASE: u64 = 1000; | ||
|
||
pub fn humanize(bytes: u64) -> String { | ||
if bytes < BASE { | ||
return format!("{bytes} B"); | ||
} | ||
let exponent = bytes.ilog10() / BASE.ilog10(); | ||
let unit = UNITS[exponent as usize]; | ||
let value = bytes as f64 / BASE.pow(exponent) as f64; | ||
let precision = match unit { | ||
"KB" => 0, | ||
"MB" => 1, | ||
_ => 2, | ||
}; | ||
format!("{value:.precision$} {unit}") | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::humanize; | ||
|
||
#[test] | ||
fn test_humanize() { | ||
assert_eq!(humanize(0), "0 B"); | ||
assert_eq!(humanize(256), "256 B"); | ||
assert_eq!(humanize(512), "512 B"); | ||
assert_eq!(humanize(1_000), "1 KB"); | ||
assert_eq!(humanize(2_650), "3 KB"); | ||
assert_eq!(humanize(737_525), "738 KB"); | ||
assert_eq!(humanize(1_000_000), "1.0 MB"); | ||
assert_eq!(humanize(1_240_000), "1.2 MB"); | ||
assert_eq!(humanize(1_250_000), "1.2 MB"); | ||
assert_eq!(humanize(1_260_000), "1.3 MB"); | ||
assert_eq!(humanize(10_525_000), "10.5 MB"); | ||
assert_eq!(humanize(2_886_000_000), "2.89 GB"); | ||
assert_eq!(humanize(200_500_150_001), "200.50 GB"); | ||
assert_eq!(humanize(50_000_000_000_000), "50.00 TB"); | ||
assert_eq!(humanize(1_421_000_000_000_000), "1.42 PB"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod bytes; | ||
mod files; | ||
|
||
use std::{collections::BTreeMap, io}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters