From 2e60dce11a04c69b697b884020185e85fda14061 Mon Sep 17 00:00:00 2001 From: Hanif Ariffin Date: Fri, 4 Feb 2022 19:29:24 +0800 Subject: [PATCH] printf: Default left-justify integer conversion to 1 width When using left-justify with integer conversion (like `printf '%-o'`), default the minimum width to 1. Closes: https://github.com/uutils/coreutils/issues/3050 Signed-off-by: Hanif Ariffin --- src/uucore/src/lib/features/tokenize/sub.rs | 6 +++++- tests/by-util/test_printf.rs | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/uucore/src/lib/features/tokenize/sub.rs b/src/uucore/src/lib/features/tokenize/sub.rs index f8b5b5caf..216953a43 100644 --- a/src/uucore/src/lib/features/tokenize/sub.rs +++ b/src/uucore/src/lib/features/tokenize/sub.rs @@ -152,7 +152,11 @@ impl SubParser { if parser.min_width_is_asterisk { CanAsterisk::Asterisk } else { - CanAsterisk::Fixed(parser.min_width_tmp.map(|x| x.parse::().unwrap())) + CanAsterisk::Fixed( + parser + .min_width_tmp + .map(|x| x.parse::().unwrap_or(1)), + ) }, if parser.second_field_is_asterisk { CanAsterisk::Asterisk diff --git a/tests/by-util/test_printf.rs b/tests/by-util/test_printf.rs index d5be4cd17..77f64750c 100644 --- a/tests/by-util/test_printf.rs +++ b/tests/by-util/test_printf.rs @@ -446,6 +446,14 @@ fn sub_any_specifiers_after_period() { .stdout_only("3"); } +#[test] +fn unspecified_left_justify_is_1_width() { + new_ucmd!() + .args(&["%-o"]) //spell-checker:disable-line + .succeeds() + .stdout_only("0"); +} + #[test] fn sub_any_specifiers_after_second_param() { new_ucmd!()