Skip to content

Commit 8ff0db1

Browse files
sylvestrejtracey
authored andcommitted
printf: Improve support for printing multi-byte values of characters
1 parent d99b7b3 commit 8ff0db1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,18 @@ impl<'a, T: Iterator<Item = &'a FormatArgument>> ArgumentIter<'a> for T {
5858
};
5959
match next {
6060
FormatArgument::UnsignedInt(n) => *n,
61-
FormatArgument::Unparsed(s) => extract_value(u64::extended_parse(s), s),
61+
FormatArgument::Unparsed(s) => {
62+
// Check if the string is a character literal enclosed in quotes
63+
if s.starts_with(['"', '\'']) && s.len() > 2 {
64+
// Extract the content between the quotes safely
65+
let chars: Vec<char> =
66+
s.trim_matches(|c| c == '"' || c == '\'').chars().collect();
67+
if chars.len() == 1 {
68+
return chars[0] as u64; // Return the Unicode code point
69+
}
70+
}
71+
extract_value(u64::extended_parse(s), s)
72+
}
6273
_ => 0,
6374
}
6475
}

tests/by-util/test_printf.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,3 +1385,13 @@ fn float_arg_with_whitespace() {
13851385
.fails()
13861386
.stderr_contains("expected a numeric value");
13871387
}
1388+
1389+
#[test]
1390+
fn mb_input() {
1391+
for format in ["\"á", "\'á"] {
1392+
new_ucmd!()
1393+
.args(&["%04x\n", format])
1394+
.succeeds()
1395+
.stdout_only("00e1\n");
1396+
}
1397+
}

0 commit comments

Comments
 (0)