mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 19:40:21 +00:00
Unit Converter: a comma in a sum reads as a decimal comma again
The number fields read a comma in a sum as a decimal comma, so 1,500+2 is 3.5 (widgets::decimal_point_tests::a_decimal_comma_reads_as_a_point), and only a value that is one number with a thousands comma, such as 1,000 mm, is refused. Checking each number of a sum for a thousands comma broke that test. The check is back to the single number, and addon.units.convert reads 1,500+2 the same way the number fields do.
This commit is contained in:
+5
-11
@@ -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())
|
||||
|
||||
@@ -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))}),
|
||||
|
||||
Reference in New Issue
Block a user