Don't call printf
[c11tester.git] / printf.c
diff --git a/printf.c b/printf.c
new file mode 100644 (file)
index 0000000..8a700ad
--- /dev/null
+++ b/printf.c
@@ -0,0 +1,914 @@
+///////////////////////////////////////////////////////////////////////////////\r
+// \author (c) Marco Paland (info@paland.com)\r
+//             2014-2019, PALANDesign Hannover, Germany\r
+//\r
+// \license The MIT License (MIT)\r
+//\r
+// Permission is hereby granted, free of charge, to any person obtaining a copy\r
+// of this software and associated documentation files (the "Software"), to deal\r
+// in the Software without restriction, including without limitation the rights\r
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+// copies of the Software, and to permit persons to whom the Software is\r
+// furnished to do so, subject to the following conditions:\r
+//\r
+// The above copyright notice and this permission notice shall be included in\r
+// all copies or substantial portions of the Software.\r
+//\r
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
+// THE SOFTWARE.\r
+//\r
+// \brief Tiny printf, sprintf and (v)snprintf implementation, optimized for speed on\r
+//        embedded systems with a very limited resources. These routines are thread\r
+//        safe and reentrant!\r
+//        Use this instead of the bloated standard/newlib printf cause these use\r
+//        malloc for printf (and may not be thread safe).\r
+//\r
+///////////////////////////////////////////////////////////////////////////////\r
+\r
+#include <stdbool.h>\r
+#include <stdint.h>\r
+\r
+#include "printf.h"\r
+\r
+\r
+// define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H ...) to include the\r
+// printf_config.h header file\r
+// default: undefined\r
+#ifdef PRINTF_INCLUDE_CONFIG_H\r
+#include "printf_config.h"\r
+#endif\r
+\r
+\r
+// 'ntoa' conversion buffer size, this must be big enough to hold one converted\r
+// numeric number including padded zeros (dynamically created on stack)\r
+// default: 32 byte\r
+#ifndef PRINTF_NTOA_BUFFER_SIZE\r
+#define PRINTF_NTOA_BUFFER_SIZE    32U\r
+#endif\r
+\r
+// 'ftoa' conversion buffer size, this must be big enough to hold one converted\r
+// float number including padded zeros (dynamically created on stack)\r
+// default: 32 byte\r
+#ifndef PRINTF_FTOA_BUFFER_SIZE\r
+#define PRINTF_FTOA_BUFFER_SIZE    32U\r
+#endif\r
+\r
+// support for the floating point type (%f)\r
+// default: activated\r
+#ifndef PRINTF_DISABLE_SUPPORT_FLOAT\r
+#define PRINTF_SUPPORT_FLOAT\r
+#endif\r
+\r
+// support for exponential floating point notation (%e/%g)\r
+// default: activated\r
+#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL\r
+#define PRINTF_SUPPORT_EXPONENTIAL\r
+#endif\r
+\r
+// define the default floating point precision\r
+// default: 6 digits\r
+#ifndef PRINTF_DEFAULT_FLOAT_PRECISION\r
+#define PRINTF_DEFAULT_FLOAT_PRECISION  6U\r
+#endif\r
+\r
+// define the largest float suitable to print with %f\r
+// default: 1e9\r
+#ifndef PRINTF_MAX_FLOAT\r
+#define PRINTF_MAX_FLOAT  1e9\r
+#endif\r
+\r
+// support for the long long types (%llu or %p)\r
+// default: activated\r
+#ifndef PRINTF_DISABLE_SUPPORT_LONG_LONG\r
+#define PRINTF_SUPPORT_LONG_LONG\r
+#endif\r
+\r
+// support for the ptrdiff_t type (%t)\r
+// ptrdiff_t is normally defined in <stddef.h> as long or long long type\r
+// default: activated\r
+#ifndef PRINTF_DISABLE_SUPPORT_PTRDIFF_T\r
+#define PRINTF_SUPPORT_PTRDIFF_T\r
+#endif\r
+\r
+///////////////////////////////////////////////////////////////////////////////\r
+\r
+// internal flag definitions\r
+#define FLAGS_ZEROPAD   (1U <<  0U)\r
+#define FLAGS_LEFT      (1U <<  1U)\r
+#define FLAGS_PLUS      (1U <<  2U)\r
+#define FLAGS_SPACE     (1U <<  3U)\r
+#define FLAGS_HASH      (1U <<  4U)\r
+#define FLAGS_UPPERCASE (1U <<  5U)\r
+#define FLAGS_CHAR      (1U <<  6U)\r
+#define FLAGS_SHORT     (1U <<  7U)\r
+#define FLAGS_LONG      (1U <<  8U)\r
+#define FLAGS_LONG_LONG (1U <<  9U)\r
+#define FLAGS_PRECISION (1U << 10U)\r
+#define FLAGS_ADAPT_EXP (1U << 11U)\r
+\r
+\r
+// import float.h for DBL_MAX\r
+#if defined(PRINTF_SUPPORT_FLOAT)\r
+#include <float.h>\r
+#endif\r
+\r
+\r
+// output function type\r
+typedef void (*out_fct_type)(char character, void* buffer, size_t idx, size_t maxlen);\r
+\r
+\r
+// wrapper (used as buffer) for output function type\r
+typedef struct {\r
+  void  (*fct)(char character, void* arg);\r
+  void* arg;\r
+} out_fct_wrap_type;\r
+\r
+\r
+// internal buffer output\r
+static inline void _out_buffer(char character, void* buffer, size_t idx, size_t maxlen)\r
+{\r
+  if (idx < maxlen) {\r
+    ((char*)buffer)[idx] = character;\r
+  }\r
+}\r
+\r
+\r
+// internal null output\r
+static inline void _out_null(char character, void* buffer, size_t idx, size_t maxlen)\r
+{\r
+  (void)character; (void)buffer; (void)idx; (void)maxlen;\r
+}\r
+\r
+\r
+// internal _putchar wrapper\r
+static inline void _out_char(char character, void* buffer, size_t idx, size_t maxlen)\r
+{\r
+  (void)buffer; (void)idx; (void)maxlen;\r
+  if (character) {\r
+    _putchar(character);\r
+  }\r
+}\r
+\r
+\r
+// internal output function wrapper\r
+static inline void _out_fct(char character, void* buffer, size_t idx, size_t maxlen)\r
+{\r
+  (void)idx; (void)maxlen;\r
+  if (character) {\r
+    // buffer is the output fct pointer\r
+    ((out_fct_wrap_type*)buffer)->fct(character, ((out_fct_wrap_type*)buffer)->arg);\r
+  }\r
+}\r
+\r
+\r
+// internal secure strlen\r
+// \return The length of the string (excluding the terminating 0) limited by 'maxsize'\r
+static inline unsigned int _strnlen_s(const char* str, size_t maxsize)\r
+{\r
+  const char* s;\r
+  for (s = str; *s && maxsize--; ++s);\r
+  return (unsigned int)(s - str);\r
+}\r
+\r
+\r
+// internal test if char is a digit (0-9)\r
+// \return true if char is a digit\r
+static inline bool _is_digit(char ch)\r
+{\r
+  return (ch >= '0') && (ch <= '9');\r
+}\r
+\r
+\r
+// internal ASCII string to unsigned int conversion\r
+static unsigned int _atoi(const char** str)\r
+{\r
+  unsigned int i = 0U;\r
+  while (_is_digit(**str)) {\r
+    i = i * 10U + (unsigned int)(*((*str)++) - '0');\r
+  }\r
+  return i;\r
+}\r
+\r
+\r
+// output the specified string in reverse, taking care of any zero-padding\r
+static size_t _out_rev(out_fct_type out, char* buffer, size_t idx, size_t maxlen, const char* buf, size_t len, unsigned int width, unsigned int flags)\r
+{\r
+  const size_t start_idx = idx;\r
+\r
+  // pad spaces up to given width\r
+  if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) {\r
+    for (size_t i = len; i < width; i++) {\r
+      out(' ', buffer, idx++, maxlen);\r
+    }\r
+  }\r
+\r
+  // reverse string\r
+  while (len) {\r
+    out(buf[--len], buffer, idx++, maxlen);\r
+  }\r
+\r
+  // append pad spaces up to given width\r
+  if (flags & FLAGS_LEFT) {\r
+    while (idx - start_idx < width) {\r
+      out(' ', buffer, idx++, maxlen);\r
+    }\r
+  }\r
+\r
+  return idx;\r
+}\r
+\r
+\r
+// internal itoa format\r
+static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags)\r
+{\r
+  // pad leading zeros\r
+  if (!(flags & FLAGS_LEFT)) {\r
+    if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {\r
+      width--;\r
+    }\r
+    while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) {\r
+      buf[len++] = '0';\r
+    }\r
+    while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) {\r
+      buf[len++] = '0';\r
+    }\r
+  }\r
+\r
+  // handle hash\r
+  if (flags & FLAGS_HASH) {\r
+    if (!(flags & FLAGS_PRECISION) && len && ((len == prec) || (len == width))) {\r
+      len--;\r
+      if (len && (base == 16U)) {\r
+        len--;\r
+      }\r
+    }\r
+    if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {\r
+      buf[len++] = 'x';\r
+    }\r
+    else if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {\r
+      buf[len++] = 'X';\r
+    }\r
+    else if ((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) {\r
+      buf[len++] = 'b';\r
+    }\r
+    if (len < PRINTF_NTOA_BUFFER_SIZE) {\r
+      buf[len++] = '0';\r
+    }\r
+  }\r
+\r
+  if (len < PRINTF_NTOA_BUFFER_SIZE) {\r
+    if (negative) {\r
+      buf[len++] = '-';\r
+    }\r
+    else if (flags & FLAGS_PLUS) {\r
+      buf[len++] = '+';  // ignore the space if the '+' exists\r
+    }\r
+    else if (flags & FLAGS_SPACE) {\r
+      buf[len++] = ' ';\r
+    }\r
+  }\r
+\r
+  return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags);\r
+}\r
+\r
+\r
+// internal itoa for 'long' type\r
+static size_t _ntoa_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long value, bool negative, unsigned long base, unsigned int prec, unsigned int width, unsigned int flags)\r
+{\r
+  char buf[PRINTF_NTOA_BUFFER_SIZE];\r
+  size_t len = 0U;\r
+\r
+  // no hash for 0 values\r
+  if (!value) {\r
+    flags &= ~FLAGS_HASH;\r
+  }\r
+\r
+  // write if precision != 0 and value is != 0\r
+  if (!(flags & FLAGS_PRECISION) || value) {\r
+    do {\r
+      const char digit = (char)(value % base);\r
+      buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10;\r
+      value /= base;\r
+    } while (value && (len < PRINTF_NTOA_BUFFER_SIZE));\r
+  }\r
+\r
+  return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags);\r
+}\r
+\r
+\r
+// internal itoa for 'long long' type\r
+#if defined(PRINTF_SUPPORT_LONG_LONG)\r
+static size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long long value, bool negative, unsigned long long base, unsigned int prec, unsigned int width, unsigned int flags)\r
+{\r
+  char buf[PRINTF_NTOA_BUFFER_SIZE];\r
+  size_t len = 0U;\r
+\r
+  // no hash for 0 values\r
+  if (!value) {\r
+    flags &= ~FLAGS_HASH;\r
+  }\r
+\r
+  // write if precision != 0 and value is != 0\r
+  if (!(flags & FLAGS_PRECISION) || value) {\r
+    do {\r
+      const char digit = (char)(value % base);\r
+      buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10;\r
+      value /= base;\r
+    } while (value && (len < PRINTF_NTOA_BUFFER_SIZE));\r
+  }\r
+\r
+  return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags);\r
+}\r
+#endif  // PRINTF_SUPPORT_LONG_LONG\r
+\r
+\r
+#if defined(PRINTF_SUPPORT_FLOAT)\r
+\r
+#if defined(PRINTF_SUPPORT_EXPONENTIAL)\r
+// forward declaration so that _ftoa can switch to exp notation for values > PRINTF_MAX_FLOAT\r
+static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags);\r
+#endif\r
+\r
+\r
+// internal ftoa for fixed decimal floating point\r
+static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)\r
+{\r
+  char buf[PRINTF_FTOA_BUFFER_SIZE];\r
+  size_t len  = 0U;\r
+  double diff = 0.0;\r
+\r
+  // powers of 10\r
+  static const double pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };\r
+\r
+  // test for special values\r
+  if (value != value)\r
+    return _out_rev(out, buffer, idx, maxlen, "nan", 3, width, flags);\r
+  if (value < -DBL_MAX)\r
+    return _out_rev(out, buffer, idx, maxlen, "fni-", 4, width, flags);\r
+  if (value > DBL_MAX)\r
+    return _out_rev(out, buffer, idx, maxlen, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4U : 3U, width, flags);\r
+\r
+  // test for very large values\r
+  // standard printf behavior is to print EVERY whole number digit -- which could be 100s of characters overflowing your buffers == bad\r
+  if ((value > PRINTF_MAX_FLOAT) || (value < -PRINTF_MAX_FLOAT)) {\r
+#if defined(PRINTF_SUPPORT_EXPONENTIAL)\r
+    return _etoa(out, buffer, idx, maxlen, value, prec, width, flags);\r
+#else\r
+    return 0U;\r
+#endif\r
+  }\r
+\r
+  // test for negative\r
+  bool negative = false;\r
+  if (value < 0) {\r
+    negative = true;\r
+    value = 0 - value;\r
+  }\r
+\r
+  // set default precision, if not set explicitly\r
+  if (!(flags & FLAGS_PRECISION)) {\r
+    prec = PRINTF_DEFAULT_FLOAT_PRECISION;\r
+  }\r
+  // limit precision to 9, cause a prec >= 10 can lead to overflow errors\r
+  while ((len < PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) {\r
+    buf[len++] = '0';\r
+    prec--;\r
+  }\r
+\r
+  int whole = (int)value;\r
+  double tmp = (value - whole) * pow10[prec];\r
+  unsigned long frac = (unsigned long)tmp;\r
+  diff = tmp - frac;\r
+\r
+  if (diff > 0.5) {\r
+    ++frac;\r
+    // handle rollover, e.g. case 0.99 with prec 1 is 1.0\r
+    if (frac >= pow10[prec]) {\r
+      frac = 0;\r
+      ++whole;\r
+    }\r
+  }\r
+  else if (diff < 0.5) {\r
+  }\r
+  else if ((frac == 0U) || (frac & 1U)) {\r
+    // if halfway, round up if odd OR if last digit is 0\r
+    ++frac;\r
+  }\r
+\r
+  if (prec == 0U) {\r
+    diff = value - (double)whole;\r
+    if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) {\r
+      // exactly 0.5 and ODD, then round up\r
+      // 1.5 -> 2, but 2.5 -> 2\r
+      ++whole;\r
+    }\r
+  }\r
+  else {\r
+    unsigned int count = prec;\r
+    // now do fractional part, as an unsigned number\r
+    while (len < PRINTF_FTOA_BUFFER_SIZE) {\r
+      --count;\r
+      buf[len++] = (char)(48U + (frac % 10U));\r
+      if (!(frac /= 10U)) {\r
+        break;\r
+      }\r
+    }\r
+    // add extra 0s\r
+    while ((len < PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) {\r
+      buf[len++] = '0';\r
+    }\r
+    if (len < PRINTF_FTOA_BUFFER_SIZE) {\r
+      // add decimal\r
+      buf[len++] = '.';\r
+    }\r
+  }\r
+\r
+  // do whole part, number is reversed\r
+  while (len < PRINTF_FTOA_BUFFER_SIZE) {\r
+    buf[len++] = (char)(48 + (whole % 10));\r
+    if (!(whole /= 10)) {\r
+      break;\r
+    }\r
+  }\r
+\r
+  // pad leading zeros\r
+  if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) {\r
+    if (width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {\r
+      width--;\r
+    }\r
+    while ((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) {\r
+      buf[len++] = '0';\r
+    }\r
+  }\r
+\r
+  if (len < PRINTF_FTOA_BUFFER_SIZE) {\r
+    if (negative) {\r
+      buf[len++] = '-';\r
+    }\r
+    else if (flags & FLAGS_PLUS) {\r
+      buf[len++] = '+';  // ignore the space if the '+' exists\r
+    }\r
+    else if (flags & FLAGS_SPACE) {\r
+      buf[len++] = ' ';\r
+    }\r
+  }\r
+\r
+  return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags);\r
+}\r
+\r
+\r
+#if defined(PRINTF_SUPPORT_EXPONENTIAL)\r
+// internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse <m.jasperse@gmail.com>\r
+static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)\r
+{\r
+  // check for NaN and special values\r
+  if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) {\r
+    return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags);\r
+  }\r
+\r
+  // determine the sign\r
+  const bool negative = value < 0;\r
+  if (negative) {\r
+    value = -value;\r
+  }\r
+\r
+  // default precision\r
+  if (!(flags & FLAGS_PRECISION)) {\r
+    prec = PRINTF_DEFAULT_FLOAT_PRECISION;\r
+  }\r
+\r
+  // determine the decimal exponent\r
+  // based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c)\r
+  union {\r
+    uint64_t U;\r
+    double   F;\r
+  } conv;\r
+\r
+  conv.F = value;\r
+  int exp2 = (int)((conv.U >> 52U) & 0x07FFU) - 1023;           // effectively log2\r
+  conv.U = (conv.U & ((1ULL << 52U) - 1U)) | (1023ULL << 52U);  // drop the exponent so conv.F is now in [1,2)\r
+  // now approximate log10 from the log2 integer part and an expansion of ln around 1.5\r
+  int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981 + (conv.F - 1.5) * 0.289529654602168);\r
+  // now we want to compute 10^expval but we want to be sure it won't overflow\r
+  exp2 = (int)(expval * 3.321928094887362 + 0.5);\r
+  const double z  = expval * 2.302585092994046 - exp2 * 0.6931471805599453;\r
+  const double z2 = z * z;\r
+  conv.U = (uint64_t)(exp2 + 1023) << 52U;\r
+  // compute exp(z) using continued fractions, see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex\r
+  conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14)))));\r
+  // correct for rounding errors\r
+  if (value < conv.F) {\r
+    expval--;\r
+    conv.F /= 10;\r
+  }\r
+\r
+  // the exponent format is "%+03d" and largest value is "307", so set aside 4-5 characters\r
+  unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U;\r
+\r
+  // in "%g" mode, "prec" is the number of *significant figures* not decimals\r
+  if (flags & FLAGS_ADAPT_EXP) {\r
+    // do we want to fall-back to "%f" mode?\r
+    if ((value >= 1e-4) && (value < 1e6)) {\r
+      if ((int)prec > expval) {\r
+        prec = (unsigned)((int)prec - expval - 1);\r
+      }\r
+      else {\r
+        prec = 0;\r
+      }\r
+      flags |= FLAGS_PRECISION;   // make sure _ftoa respects precision\r
+      // no characters in exponent\r
+      minwidth = 0U;\r
+      expval   = 0;\r
+    }\r
+    else {\r
+      // we use one sigfig for the whole part\r
+      if ((prec > 0) && (flags & FLAGS_PRECISION)) {\r
+        --prec;\r
+      }\r
+    }\r
+  }\r
+\r
+  // will everything fit?\r
+  unsigned int fwidth = width;\r
+  if (width > minwidth) {\r
+    // we didn't fall-back so subtract the characters required for the exponent\r
+    fwidth -= minwidth;\r
+  } else {\r
+    // not enough characters, so go back to default sizing\r
+    fwidth = 0U;\r
+  }\r
+  if ((flags & FLAGS_LEFT) && minwidth) {\r
+    // if we're padding on the right, DON'T pad the floating part\r
+    fwidth = 0U;\r
+  }\r
+\r
+  // rescale the float value\r
+  if (expval) {\r
+    value /= conv.F;\r
+  }\r
+\r
+  // output the floating part\r
+  const size_t start_idx = idx;\r
+  idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP);\r
+\r
+  // output the exponent part\r
+  if (minwidth) {\r
+    // output the exponential symbol\r
+    out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen);\r
+    // output the exponent value\r
+    idx = _ntoa_long(out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth-1, FLAGS_ZEROPAD | FLAGS_PLUS);\r
+    // might need to right-pad spaces\r
+    if (flags & FLAGS_LEFT) {\r
+      while (idx - start_idx < width) out(' ', buffer, idx++, maxlen);\r
+    }\r
+  }\r
+  return idx;\r
+}\r
+#endif  // PRINTF_SUPPORT_EXPONENTIAL\r
+#endif  // PRINTF_SUPPORT_FLOAT\r
+\r
+\r
+// internal vsnprintf\r
+static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, va_list va)\r
+{\r
+  unsigned int flags, width, precision, n;\r
+  size_t idx = 0U;\r
+\r
+  if (!buffer) {\r
+    // use null output function\r
+    out = _out_null;\r
+  }\r
+\r
+  while (*format)\r
+  {\r
+    // format specifier?  %[flags][width][.precision][length]\r
+    if (*format != '%') {\r
+      // no\r
+      out(*format, buffer, idx++, maxlen);\r
+      format++;\r
+      continue;\r
+    }\r
+    else {\r
+      // yes, evaluate it\r
+      format++;\r
+    }\r
+\r
+    // evaluate flags\r
+    flags = 0U;\r
+    do {\r
+      switch (*format) {\r
+        case '0': flags |= FLAGS_ZEROPAD; format++; n = 1U; break;\r
+        case '-': flags |= FLAGS_LEFT;    format++; n = 1U; break;\r
+        case '+': flags |= FLAGS_PLUS;    format++; n = 1U; break;\r
+        case ' ': flags |= FLAGS_SPACE;   format++; n = 1U; break;\r
+        case '#': flags |= FLAGS_HASH;    format++; n = 1U; break;\r
+        default :                                   n = 0U; break;\r
+      }\r
+    } while (n);\r
+\r
+    // evaluate width field\r
+    width = 0U;\r
+    if (_is_digit(*format)) {\r
+      width = _atoi(&format);\r
+    }\r
+    else if (*format == '*') {\r
+      const int w = va_arg(va, int);\r
+      if (w < 0) {\r
+        flags |= FLAGS_LEFT;    // reverse padding\r
+        width = (unsigned int)-w;\r
+      }\r
+      else {\r
+        width = (unsigned int)w;\r
+      }\r
+      format++;\r
+    }\r
+\r
+    // evaluate precision field\r
+    precision = 0U;\r
+    if (*format == '.') {\r
+      flags |= FLAGS_PRECISION;\r
+      format++;\r
+      if (_is_digit(*format)) {\r
+        precision = _atoi(&format);\r
+      }\r
+      else if (*format == '*') {\r
+        const int prec = (int)va_arg(va, int);\r
+        precision = prec > 0 ? (unsigned int)prec : 0U;\r
+        format++;\r
+      }\r
+    }\r
+\r
+    // evaluate length field\r
+    switch (*format) {\r
+      case 'l' :\r
+        flags |= FLAGS_LONG;\r
+        format++;\r
+        if (*format == 'l') {\r
+          flags |= FLAGS_LONG_LONG;\r
+          format++;\r
+        }\r
+        break;\r
+      case 'h' :\r
+        flags |= FLAGS_SHORT;\r
+        format++;\r
+        if (*format == 'h') {\r
+          flags |= FLAGS_CHAR;\r
+          format++;\r
+        }\r
+        break;\r
+#if defined(PRINTF_SUPPORT_PTRDIFF_T)\r
+      case 't' :\r
+        flags |= (sizeof(ptrdiff_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);\r
+        format++;\r
+        break;\r
+#endif\r
+      case 'j' :\r
+        flags |= (sizeof(intmax_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);\r
+        format++;\r
+        break;\r
+      case 'z' :\r
+        flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);\r
+        format++;\r
+        break;\r
+      default :\r
+        break;\r
+    }\r
+\r
+    // evaluate specifier\r
+    switch (*format) {\r
+      case 'd' :\r
+      case 'i' :\r
+      case 'u' :\r
+      case 'x' :\r
+      case 'X' :\r
+      case 'o' :\r
+      case 'b' : {\r
+        // set the base\r
+        unsigned int base;\r
+        if (*format == 'x' || *format == 'X') {\r
+          base = 16U;\r
+        }\r
+        else if (*format == 'o') {\r
+          base =  8U;\r
+        }\r
+        else if (*format == 'b') {\r
+          base =  2U;\r
+        }\r
+        else {\r
+          base = 10U;\r
+          flags &= ~FLAGS_HASH;   // no hash for dec format\r
+        }\r
+        // uppercase\r
+        if (*format == 'X') {\r
+          flags |= FLAGS_UPPERCASE;\r
+        }\r
+\r
+        // no plus or space flag for u, x, X, o, b\r
+        if ((*format != 'i') && (*format != 'd')) {\r
+          flags &= ~(FLAGS_PLUS | FLAGS_SPACE);\r
+        }\r
+\r
+        // ignore '0' flag when precision is given\r
+        if (flags & FLAGS_PRECISION) {\r
+          flags &= ~FLAGS_ZEROPAD;\r
+        }\r
+\r
+        // convert the integer\r
+        if ((*format == 'i') || (*format == 'd')) {\r
+          // signed\r
+          if (flags & FLAGS_LONG_LONG) {\r
+#if defined(PRINTF_SUPPORT_LONG_LONG)\r
+            const long long value = va_arg(va, long long);\r
+            idx = _ntoa_long_long(out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);\r
+#endif\r
+          }\r
+          else if (flags & FLAGS_LONG) {\r
+            const long value = va_arg(va, long);\r
+            idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);\r
+          }\r
+          else {\r
+            const int value = (flags & FLAGS_CHAR) ? (char)va_arg(va, int) : (flags & FLAGS_SHORT) ? (short int)va_arg(va, int) : va_arg(va, int);\r
+            idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);\r
+          }\r
+        }\r
+        else {\r
+          // unsigned\r
+          if (flags & FLAGS_LONG_LONG) {\r
+#if defined(PRINTF_SUPPORT_LONG_LONG)\r
+            idx = _ntoa_long_long(out, buffer, idx, maxlen, va_arg(va, unsigned long long), false, base, precision, width, flags);\r
+#endif\r
+          }\r
+          else if (flags & FLAGS_LONG) {\r
+            idx = _ntoa_long(out, buffer, idx, maxlen, va_arg(va, unsigned long), false, base, precision, width, flags);\r
+          }\r
+          else {\r
+            const unsigned int value = (flags & FLAGS_CHAR) ? (unsigned char)va_arg(va, unsigned int) : (flags & FLAGS_SHORT) ? (unsigned short int)va_arg(va, unsigned int) : va_arg(va, unsigned int);\r
+            idx = _ntoa_long(out, buffer, idx, maxlen, value, false, base, precision, width, flags);\r
+          }\r
+        }\r
+        format++;\r
+        break;\r
+      }\r
+#if defined(PRINTF_SUPPORT_FLOAT)\r
+      case 'f' :\r
+      case 'F' :\r
+        if (*format == 'F') flags |= FLAGS_UPPERCASE;\r
+        idx = _ftoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags);\r
+        format++;\r
+        break;\r
+#if defined(PRINTF_SUPPORT_EXPONENTIAL)\r
+      case 'e':\r
+      case 'E':\r
+      case 'g':\r
+      case 'G':\r
+        if ((*format == 'g')||(*format == 'G')) flags |= FLAGS_ADAPT_EXP;\r
+        if ((*format == 'E')||(*format == 'G')) flags |= FLAGS_UPPERCASE;\r
+        idx = _etoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags);\r
+        format++;\r
+        break;\r
+#endif  // PRINTF_SUPPORT_EXPONENTIAL\r
+#endif  // PRINTF_SUPPORT_FLOAT\r
+      case 'c' : {\r
+        unsigned int l = 1U;\r
+        // pre padding\r
+        if (!(flags & FLAGS_LEFT)) {\r
+          while (l++ < width) {\r
+            out(' ', buffer, idx++, maxlen);\r
+          }\r
+        }\r
+        // char output\r
+        out((char)va_arg(va, int), buffer, idx++, maxlen);\r
+        // post padding\r
+        if (flags & FLAGS_LEFT) {\r
+          while (l++ < width) {\r
+            out(' ', buffer, idx++, maxlen);\r
+          }\r
+        }\r
+        format++;\r
+        break;\r
+      }\r
+\r
+      case 's' : {\r
+        const char* p = va_arg(va, char*);\r
+        unsigned int l = _strnlen_s(p, precision ? precision : (size_t)-1);\r
+        // pre padding\r
+        if (flags & FLAGS_PRECISION) {\r
+          l = (l < precision ? l : precision);\r
+        }\r
+        if (!(flags & FLAGS_LEFT)) {\r
+          while (l++ < width) {\r
+            out(' ', buffer, idx++, maxlen);\r
+          }\r
+        }\r
+        // string output\r
+        while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) {\r
+          out(*(p++), buffer, idx++, maxlen);\r
+        }\r
+        // post padding\r
+        if (flags & FLAGS_LEFT) {\r
+          while (l++ < width) {\r
+            out(' ', buffer, idx++, maxlen);\r
+          }\r
+        }\r
+        format++;\r
+        break;\r
+      }\r
+\r
+      case 'p' : {\r
+        width = sizeof(void*) * 2U;\r
+        flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE;\r
+#if defined(PRINTF_SUPPORT_LONG_LONG)\r
+        const bool is_ll = sizeof(uintptr_t) == sizeof(long long);\r
+        if (is_ll) {\r
+          idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va_arg(va, void*), false, 16U, precision, width, flags);\r
+        }\r
+        else {\r
+#endif\r
+          idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va_arg(va, void*)), false, 16U, precision, width, flags);\r
+#if defined(PRINTF_SUPPORT_LONG_LONG)\r
+        }\r
+#endif\r
+        format++;\r
+        break;\r
+      }\r
+\r
+      case '%' :\r
+        out('%', buffer, idx++, maxlen);\r
+        format++;\r
+        break;\r
+\r
+      default :\r
+        out(*format, buffer, idx++, maxlen);\r
+        format++;\r
+        break;\r
+    }\r
+  }\r
+\r
+  // termination\r
+  out((char)0, buffer, idx < maxlen ? idx : maxlen - 1U, maxlen);\r
+\r
+  // return written chars without terminating \0\r
+  return (int)idx;\r
+}\r
+\r
+\r
+///////////////////////////////////////////////////////////////////////////////\r
+\r
+int printf_(const char* format, ...)\r
+{\r
+  va_list va;\r
+  va_start(va, format);\r
+  char buffer[1];\r
+  const int ret = _vsnprintf(_out_char, buffer, (size_t)-1, format, va);\r
+  va_end(va);\r
+  return ret;\r
+}\r
+\r
+\r
+int sprintf_(char* buffer, const char* format, ...)\r
+{\r
+  va_list va;\r
+  va_start(va, format);\r
+  const int ret = _vsnprintf(_out_buffer, buffer, (size_t)-1, format, va);\r
+  va_end(va);\r
+  return ret;\r
+}\r
+\r
+\r
+int snprintf_(char* buffer, size_t count, const char* format, ...)\r
+{\r
+  va_list va;\r
+  va_start(va, format);\r
+  const int ret = _vsnprintf(_out_buffer, buffer, count, format, va);\r
+  va_end(va);\r
+  return ret;\r
+}\r
+\r
+\r
+int vprintf_(const char* format, va_list va)\r
+{\r
+  char buffer[1];\r
+  return _vsnprintf(_out_char, buffer, (size_t)-1, format, va);\r
+}\r
+\r
+\r
+int vsnprintf_(char* buffer, size_t count, const char* format, va_list va)\r
+{\r
+  return _vsnprintf(_out_buffer, buffer, count, format, va);\r
+}\r
+\r
+\r
+int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...)\r
+{\r
+  va_list va;\r
+  va_start(va, format);\r
+  const out_fct_wrap_type out_fct_wrap = { out, arg };\r
+  const int ret = _vsnprintf(_out_fct, (char*)(uintptr_t)&out_fct_wrap, (size_t)-1, format, va);\r
+  va_end(va);\r
+  return ret;\r
+}\r