# HG changeset patch # User Matti Hamalainen # Date 1457518005 -7200 # Node ID 9b1b21954952d3bce8b35eba6f33d1143eb35b26 # Parent 6b50bf4317c043405cbe4bb0bf2454d33fd28f25 Improve altfmt modifier handling for printf implementation. Not complete/perfect or very nice yet. diff -r 6b50bf4317c0 -r 9b1b21954952 th_string.c --- 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 ""; }