Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalroz committed Jul 30, 2021
1 parent 35d8428 commit 47b3f0e
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma
{
// 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 @@ -240,6 +241,12 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma
while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = '0';
}
if (base == 8U && (flags & FLAGS_HASH)) {
if (len > initial_len) {
// Since we've written some zeros, we've satisfied the alternative format leading space requirement
flags &= ~FLAGS_HASH;
}
}
}

// handle hash
Expand Down Expand Up @@ -286,13 +293,18 @@ static size_t _ntoa_long(out_fct_type out, char* buffer, size_t idx, size_t maxl
char buf[PRINTF_NTOA_BUFFER_SIZE];
size_t len = 0U;

// no hash for 0 values
if (!value) {
if (!(flags & FLAGS_PRECISION)
|| (base == 8U && (flags & ~FLAGS_HASH))
) {
buf[len++] = '0';
}
flags &= ~FLAGS_HASH;
// We drop this flag this since either the alternative and regular modes of the specifier
// don't differ on 0 values, or (in the case of octal) we've already provided the special
// handling for this mode.
}

// write if precision != 0 and value is != 0
if (!(flags & FLAGS_PRECISION) || value) {
else {
do {
const char digit = (char)(value % base);
buf[len++] = (char)(digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10);
Expand Down

0 comments on commit 47b3f0e

Please sign in to comment.