Skip to content

Commit 20ae081

Browse files
committed
Regards mpaland#116 : Defined a (hopefully) appropriate type and some constants for numeric base variables; no functionality change.
1 parent b232802 commit 20ae081

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

printf.c

+22-16
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@
120120
#define FLAGS_POINTER (1U << 12U)
121121
// Note: Similar, but not identical, effect as FLAGS_HASH
122122

123+
#define BASE_BINARY 2
124+
#define BASE_OCTAL 8
125+
#define BASE_DECIMAL 10
126+
#define BASE_HEX 16
127+
128+
typedef uint8_t numeric_base_t;
123129

124130
// import float.h for DBL_MAX
125131
#if PRINTF_SUPPORT_FLOAT_SPECIFIERS
@@ -234,7 +240,7 @@ static size_t _out_rev(out_fct_type out, char* buffer, size_t idx, size_t maxlen
234240

235241

236242
// internal itoa format
237-
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)
243+
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)
238244
{
239245
size_t unpadded_len = len;
240246

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

256-
if (base == 8U && (len > unpadded_len)) {
262+
if (base == BASE_OCTAL && (len > unpadded_len)) {
257263
// Since we've written some zeros, we've satisfied the alternative format leading space requirement
258264
flags &= ~FLAGS_HASH;
259265
}
@@ -267,19 +273,19 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma
267273
if (unpadded_len < len) {
268274
len--;
269275
}
270-
if (len && (base == 16U)) {
276+
if (len && (base == BASE_HEX)) {
271277
if (unpadded_len < len) {
272278
len--;
273279
}
274280
}
275281
}
276-
if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
282+
if ((base == BASE_HEX) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
277283
buf[len++] = 'x';
278284
}
279-
else if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
285+
else if ((base == BASE_HEX) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
280286
buf[len++] = 'X';
281287
}
282-
else if ((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
288+
else if ((base == BASE_BINARY) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
283289
buf[len++] = 'b';
284290
}
285291
if (len < PRINTF_NTOA_BUFFER_SIZE) {
@@ -304,7 +310,7 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma
304310

305311

306312
// internal itoa
307-
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)
313+
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)
308314
{
309315
char buf[PRINTF_NTOA_BUFFER_SIZE];
310316
size_t len = 0U;
@@ -317,7 +323,7 @@ static size_t _ntoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, N
317323
// don't differ on 0 values, or (in the case of octal) we've already provided the special
318324
// handling for this mode.
319325
}
320-
else if (base == 16U) {
326+
else if (base == BASE_HEX) {
321327
flags &= ~FLAGS_HASH;
322328
// We drop this flag this since either the alternative and regular modes of the specifier
323329
// don't differ on 0 values
@@ -331,7 +337,7 @@ static size_t _ntoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, N
331337
} while (value && (len < PRINTF_NTOA_BUFFER_SIZE));
332338
}
333339

334-
return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, precision, width, flags);
340+
return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, base, precision, width, flags);
335341
}
336342

337343

@@ -721,18 +727,18 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
721727
case 'o' :
722728
case 'b' : {
723729
// set the base
724-
unsigned int base;
730+
numeric_base_t base;
725731
if (*format == 'x' || *format == 'X') {
726-
base = 16U;
732+
base = BASE_HEX;
727733
}
728734
else if (*format == 'o') {
729-
base = 8U;
735+
base = BASE_OCTAL;
730736
}
731737
else if (*format == 'b') {
732-
base = 2U;
738+
base = BASE_BINARY;
733739
}
734740
else {
735-
base = 10U;
741+
base = BASE_DECIMAL;
736742
flags &= ~FLAGS_HASH; // no hash for dec format
737743
}
738744
// uppercase
@@ -868,11 +874,11 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
868874
#if PRINTF_SUPPORT_LONG_LONG
869875
const bool is_ll = sizeof(uintptr_t) == sizeof(long long);
870876
if (is_ll) {
871-
idx = _ntoa(out, buffer, idx, maxlen, (NTOA_VALUE_TYPE) value, false, 16U, precision, width, flags);
877+
idx = _ntoa(out, buffer, idx, maxlen, (NTOA_VALUE_TYPE) value, false, BASE_HEX, precision, width, flags);
872878
}
873879
else {
874880
#endif
875-
idx = _ntoa(out, buffer, idx, maxlen, (NTOA_VALUE_TYPE)((uintptr_t)va_arg(va, void*)), false, 16U, precision, width, flags);
881+
idx = _ntoa(out, buffer, idx, maxlen, (NTOA_VALUE_TYPE)((uintptr_t)va_arg(va, void*)), false, BASE_HEX, precision, width, flags);
876882
#if PRINTF_SUPPORT_LONG_LONG
877883
}
878884
#endif

0 commit comments

Comments
 (0)