diff --git a/crates/doc/src/units.rs b/crates/doc/src/units.rs index 2ae33d0783..052830ef0f 100644 --- a/crates/doc/src/units.rs +++ b/crates/doc/src/units.rs @@ -317,18 +317,12 @@ pub fn decimal_point(text: &str) -> std::borrow::Cow<'_, str> { if text.contains(',') && !thousands(text) { text.replace(',', ".").into() } else { text.into() } } -/// Does `text` hold a number written with a thousands comma, as `1,000` or `12,500 mm`: one to -/// three digits (not a lone 0), a comma, exactly three digits, then at most a unit? Read as a -/// decimal it would be a thousand times too small, so the field refuses it instead (`0,125` and -/// `1,25` are decimals). Each number of a sum is checked on its own, so `1,000 mm+1 mm` and -/// `+1,000` are refused too. +/// Is `text` a number written with a thousands comma, as `1,000` or `12,500 mm`: one to three +/// digits (not a lone 0), a comma, exactly three digits, then at most a unit? Read as a decimal it +/// would be a thousand times too small, so the field refuses it instead (`0,125` and `1,25` are +/// decimals). fn thousands(text: &str) -> bool { - text.split(['+', '-', '*', '/']).any(thousands_number) -} - -/// Is `number`, one number of a field value, written with a thousands comma ([`thousands`])? -fn thousands_number(number: &str) -> bool { - let number = number.trim(); + let number = text.trim().trim_start_matches('-'); let Some((whole, rest)) = number.split_once(',') else { return false }; let (part, unit) = rest.split_at(rest.find(|c: char| !c.is_ascii_digit()).unwrap_or(rest.len())); (1..=3).contains(&whole.len()) diff --git a/crates/engine/src/cmd/addons/units/tests.rs b/crates/engine/src/cmd/addons/units/tests.rs index 12e93a8a02..8ab0bd8984 100644 --- a/crates/engine/src/cmd/addons/units/tests.rs +++ b/crates/engine/src/cmd/addons/units/tests.rs @@ -72,6 +72,8 @@ fn values_read_as_number_fields_read_them() { assert!(near(pt(&mut s, json!({"value": "1,5", "unit": "in"})), 108.0)); // Each number of a sum reads its own separator. assert!(near(pt(&mut s, json!({"value": "100.5 mm-0,5", "unit": "mm"})), 100.0 * 72.0 / 25.4)); + // A comma in a sum is a decimal comma, as in the number fields: 1,500+2 is 3.5. + assert!(near(pt(&mut s, json!({"value": "1,500+2", "unit": "mm"})), 3.5 * 72.0 / 25.4)); // A sum of 128 signs still reads. assert!(near(pt(&mut s, json!({"value": format!("1{}", "+1".repeat(128))})), 129.0)); } @@ -145,11 +147,6 @@ fn bad_values_and_units_are_refused() { // A thousands comma is refused, as the number fields refuse it. json!({"value": "1,000 mm"}), json!({"value": "1,000.5"}), - // So is a thousands comma in any number of a sum. - json!({"value": "1,000 mm+1 mm"}), - json!({"value": "1 mm+1,000 mm"}), - json!({"value": "+1,000"}), - json!({"value": "2*1,000"}), // A long expression is refused instead of exhausting the stack. json!({"value": format!("1{}", "+1".repeat(200_000))}), json!({"value": format!("1{}", "+1".repeat(129))}),