diff options
author | Daisuke Nojiri <dnojiri@chromium.org> | 2016-07-27 14:06:17 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2016-07-29 10:51:22 -0700 |
commit | d3d6814b2d0579069e51c241bab0e1b4bc3c9cdc (patch) | |
tree | 7a0db69b053ed6f0cb7e349b927530dfc3491d6e | |
parent | 4e0c89c23a826f1d22bbd7f16bcd66536bf792f1 (diff) | |
download | chrome-ec-d3d6814b2d0579069e51c241bab0e1b4bc3c9cdc.tar.gz |
printf: Add sign ('+') flag
'+' flag can be used with signed integer type (%d) and causes positive
integers to be prefixed with '+' (e.g. +1745). This emphasizes output
values as a signed value. It can be mixed with left-justification flag
'-': %-+8d. It's ignored when used with unsigned integer or non-integer
types: %u, %x, %p, %s, %c, etc.
BUG=none
BRANCH=none
TEST=make buildall &&
int32_t d = 1745;
CPRINTS("'%-+8d'", -d); /* '-1745 ' */
CPRINTS("'%-+8d'", d); /* '+1745 ' */
CPRINTS("'%d'", d); /* '1745' */
CPRINTS("'%+08d'", -d); /* '000-1745' */
CPRINTS("'%+08d'", d); /* '000+1745' */
CPRINTS("'%+d'", -d); /* '-1745' */
CPRINTS("'%+d'", d); /* '+1745' */
CPRINTS("'%+s'", "foo"); /* 'foo' */
CPRINTS("'%-+8s'", "foo"); /* 'foo ' */
CPRINTS("'%+08x'", d); /* '000006d1' */
CPRINTS("'%+u'", d); /* '1745' */
Change-Id: I8dcd34b0cf03dbefc500b9c98fea235d85bde8d3
Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/363924
-rw-r--r-- | common/printf.c | 21 | ||||
-rw-r--r-- | include/printf.h | 3 |
2 files changed, 19 insertions, 5 deletions
diff --git a/common/printf.c b/common/printf.c index 987f7415ca..f22233c855 100644 --- a/common/printf.c +++ b/common/printf.c @@ -47,7 +47,7 @@ static int hexdigit(int c) /* Flags for vfnprintf() flags */ #define PF_LEFT (1 << 0) /* Left-justify */ #define PF_PADZERO (1 << 1) /* Pad with 0's not spaces */ -#define PF_NEGATIVE (1 << 2) /* Number is negative */ +#define PF_SIGN (1 << 2) /* Add sign (+) for a positive number */ #define PF_64BIT (1 << 3) /* Number is 64-bit */ int vfnprintf(int (*addchar)(void *context, int c), void *context, @@ -68,6 +68,7 @@ int vfnprintf(int (*addchar)(void *context, int c), void *context, while (*format) { int c = *format++; + char sign = 0; /* Copy normal characters */ if (c != '%') { @@ -103,6 +104,12 @@ int vfnprintf(int (*addchar)(void *context, int c), void *context, c = *format++; } + /* Handle positive sign (%+d) */ + if (c == '+') { + flags |= PF_SIGN; + c = *format++; + } + /* Handle padding with 0's */ if (c == '0') { flags |= PF_PADZERO; @@ -198,15 +205,19 @@ int vfnprintf(int (*addchar)(void *context, int c), void *context, case 'd': if (flags & PF_64BIT) { if ((int64_t)v < 0) { - flags |= PF_NEGATIVE; + sign = '-'; if (v != (1ULL << 63)) v = -v; + } else if (flags & PF_SIGN) { + sign = '+'; } } else { if ((int)v < 0) { - flags |= PF_NEGATIVE; + sign = '-'; if (v != (1ULL << 31)) v = -(int)v; + } else if (flags & PF_SIGN) { + sign = '+'; } } break; @@ -263,8 +274,8 @@ int vfnprintf(int (*addchar)(void *context, int c), void *context, *(--vstr) = 'a' + digit - 10; } - if (flags & PF_NEGATIVE) - *(--vstr) = '-'; + if (sign) + *(--vstr) = sign; /* * Precision field was interpreted by fixed-point diff --git a/include/printf.h b/include/printf.h index 109fae4e2e..0a26ea0daa 100644 --- a/include/printf.h +++ b/include/printf.h @@ -18,6 +18,9 @@ * order if present: * - '0' = prefixed with 0's instead of spaces (%08x) * - '-' = left-justify instead of right-justify (%-5s) + * - '+' = prefix positive value with '+' (%+d). Write '%-+' instead of + * '%+-' when used with left-justification. Ignored when + * used with unsigned integer types or non-integer types. * * Width is the minimum output width, and may be: * - A number ("0" - "255") |