Skip to content

Commit 4a1e370

Browse files
committed
Regards mpaland#77: Expanded use of typedefs instead of plain unsigned int all over.
1 parent 87878b2 commit 4a1e370

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

src/printf/printf.c

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ extern "C" {
137137
#define FLAGS_ADAPT_EXP (1U << 11U)
138138
#define FLAGS_POINTER (1U << 12U)
139139
// Note: Similar, but not identical, effect as FLAGS_HASH
140+
typedef unsigned int printf_flags_t;
140141

141142
#define BASE_BINARY 2
142143
#define BASE_OCTAL 8
@@ -153,6 +154,9 @@ typedef long printf_signed_value_t;
153154

154155
typedef uint8_t numeric_base_t;
155156

157+
typedef unsigned int printf_precision_t;
158+
typedef unsigned int printf_width_t;
159+
156160
#if (PRINTF_SUPPORT_DECIMAL_SPECIFIERS || PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS)
157161
#include <float.h>
158162
#if FLT_RADIX != 2
@@ -266,11 +270,11 @@ static inline void out_wrapped_function(char character, void* wrapped_function,
266270

267271
// internal secure strlen
268272
// @return The length of the string (excluding the terminating 0) limited by 'maxsize'
269-
static inline unsigned int strnlen_s_(const char* str, size_t maxsize)
273+
static inline size_t strnlen_s_(const char* str, size_t maxsize)
270274
{
271275
const char* s;
272276
for (s = str; *s && maxsize--; ++s);
273-
return (unsigned int)(s - str);
277+
return (size_t)(s - str);
274278
}
275279

276280

@@ -294,7 +298,7 @@ static unsigned int atoi_(const char** str)
294298

295299

296300
// output the specified string in reverse, taking care of any zero-padding
297-
static size_t out_rev_(out_fct_type out, char* buffer, size_t idx, size_t maxlen, const char* buf, size_t len, unsigned int width, unsigned int flags)
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)
298302
{
299303
const size_t start_idx = idx;
300304

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

324328
// Invoked by print_integer after the actual number has been printed, performing necessary
325329
// work on the number's prefix (as the number is initially printed in reverse order)
326-
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, unsigned int precision, unsigned int width, unsigned int flags)
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)
327331
{
328332
size_t unpadded_len = len;
329333

@@ -390,7 +394,7 @@ static size_t print_integer_finalization(out_fct_type out, char* buffer, size_t
390394
}
391395

392396
// An internal itoa-like function
393-
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, unsigned int precision, unsigned int width, unsigned int flags)
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)
394398
{
395399
char buf[PRINTF_INTEGER_BUFFER_SIZE];
396400
size_t len = 0U;
@@ -441,7 +445,7 @@ static const double powers_of_10[NUM_DECIMAL_DIGITS_IN_INT64_T] = {
441445
// Break up a double number - which is known to be a finite non-negative number -
442446
// into its base-10 parts: integral - before the decimal point, and fractional - after it.
443447
// Taken the precision into account, but does not change it even internally.
444-
static struct double_components get_components(double number, unsigned int precision)
448+
static struct double_components get_components(double number, printf_precision_t precision)
445449
{
446450
struct double_components number_;
447451
number_.is_negative = get_sign(number);
@@ -516,7 +520,7 @@ static struct scaling_factor update_normalization(struct scaling_factor sf, doub
516520
return result;
517521
}
518522

519-
static struct double_components get_normalized_components(bool negative, unsigned int precision, double non_normalized, struct scaling_factor normalization)
523+
static struct double_components get_normalized_components(bool negative, printf_precision_t precision, double non_normalized, struct scaling_factor normalization)
520524
{
521525
struct double_components components;
522526
components.is_negative = negative;
@@ -556,8 +560,8 @@ static struct double_components get_normalized_components(bool negative, unsigne
556560
#endif
557561

558562
static size_t print_broken_up_decimal(
559-
struct double_components number_, out_fct_type out, char *buffer, size_t idx, size_t maxlen, unsigned int precision,
560-
unsigned int width, unsigned int flags, char *buf, size_t len)
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)
561565
{
562566
if (precision != 0U) {
563567
// do fractional part, as an unsigned number
@@ -638,15 +642,15 @@ static size_t print_broken_up_decimal(
638642
}
639643

640644
// internal ftoa for fixed decimal floating point
641-
static size_t print_decimal_number(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double number, unsigned int precision, unsigned int width, unsigned int flags, char* buf, size_t len)
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)
642646
{
643647
struct double_components value_ = get_components(number, precision);
644648
return print_broken_up_decimal(value_, out, buffer, idx, maxlen, precision, width, flags, buf, len);
645649
}
646650

647651
#if PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS
648652
// internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse <[email protected]>
649-
static size_t print_exponential_number(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double number, unsigned int precision, unsigned int width, unsigned int flags, char* buf, size_t len)
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)
650654
{
651655
const bool negative = get_sign(number);
652656
// This number will decrease gradually (by factors of 10) as we "extract" the exponent out of it
@@ -737,9 +741,9 @@ static size_t print_exponential_number(out_fct_type out, char* buffer, size_t id
737741

738742
// the exp10 format is "E%+03d" and largest possible exp10 value for a 64-bit double
739743
// is "307" (for 2^1023), so we set aside 4-5 characters overall
740-
unsigned int exp10_part_width = fall_back_to_decimal_only_mode ? 0U : (PRINTF_ABS(exp10) < 100) ? 4U : 5U;
744+
printf_width_t exp10_part_width = fall_back_to_decimal_only_mode ? 0U : (PRINTF_ABS(exp10) < 100) ? 4U : 5U;
741745

742-
unsigned int decimal_part_width =
746+
printf_width_t decimal_part_width =
743747
((flags & FLAGS_LEFT) && exp10_part_width) ?
744748
// We're padding on the right, so the width constraint is the exponent part's
745749
// problem, not the decimal part's, so we'll use as many characters as we need:
@@ -772,8 +776,7 @@ static size_t print_exponential_number(out_fct_type out, char* buffer, size_t id
772776
}
773777
#endif // PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS
774778

775-
776-
static size_t print_floating_point(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int precision, unsigned int width, unsigned int flags, bool prefer_exponential)
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)
777780
{
778781
char buf[PRINTF_FTOA_BUFFER_SIZE];
779782
size_t len = 0U;
@@ -821,7 +824,10 @@ static size_t print_floating_point(out_fct_type out, char* buffer, size_t idx, s
821824
// internal vsnprintf
822825
static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, va_list va)
823826
{
824-
unsigned int flags, width, precision, n;
827+
printf_flags_t flags;
828+
printf_width_t width;
829+
printf_precision_t precision;
830+
unsigned int n;
825831
size_t idx = 0U;
826832

827833
if (!buffer) {
@@ -865,10 +871,10 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
865871
const int w = va_arg(va, int);
866872
if (w < 0) {
867873
flags |= FLAGS_LEFT; // reverse padding
868-
width = (unsigned int)-w;
874+
width = (printf_width_t)-w;
869875
}
870876
else {
871-
width = (unsigned int)w;
877+
width = (printf_width_t)w;
872878
}
873879
format++;
874880
}
@@ -883,7 +889,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
883889
}
884890
else if (*format == '*') {
885891
const int precision_ = va_arg(va, int);
886-
precision = precision_ > 0 ? (unsigned int)precision_ : 0U;
892+
precision = precision_ > 0 ? (printf_precision_t)precision_ : 0U;
887893
format++;
888894
}
889895
}
@@ -1183,4 +1189,4 @@ int vfctprintf(void (*out)(char character, void* arg), void* arg, const char* fo
11831189

11841190
#ifdef __cplusplus
11851191
} // extern "C"
1186-
#endif
1192+
#endif

0 commit comments

Comments
 (0)