Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1309721

[PATCH v2 01/11] lib/vsprintf: introduce put_one_char() for 3 line idiom

From Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Newsgroups linux.kernel
Subject [PATCH v2 01/11] lib/vsprintf: introduce put_one_char() for 3 line idiom
Date 2016-01-14 23:30 +0100
Message-ID <qQYz1-5b5-49@gated-at.bofh.it> (permalink)
References <qQYz0-5b5-1@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


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

Back to linux.kernel | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH v2 00/11] lib/vsprintf: refactor and introduce %pl specifier Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 08/11] lib/vsprintf: allow range of prefix for %pl[From[To]] Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
    Re: [PATCH v2 08/11] lib/vsprintf: allow range of prefix for %pl[From[To]] Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2016-01-18 22:50 +0100
  [PATCH v2 02/11] lib/vsprintf: make default_dec_spec global Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
    Re: [PATCH v2 02/11] lib/vsprintf: make default_dec_spec global Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2016-01-18 21:40 +0100
  [PATCH v2 11/11] pcmciamtd: print value in human-readable form via %.0plKM Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 04/11] lib/string_helpers: export string_units_{2,10} for others Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 03/11] lib/vsprintf: make default_str_spec global Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 07/11] x86/efi: print size and base in binary units in efi_print_memmap Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 10/11] cxgb4: print value in human-readable form via %.0plKM Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 05/11] lib/string_helpers: fix indentation in few places Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 06/11] lib/vsprintf: introduce %pl to print in human-readable form Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
    Re: [PATCH v2 06/11] lib/vsprintf: introduce %pl to print in  human-readable form Joe Perches <joe@perches.com> - 2016-01-15 04:10 +0100
    Re: [PATCH v2 06/11] lib/vsprintf: introduce %pl to print in human-readable form Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2016-01-18 22:00 +0100
    Re: [PATCH v2 06/11] lib/vsprintf: introduce %pl to print in  human-readable form "H. Peter Anvin" <hpa@zytor.com> - 2016-01-21 22:20 +0100
  [PATCH v2 09/11] lib/vsprintf: use precision field with %pl[From[To]] Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
    Re: [PATCH v2 09/11] lib/vsprintf: use precision field with %pl[From[To]] Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2016-01-18 23:30 +0100
  [PATCH v2 01/11] lib/vsprintf: introduce put_one_char() for 3 line idiom Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
    Re: [PATCH v2 01/11] lib/vsprintf: introduce put_one_char() for 3 line idiom Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2016-01-18 21:40 +0100
  Re: [PATCH v2 00/11] lib/vsprintf: refactor and introduce %pl  specifier Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-21 14:00 +0100

csiph-web