-
Notifications
You must be signed in to change notification settings - Fork 31
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
fix inverted SI / IEC conditions #69
Merged
+81
−44
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
05bcfc4
fix: swap values of `LN_KIB` & `LN_KB`
Andrew15-5 8e7c57d
fix: invert SI/IEC conditions
Andrew15-5 d9eef51
fix: format with IEC (binary) by default
robjtede 3ecd475
docs: doc units_* statics
robjtede c72ffa9
refactor: move statics to consts
robjtede 9fae90c
refactor: introduce format enum
robjtede ee144fa
test: use to_string_format in tests
robjtede 80881ac
chore: expand debug impl
robjtede 81d885f
fix: increase precision of LN_ constants
robjtede File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -23,8 +23,8 @@ | |
//! ``` | ||
//! use bytesize::ByteSize; | ||
//! | ||
//! assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(true)); | ||
//! assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(false)); | ||
//! assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(false)); | ||
//! assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(true)); | ||
//! ``` | ||
|
||
mod parse; | ||
|
@@ -58,10 +58,28 @@ pub const TIB: u64 = 1_099_511_627_776; | |
/// bytes size for 1 pebibyte | ||
pub const PIB: u64 = 1_125_899_906_842_624; | ||
|
||
static UNITS: &str = "KMGTPE"; | ||
static UNITS_SI: &str = "KMGTPE"; | ||
static LN_KB: f64 = 6.931471806; // ln 1024 | ||
static LN_KIB: f64 = 6.907755279; // ln 1000 | ||
/// IEC (binary) units. | ||
/// | ||
/// See <https://en.wikipedia.org/wiki/Kilobyte>. | ||
const UNITS_IEC: &str = "KMGTPE"; | ||
|
||
/// SI (decimal) units. | ||
/// | ||
/// See <https://en.wikipedia.org/wiki/Kilobyte>. | ||
const UNITS_SI: &str = "kMGTPE"; | ||
|
||
/// `ln(1024) ~= 6.931` | ||
const LN_KIB: f64 = 6.931_471_805_599_453; | ||
|
||
/// `ln(1000) ~= 6.908` | ||
const LN_KB: f64 = 6.907_755_278_982_137; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub enum Format { | ||
#[default] | ||
IEC, | ||
SI, | ||
} | ||
Comment on lines
+77
to
+82
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 enum may or may not stay public (i have an idea to present in a follow up PR) but it really makes this PR easier to reason about |
||
|
||
pub fn kb<V: Into<u64>>(size: V) -> u64 { | ||
size.into() * KB | ||
|
@@ -175,15 +193,28 @@ impl ByteSize { | |
} | ||
} | ||
|
||
pub fn to_string(bytes: u64, si_prefix: 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() | ||
pub fn to_string(bytes: u64, si_unit: bool) -> String { | ||
to_string_format(bytes, if si_unit { Format::SI } else { Format::IEC }) | ||
} | ||
|
||
pub fn to_string_format(bytes: u64, format: Format) -> String { | ||
let unit = match format { | ||
Format::IEC => KIB, | ||
Format::SI => KB, | ||
}; | ||
let unit_base = match format { | ||
Format::IEC => LN_KIB, | ||
Format::SI => LN_KB, | ||
}; | ||
|
||
let unit_prefix = match format { | ||
Format::IEC => UNITS_IEC.as_bytes(), | ||
Format::SI => UNITS_SI.as_bytes(), | ||
}; | ||
let unit_suffix = match format { | ||
Format::IEC => "iB", | ||
Format::SI => "B", | ||
}; | ||
let unit_suffix = if si_prefix { "iB" } else { "B" }; | ||
|
||
if bytes < unit { | ||
format!("{} B", bytes) | ||
|
@@ -205,13 +236,13 @@ pub fn to_string(bytes: u64, si_prefix: bool) -> String { | |
|
||
impl Display for ByteSize { | ||
fn fmt(&self, f: &mut Formatter) -> fmt::Result { | ||
f.pad(&to_string(self.0, true)) | ||
f.pad(&to_string_format(self.0, Format::IEC)) | ||
} | ||
} | ||
|
||
impl Debug for ByteSize { | ||
fn fmt(&self, f: &mut Formatter) -> fmt::Result { | ||
write!(f, "{}", self) | ||
<Self as Display>::fmt(self, f) | ||
} | ||
} | ||
|
||
|
@@ -395,6 +426,7 @@ mod tests { | |
assert!(ByteSize::b(0) < ByteSize::tib(1)); | ||
} | ||
|
||
#[track_caller] | ||
fn assert_display(expected: &str, b: ByteSize) { | ||
assert_eq!(expected, format!("{}", b)); | ||
} | ||
|
@@ -422,39 +454,39 @@ mod tests { | |
assert_eq!("|--357 B---|", format!("|{:-^10}|", ByteSize(357))); | ||
} | ||
|
||
fn assert_to_string(expected: &str, b: ByteSize, si: bool) { | ||
assert_eq!(expected.to_string(), b.to_string_as(si)); | ||
#[track_caller] | ||
fn assert_to_string(expected: &str, b: ByteSize, format: Format) { | ||
assert_eq!(expected.to_string(), to_string_format(b.0, format)); | ||
} | ||
|
||
#[test] | ||
fn test_to_string_as() { | ||
assert_to_string("215 B", ByteSize::b(215), true); | ||
assert_to_string("215 B", ByteSize::b(215), false); | ||
assert_to_string("215 B", ByteSize::b(215), Format::IEC); | ||
assert_to_string("215 B", ByteSize::b(215), Format::SI); | ||
|
||
assert_to_string("1.0 KiB", ByteSize::kib(1), true); | ||
assert_to_string("1.0 KB", ByteSize::kib(1), false); | ||
assert_to_string("1.0 KiB", ByteSize::kib(1), Format::IEC); | ||
assert_to_string("1.0 kB", ByteSize::kib(1), Format::SI); | ||
|
||
assert_to_string("293.9 KiB", ByteSize::kb(301), true); | ||
assert_to_string("301.0 KB", ByteSize::kb(301), false); | ||
assert_to_string("293.9 KiB", ByteSize::kb(301), Format::IEC); | ||
assert_to_string("301.0 kB", ByteSize::kb(301), Format::SI); | ||
|
||
assert_to_string("1.0 MiB", ByteSize::mib(1), true); | ||
assert_to_string("1048.6 KB", ByteSize::mib(1), false); | ||
assert_to_string("1.0 MiB", ByteSize::mib(1), Format::IEC); | ||
assert_to_string("1.0 MB", ByteSize::mib(1), Format::SI); | ||
|
||
// a bug case: https://github.com/flang-project/bytesize/issues/8 | ||
assert_to_string("1.9 GiB", ByteSize::mib(1907), true); | ||
assert_to_string("2.0 GB", ByteSize::mib(1908), false); | ||
assert_to_string("1.9 GiB", ByteSize::mib(1907), Format::IEC); | ||
assert_to_string("2.0 GB", ByteSize::mib(1908), Format::SI); | ||
|
||
assert_to_string("399.6 MiB", ByteSize::mb(419), true); | ||
assert_to_string("419.0 MB", ByteSize::mb(419), false); | ||
assert_to_string("399.6 MiB", ByteSize::mb(419), Format::IEC); | ||
assert_to_string("419.0 MB", ByteSize::mb(419), Format::SI); | ||
|
||
assert_to_string("482.4 GiB", ByteSize::gb(518), true); | ||
assert_to_string("518.0 GB", ByteSize::gb(518), false); | ||
assert_to_string("482.4 GiB", ByteSize::gb(518), Format::IEC); | ||
assert_to_string("518.0 GB", ByteSize::gb(518), Format::SI); | ||
|
||
assert_to_string("741.2 TiB", ByteSize::tb(815), true); | ||
assert_to_string("815.0 TB", ByteSize::tb(815), false); | ||
assert_to_string("741.2 TiB", ByteSize::tb(815), Format::IEC); | ||
assert_to_string("815.0 TB", ByteSize::tb(815), Format::SI); | ||
|
||
assert_to_string("540.9 PiB", ByteSize::pb(609), true); | ||
assert_to_string("609.0 PB", ByteSize::pb(609), false); | ||
assert_to_string("540.9 PiB", ByteSize::pb(609), Format::IEC); | ||
assert_to_string("609.0 PB", ByteSize::pb(609), Format::SI); | ||
} | ||
|
||
#[test] | ||
|
@@ -464,6 +496,6 @@ mod tests { | |
|
||
#[test] | ||
fn test_to_string() { | ||
assert_to_string("609.0 PB", ByteSize::pb(609), false); | ||
assert_to_string("609.0 PB", ByteSize::pb(609), Format::SI); | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The rust doc also needs to be updated.
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.
it will be, this fn still exists for now