Skip to content

Commit 673c907

Browse files
committed
adds a simple one-word to_string variant
this way you can `sort -h` on an output column on the command line
1 parent 86cdb98 commit 673c907

File tree

1 file changed

+71
-7
lines changed

1 file changed

+71
-7
lines changed

src/lib.rs

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,41 +171,70 @@ impl ByteSize {
171171

172172
#[inline(always)]
173173
pub fn to_string_as(&self, si_unit: bool) -> String {
174-
to_string(self.0, si_unit)
174+
to_string(self.0, si_unit, false)
175+
}
176+
177+
#[inline(always)]
178+
pub fn to_simple_string_as(&self, si_unit: bool, simple: bool) -> String {
179+
to_string(self.0, si_unit, simple)
175180
}
176181
}
177182

178-
pub fn to_string(bytes: u64, si_prefix: bool) -> String {
183+
pub fn to_string(bytes: u64, si_prefix: bool, simple: bool) -> String {
179184
let unit = if si_prefix { KIB } else { KB };
180185
let unit_base = if si_prefix { LN_KIB } else { LN_KB };
181186
let unit_prefix = if si_prefix {
182187
UNITS_SI.as_bytes()
183188
} else {
184189
UNITS.as_bytes()
185190
};
186-
let unit_suffix = if si_prefix { "iB" } else { "B" };
191+
192+
let unit_suffix = if simple {
193+
""
194+
} else if si_prefix {
195+
"iB"
196+
} else {
197+
"B"
198+
};
187199

188200
if bytes < unit {
189-
format!("{} B", bytes)
201+
if simple {
202+
format!("{}", bytes)
203+
} else {
204+
format!("{} B", bytes)
205+
}
190206
} else {
191207
let size = bytes as f64;
192208
let exp = match (size.ln() / unit_base) as usize {
193209
e if e == 0 => 1,
194210
e => e,
195211
};
196212

213+
println!("exp: {}", exp);
214+
println!("value: {}", (size / unit.pow(exp as u32) as f64));
215+
216+
let prefix = if simple {
217+
format!("{}", unit_prefix[exp - 1] as char).to_uppercase()
218+
} else {
219+
format!(" {}", unit_prefix[exp - 1] as char)
220+
};
221+
222+
println!("unit_prefix: {}", unit_prefix[exp - 1] as char);
223+
println!("prefix: {}", prefix);
224+
println!("suffix: {}", unit_suffix);
225+
197226
format!(
198-
"{:.1} {}{}",
227+
"{:.1}{}{}",
199228
(size / unit.pow(exp as u32) as f64),
200-
unit_prefix[exp - 1] as char,
229+
prefix,
201230
unit_suffix
202231
)
203232
}
204233
}
205234

206235
impl Display for ByteSize {
207236
fn fmt(&self, f: &mut Formatter) -> Result {
208-
write!(f, "{}", to_string(self.0, false))
237+
write!(f, "{}", to_string(self.0, false, false))
209238
}
210239
}
211240

@@ -362,4 +391,39 @@ mod tests {
362391
fn test_to_string() {
363392
assert_to_string("609.0 PB", ByteSize::pb(609), false);
364393
}
394+
395+
fn assert_simple(expected: &str, b: ByteSize, si: bool) {
396+
assert_eq!(expected.to_string(), b.to_simple_string_as(si, true));
397+
}
398+
399+
#[test]
400+
fn test_simple() {
401+
assert_simple("215", ByteSize::b(215), true);
402+
assert_simple("215", ByteSize::b(215), false);
403+
404+
assert_simple("1.0K", ByteSize::kib(1), true);
405+
assert_simple("1.0K", ByteSize::kib(1), false);
406+
407+
assert_simple("293.9K", ByteSize::kb(301), true);
408+
assert_simple("301.0K", ByteSize::kb(301), false);
409+
410+
assert_simple("1.0M", ByteSize::mib(1), true);
411+
assert_simple("1048.6K", ByteSize::mib(1), false);
412+
413+
// a bug case: https://github.com/flang-project/bytesize/issues/8
414+
assert_simple("1.9G", ByteSize::mib(1907), true);
415+
assert_simple("2.0G", ByteSize::mib(1908), false);
416+
417+
assert_simple("399.6M", ByteSize::mb(419), true);
418+
assert_simple("419.0M", ByteSize::mb(419), false);
419+
420+
assert_simple("482.4G", ByteSize::gb(518), true);
421+
assert_simple("518.0G", ByteSize::gb(518), false);
422+
423+
assert_simple("741.2T", ByteSize::tb(815), true);
424+
assert_simple("815.0T", ByteSize::tb(815), false);
425+
426+
assert_simple("540.9P", ByteSize::pb(609), true);
427+
assert_simple("609.0P", ByteSize::pb(609), false);
428+
}
365429
}

0 commit comments

Comments
 (0)