Skip to content

Commit

Permalink
Fixes mpaland#99 : Now only "taking back" digits in favor of prefix c…
Browse files Browse the repository at this point in the history
…haracters only if those were added as padding.
  • Loading branch information
eyalroz committed Jul 30, 2021
1 parent 65a3d2b commit 87e1ae9
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ static size_t _out_rev(out_fct_type out, char* buffer, size_t idx, size_t maxlen
// internal itoa format
static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int precision, unsigned int width, unsigned int flags)
{
size_t unpadded_len = len;
// pad leading zeros
if (!(flags & FLAGS_LEFT)) {
size_t initial_len = len;
if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
width--;
}
Expand All @@ -242,7 +242,7 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma
buf[len++] = '0';
}
if (base == 8U && (flags & FLAGS_HASH)) {
if (len > initial_len) {
if (len > unpadded_len) {
// Since we've written some zeros, we've satisfied the alternative format leading space requirement
flags &= ~FLAGS_HASH;
}
Expand All @@ -252,10 +252,16 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma
// handle hash
if (flags & (FLAGS_HASH | FLAGS_POINTER)) {
if (!(flags & FLAGS_PRECISION) && len && ((len == precision) || (len == width))) {
len--;
if (len && (base == 16U)) {
// Let's take back some padding digits to fit in what will eventually
// be the format-specific prefix
if (unpadded_len < len) {
len--;
}
if (len && (base == 16U)) {
if (unpadded_len < len) {
len--;
}
}
}
if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = 'x';
Expand Down

0 comments on commit 87e1ae9

Please sign in to comment.