From e297a1d10730f2f3410d4353daa588241dd62a80 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 30 Jun 2024 14:48:05 +0200 Subject: [PATCH] Handle unicode minus character when parsing floats --- crates/egui/src/widgets/drag_value.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/egui/src/widgets/drag_value.rs b/crates/egui/src/widgets/drag_value.rs index 509dc5f9..1c237543 100644 --- a/crates/egui/src/widgets/drag_value.rs +++ b/crates/egui/src/widgets/drag_value.rs @@ -677,8 +677,14 @@ fn parse(custom_parser: &Option>, value_text: &str) -> Option } fn default_parser(value_text: &str) -> Option { - // Ignore whitespace (trailing, leading, and thousands separators): - let value_text: String = value_text.chars().filter(|c| !c.is_whitespace()).collect(); + let value_text: String = value_text + .chars() + // Ignore whitespace (trailing, leading, and thousands separators): + .filter(|c| !c.is_whitespace()) + // Replace special minus character with normal minus (hyphen): + .map(|c| if c == '−' { '-' } else { c }) + .collect(); + value_text.parse().ok() } @@ -744,5 +750,16 @@ mod tests { Some(1_234_567.0), "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)" + ); } }