Unit Converter: a thousands comma in any number of a sum is refused

A value such as 1,000 mm+1 mm read the comma as a decimal point and gave
2 mm, because only a value that was one number was checked for a thousands
comma. Each number of a sum is now checked on its own, so 1,000 mm+1 mm,
+1,000 and 2*1,000 are refused in the number fields and in
addon.units.convert, and 100.5 mm-0,5 still reads as 100 mm.
This commit is contained in:
dotCooCoo
2026-11-14 15:32:57 -08:00
parent 907a759710
commit e4955fb933
2 changed files with 18 additions and 5 deletions
+11 -5
View File
@@ -317,12 +317,18 @@ pub fn decimal_point(text: &str) -> std::borrow::Cow<'_, str> {
if text.contains(',') && !thousands(text) { text.replace(',', ".").into() } else { text.into() }
}
/// 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).
/// 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.
fn thousands(text: &str) -> bool {
let number = text.trim().trim_start_matches('-');
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 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())
@@ -70,6 +70,8 @@ fn values_read_as_number_fields_read_them() {
// A decimal comma reads as a point, as in the number fields.
assert!(near(pt(&mut s, json!({"value": "0,5 in"})), 36.0));
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 sum of 128 signs still reads.
assert!(near(pt(&mut s, json!({"value": format!("1{}", "+1".repeat(128))})), 129.0));
}
@@ -143,6 +145,11 @@ 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))}),