Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
9794e0c9d5 |
|||
|
e23327c08f |
|||
|
0f937fe9a8 |
|||
|
58b3d876e4 |
2 changed files with 18 additions and 16 deletions
|
|
@ -3,7 +3,7 @@ use rug::Float;
|
||||||
use crate::nodes::{self, Error};
|
use crate::nodes::{self, Error};
|
||||||
|
|
||||||
pub fn pretty_eval(input: &str, max_digits: usize) -> Result<String, Error> {
|
pub fn pretty_eval(input: &str, max_digits: usize) -> Result<String, Error> {
|
||||||
let (prefix, deliminator, input) = input.rsplit_once_any(&["plus.minus ", "& ", "= "]);
|
let (prefix, deliminator, input) = input.rsplit_once_last(&["= ", "& ", "plus.minus "]);
|
||||||
|
|
||||||
// no need to remove the character here,
|
// no need to remove the character here,
|
||||||
// as we ignore it in all other logic already
|
// as we ignore it in all other logic already
|
||||||
|
|
@ -32,22 +32,16 @@ pub fn pretty_eval(input: &str, max_digits: usize) -> Result<String, Error> {
|
||||||
}
|
}
|
||||||
|
|
||||||
trait StrExt {
|
trait StrExt {
|
||||||
fn rsplit_once_any<'a>(
|
fn rsplit_once_last<'a, 'b>(&'a self, delimiters: &[&'b str]) -> (&'a str, &'b str, &'a str);
|
||||||
&'a self,
|
|
||||||
delimiters: &[&'static str],
|
|
||||||
) -> (&'a str, &'static str, &'a str);
|
|
||||||
}
|
}
|
||||||
impl StrExt for str {
|
impl StrExt for str {
|
||||||
fn rsplit_once_any<'a>(
|
fn rsplit_once_last<'a, 'b>(&'a self, delimiters: &[&'b str]) -> (&'a str, &'b str, &'a str) {
|
||||||
&'a self,
|
delimiters
|
||||||
delimiters: &[&'static str],
|
.iter()
|
||||||
) -> (&'a str, &'static str, &'a str) {
|
.filter_map(|delimiter| self.rfind(delimiter).map(|i| (delimiter, i)))
|
||||||
for delimiter in delimiters {
|
.max_by_key(|(_, i)| *i)
|
||||||
if let Some((prefix, suffix)) = self.rsplit_once(delimiter) {
|
.map(|(delimiter, i)| (&self[..i], *delimiter, &self[i + delimiter.len()..]))
|
||||||
return (prefix, delimiter, suffix);
|
.unwrap_or(("", "", self))
|
||||||
}
|
|
||||||
}
|
|
||||||
return ("", "", self);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -124,6 +118,14 @@ mod tests {
|
||||||
assert_eq!(pretty_eval("-11", MAX_DIGITS).unwrap(), "-11");
|
assert_eq!(pretty_eval("-11", MAX_DIGITS).unwrap(), "-11");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn complex_prefix() {
|
||||||
|
assert_eq!(
|
||||||
|
pretty_eval("plus.minus 5 & = 5", MAX_DIGITS).unwrap(),
|
||||||
|
"plus.minus 5 & = 5"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn basic_get_significant_digits() {
|
fn basic_get_significant_digits() {
|
||||||
let many_zeros = eval("14.00643 + 1.00784 * 4 + 35.446 + 15.9994 * 4");
|
let many_zeros = eval("14.00643 + 1.00784 * 4 + 35.446 + 15.9994 * 4");
|
||||||
|
|
|
||||||
|
|
@ -127,7 +127,7 @@ impl FromStr for Nodes {
|
||||||
&& !(c == '-' && filtered_string.chars().nth(i - 1).is_some_and(|c| c == 'e'))
|
&& !(c == '-' && filtered_string.chars().nth(i - 1).is_some_and(|c| c == 'e'))
|
||||||
{
|
{
|
||||||
// likely a constant before this number
|
// likely a constant before this number
|
||||||
// no need to check for parenthesis though (why?)
|
// no need to check for parenthesis here though (why?)
|
||||||
if matches!(nodes.last(), Some(Node::Number(_))) {
|
if matches!(nodes.last(), Some(Node::Number(_))) {
|
||||||
nodes.push(Node::Operator(Default::default()));
|
nodes.push(Node::Operator(Default::default()));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue