From: Brian Norris Date: Mon, 29 Nov 2010 08:01:57 +0000 (-0800) Subject: nanddump: Refactor pretty print code into an sprintf() X-Git-Tag: v1.4.2~7 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=5a4d06c0fc94cf48d5bab3afa97b16c9022c4181;p=mtd-utils.git nanddump: Refactor pretty print code into an sprintf() A do-while loop in pretty_dump_to_buffer() can be refactored into a single sprintf() statement. MAX() and MIN() are used to ensure that: (1) We have at least a single space between hex and ASCII output (2) We don't overflow the line buffer This patch was suggested by Mike Frysinger. Signed-off-by: Brian Norris Signed-off-by: Artem Bityutskiy --- diff --git a/nanddump.c b/nanddump.c index bf95e81..14a8816 100644 --- a/nanddump.c +++ b/nanddump.c @@ -252,9 +252,10 @@ static void pretty_dump_to_buffer(const unsigned char *buf, size_t len, if (!ascii) goto nil; - do { - linebuf[lx++] = ' '; - } while (lx < (linebuflen - 1) && lx < (ascii_column - 1)); + /* Spacing between hex and ASCII - ensure at least one space */ + lx += sprintf(linebuf + lx, "%*s", + MAX((int)MIN(linebuflen, ascii_column) - 1 - lx, 1), + " "); linebuf[lx++] = '|'; for (j = 0; (j < len) && (lx + 2) < linebuflen; j++)