Skip to content
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 BigInt formatting negative check #4088

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

--------------------------------------------------------------------------------

Expand Down
16 changes: 8 additions & 8 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines -1395 to +1397
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I swapped these to make it more readable. Now if the condition is true, then the bool is true.

}
}
}
Expand Down Expand Up @@ -1504,31 +1504,31 @@ 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))
}
}

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))
}
}

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))
}
}

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))
}
}

Expand All @@ -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)
}
}

Expand Down