1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

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).
This commit is contained in:
Nicolas Boichat 2025-03-21 20:38:06 +01:00
parent bd68eb8beb
commit 30c89af9ac

View file

@ -268,7 +268,7 @@ fn parse(
// Parse the integral part of the number
let mut chars = rest.chars().enumerate().fuse().peekable();
let mut digits = BigUint::zero();
let mut scale = 0i64;
let mut scale = 0u64;
let mut exponent = 0i64;
while let Some(d) = chars.peek().and_then(|&(_, c)| base.digit(c)) {
chars.next();
@ -335,7 +335,7 @@ fn parse(
let bd = if scale == 0 && exponent == 0 {
BigDecimal::from_bigint(signed_digits, 0)
} else if base == Base::Decimal {
BigDecimal::from_bigint(signed_digits, scale - exponent)
BigDecimal::from_bigint(signed_digits, scale as i64 - exponent)
} else if base == Base::Hexadecimal {
// Base is 16, init at scale 0 then divide by base**scale.
let bd = BigDecimal::from_bigint(signed_digits, 0)