LKML Archive on lore.kernel.org help / color / mirror / Atom feed
From: Yury Norov <yury.norov@gmail.com> To: Petr Mladek <pmladek@suse.com>, Andrew Morton <akpm@linux-foundation.org>, Andy Shevchenko <andy.shevchenko@gmail.com>, Bartosz Golaszewski <bgolaszewski@baylibre.com>, Chris Down <chris@chrisdown.name>, Gilles Muller <Gilles.Muller@inria.fr>, Ingo Molnar <mingo@kernel.org>, Jacob Keller <jacob.e.keller@intel.com>, Joe Perches <joe@perches.com>, Julia Lawall <Julia.Lawall@inria.fr>, Michal Marek <michal.lkml@markovi.net>, Nicolas Palix <nicolas.palix@imag.fr>, Peter Zijlstra <peterz@infradead.org>, Rasmus Villemoes <linux@rasmusvillemoes.dk>, Sergey Senozhatsky <senozhatsky@chromium.org>, Stephen Boyd <swboyd@chromium.org>, Steven Rostedt <rostedt@goodmis.org>, Thomas Gleixner <tglx@linutronix.de>, linux-kernel@vger.kernel.org, cocci@systeme.lip6.fr Cc: Yury Norov <yury.norov@gmail.com> Subject: [PATCH v2 1/2] lib: add sputchar() helper Date: Sat, 4 Sep 2021 16:10:19 -0700 [thread overview] Message-ID: <20210904231020.331185-2-yury.norov@gmail.com> (raw) In-Reply-To: <20210904231020.331185-1-yury.norov@gmail.com> There are 47 occurrences of the code snippet like this: if (buf < end) *buf = ' '; ++buf; This patch adds a helper function sputchar() to replace opencoding. It adds a lot to readability, and also saves 43 bytes of text on x86. v2: cleanup cases discovered with coccinelle script. Signed-off-by: Yury Norov <yury.norov@gmail.com> --- include/linux/kernel.h | 8 ++ lib/string_helpers.c | 56 +++-------- lib/vsprintf.c | 222 +++++++++++++---------------------------- 3 files changed, 90 insertions(+), 196 deletions(-) diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 1b2f0a7e00d6..01941ed2f5aa 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -283,6 +283,14 @@ extern char *bin2hex(char *dst, const void *src, size_t count); bool mac_pton(const char *s, u8 *mac); +static inline char *sputchar(char *buf, const char *end, char c) +{ + if (buf < end) + *buf = c; + + return buf + 1; +} + /* * General tracing related utility functions - trace_printk(), * tracing_on/tracing_off and tracing_start()/tracing_stop diff --git a/lib/string_helpers.c b/lib/string_helpers.c index 3806a52ce697..f4e0c719650d 100644 --- a/lib/string_helpers.c +++ b/lib/string_helpers.c @@ -335,12 +335,8 @@ static bool escape_space(unsigned char c, char **dst, char *end) return false; } - if (out < end) - *out = '\\'; - ++out; - if (out < end) - *out = to; - ++out; + out = sputchar(out, end, '\\'); + out = sputchar(out, end, to); *dst = out; return true; @@ -368,12 +364,8 @@ static bool escape_special(unsigned char c, char **dst, char *end) return false; } - if (out < end) - *out = '\\'; - ++out; - if (out < end) - *out = to; - ++out; + out = sputchar(out, end, '\\'); + out = sputchar(out, end, to); *dst = out; return true; @@ -386,12 +378,8 @@ static bool escape_null(unsigned char c, char **dst, char *end) if (c) return false; - if (out < end) - *out = '\\'; - ++out; - if (out < end) - *out = '0'; - ++out; + out = sputchar(out, end, '\\'); + out = sputchar(out, end, '0'); *dst = out; return true; @@ -401,18 +389,10 @@ static bool escape_octal(unsigned char c, char **dst, char *end) { char *out = *dst; - if (out < end) - *out = '\\'; - ++out; - if (out < end) - *out = ((c >> 6) & 0x07) + '0'; - ++out; - if (out < end) - *out = ((c >> 3) & 0x07) + '0'; - ++out; - if (out < end) - *out = ((c >> 0) & 0x07) + '0'; - ++out; + out = sputchar(out, end, '\\'); + out = sputchar(out, end, ((c >> 6) & 0x07) + '0'); + out = sputchar(out, end, ((c >> 3) & 0x07) + '0'); + out = sputchar(out, end, ((c >> 0) & 0x07) + '0'); *dst = out; return true; @@ -422,18 +402,10 @@ static bool escape_hex(unsigned char c, char **dst, char *end) { char *out = *dst; - if (out < end) - *out = '\\'; - ++out; - if (out < end) - *out = 'x'; - ++out; - if (out < end) - *out = hex_asc_hi(c); - ++out; - if (out < end) - *out = hex_asc_lo(c); - ++out; + out = sputchar(out, end, '\\'); + out = sputchar(out, end, 'x'); + out = sputchar(out, end, hex_asc_hi(c)); + out = sputchar(out, end, hex_asc_lo(c)); *dst = out; return true; diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 919b70d00855..9d4c9fd81845 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -509,59 +509,38 @@ char *number(char *buf, char *end, unsigned long long num, /* leading space padding */ field_width -= precision; if (!(spec.flags & (ZEROPAD | LEFT))) { - while (--field_width >= 0) { - if (buf < end) - *buf = ' '; - ++buf; - } + while (--field_width >= 0) + buf = sputchar(buf, end, ' '); } /* sign */ - if (sign) { - if (buf < end) - *buf = sign; - ++buf; - } + if (sign) + buf = sputchar(buf, end, sign); + /* "0x" / "0" prefix */ if (need_pfx) { - if (spec.base == 16 || !is_zero) { - if (buf < end) - *buf = '0'; - ++buf; - } - if (spec.base == 16) { - if (buf < end) - *buf = ('X' | locase); - ++buf; - } + if (spec.base == 16 || !is_zero) + buf = sputchar(buf, end, '0'); + if (spec.base == 16) + buf = sputchar(buf, end, 'X' | locase); } /* zero or space padding */ if (!(spec.flags & LEFT)) { char c = ' ' + (spec.flags & ZEROPAD); - while (--field_width >= 0) { - if (buf < end) - *buf = c; - ++buf; - } + while (--field_width >= 0) + buf = sputchar(buf, end, c); } /* hmm even more zero padding? */ - while (i <= --precision) { - if (buf < end) - *buf = '0'; - ++buf; - } + while (i <= --precision) + buf = sputchar(buf, end, '0'); + /* actual digits of result */ - while (--i >= 0) { - if (buf < end) - *buf = tmp[i]; - ++buf; - } + while (--i >= 0) + buf = sputchar(buf, end, tmp[i]); + /* trailing space padding */ - while (--field_width >= 0) { - if (buf < end) - *buf = ' '; - ++buf; - } + while (--field_width >= 0) + buf = sputchar(buf, end, ' '); return buf; } @@ -619,11 +598,9 @@ char *widen_string(char *buf, int n, char *end, struct printf_spec spec) move_right(buf - n, end, n, spaces); return buf + spaces; } - while (spaces--) { - if (buf < end) - *buf = ' '; - ++buf; - } + while (spaces--) + buf = sputchar(buf, end, ' '); + return buf; } @@ -638,9 +615,7 @@ static char *string_nocheck(char *buf, char *end, const char *s, char c = *s++; if (!c) break; - if (buf < end) - *buf = c; - ++buf; + buf = sputchar(buf, end, c); ++len; } return widen_string(buf, len, end, spec); @@ -931,7 +906,7 @@ char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp } } s = array[--i]; - for (n = 0; n != spec.precision; n++, buf++) { + for (n = 0; n != spec.precision; n++) { char c = *s++; if (!c) { if (!i) @@ -939,8 +914,7 @@ char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp c = '/'; s = array[--i]; } - if (buf < end) - *buf = c; + buf = sputchar(buf, end, c); } rcu_read_unlock(); return widen_string(buf, n, end, spec); @@ -968,11 +942,9 @@ char *bdev_name(char *buf, char *end, struct block_device *bdev, hd = bdev->bd_disk; buf = string(buf, end, hd->disk_name, spec); if (bdev->bd_partno) { - if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) { - if (buf < end) - *buf = 'p'; - buf++; - } + if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) + buf = sputchar(buf, end, 'p'); + buf = number(buf, end, bdev->bd_partno, spec); } return buf; @@ -1175,18 +1147,10 @@ char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, len = min_t(int, spec.field_width, 64); for (i = 0; i < len; ++i) { - if (buf < end) - *buf = hex_asc_hi(addr[i]); - ++buf; - if (buf < end) - *buf = hex_asc_lo(addr[i]); - ++buf; - - if (separator && i != len - 1) { - if (buf < end) - *buf = separator; - ++buf; - } + buf = sputchar(buf, end, hex_asc_hi(addr[i])); + buf = sputchar(buf, end, hex_asc_lo(addr[i])); + if (separator && i != len - 1) + buf = sputchar(buf, end, separator); } return buf; @@ -1221,11 +1185,9 @@ char *bitmap_string(char *buf, char *end, unsigned long *bitmap, bit = i % BITS_PER_LONG; val = (bitmap[word] >> bit) & chunkmask; - if (!first) { - if (buf < end) - *buf = ','; - buf++; - } + if (!first) + buf = sputchar(buf, end, ','); + first = false; spec.field_width = DIV_ROUND_UP(chunksz, 4); @@ -1248,20 +1210,17 @@ char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, return buf; for_each_set_bitrange(rbot, rtop, bitmap, nr_bits) { - if (!first) { - if (buf < end) - *buf = ','; - buf++; - } + if (!first) + buf = sputchar(buf, end, ','); + first = false; buf = number(buf, end, rbot, default_dec_spec); if (rtop == rbot + 1) continue; - if (buf < end) - *buf = '-'; - buf = number(buf + 1, end, rtop - 1, default_dec_spec); + buf = sputchar(buf, end, '-'); + buf = number(buf, end, rtop - 1, default_dec_spec); } return buf; } @@ -1822,14 +1781,9 @@ char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r) int mon = tm->tm_mon + (r ? 0 : 1); buf = number(buf, end, year, default_dec04_spec); - if (buf < end) - *buf = '-'; - buf++; - + buf = sputchar(buf, end, '-'); buf = number(buf, end, mon, default_dec02_spec); - if (buf < end) - *buf = '-'; - buf++; + buf = sputchar(buf, end, '-'); return number(buf, end, tm->tm_mday, default_dec02_spec); } @@ -1838,14 +1792,9 @@ static noinline_for_stack char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r) { buf = number(buf, end, tm->tm_hour, default_dec02_spec); - if (buf < end) - *buf = ':'; - buf++; - + buf = sputchar(buf, end, ':'); buf = number(buf, end, tm->tm_min, default_dec02_spec); - if (buf < end) - *buf = ':'; - buf++; + buf = sputchar(buf, end, ':'); return number(buf, end, tm->tm_sec, default_dec02_spec); } @@ -1889,11 +1838,8 @@ char *rtc_str(char *buf, char *end, const struct rtc_time *tm, if (have_d) buf = date_str(buf, end, tm, raw); - if (have_d && have_t) { - if (buf < end) - *buf = iso8601_separator ? 'T' : ' '; - buf++; - } + if (have_d && have_t) + buf = sputchar(buf, end, iso8601_separator ? 'T' : ' '); if (have_t) buf = time_str(buf, end, tm, raw); @@ -1972,11 +1918,8 @@ char *format_flags(char *buf, char *end, unsigned long flags, buf = string(buf, end, names->name, default_str_spec); flags &= ~mask; - if (flags) { - if (buf < end) - *buf = '|'; - buf++; - } + if (flags) + buf = sputchar(buf, end, '|'); } if (flags) @@ -2026,16 +1969,11 @@ char *format_page_flags(char *buf, char *end, unsigned long flags) continue; /* Format: Flag Name + '=' (equals sign) + Number + '|' (separator) */ - if (append) { - if (buf < end) - *buf = '|'; - buf++; - } + if (append) + buf = sputchar(buf, end, '|'); buf = string(buf, end, pff[i].name, default_str_spec); - if (buf < end) - *buf = '='; - buf++; + buf = sputchar(buf, end, '='); buf = number(buf, end, (flags >> pff[i].shift) & pff[i].mask, *pff[i].spec); @@ -2125,11 +2063,8 @@ char *device_node_string(char *buf, char *end, struct device_node *dn, for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) { int precision; - if (pass) { - if (buf < end) - *buf = ':'; - buf++; - } + if (pass) + buf = sputchar(buf, end, ':'); switch (*fmt) { case 'f': /* full_name */ @@ -2773,25 +2708,14 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) break; case FORMAT_TYPE_CHAR: { - char c; - if (!(spec.flags & LEFT)) { - while (--spec.field_width > 0) { - if (str < end) - *str = ' '; - ++str; - - } - } - c = (unsigned char) va_arg(args, int); - if (str < end) - *str = c; - ++str; - while (--spec.field_width > 0) { - if (str < end) - *str = ' '; - ++str; + while (--spec.field_width > 0) + str = sputchar(str, end, ' '); } + str = sputchar(str, end, (unsigned char) va_arg(args, int)); + + while (--spec.field_width > 0) + str = sputchar(str, end, ' '); break; } @@ -2807,9 +2731,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) break; case FORMAT_TYPE_PERCENT_CHAR: - if (str < end) - *str = '%'; - ++str; + str = sputchar(str, end, '%'); break; case FORMAT_TYPE_INVALID: @@ -3251,21 +3173,15 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) char c; if (!(spec.flags & LEFT)) { - while (--spec.field_width > 0) { - if (str < end) - *str = ' '; - ++str; - } + while (--spec.field_width > 0) + str = sputchar(str, end, ' '); } + c = (unsigned char) get_arg(char); - if (str < end) - *str = c; - ++str; - while (--spec.field_width > 0) { - if (str < end) - *str = ' '; - ++str; - } + str = sputchar(str, end, c); + + while (--spec.field_width > 0) + str = sputchar(str, end, ' '); break; } @@ -3312,9 +3228,7 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) } case FORMAT_TYPE_PERCENT_CHAR: - if (str < end) - *str = '%'; - ++str; + str = sputchar(str, end, '%'); break; case FORMAT_TYPE_INVALID: -- 2.30.2
next prev parent reply other threads:[~2021-09-04 23:10 UTC|newest] Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-09-04 23:10 [PATCH 0/2] lib: add sputchar() helper Yury Norov 2021-09-04 23:10 ` Yury Norov [this message] 2021-09-05 1:36 ` [PATCH v2 1/2] " Joe Perches 2021-09-05 3:20 ` Yury Norov 2021-09-06 11:51 ` Rasmus Villemoes 2021-09-07 9:35 ` Petr Mladek 2021-09-04 23:10 ` [PATCH 2/2] coccinelle: add script for sputchar() Yury Norov
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=20210904231020.331185-2-yury.norov@gmail.com \ --to=yury.norov@gmail.com \ --cc=Gilles.Muller@inria.fr \ --cc=Julia.Lawall@inria.fr \ --cc=akpm@linux-foundation.org \ --cc=andy.shevchenko@gmail.com \ --cc=bgolaszewski@baylibre.com \ --cc=chris@chrisdown.name \ --cc=cocci@systeme.lip6.fr \ --cc=jacob.e.keller@intel.com \ --cc=joe@perches.com \ --cc=linux-kernel@vger.kernel.org \ --cc=linux@rasmusvillemoes.dk \ --cc=michal.lkml@markovi.net \ --cc=mingo@kernel.org \ --cc=nicolas.palix@imag.fr \ --cc=peterz@infradead.org \ --cc=pmladek@suse.com \ --cc=rostedt@goodmis.org \ --cc=senozhatsky@chromium.org \ --cc=swboyd@chromium.org \ --cc=tglx@linutronix.de \ /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: linkBe 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).