Skip to content

Commit 6448613

Browse files
committed
Minor cosmetic changes:
* More use of typedef's over raw primitive C types. Specifically, using `printf_size_t` in many relevant places * Regards mpaland#77 * Removed redundant parentheses * Comment tweaks
1 parent 4a1e370 commit 6448613

File tree

1 file changed

+48
-42
lines changed

1 file changed

+48
-42
lines changed

src/printf/printf.c

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ typedef unsigned int printf_flags_t;
144144
#define BASE_DECIMAL 10
145145
#define BASE_HEX 16
146146

147+
typedef uint8_t numeric_base_t;
148+
147149
#if PRINTF_SUPPORT_LONG_LONG
148150
typedef unsigned long long printf_unsigned_value_t;
149151
typedef long long printf_signed_value_t;
@@ -152,10 +154,12 @@ typedef unsigned long printf_unsigned_value_t;
152154
typedef long printf_signed_value_t;
153155
#endif
154156

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;
159163

160164
#if (PRINTF_SUPPORT_DECIMAL_SPECIFIERS || PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS)
161165
#include <float.h>
@@ -188,7 +192,9 @@ typedef union {
188192
} double_with_bit_access;
189193

190194
// 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;
192198
// 3. If you try to use it as C++, only C++20 supports compound literals
193199
static inline double_with_bit_access get_bit_access(double x)
194200
{
@@ -270,11 +276,13 @@ static inline void out_wrapped_function(char character, void* wrapped_function,
270276

271277
// internal secure strlen
272278
// @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)
274282
{
275283
const char* s;
276284
for (s = str; *s && maxsize--; ++s);
277-
return (size_t)(s - str);
285+
return (printf_size_t)(s - str);
278286
}
279287

280288

@@ -286,19 +294,19 @@ static inline bool is_digit_(char ch)
286294
}
287295

288296

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)
291299
{
292-
unsigned int i = 0U;
300+
printf_size_t i = 0U;
293301
while (is_digit_(**str)) {
294-
i = i * 10U + (unsigned int)(*((*str)++) - '0');
302+
i = i * 10U + (printf_size_t)(*((*str)++) - '0');
295303
}
296304
return i;
297305
}
298306

299307

