Skip to content

Commit

Permalink
Merge pull request #5811 from spineki/fix-printf-hex-alternate-zero
Browse files Browse the repository at this point in the history
Printf: Fix printf hex alternate zero
  • Loading branch information
tertsdiepraam authored Jan 17, 2024
2 parents dc533a9 + 4557821 commit 3bd9f0e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/uucore/src/lib/features/format/num_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,21 @@ impl Formatter for UnsignedInt {
format!("{x:x}")
}
UnsignedIntVariant::Hexadecimal(Case::Lowercase, Prefix::Yes) => {
format!("{x:#x}")
if x == 0 {
"0".to_string()
} else {
format!("{x:#x}")
}
}
UnsignedIntVariant::Hexadecimal(Case::Uppercase, Prefix::No) => {
format!("{x:X}")
}
UnsignedIntVariant::Hexadecimal(Case::Uppercase, Prefix::Yes) => {
format!("{x:#X}")
if x == 0 {
"0".to_string()
} else {
format!("{x:#X}")
}
}
};

Expand Down
26 changes: 26 additions & 0 deletions tests/by-util/test_printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,32 @@ fn partial_char() {
);
}

#[test]
fn sub_alternative_lower_hex_0() {
new_ucmd!().args(&["%#x", "0"]).succeeds().stdout_only("0");
}

#[test]
fn sub_alternative_lower_hex() {
new_ucmd!()
.args(&["%#x", "42"])
.succeeds()
.stdout_only("0x2a");
}

#[test]
fn sub_alternative_upper_hex_0() {
new_ucmd!().args(&["%#X", "0"]).succeeds().stdout_only("0");
}

#[test]
fn sub_alternative_upper_hex() {
new_ucmd!()
.args(&["%#X", "42"])
.succeeds()
.stdout_only("0x2A");
}

#[test]
fn char_as_byte() {
new_ucmd!().args(&["%c", "🙃"]).succeeds().stdout_only("ð");
Expand Down

0 comments on commit 3bd9f0e

Please sign in to comment.