From 9d6693a14e5107f7beed84599525c571a70e7189 Mon Sep 17 00:00:00 2001 From: Ryan Goree Date: Tue, 27 Aug 2024 03:09:29 -0500 Subject: [PATCH] Fix `BigInt` formatting negative check (#4088) --- CHANGELOG.md | 1 + crates/js-sys/src/lib.rs | 16 ++++++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80cc9e080ac..eb7a7fc082b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ * Fixed negative `BigInt` values being incorrectly formatted with two minus signs. [#4082](https://github.com/rustwasm/wasm-bindgen/pull/4082) + [#4088](https://github.com/rustwasm/wasm-bindgen/pull/4088) -------------------------------------------------------------------------------- diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 8d64a937e41..829a6246d9b 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -1391,10 +1391,10 @@ impl BigInt { /// Returns a tuple of this [`BigInt`]'s absolute value along with a /// [`bool`] indicating whether the [`BigInt`] was negative. fn abs(&self) -> (Self, bool) { - if self >= &BigInt::from(0) { - (self.clone(), false) - } else { + if self < &BigInt::from(0) { (-self, true) + } else { + (self.clone(), false) } } } @@ -1504,7 +1504,7 @@ impl fmt::Display for BigInt { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let (abs, is_neg) = self.abs(); - f.pad_integral(is_neg, "", &abs.to_string_unchecked(10)) + f.pad_integral(!is_neg, "", &abs.to_string_unchecked(10)) } } @@ -1512,7 +1512,7 @@ impl fmt::Binary for BigInt { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let (abs, is_neg) = self.abs(); - f.pad_integral(is_neg, "0b", &abs.to_string_unchecked(2)) + f.pad_integral(!is_neg, "0b", &abs.to_string_unchecked(2)) } } @@ -1520,7 +1520,7 @@ impl fmt::Octal for BigInt { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let (abs, is_neg) = self.abs(); - f.pad_integral(is_neg, "0o", &abs.to_string_unchecked(8)) + f.pad_integral(!is_neg, "0o", &abs.to_string_unchecked(8)) } } @@ -1528,7 +1528,7 @@ impl fmt::LowerHex for BigInt { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let (abs, is_neg) = self.abs(); - f.pad_integral(is_neg, "0x", &abs.to_string_unchecked(16)) + f.pad_integral(!is_neg, "0x", &abs.to_string_unchecked(16)) } } @@ -1538,7 +1538,7 @@ impl fmt::UpperHex for BigInt { let (abs, is_neg) = self.abs(); let mut s: String = abs.to_string_unchecked(16); s.make_ascii_uppercase(); - f.pad_integral(is_neg, "0x", &s) + f.pad_integral(!is_neg, "0x", &s) } }