Refactor & opti

cli
E. Almqvist 2 years ago
parent 4ed00b2039
commit 6625d8869a
  1. 1
      src/main.rs
  2. 11
      src/permutations.rs
  3. 37
      src/util.rs

@ -4,6 +4,7 @@
//unused_imports, // TODO: rm //unused_imports, // TODO: rm
)] )]
mod util;
mod scanner; mod scanner;
mod permutations; mod permutations;

@ -8,19 +8,8 @@ let i = 0 .. u32:max_value()
# Convert each i to base 256 and we get all the ipv4 addresses # Convert each i to base 256 and we get all the ipv4 addresses
# This is waaaay better than a stupid loop # This is waaaay better than a stupid loop
*/ */
// TODO: make a better implementation of this function
fn digits(n: usize) -> impl Iterator<Item = u32> {
n.to_string()
.chars()
.map(|digit| digit.to_digit(10).unwrap())
.collect::<Vec<_>>()
.into_iter()
}
struct IPv4 { struct IPv4 {
id: u32, id: u32,
ip: Vec<u8> ip: Vec<u8>

@ -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<u8> {
let out: Vec<u8> = Vec::new();
let len = numlen(num);
for idx in 0..len {
out.push(digit(num, idx as u32));
}
out
}
Loading…
Cancel
Save