[PATCH] vsprintf/doc: Document format flags including field width and precision

From: Petr Mladek
Date: Mon May 22 2023 - 11:09:12 EST


The kernel implementation of vsprintf() tries to be as compatible with
the user space variant as possible. Though it does not implement all
features. On the other hand, it adds some special pointer printing
modifiers.

Most differences are described in Documentation/core-api/printk-formats.rst
Add the missing documentation of the supported flag characters
'#', '0', '-', ' ', '+' together with field width and precision modifiers.

Suggested-by: Luca Weiss <luca.weiss@xxxxxxxxxxxxx>
Signed-off-by: Petr Mladek <pmladek@xxxxxxxx>
---
What about something like this, please?

Documentation/core-api/printk-formats.rst | 69 +++++++++++++++++++++++
1 file changed, 69 insertions(+)

diff --git a/Documentation/core-api/printk-formats.rst b/Documentation/core-api/printk-formats.rst
index dfe7e75a71de..79655b319658 100644
--- a/Documentation/core-api/printk-formats.rst
+++ b/Documentation/core-api/printk-formats.rst
@@ -8,6 +8,75 @@ How to get printk format specifiers right
:Author: Andrew Murray <amurray@xxxxxxxxxxxxxx>


+Flag characters
+===============
+
+The character '%' might be followed by the following flags that modify
+the output:
+
+ - '#' - prepend '0', '0x', or 'OX for 'o', 'x', 'X' number conversions
+ - '0' - zero pad number conversions on the field boundary
+ - '-' - left adjust on the field boundary, blank pad on the right
+ - ' ' - prepend space on positive numbers
+ - '+' - prepend + for positive numbers when using signed formats
+
+Examples::
+
+ |%x| |1a|
+ |%#x| |0x1a|
+ |%d| |26|
+ |% d| | 26|
+ |%+d| |+26|
+
+
+Field width
+===========
+
+A field width may be defined when '%' is optionally followed by the above flag
+characters and:
+
+ - 'number' - the decimal number defines the field width
+ - '*' the field width is defined by an extra parameter
+
+Values are never truncated when the filed width is not big enough.
+Spaces are used by default when a padding is needed.
+
+Examples::
+
+ |%6d| | 26|
+ |%-6d| |26 |
+ |%06d| |000026|
+
+ printk("Dynamic table: |%*d|%*s|\n", id_width, id, max_name_len, name);
+
+The filed width value might have special meaning for some pointer formats.
+For example, it limits the size of the bitmap handled by %*pb format.
+
+
+
+Field precision:
+================
+
+A field width may be defined when '%' is optionally followed by the above flag
+characters:
+
+ - '.number' - the decimal number defines the field precision
+ - '.*' the field precision is defined by an extra parameter
+
+The precision defines:
+
+ - number of digits after the decimal point in float number conversions
+ - minimal number of digits in integer conversions
+ - maximum number of characters in string conversions
+
+Examples::
+
+ |%.3f| |12.300|
+ |%.6d| | 26|
+ |%.6s| |Hi, th|
+
+ printk("Dynamic precision: %.*f\n", precision, value);
+
Integer types
=============

--
2.35.3