Skip to content

Commit c18f503

Browse files
committed
Minor refactoring
Extracting computation of the "extended" absolute value to a separate function.
1 parent f3fc049 commit c18f503

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/util/print_dec.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,7 @@ pub unsafe fn write<W: io::Write>(wr: &mut W, positive: bool, mut n: u64, expone
7474
)
7575
);
7676
} else if exponent < 0 {
77-
let mut e = match exponent.checked_neg() {
78-
Some(neg) => neg as u16,
79-
None => i16::max_value() as u16 + 1u16
80-
};
77+
let mut e = safe_abs(exponent);
8178

8279
// Decimal number with a fraction that's fully printable
8380
if e < 18 {
@@ -218,3 +215,11 @@ pub unsafe fn write<W: io::Write>(wr: &mut W, positive: bool, mut n: u64, expone
218215
try!(wr.write_all(b"e"));
219216
write(wr, true, e, 0)
220217
}
218+
219+
fn safe_abs(x : i16) -> u16 {
220+
if let Some(y) = x.checked_abs() {
221+
y as u16
222+
} else {
223+
i16::max_value() as u16 + 1u16
224+
}
225+
}

0 commit comments

Comments
 (0)