Add Radix structure to convert number to any radix up to 36.

This commit is contained in:
Oleksandr Kozachuk
2022-12-11 16:21:17 +01:00
parent dd85460b69
commit c8080b9856
+51
View File
@@ -1,4 +1,5 @@
use crate::password::{Comment, Name, PasswordRef}; use crate::password::{Comment, Name, PasswordRef};
use std::fmt;
#[derive(thiserror::Error, Debug, PartialEq)] #[derive(thiserror::Error, Debug, PartialEq)]
pub enum LKErr<'a> { pub enum LKErr<'a> {
@@ -57,3 +58,53 @@ impl std::fmt::Display for Mode {
) )
} }
} }
pub struct Radix {
x: i32,
radix: u32,
}
impl Radix {
pub fn new(x: i32, radix: u32) -> Result<Self, &'static str> {
if radix < 2 || radix > 36 {
Err("Unnsupported radix")
} else {
Ok(Self { x, radix })
}
}
}
impl fmt::Display for Radix {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut x = self.x;
// Good for binary formatting of `u128`s
let mut result = ['\0'; 128];
let mut used = 0;
let negative = x < 0;
if negative {
x*=-1;
}
let mut x = x as u32;
loop {
let m = x % self.radix;
x /= self.radix;
result[used] = std::char::from_digit(m, self.radix).unwrap();
used += 1;
if x == 0 {
break;
}
}
if negative {
write!(f, "-")?;
}
for c in result[..used].iter().rev() {
write!(f, "{}", c)?;
}
Ok(())
}
}