mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-06 11:35:45 +00:00
fix: remove extra allocation in Bytes write
This commit is contained in:
parent
c626d3c80b
commit
06c5a944b4
@ -109,17 +109,18 @@ impl std::fmt::Display for Bytes {
|
|||||||
|
|
||||||
debug_assert!(num >= 0.0);
|
debug_assert!(num >= 0.0);
|
||||||
if num < 1_f64 {
|
if num < 1_f64 {
|
||||||
let num_pretty = format!("{:.2}", num);
|
return write!(f, "{:>6.2} B", num);
|
||||||
return write!(f, "{:>6} B", num_pretty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let delimiter = 1000_f64;
|
let delimiter = 1000_f64;
|
||||||
let exponent = cmp::min((num.ln() / 6.90775).floor() as i32, 4);
|
let exponent = cmp::min((num.ln() / 6.90775).floor() as i32, 4);
|
||||||
|
|
||||||
let num_pretty = format!("{:.2}", (num / delimiter.powi(exponent)));
|
write!(
|
||||||
let unit_pretty = format!("{}B", Bytes::UNIT_PREFIXES[exponent as usize]);
|
f,
|
||||||
|
"{:>6.2} {:>2}B",
|
||||||
write!(f, "{:>6} {:>3}", num_pretty, unit_pretty,)
|
num / delimiter.powi(exponent),
|
||||||
|
Bytes::UNIT_PREFIXES[exponent as usize],
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user