Skip to contentSkip to conversation

tools/nolibc: support left-aligned printing in printf

PATCH Superseded licheng.li 2 participants 4 messages Jan 30

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

licheng.li

author
#0 lore raw

v3 cover tools/nolibc: support left-aligned printing in printf

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

v3 drop the implicit support for the '0' flag from this patch and revert to my original goal: strictly supporting left alignment ('-').

Best regards, Cheng Li

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                 | 19 ++++++++++++++++---
 tools/testing/selftests/nolibc/nolibc-test.c |  2 ++
 2 files changed, 18 insertions(+), 3 deletions(-)
Signature · 1 line
2.52.0
author
#1 lore raw

PATCH v3 1/2 tools/nolibc: support left alignment (-) in printf

From: Cheng Li <[email protected]>

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.

The implementation reuses the padding character logic to handle both cases.

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

---
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 | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
tools/include/nolibc/stdio.h +16 -3
index 1f16dab2ac88..f31b77f61d3b 100644
@@ -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,13 +364,20 @@ 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) {
+				while (width > w && padc != '-') {
+					written += 1;
 					if (cb(state, " ", 1) != 0)
 						return -1;
-					written += 1;
+					width--;
 				}
 				if (cb(state, outstr, w) != 0)
 					return -1;
+				while (width > w) {
+					written += 1;
+					if (cb(state, " ", 1) != 0)
+						return -1;
+					width--;
+				}
 			}
 
 			written += len;
Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH v3 1/2] tools/nolibc: support left alignment (-) in printf

Quoted 10 lines · On Fri, 30 Jan 2026 15:18:46 +0800 "licheng.li" <[email protected]> wrote:

From: Cheng Li <[email protected]>

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.

The implementation reuses the padding character logic to handle both cases.

You don't have two cases any more.

Quoted 35 lines

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

---
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 | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
tools/include/nolibc/stdio.h +1 -1
index 1f16dab2ac88..f31b77f61d3b 100644
@@ -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;

tools/include/nolibc/stdio.h int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char · line 273

Quoted 10 lines
tools/include/nolibc/stdio.h +5 -0
+		padc = ' ';
 
 		if (escape) {
 			/* we're in an escape sequence, ofs == 1 */
 			escape = 0;
 
+			if (c == '-') {
+				padc = c;
+				c = fmt[ofs++];
+			}

You only need a 'left align' flag.

Quoted 14 lines
tools/include/nolibc/stdio.h +3 -2
+
 			/* width */
 			while (c >= '0' && c <= '9') {
 				width *= 10;
@@ -358,13 +364,20 @@ 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) {
+				while (width > w && padc != '-') {
+					written += 1;
 					if (cb(state, " ", 1) != 0)
 						return -1;
-					written += 1;

tools/include/nolibc/stdio.h int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char · line 380

Quoted 10 lines
tools/include/nolibc/stdio.h +7 -0
+					width--;
 				}
 				if (cb(state, outstr, w) != 0)
 					return -1;
+				while (width > w) {
+					written += 1;
+					if (cb(state, " ", 1) != 0)
+						return -1;
+					width--;
+				}

I'd swap the code so the short bit is replicated - should be smaller:

				if (padc == '-' && cb(state, outstr, w) != 0)
					return -1;
				while (width > w) {
					written += 1;
					if (cb(state, " ", 1) != 0)
						return -1;
					width--;
				}
				if (padc != '-' && cb(state, outstr, w) != 0)
					return -1;

There is also a slight inconsistency between 'width--' and 'written += 1'. I'd use the postfix operator for both, but that is a matter of taste.

David

Quoted 3 lines
tools/include/nolibc/stdio.h +0 -0
 			}
 
 			written += len;
author
#3 lore raw

PATCH v3 2/2 selftests/nolibc: add tests for printf left alignment and zero padding

In reply to licheng.li

From: Cheng Li <[email protected]>

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.

---
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(+)
tools/testing/selftests/nolibc/nolibc-test.c +2 -0
index 3c5a226dad3a..c11fcf6c5075 100644
@@ -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;
Signature · 1 line
2.52.0