mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
uucore: format: Fix i64::MIN printing
-i64::MIN overflows i64, so cast to i128 first.
This commit is contained in:
parent
5bea6ff013
commit
9872263a96
1 changed files with 6 additions and 2 deletions
|
@ -80,10 +80,12 @@ pub struct SignedInt {
|
|||
|
||||
impl Formatter<i64> for SignedInt {
|
||||
fn fmt(&self, writer: impl Write, x: i64) -> std::io::Result<()> {
|
||||
// -i64::MIN is actually 1 larger than i64::MAX, so we need to cast to i128 first.
|
||||
let abs = (x as i128).abs();
|
||||
let s = if self.precision > 0 {
|
||||
format!("{:0>width$}", x.abs(), width = self.precision)
|
||||
format!("{:0>width$}", abs, width = self.precision)
|
||||
} else {
|
||||
x.abs().to_string()
|
||||
abs.to_string()
|
||||
};
|
||||
|
||||
let sign_indicator = get_sign_indicator(self.positive_sign, x.is_negative());
|
||||
|
@ -1046,6 +1048,8 @@ mod test {
|
|||
let format = Format::<SignedInt, i64>::parse("%d").unwrap();
|
||||
assert_eq!(fmt(&format, 123i64), "123");
|
||||
assert_eq!(fmt(&format, -123i64), "-123");
|
||||
assert_eq!(fmt(&format, i64::MAX), "9223372036854775807");
|
||||
assert_eq!(fmt(&format, i64::MIN), "-9223372036854775808");
|
||||
|
||||
let format = Format::<SignedInt, i64>::parse("%i").unwrap();
|
||||
assert_eq!(fmt(&format, 123i64), "123");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue