Skip to content

Commit af577e7

Browse files
sylvestrejtracey
authored andcommitted
printf: support for extract chars
Should fix tests/printf/printf-mb.sh
1 parent 79cb095 commit af577e7

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,21 @@ impl<'a, T: Iterator<Item = &'a FormatArgument>> ArgumentIter<'a> for T {
6060
FormatArgument::UnsignedInt(n) => *n,
6161
FormatArgument::Unparsed(s) => {
6262
// 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
63+
if s.starts_with(['"', '\'']) {
64+
// Extract the content between the quotes safely using chars
65+
let mut chars = s.trim_matches(|c| c == '"' || c == '\'').chars();
66+
if let Some(first_char) = chars.next() {
67+
if chars.clone().count() > 0 {
68+
// Emit a warning if there are additional characters
69+
let remaining: String = chars.collect();
70+
show_warning!(
71+
"{}: character(s) following character constant have been ignored",
72+
remaining
73+
);
74+
}
75+
return first_char as u64; // Use only the first character
6976
}
77+
return 0; // Empty quotes
7078
}
7179
extract_value(u64::extended_parse(s), s)
7280
}

tests/by-util/test_printf.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1291,10 +1291,25 @@ fn float_arg_with_whitespace() {
12911291

12921292
#[test]
12931293
fn mb_input() {
1294-
for format in ["\"á", "\'á"] {
1294+
for format in ["\"á", "\'á", "'\u{e1}"] {
12951295
new_ucmd!()
12961296
.args(&["%04x\n", format])
12971297
.succeeds()
12981298
.stdout_only("00e1\n");
12991299
}
1300+
1301+
let cases = vec![
1302+
("\"á=", "="),
1303+
("\'á-", "-"),
1304+
("\'á=-==", "=-=="),
1305+
("'\u{e1}++", "++"),
1306+
];
1307+
1308+
for (format, expected) in cases {
1309+
new_ucmd!()
1310+
.args(&["%04x\n", format])
1311+
.succeeds()
1312+
.stdout_is("00e1\n")
1313+
.stderr_is(format!("printf: warning: {expected}: character(s) following character constant have been ignored\n"));
1314+
}
13001315
}

0 commit comments

Comments
 (0)