From 2019bc0192a1b69036b0659f642a9e7e6c426a32 Mon Sep 17 00:00:00 2001 From: Martijn Jasperse Date: Fri, 1 Feb 2019 13:52:24 +1100 Subject: Fix for left-justified %E formatting Adding %E/%G test cases to suite --- printf.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'printf.c') diff --git a/printf.c b/printf.c index 7c346bf..0ee5c3e 100644 --- a/printf.c +++ b/printf.c @@ -487,19 +487,25 @@ static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d } } // will everything fit? + unsigned int fwidth = width; if (width > minwidth) { // we didn't fall-back so subtract the characters required for the exponent - width -= minwidth; + fwidth -= minwidth; } else { // not enough characters, so go back to default sizing - width = 0; + fwidth = 0; + } + if ((flags & FLAGS_LEFT) && minwidth) { + // if we're padding on the right, DON'T pad the floating part + fwidth = 0; } // rescale the float value if (expval) value *= pow(10.0, -expval); // output the floating part - idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, width, flags & ~FLAGS_ADAPT_EXP); + const size_t start_idx = idx; + idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP); // output the exponent part if (minwidth) { @@ -507,6 +513,10 @@ static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen); // output the exponent value idx = _ntoa_long(out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth-1, FLAGS_ZEROPAD | FLAGS_PLUS); + // might need to right-pad spaces + if (flags & FLAGS_LEFT) { + while (idx - start_idx < width) out(' ', buffer, idx++, maxlen); + } } return idx; } -- cgit v1.2.3