[PATCH v5 next 05/17] tools/nolibc/printf: Output pad characters in 16 byte chunks

david.laight.linux@gmail.com posted 17 patches 1 month ago
[PATCH v5 next 05/17] tools/nolibc/printf: Output pad characters in 16 byte chunks
Posted by david.laight.linux@gmail.com 1 month ago
From: David Laight <david.laight.linux@gmail.com>

Simple to do and saves calls to the callback function.

Change variables written, width and len to 'signed int' to get
better code.

Acked-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---

Unchanged for v4 and v5.

For v3:
- Change to signed variables here rather than a later patch.

 tools/include/nolibc/stdio.h | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 17ca206f77fe..c6d5d075f012 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -312,8 +312,8 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
 {
 	char escape, lpref, ch;
 	unsigned long long v;
-	unsigned int written, width;
-	size_t len, ofs;
+	int written, width, len;
+	size_t ofs;
 	char outbuf[21];
 	const char *outstr;
 
@@ -415,10 +415,14 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
 			outstr = fmt;
 			len = ofs - 1;
 		flush_str:
-			while (width-- > len) {
-				if (cb(state, " ", 1) != 0)
+			width -= len;
+			while (width > 0) {
+				/* Output pad in 16 byte blocks with the small block first. */
+				int pad_len = ((width - 1) & 15) + 1;
+				width -= pad_len;
+				written += pad_len;
+				if (cb(state, "                ", pad_len) != 0)
 					return -1;
-				written += 1;
 			}
 			if (cb(state, outstr, len) != 0)
 				return -1;
-- 
2.39.5