Browse Source

Handle unicode minus character when parsing floats

pull/4740/head
Emil Ernerfeldt 4 months ago
parent
commit
e297a1d107
  1. 19
      crates/egui/src/widgets/drag_value.rs

19
crates/egui/src/widgets/drag_value.rs

@ -677,8 +677,14 @@ fn parse(custom_parser: &Option<NumParser<'_>>, value_text: &str) -> Option<f64>
} }
fn default_parser(value_text: &str) -> Option<f64> { fn default_parser(value_text: &str) -> Option<f64> {
let value_text: String = value_text
.chars()
// Ignore whitespace (trailing, leading, and thousands separators): // Ignore whitespace (trailing, leading, and thousands separators):
let value_text: String = value_text.chars().filter(|c| !c.is_whitespace()).collect(); .filter(|c| !c.is_whitespace())
// Replace special minus character with normal minus (hyphen):
.map(|c| if c == '−' { '-' } else { c })
.collect();
value_text.parse().ok() value_text.parse().ok()
} }
@ -744,5 +750,16 @@ mod tests {
Some(1_234_567.0), Some(1_234_567.0),
"We should handle thousands separators using half-space" "We should handle thousands separators using half-space"
); );
assert_eq!(
super::default_parser("-1.23"),
Some(-1.23),
"Should handle normal hyphen as minus character"
);
assert_eq!(
super::default_parser("−1.23"),
Some(-1.23),
"Should handle special minus character (https://www.compart.com/en/unicode/U+2212)"
);
} }
} }

Loading…
Cancel
Save