mirror of
https://github.com/uutils/coreutils.git
synced 2026-06-10 15:48:22 -07:00
printf: Improve support for printing multi-byte values of characters
This commit is contained in:
@@ -58,7 +58,18 @@ impl<'a, T: Iterator<Item = &'a FormatArgument>> ArgumentIter<'a> for T {
|
|||||||
};
|
};
|
||||||
match next {
|
match next {
|
||||||
FormatArgument::UnsignedInt(n) => *n,
|
FormatArgument::UnsignedInt(n) => *n,
|
||||||
FormatArgument::Unparsed(s) => extract_value(u64::extended_parse(s), s),
|
FormatArgument::Unparsed(s) => {
|
||||||
|
// Check if the string is a character literal enclosed in quotes
|
||||||
|
if s.starts_with(['"', '\'']) && s.len() > 2 {
|
||||||
|
// Extract the content between the quotes safely
|
||||||
|
let chars: Vec<char> =
|
||||||
|
s.trim_matches(|c| c == '"' || c == '\'').chars().collect();
|
||||||
|
if chars.len() == 1 {
|
||||||
|
return chars[0] as u64; // Return the Unicode code point
|
||||||
|
}
|
||||||
|
}
|
||||||
|
extract_value(u64::extended_parse(s), s)
|
||||||
|
}
|
||||||
_ => 0,
|
_ => 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1385,3 +1385,13 @@ fn float_arg_with_whitespace() {
|
|||||||
.fails()
|
.fails()
|
||||||
.stderr_contains("expected a numeric value");
|
.stderr_contains("expected a numeric value");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mb_input() {
|
||||||
|
for format in ["\"á", "\'á"] {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["%04x\n", format])
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("00e1\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user