Skip to content

Commit

Permalink
Validate digits in input
Browse files Browse the repository at this point in the history
  • Loading branch information
spejamchr committed Mar 15, 2024
1 parent 0e48392 commit 95c05d1
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/bases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,29 @@ fn floor(num: &BigDecimal) -> BigDecimal {

fn base_digits_to_val(digits: &str, base: &BigDecimal) -> Result<BigDecimal, String> {
let mut power = base.inverse();
let valid_for_base = |char: String| {
move |n: u32| -> Result<u32, String> {
match base
.with_scale_round(0, bigdecimal::RoundingMode::Up)
.to_u32()
.map(|b| n >= b)
.unwrap_or(false)
{
true => Err(format!("Invalid digit `{char}` for base-{}", base)),
false => Ok(n),
}
}
};
rep_to_digit_exponent_pairs(digits)
.into_iter()
.rev()
.try_fold(bigdecimal::Zero::zero(), |sum: BigDecimal, (char, _)| {
power *= base;
match char.parse().or_else(|_| u32::from_str_radix(&char, 36)) {
Ok(int) => Ok(sum + int * power.clone()),
Err(_) => Err(format!("Unrecognized digit in input: {char}")),
}
char.parse()
.or_else(|_| u32::from_str_radix(&char, 36))
.map_err(|_| format!("Unrecognized digit in input: {char}"))
.and_then(valid_for_base(char))
.map(|int| sum + int * power.clone())
})
.map(|n| n.round(32).normalized())
}
Expand Down

0 comments on commit 95c05d1

Please sign in to comment.