public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 0/2] tools/nolibc: support left-aligned printing in printf
@ 2026-01-30 10:22 licheng.li
  2026-01-30 10:22 ` [PATCH v4 1/2] tools/nolibc: support left alignment (-) " licheng.li
  2026-01-30 10:22 ` [PATCH v4 2/2] selftests/nolibc: add tests for printf left alignment and zero padding licheng.li
  0 siblings, 2 replies; 4+ messages in thread
From: licheng.li @ 2026-01-30 10:22 UTC (permalink / raw)
  To: Willy Tarreau, David Laight
  Cc: Thomas Weißschuh, linux-kernel, im.lechain

From: Cheng Li <im.lechain@gmail.com>

This series adds support for left alignment ('-') to nolibc's printf implementation.

v4 adopted optimization suggestions from David Laight

Cheng Li (2):
  tools/nolibc: support left alignment (-) in printf
  selftests/nolibc: add tests for printf left alignment and zero padding

 tools/include/nolibc/stdio.h                 | 17 +++++++++++++----
 tools/testing/selftests/nolibc/nolibc-test.c |  2 ++
 2 files changed, 15 insertions(+), 4 deletions(-)

-- 
2.52.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v4 1/2] tools/nolibc: support left alignment (-) in printf
  2026-01-30 10:22 [PATCH v4 0/2] tools/nolibc: support left-aligned printing in printf licheng.li
@ 2026-01-30 10:22 ` licheng.li
  2026-01-30 15:00   ` David Laight
  2026-01-30 10:22 ` [PATCH v4 2/2] selftests/nolibc: add tests for printf left alignment and zero padding licheng.li
  1 sibling, 1 reply; 4+ messages in thread
From: licheng.li @ 2026-01-30 10:22 UTC (permalink / raw)
  To: Willy Tarreau, David Laight
  Cc: Thomas Weißschuh, linux-kernel, im.lechain

From: Cheng Li <im.lechain@gmail.com>

Currently, __nolibc_printf() in nolibc parses the width field but always
pads with spaces on the left. It ignores the '-' flag (left alignment).

This patch implements support for the '-' flag
to forces left alignment by padding spaces on the right.

Logic behavior:
 - "%5d"  -> "   -5" (unchanged, right align)
 - "%-5d" -> "-5   " (new, left align)

Suggested-by: David Laight <david.laight.linux@gmail.com>
Suggested-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Cheng Li <im.lechain@gmail.com>
---
v4 changes:
 - Adopted optimization suggestions from David Laight
v3 changes:
 - Removed pad zeros support because of bug for signed number and pointer
v2 changes:
 - Adopted optimization suggestions from Willy Tarreau:
   - Incremented 'written' counter at the start of loops.
   - Reordered loop checks to optimize compiler register usage.
 - Updated commit message to explicitly mention zero-padding ('0') support.
---
 tools/include/nolibc/stdio.h | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 1f16dab2ac88..1197928a1364 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -250,7 +250,7 @@ typedef int (*__nolibc_printf_cb)(intptr_t state, const char *buf, size_t size);
 static __attribute__((unused, format(printf, 4, 0)))
 int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char *fmt, va_list args)
 {
-	char escape, lpref, c;
+	char escape, lpref, padc, c;
 	unsigned long long v;
 	unsigned int written, width;
 	size_t len, ofs, w;
@@ -261,11 +261,17 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char
 	while (1) {
 		c = fmt[ofs++];
 		width = 0;
+		padc = ' ';
 
 		if (escape) {
 			/* we're in an escape sequence, ofs == 1 */
 			escape = 0;
 
+			if (c == '-') {
+				padc = c;
+				c = fmt[ofs++];
+			}
+
 			/* width */
 			while (c >= '0' && c <= '9') {
 				width *= 10;
@@ -358,12 +364,15 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char
 			if (n) {
 				w = len < n ? len : n;
 				n -= w;
-				while (width-- > w) {
+				if (padc == '-' && cb(state, outstr, w) != 0)
+					return -1;
+				while (width > w) {
+					written++;
 					if (cb(state, " ", 1) != 0)
 						return -1;
-					written += 1;
+					width--;
 				}
-				if (cb(state, outstr, w) != 0)
+				if (padc != '-' && cb(state, outstr, w) != 0)
 					return -1;
 			}
 
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v4 2/2] selftests/nolibc: add tests for printf left alignment and zero padding
  2026-01-30 10:22 [PATCH v4 0/2] tools/nolibc: support left-aligned printing in printf licheng.li
  2026-01-30 10:22 ` [PATCH v4 1/2] tools/nolibc: support left alignment (-) " licheng.li
