1
0
Fork 0

Fix subtract with overflow in Hz->kHz formatter

When supplying a short input value without a unit.
This commit is contained in:
Robbert van der Helm 2022-03-21 14:59:20 +01:00
parent 2911efaa79
commit a1cb362dda

View file

@ -91,7 +91,7 @@ pub fn from_f32_hz_then_khz() -> Arc<dyn Fn(&str) -> Option<f32> + Send + Sync>
.trim_end_matches(&[' ', 'k', 'K', 'h', 'H', 'z', 'Z']) .trim_end_matches(&[' ', 'k', 'K', 'h', 'H', 'z', 'Z'])
.parse() .parse()
.ok(); .ok();
match string.get(string.len() - 3..) { match string.get(string.len().saturating_sub(3)..) {
Some(unit) if unit.eq_ignore_ascii_case("khz") => cleaned_string.map(|x| x * 1000.0), Some(unit) if unit.eq_ignore_ascii_case("khz") => cleaned_string.map(|x| x * 1000.0),
// Even if there's no unit at all, just assume the input is in Hertz // Even if there's no unit at all, just assume the input is in Hertz
_ => cleaned_string, _ => cleaned_string,