mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Robert Elliott <elliott@hpe.com>,
	Matt Fleming <matt@codeblueprint.co.uk>,
	Andrew Morton <akpm@linux-foundation.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>,
	linux-kernel@vger.kernel.org,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Brian Norris <computersforpeace@gmail.com>,
	Hariprasad S <hariprasad@chelsio.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Subject: [PATCH v2 01/11] lib/vsprintf: introduce put_one_char() for 3 line idiom
Date: Fri, 15 Jan 2016 00:23:31 +0200	[thread overview]
Message-ID: <1452810221-116505-2-git-send-email-andriy.shevchenko@linux.intel.com> (raw)
In-Reply-To: <1452810221-116505-1-git-send-email-andriy.shevchenko@linux.intel.com>

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 <andriy.shevchenko@linux.intel.com>
---
 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

  reply	other threads:[~2016-01-14 22:24 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-14 22:23 [PATCH v2 00/11] lib/vsprintf: refactor and introduce %pl specifier Andy Shevchenko
2016-01-14 22:23 ` Andy Shevchenko [this message]
2016-01-18 20:35   ` [PATCH v2 01/11] lib/vsprintf: introduce put_one_char() for 3 line idiom Rasmus Villemoes
2016-01-14 22:23 ` [PATCH v2 02/11] lib/vsprintf: make default_dec_spec global Andy Shevchenko
2016-01-18 20:38   ` Rasmus Villemoes
2016-01-14 22:23 ` [PATCH v2 03/11] lib/vsprintf: make default_str_spec global Andy Shevchenko
2016-01-14 22:23 ` [PATCH v2 04/11] lib/string_helpers: export string_units_{2,10} for others Andy Shevchenko
2016-01-14 22:23 ` [PATCH v2 05/11] lib/string_helpers: fix indentation in few places Andy Shevchenko
2016-01-14 22:23 ` [PATCH v2 06/11] lib/vsprintf: introduce %pl to print in human-readable form Andy Shevchenko
2016-01-15  3:04   ` Joe Perches
2016-01-18 20:57   ` Rasmus Villemoes
2016-01-21 21:13   ` H. Peter Anvin
2016-01-14 22:23 ` [PATCH v2 07/11] x86/efi: print size and base in binary units in efi_print_memmap Andy Shevchenko
2016-01-14 22:23 ` [PATCH v2 08/11] lib/vsprintf: allow range of prefix for %pl[From[To]] Andy Shevchenko
2016-01-18 21:40   ` Rasmus Villemoes
2016-01-14 22:23 ` [PATCH v2 09/11] lib/vsprintf: use precision field with %pl[From[To]] Andy Shevchenko
2016-01-18 22:21   ` Rasmus Villemoes
2016-01-14 22:23 ` [PATCH v2 10/11] cxgb4: print value in human-readable form via %.0plKM Andy Shevchenko
2016-01-14 22:23 ` [PATCH v2 11/11] pcmciamtd: " Andy Shevchenko
2016-01-21 12:51 ` [PATCH v2 00/11] lib/vsprintf: refactor and introduce %pl specifier Andy Shevchenko

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=1452810221-116505-2-git-send-email-andriy.shevchenko@linux.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=computersforpeace@gmail.com \
    --cc=elliott@hpe.com \
    --cc=hariprasad@chelsio.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=matt@codeblueprint.co.uk \
    --cc=mingo@redhat.com \
    --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: 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

Powered by JetHome