diff options
-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") |