Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip currency prefix to mitigate #410 #411

Merged
merged 3 commits into from
Apr 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions quadratic-core/src/formulas/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,9 @@ fn eval(grid: &mut impl GridProxy, s: &str) -> FormulaResult<Value> {
.eval_blocking(grid, Pos::ORIGIN)
.map(|value| value.inner)
}

/// Regression test for quadratic#410
#[test]
fn test_currency_string() {
assert_eq!("30", eval_to_string(&mut PanicGridMock, "\"$10\" + 20"));
}
10 changes: 8 additions & 2 deletions quadratic-core/src/formulas/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::fmt;

use super::{FormulaErrorMsg, FormulaResult, Spanned};

const CURRENCY_PREFIX: &[char] = &['$', '¥', '£', '€'];

#[derive(Debug, Clone, PartialEq)]
pub enum Value {
String(String),
Expand Down Expand Up @@ -78,10 +80,14 @@ impl Spanned<Value> {
pub fn to_number(&self) -> FormulaResult<f64> {
match &self.inner {
Value::String(s) => {
if s.trim().is_empty() {
let mut s = s.trim();
if s.is_empty() {
return Ok(0.0);
}
s.trim().parse().map_err(|_| {
if let Some(rest) = s.strip_prefix(CURRENCY_PREFIX) {
s = rest;
}
s.parse().map_err(|_| {
FormulaErrorMsg::Expected {
expected: "number".into(),
got: Some(format!("{s:?}").into()),
Expand Down