Skip to content

Commit

Permalink
Regards mpaland#116 : Defined a (hopefully) appropriate type and some…
Browse files Browse the repository at this point in the history
… constants for numeric base variables; no functionality change.
  • Loading branch information
eyalroz committed Aug 3, 2021
1 parent b232802 commit 20ae081
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@
#define FLAGS_POINTER (1U << 12U)
// Note: Similar, but not identical, effect as FLAGS_HASH

#define BASE_BINARY 2
#define BASE_OCTAL 8
#define BASE_DECIMAL 10
#define BASE_HEX 16

typedef uint8_t numeric_base_t;

// import float.h for DBL_MAX
#if PRINTF_SUPPORT_FLOAT_SPECIFIERS
Expand Down Expand Up @@ -234,7 +240,7 @@ 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)
static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, numeric_base_t base, unsigned int precision, unsigned int width, unsigned int flags)
{
size_t unpadded_len = len;

Expand All @@ -253,7 +259,7 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma
buf[len++] = '0';
}

if (base == 8U && (len > unpadded_len)) {
if (base == BASE_OCTAL && (len > unpadded_len)) {
// Since we've written some zeros, we've satisfied the alternative format leading space requirement
flags &= ~FLAGS_HASH;
}
Expand All @@ -267,19 +273,19 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma
if (unpadded_len < len) {
len--;
}
if (len && (base == 16U)) {
if (len && (base == BASE_HEX)) {
if (unpadded_len < len) {
len--;
}
}
}
if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
if ((base == BASE_HEX) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = 'x';
}
else if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
else if ((base == BASE_HEX) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = 'X';
}
else if ((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
else if ((base == BASE_BINARY) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = 'b';
}
if (len < PRINTF_NTOA_BUFFER_SIZE) {
Expand All @@ -304,7 +310,7 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma


// internal itoa
static size_t _ntoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, NTOA_VALUE_TYPE value, bool negative, NTOA_VALUE_TYPE base, unsigned int precision, unsigned int width, unsigned int flags)
static size_t _ntoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, NTOA_VALUE_TYPE value, bool negative, numeric_base_t base, unsigned int precision, unsigned int width, unsigned int flags)
{
char buf[PRINTF_NTOA_BUFFER_SIZE];
size_t len = 0U;
Expand All @@ -317,7 +323,7 @@ static size_t _ntoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, N
// don't differ on 0 values, or (in the case of octal) we've already provided the special
// handling for this mode.
}
else if (base == 16U) {
else if (base == BASE_HEX) {
flags &= ~FLAGS_HASH;
// We drop this flag this since either the alternative and regular modes of the specifier
// don't differ on 0 values
Expand All @@ -331,7 +337,7 @@ static size_t _ntoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, N
} while (value && (len < PRINTF_NTOA_BUFFER_SIZE));
}

return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, precision, width, flags);
return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, base, precision, width, flags);
}


Expand Down Expand Up @@ -721,18 +727,18 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
case 'o' :
case 'b' : {
// set the base
unsigned int base;
numeric_base_t base;
if (*format == 'x' || *format == 'X') {
base = 16U;
base = BASE_HEX;
}
else if (*format == 'o') {
base = 8U;
base = BASE_OCTAL;
}
else if (*format == 'b') {
base = 2U;
base = BASE_BINARY;
}
else {
base = 10U;
base = BASE_DECIMAL;
flags &= ~FLAGS_HASH; // no hash for dec format
}
// uppercase
Expand Down Expand Up @@ -868,11 +874,11 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
#if PRINTF_SUPPORT_LONG_LONG
const bool is_ll = sizeof(uintptr_t) == sizeof(long long);
if (is_ll) {
idx = _ntoa(out, buffer, idx, maxlen, (NTOA_VALUE_TYPE) value, false, 16U, precision, width, flags);
idx = _ntoa(out, buffer, idx, maxlen, (NTOA_VALUE_TYPE) value, false, BASE_HEX, precision, width, flags);
}
else {
#endif
idx = _ntoa(out, buffer, idx, maxlen, (NTOA_VALUE_TYPE)((uintptr_t)va_arg(va, void*)), false, 16U, precision, width, flags);
idx = _ntoa(out, buffer, idx, maxlen, (NTOA_VALUE_TYPE)((uintptr_t)va_arg(va, void*)), false, BASE_HEX, precision, width, flags);
#if PRINTF_SUPPORT_LONG_LONG
}
#endif
Expand Down

0 comments on commit 20ae081

Please sign in to comment.