From: Kent Overstreet <kent.overstreet@linux.dev>
To: akpm@linux-foundation.org
Cc: linux-kernel@vger.kernel.org,
Kent Overstreet <kent.overstreet@gmail.com>,
Petr Mladek <pmladek@suse.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>
Subject: [PATCH 09/32] vsprintf: Improve number()
Date: Sun, 14 Aug 2022 17:19:48 -0400 [thread overview]
Message-ID: <20220814212011.1727798-10-kent.overstreet@linux.dev> (raw)
In-Reply-To: <20220814212011.1727798-1-kent.overstreet@linux.dev>
From: Kent Overstreet <kent.overstreet@gmail.com>
This patch refactors number() to make it a bit clearer, and it also
changes it to call printbuf_make_room() only once at the start, instead
of in the printbuf output helpers.
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Cc: Petr Mladek <pmladek@suse.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
lib/vsprintf.c | 83 +++++++++++++++++++++++++-------------------------
1 file changed, 41 insertions(+), 42 deletions(-)
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 52dac8519a..87adc528c6 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -457,93 +457,92 @@ void number(struct printbuf *out, unsigned long long num,
{
/* put_dec requires 2-byte alignment of the buffer. */
char tmp[3 * sizeof(num)] __aligned(2);
- char sign;
- char locase;
+ char sign = 0;
+ /* locase = 0 or 0x20. ORing digits or letters with 'locase'
+ * produces same digits or (maybe lowercased) letters */
+ char locase = (spec.flags & SMALL);
int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
- int i;
bool is_zero = num == 0LL;
int field_width = spec.field_width;
int precision = spec.precision;
+ int nr_digits = 0;
+ int output_bytes = 0;
- /* locase = 0 or 0x20. ORing digits or letters with 'locase'
- * produces same digits or (maybe lowercased) letters */
- locase = (spec.flags & SMALL);
if (spec.flags & LEFT)
spec.flags &= ~ZEROPAD;
- sign = 0;
if (spec.flags & SIGN) {
if ((signed long long)num < 0) {
sign = '-';
num = -(signed long long)num;
- field_width--;
+ output_bytes++;
} else if (spec.flags & PLUS) {
sign = '+';
- field_width--;
+ output_bytes++;
} else if (spec.flags & SPACE) {
sign = ' ';
- field_width--;
+ output_bytes++;
}
}
if (need_pfx) {
if (spec.base == 16)
- field_width -= 2;
+ output_bytes += 2;
else if (!is_zero)
- field_width--;
+ output_bytes++;
}
/* generate full string in tmp[], in reverse order */
- i = 0;
- if (num < spec.base)
- tmp[i++] = hex_asc_upper[num] | locase;
- else if (spec.base != 10) { /* 8 or 16 */
+ if (spec.base == 10) {
+ nr_digits = put_dec(tmp, num) - tmp;
+ } else { /* 8 or 16 */
int mask = spec.base - 1;
- int shift = 3;
+ int shift = ilog2((unsigned) spec.base);
- if (spec.base == 16)
- shift = 4;
do {
- tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
+ tmp[nr_digits++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
num >>= shift;
} while (num);
- } else { /* base 10 */
- i = put_dec(tmp, num) - tmp;
}
/* printing 100 using %2d gives "100", not "00" */
- if (i > precision)
- precision = i;
+ precision = max(nr_digits, precision);
+ output_bytes += precision;
+ field_width = max(0, field_width - output_bytes);
+
+ printbuf_make_room(out, field_width + output_bytes);
+
/* leading space padding */
- field_width = max(0, field_width - precision);
if (!(spec.flags & (ZEROPAD | LEFT)) && field_width) {
- __prt_chars(out, ' ', field_width);
+ __prt_chars_reserved(out, ' ', field_width);
field_width = 0;
}
+
/* sign */
if (sign)
- __prt_char(out, sign);
+ __prt_char_reserved(out, sign);
+
/* "0x" / "0" prefix */
if (need_pfx) {
if (spec.base == 16 || !is_zero)
- __prt_char(out, '0');
+ __prt_char_reserved(out, '0');
if (spec.base == 16)
- __prt_char(out, 'X' | locase);
+ __prt_char_reserved(out, 'X' | locase);
}
- /* zero or space padding */
- if (!(spec.flags & LEFT) && field_width) {
- char c = ' ' + (spec.flags & ZEROPAD);
- __prt_chars(out, c, field_width);
- field_width = 0;
- }
- /* hmm even more zero padding? */
- if (precision > i)
- __prt_chars(out, '0', precision - i);
+ /* zero padding */
+ if (!(spec.flags & LEFT) && field_width)
+ __prt_chars_reserved(out, '0', field_width);
+
+ /* zero padding from precision */
+ if (precision > nr_digits)
+ __prt_chars_reserved(out, '0', precision - nr_digits);
+
/* actual digits of result */
- while (--i >= 0)
- __prt_char(out, tmp[i]);
+ while (--nr_digits >= 0)
+ __prt_char_reserved(out, tmp[nr_digits]);
+
/* trailing space padding */
- if (field_width)
- __prt_chars(out, ' ', field_width);
+ if ((spec.flags & LEFT) && field_width)
+ __prt_chars_reserved(out, ' ', field_width);
printbuf_nul_terminate(out);
}
--
2.36.1
next prev parent reply other threads:[~2022-08-14 21:21 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-14 21:19 Printbufs v6 Kent Overstreet
2022-08-14 21:19 ` [PATCH 01/32] lib/printbuf: New data structure for printing strings Kent Overstreet
2022-08-14 21:19 ` [PATCH 02/32] lib/string_helpers: Convert string_escape_mem() to printbuf Kent Overstreet
2022-08-14 21:19 ` [PATCH 03/32] vsprintf: Convert " Kent Overstreet
2022-08-14 21:19 ` [PATCH 04/32] lib/hexdump: " Kent Overstreet
2022-08-14 21:19 ` [PATCH 05/32] lib/string_helpers: string_get_size() now returns characters wrote Kent Overstreet
2022-08-14 21:19 ` [PATCH 06/32] lib/printbuf: Heap allocation Kent Overstreet
2022-08-14 21:19 ` [PATCH 07/32] lib/printbuf: Tabstops, indenting Kent Overstreet
2022-08-14 21:19 ` [PATCH 08/32] lib/printbuf: Unit specifiers Kent Overstreet
2022-08-14 21:19 ` Kent Overstreet [this message]
2022-08-14 21:19 ` [PATCH 10/32] vsprintf: prt_u64_minwidth(), prt_u64() Kent Overstreet
2022-08-14 21:19 ` [PATCH 11/32] test_printf: Drop requirement that sprintf not write past nul Kent Overstreet
2022-08-14 21:19 ` [PATCH 12/32] vsprintf: Start consolidating printf_spec handling Kent Overstreet
2022-08-14 21:19 ` [PATCH 13/32] vsprintf: Refactor resource_string() Kent Overstreet
2022-08-14 21:19 ` [PATCH 14/32] vsprintf: Refactor fourcc_string() Kent Overstreet
2022-08-14 21:19 ` [PATCH 15/32] vsprintf: Refactor ip_addr_string() Kent Overstreet
2022-08-14 21:19 ` [PATCH 16/32] vsprintf: Refactor mac_address_string() Kent Overstreet
2022-08-14 21:19 ` [PATCH 17/32] vsprintf: time_and_date() no longer takes printf_spec Kent Overstreet
2022-08-14 21:19 ` [PATCH 18/32] vsprintf: flags_string() " Kent Overstreet
2022-08-14 21:19 ` [PATCH 19/32] vsprintf: Refactor device_node_string, fwnode_string Kent Overstreet
2022-08-14 21:19 ` [PATCH 20/32] vsprintf: Refactor hex_string, bitmap_string_list, bitmap_string Kent Overstreet
2022-08-14 21:20 ` [PATCH 21/32] Input/joystick/analog: Convert from seq_buf -> printbuf Kent Overstreet
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220814212011.1727798-10-kent.overstreet@linux.dev \
--to=kent.overstreet@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=kent.overstreet@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=pmladek@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).