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 5ab1e6f commit 0fe7787
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 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 @@ -250,10 +250,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 0fe7787

Please sign in to comment.