From aa47054dd8b6fd0b0303a997b8b4149aaa066b47 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 09:22:57 +0100 Subject: Add 'bool' type support --- printf.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/printf.cpp b/printf.cpp index d5f3603..9b46130 100644 --- a/printf.cpp +++ b/printf.cpp @@ -31,6 +31,7 @@ /////////////////////////////////////////////////////////////////////////////// #include +#include #include "printf.h" -- cgit v1.2.3 From 4c48045bff631bc0e3bfb0676f3f7dbadb1a3592 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 09:24:41 +0100 Subject: 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 9b46130..3e466b2 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; -- cgit v1.2.3 From b5b539d814ce47c248e8f03c0e2919d32299cd8d Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 09:25:31 +0100 Subject: 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 97ce87f..a291cc7 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")); -- cgit v1.2.3 From 65b9a297d3433fc8b2d6ca65c430b835b25923d0 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 09:27:19 +0100 Subject: 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 c448f92..ebb76db 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_ -- cgit v1.2.3 From 7936fc34c4e2f734981832c1201777cb429d4a0a Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 09:29:14 +0100 Subject: Changed printf.cpp to printf.c to be usable with 'C'-compilers closes #1 --- README.md | 18 +- printf.c | 619 ++++++++++++++++++++++++++++++++++++++++++++++++++++ printf.cpp | 619 ---------------------------------------------------- test/test_suite.cpp | 8 +- 4 files changed, 636 insertions(+), 628 deletions(-) create mode 100644 printf.c delete mode 100644 printf.cpp diff --git a/README.md b/README.md index bd4c73f..7af4a2f 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.c b/printf.c new file mode 100644 index 0000000..3e466b2 --- /dev/null +++ b/printf.c @@ -0,0 +1,619 @@ +/////////////////////////////////////////////////////////////////////////////// +// \author (c) Marco Paland (info@paland.com) +// 2014-2017, PALANDesign Hannover, Germany +// +// \license The MIT License (MIT) +// +// 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. These routines are thread +// safe and reentrant! +// Use this instead of the bloated standard/newlib printf cause these use +// malloc for printf (and may not be thread safe). +// +/////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include "printf.h" + + +// buffer size used for printf +#define PRINTF_BUFFER_SIZE 128U + +// ntoa conversion buffer size, this must be big enough to hold one converted numeric number +#define NTOA_BUFFER_SIZE 32U + +// ftoa conversion buffer size, this must be big enough to hold one converted float number +#define FTOA_BUFFER_SIZE 32U + +// 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 + +/////////////////////////////////////////////////////////////////////////////// + +// internal flag definitions +#define FLAGS_ZEROPAD (1U << 0U) +#define FLAGS_LEFT (1U << 1U) +#define FLAGS_PLUS (1U << 2U) +#define FLAGS_SPACE (1U << 3U) +#define FLAGS_HASH (1U << 4U) +#define FLAGS_UPPERCASE (1U << 5U) +#define FLAGS_LONG (1U << 6U) +#define FLAGS_LONG_LONG (1U << 7U) +#define FLAGS_PRECISION (1U << 8U) +#define FLAGS_WIDTH (1U << 9U) + + +// internal strlen, returns the length of the string +static inline size_t _strlen(const char* str) +{ + size_t len = 0U; + while (str[len] != '\0') { + len++; + } + return len; +} + + +// returns true if char is a digit +static inline bool _is_digit(char ch) +{ + return (ch >= '0') && (ch <= '9'); +} + + +// internal ASCII to unsigned int conversion +static inline unsigned int _atoi(const char** str) +{ + unsigned int i = 0U; + while (_is_digit(**str)) { + i = i * 10U + (unsigned int)(*((*str)++) - '0'); + } + return i; +} + + +// 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) +{ + if (maxlen == 0U) { + return 0U; + } + if (base > 16U) { + return 0U; + } + + // pad leading zeros + while (!(flags & FLAGS_LEFT) && (len < prec) && (len < NTOA_BUFFER_SIZE)) { + buf[len++] = '0'; + } + while (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD) && (len < width) && (len < NTOA_BUFFER_SIZE)) { + buf[len++] = '0'; + } + + // handle hash + if (flags & FLAGS_HASH) { + if (((len == prec) || (len == width)) && (len > 0U)) { + len--; + if ((base == 16U) && (len > 0U)) { + len--; + } + } + if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < NTOA_BUFFER_SIZE)) { + buf[len++] = 'x'; + } + if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < NTOA_BUFFER_SIZE)) { + buf[len++] = 'X'; + } + if (len < NTOA_BUFFER_SIZE) { + buf[len++] = '0'; + } + } + + // handle sign + if ((len == width) && (negative || (flags & FLAGS_PLUS) || (flags & FLAGS_SPACE))) { + len--; + } + if (len < NTOA_BUFFER_SIZE) { + if (negative) { + buf[len++] = '-'; + } + else if (flags & FLAGS_PLUS) { + buf[len++] = '+'; // ignore the space if the '+' exists + } + else if (flags & FLAGS_SPACE) { + buf[len++] = ' '; + } + } + + // pad spaces up to given width + if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) { + while ((len < width) && (len < NTOA_BUFFER_SIZE)) { + buf[len++] = ' '; + } + } + + // reverse string + for (size_t i = 0U; (i < len) && (i < maxlen); ++i) { + buffer[i] = buf[len - i - 1U]; + } + + // append pad spaces up to given width + if (flags & FLAGS_LEFT) { + while ((len < width) && (len < maxlen)) { + buffer[len++] = ' '; + } + } + + return len; +} + + +// 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) +{ + 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; + + // 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 + } + if (prec > 9U) { + // precision of >= 10 can lead to overflow errors + 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; + diff = tmp - frac; + + if (diff > 0.5) { + ++frac; + // handle rollover, e.g. case 0.99 with prec 1 is 1.0 + if (frac >= pow10[prec]) { + frac = 0; + ++whole; + } + } + else if ((diff == 0.5) && ((frac == 0) || (frac & 1))) { + // if halfway, round up if odd, OR if last digit is 0 + ++frac; + } + + // 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; + } + + if (prec == 0) { + diff = value - whole; + if (diff > 0.5) { + // greater than 0.5, round up, e.g. 1.6 -> 2 + ++whole; + } + else if (diff == 0.5 && (whole & 1)) { + // exactly 0.5 and ODD, then round up + // 1.5 -> 2, but 2.5 -> 2 + ++whole; + } + } + else { + unsigned int count = prec; + // now do fractional part, as an unsigned number + do { + --count; + buf[len++] = (char)(48U + (frac % 10U)); + } while ((len < FTOA_BUFFER_SIZE) && (frac /= 10U)); + // add extra 0s + while ((len < FTOA_BUFFER_SIZE) && (count-- > 0U)) { + buf[len++] = '0'; + } + if (len < FTOA_BUFFER_SIZE) { + // add decimal + buf[len++] = '.'; + } + } + + // do whole part, number is reversed + while (len < FTOA_BUFFER_SIZE) { + buf[len++] = (char)(48 + (whole % 10)); + if (!(whole /= 10)) { + break; + } + } + + // pad leading zeros + while (!(flags & FLAGS_LEFT) && (len < prec) && (len < FTOA_BUFFER_SIZE)) { + buf[len++] = '0'; + } + while (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD) && (len < width) && (len < FTOA_BUFFER_SIZE)) { + buf[len++] = '0'; + } + + // handle sign + if (len < FTOA_BUFFER_SIZE) { + if (negative) { + buf[len++] = '-'; + } + else if (flags & FLAGS_PLUS) { + buf[len++] = '+'; // ignore the space if the '+' exists + } + else if (flags & FLAGS_SPACE) { + buf[len++] = ' '; + } + } + + // pad spaces up to given width + if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) { + while ((len < width) && (len < FTOA_BUFFER_SIZE)) { + buf[len++] = ' '; + } + } + + // reverse string + for (size_t i = 0U; (i < len) && (i < maxlen); ++i) { + buffer[i] = buf[len - i - 1]; + } + + // append pad spaces up to given width + if (flags & FLAGS_LEFT) { + while ((len < width) && (len < maxlen)) { + buffer[len++] = ' '; + } + } + + return len; +} +#endif // PRINTF_FLOAT_SUPPORT + + +// internal vsnprintf +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'; + break; + } + + // format specifier? %[flags][width][.precision][length] + if (*format != '%') { + // no + buffer[idx++] = *format; + format++; + continue; + } + else { + // yes, evaluate it + format++; + } + + // evaluate flags + flags = 0U; + do { + switch (*format) { + case '0': flags |= FLAGS_ZEROPAD; format++; n = 1U; break; + case '-': flags |= FLAGS_LEFT; format++; n = 1U; break; + case '+': flags |= FLAGS_PLUS; format++; n = 1U; break; + case ' ': flags |= FLAGS_SPACE; format++; n = 1U; break; + case '#': flags |= FLAGS_HASH; format++; n = 1U; break; + default : n = 0U; break; + } + } while (n); + + // evaluate width field + width = 0U; + if (_is_digit(*format)) { + width = _atoi(&format); + } + else if (*format == '*') { + const int w = va_arg(va, int); + if (w < 0) { + flags |= FLAGS_LEFT; // reverse padding + width = (unsigned int)-w; + } + else { + width = (unsigned int)w; + } + format++; + } + + // evaluate precision field + precision = 0U; + if (*format == '.') { + flags |= FLAGS_PRECISION; + format++; + if (_is_digit(*format)) { + precision = _atoi(&format); + } + else if (*format == '*') { + precision = (unsigned int)va_arg(va, int); + format++; + } + } + + // evaluate length field + if (*format == 'l' || *format == 'L') { + flags |= FLAGS_LONG; + format++; + } + if ((*format == 'l') && (flags & FLAGS_LONG)) { + flags |= FLAGS_LONG_LONG; + format++; + } + + // evaluate specifier + switch (*format) { + case 'u' : + case 'x' : + case 'X' : + case 'o' : + case 'b' : + case 'd' : + case 'i' : { + // set the base + unsigned int base; + if (*format == 'x' || *format == 'X') { + base = 16U; + } + else if (*format == 'o') { + base = 8U; + } + else if (*format == 'b') { + base = 2U; + flags &= ~FLAGS_HASH; // no hash for bin format + } + else { + base = 10U; + flags &= ~FLAGS_HASH; // no hash for dec format + } + // uppercase + if (*format == 'X') { + flags |= FLAGS_UPPERCASE; + } + + // no plus or space flag for u, x, X, o, b + if ((*format != 'i') && (*format != 'd')) { + flags &= ~(FLAGS_PLUS | FLAGS_SPACE); + } + + // convert the integer + if ((*format == 'i') || (*format == 'd')) { + // signed + if (flags & FLAGS_LONG_LONG) { +#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) { + 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 { + 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) { +#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_long(&buffer[idx], va_arg(va, unsigned long), false, base, buffer_len - idx, precision, width, flags); + } + else { + idx += _ntoa_long(&buffer[idx], va_arg(va, unsigned int), false, base, buffer_len - idx, precision, width, flags); + } + } + format++; + break; + } +#if defined(PRINTF_FLOAT_SUPPORT) + case 'f' : + case 'F' : + idx += _ftoa(va_arg(va, double), &buffer[idx], buffer_len - idx, precision, width, flags); + format++; + break; +#endif // PRINTF_FLOAT_SUPPORT + case 'c' : { + size_t l = 1U; + // pre padding + if (!(flags & FLAGS_LEFT)) { + while ((idx < buffer_len) && (l++ < width)) { + buffer[idx++] = ' '; + } + } + // char output + buffer[idx++] = (char)va_arg(va, int); + // post padding + if (flags & FLAGS_LEFT) { + while ((idx < buffer_len) && (l++ < width)) { + buffer[idx++] = ' '; + } + } + format++; + break; + } + + case 's' : { + char* p = va_arg(va, char*); + size_t l = _strlen(p); + // pre padding + if (flags & FLAGS_PRECISION) { + l = (l < precision ? l : precision); + } + if (!(flags & FLAGS_LEFT)) { + while ((idx < buffer_len) && (l++ < width)) { + buffer[idx++] = ' '; + } + } + // string output + while ((idx < buffer_len) && (*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) { + buffer[idx++] = *(p++); + } + // post padding + if (flags & FLAGS_LEFT) { + while ((idx < buffer_len) && (l++ < width)) { + buffer[idx++] = ' '; + } + } + format++; + break; + } + + case 'p' : { + width = sizeof(void*) * 2U; + flags |= FLAGS_ZEROPAD; + size_t size_void = sizeof(void*); + if (size_void > sizeof(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 + } + else { + idx += _ntoa_long(&buffer[idx], (unsigned long)va_arg(va, void*), false, 16U, buffer_len - idx, precision, width, flags); + } + format++; + break; + } + + case '%' : + buffer[idx++] = '%'; + format++; + break; + + default : + buffer[idx++] = *format; + format++; + break; + } + } + + return idx; +} + +/////////////////////////////////////////////////////////////////////////////// + +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); + va_end(va); + for (size_t i = 0U; i < ret; ++i) { + _putchar(buffer[i]); + } + return (int)ret; +} + + +int sprintf(char* buffer, const char* format, ...) +{ + va_list va; + va_start(va, format); + size_t ret = vsnprintf(buffer, (size_t)-1, format, va); + va_end(va); + return (int)ret; +} + + +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); + va_end(va); + return (int)ret; +} diff --git a/printf.cpp b/printf.cpp deleted file mode 100644 index 3e466b2..0000000 --- a/printf.cpp +++ /dev/null @@ -1,619 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////// -// \author (c) Marco Paland (info@paland.com) -// 2014-2017, PALANDesign Hannover, Germany -// -// \license The MIT License (MIT) -// -// 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. These routines are thread -// safe and reentrant! -// Use this instead of the bloated standard/newlib printf cause these use -// malloc for printf (and may not be thread safe). -// -/////////////////////////////////////////////////////////////////////////////// - -#include -#include -#include "printf.h" - - -// buffer size used for printf -#define PRINTF_BUFFER_SIZE 128U - -// ntoa conversion buffer size, this must be big enough to hold one converted numeric number -#define NTOA_BUFFER_SIZE 32U - -// ftoa conversion buffer size, this must be big enough to hold one converted float number -#define FTOA_BUFFER_SIZE 32U - -// 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 - -/////////////////////////////////////////////////////////////////////////////// - -// internal flag definitions -#define FLAGS_ZEROPAD (1U << 0U) -#define FLAGS_LEFT (1U << 1U) -#define FLAGS_PLUS (1U << 2U) -#define FLAGS_SPACE (1U << 3U) -#define FLAGS_HASH (1U << 4U) -#define FLAGS_UPPERCASE (1U << 5U) -#define FLAGS_LONG (1U << 6U) -#define FLAGS_LONG_LONG (1U << 7U) -#define FLAGS_PRECISION (1U << 8U) -#define FLAGS_WIDTH (1U << 9U) - - -// internal strlen, returns the length of the string -static inline size_t _strlen(const char* str) -{ - size_t len = 0U; - while (str[len] != '\0') { - len++; - } - return len; -} - - -// returns true if char is a digit -static inline bool _is_digit(char ch) -{ - return (ch >= '0') && (ch <= '9'); -} - - -// internal ASCII to unsigned int conversion -static inline unsigned int _atoi(const char** str) -{ - unsigned int i = 0U; - while (_is_digit(**str)) { - i = i * 10U + (unsigned int)(*((*str)++) - '0'); - } - return i; -} - - -// 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) -{ - if (maxlen == 0U) { - return 0U; - } - if (base > 16U) { - return 0U; - } - - // pad leading zeros - while (!(flags & FLAGS_LEFT) && (len < prec) && (len < NTOA_BUFFER_SIZE)) { - buf[len++] = '0'; - } - while (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD) && (len < width) && (len < NTOA_BUFFER_SIZE)) { - buf[len++] = '0'; - } - - // handle hash - if (flags & FLAGS_HASH) { - if (((len == prec) || (len == width)) && (len > 0U)) { - len--; - if ((base == 16U) && (len > 0U)) { - len--; - } - } - if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < NTOA_BUFFER_SIZE)) { - buf[len++] = 'x'; - } - if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < NTOA_BUFFER_SIZE)) { - buf[len++] = 'X'; - } - if (len < NTOA_BUFFER_SIZE) { - buf[len++] = '0'; - } - } - - // handle sign - if ((len == width) && (negative || (flags & FLAGS_PLUS) || (flags & FLAGS_SPACE))) { - len--; - } - if (len < NTOA_BUFFER_SIZE) { - if (negative) { - buf[len++] = '-'; - } - else if (flags & FLAGS_PLUS) { - buf[len++] = '+'; // ignore the space if the '+' exists - } - else if (flags & FLAGS_SPACE) { - buf[len++] = ' '; - } - } - - // pad spaces up to given width - if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) { - while ((len < width) && (len < NTOA_BUFFER_SIZE)) { - buf[len++] = ' '; - } - } - - // reverse string - for (size_t i = 0U; (i < len) && (i < maxlen); ++i) { - buffer[i] = buf[len - i - 1U]; - } - - // append pad spaces up to given width - if (flags & FLAGS_LEFT) { - while ((len < width) && (len < maxlen)) { - buffer[len++] = ' '; - } - } - - return len; -} - - -// 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) -{ - 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; - - // 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 - } - if (prec > 9U) { - // precision of >= 10 can lead to overflow errors - 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; - diff = tmp - frac; - - if (diff > 0.5) { - ++frac; - // handle rollover, e.g. case 0.99 with prec 1 is 1.0 - if (frac >= pow10[prec]) { - frac = 0; - ++whole; - } - } - else if ((diff == 0.5) && ((frac == 0) || (frac & 1))) { - // if halfway, round up if odd, OR if last digit is 0 - ++frac; - } - - // 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; - } - - if (prec == 0) { - diff = value - whole; - if (diff > 0.5) { - // greater than 0.5, round up, e.g. 1.6 -> 2 - ++whole; - } - else if (diff == 0.5 && (whole & 1)) { - // exactly 0.5 and ODD, then round up - // 1.5 -> 2, but 2.5 -> 2 - ++whole; - } - } - else { - unsigned int count = prec; - // now do fractional part, as an unsigned number - do { - --count; - buf[len++] = (char)(48U + (frac % 10U)); - } while ((len < FTOA_BUFFER_SIZE) && (frac /= 10U)); - // add extra 0s - while ((len < FTOA_BUFFER_SIZE) && (count-- > 0U)) { - buf[len++] = '0'; - } - if (len < FTOA_BUFFER_SIZE) { - // add decimal - buf[len++] = '.'; - } - } - - // do whole part, number is reversed - while (len < FTOA_BUFFER_SIZE) { - buf[len++] = (char)(48 + (whole % 10)); - if (!(whole /= 10)) { - break; - } - } - - // pad leading zeros - while (!(flags & FLAGS_LEFT) && (len < prec) && (len < FTOA_BUFFER_SIZE)) { - buf[len++] = '0'; - } - while (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD) && (len < width) && (len < FTOA_BUFFER_SIZE)) { - buf[len++] = '0'; - } - - // handle sign - if (len < FTOA_BUFFER_SIZE) { - if (negative) { - buf[len++] = '-'; - } - else if (flags & FLAGS_PLUS) { - buf[len++] = '+'; // ignore the space if the '+' exists - } - else if (flags & FLAGS_SPACE) { - buf[len++] = ' '; - } - } - - // pad spaces up to given width - if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) { - while ((len < width) && (len < FTOA_BUFFER_SIZE)) { - buf[len++] = ' '; - } - } - - // reverse string - for (size_t i = 0U; (i < len) && (i < maxlen); ++i) { - buffer[i] = buf[len - i - 1]; - } - - // append pad spaces up to given width - if (flags & FLAGS_LEFT) { - while ((len < width) && (len < maxlen)) { - buffer[len++] = ' '; - } - } - - return len; -} -#endif // PRINTF_FLOAT_SUPPORT - - -// internal vsnprintf -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'; - break; - } - - // format specifier? %[flags][width][.precision][length] - if (*format != '%') { - // no - buffer[idx++] = *format; - format++; - continue; - } - else { - // yes, evaluate it - format++; - } - - // evaluate flags - flags = 0U; - do { - switch (*format) { - case '0': flags |= FLAGS_ZEROPAD; format++; n = 1U; break; - case '-': flags |= FLAGS_LEFT; format++; n = 1U; break; - case '+': flags |= FLAGS_PLUS; format++; n = 1U; break; - case ' ': flags |= FLAGS_SPACE; format++; n = 1U; break; - case '#': flags |= FLAGS_HASH; format++; n = 1U; break; - default : n = 0U; break; - } - } while (n); - - // evaluate width field - width = 0U; - if (_is_digit(*format)) { - width = _atoi(&format); - } - else if (*format == '*') { - const int w = va_arg(va, int); - if (w < 0) { - flags |= FLAGS_LEFT; // reverse padding - width = (unsigned int)-w; - } - else { - width = (unsigned int)w; - } - format++; - } - - // evaluate precision field - precision = 0U; - if (*format == '.') { - flags |= FLAGS_PRECISION; - format++; - if (_is_digit(*format)) { - precision = _atoi(&format); - } - else if (*format == '*') { - precision = (unsigned int)va_arg(va, int); - format++; - } - } - - // evaluate length field - if (*format == 'l' || *format == 'L') { - flags |= FLAGS_LONG; - format++; - } - if ((*format == 'l') && (flags & FLAGS_LONG)) { - flags |= FLAGS_LONG_LONG; - format++; - } - - // evaluate specifier - switch (*format) { - case 'u' : - case 'x' : - case 'X' : - case 'o' : - case 'b' : - case 'd' : - case 'i' : { - // set the base - unsigned int base; - if (*format == 'x' || *format == 'X') { - base = 16U; - } - else if (*format == 'o') { - base = 8U; - } - else if (*format == 'b') { - base = 2U; - flags &= ~FLAGS_HASH; // no hash for bin format - } - else { - base = 10U; - flags &= ~FLAGS_HASH; // no hash for dec format - } - // uppercase - if (*format == 'X') { - flags |= FLAGS_UPPERCASE; - } - - // no plus or space flag for u, x, X, o, b - if ((*format != 'i') && (*format != 'd')) { - flags &= ~(FLAGS_PLUS | FLAGS_SPACE); - } - - // convert the integer - if ((*format == 'i') || (*format == 'd')) { - // signed - if (flags & FLAGS_LONG_LONG) { -#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) { - 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 { - 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) { -#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_long(&buffer[idx], va_arg(va, unsigned long), false, base, buffer_len - idx, precision, width, flags); - } - else { - idx += _ntoa_long(&buffer[idx], va_arg(va, unsigned int), false, base, buffer_len - idx, precision, width, flags); - } - } - format++; - break; - } -#if defined(PRINTF_FLOAT_SUPPORT) - case 'f' : - case 'F' : - idx += _ftoa(va_arg(va, double), &buffer[idx], buffer_len - idx, precision, width, flags); - format++; - break; -#endif // PRINTF_FLOAT_SUPPORT - case 'c' : { - size_t l = 1U; - // pre padding - if (!(flags & FLAGS_LEFT)) { - while ((idx < buffer_len) && (l++ < width)) { - buffer[idx++] = ' '; - } - } - // char output - buffer[idx++] = (char)va_arg(va, int); - // post padding - if (flags & FLAGS_LEFT) { - while ((idx < buffer_len) && (l++ < width)) { - buffer[idx++] = ' '; - } - } - format++; - break; - } - - case 's' : { - char* p = va_arg(va, char*); - size_t l = _strlen(p); - // pre padding - if (flags & FLAGS_PRECISION) { - l = (l < precision ? l : precision); - } - if (!(flags & FLAGS_LEFT)) { - while ((idx < buffer_len) && (l++ < width)) { - buffer[idx++] = ' '; - } - } - // string output - while ((idx < buffer_len) && (*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) { - buffer[idx++] = *(p++); - } - // post padding - if (flags & FLAGS_LEFT) { - while ((idx < buffer_len) && (l++ < width)) { - buffer[idx++] = ' '; - } - } - format++; - break; - } - - case 'p' : { - width = sizeof(void*) * 2U; - flags |= FLAGS_ZEROPAD; - size_t size_void = sizeof(void*); - if (size_void > sizeof(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 - } - else { - idx += _ntoa_long(&buffer[idx], (unsigned long)va_arg(va, void*), false, 16U, buffer_len - idx, precision, width, flags); - } - format++; - break; - } - - case '%' : - buffer[idx++] = '%'; - format++; - break; - - default : - buffer[idx++] = *format; - format++; - break; - } - } - - return idx; -} - -/////////////////////////////////////////////////////////////////////////////// - -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); - va_end(va); - for (size_t i = 0U; i < ret; ++i) { - _putchar(buffer[i]); - } - return (int)ret; -} - - -int sprintf(char* buffer, const char* format, ...) -{ - va_list va; - va_start(va, format); - size_t ret = vsnprintf(buffer, (size_t)-1, format, va); - va_end(va); - return (int)ret; -} - - -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); - va_end(va); - return (int)ret; -} diff --git a/test/test_suite.cpp b/test/test_suite.cpp index a291cc7..9e186aa 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) +{ } -- cgit v1.2.3 From 5da5ac6cdd0ae16fa073963b40873620d4457d00 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 09:38:07 +0100 Subject: Update readme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7af4a2f..5ce15a5 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.** -- cgit v1.2.3 From ebe4998712a3cb389d21396c79f328c381f63d44 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 09:44:50 +0100 Subject: 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 0000000..6d44728 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# ignore test path +test/* linguist-vendored -- cgit v1.2.3 From a0af9125e8643bc0c765b281d0e6acc2e25f4fee Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 10:08:50 +0100 Subject: Updated readme --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5ce15a5..7f91d8d 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 -- cgit v1.2.3 From 6e0f97d33dc43f9663edcc999337e5c80a1fc64b Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 10:13:17 +0100 Subject: Updated license info --- printf.h | 1 - 1 file changed, 1 deletion(-) diff --git a/printf.h b/printf.h index ebb76db..4ddaa95 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 -- cgit v1.2.3 From 80dd57309a4ada96611adb41472d083105c61daa Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Mon, 13 Nov 2017 11:45:13 +0100 Subject: Code cleanup --- printf.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/printf.c b/printf.c index 3e466b2..f634519 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); -- cgit v1.2.3 From 9b7cc4837c77caa242e5b06ff29117ae874c6785 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Tue, 21 Nov 2017 14:46:36 +0100 Subject: Fixed param comment --- printf.h | 1 - 1 file changed, 1 deletion(-) diff --git a/printf.h b/printf.h index 4ddaa95..37d93dd 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); -- cgit v1.2.3 From 7158a6f50f60008d7b475155f57120113c85cf69 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Tue, 21 Nov 2017 14:47:13 +0100 Subject: 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 9e186aa..910ed91 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")); + } } -- cgit v1.2.3 From c6c8d964201b26765efa48d9d54467a792759329 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Tue, 21 Nov 2017 14:49:58 +0100 Subject: Updated comments --- printf.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/printf.c b/printf.c index f634519..fd29785 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) -- cgit v1.2.3 From 2b3f3c13067f0f3f061dcfd7b59b925082e72d0d Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Tue, 21 Nov 2017 14:52:23 +0100 Subject: 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 fd29785..83c6d9e 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; -- cgit v1.2.3 From 99021707632120a804a6126a149620c651088eb8 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Tue, 21 Nov 2017 14:54:14 +0100 Subject: 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 83c6d9e..3ec36f4 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 -- cgit v1.2.3 From cb7d11a542b085ab4a2f771405c76e62da0b18c9 Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Tue, 21 Nov 2017 14:54:48 +0100 Subject: Code cleanup --- printf.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/printf.c b/printf.c index 3ec36f4..49ef68f 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; } -- cgit v1.2.3