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
This commit is contained in:
Daisuke Nojiri
2016-07-27 14:06:17 -07:00
committed by chrome-bot
parent 4e0c89c23a
commit d3d6814b2d
2 changed files with 19 additions and 5 deletions

View File

@@ -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

View File

@@ -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")