300308
// 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)
302310
{
303311
const size_t start_idx = idx;
304312

@@ -327,7 +335,7 @@ static size_t out_rev_(out_fct_type out, char* buffer, size_t idx, size_t maxlen
327335

328336
// Invoked by print_integer after the actual number has been printed, performing necessary
329337
// 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)
331339
{
332340
size_t unpadded_len = len;
333341

@@ -394,7 +402,7 @@ static size_t print_integer_finalization(out_fct_type out, char* buffer, size_t
394402
}
395403

396404
// 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)
398406
{
399407
char buf[PRINTF_INTEGER_BUFFER_SIZE];
400408
size_t len = 0U;
@@ -445,7 +453,7 @@ static const double powers_of_10[NUM_DECIMAL_DIGITS_IN_INT64_T] = {
445453
// Break up a double number - which is known to be a finite non-negative number -
446454
// into its base-10 parts: integral - before the decimal point, and fractional - after it.
447455
// 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)
449457
{
450458
struct double_components number_;
451459
number_.is_negative = get_sign(number);
@@ -520,7 +528,7 @@ static struct scaling_factor update_normalization(struct scaling_factor sf, doub
520528
return result;
521529
}
522530

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)
524532
{
525533
struct double_components components;
526534
components.is_negative = negative;
@@ -560,13 +568,13 @@ static struct double_components get_normalized_components(bool negative, printf_
560568
#endif
561569

562570
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)
565573
{
566574
if (precision != 0U) {
567575
// do fractional part, as an unsigned number
568576

569-
unsigned int count = precision;
577+
printf_size_t count = precision;
570578

571579
// %g/%G mandates we skip the trailing 0 digits...
572580
if ((flags & FLAGS_ADAPT_EXP) && !(flags & FLAGS_HASH) && (number_.fractional > 0)) {
@@ -642,15 +650,15 @@ static size_t print_broken_up_decimal(
642650
}
643651

644652
// 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)
646654
{
647655
struct double_components value_ = get_components(number, precision);
648656
return print_broken_up_decimal(value_, out, buffer, idx, maxlen, precision, width, flags, buf, len);
649657
}
650658

651659
#if PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS
652660
// 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)
654662
{
655663
const bool negative = get_sign(number);
656664
// 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
707715
// This also decided how we adjust the precision value - as in "%g" mode,
708716
// "precision" is the number of _significant digits_, and this is when we "translate"
709717
// 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 ?
711719
(int) precision - 1 - exp10 :
712720
(int) precision - 1; // the presence of the exponent ensures only one significant digit comes before the decimal point
713721
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
741749

742750
// the exp10 format is "E%+03d" and largest possible exp10 value for a 64-bit double
743751
// 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;
745753

746-
printf_width_t decimal_part_width =
754+
printf_size_t decimal_part_width =
747755
((flags & FLAGS_LEFT) && exp10_part_width) ?
748756
// We're padding on the right, so the width constraint is the exponent part's
749757
// 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
776784
}
777785
#endif // PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS
778786

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)
780788
{
781789
char buf[PRINTF_FTOA_BUFFER_SIZE];
782790
size_t len = 0U;
@@ -822,13 +830,11 @@ static size_t print_floating_point(out_fct_type out, char* buffer, size_t idx, s
822830
#endif // (PRINTF_SUPPORT_DECIMAL_SPECIFIERS || PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS)
823831

824832
// 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)
826834
{
827835
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;
832838

833839
if (!buffer) {
834840
// use null output function
@@ -865,16 +871,16 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
865871
// evaluate width field
866872
width = 0U;
867873
if (is_digit_(*format)) {
868-
width = atoi_(&format);
874+
width = (printf_size_t) atou_(&format);
869875
}
870876
else if (*format == '*') {
871877
const int w = va_arg(va, int);
872878
if (w < 0) {
873879
flags |= FLAGS_LEFT; // reverse padding
874-
width = (printf_width_t)-w;
880+
width = (printf_size_t)-w;
875881
}
876882
else {
877-
width = (printf_width_t)w;
883+
width = (printf_size_t)w;
878884
}
879885
format++;
880886
}
@@ -885,11 +891,11 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
885891
flags |= FLAGS_PRECISION;
886892
format++;
887893
if (is_digit_(*format)) {
888-
precision = atoi_(&format);
894+
precision = (printf_size_t) atou_(&format);
889895
}
890896
else if (*format == '*') {
891897
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;
893899
format++;
894900
}
895901
}
@@ -1023,7 +1029,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
10231029
break;
10241030
#endif // PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS
10251031
case 'c' : {
1026-
unsigned int l = 1U;
1032+
printf_size_t l = 1U;
10271033
// pre padding
10281034
if (!(flags & FLAGS_LEFT)) {
10291035
while (l++ < width) {
@@ -1048,7 +1054,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
10481054
idx = out_rev_(out, buffer, idx, maxlen, ")llun(", 6, width, flags);
10491055
}
10501056
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);
10521058
// pre padding
10531059
if (flags & FLAGS_PRECISION) {
10541060
l = (l < precision ? l : precision);
@@ -1129,7 +1135,7 @@ int printf_(const char* format, ...)
11291135
va_list va;
11301136
va_start(va, format);
11311137
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);
11331139
va_end(va);
11341140
return ret;
11351141
}
@@ -1139,7 +1145,7 @@ int sprintf_(char* buffer, const char* format, ...)
11391145
{
11401146
va_list va;
11411147
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);
11431149
va_end(va);
11441150
return ret;
11451151
}
@@ -1158,12 +1164,12 @@ int snprintf_(char* buffer, size_t count, const char* format, ...)
11581164
int vprintf_(const char* format, va_list va)
11591165
{
11601166
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);
11621168
}
11631169

11641170
int vsprintf_(char* buffer, const char* format, va_list va)
11651171
{
1166-
return _vsnprintf(out_buffer, buffer, (size_t)-1, format, va);
1172+
return _vsnprintf(out_buffer, buffer, (printf_size_t)-1, format, va);
11671173
}
11681174

11691175
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
11841190
int vfctprintf(void (*out)(char character, void* arg), void* arg, const char* format, va_list va)
11851191
{
11861192
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);
11881194
}
11891195

11901196
#ifdef __cplusplus

0 commit comments

Comments
 (0)