diff --git a/src/lib.rs b/src/lib.rs index 8071e64..8fa93ff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -use std::ops::{Add, AddAssign, Sub, SubAssign}; +use std::ops::{Add, AddAssign, DivAssign, Sub, SubAssign}; use num::*; @@ -266,3 +266,16 @@ fn test_anticlockwise() { current = current.anticlockwise(true); } } + +pub fn numberlength + Copy>(n: T) -> u32 { + let divider = T::from(10); + let mut num = n; + let mut length = 0; + loop { + if num < divider { + return length + 1; + } + num /= divider; + length += 1; + } +} diff --git a/src/strings.rs b/src/strings.rs index 976ffa9..30f2e8b 100644 --- a/src/strings.rs +++ b/src/strings.rs @@ -1,51 +1,56 @@ -const MAXMUL: u32 = 100000; +use num::{pow::Pow, Integer}; +use std::ops::AddAssign; pub fn splitspace(input: &str) -> (&str, &str) { - let mut output = input.clone().split_ascii_whitespace(); - (output.next().unwrap(), output.next().unwrap()) + let mut output = input.split_ascii_whitespace(); + (output.next().unwrap().trim(), output.next().unwrap().trim()) } -pub fn parsenumber(input: &str) -> u32 { - let mut output = 0; - let mut mul = 1; +pub fn parsenumber + Pow + AddAssign + Copy>( + input: &str, +) -> T { + const MAX_POWER: u32 = 32; + let base = T::from(10); + let mut output: T = T::from(0); + let mut mul: u32 = 0; for c in input.chars().rev() { - let i: u32 = match c { - '1' => 1, - '2' => 2, - '3' => 3, - '4' => 4, - '5' => 5, - '6' => 6, - '7' => 7, - '8' => 8, - '9' => 9, - '0' => 0, - _ => 0, + let i: T = match c { + '1' => T::from(1), + '2' => T::from(2), + '3' => T::from(3), + '4' => T::from(4), + '5' => T::from(5), + '6' => T::from(6), + '7' => T::from(7), + '8' => T::from(8), + '9' => T::from(9), + '0' => T::from(0), + _ => unreachable!(), }; - if mul > MAXMUL { + if mul > MAX_POWER { return output; } - output += i * mul; - mul *= 10; + output += i * base.pow(mul); + mul += 1; } output } -pub fn get_numbers(input: &str) -> Vec { +pub fn get_numbers>(input: &str) -> Vec { let mut numbers = Vec::new(); for char in input.chars() { match char { - '1' => numbers.push(1), - '2' => numbers.push(2), - '3' => numbers.push(3), - '4' => numbers.push(4), - '5' => numbers.push(5), - '6' => numbers.push(6), - '7' => numbers.push(7), - '8' => numbers.push(8), - '9' => numbers.push(9), - '0' => numbers.push(0), - _ => {} + '1' => numbers.push(T::from(1)), + '2' => numbers.push(T::from(2)), + '3' => numbers.push(T::from(3)), + '4' => numbers.push(T::from(4)), + '5' => numbers.push(T::from(5)), + '6' => numbers.push(T::from(6)), + '7' => numbers.push(T::from(7)), + '8' => numbers.push(T::from(8)), + '9' => numbers.push(T::from(9)), + '0' => numbers.push(T::from(0)), + _ => unimplemented!(), } } numbers @@ -163,5 +168,10 @@ pub fn line_to_char(line: &str) -> Vec { } pub fn convert_to_array T, const S: char>(input: &str, func: F) -> Vec { - input.split(S).map(func).collect() + input + .trim() + .split(S) + .map(str::trim_ascii) + .map(func) + .collect() }