From 7d32e49fb907f3fd9e9a1bbb1e152a4083d1b9a5 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Wed, 10 Jan 2024 18:19:56 +0100 Subject: [PATCH] printf: %c prints the first byte of its argument --- src/uucore/src/lib/features/format/argument.rs | 2 +- tests/by-util/test_printf.rs | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/uucore/src/lib/features/format/argument.rs b/src/uucore/src/lib/features/format/argument.rs index 92d6c1603..ef81fc353 100644 --- a/src/uucore/src/lib/features/format/argument.rs +++ b/src/uucore/src/lib/features/format/argument.rs @@ -45,7 +45,7 @@ impl<'a, T: Iterator> ArgumentIter<'a> for T { }; match next { FormatArgument::Char(c) => *c, - FormatArgument::Unparsed(s) => s.chars().next().unwrap_or('\0'), + FormatArgument::Unparsed(s) => s.bytes().next().map_or('\0', char::from), _ => '\0', } } diff --git a/tests/by-util/test_printf.rs b/tests/by-util/test_printf.rs index 48fc1e6ac..c35277106 100644 --- a/tests/by-util/test_printf.rs +++ b/tests/by-util/test_printf.rs @@ -639,3 +639,8 @@ fn partial_char() { "printf: warning: bc: character(s) following character constant have been ignored\n", ); } + +#[test] +fn char_as_byte() { + new_ucmd!().args(&["%c", "🙃"]).succeeds().stdout_only("ð"); +}