Skip to content

Commit

Permalink
Handle unicode minus character when parsing floats
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jun 30, 2024
1 parent 3add56f commit e297a1d
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions crates/egui/src/widgets/drag_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,8 +677,14 @@ fn parse(custom_parser: &Option<NumParser<'_>>, value_text: &str) -> Option<f64>
}

fn default_parser(value_text: &str) -> Option<f64> {
// 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()
}

Expand Down Expand Up @@ -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)"
);
}
}

0 comments on commit e297a1d

Please sign in to comment.