From 30c89af9acc87a528adff2147f669e700cdff1fa Mon Sep 17 00:00:00 2001 From: Nicolas Boichat Date: Fri, 21 Mar 2025 20:38:06 +0100 Subject: [PATCH] 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). --- src/uucore/src/lib/features/format/num_parser.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uucore/src/lib/features/format/num_parser.rs b/src/uucore/src/lib/features/format/num_parser.rs index 645e6c2bf..f11a75cdb 100644 --- a/src/uucore/src/lib/features/format/num_parser.rs +++ b/src/uucore/src/lib/features/format/num_parser.rs @@ -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)