diff --git a/hel/Cargo.toml b/hel/Cargo.toml index 3d98328..638052c 100644 --- a/hel/Cargo.toml +++ b/hel/Cargo.toml @@ -23,6 +23,7 @@ shlex = "1.1.0" shellexpand = "3.0.0" scopeguard = "1.1.0" parking_lot = "0.12.1" +num-integer = "0.1.45" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] chrono = "0.4.23" diff --git a/hel/src/lib.rs b/hel/src/lib.rs index 119fb98..a48a092 100644 --- a/hel/src/lib.rs +++ b/hel/src/lib.rs @@ -3,6 +3,7 @@ extern crate lazy_static; #[allow(unused_imports)] #[macro_use(defer)] extern crate scopeguard; +extern crate num_integer; pub mod commands; pub mod lk; diff --git a/hel/src/structs.rs b/hel/src/structs.rs index f87d3fa..df2d3f2 100644 --- a/hel/src/structs.rs +++ b/hel/src/structs.rs @@ -1,4 +1,5 @@ use crate::password::{Comment, Name, PasswordRef}; +use num_integer::Integer; use parking_lot::Mutex; use parking_lot::ReentrantMutex; use std::cell::RefCell; @@ -261,11 +262,19 @@ impl LKOut { pub fn output(&self) -> Vec { let mut out: Vec = vec![]; 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 { - 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 @@ -317,35 +326,26 @@ impl 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; + let (mut x, negative): (u32, bool) = if self.x < 0 { + ((self.x * -1).try_into().unwrap(), true) + } else { + ((self.x).try_into().unwrap(), false) + }; + let mut result = Vec::new(); - result[used] = std::char::from_digit(m, self.radix).unwrap(); - used += 1; - - if x == 0 { - break; - } + while x != 0 { + let (n, m) = x.div_rem(&self.radix); + result.push(std::char::from_digit(m as u32, self.radix).unwrap()); + x = n; } if negative { write!(f, "-")?; } - for c in result[..used].iter().rev() { + for c in result.iter().rev() { write!(f, "{}", c)?; } - Ok(()) } } @@ -364,7 +364,9 @@ pub fn init() -> Option { } } 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 => (),