Skip to content

VectorCraft: An open-source, vector art tool written by scratch in pure Rust

Reject exponent notation in the pica form of Unit::parse

NetterSilver76f8574b4b05
diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs
index a222e8e91..6fa9a2304 100644
--- a/crates/doc/src/lib.rs
+++ b/crates/doc/src/lib.rs
@@ -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)]