Small refactoring/simplification of radix code.

This commit is contained in:
Oleksandr Kozachuk
2023-01-10 13:58:25 +01:00
parent ab79a100d3
commit 8db5e66f83
3 changed files with 27 additions and 23 deletions
+1
View File
@@ -23,6 +23,7 @@ shlex = "1.1.0"
shellexpand = "3.0.0" shellexpand = "3.0.0"
scopeguard = "1.1.0" scopeguard = "1.1.0"
parking_lot = "0.12.1" parking_lot = "0.12.1"
num-integer = "0.1.45"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]
chrono = "0.4.23" chrono = "0.4.23"
+1
View File
@@ -3,6 +3,7 @@ extern crate lazy_static;
#[allow(unused_imports)] #[allow(unused_imports)]
#[macro_use(defer)] #[macro_use(defer)]
extern crate scopeguard; extern crate scopeguard;
extern crate num_integer;
pub mod commands; pub mod commands;
pub mod lk; pub mod lk;
+25 -23
View File
@@ -1,4 +1,5 @@
use crate::password::{Comment, Name, PasswordRef}; use crate::password::{Comment, Name, PasswordRef};
use num_integer::Integer;
use parking_lot::Mutex; use parking_lot::Mutex;
use parking_lot::ReentrantMutex; use parking_lot::ReentrantMutex;
use std::cell::RefCell; use std::cell::RefCell;
@@ -261,11 +262,19 @@ impl LKOut {
pub fn output(&self) -> Vec<String> { pub fn output(&self) -> Vec<String> {
let mut out: Vec<String> = vec![]; let mut out: Vec<String> = vec![];
match &self.err { match &self.err {
Some(o) => for l in &*o.lock() { out.push(l.to_string()); }, Some(o) => {
for l in &*o.lock() {
out.push(l.to_string());
}
}
_ => (), _ => (),
} }
match &self.out { match &self.out {
Some(o) => for l in &*o.lock() { out.push(l.to_string()); }, Some(o) => {
for l in &*o.lock() {
out.push(l.to_string());
}
}
_ => (), _ => (),
} }
out out
@@ -317,35 +326,26 @@ impl Radix {
impl fmt::Display for Radix { impl fmt::Display for Radix {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut x = self.x; let (mut x, negative): (u32, bool) = if self.x < 0 {
// Good for binary formatting of `u128`s ((self.x * -1).try_into().unwrap(), true)
let mut result = ['\0'; 128]; } else {
let mut used = 0; ((self.x).try_into().unwrap(), false)
let negative = x < 0; };
if negative { let mut result = Vec::new();
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(); while x != 0 {
used += 1; let (n, m) = x.div_rem(&self.radix);
result.push(std::char::from_digit(m as u32, self.radix).unwrap());
if x == 0 { x = n;
break;
}
} }
if negative { if negative {
write!(f, "-")?; write!(f, "-")?;
} }
for c in result[..used].iter().rev() { for c in result.iter().rev() {
write!(f, "{}", c)?; write!(f, "{}", c)?;
} }
Ok(()) Ok(())
} }
} }
@@ -364,7 +364,9 @@ pub fn init() -> Option<LKRead> {
} }
} }
Err(err) => { Err(err) => {
LKEval::new(editor.clone(), Command::Error(LKErr::ParseError(err)), lk.clone(), password).eval().print(); LKEval::new(editor.clone(), Command::Error(LKErr::ParseError(err)), lk.clone(), password)
.eval()
.print();
} }
}, },
Err(err) if err.kind() == std::io::ErrorKind::NotFound => (), Err(err) if err.kind() == std::io::ErrorKind::NotFound => (),