Mercurial > hg > th-libs
changeset 409:9b1b21954952
Improve altfmt modifier handling for printf implementation. Not
complete/perfect or very nice yet.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 09 Mar 2016 12:06:45 +0200 |
parents | 6b50bf4317c0 |
children | 04cb03baf114 |
files | th_string.c |
diffstat | 1 files changed, 9 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/th_string.c Wed Mar 09 12:05:16 2016 +0200 +++ b/th_string.c Wed Mar 09 12:06:45 2016 +0200 @@ -196,7 +196,7 @@ static int th_vprintf_put_int(th_vprintf_ctx *ctx, th_vprintf_putch vputch, va_list ap, const int f_radix, int f_flags, int f_width, int f_prec, - const BOOL f_unsig, char *(f_alt)(const char *buf, const int vret, const int flags)) + const BOOL f_unsig, char *(f_alt)(const char *buf, const size_t blen, const int vret, const int flags)) { char buf[64]; int f_len = 0, ret = 0, vret, nwidth, nprec; @@ -239,7 +239,7 @@ } // Get alternative format string, if needed and available - f_altstr = vret != 0 && (f_flags & TH_PF_ALT) && f_alt != NULL ? f_alt(buf, vret, f_flags) : NULL; + f_altstr = (f_flags & TH_PF_ALT) && f_alt != NULL ? f_alt(buf, f_len, vret, f_flags) : NULL; // Are we using a sign prefix? f_sign = f_unsig ? 0 : ((f_flags & TH_PF_SIGN) ? @@ -353,20 +353,22 @@ } -static char * th_printf_altfmt_oct(const char *buf, const int vret, const int flags) +static char * th_printf_altfmt_oct(const char *buf, const size_t len, const int vret, const int flags) { (void) vret; (void) flags; - return (buf[0] != '0') ? "0" : ""; -; + return (buf[len - 1] != '0') ? "0" : ""; } -static char * th_printf_altfmt_hex(const char *buf, const int vret, const int flags) +static char * th_printf_altfmt_hex(const char *buf, const size_t len, const int vret, const int flags) { (void) buf; (void) vret; - return (flags & TH_PF_UPCASE) ? "0X" : "0x"; + if (vret != 0) + return (flags & TH_PF_UPCASE) ? "0X" : "0x"; + else + return ""; }