Repository navigation
Fix human-readable size rounding at unit boundaries #21136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,6 @@ license = { workspace = true } | |
|
|
||
| [lib] | ||
| doctest = false | ||
| test = false | ||
|
|
||
| [lints] | ||
| workspace = true | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| use console::{Key, Term, measure_text_width, style}; | ||
| use std::{cmp::Ordering, iter}; | ||
| use std::{cmp::Ordering, fmt, iter}; | ||
|
|
||
| /// Prompt the user for confirmation in the given [`Term`]. | ||
| /// | ||
|
|
@@ -297,18 +297,71 @@ pub fn input(prompt: &str, term: &Term) -> std::io::Result<String> { | |
| Ok(input) | ||
| } | ||
|
|
||
| /// Formats a number of bytes into a human readable SI-prefixed size (binary units). | ||
| /// | ||
| /// Returns a tuple of `(quantity, units)`. | ||
| #[allow( | ||
| clippy::cast_possible_truncation, | ||
| clippy::cast_possible_wrap, | ||
| clippy::cast_precision_loss, | ||
| clippy::cast_sign_loss | ||
| )] | ||
| pub fn human_readable_bytes(bytes: u64) -> (f32, &'static str) { | ||
| /// Formats a number of bytes into a human readable IEC-prefixed size (binary units). | ||
| #[expect(clippy::cast_precision_loss)] | ||
| pub fn human_readable_bytes(bytes: u64) -> impl fmt::Display { | ||
| const UNITS: [&str; 7] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"]; | ||
| let bytes_f32 = bytes as f32; | ||
| let i = ((bytes_f32.log2() / 10.0) as usize).min(UNITS.len() - 1); | ||
| (bytes_f32 / 1024_f32.powi(i as i32), UNITS[i]) | ||
|
|
||
| fmt::from_fn(move |formatter| { | ||
| let rounding_margin = formatter | ||
| .precision() | ||
| .and_then(|precision| i32::try_from(precision).ok()) | ||
| .map_or(0.0, |precision| 0.5 / 10_f64.powi(precision)); | ||
| let mut quantity = bytes as f64; | ||
| let [mut unit, units @ ..] = UNITS; | ||
|
|
||
| for next_unit in units { | ||
|
Comment on lines
+311
to
+313
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a neat trick |
||
| if 1024.0 - quantity > rounding_margin { | ||
| break; | ||
| } | ||
| quantity /= 1024.0; | ||
| unit = next_unit; | ||
| } | ||
|
|
||
| fmt::Display::fmt(&quantity, formatter)?; | ||
| formatter.write_str(unit) | ||
| }) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::human_readable_bytes; | ||
|
|
||
| #[test] | ||
| fn human_readable_sizes() { | ||
| const MIB: u64 = 1024_u64.pow(2); | ||
| const PIB: u64 = 1024_u64.pow(5); | ||
|
|
||
| assert_eq!(format!("{}", human_readable_bytes(0)), "0B"); | ||
| assert_eq!(format!("{}", human_readable_bytes(1024)), "1KiB"); | ||
| assert_eq!(format!("{}", human_readable_bytes(1536)), "1.5KiB"); | ||
| assert_eq!(format!("{}", human_readable_bytes(u64::MAX)), "16EiB"); | ||
| assert_eq!( | ||
| format!("{}", human_readable_bytes(MIB - 51)), | ||
| "1023.9501953125KiB" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one doesn't look good, the exact precision looks misplaced, a user would be satisfied with
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The format doesn't specify a precision so it falls back to normal float formatting. I thought about it and I felt it was the least surprising behaviour given it's consistent with how floats are normally formatted and all the cases where we use this do specify a precision. In this case, with
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My confusion here may be that the function is called |
||
| ); | ||
|
|
||
| assert_eq!(format!("{:.2}", human_readable_bytes(1023)), "1023.00B"); | ||
| assert_eq!(format!("{:.0}", human_readable_bytes(MIB - 513)), "1023KiB"); | ||
| assert_eq!(format!("{:.0}", human_readable_bytes(MIB - 512)), "1MiB"); | ||
| assert_eq!( | ||
| format!("{:.1}", human_readable_bytes(MIB - 52)), | ||
| "1023.9KiB" | ||
| ); | ||
| assert_eq!(format!("{:.1}", human_readable_bytes(MIB - 51)), "1.0MiB"); | ||
| assert_eq!( | ||
| format!("{:.2}", human_readable_bytes(MIB - 51)), | ||
| "1023.95KiB" | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| format!("{:.10}", human_readable_bytes(PIB - 55)), | ||
| "1023.9999999999TiB" | ||
| ); | ||
| // Specifically trigger a floating point imprecision corner case. | ||
| assert_eq!( | ||
| format!("{:.1}", human_readable_bytes(PIB * 1024 - PIB / 20 - 1)), | ||
| "1.0EiB" | ||
| ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh we're doing 1024 instead of proper powers of 10 :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well we are specifying KiB, MiB, GiB etc. not kB, MB, GB...
I agree that 1024 based units are a convention strongly tied to technical details that users would find irrelevant, but it's also the industry standard practice at this point.