mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-26 02:37:01 -06:00
Merge pull request #4126 from Silvertree2010/fix/h2467-unit-parse-accepts-scientific-no
Reject exponent notation in the pica form of Unit::parse
This commit is contained in:
+11
-2
@@ -360,10 +360,12 @@ fn parse_measure(s: &str, default: Unit) -> Option<f64> {
|
||||
};
|
||||
}
|
||||
}
|
||||
// Plain decimals only, as in `number_unit` (`str::parse` would accept `1e5`, `inf`, `nan`).
|
||||
let plain = |t: &str| t.trim().chars().all(|c| c.is_ascii_digit() || c == '.' || c == '-') && t.trim().parse::<f64>().is_ok();
|
||||
if let Some((p, pt)) = s.split_once('p')
|
||||
&& !p.is_empty()
|
||||
&& p.trim().parse::<f64>().is_ok()
|
||||
&& (pt.is_empty() || pt.trim().parse::<f64>().is_ok())
|
||||
&& plain(p)
|
||||
&& (pt.is_empty() || plain(pt))
|
||||
&& !s.ends_with("pt")
|
||||
&& !s.ends_with("px")
|
||||
{
|
||||
@@ -2713,6 +2715,13 @@ mod tests {
|
||||
assert_eq!(Unit::Points.parse("-2p"), Some(-24.0));
|
||||
assert_eq!(Unit::Points.parse("2p6"), Some(30.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pica_form_rejects_exponent_notation() {
|
||||
for s in ["1e5p", "infp", "nanp", "infp3", "1p1e2"] {
|
||||
assert_eq!(Unit::Points.parse(s), None, "{s}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user