Improve the write_u32 method
parent
8705519c0b
commit
0636253f06
|
@ -158,37 +158,22 @@ impl<
|
||||||
self.write_str(core::str::from_utf8(&buff[..=num_len]).unwrap());
|
self.write_str(core::str::from_utf8(&buff[..=num_len]).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_u32(&mut self, num: u32) {
|
pub fn write_u32(&mut self, mut num: u32) {
|
||||||
// This function is for debugging purposes only, it does not show
|
if num == 0 {
|
||||||
// The 0's to the right of the number
|
self.write_char('0');
|
||||||
// The max number in an u32 is: 4,294,967,295
|
return;
|
||||||
let mut buff = [0; 10];
|
}
|
||||||
let len = num_chars(num, &mut buff);
|
|
||||||
self.write_str(&core::str::from_utf8(&buff[..len]).unwrap());
|
let mut buff = [0u8; 10];
|
||||||
|
let num_len = num.ilog10() as usize;
|
||||||
|
for i in (0..=num_len).rev() {
|
||||||
|
buff[i] = (num % 10) as u8 + '0' as u8;
|
||||||
|
num /= 10;
|
||||||
|
}
|
||||||
|
self.write_str(core::str::from_utf8(&buff[..=num_len]).unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn last_bit(n: u8) -> bool {
|
fn last_bit(n: u8) -> bool {
|
||||||
(n & 1) != 0
|
(n & 1) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn num_rev(mut num: u32) -> u32 {
|
|
||||||
let mut rev_num = 0;
|
|
||||||
while num > 0 {
|
|
||||||
let digit = num % 10;
|
|
||||||
rev_num = rev_num * 10 + digit;
|
|
||||||
num /= 10;
|
|
||||||
}
|
|
||||||
rev_num
|
|
||||||
}
|
|
||||||
|
|
||||||
fn num_chars(n: u32, output: &mut [u8]) -> usize {
|
|
||||||
let mut n = num_rev(n);
|
|
||||||
let mut i = 0;
|
|
||||||
while n > 0 {
|
|
||||||
output[i] = (n % 10) as u8 + '0' as u8;
|
|
||||||
n = n / 10;
|
|
||||||
i += 1;
|
|
||||||
}
|
|
||||||
i
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue