Mercurial > hg > th-libs
view th_printf1.c @ 325:40ce4106f4ad
Add special case handling for f_prec == 0 && val == 0. Fixes several test cases.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 23 Feb 2016 12:16:12 +0200 |
parents | 0362ea9872f0 |
children | cda5a2aebbb6 |
line wrap: on
line source
/* * printf() helper implementation * Programmed and designed by Matti 'ccr' Hamalainen * (C) Copyright 2016 Tecnic Software productions (TNSP) * * Please read file 'COPYING' for information on license and distribution. */ static int TH_PFUNC_NAME (th_printf_ctx *ctx, th_printf_vputch vputch, #ifdef TH_PFUNC_SIGNED TH_PFUNC_TYPE_S pval, #else TH_PFUNC_TYPE_U val, #endif const int radix, const int f_flags, const int f_width, const int f_prec, #ifdef TH_PFUNC_SIGNED const BOOL unsig, #endif const BOOL upcase) { char buf[64]; size_t pos = 0; char f_sign = 0; #ifdef TH_PFUNC_SIGNED BOOL neg = FALSE; #endif if (radix > 16) return EOF; #ifdef TH_PFUNC_SIGNED // Check for negative value if (!unsig && pval < 0) { neg = TRUE; pval = -pval; } // Render the value to a string in buf (reversed) TH_PFUNC_TYPE_U val = pval; #endif // Special case for value of 0 and precision 0 if (val == 0 && f_prec == 0) return 0; do { TH_PFUNC_TYPE_U digit = val % radix; if (digit < 10) buf[pos] = '0' + digit; else buf[pos] = (upcase ? 'A' : 'a') + digit - 10; val /= radix; pos++; } while (val > 0 && pos < sizeof(buf) - 1); buf[pos] = 0; // Oops, the value did not fit in the buffer! if (val > 0) return EOF; // Do we want a sign prefix? Not for unsigned values #ifdef TH_PFUNC_SIGNED if (!unsig) f_sign = (f_flags & TH_PF_SIGN) ? (neg ? '-' : '+') : (neg ? '-' : ((f_flags & TH_PF_SPACE) ? ' ' : 0)); #endif // Output the data return th_printf_vput_intstr(ctx, vputch, buf, f_width, pos, f_prec, f_flags, f_sign); } #undef TH_PFUNC_NAME #undef TH_PFUNC_SIGNED #undef TH_PFUNC_TYPE_S #undef TH_PFUNC_TYPE_U