1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

uucore/format: ignore the 0 flag if a precision is specified

This commit is contained in:
Terts Diepraam 2024-02-10 12:17:31 +01:00
parent 5fbbfc75de
commit 3a21d27c1e
2 changed files with 26 additions and 6 deletions

View file

@ -115,12 +115,6 @@ impl Spec {
index += 1;
}
let alignment = match (minus, zero) {
(true, _) => NumberAlignment::Left,
(false, true) => NumberAlignment::RightZero,
(false, false) => NumberAlignment::RightSpace,
};
let positive_sign = match (plus, space) {
(true, _) => PositiveSign::Plus,
(false, true) => PositiveSign::Space,
@ -136,6 +130,17 @@ impl Spec {
None
};
// The `0` flag is ignored if `-` is given or a precision is specified.
// So the only case for RightZero, is when `-` is not given and the
// precision is none.
let alignment = if minus {
NumberAlignment::Left
} else if zero && precision.is_none() {
NumberAlignment::RightZero
} else {
NumberAlignment::RightSpace
};
// We ignore the length. It's not really relevant to printf
let _ = Self::parse_length(rest, &mut index);

View file

@ -1357,3 +1357,18 @@ fn precision_format() {
assert_eq!(at.read("xx 000"), generate(1, 10));
assert_eq!(at.read("xx 0x001"), generate(10, 51));
}
#[test]
fn precision_format2() {
let (at, mut ucmd) = at_and_ucmd!();
ucmd.args(&["numbers50.txt", "10", "--suffix-format", "%0#6.3x"])
.succeeds()
.stdout_only("18\n123\n");
let count = glob(&at.plus_as_string("xx*"))
.expect("there should be splits created")
.count();
assert_eq!(count, 2);
assert_eq!(at.read("xx 000"), generate(1, 10));
assert_eq!(at.read("xx 0x001"), generate(10, 51));
}