From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756883AbcANWY1 (ORCPT ); Thu, 14 Jan 2016 17:24:27 -0500 Received: from mga03.intel.com ([134.134.136.65]:30560 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754487AbcANWYX (ORCPT ); Thu, 14 Jan 2016 17:24:23 -0500 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.22,296,1449561600"; d="scan'208";a="890747475" From: Andy Shevchenko To: Robert Elliott , Matt Fleming , Andrew Morton , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , linux-kernel@vger.kernel.org, Rasmus Villemoes , Brian Norris , Hariprasad S Cc: Andy Shevchenko Subject: [PATCH v2 01/11] lib/vsprintf: introduce put_one_char() for 3 line idiom Date: Fri, 15 Jan 2016 00:23:31 +0200 Message-Id: <1452810221-116505-2-git-send-email-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.6.4 In-Reply-To: <1452810221-116505-1-git-send-email-andriy.shevchenko@linux.intel.com> References: <1452810221-116505-1-git-send-email-andriy.shevchenko@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There is an idiom used widely in the code if (buf < end) *buf = c; ++buf; Introduce put_one_char() helper as implementation of this idiom. Signed-off-by: Andy Shevchenko --- lib/vsprintf.c | 180 ++++++++++++++++++--------------------------------------- 1 file changed, 55 insertions(+), 125 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 75eb342..fe6ffe3 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -325,6 +325,14 @@ char *put_dec(char *buf, unsigned long long n) #endif +static +char *put_one_char(char *buf, char *end, char c) +{ + if (buf < end) + *buf = c; + return ++buf; +} + /* * Convert passed number to decimal string. * Returns the length of string. On buffer overflow, returns 0. @@ -458,59 +466,35 @@ 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 = put_one_char(buf, end, ' '); } /* sign */ - if (sign) { - if (buf < end) - *buf = sign; - ++buf; - } + if (sign) + buf = put_one_char(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 = put_one_char(buf, end, '0'); + if (spec.base == 16) + buf = put_one_char(buf, end, 'X' | locase); } /* zero or space padding */ if (!(spec.flags & LEFT)) { char c = ' ' + (spec.flags & ZEROPAD); BUILD_BUG_ON(' ' + ZEROPAD != '0'); - while (--field_width >= 0) { - if (buf < end) - *buf = c; - ++buf; - } + while (--field_width >= 0) + buf = put_one_char(buf, end, c); } /* hmm even more zero padding? */ - while (i <= --precision) { - if (buf < end) - *buf = '0'; - ++buf; - } + while (i <= --precision) + buf = put_one_char(buf, end, '0'); /* actual digits of result */ - while (--i >= 0) { - if (buf < end) - *buf = tmp[i]; - ++buf; - } + while (--i >= 0) + buf = put_one_char(buf, end, tmp[i]); /* trailing space padding */ - while (--field_width >= 0) { - if (buf < end) - *buf = ' '; - ++buf; - } + while (--field_width >= 0) + buf = put_one_char(buf, end, ' '); return buf; } @@ -571,11 +555,8 @@ 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 = put_one_char(buf, end, ' '); return buf; } @@ -592,9 +573,7 @@ char *string(char *buf, char *end, const char *s, struct printf_spec spec) char c = *s++; if (!c) break; - if (buf < end) - *buf = c; - ++buf; + buf = put_one_char(buf, end, c); ++len; } return widen_string(buf, len, end, spec); @@ -629,7 +608,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) @@ -637,8 +616,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 = put_one_char(buf, end, c); } rcu_read_unlock(); return widen_string(buf, n, end, spec); @@ -653,11 +631,8 @@ char *bdev_name(char *buf, char *end, struct block_device *bdev, buf = string(buf, end, hd->disk_name, spec); if (bdev->bd_part->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 = put_one_char(buf, end, 'p'); buf = number(buf, end, bdev->bd_part->partno, spec); } return buf; @@ -834,18 +809,11 @@ 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 = put_one_char(buf, end, hex_asc_hi(addr[i])); + buf = put_one_char(buf, end, hex_asc_lo(addr[i])); + + if (separator && i != len - 1) + buf = put_one_char(buf, end, separator); } return buf; @@ -877,11 +845,8 @@ 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 = put_one_char(buf, end, ','); first = false; spec.field_width = DIV_ROUND_UP(chunksz, 4); @@ -911,19 +876,13 @@ char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, if (cur < nr_bits && cur <= rtop + 1) continue; - if (!first) { - if (buf < end) - *buf = ','; - buf++; - } + if (!first) + buf = put_one_char(buf, end, ','); first = false; buf = number(buf, end, rbot, spec); if (rbot < rtop) { - if (buf < end) - *buf = '-'; - buf++; - + buf = put_one_char(buf, end, '-'); buf = number(buf, end, rtop, spec); } @@ -1950,28 +1909,15 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) set_precision(&spec, va_arg(args, int)); break; - case FORMAT_TYPE_CHAR: { - char c; - + case FORMAT_TYPE_CHAR: 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 = put_one_char(str, end, ' '); } + str = put_one_char(str, end, (unsigned char)va_arg(args, int)); + while (--spec.field_width > 0) + str = put_one_char(str, end, ' '); break; - } case FORMAT_TYPE_STR: str = string(str, end, va_arg(args, char *), spec); @@ -1985,9 +1931,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 = put_one_char(str, end, '%'); break; case FORMAT_TYPE_INVALID: @@ -2394,27 +2338,15 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) set_precision(&spec, get_arg(int)); break; - case FORMAT_TYPE_CHAR: { - char c; - + case FORMAT_TYPE_CHAR: if (!(spec.flags & LEFT)) { - while (--spec.field_width > 0) { - if (str < end) - *str = ' '; - ++str; - } - } - c = (unsigned char) get_arg(char); - if (str < end) - *str = c; - ++str; - while (--spec.field_width > 0) { - if (str < end) - *str = ' '; - ++str; + while (--spec.field_width > 0) + str = put_one_char(str, end, ' '); } + str = put_one_char(str, end, (unsigned char)get_arg(char)); + while (--spec.field_width > 0) + str = put_one_char(str, end, ' '); break; - } case FORMAT_TYPE_STR: { const char *str_arg = args; @@ -2430,9 +2362,7 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) break; case FORMAT_TYPE_PERCENT_CHAR: - if (str < end) - *str = '%'; - ++str; + str = put_one_char(str, end, '%'); break; case FORMAT_TYPE_INVALID: -- 2.6.4