mirror of
https://github.com/storytold/deckcraft.git
synced 2026-11-02 06:57:04 -06:00
Fix Unit::format printing "-0.00" for tiny negative lengths
Found while testing crates/geom/src/lib.rs:72-76 Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Haiku 5.5
parent
90058395b7
commit
b0b87c5cad
+12
-1
@@ -71,7 +71,10 @@ impl Unit {
|
||||
/// `2.08"` style text for a length in points.
|
||||
pub fn format(self, pt: f64) -> String {
|
||||
let v = pt / self.points_per_unit();
|
||||
let s = format!("{v:.2}");
|
||||
let mut s = format!("{v:.2}");
|
||||
if s.starts_with('-') && s[1..].bytes().all(|b| b == b'0' || b == b'.') {
|
||||
s.remove(0);
|
||||
}
|
||||
format!("{s}{}", self.suffix())
|
||||
}
|
||||
/// Parses `2.5`, `2.5"`, `2.5 in`, `6 cm`, `30 pt`, `40px`, `5mm` into points. A bare number uses `self`.
|
||||
@@ -222,6 +225,14 @@ pub fn preset_flatten(path: &BezPath, f: &mut dyn FnMut(PathEl)) {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn format_has_no_negative_zero() {
|
||||
assert_eq!(Unit::Points.format(-0.001), "0.00 pt");
|
||||
assert_eq!(Unit::Inches.format(-0.001), "0.00\"");
|
||||
assert!(!Unit::Centimeters.format(-0.01).starts_with('-'));
|
||||
assert_eq!(Unit::Points.format(-1.5), "-1.50 pt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn emu_roundtrip_is_exact() {
|
||||
for emu in [0i64, 1, 12_699, 12_700, 914_400, 12_192_000, 6_858_000, -5, 123_456_789] {
|
||||
|
||||
Reference in New Issue
Block a user