bigint: use a simple let-Some-pop to truncate zeros
This commit is contained in:
parent
c310e5da8e
commit
7781256041
|
@ -819,12 +819,15 @@ fn to_str_radix_reversed(u: &BigUint, radix: u32) -> Vec<u8> {
|
||||||
} else {
|
} else {
|
||||||
let mut res = Vec::new();
|
let mut res = Vec::new();
|
||||||
let mut digits = u.data.to_vec();
|
let mut digits = u.data.to_vec();
|
||||||
let mut n_digits = digits.len();
|
|
||||||
|
|
||||||
while n_digits != 0 {
|
while !digits.is_empty() {
|
||||||
let rem = div_rem_in_place(&mut digits[..n_digits], radix);
|
let rem = div_rem_in_place(&mut digits, radix);
|
||||||
res.push(to_digit(rem as u8));
|
res.push(to_digit(rem as u8));
|
||||||
n_digits = count_non_zero(&digits[..n_digits]);
|
|
||||||
|
// If we finished the most significant digit, drop it
|
||||||
|
if let Some(&0) = digits.last() {
|
||||||
|
digits.pop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
res
|
res
|
||||||
|
@ -843,20 +846,6 @@ fn div_rem_in_place(digits: &mut [BigDigit], divisor: BigDigit) -> BigDigit {
|
||||||
rem
|
rem
|
||||||
}
|
}
|
||||||
|
|
||||||
fn count_non_zero(digits: &[u32]) -> usize {
|
|
||||||
let mut n = digits.len();
|
|
||||||
|
|
||||||
for &i in digits.iter().rev() {
|
|
||||||
if i == 0 {
|
|
||||||
n -= 1;
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
n
|
|
||||||
}
|
|
||||||
|
|
||||||
fn full_div_rem(a: BigDigit, b: BigDigit, borrow: BigDigit) -> (BigDigit, BigDigit) {
|
fn full_div_rem(a: BigDigit, b: BigDigit, borrow: BigDigit) -> (BigDigit, BigDigit) {
|
||||||
let lo = a as DoubleBigDigit;
|
let lo = a as DoubleBigDigit;
|
||||||
let hi = borrow as DoubleBigDigit;
|
let hi = borrow as DoubleBigDigit;
|
||||||
|
@ -881,8 +870,9 @@ impl BigUint {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new(mut digits: Vec<BigDigit>) -> BigUint {
|
pub fn new(mut digits: Vec<BigDigit>) -> BigUint {
|
||||||
// omit trailing zeros
|
// omit trailing zeros
|
||||||
let new_len = digits.iter().rposition(|n| *n != 0).map_or(0, |p| p + 1);
|
while let Some(&0) = digits.last() {
|
||||||
digits.truncate(new_len);
|
digits.pop();
|
||||||
|
}
|
||||||
BigUint { data: digits }
|
BigUint { data: digits }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -953,8 +943,8 @@ impl BigUint {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(index) = result.iter().rposition(|x| *x != 0) {
|
while let Some(&0) = result.last() {
|
||||||
result.truncate(index + 1);
|
result.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
if result.is_empty() {
|
if result.is_empty() {
|
||||||
|
|
Loading…
Reference in New Issue