From: Matthew Wilcox Date: Wed, 19 Dec 2018 20:59:27 +0000 (-0500) Subject: printf: Add printf_char X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=3a4b7eb9d7e321e700bbbc06e1714ff05d59314a;p=users%2Fwilly%2Flinux.git printf: Add printf_char Convert all open-coded versions. Signed-off-by: Matthew Wilcox --- diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 375088634bca..ceab03dd297f 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -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;