feat: ignore underscores in input
this isn't useful for typst usually, since underscores are subscript and don't appear in expressions much. however, when using tylc as a standalone calculator, this is useful eg to seperate sets of three digits. idea totally stolen from rust literals note that this might cause problems when it comes to special variables, eg A_n for Avogadro's number (but just A is fine)
This commit is contained in:
parent
bb985f917a
commit
046a3e5b20
2 changed files with 14 additions and 12 deletions
|
|
@ -2,7 +2,9 @@ use rug::Float;
|
|||
|
||||
use crate::nodes::{self, Error};
|
||||
|
||||
pub fn pretty_eval(mut input: String, max_digits: usize) -> Result<String, Error> {
|
||||
pub fn pretty_eval(input: &str, max_digits: usize) -> Result<String, Error> {
|
||||
let mut input = input.replace('_', "");
|
||||
|
||||
// no need to remove the characters here,
|
||||
// as we ignore the them in all other logic already
|
||||
let should_add_back_alignment = input.contains('&');
|
||||
|
|
@ -49,20 +51,20 @@ pub fn pretty_eval(mut input: String, max_digits: usize) -> Result<String, Error
|
|||
}
|
||||
#[test]
|
||||
fn test_character_readdition() {
|
||||
assert_eq!(pretty_eval(" = 11".into(), 10).unwrap(), " = 11");
|
||||
assert_eq!(pretty_eval(" &= 11".into(), 10).unwrap(), " & = 11");
|
||||
assert_eq!(pretty_eval(" & 11".into(), 10).unwrap(), " & 11");
|
||||
assert_eq!(pretty_eval(" & 11 \\".into(), 10).unwrap(), " & 11 \\");
|
||||
assert_eq!(pretty_eval(" = 11", 10).unwrap(), " = 11");
|
||||
assert_eq!(pretty_eval(" &= 11", 10).unwrap(), " & = 11");
|
||||
assert_eq!(pretty_eval(" & 11", 10).unwrap(), " & 11");
|
||||
assert_eq!(pretty_eval(" & 11 \\", 10).unwrap(), " & 11 \\");
|
||||
}
|
||||
#[test]
|
||||
fn test_basic_up_to_999() {
|
||||
assert_eq!(pretty_eval("1".into(), 10).unwrap(), "1");
|
||||
assert_eq!(pretty_eval("10".into(), 10).unwrap(), "10");
|
||||
assert_eq!(pretty_eval("100".into(), 10).unwrap(), "100");
|
||||
assert_eq!(pretty_eval("999".into(), 10).unwrap(), "999");
|
||||
assert_eq!(pretty_eval("1000".into(), 10).unwrap(), "1 * 10^3");
|
||||
assert_eq!(pretty_eval("1", 10).unwrap(), "1");
|
||||
assert_eq!(pretty_eval("10", 10).unwrap(), "10");
|
||||
assert_eq!(pretty_eval("100", 10).unwrap(), "100");
|
||||
assert_eq!(pretty_eval("999", 10).unwrap(), "999");
|
||||
assert_eq!(pretty_eval("1000", 10).unwrap(), "1 * 10^3");
|
||||
|
||||
assert_eq!(pretty_eval("11.111111".into(), 10).unwrap(), "11.111111");
|
||||
assert_eq!(pretty_eval("11.111111", 10).unwrap(), "11.111111");
|
||||
}
|
||||
|
||||
pub fn get_significant_digits(n: &Float) -> usize {
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ fn main() -> ExitCode {
|
|||
buf
|
||||
});
|
||||
|
||||
match lib::formatting::pretty_eval(input, 10) {
|
||||
match lib::formatting::pretty_eval(&input, 10) {
|
||||
Ok(s) => println!("{s}"),
|
||||
|
||||
Err(e) => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue