Refactor & cleanup

cli
E. Almqvist 2 years ago
parent ac2352de10
commit 99865e2efe
  1. 12
      src/ipv4.rs
  2. 2
      src/main.rs
  3. 9
      src/util.rs

@ -22,7 +22,17 @@ impl IPv4 {
let mut base = Convert::new(10, 256); let mut base = Convert::new(10, 256);
let id_vec = util::number_to_vec(id); // push all digits into a vec let id_vec = util::number_to_vec(id); // push all digits into a vec
let ip = base.convert::<u8, u16>(&id_vec); let mut ip = base.convert::<u8, u16>(&id_vec);
// In case we are missing some digits
if ip.len() < 4 {
for i in 0..(4-ip.len()) {
ip.insert(0, 0);
}
}
// Reverse it so that we start from the top
ip = ip.into_iter().rev().collect();
Self { id, ip } Self { id, ip }
} }

@ -14,6 +14,8 @@ fn main() {
// scanner::start_scan(100); // scanner::start_scan(100);
// permutations::ipv4(None); // permutations::ipv4(None);
let ip = IPv4::new((u32::max_value() - 256 - 255) as u64);
println!("{:?}", ip);
let ip = IPv4::new(256); let ip = IPv4::new(256);
println!("{:?}", ip); println!("{:?}", ip);
} }

@ -1,6 +1,5 @@
/* /*
uint ulong_len(ulong n) { // get the digit length of a number uint ulong_len(ulong n) { // get the nth_digit length of a number
int len = 0; int len = 0;
while (n != 0) { while (n != 0) {
n = n / 10; n = n / 10;
@ -10,7 +9,7 @@ uint ulong_len(ulong n) { // get the digit length of a number
} }
*/ */
fn digit(num: u64, idx: u32) -> u8 { fn nth_digit(num: u64, idx: u32) -> u8 {
// ((num % (10**(idx+1))) - (num % (10**idx)))/(10**idx) // ((num % (10**(idx+1))) - (num % (10**idx)))/(10**idx)
(((num % (10_u64.pow(idx+1))) - (num % (10_u64.pow(idx))))/(10_u64.pow(idx))) as u8 (((num % (10_u64.pow(idx+1))) - (num % (10_u64.pow(idx))))/(10_u64.pow(idx))) as u8
} }
@ -35,11 +34,9 @@ pub fn number_to_vec(num: u64) -> Vec<u8> {
let len = numlen(num); let len = numlen(num);
for idx in 0..len { for idx in 0..len {
let digit = digit(num, idx as u32); let digit = nth_digit(num, idx as u32);
out.push(digit); out.push(digit);
} }
println!("{:?}", out);
out out
} }

Loading…
Cancel
Save