diff options
author | Brian Norris <computersforpeace@gmail.com> | 2010-11-29 00:01:57 -0800 |
---|---|---|
committer | Artem Bityutskiy <Artem.Bityutskiy@nokia.com> | 2010-12-02 05:25:33 +0200 |
commit | 5a4d06c0fc94cf48d5bab3afa97b16c9022c4181 (patch) | |
tree | ff442e859a02dcb6468b99c61d7d2ba9bdea7968 | |
parent | 3941e9fd08f1d03a52f6584b70077ff0ac866696 (diff) |
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 <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
-rw-r--r-- | nanddump.c | 7 |
1 files changed, 4 insertions, 3 deletions
@@ -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++) |