From 19fa9bf6c7cf01221e91e934c7ac2017104e21aa Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 11 Sep 2020 15:57:17 -0400 Subject: [PATCH] Start implementing parsing numbers from strings --- src/bigint.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/bigint.rs b/src/bigint.rs index 8472bb3..9145e2e 100644 --- a/src/bigint.rs +++ b/src/bigint.rs @@ -128,7 +128,28 @@ impl BigInt { /// Convert a `&str` or a `String` representing a number in the specified radix to a Bigint. /// /// For radix 10, use the `from` associated function instead. + /// + /// Radix must be between 1 and 36, inclusive, with radix higher + /// than 11 represented by A-Z + /// + /// Only alphanumeric characters are considered, so exponents and + /// other forms are not parsed pub fn from_str_radix(s: T, radix: usize) -> BigInt { + let input = s.to_string(); + + // Convert each digit to it's decimal representation + let mut raw_digits: Vec = Vec::with_capacity(input.len()); + for maybe_digit in input.chars() { + match maybe_digit.to_digit(radix as u32) { + Some(d) => raw_digits.push(d as usize), + None => continue, + } + } + + // Calculate the decimal value by calculating the value + // of each place value + + // In base 1, number of place values = total value todo!(); }