Skip to content

Commit b65be83

Browse files
committed
chore(printf): minor cleanup
1 parent 8684603 commit b65be83

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Primarily designed for usage in embedded systems, where printf is not available
1212
Using the standard libc printf may pull **a lot** of unwanted library stuff and can bloat code size about 20k or is not 100% thread safe. In this cases the following implementation can be used.
1313
Absolutely **NO dependencies** are required, *printf.c* brings all necessary routines, even its own fast `ftoa` (floating point), `ntoa` (decimal) conversion.
1414

15-
If memory footprint is really a critical issue, floating point and 'long long' support and can be turned off via the `PRINTF_DISABLE_SUPPORT_FLOAT` and `PRINTF_DISABLE_SUPPORT_LONG_LONG` compiler switches.
15+
If memory footprint is really a critical issue, floating point, exponential and 'long long' support and can be turned off via the `PRINTF_DISABLE_SUPPORT_FLOAT`, `PRINTF_DISABLE_SUPPORT_EXPONENTIAL` and `PRINTF_DISABLE_SUPPORT_LONG_LONG` compiler switches.
1616
When using printf (instead of sprintf/snprintf) you have to provide your own `_putchar()` low level function as console/serial output.
1717

1818

printf.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@
112112
#define FLAGS_ADAPT_EXP (1U << 11U)
113113

114114

115+
// import float.h for DBL_MAX
116+
#if defined(PRINTF_SUPPORT_FLOAT)
117+
#include <float.h>
118+
#endif
119+
120+
115121
// output function type
116122
typedef void (*out_fct_type)(char character, void* buffer, size_t idx, size_t maxlen);
117123

@@ -322,7 +328,7 @@ static size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx, size_t
322328

323329

324330
#if defined(PRINTF_SUPPORT_FLOAT)
325-
#include <float.h>
331+
326332
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
327333
// forward declaration so that _ftoa can switch to exp notation for values > PRINTF_MAX_FLOAT
328334
static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags);
@@ -345,7 +351,7 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
345351
if (value < -DBL_MAX)
346352
return _out_rev(out, buffer, idx, maxlen, "fni-", 4, width, flags);
347353
if (value > DBL_MAX)
348-
return _out_rev(out, buffer, idx, maxlen, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4 : 3, width, flags);
354+
return _out_rev(out, buffer, idx, maxlen, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4U : 3U, width, flags);
349355

350356
// test for very large values
351357
// standard printf behavior is to print EVERY whole number digit -- which could be 100s of characters overflowing your buffers == bad
@@ -497,12 +503,12 @@ static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
497503
conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14)))));
498504
// correct for rounding errors
499505
if (value < conv.F) {
500-
expval--;
501-
conv.F /= 10;
506+
expval--;
507+
conv.F /= 10;
502508
}
503509

504510
// the exponent format is "%+03d" and largest value is "307", so set aside 4-5 characters
505-
unsigned int minwidth = ((expval < 100)&&(expval > -100)) ? 4 : 5;
511+
unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U;
506512

507513
// in "%g" mode, "prec" is the number of *significant figures* not decimals
508514
if (flags & FLAGS_ADAPT_EXP) {
@@ -516,8 +522,8 @@ static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
516522
}
517523
flags |= FLAGS_PRECISION; // make sure _ftoa respects precision
518524
// no characters in exponent
519-
minwidth = 0;
520-
expval = 0;
525+
minwidth = 0U;
526+
expval = 0;
521527
}
522528
else {
523529
// we use one sigfig for the whole part
@@ -534,11 +540,11 @@ static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
534540
fwidth -= minwidth;
535541
} else {
536542
// not enough characters, so go back to default sizing
537-
fwidth = 0;
543+
fwidth = 0U;
538544
}
539545
if ((flags & FLAGS_LEFT) && minwidth) {
540546
// if we're padding on the right, DON'T pad the floating part
541-
fwidth = 0;
547+
fwidth = 0U;
542548
}
543549

544550
// rescale the float value

0 commit comments

Comments
 (0)