Handle unicode minus character when parsing floats

This commit is contained in:
Emil Ernerfeldt 2024-06-30 14:48:05 +02:00
parent 3add56f81b
commit e297a1d107
1 changed files with 19 additions and 2 deletions

View File

@ -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()
}
@ -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)"
);
}
}