Skip to content

Commit 9c71ab0

Browse files
committed
uucore: format: num_parser: Make it clear that scale can only be positive
After scratching my head a bit about why the hexadecimal code works, seems better to do make scale an u64 to clarify. Note that this may u64 may exceed i64 capacity, but that can only happen if the number of digits provided > 2**63 (impossible).
1 parent cd02d42 commit 9c71ab0

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/uucore/src/lib/features/format/num_parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ fn parse(
266266
// Parse the integral part of the number
267267
let mut chars = rest.chars().enumerate().fuse().peekable();
268268
let mut digits = BigUint::zero();
269-
let mut scale = 0i64;
269+
let mut scale = 0u64;
270270
let mut exponent = 0i64;
271271
while let Some(d) = chars.peek().and_then(|&(_, c)| base.digit(c)) {
272272
chars.next();
@@ -333,7 +333,7 @@ fn parse(
333333
let bd = if scale == 0 && exponent == 0 {
334334
BigDecimal::from_bigint(signed_digits, 0)
335335
} else if base == Base::Decimal {
336-
BigDecimal::from_bigint(signed_digits, scale - exponent)
336+
BigDecimal::from_bigint(signed_digits, scale as i64 - exponent)
337337
} else if base == Base::Hexadecimal {
338338
// Base is 16, init at scale 0 then divide by base**scale.
339339
let bd = BigDecimal::from_bigint(signed_digits, 0)

0 commit comments

Comments
 (0)