Fix typos and improve Radix
This commit is contained in:
+2
-2
@@ -228,7 +228,7 @@ impl<'a> LKEval<'a> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => out.e(format!("error: faild to parse command {}: {}", command, e.to_string())),
|
Err(e) => out.e(format!("error: failed to parse command {}: {}", command, e.to_string())),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -330,7 +330,7 @@ impl<'a> LKEval<'a> {
|
|||||||
} else {
|
} else {
|
||||||
match save_dump(&self.state.lock().borrow().db, &script) {
|
match save_dump(&self.state.lock().borrow().db, &script) {
|
||||||
Ok(()) => out.o(format!("Passwords saved to file {}", script)),
|
Ok(()) => out.o(format!("Passwords saved to file {}", script)),
|
||||||
Err(e) => out.e(format!("error: failed to dump passswords to {}: {}", script, e.to_string())),
|
Err(e) => out.e(format!("error: failed to dump passwords to {}: {}", script, e.to_string())),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-1
@@ -317,7 +317,7 @@ pub struct Radix {
|
|||||||
impl Radix {
|
impl Radix {
|
||||||
pub fn new(x: i32, radix: u32) -> Result<Self, &'static str> {
|
pub fn new(x: i32, radix: u32) -> Result<Self, &'static str> {
|
||||||
if radix < 2 || radix > 36 {
|
if radix < 2 || radix > 36 {
|
||||||
Err("Unnsupported radix")
|
Err("Unsupported radix")
|
||||||
} else {
|
} else {
|
||||||
Ok(Self { x, radix })
|
Ok(Self { x, radix })
|
||||||
}
|
}
|
||||||
@@ -333,11 +333,16 @@ impl fmt::Display for Radix {
|
|||||||
};
|
};
|
||||||
let mut result = Vec::new();
|
let mut result = Vec::new();
|
||||||
|
|
||||||
|
// Convert to requested radix by repeated division
|
||||||
while x != 0 {
|
while x != 0 {
|
||||||
let (n, m) = x.div_rem(&self.radix);
|
let (n, m) = x.div_rem(&self.radix);
|
||||||
result.push(std::char::from_digit(m as u32, self.radix).unwrap());
|
result.push(std::char::from_digit(m as u32, self.radix).unwrap());
|
||||||
x = n;
|
x = n;
|
||||||
}
|
}
|
||||||
|
// Ensure 0 is represented with a single digit
|
||||||
|
if result.is_empty() {
|
||||||
|
result.push('0');
|
||||||
|
}
|
||||||
|
|
||||||
if negative {
|
if negative {
|
||||||
write!(f, "-")?;
|
write!(f, "-")?;
|
||||||
@@ -527,4 +532,11 @@ mod tests {
|
|||||||
);
|
);
|
||||||
assert_eq!(std::fs::read_to_string("test_pb_out").expect("read"), "san bud most noon jaw cash");
|
assert_eq!(std::fs::read_to_string("test_pb_out").expect("read"), "san bud most noon jaw cash");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn radix_display_zero() {
|
||||||
|
assert_eq!(format!("{}", Radix::new(0, 10).unwrap()), "0");
|
||||||
|
assert_eq!(format!("{}", Radix::new(15, 16).unwrap()), "f");
|
||||||
|
assert_eq!(format!("{}", Radix::new(-15, 16).unwrap()), "-f");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user