Skip to content

Commit 80f374c

Browse files
committed
uucore/format: ignore the 0 flag if a precision is specified
1 parent 5fbbfc7 commit 80f374c

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/uucore/src/lib/features/format/spec.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,6 @@ impl Spec {
115115
index += 1;
116116
}
117117

118-
let alignment = match (minus, zero) {
119-
(true, _) => NumberAlignment::Left,
120-
(false, true) => NumberAlignment::RightZero,
121-
(false, false) => NumberAlignment::RightSpace,
122-
};
123-
124118
let positive_sign = match (plus, space) {
125119
(true, _) => PositiveSign::Plus,
126120
(false, true) => PositiveSign::Space,
@@ -136,6 +130,15 @@ impl Spec {
136130
None
137131
};
138132

133+
// The `0` flag is ignored if `-` is given or a precision is specified.
134+
// So the only case for RightZero, is when `-` is not given and the
135+
// precision is none.
136+
let alignment = match (minus, zero) {
137+
(true, _) => NumberAlignment::Left,
138+
(false, true) if precision.is_none() => NumberAlignment::RightZero,
139+
_ => NumberAlignment::RightSpace,
140+
};
141+
139142
// We ignore the length. It's not really relevant to printf
140143
let _ = Self::parse_length(rest, &mut index);
141144

tests/by-util/test_csplit.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1357,3 +1357,18 @@ fn precision_format() {
13571357
assert_eq!(at.read("xx 000"), generate(1, 10));
13581358
assert_eq!(at.read("xx 0x001"), generate(10, 51));
13591359
}
1360+
1361+
#[test]
1362+
fn precision_format2() {
1363+
let (at, mut ucmd) = at_and_ucmd!();
1364+
ucmd.args(&["numbers50.txt", "10", "--suffix-format", "%0#6.3x"])
1365+
.succeeds()
1366+
.stdout_only("18\n123\n");
1367+
1368+
let count = glob(&at.plus_as_string("xx*"))
1369+
.expect("there should be splits created")
1370+
.count();
1371+
assert_eq!(count, 2);
1372+
assert_eq!(at.read("xx 000"), generate(1, 10));
1373+
assert_eq!(at.read("xx 0x001"), generate(10, 51));
1374+
}

0 commit comments

Comments
 (0)