@@ -144,6 +144,8 @@ typedef unsigned int printf_flags_t;
144
144
#define BASE_DECIMAL 10
145
145
#define BASE_HEX 16
146
146
147
+ typedef uint8_t numeric_base_t ;
148
+
147
149
#if PRINTF_SUPPORT_LONG_LONG
148
150
typedef unsigned long long printf_unsigned_value_t ;
149
151
typedef long long printf_signed_value_t ;
@@ -152,10 +154,12 @@ typedef unsigned long printf_unsigned_value_t;
152
154
typedef long printf_signed_value_t ;
153
155
#endif
154
156
155
- typedef uint8_t numeric_base_t ;
156
-
157
- typedef unsigned int printf_precision_t ;
158
- typedef unsigned int printf_width_t ;
157
+ // The printf()-family functions return an `int`; it is therefore
158
+ // unnecessary/inappropriate to use size_t - often larger than int
159
+ // in practice - for non-negative related values, such as widths,
160
+ // precisions, offsets into buffers used for printing and the sizes
161
+ // of these buffers. instead, we use:
162
+ typedef unsigned int printf_size_t ;
159
163
160
164
#if (PRINTF_SUPPORT_DECIMAL_SPECIFIERS || PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS )
161
165
#include <float.h>
@@ -188,7 +192,9 @@ typedef union {
188
192
} double_with_bit_access ;
189
193
190
194
// This is unnecessary in C99, since compound initializers can be used,
191
- // but: 1. Some compilers are finicky about this; 2. Some people may want to convert this to C89;
195
+ // but:
196
+ // 1. Some compilers are finicky about this;
197
+ // 2. Some people may want to convert this to C89;
192
198
// 3. If you try to use it as C++, only C++20 supports compound literals
193
199
static inline double_with_bit_access get_bit_access (double x )
194
200
{
@@ -270,11 +276,13 @@ static inline void out_wrapped_function(char character, void* wrapped_function,
270
276
271
277
// internal secure strlen
272
278
// @return The length of the string (excluding the terminating 0) limited by 'maxsize'
273
- static inline size_t strnlen_s_ (const char * str , size_t maxsize )
279
+ // @note strlen uses size_t, but wes only use this function with printf_size_t
280
+ // variables - hence the signature.
281
+ static inline printf_size_t strnlen_s_ (const char * str , printf_size_t maxsize )
274
282
{
275
283
const char * s ;
276
284
for (s = str ; * s && maxsize -- ; ++ s );
277
- return (size_t )(s - str );
285
+ return (printf_size_t )(s - str );
278
286
}
279
287
280
288
@@ -286,19 +294,19 @@ static inline bool is_digit_(char ch)
286
294
}
287
295
288
296
289
- // internal ASCII string to unsigned int conversion
290
- static unsigned int atoi_ (const char * * str )
297
+ // internal ASCII string to printf_size_t conversion
298
+ static printf_size_t atou_ (const char * * str )
291
299
{
292
- unsigned int i = 0U ;
300
+ printf_size_t i = 0U ;
293
301
while (is_digit_ (* * str )) {
294
- i = i * 10U + (unsigned int )(* ((* str )++ ) - '0' );
302
+ i = i * 10U + (printf_size_t )(* ((* str )++ ) - '0' );
295
303
}
296
304
return i ;
297
305
}
298
306
299
307
300
308
// output the specified string in reverse, taking care of any zero-padding
301
- static size_t out_rev_ (out_fct_type out , char * buffer , size_t idx , size_t maxlen , const char * buf , size_t len , printf_width_t width , printf_flags_t flags )
309
+ static size_t out_rev_ (out_fct_type out , char * buffer , size_t idx , size_t maxlen , const char * buf , size_t len , printf_size_t width , printf_flags_t flags )
302
310
{
303
311
const size_t start_idx = idx ;
304
312
@@ -327,7 +335,7 @@ static size_t out_rev_(out_fct_type out, char* buffer, size_t idx, size_t maxlen
327
335
328
336
// Invoked by print_integer after the actual number has been printed, performing necessary
329
337
// work on the number's prefix (as the number is initially printed in reverse order)
330
- static size_t print_integer_finalization (out_fct_type out , char * buffer , size_t idx , size_t maxlen , char * buf , size_t len , bool negative , numeric_base_t base , printf_precision_t precision , printf_width_t width , printf_flags_t flags )
338
+ static size_t print_integer_finalization (out_fct_type out , char * buffer , size_t idx , size_t maxlen , char * buf , size_t len , bool negative , numeric_base_t base , printf_size_t precision , printf_size_t width , printf_flags_t flags )
331
339
{
332
340
size_t unpadded_len = len ;
333
341
@@ -394,7 +402,7 @@ static size_t print_integer_finalization(out_fct_type out, char* buffer, size_t
394
402
}
395
403
396
404
// An internal itoa-like function
397
- static size_t print_integer (out_fct_type out , char * buffer , size_t idx , size_t maxlen , printf_unsigned_value_t value , bool negative , numeric_base_t base , printf_precision_t precision , printf_width_t width , printf_flags_t flags )
405
+ static size_t print_integer (out_fct_type out , char * buffer , size_t idx , size_t maxlen , printf_unsigned_value_t value , bool negative , numeric_base_t base , printf_size_t precision , printf_size_t width , printf_flags_t flags )
398
406
{
399
407
char buf [PRINTF_INTEGER_BUFFER_SIZE ];
400
408
size_t len = 0U ;
@@ -445,7 +453,7 @@ static const double powers_of_10[NUM_DECIMAL_DIGITS_IN_INT64_T] = {
445
453
// Break up a double number - which is known to be a finite non-negative number -
446
454
// into its base-10 parts: integral - before the decimal point, and fractional - after it.
447
455
// Taken the precision into account, but does not change it even internally.
448
- static struct double_components get_components (double number , printf_precision_t precision )
456
+ static struct double_components get_components (double number , printf_size_t precision )
449
457
{
450
458
struct double_components number_ ;
451
459
number_ .is_negative = get_sign (number );
@@ -520,7 +528,7 @@ static struct scaling_factor update_normalization(struct scaling_factor sf, doub
520
528
return result ;
521
529
}
522
530
523
- static struct double_components get_normalized_components (bool negative , printf_precision_t precision , double non_normalized , struct scaling_factor normalization )
531
+ static struct double_components get_normalized_components (bool negative , printf_size_t precision , double non_normalized , struct scaling_factor normalization )
524
532
{
525
533
struct double_components components ;
526
534
components .is_negative = negative ;
@@ -560,13 +568,13 @@ static struct double_components get_normalized_components(bool negative, printf_
560
568
#endif
561
569
562
570
static size_t print_broken_up_decimal (
563
- struct double_components number_ , out_fct_type out , char * buffer , size_t idx , size_t maxlen , printf_precision_t precision ,
564
- printf_width_t width , printf_flags_t flags , char * buf , size_t len )
571
+ struct double_components number_ , out_fct_type out , char * buffer , size_t idx , size_t maxlen , printf_size_t precision ,
572
+ printf_size_t width , printf_flags_t flags , char * buf , size_t len )
565
573
{
566
574
if (precision != 0U ) {
567
575
// do fractional part, as an unsigned number
568
576
569
- unsigned int count = precision ;
577
+ printf_size_t count = precision ;
570
578
571
579
// %g/%G mandates we skip the trailing 0 digits...
572
580
if ((flags & FLAGS_ADAPT_EXP ) && !(flags & FLAGS_HASH ) && (number_ .fractional > 0 )) {
@@ -642,15 +650,15 @@ static size_t print_broken_up_decimal(
642
650
}
643
651
644
652
// internal ftoa for fixed decimal floating point
645
- static size_t print_decimal_number (out_fct_type out , char * buffer , size_t idx , size_t maxlen , double number , printf_precision_t precision , printf_width_t width , printf_flags_t flags , char * buf , size_t len )
653
+ static size_t print_decimal_number (out_fct_type out , char * buffer , size_t idx , size_t maxlen , double number , printf_size_t precision , printf_size_t width , printf_flags_t flags , char * buf , size_t len )
646
654
{
647
655
struct double_components value_ = get_components (number , precision );
648
656
return print_broken_up_decimal (value_ , out , buffer , idx , maxlen , precision , width , flags , buf , len );
649
657
}
650
658
651
659
#if PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS
652
660
// internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse <[email protected] >
653
- static size_t print_exponential_number (out_fct_type out , char * buffer , size_t idx , size_t maxlen , double number , printf_precision_t precision , printf_width_t width , printf_flags_t flags , char * buf , size_t len )
661
+ static size_t print_exponential_number (out_fct_type out , char * buffer , size_t idx , size_t maxlen , double number , printf_size_t precision , printf_size_t width , printf_flags_t flags , char * buf , size_t len )
654
662
{
655
663
const bool negative = get_sign (number );
656
664
// This number will decrease gradually (by factors of 10) as we "extract" the exponent out of it
@@ -707,7 +715,7 @@ static size_t print_exponential_number(out_fct_type out, char* buffer, size_t id
707
715
// This also decided how we adjust the precision value - as in "%g" mode,
708
716
// "precision" is the number of _significant digits_, and this is when we "translate"
709
717
// the precision value to an actual number of decimal digits.
710
- int precision_ = ( fall_back_to_decimal_only_mode ) ?
718
+ int precision_ = fall_back_to_decimal_only_mode ?
711
719
(int ) precision - 1 - exp10 :
712
720
(int ) precision - 1 ; // the presence of the exponent ensures only one significant digit comes before the decimal point
713
721
precision = (precision_ > 0 ? (unsigned ) precision_ : 0U );
@@ -741,9 +749,9 @@ static size_t print_exponential_number(out_fct_type out, char* buffer, size_t id
741
749
742
750
// the exp10 format is "E%+03d" and largest possible exp10 value for a 64-bit double
743
751
// is "307" (for 2^1023), so we set aside 4-5 characters overall
744
- printf_width_t exp10_part_width = fall_back_to_decimal_only_mode ? 0U : (PRINTF_ABS (exp10 ) < 100 ) ? 4U : 5U ;
752
+ printf_size_t exp10_part_width = fall_back_to_decimal_only_mode ? 0U : (PRINTF_ABS (exp10 ) < 100 ) ? 4U : 5U ;
745
753
746
- printf_width_t decimal_part_width =
754
+ printf_size_t decimal_part_width =
747
755
((flags & FLAGS_LEFT ) && exp10_part_width ) ?
748
756
// We're padding on the right, so the width constraint is the exponent part's
749
757
// problem, not the decimal part's, so we'll use as many characters as we need:
@@ -776,7 +784,7 @@ static size_t print_exponential_number(out_fct_type out, char* buffer, size_t id
776
784
}
777
785
#endif // PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS
778
786
779
- static size_t print_floating_point (out_fct_type out , char * buffer , size_t idx , size_t maxlen , double value , printf_precision_t precision , printf_width_t width , printf_flags_t flags , bool prefer_exponential )
787
+ static size_t print_floating_point (out_fct_type out , char * buffer , size_t idx , size_t maxlen , double value , printf_size_t precision , printf_size_t width , printf_flags_t flags , bool prefer_exponential )
780
788
{
781
789
char buf [PRINTF_FTOA_BUFFER_SIZE ];
782
790
size_t len = 0U ;
@@ -822,13 +830,11 @@ static size_t print_floating_point(out_fct_type out, char* buffer, size_t idx, s
822
830
#endif // (PRINTF_SUPPORT_DECIMAL_SPECIFIERS || PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS)
823
831
824
832
// internal vsnprintf
825
- static int _vsnprintf (out_fct_type out , char * buffer , const size_t maxlen , const char * format , va_list va )
833
+ static int _vsnprintf (out_fct_type out , char * buffer , printf_size_t maxlen , const char * format , va_list va )
826
834
{
827
835
printf_flags_t flags ;
828
- printf_width_t width ;
829
- printf_precision_t precision ;
830
- unsigned int n ;
831
- size_t idx = 0U ;
836
+ printf_size_t width , precision , n ;
837
+ printf_size_t idx = 0U ;
832
838
833
839
if (!buffer ) {
834
840
// use null output function
@@ -865,16 +871,16 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
865
871
// evaluate width field
866
872
width = 0U ;
867
873
if (is_digit_ (* format )) {
868
- width = atoi_ (& format );
874
+ width = ( printf_size_t ) atou_ (& format );
869
875
}
870
876
else if (* format == '*' ) {
871
877
const int w = va_arg (va , int );
872
878
if (w < 0 ) {
873
879
flags |= FLAGS_LEFT ; // reverse padding
874
- width = (printf_width_t )- w ;
880
+ width = (printf_size_t )- w ;
875
881
}
876
882
else {
877
- width = (printf_width_t )w ;
883
+ width = (printf_size_t )w ;
878
884
}
879
885
format ++ ;
880
886
}
@@ -885,11 +891,11 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
885
891
flags |= FLAGS_PRECISION ;
886
892
format ++ ;
887
893
if (is_digit_ (* format )) {
888
- precision = atoi_ (& format );
894
+ precision = ( printf_size_t ) atou_ (& format );
889
895
}
890
896
else if (* format == '*' ) {
891
897
const int precision_ = va_arg (va , int );
892
- precision = precision_ > 0 ? (printf_precision_t ) precision_ : 0U ;
898
+ precision = precision_ > 0 ? (printf_size_t ) precision_ : 0U ;
893
899
format ++ ;
894
900
}
895
901
}
@@ -1023,7 +1029,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
1023
1029
break ;
1024
1030
#endif // PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS
1025
1031
case 'c' : {
1026
- unsigned int l = 1U ;
1032
+ printf_size_t l = 1U ;
1027
1033
// pre padding
1028
1034
if (!(flags & FLAGS_LEFT )) {
1029
1035
while (l ++ < width ) {
@@ -1048,7 +1054,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
1048
1054
idx = out_rev_ (out , buffer , idx , maxlen , ")llun(" , 6 , width , flags );
1049
1055
}
1050
1056
else {
1051
- unsigned int l = strnlen_s_ (p , precision ? precision : (size_t ) -1 );
1057
+ printf_size_t l = strnlen_s_ (p , precision ? precision : (printf_size_t ) - 1 );
1052
1058
// pre padding
1053
1059
if (flags & FLAGS_PRECISION ) {
1054
1060
l = (l < precision ? l : precision );
@@ -1129,7 +1135,7 @@ int printf_(const char* format, ...)
1129
1135
va_list va ;
1130
1136
va_start (va , format );
1131
1137
char buffer [1 ];
1132
- const int ret = _vsnprintf (& out_putchar , buffer , (size_t )-1 , format , va );
1138
+ const int ret = _vsnprintf (& out_putchar , buffer , (printf_size_t )- 1 , format , va );
1133
1139
va_end (va );
1134
1140
return ret ;
1135
1141
}
@@ -1139,7 +1145,7 @@ int sprintf_(char* buffer, const char* format, ...)
1139
1145
{
1140
1146
va_list va ;
1141
1147
va_start (va , format );
1142
- const int ret = _vsnprintf (out_buffer , buffer , (size_t )-1 , format , va );
1148
+ const int ret = _vsnprintf (out_buffer , buffer , (printf_size_t )- 1 , format , va );
1143
1149
va_end (va );
1144
1150
return ret ;
1145
1151
}
@@ -1158,12 +1164,12 @@ int snprintf_(char* buffer, size_t count, const char* format, ...)
1158
1164
int vprintf_ (const char * format , va_list va )
1159
1165
{
1160
1166
char buffer [1 ];
1161
- return _vsnprintf (& out_putchar , buffer , (size_t )-1 , format , va );
1167
+ return _vsnprintf (& out_putchar , buffer , (printf_size_t )- 1 , format , va );
1162
1168
}
1163
1169
1164
1170
int vsprintf_ (char * buffer , const char * format , va_list va )
1165
1171
{
1166
- return _vsnprintf (out_buffer , buffer , (size_t )-1 , format , va );
1172
+ return _vsnprintf (out_buffer , buffer , (printf_size_t )- 1 , format , va );
1167
1173
}
1168
1174
1169
1175
int vsnprintf_ (char * buffer , size_t count , const char * format , va_list va )
@@ -1184,7 +1190,7 @@ int fctprintf(void (*out)(char character, void* arg), void* arg, const char* for
1184
1190
int vfctprintf (void (* out )(char character , void * arg ), void * arg , const char * format , va_list va )
1185
1191
{
1186
1192
const out_function_wrapper_type out_fct_wrap = { out , arg };
1187
- return _vsnprintf (out_wrapped_function , (char * )(uintptr_t )& out_fct_wrap , (size_t )-1 , format , va );
1193
+ return _vsnprintf (out_wrapped_function , (char * )(uintptr_t )& out_fct_wrap , (printf_size_t )- 1 , format , va );
1188
1194
}
1189
1195
1190
1196
#ifdef __cplusplus
0 commit comments