From aa47054dd8b6fd0b0303a997b8b4149aaa066b47 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 09:22:57 +0100 Subject: [PATCH 01/16] Add 'bool' type support --- printf.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/printf.cpp b/printf.cpp index d5f36038..9b461306 100644 --- a/printf.cpp +++ b/printf.cpp @@ -31,6 +31,7 @@ /////////////////////////////////////////////////////////////////////////////// #include +#include #include "printf.h" From 4c48045bff631bc0e3bfb0676f3f7dbadb1a3592 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 09:24:41 +0100 Subject: [PATCH 02/16] Convert _ntoa template in discrete functions This reduces the compiled size and makes this file 'c' compatible --- printf.cpp | 116 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 75 insertions(+), 41 deletions(-) diff --git a/printf.cpp b/printf.cpp index 9b461306..3e466b20 100644 --- a/printf.cpp +++ b/printf.cpp @@ -47,6 +47,8 @@ // define this to support floating point (%f) #define PRINTF_FLOAT_SUPPORT +// define this to support long long types (%llu or %p) +#define PRINTF_LONG_LONG_SUPPORT /////////////////////////////////////////////////////////////////////////////// @@ -64,8 +66,8 @@ // internal strlen, returns the length of the string -static inline size_t _strlen(const char* str) -{ +static inline size_t _strlen(const char* str) +{ size_t len = 0U; while (str[len] != '\0') { len++; @@ -92,33 +94,15 @@ static inline unsigned int _atoi(const char** str) } -// internal itoa -template -static size_t _ntoa(T value, char* buffer, unsigned int base, size_t maxlen, unsigned int prec, unsigned int width, unsigned int flags) +// internal itoa format +static size_t _ntoa_format(char* buffer, char* buf, size_t len, bool negative, unsigned int base, size_t maxlen, unsigned int prec, unsigned int width, unsigned int flags) { - char buf[NTOA_BUFFER_SIZE]; - size_t len = 0U; - unsigned int negative = 0U; - if (maxlen == 0U) { return 0U; } if (base > 16U) { return 0U; } - if (value < 0) { - negative = 1U; - value = 0 - value; - } - - // write if precision != 0 and value is != 0 - if (!(flags & FLAGS_PRECISION) || (value != 0)) { - do { - char digit = (char)(value % (T)base); - buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10; - value /= (T)base; - } while ((len < NTOA_BUFFER_SIZE) && (value > 0)); - } // pad leading zeros while (!(flags & FLAGS_LEFT) && (len < prec) && (len < NTOA_BUFFER_SIZE)) { @@ -172,7 +156,7 @@ static size_t _ntoa(T value, char* buffer, unsigned int base, size_t maxlen, uns // reverse string for (size_t i = 0U; (i < len) && (i < maxlen); ++i) { - buffer[i] = buf[len - i - 1]; + buffer[i] = buf[len - i - 1U]; } // append pad spaces up to given width @@ -186,24 +170,65 @@ static size_t _ntoa(T value, char* buffer, unsigned int base, size_t maxlen, uns } +// internal itoa for 'long' type +static size_t _ntoa_long(char* buffer, unsigned long value, bool negative, unsigned long base, size_t maxlen, unsigned int prec, unsigned int width, unsigned int flags) +{ + char buf[NTOA_BUFFER_SIZE]; + size_t len = 0U; + + // write if precision != 0 and value is != 0 + if (!(flags & FLAGS_PRECISION) || (value != 0)) { + do { + char digit = (char)(value % base); + buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10; + value /= base; + } while ((len < NTOA_BUFFER_SIZE) && (value > 0)); + } + + return _ntoa_format(buffer, buf, len, negative, (unsigned int)base, maxlen, prec, width, flags); +} + + +// internal itoa for 'long long' type +#if defined(PRINTF_LONG_LONG_SUPPORT) +static size_t _ntoa_long_long(char* buffer, unsigned long long value, bool negative, unsigned long long base, size_t maxlen, unsigned int prec, unsigned int width, unsigned int flags) +{ + char buf[NTOA_BUFFER_SIZE]; + size_t len = 0U; + + // write if precision != 0 and value is != 0 + if (!(flags & FLAGS_PRECISION) || (value != 0)) { + do { + char digit = (char)(value % base); + buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10; + value /= base; + } while ((len < NTOA_BUFFER_SIZE) && (value > 0)); + } + + return _ntoa_format(buffer, buf, len, negative, (unsigned int)base, maxlen, prec, width, flags); +} +#endif // PRINTF_LONG_LONG_SUPPORT + + #if defined(PRINTF_FLOAT_SUPPORT) static size_t _ftoa(double value, char* buffer, size_t maxlen, unsigned int prec, unsigned int width, unsigned int flags) { - // test for NaN - if (!(value == value) && (maxlen > 2U)) { - buffer[0] = 'n'; buffer[1] = 'a'; buffer[2] = 'n'; - return (size_t)3U; - } + char buf[FTOA_BUFFER_SIZE]; + size_t len = 0U; + double diff = 0.0; + // if input is larger than thres_max, revert to exponential const double thres_max = (double)0x7FFFFFFF; - char buf[FTOA_BUFFER_SIZE]; - size_t len = 0U; - double diff = 0; - // powers of 10 static const double pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; + // test for NaN + if (!(value == value) && (maxlen > 2U)) { + buffer[0] = 'n'; buffer[1] = 'a'; buffer[2] = 'n'; + return (size_t)3U; + } + // limit precision if (!(flags & FLAGS_PRECISION)) { prec = 6U; // by default, precesion is 6 @@ -279,7 +304,7 @@ static size_t _ftoa(double value, char* buffer, size_t maxlen, unsigned int prec break; } } - + // pad leading zeros while (!(flags & FLAGS_LEFT) && (len < prec) && (len < FTOA_BUFFER_SIZE)) { buf[len++] = '0'; @@ -443,25 +468,32 @@ static size_t vsnprintf(char* buffer, size_t buffer_len, const char* format, va_ if ((*format == 'i') || (*format == 'd')) { // signed if (flags & FLAGS_LONG_LONG) { - idx += _ntoa(va_arg(va, long long), &buffer[idx], base, buffer_len - idx, precision, width, flags); +#if defined(PRINTF_LONG_LONG_SUPPORT) + const long long value = va_arg(va, long long); + idx += _ntoa_long_long(&buffer[idx], (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base, buffer_len - idx, precision, width, flags); +#endif } else if (flags & FLAGS_LONG) { - idx += _ntoa(va_arg(va, long), &buffer[idx], base, buffer_len - idx, precision, width, flags); + const long value = va_arg(va, long); + idx += _ntoa_long(&buffer[idx], (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, buffer_len - idx, precision, width, flags); } else { - idx += _ntoa(va_arg(va, int), &buffer[idx], base, buffer_len - idx, precision, width, flags); + const int value = va_arg(va, int); + idx += _ntoa_long(&buffer[idx], (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, buffer_len - idx, precision, width, flags); } } else { // unsigned if (flags & FLAGS_LONG_LONG) { - idx += _ntoa(va_arg(va, unsigned long long), &buffer[idx], base, buffer_len - idx, precision, width, flags); +#if defined(PRINTF_LONG_LONG_SUPPORT) + idx += _ntoa_long_long(&buffer[idx], va_arg(va, unsigned long long), false, base, buffer_len - idx, precision, width, flags); +#endif } else if (flags & FLAGS_LONG) { - idx += _ntoa(va_arg(va, unsigned long), &buffer[idx], base, buffer_len - idx, precision, width, flags); + idx += _ntoa_long(&buffer[idx], va_arg(va, unsigned long), false, base, buffer_len - idx, precision, width, flags); } else { - idx += _ntoa(va_arg(va, unsigned int), &buffer[idx], base, buffer_len - idx, precision, width, flags); + idx += _ntoa_long(&buffer[idx], va_arg(va, unsigned int), false, base, buffer_len - idx, precision, width, flags); } } format++; @@ -525,10 +557,12 @@ static size_t vsnprintf(char* buffer, size_t buffer_len, const char* format, va_ flags |= FLAGS_ZEROPAD; size_t size_void = sizeof(void*); if (size_void > sizeof(long)) { - idx +=_ntoa(reinterpret_cast(va_arg(va, void*)), &buffer[idx], 16U, buffer_len - idx, precision, width, flags); +#if defined(PRINTF_LONG_LONG_SUPPORT) + idx += _ntoa_long_long(&buffer[idx], (unsigned long long)va_arg(va, void*), false, 16U, buffer_len - idx, precision, width, flags); +#endif } else { - idx += _ntoa(reinterpret_cast(va_arg(va, void*)), &buffer[idx], 16U, buffer_len - idx, precision, width, flags); + idx += _ntoa_long(&buffer[idx], (unsigned long)va_arg(va, void*), false, 16U, buffer_len - idx, precision, width, flags); } format++; break; From b5b539d814ce47c248e8f03c0e2919d32299cd8d Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 09:25:31 +0100 Subject: [PATCH 03/16] Added more test cases --- test/test_suite.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/test_suite.cpp b/test/test_suite.cpp index 97ce87f9..a291cc76 100644 --- a/test/test_suite.cpp +++ b/test/test_suite.cpp @@ -883,21 +883,48 @@ TEST_CASE("float", "[]" ) { TEST_CASE("types", "[]" ) { char buffer[100]; + test::sprintf(buffer, "%i", 0); + REQUIRE(!strcmp(buffer, "0")); + test::sprintf(buffer, "%i", 1234); REQUIRE(!strcmp(buffer, "1234")); + test::sprintf(buffer, "%i", 32767); + REQUIRE(!strcmp(buffer, "32767")); + + test::sprintf(buffer, "%i", -32767); + REQUIRE(!strcmp(buffer, "-32767")); + test::sprintf(buffer, "%li", 30L); REQUIRE(!strcmp(buffer, "30")); + test::sprintf(buffer, "%li", -2147483647L); + REQUIRE(!strcmp(buffer, "-2147483647")); + + test::sprintf(buffer, "%li", 2147483647L); + REQUIRE(!strcmp(buffer, "2147483647")); + test::sprintf(buffer, "%lli", 30LL); REQUIRE(!strcmp(buffer, "30")); + test::sprintf(buffer, "%lli", -9223372036854775807LL); + REQUIRE(!strcmp(buffer, "-9223372036854775807")); + + test::sprintf(buffer, "%lli", 9223372036854775807LL); + REQUIRE(!strcmp(buffer, "9223372036854775807")); + test::sprintf(buffer, "%lu", 100000L); REQUIRE(!strcmp(buffer, "100000")); + test::sprintf(buffer, "%lu", 0xFFFFFFFFL); + REQUIRE(!strcmp(buffer, "4294967295")); + test::sprintf(buffer, "%llu", 281474976710656LLU); REQUIRE(!strcmp(buffer, "281474976710656")); + test::sprintf(buffer, "%llu", 18446744073709551615LLU); + REQUIRE(!strcmp(buffer, "18446744073709551615")); + test::sprintf(buffer, "%b", 60000); REQUIRE(!strcmp(buffer, "1110101001100000")); From 65b9a297d3433fc8b2d6ca65c430b835b25923d0 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 09:27:19 +0100 Subject: [PATCH 04/16] Changed _putchar() to void, changed format to CRLF --- printf.h | 163 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 87 insertions(+), 76 deletions(-) diff --git a/printf.h b/printf.h index c448f921..ebb76dba 100644 --- a/printf.h +++ b/printf.h @@ -1,76 +1,87 @@ -/////////////////////////////////////////////////////////////////////////////// -// \author (c) Marco Paland (info@paland.com) -// 2014-2017, PALANDesign Hannover, Germany -// -// \license The MIT License (MIT) -// -// This file is part of the turnkey-board. -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// -// \brief Tiny printf, sprintf and snprintf implementation, optimized for speed on -// embedded systems with a very limited resources. -// Use this instead of bloated standard/newlib printf. -// These routines are thread safe and reentrant! -// -/////////////////////////////////////////////////////////////////////////////// - -#ifndef _PRINTF_H_ -#define _PRINTF_H_ - -#include - -/** - * Output a character to a custom device like UART. - * This function is declared here only. You have to write your custom implementation somewhere. - * \param character to output - * \return On success, the character written is returned - */ -int _putchar(char character); - - -/** - * Tiny printf implementation - * You have to implement _putchar if you use printf() - * \param format A string that specifies the format of the output - * \return The number of characters that are written into the array, not counting the terminating null character - */ -int printf(const char* format, ...); - - -/** - * Tiny sprintf implementation - * Due to security reasons YOU SHOULD CONSIDER USING SNPRINTF INSTEAD! - * \param buffer A pointer to the buffer where to store the formatted string - * \param format A string that specifies the format of the output - * \return The number of characters that are written into the array, not counting the terminating null character - */ -int sprintf(char* buffer, const char* format, ...); - - -/** - * Tiny snprintf implementation - * \param buffer A pointer to the buffer where to store the formatted string - * \param count The maximum number of characters to store in the buffer, including a terminating null character - * \param format A string that specifies the format of the output - * \return The number of characters that are written into the array, not counting the terminating null character - */ -int snprintf(char* buffer, size_t count, const char* format, ...); - - -#endif // _PRINTF_H_ +/////////////////////////////////////////////////////////////////////////////// +// \author (c) Marco Paland (info@paland.com) +// 2014-2017, PALANDesign Hannover, Germany +// +// \license The MIT License (MIT) +// +// This file is part of the turnkey-board. +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +// \brief Tiny printf, sprintf and snprintf implementation, optimized for speed on +// embedded systems with a very limited resources. +// Use this instead of bloated standard/newlib printf. +// These routines are thread safe and reentrant! +// +/////////////////////////////////////////////////////////////////////////////// + +#ifndef _PRINTF_H_ +#define _PRINTF_H_ + +#include + + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * Output a character to a custom device like UART. + * This function is declared here only. You have to write your custom implementation somewhere. + * \param character to output + * \return On success, the character written is returned + */ +void _putchar(char character); + + +/** + * Tiny printf implementation + * You have to implement _putchar if you use printf() + * \param format A string that specifies the format of the output + * \return The number of characters that are written into the array, not counting the terminating null character + */ +int printf(const char* format, ...); + + +/** + * Tiny sprintf implementation + * Due to security reasons YOU SHOULD CONSIDER USING SNPRINTF INSTEAD! + * \param buffer A pointer to the buffer where to store the formatted string + * \param format A string that specifies the format of the output + * \return The number of characters that are written into the array, not counting the terminating null character + */ +int sprintf(char* buffer, const char* format, ...); + + +/** + * Tiny snprintf implementation + * \param buffer A pointer to the buffer where to store the formatted string + * \param count The maximum number of characters to store in the buffer, including a terminating null character + * \param format A string that specifies the format of the output + * \return The number of characters that are written into the array, not counting the terminating null character + */ +int snprintf(char* buffer, size_t count, const char* format, ...); + + +#ifdef __cplusplus +} +#endif + + +#endif // _PRINTF_H_ From 7936fc34c4e2f734981832c1201777cb429d4a0a Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 09:29:14 +0100 Subject: [PATCH 05/16] Changed printf.cpp to printf.c to be usable with 'C'-compilers closes #1 --- README.md | 18 ++++++++++++++---- printf.cpp => printf.c | 0 test/test_suite.cpp | 8 +++----- 3 files changed, 17 insertions(+), 9 deletions(-) rename printf.cpp => printf.c (100%) diff --git a/README.md b/README.md index bd4c73ff..7af4a2fa 100644 --- a/README.md +++ b/README.md @@ -28,18 +28,28 @@ Therefore I decided to write an own implementation which meets the following ite - Support of dec/float number representation (with an own fast itoa/ftoa) - Reentrant and thread-safe, malloc free - LINT and compiler L4 warning free, coverity clean, automotive ready - - Extensive test suite (> 270 test cases) passing + - Extensive test suite (> 280 test cases) passing + - Simply the best printf around the net - MIT license ## Usage -Add/link `printf.cpp` to your project and include `printf.h`. That's it. -Usage is 1:1 like the according stdio.h library version: - +Add/link *printf.c* to your project and include *printf.h*. That's it. +Implement your low level output function needed for `printf()`: +```C +void _putchar(char character) +{ + // send char to console etc. +} +``` + +Usage is 1:1 like the according stdio.h library version: +```C `int printf(const char* format, ...);` `int sprintf(char* buffer, const char* format, ...);` `int snprintf(char* buffer, size_t count, const char* format, ...);` +``` **Due to genaral security reasons it is highly recommended to use `snprintf` (with the max buffer size as `count` parameter) only.** `sprintf` has no buffer limitation, so when necessary - use it with care! diff --git a/printf.cpp b/printf.c similarity index 100% rename from printf.cpp rename to printf.c diff --git a/test/test_suite.cpp b/test/test_suite.cpp index a291cc76..9e186aaa 100644 --- a/test/test_suite.cpp +++ b/test/test_suite.cpp @@ -35,15 +35,13 @@ namespace test { // use functions in own test namespace to avoid stdio conflicts #include "../printf.h" - #include "../printf.cpp" + #include "../printf.c" } // namespace test // dummy putchar -int test::_putchar(char) -{ - return 0; -} +void test::_putchar(char) +{ } From 5da5ac6cdd0ae16fa073963b40873620d4457d00 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 09:38:07 +0100 Subject: [PATCH 06/16] Update readme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7af4a2fa..5ce15a54 100644 --- a/README.md +++ b/README.md @@ -46,9 +46,9 @@ void _putchar(char character) Usage is 1:1 like the according stdio.h library version: ```C -`int printf(const char* format, ...);` -`int sprintf(char* buffer, const char* format, ...);` -`int snprintf(char* buffer, size_t count, const char* format, ...);` +int printf(const char* format, ...); +int sprintf(char* buffer, const char* format, ...); +int snprintf(char* buffer, size_t count, const char* format, ...); ``` **Due to genaral security reasons it is highly recommended to use `snprintf` (with the max buffer size as `count` parameter) only.** From ebe4998712a3cb389d21396c79f328c381f63d44 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 09:44:50 +0100 Subject: [PATCH 07/16] linguist should ignore 'test' path --- .gitattributes | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..6d447286 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# ignore test path +test/* linguist-vendored From a0af9125e8643bc0c765b281d0e6acc2e25f4fee Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 10:08:50 +0100 Subject: [PATCH 08/16] Updated readme --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5ce15a54..7f91d8d7 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,11 @@ This is a tiny but **fully loaded** printf, sprintf and snprintf implementation. Primarily designed for usage in embedded systems, where printf is not available due to memory issues or in avoidance of linking against libc. -Using the standard libc printf may pull **a lot** of unwanted library stuff and can bloat code size about 20k. In this case the following implementation can be used. -Absolutely **NO dependencies** are required, printf.cpp brings all necessary routines, even its own fast `ftoa` conversion. +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`, `ntoa` conversion. -If memory footprint is really a critical issue, floating point support can be turned off via the `PRINTF_FLOAT_SUPPORT` compiler switch. -When using printf (instead of sprintf) you have to provide your own `_putchar()` low level function as console output. +If memory footprint is really a critical issue, floating point and 'long long' support and can be turned off via the `PRINTF_FLOAT_SUPPORT` and `PRINTF_LONG_LONG_SUPPORT` compiler switches. +When using printf (instead of sprintf) you have to provide your own `_putchar()` low level function as console/serial output. ## Highligths and design goals @@ -29,7 +29,7 @@ Therefore I decided to write an own implementation which meets the following ite - Reentrant and thread-safe, malloc free - LINT and compiler L4 warning free, coverity clean, automotive ready - Extensive test suite (> 280 test cases) passing - - Simply the best printf around the net + - Simply the best *printf* around the net - MIT license @@ -125,6 +125,7 @@ The length sub-specifier modifies the length of the data type. | NTOA_BUFFER_SIZE | 32 | ntoa (integer) conversion buffer size. This must be big enough to hold one converted numeric number, normally 32 is a sufficient value. | | FTOA_BUFFER_SIZE | 32 | ftoa (float) conversion buffer size. This must be big enough to hold one converted float number, normally 32 is a sufficient value. | | PRINTF_FLOAT_SUPPORT | defined | Define this to enable floating point (%f) support | +| PRINTF_LONG_LONG_SUPPORT | defined | Define this to enable long long (%ll) support | ## Test suite From 6e0f97d33dc43f9663edcc999337e5c80a1fc64b Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 10:13:17 +0100 Subject: [PATCH 09/16] Updated license info --- printf.h | 1 - 1 file changed, 1 deletion(-) diff --git a/printf.h b/printf.h index ebb76dba..4ddaa953 100644 --- a/printf.h +++ b/printf.h @@ -4,7 +4,6 @@ // // \license The MIT License (MIT) // -// This file is part of the turnkey-board. // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights From 80dd57309a4ada96611adb41472d083105c61daa Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 11:45:13 +0100 Subject: [PATCH 10/16] Code cleanup --- printf.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/printf.c b/printf.c index 3e466b20..f6345196 100644 --- a/printf.c +++ b/printf.c @@ -177,12 +177,12 @@ static size_t _ntoa_long(char* buffer, unsigned long value, bool negative, unsig size_t len = 0U; // write if precision != 0 and value is != 0 - if (!(flags & FLAGS_PRECISION) || (value != 0)) { + if (!(flags & FLAGS_PRECISION) || value) { do { char digit = (char)(value % base); buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10; value /= base; - } while ((len < NTOA_BUFFER_SIZE) && (value > 0)); + } while ((len < NTOA_BUFFER_SIZE) && value); } return _ntoa_format(buffer, buf, len, negative, (unsigned int)base, maxlen, prec, width, flags); @@ -197,12 +197,12 @@ static size_t _ntoa_long_long(char* buffer, unsigned long long value, bool negat size_t len = 0U; // write if precision != 0 and value is != 0 - if (!(flags & FLAGS_PRECISION) || (value != 0)) { + if (!(flags & FLAGS_PRECISION) || value) { do { char digit = (char)(value % base); buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10; value /= base; - } while ((len < NTOA_BUFFER_SIZE) && (value > 0)); + } while ((len < NTOA_BUFFER_SIZE) && value); } return _ntoa_format(buffer, buf, len, negative, (unsigned int)base, maxlen, prec, width, flags); From 9b7cc4837c77caa242e5b06ff29117ae874c6785 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Tue, 21 Nov 2017 14:46:36 +0100 Subject: [PATCH 11/16] Fixed param comment --- printf.h | 1 - 1 file changed, 1 deletion(-) diff --git a/printf.h b/printf.h index 4ddaa953..37d93dda 100644 --- a/printf.h +++ b/printf.h @@ -44,7 +44,6 @@ extern "C" { * Output a character to a custom device like UART. * This function is declared here only. You have to write your custom implementation somewhere. * \param character to output - * \return On success, the character written is returned */ void _putchar(char character); From 7158a6f50f60008d7b475155f57120113c85cf69 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Tue, 21 Nov 2017 14:47:13 +0100 Subject: [PATCH 12/16] Added pointer testcase --- test/test_suite.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/test_suite.cpp b/test/test_suite.cpp index 9e186aaa..910ed91a 100644 --- a/test/test_suite.cpp +++ b/test/test_suite.cpp @@ -976,6 +976,14 @@ TEST_CASE("pointer", "[]" ) { else { REQUIRE(!strcmp(buffer, "0000000012345678")); } + + test::sprintf(buffer, "%p", (void*)0xFFFFFFFFLU); + if (sizeof(void*) == 4U) { + REQUIRE(!strcmp(buffer, "FFFFFFFF")); + } + else { + REQUIRE(!strcmp(buffer, "00000000FFFFFFFF")); + } } From c6c8d964201b26765efa48d9d54467a792759329 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Tue, 21 Nov 2017 14:49:58 +0100 Subject: [PATCH 13/16] Updated comments --- printf.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/printf.c b/printf.c index f6345196..fd29785a 100644 --- a/printf.c +++ b/printf.c @@ -35,13 +35,13 @@ #include "printf.h" -// buffer size used for printf +// buffer size used for printf (created on stack) #define PRINTF_BUFFER_SIZE 128U -// ntoa conversion buffer size, this must be big enough to hold one converted numeric number +// ntoa conversion buffer size, this must be big enough to hold one converted numeric number (created on stack) #define NTOA_BUFFER_SIZE 32U -// ftoa conversion buffer size, this must be big enough to hold one converted float number +// ftoa conversion buffer size, this must be big enough to hold one converted float number (created on stack) #define FTOA_BUFFER_SIZE 32U // define this to support floating point (%f) From 2b3f3c13067f0f3f061dcfd7b59b925082e72d0d Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Tue, 21 Nov 2017 14:52:23 +0100 Subject: [PATCH 14/16] Removed ftoa NaN check NaN check may not work with optimizing compilers. Use your implementation specific NaN check if necessary --- printf.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/printf.c b/printf.c index fd29785a..83c6d9ec 100644 --- a/printf.c +++ b/printf.c @@ -214,7 +214,7 @@ static size_t _ntoa_long_long(char* buffer, unsigned long long value, bool negat static size_t _ftoa(double value, char* buffer, size_t maxlen, unsigned int prec, unsigned int width, unsigned int flags) { char buf[FTOA_BUFFER_SIZE]; - size_t len = 0U; + size_t len = 0U; double diff = 0.0; // if input is larger than thres_max, revert to exponential @@ -223,10 +223,11 @@ static size_t _ftoa(double value, char* buffer, size_t maxlen, unsigned int prec // powers of 10 static const double pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; - // test for NaN - if (!(value == value) && (maxlen > 2U)) { - buffer[0] = 'n'; buffer[1] = 'a'; buffer[2] = 'n'; - return (size_t)3U; + // test for negative + bool negative = false; + if (value < 0) { + negative = true; + value = 0 - value; } // limit precision @@ -238,12 +239,6 @@ static size_t _ftoa(double value, char* buffer, size_t maxlen, unsigned int prec prec = 9U; } - unsigned int negative = 0U; - if (value < 0) { - negative = 1U; - value = 0 - value; - } - int whole = (int)value; double tmp = (value - whole) * pow10[prec]; unsigned long frac = (unsigned long)tmp; From 99021707632120a804a6126a149620c651088eb8 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Tue, 21 Nov 2017 14:54:14 +0100 Subject: [PATCH 15/16] Return %p values in upper case, fixed %p 64 bit support --- printf.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/printf.c b/printf.c index 83c6d9ec..3ec36f4f 100644 --- a/printf.c +++ b/printf.c @@ -549,9 +549,8 @@ static size_t vsnprintf(char* buffer, size_t buffer_len, const char* format, va_ case 'p' : { width = sizeof(void*) * 2U; - flags |= FLAGS_ZEROPAD; - size_t size_void = sizeof(void*); - if (size_void > sizeof(long)) { + flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE; + if (sizeof(void*) == sizeof(long long)) { #if defined(PRINTF_LONG_LONG_SUPPORT) idx += _ntoa_long_long(&buffer[idx], (unsigned long long)va_arg(va, void*), false, 16U, buffer_len - idx, precision, width, flags); #endif From cb7d11a542b085ab4a2f771405c76e62da0b18c9 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Tue, 21 Nov 2017 14:54:48 +0100 Subject: [PATCH 16/16] Code cleanup --- printf.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/printf.c b/printf.c index 3ec36f4f..49ef68f8 100644 --- a/printf.c +++ b/printf.c @@ -65,11 +65,12 @@ #define FLAGS_WIDTH (1U << 9U) -// internal strlen, returns the length of the string +// internal strlen +// \return The length of the string (excluding the terminating 0) static inline size_t _strlen(const char* str) { size_t len = 0U; - while (str[len] != '\0') { + while (str[len] != (char)0) { len++; } return len; @@ -260,10 +261,10 @@ static size_t _ftoa(double value, char* buffer, size_t maxlen, unsigned int prec // for very large numbers switch back to native sprintf for exponentials. anyone want to write code to replace this? // normal printf behavior is to print EVERY whole number digit which can be 100s of characters overflowing your buffers == bad if (value > thres_max) { - return 0; + return 0U; } - if (prec == 0) { + if (prec == 0U) { diff = value - whole; if (diff > 0.5) { // greater than 0.5, round up, e.g. 1.6 -> 2 @@ -346,15 +347,15 @@ static size_t _ftoa(double value, char* buffer, size_t maxlen, unsigned int prec // internal vsnprintf -static size_t vsnprintf(char* buffer, size_t buffer_len, const char* format, va_list va) +static size_t _vsnprintf(char* buffer, size_t buffer_len, const char* format, va_list va) { unsigned int flags, width, precision, n; size_t idx = 0U; while (idx < buffer_len) { // end reached? - if (*format == '\0') { - buffer[idx] = '\0'; + if (*format == (char)0) { + buffer[idx] = (char)0; break; } @@ -426,13 +427,13 @@ static size_t vsnprintf(char* buffer, size_t buffer_len, const char* format, va_ // evaluate specifier switch (*format) { + case 'd' : + case 'i' : case 'u' : case 'x' : case 'X' : case 'o' : - case 'b' : - case 'd' : - case 'i' : { + case 'b' : { // set the base unsigned int base; if (*format == 'x' || *format == 'X') { @@ -584,7 +585,7 @@ int printf(const char* format, ...) va_list va; va_start(va, format); char buffer[PRINTF_BUFFER_SIZE]; - size_t ret = vsnprintf(buffer, PRINTF_BUFFER_SIZE, format, va); + size_t ret = _vsnprintf(buffer, PRINTF_BUFFER_SIZE, format, va); va_end(va); for (size_t i = 0U; i < ret; ++i) { _putchar(buffer[i]); @@ -597,7 +598,7 @@ int sprintf(char* buffer, const char* format, ...) { va_list va; va_start(va, format); - size_t ret = vsnprintf(buffer, (size_t)-1, format, va); + size_t ret = _vsnprintf(buffer, (size_t)-1, format, va); va_end(va); return (int)ret; } @@ -607,7 +608,7 @@ int snprintf(char* buffer, size_t count, const char* format, ...) { va_list va; va_start(va, format); - size_t ret = vsnprintf(buffer, count, format, va); + size_t ret = _vsnprintf(buffer, count, format, va); va_end(va); return (int)ret; }