diff --git a/src/main.rs b/src/main.rs index e27a4bb..61b0195 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ //unused_imports, // TODO: rm )] +mod util; mod scanner; mod permutations; diff --git a/src/permutations.rs b/src/permutations.rs index dfa5b4b..db7cc35 100644 --- a/src/permutations.rs +++ b/src/permutations.rs @@ -8,19 +8,8 @@ let i = 0 .. u32:max_value() # Convert each i to base 256 and we get all the ipv4 addresses # This is waaaay better than a stupid loop - - */ -// TODO: make a better implementation of this function -fn digits(n: usize) -> impl Iterator { - n.to_string() - .chars() - .map(|digit| digit.to_digit(10).unwrap()) - .collect::>() - .into_iter() -} - struct IPv4 { id: u32, ip: Vec diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..3e6c9a6 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,37 @@ + +/* +uint ulong_len(ulong n) { // get the digit length of a number + int len = 0; + while (n != 0) { + n = n / 10; + ++len; + } + return len; +} +*/ + +fn digit(num: u32, idx: u32) -> u8 { + // ((num % (10**(idx+1))) - (num % (10**idx)))/(10**idx) + (((num % (10_u32.pow(idx+1))) - (num % (10_u32.pow(idx))))/(10_u32.pow(idx))) as u8 +} + +fn numlen(num: u32) -> u8 { + let len: u8 = 0; + while num != 0 { + num = num / 10; + len += 1; + } + len +} + +fn get_digits(num: u32) -> Vec { + let out: Vec = Vec::new(); + + let len = numlen(num); + + for idx in 0..len { + out.push(digit(num, idx as u32)); + } + + out +}