Skip to content

printf: pad unsigned numbers properly #5955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 13 additions & 31 deletions src/uucore/src/lib/features/format/num_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,43 +140,25 @@ impl Formatter for UnsignedInt {
fn fmt(&self, mut writer: impl Write, x: Self::Input) -> std::io::Result<()> {
let mut s = match self.variant {
UnsignedIntVariant::Decimal => format!("{x}"),
UnsignedIntVariant::Octal(Prefix::No) => format!("{x:o}"),
UnsignedIntVariant::Octal(Prefix::Yes) => {
// The prefix that rust uses is `0o`, but GNU uses `0`.
// We also need to take into account that 0 should not be 00
// Since this is an unsigned int, we do not need to take the minus
// sign into account.
if x == 0 {
format!("{x:o}")
} else {
format!("0{x:o}")
}
}
UnsignedIntVariant::Hexadecimal(Case::Lowercase, Prefix::No) => {
UnsignedIntVariant::Octal(_) => format!("{x:o}"),
UnsignedIntVariant::Hexadecimal(Case::Lowercase, _) => {
format!("{x:x}")
}
UnsignedIntVariant::Hexadecimal(Case::Lowercase, Prefix::Yes) => {
if x == 0 {
"0".to_string()
} else {
format!("{x:#x}")
}
}
UnsignedIntVariant::Hexadecimal(Case::Uppercase, Prefix::No) => {
UnsignedIntVariant::Hexadecimal(Case::Uppercase, _) => {
format!("{x:X}")
}
UnsignedIntVariant::Hexadecimal(Case::Uppercase, Prefix::Yes) => {
if x == 0 {
"0".to_string()
} else {
format!("{x:#X}")
}
}
};

if self.precision > s.len() {
s = format!("{:0width$}", s, width = self.precision);
}
// Zeroes do not get a prefix. An octal value does also not get a
// prefix if the padded value will not start with a zero.
let prefix = match (x, self.variant) {
(1.., UnsignedIntVariant::Hexadecimal(Case::Lowercase, Prefix::Yes)) => "0x",
(1.., UnsignedIntVariant::Hexadecimal(Case::Uppercase, Prefix::Yes)) => "0X",
(1.., UnsignedIntVariant::Octal(Prefix::Yes)) if s.len() >= self.precision => "0",
_ => "",
};

s = format!("{prefix}{s:0>width$}", width = self.precision);

match self.alignment {
NumberAlignment::Left => write!(writer, "{s:<width$}", width = self.width),
Expand Down
58 changes: 57 additions & 1 deletion tests/by-util/test_printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ fn sub_alternative_upper_hex() {
new_ucmd!()
.args(&["%#X", "42"])
.succeeds()
.stdout_only("0x2A");
.stdout_only("0X2A");
}

#[test]
Expand All @@ -680,3 +680,59 @@ fn char_as_byte() {
fn no_infinite_loop() {
new_ucmd!().args(&["a", "b"]).succeeds().stdout_only("a");
}

#[test]
fn pad_octal_with_prefix() {
new_ucmd!()
.args(&[">%#15.6o<", "0"])
.succeeds()
.stdout_only("> 000000<");

new_ucmd!()
.args(&[">%#15.6o<", "01"])
.succeeds()
.stdout_only("> 000001<");

new_ucmd!()
.args(&[">%#15.6o<", "01234"])
.succeeds()
.stdout_only("> 001234<");

new_ucmd!()
.args(&[">%#15.6o<", "012345"])
.succeeds()
.stdout_only("> 012345<");

new_ucmd!()
.args(&[">%#15.6o<", "0123456"])
.succeeds()
.stdout_only("> 0123456<");
}

#[test]
fn pad_unsigned_zeroes() {
for format in ["%.3u", "%.3x", "%.3X", "%.3o"] {
new_ucmd!()
.args(&[format, "0"])
.succeeds()
.stdout_only("000");
}
}

#[test]
fn pad_unsigned_three() {
for (format, expected) in [
("%.3u", "003"),
("%.3x", "003"),
("%.3X", "003"),
("%.3o", "003"),
("%#.3x", "0x003"),
("%#.3X", "0X003"),
("%#.3o", "003"),
] {
new_ucmd!()
.args(&[format, "3"])
.succeeds()
.stdout_only(expected);
}
}