]> www.infradead.org Git - users/willy/linux.git/commitdiff
printf: Add printf_char
authorMatthew Wilcox <willy@infradead.org>
Wed, 19 Dec 2018 20:59:27 +0000 (15:59 -0500)
committerMatthew Wilcox <willy@infradead.org>
Wed, 19 Dec 2018 20:59:27 +0000 (15:59 -0500)
Convert all open-coded versions.

Signed-off-by: Matthew Wilcox <willy@infradead.org>
lib/vsprintf.c

index 375088634bca62e9718820d2d138eb054949f9a4..ceab03dd297fce2c5b842bcf157149c633ef5674 100644 (file)
@@ -618,6 +618,13 @@ static void printf_string(struct printf_state *ps, const char *s)
        ps->buf = string(ps->buf, ps->end, s, ps->spec);
 }
 
+static void printf_char(struct printf_state *ps, char c)
+{
+       if (ps->buf < ps->end)
+               *ps->buf = c;
+       ps->buf++;
+}
+
 static noinline_for_stack
 char *pointer_string(struct printf_state *ps, const void *ptr)
 {
@@ -773,11 +780,8 @@ char *bdev_name(struct printf_state *ps, struct block_device *bdev)
        
        printf_string(ps, hd->disk_name);
        if (bdev->bd_part->partno) {
-               if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
-                       if (ps->buf < ps->end)
-                               *ps->buf = 'p';
-                       ps->buf++;
-               }
+               if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
+                       printf_char(ps, 'p');
                printf_number(ps, bdev->bd_part->partno);
        }
        return ps->buf;
@@ -966,18 +970,11 @@ char *hex_string(struct printf_state *ps, u8 *addr)
                len = min_t(int, ps->spec.field_width, 64);
 
        for (i = 0; i < len; ++i) {
-               if (ps->buf < ps->end)
-                       *ps->buf = hex_asc_hi(addr[i]);
-               ++ps->buf;
-               if (ps->buf < ps->end)
-                       *ps->buf = hex_asc_lo(addr[i]);
-               ++ps->buf;
+               printf_char(ps, hex_asc_hi(addr[i]));
+               printf_char(ps, hex_asc_lo(addr[i]));
 
-               if (separator && i != len - 1) {
-                       if (ps->buf < ps->end)
-                               *ps->buf = separator;
-                       ++ps->buf;
-               }
+               if (separator && i != len - 1)
+                       printf_char(ps, separator);
        }
 
        return ps->buf;
@@ -1008,11 +1005,8 @@ char *bitmap_string(struct printf_state *ps, unsigned long *bitmap)
                bit = i % BITS_PER_LONG;
                val = (bitmap[word] >> bit) & chunkmask;
 
-               if (!first) {
-                       if (ps->buf < ps->end)
-                               *ps->buf = ',';
-                       ps->buf++;
-               }
+               if (!first)
+                       printf_char(ps, ',');
                first = false;
 
                ps->spec.field_width = DIV_ROUND_UP(chunksz, 4);
@@ -1039,19 +1033,13 @@ char *bitmap_list_string(struct printf_state *ps, unsigned long *bitmap)
                if (cur < nr_bits && cur <= rtop + 1)
                        continue;
 
-               if (!first) {
-                       if (ps->buf < ps->end)
-                               *ps->buf = ',';
-                       ps->buf++;
-               }
+               if (!first)
+                       printf_char(ps, ',');
                first = false;
 
                printf_number(ps, rbot);
                if (rbot < rtop) {
-                       if (ps->buf < ps->end)
-                               *ps->buf = '-';
-                       ps->buf++;
-
+                       printf_char(ps, '-');
                        printf_number(ps, rtop);
                }
 
@@ -1593,11 +1581,8 @@ char *format_flags(struct printf_state *ps, unsigned long flags,
                printf_string(ps, names->name);
 
                flags &= ~mask;
-               if (flags) {
-                       if (ps->buf < ps->end)
-                               *ps->buf = '|';
-                       ps->buf++;
-               }
+               if (flags)
+                       printf_char(ps, '|');
        }
 
        if (flags) {
@@ -1702,11 +1687,8 @@ char *device_node_string(struct printf_state *ps, struct device_node *dn)
 
        for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
                int precision;
-               if (pass) {
-                       if (ps->buf < ps->end)
-                               *ps->buf = ':';
-                       ps->buf++;
-               }
+               if (pass)
+                       printf_char(ps, ':');
 
                switch (*fmt) {
                case 'f':       /* full_name */
@@ -1746,11 +1728,10 @@ char *device_node_string(struct printf_state *ps, struct device_node *dn)
                        has_mult = false;
                        of_property_for_each_string(dn, "compatible", prop, p) {
                                if (has_mult)
-                                       ps->buf = string(ps->buf, ps->end, ",", str_spec);
-                               ps->buf = string(ps->buf, ps->end, "\"", str_spec);
+                                       printf_char(ps, ',');
+                               printf_char(ps, '"');
                                ps->buf = string(ps->buf, ps->end, p, str_spec);
-                               ps->buf = string(ps->buf, ps->end, "\"", str_spec);
-
+                               printf_char(ps, '"');
                                has_mult = true;
                        }
                        break;