scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit 665b4e3615c9340253803722761e83a70ffefa7c
parent f75308d46c6b043f5b284fae0e149352f01f839c
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue,  5 Dec 2017 20:48:09 +0100

[lib/c] Fix number conversions with prefix

When we want to prefix with 0 a number we have to worry about the
prefix of the number, because if it begins with a sign or 0x then
we have to append the 0 after the prefix.

Diffstat:
Mlib/c/src/vfprintf.c | 22++++++++++++++++------
1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/lib/c/src/vfprintf.c b/lib/c/src/vfprintf.c @@ -80,6 +80,7 @@ numtostr(uintmax_t val, int flags, struct conv *conv, char *buf) { char *buf0 = buf; int len, base = conv->base, prec = conv->prec; + uintmax_t oval = val; if (prec == -1) prec = 1; @@ -89,14 +90,10 @@ numtostr(uintmax_t val, int flags, struct conv *conv, char *buf) while (buf0 - buf < prec) *--buf = '0'; - /* - * TODO: It cannot be done here because the combination - * %#04x produces 00x1 - */ if (flags & ALTFORM) { if (base == 8 && *buf != '0') { *--buf = '0'; - } else if (base == 16 && val != 0) { + } else if (base == 16 && oval != 0) { *--buf = conv->digs[16]; *--buf = '0'; } @@ -161,7 +158,7 @@ wstrout(wchar_t *ws, size_t len, int width, int fill, FILE * restrict fp) static size_t strout(char *s, size_t len, int width, int fill, FILE * restrict fp) { - int left = 0, adjust, ch; + int left = 0, adjust, ch, prefix; size_t cnt = 0; if (width < 0) { @@ -176,6 +173,19 @@ strout(char *s, size_t len, int width, int fill, FILE * restrict fp) if (left) adjust = -adjust; + if (fill == '0') { + if (*s == '-' || *s == '+') + prefix = 1; + else if (*s == '0' && toupper(s[1]) == 'X') + prefix = 2; + else + prefix = 0; + while (prefix--) { + putc(*s++, fp); + --len; + } + } + for ( ; adjust > 0; adjust--) putc(fill, fp);