From cdba96d82c0ed3a20a8239f94e3e7f57560ff2a2 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Sun, 4 Nov 2018 12:57:49 +0100 Subject: [PATCH 1/2] perf(printf): check flags outside while loop --- printf.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/printf.c b/printf.c index b5a38fcc..8cbb2117 100644 --- a/printf.c +++ b/printf.c @@ -166,11 +166,13 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma const size_t start_idx = idx; // pad leading zeros - while (!(flags & FLAGS_LEFT) && (len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) { - buf[len++] = '0'; - } - while (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) { - buf[len++] = '0'; + if (!(flags & FLAGS_LEFT)) { + while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) { + buf[len++] = '0'; + } + while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) { + buf[len++] = '0'; + } } // handle hash @@ -381,8 +383,10 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d } // pad leading zeros - while (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) { - buf[len++] = '0'; + if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) { + while ((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) { + buf[len++] = '0'; + } } // handle sign From 29d136d88dd47c27c7fd901ce660b2b5ea99ee87 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Sun, 4 Nov 2018 12:59:42 +0100 Subject: [PATCH 2/2] chore(readme): update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0bd9009a..84a645ac 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Primarily designed for usage in embedded systems, where printf is not available 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. Absolutely **NO dependencies** are required, *printf.c* brings all necessary routines, even its own fast `ftoa` (float), `ntoa` (decimal) conversion. -If memory footprint is really a critical issue, floating point and 'long long' support and can be turned off via the `PRINTF_SUPPORT_FLOAT` and `PRINTF_SUPPORT_LONG_LONG` compiler switches. +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. When using printf (instead of sprintf/snprintf) you have to provide your own `_putchar()` low level function as console/serial output.