1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

printf: support for extract chars

Should fix tests/printf/printf-mb.sh
This commit is contained in:
Sylvestre Ledru 2025-01-01 17:17:44 +01:00 committed by Justin Tracey
parent 79cb095636
commit af577e7a14
No known key found for this signature in database
GPG key ID: 62B84F5ABDDDCE54
2 changed files with 30 additions and 7 deletions

View file

@ -60,13 +60,21 @@ impl<'a, T: Iterator<Item = &'a FormatArgument>> ArgumentIter<'a> for T {
FormatArgument::UnsignedInt(n) => *n, FormatArgument::UnsignedInt(n) => *n,
FormatArgument::Unparsed(s) => { FormatArgument::Unparsed(s) => {
// Check if the string is a character literal enclosed in quotes // Check if the string is a character literal enclosed in quotes
if s.starts_with(['"', '\'']) && s.len() > 2 { if s.starts_with(['"', '\'']) {
// Extract the content between the quotes safely // Extract the content between the quotes safely using chars
let chars: Vec<char> = let mut chars = s.trim_matches(|c| c == '"' || c == '\'').chars();
s.trim_matches(|c| c == '"' || c == '\'').chars().collect(); if let Some(first_char) = chars.next() {
if chars.len() == 1 { if chars.clone().count() > 0 {
return chars[0] as u64; // Return the Unicode code point // Emit a warning if there are additional characters
let remaining: String = chars.collect();
show_warning!(
"{}: character(s) following character constant have been ignored",
remaining
);
}
return first_char as u64; // Use only the first character
} }
return 0; // Empty quotes
} }
extract_value(u64::extended_parse(s), s) extract_value(u64::extended_parse(s), s)
} }

View file

@ -1291,10 +1291,25 @@ fn float_arg_with_whitespace() {
#[test] #[test]
fn mb_input() { fn mb_input() {
for format in ["\"á", "\'á"] { for format in ["\"á", "\'á", "'\u{e1}"] {
new_ucmd!() new_ucmd!()
.args(&["%04x\n", format]) .args(&["%04x\n", format])
.succeeds() .succeeds()
.stdout_only("00e1\n"); .stdout_only("00e1\n");
} }
let cases = vec![
("\"á=", "="),
("\'á-", "-"),
("\'á=-==", "=-=="),
("'\u{e1}++", "++"),
];
for (format, expected) in cases {
new_ucmd!()
.args(&["%04x\n", format])
.succeeds()
.stdout_is("00e1\n")
.stderr_is(format!("printf: warning: {expected}: character(s) following character constant have been ignored\n"));
}
} }