@ 2026-01-30 10:22 ` licheng.li
  1 sibling, 0 replies; 4+ messages in thread
From: licheng.li @ 2026-01-30 10:22 UTC (permalink / raw)
  To: Willy Tarreau, David Laight
  Cc: Thomas Weißschuh, linux-kernel, im.lechain

From: Cheng Li <im.lechain@gmail.com>

This patch adds validation for the recently added left-alignment ('-')
flag in printf().

It ensures that Fields are correctly padded with spaces on the right
when the '-' flag is used.

Signed-off-by: Cheng Li <im.lechain@gmail.com>
---
v3 changes:
 - Removed test case for zero padding that cannot work properly
v2 changes:
 - Added test cases for zero padding (%08d)
---
 tools/testing/selftests/nolibc/nolibc-test.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 3c5a226dad3a..c11fcf6c5075 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -1723,6 +1723,8 @@ static int run_printf(int min, int max)
 		CASE_TEST(truncation);   EXPECT_VFPRINTF(25, "01234567890123456789", "%s", "0123456789012345678901234"); break;
 		CASE_TEST(string_width); EXPECT_VFPRINTF(10, "         1", "%10s", "1"); break;
 		CASE_TEST(number_width); EXPECT_VFPRINTF(10, "         1", "%10d", 1); break;
+		CASE_TEST(number_left);  EXPECT_VFPRINTF(10, "|-5      |", "|%-8d|", -5); break;
+		CASE_TEST(string_align); EXPECT_VFPRINTF(10, "|foo     |", "|%-8s|", "foo"); break;
 		CASE_TEST(width_trunc);  EXPECT_VFPRINTF(25, "                    ", "%25d", 1); break;
 		CASE_TEST(scanf);        EXPECT_ZR(1, test_scanf()); break;
 		CASE_TEST(strerror);     EXPECT_ZR(1, test_strerror()); break;
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v4 1/2] tools/nolibc: support left alignment (-) in printf
  2026-01-30 10:22 ` [PATCH v4 1/2] tools/nolibc: support left alignment (-) " licheng.li
@ 2026-01-30 15:00   ` David Laight
  0 siblings, 0 replies; 4+ messages in thread
From: David Laight @ 2026-01-30 15:00 UTC (permalink / raw)
  To: licheng.li; +Cc: Willy Tarreau, Thomas Weißschuh, linux-kernel

On Fri, 30 Jan 2026 18:22:17 +0800
"licheng.li" <im.lechain@gmail.com> wrote:

> From: Cheng Li <im.lechain@gmail.com>
> 
> Currently, __nolibc_printf() in nolibc parses the width field but always
> pads with spaces on the left. It ignores the '-' flag (left alignment).
> 
> This patch implements support for the '-' flag
> to forces left alignment by padding spaces on the right.
> 
> Logic behavior:
>  - "%5d"  -> "   -5" (unchanged, right align)
>  - "%-5d" -> "-5   " (new, left align)
> 
> Suggested-by: David Laight <david.laight.linux@gmail.com>
> Suggested-by: Willy Tarreau <w@1wt.eu>
> Signed-off-by: Cheng Li <im.lechain@gmail.com>

Looks reasonable.

Reviewed-by: David Laight <david.laight.linux@gmail.com>

	David

> ---
> v4 changes:
>  - Adopted optimization suggestions from David Laight
> v3 changes:
>  - Removed pad zeros support because of bug for signed number and pointer
> v2 changes:
>  - Adopted optimization suggestions from Willy Tarreau:
>    - Incremented 'written' counter at the start of loops.
>    - Reordered loop checks to optimize compiler register usage.
>  - Updated commit message to explicitly mention zero-padding ('0') support.
> ---
>  tools/include/nolibc/stdio.h | 17 +++++++++++++----
>  1 file changed, 13 insertions(+), 4 deletions(-)
> 
> diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
> index 1f16dab2ac88..1197928a1364 100644
> --- a/tools/include/nolibc/stdio.h
> +++ b/tools/include/nolibc/stdio.h
> @@ -250,7 +250,7 @@ typedef int (*__nolibc_printf_cb)(intptr_t state, const char *buf, size_t size);
>  static __attribute__((unused, format(printf, 4, 0)))
>  int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char *fmt, va_list args)
>  {
> -	char escape, lpref, c;
> +	char escape, lpref, padc, c;
>  	unsigned long long v;
>  	unsigned int written, width;
>  	size_t len, ofs, w;
> @@ -261,11 +261,17 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char
>  	while (1) {
>  		c = fmt[ofs++];
>  		width = 0;
> +		padc = ' ';
>  
>  		if (escape) {
>  			/* we're in an escape sequence, ofs == 1 */
>  			escape = 0;
>  
> +			if (c == '-') {
> +				padc = c;
> +				c = fmt[ofs++];
> +			}
> +
>  			/* width */
>  			while (c >= '0' && c <= '9') {
>  				width *= 10;
> @@ -358,12 +364,15 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char
>  			if (n) {
>  				w = len < n ? len : n;
>  				n -= w;
> -				while (width-- > w) {
> +				if (padc == '-' && cb(state, outstr, w) != 0)
> +					return -1;
> +				while (width > w) {
> +					written++;
>  					if (cb(state, " ", 1) != 0)
>  						return -1;
> -					written += 1;
> +					width--;
>  				}
> -				if (cb(state, outstr, w) != 0)
> +				if (padc != '-' && cb(state, outstr, w) != 0)
>  					return -1;
>  			}
>  


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-01-30 15:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-30 10:22 [PATCH v4 0/2] tools/nolibc: support left-aligned printing in printf licheng.li
2026-01-30 10:22 ` [PATCH v4 1/2] tools/nolibc: support left alignment (-) " licheng.li
2026-01-30 15:00   ` David Laight
2026-01-30 10:22 ` [PATCH v4 2/2] selftests/nolibc: add tests for printf left alignment and zero padding licheng.li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox