120
120
#define FLAGS_POINTER (1U << 12U)
121
121
// Note: Similar, but not identical, effect as FLAGS_HASH
122
122
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 ;
123
129
124
130
// import float.h for DBL_MAX
125
131
#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
234
240
235
241
236
242
// 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 )
238
244
{
239
245
size_t unpadded_len = len ;
240
246
@@ -253,7 +259,7 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma
253
259
buf [len ++ ] = '0' ;
254
260
}
255
261
256
- if (base == 8U && (len > unpadded_len )) {
262
+ if (base == BASE_OCTAL && (len > unpadded_len )) {
257
263
// Since we've written some zeros, we've satisfied the alternative format leading space requirement
258
264
flags &= ~FLAGS_HASH ;
259
265
}
@@ -267,19 +273,19 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma
267
273
if (unpadded_len < len ) {
268
274
len -- ;
269
275
}
270
- if (len && (base == 16U )) {
276
+ if (len && (base == BASE_HEX )) {
271
277
if (unpadded_len < len ) {
272
278
len -- ;
273
279
}
274
280
}
275
281
}
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 )) {
277
283
buf [len ++ ] = 'x' ;
278
284
}
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 )) {
280
286
buf [len ++ ] = 'X' ;
281
287
}
282
- else if ((base == 2U ) && (len < PRINTF_NTOA_BUFFER_SIZE )) {
288
+ else if ((base == BASE_BINARY ) && (len < PRINTF_NTOA_BUFFER_SIZE )) {
283
289
buf [len ++ ] = 'b' ;
284
290
}
285
291
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
304
310
305
311
306
312
// 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 )
308
314
{
309
315
char buf [PRINTF_NTOA_BUFFER_SIZE ];
310
316
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
317
323
// don't differ on 0 values, or (in the case of octal) we've already provided the special
318
324
// handling for this mode.
319
325
}
320
- else if (base == 16U ) {
326
+ else if (base == BASE_HEX ) {
321
327
flags &= ~FLAGS_HASH ;
322
328
// We drop this flag this since either the alternative and regular modes of the specifier
323
329
// 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
331
337
} while (value && (len < PRINTF_NTOA_BUFFER_SIZE ));
332
338
}
333
339
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 );
335
341
}
336
342
337
343
@@ -721,18 +727,18 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
721
727
case 'o' :
722
728
case 'b' : {
723
729
// set the base
724
- unsigned int base ;
730
+ numeric_base_t base ;
725
731
if (* format == 'x' || * format == 'X' ) {
726
- base = 16U ;
732
+ base = BASE_HEX ;
727
733
}
728
734
else if (* format == 'o' ) {
729
- base = 8U ;
735
+ base = BASE_OCTAL ;
730
736
}
731
737
else if (* format == 'b' ) {
732
- base = 2U ;
738
+ base = BASE_BINARY ;
733
739
}
734
740
else {
735
- base = 10U ;
741
+ base = BASE_DECIMAL ;
736
742
flags &= ~FLAGS_HASH ; // no hash for dec format
737
743
}
738
744
// uppercase
@@ -868,11 +874,11 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
868
874
#if PRINTF_SUPPORT_LONG_LONG
869
875
const bool is_ll = sizeof (uintptr_t ) == sizeof (long long );
870
876
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 );
872
878
}
873
879
else {
874
880
#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 );
876
882
#if PRINTF_SUPPORT_LONG_LONG
877
883
}
878
884
#endif
0 commit comments