acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

lv_printf.c (30252B)

      1 ///////////////////////////////////////////////////////////////////////////////
      2 // \author (c) Marco Paland (info@paland.com)
      3 //             2014-2019, PALANDesign Hannover, Germany
      4 //
      5 // \license The MIT License (MIT)
      6 //
      7 // Permission is hereby granted, free of charge, to any person obtaining a copy
      8 // of this software and associated documentation files (the "Software"), to deal
      9 // in the Software without restriction, including without limitation the rights
     10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     11 // copies of the Software, and to permit persons to whom the Software is
     12 // furnished to do so, subject to the following conditions:
     13 //
     14 // The above copyright notice and this permission notice shall be included in
     15 // all copies or substantial portions of the Software.
     16 //
     17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     23 // THE SOFTWARE.
     24 //
     25 // \brief Tiny printf, sprintf and (v)snprintf implementation, optimized for speed on
     26 //        embedded systems with a very limited resources. These routines are thread
     27 //        safe and reentrant!
     28 //        Use this instead of the bloated standard/newlib printf cause these use
     29 //        malloc for printf (and may not be thread safe).
     30 //
     31 ///////////////////////////////////////////////////////////////////////////////
     32 
     33 /*Original repository: https://github.com/mpaland/printf*/
     34 
     35 #include "lv_printf.h"
     36 
     37 #if LV_SPRINTF_CUSTOM == 0
     38 
     39 #include <stdbool.h>
     40 
     41 #define PRINTF_DISABLE_SUPPORT_FLOAT    (!LV_SPRINTF_USE_FLOAT)
     42 
     43 // 'ntoa' conversion buffer size, this must be big enough to hold one converted
     44 // numeric number including padded zeros (dynamically created on stack)
     45 // default: 32 byte
     46 #ifndef PRINTF_NTOA_BUFFER_SIZE
     47     #define PRINTF_NTOA_BUFFER_SIZE    32U
     48 #endif
     49 
     50 // 'ftoa' conversion buffer size, this must be big enough to hold one converted
     51 // float number including padded zeros (dynamically created on stack)
     52 // default: 32 byte
     53 #ifndef PRINTF_FTOA_BUFFER_SIZE
     54     #define PRINTF_FTOA_BUFFER_SIZE    32U
     55 #endif
     56 
     57 // support for the floating point type (%f)
     58 // default: activated
     59 #if !PRINTF_DISABLE_SUPPORT_FLOAT
     60     #define PRINTF_SUPPORT_FLOAT
     61 #endif
     62 
     63 // support for exponential floating point notation (%e/%g)
     64 // default: activated
     65 #ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
     66     #define PRINTF_SUPPORT_EXPONENTIAL
     67 #endif
     68 
     69 // define the default floating point precision
     70 // default: 6 digits
     71 #ifndef PRINTF_DEFAULT_FLOAT_PRECISION
     72     #define PRINTF_DEFAULT_FLOAT_PRECISION 6U
     73 #endif
     74 
     75 // define the largest float suitable to print with %f
     76 // default: 1e9
     77 #ifndef PRINTF_MAX_FLOAT
     78     #define PRINTF_MAX_FLOAT 1e9
     79 #endif
     80 
     81 // support for the long long types (%llu or %p)
     82 // default: activated
     83 #ifndef PRINTF_DISABLE_SUPPORT_LONG_LONG
     84     #define PRINTF_SUPPORT_LONG_LONG
     85 #endif
     86 
     87 // support for the ptrdiff_t type (%t)
     88 // ptrdiff_t is normally defined in <stddef.h> as long or long long type
     89 // default: activated
     90 #ifndef PRINTF_DISABLE_SUPPORT_PTRDIFF_T
     91     #define PRINTF_SUPPORT_PTRDIFF_T
     92 #endif
     93 
     94 ///////////////////////////////////////////////////////////////////////////////
     95 
     96 // internal flag definitions
     97 #define FLAGS_ZEROPAD   (1U <<  0U)
     98 #define FLAGS_LEFT      (1U <<  1U)
     99 #define FLAGS_PLUS      (1U <<  2U)
    100 #define FLAGS_SPACE     (1U <<  3U)
    101 #define FLAGS_HASH      (1U <<  4U)
    102 #define FLAGS_UPPERCASE (1U <<  5U)
    103 #define FLAGS_CHAR      (1U <<  6U)
    104 #define FLAGS_SHORT     (1U <<  7U)
    105 #define FLAGS_LONG      (1U <<  8U)
    106 #define FLAGS_LONG_LONG (1U <<  9U)
    107 #define FLAGS_PRECISION (1U << 10U)
    108 #define FLAGS_ADAPT_EXP (1U << 11U)
    109 
    110 // import float.h for DBL_MAX
    111 #if defined(PRINTF_SUPPORT_FLOAT)
    112     #include <float.h>
    113 #endif
    114 
    115 // output function type
    116 typedef void (*out_fct_type)(char character, void * buffer, size_t idx, size_t maxlen);
    117 
    118 // wrapper (used as buffer) for output function type
    119 typedef struct {
    120     void (*fct)(char character, void * arg);
    121     void * arg;
    122 } out_fct_wrap_type;
    123 
    124 // internal buffer output
    125 static inline void _out_buffer(char character, void * buffer, size_t idx, size_t maxlen)
    126 {
    127     if(idx < maxlen) {
    128         ((char *)buffer)[idx] = character;
    129     }
    130 }
    131 
    132 // internal null output
    133 static inline void _out_null(char character, void * buffer, size_t idx, size_t maxlen)
    134 {
    135     LV_UNUSED(character);
    136     LV_UNUSED(buffer);
    137     LV_UNUSED(idx);
    138     LV_UNUSED(maxlen);
    139 }
    140 
    141 // internal secure strlen
    142 // \return The length of the string (excluding the terminating 0) limited by 'maxsize'
    143 static inline unsigned int _strnlen_s(const char * str, size_t maxsize)
    144 {
    145     const char * s;
    146     for(s = str; *s && maxsize--; ++s);
    147     return (unsigned int)(s - str);
    148 }
    149 
    150 // internal test if char is a digit (0-9)
    151 // \return true if char is a digit
    152 static inline bool _is_digit(char ch)
    153 {
    154     return (ch >= '0') && (ch <= '9');
    155 }
    156 
    157 // internal ASCII string to unsigned int conversion
    158 static unsigned int _atoi(const char ** str)
    159 {
    160     unsigned int i = 0U;
    161     while(_is_digit(**str)) {
    162         i = i * 10U + (unsigned int)(*((*str)++) - '0');
    163     }
    164     return i;
    165 }
    166 
    167 // output the specified string in reverse, taking care of any zero-padding
    168 static size_t _out_rev(out_fct_type out, char * buffer, size_t idx, size_t maxlen, const char * buf, size_t len,
    169                        unsigned int width, unsigned int flags)
    170 {
    171     const size_t start_idx = idx;
    172 
    173     // pad spaces up to given width
    174     if(!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) {
    175         size_t i;
    176         for(i = len; i < width; i++) {
    177             out(' ', buffer, idx++, maxlen);
    178         }
    179     }
    180 
    181     // reverse string
    182     while(len) {
    183         out(buf[--len], buffer, idx++, maxlen);
    184     }
    185 
    186     // append pad spaces up to given width
    187     if(flags & FLAGS_LEFT) {
    188         while(idx - start_idx < width) {
    189             out(' ', buffer, idx++, maxlen);
    190         }
    191     }
    192 
    193     return idx;
    194 }
    195 
    196 // internal itoa format
    197 static size_t _ntoa_format(out_fct_type out, char * buffer, size_t idx, size_t maxlen, char * buf, size_t len,
    198                            bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags)
    199 {
    200     // pad leading zeros
    201     if(!(flags & FLAGS_LEFT)) {
    202         if(width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
    203             width--;
    204         }
    205         while((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
    206             buf[len++] = '0';
    207         }
    208         while((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
    209             buf[len++] = '0';
    210         }
    211     }
    212 
    213     // handle hash
    214     if(flags & FLAGS_HASH) {
    215         if(!(flags & FLAGS_PRECISION) && len && ((len == prec) || (len == width))) {
    216             len--;
    217             if(len && (base == 16U)) {
    218                 len--;
    219             }
    220         }
    221         if((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
    222             buf[len++] = 'x';
    223         }
    224         else if((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
    225             buf[len++] = 'X';
    226         }
    227         else if((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
    228             buf[len++] = 'b';
    229         }
    230         if(len < PRINTF_NTOA_BUFFER_SIZE) {
    231             buf[len++] = '0';
    232         }
    233     }
    234 
    235     if(len < PRINTF_NTOA_BUFFER_SIZE) {
    236         if(negative) {
    237             buf[len++] = '-';
    238         }
    239         else if(flags & FLAGS_PLUS) {
    240             buf[len++] = '+';  // ignore the space if the '+' exists
    241         }
    242         else if(flags & FLAGS_SPACE) {
    243             buf[len++] = ' ';
    244         }
    245     }
    246 
    247     return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags);
    248 }
    249 
    250 // internal itoa for 'long' type
    251 static size_t _ntoa_long(out_fct_type out, char * buffer, size_t idx, size_t maxlen, unsigned long value, bool negative,
    252                          unsigned long base, unsigned int prec, unsigned int width, unsigned int flags)
    253 {
    254     char buf[PRINTF_NTOA_BUFFER_SIZE];
    255     size_t len = 0U;
    256 
    257     // no hash for 0 values
    258     if(!value) {
    259         flags &= ~FLAGS_HASH;
    260     }
    261 
    262     // write if precision != 0 and value is != 0
    263     if(!(flags & FLAGS_PRECISION) || value) {
    264         do {
    265             const char digit = (char)(value % base);
    266             buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10;
    267             value /= base;
    268         } while(value && (len < PRINTF_NTOA_BUFFER_SIZE));
    269     }
    270 
    271     return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags);
    272 }
    273 
    274 // internal itoa for 'long long' type
    275 #if defined(PRINTF_SUPPORT_LONG_LONG)
    276 static size_t _ntoa_long_long(out_fct_type out, char * buffer, size_t idx, size_t maxlen, unsigned long long value,
    277                               bool negative, unsigned long long base, unsigned int prec, unsigned int width, unsigned int flags)
    278 {
    279     char buf[PRINTF_NTOA_BUFFER_SIZE];
    280     size_t len = 0U;
    281 
    282     // no hash for 0 values
    283     if(!value) {
    284         flags &= ~FLAGS_HASH;
    285     }
    286 
    287     // write if precision != 0 and value is != 0
    288     if(!(flags & FLAGS_PRECISION) || value) {
    289         do {
    290             const char digit = (char)(value % base);
    291             buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10;
    292             value /= base;
    293         } while(value && (len < PRINTF_NTOA_BUFFER_SIZE));
    294     }
    295 
    296     return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags);
    297 }
    298 #endif  // PRINTF_SUPPORT_LONG_LONG
    299 
    300 #if defined(PRINTF_SUPPORT_FLOAT)
    301 
    302 #if defined(PRINTF_SUPPORT_EXPONENTIAL)
    303 // forward declaration so that _ftoa can switch to exp notation for values > PRINTF_MAX_FLOAT
    304 static size_t _etoa(out_fct_type out, char * buffer, size_t idx, size_t maxlen, double value, unsigned int prec,
    305                     unsigned int width, unsigned int flags);
    306 #endif
    307 
    308 // internal ftoa for fixed decimal floating point
    309 static size_t _ftoa(out_fct_type out, char * buffer, size_t idx, size_t maxlen, double value, unsigned int prec,
    310                     unsigned int width, unsigned int flags)
    311 {
    312     char buf[PRINTF_FTOA_BUFFER_SIZE];
    313     size_t len  = 0U;
    314     double diff = 0.0;
    315 
    316     // powers of 10
    317     static const double pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
    318 
    319     // test for special values
    320     if(value != value)
    321         return _out_rev(out, buffer, idx, maxlen, "nan", 3, width, flags);
    322     if(value < -DBL_MAX)
    323         return _out_rev(out, buffer, idx, maxlen, "fni-", 4, width, flags);
    324     if(value > DBL_MAX)
    325         return _out_rev(out, buffer, idx, maxlen, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4U : 3U, width,
    326                         flags);
    327 
    328     // test for very large values
    329     // standard printf behavior is to print EVERY whole number digit -- which could be 100s of characters overflowing your buffers == bad
    330     if((value > PRINTF_MAX_FLOAT) || (value < -PRINTF_MAX_FLOAT)) {
    331 #if defined(PRINTF_SUPPORT_EXPONENTIAL)
    332         return _etoa(out, buffer, idx, maxlen, value, prec, width, flags);
    333 #else
    334         return 0U;
    335 #endif
    336     }
    337 
    338     // test for negative
    339     bool negative = false;
    340     if(value < 0) {
    341         negative = true;
    342         value = 0 - value;
    343     }
    344 
    345     // set default precision, if not set explicitly
    346     if(!(flags & FLAGS_PRECISION)) {
    347         prec = PRINTF_DEFAULT_FLOAT_PRECISION;
    348     }
    349     // limit precision to 9, cause a prec >= 10 can lead to overflow errors
    350     while((len < PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) {
    351         buf[len++] = '0';
    352         prec--;
    353     }
    354 
    355     int whole = (int)value;
    356     double tmp = (value - whole) * pow10[prec];
    357     unsigned long frac = (unsigned long)tmp;
    358     diff = tmp - frac;
    359 
    360     if(diff > 0.5) {
    361         ++frac;
    362         // handle rollover, e.g. case 0.99 with prec 1 is 1.0
    363         if(frac >= pow10[prec]) {
    364             frac = 0;
    365             ++whole;
    366         }
    367     }
    368     else if(diff < 0.5) {
    369     }
    370     else if((frac == 0U) || (frac & 1U)) {
    371         // if halfway, round up if odd OR if last digit is 0
    372         ++frac;
    373     }
    374 
    375     if(prec == 0U) {
    376         diff = value - (double)whole;
    377         if((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) {
    378             // exactly 0.5 and ODD, then round up
    379             // 1.5 -> 2, but 2.5 -> 2
    380             ++whole;
    381         }
    382     }
    383     else {
    384         unsigned int count = prec;
    385         // now do fractional part, as an unsigned number
    386         while(len < PRINTF_FTOA_BUFFER_SIZE) {
    387             --count;
    388             buf[len++] = (char)(48U + (frac % 10U));
    389             if(!(frac /= 10U)) {
    390                 break;
    391             }
    392         }
    393         // add extra 0s
    394         while((len < PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) {
    395             buf[len++] = '0';
    396         }
    397         if(len < PRINTF_FTOA_BUFFER_SIZE) {
    398             // add decimal
    399             buf[len++] = '.';
    400         }
    401     }
    402 
    403     // do whole part, number is reversed
    404     while(len < PRINTF_FTOA_BUFFER_SIZE) {
    405         buf[len++] = (char)(48 + (whole % 10));
    406         if(!(whole /= 10)) {
    407             break;
    408         }
    409     }
    410 
    411     // pad leading zeros
    412     if(!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) {
    413         if(width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
    414             width--;
    415         }
    416         while((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) {
    417             buf[len++] = '0';
    418         }
    419     }
    420 
    421     if(len < PRINTF_FTOA_BUFFER_SIZE) {
    422         if(negative) {
    423             buf[len++] = '-';
    424         }
    425         else if(flags & FLAGS_PLUS) {
    426             buf[len++] = '+';  // ignore the space if the '+' exists
    427         }
    428         else if(flags & FLAGS_SPACE) {
    429             buf[len++] = ' ';
    430         }
    431     }
    432 
    433     return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags);
    434 }
    435 
    436 #if defined(PRINTF_SUPPORT_EXPONENTIAL)
    437 // internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse <m.jasperse@gmail.com>
    438 static size_t _etoa(out_fct_type out, char * buffer, size_t idx, size_t maxlen, double value, unsigned int prec,
    439                     unsigned int width, unsigned int flags)
    440 {
    441     // check for NaN and special values
    442     if((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) {
    443         return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags);
    444     }
    445 
    446     // determine the sign
    447     const bool negative = value < 0;
    448     if(negative) {
    449         value = -value;
    450     }
    451 
    452     // default precision
    453     if(!(flags & FLAGS_PRECISION)) {
    454         prec = PRINTF_DEFAULT_FLOAT_PRECISION;
    455     }
    456 
    457     // determine the decimal exponent
    458     // based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c)
    459     union {
    460         uint64_t U;
    461         double   F;
    462     } conv;
    463 
    464     conv.F = value;
    465     int exp2 = (int)((conv.U >> 52U) & 0x07FFU) - 1023;           // effectively log2
    466     conv.U = (conv.U & ((1ULL << 52U) - 1U)) | (1023ULL << 52U);  // drop the exponent so conv.F is now in [1,2)
    467     // now approximate log10 from the log2 integer part and an expansion of ln around 1.5
    468     int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981 + (conv.F - 1.5) * 0.289529654602168);
    469     // now we want to compute 10^expval but we want to be sure it won't overflow
    470     exp2 = (int)(expval * 3.321928094887362 + 0.5);
    471     const double z  = expval * 2.302585092994046 - exp2 * 0.6931471805599453;
    472     const double z2 = z * z;
    473     conv.U = (uint64_t)(exp2 + 1023) << 52U;
    474     // compute exp(z) using continued fractions, see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex
    475     conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14)))));
    476     // correct for rounding errors
    477     if(value < conv.F) {
    478         expval--;
    479         conv.F /= 10;
    480     }
    481 
    482     // the exponent format is "%+03d" and largest value is "307", so set aside 4-5 characters
    483     unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U;
    484 
    485     // in "%g" mode, "prec" is the number of *significant figures* not decimals
    486     if(flags & FLAGS_ADAPT_EXP) {
    487         // do we want to fall-back to "%f" mode?
    488         if((value >= 1e-4) && (value < 1e6)) {
    489             if((int)prec > expval) {
    490                 prec = (unsigned)((int)prec - expval - 1);
    491             }
    492             else {
    493                 prec = 0;
    494             }
    495             flags |= FLAGS_PRECISION;   // make sure _ftoa respects precision
    496             // no characters in exponent
    497             minwidth = 0U;
    498             expval   = 0;
    499         }
    500         else {
    501             // we use one sigfig for the whole part
    502             if((prec > 0) && (flags & FLAGS_PRECISION)) {
    503                 --prec;
    504             }
    505         }
    506     }
    507 
    508     // will everything fit?
    509     unsigned int fwidth = width;
    510     if(width > minwidth) {
    511         // we didn't fall-back so subtract the characters required for the exponent
    512         fwidth -= minwidth;
    513     }
    514     else {
    515         // not enough characters, so go back to default sizing
    516         fwidth = 0U;
    517     }
    518     if((flags & FLAGS_LEFT) && minwidth) {
    519         // if we're padding on the right, DON'T pad the floating part
    520         fwidth = 0U;
    521     }
    522 
    523     // rescale the float value
    524     if(expval) {
    525         value /= conv.F;
    526     }
    527 
    528     // output the floating part
    529     const size_t start_idx = idx;
    530     idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP);
    531 
    532     // output the exponent part
    533     if(minwidth) {
    534         // output the exponential symbol
    535         out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen);
    536         // output the exponent value
    537         idx = _ntoa_long(out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth - 1,
    538                          FLAGS_ZEROPAD | FLAGS_PLUS);
    539         // might need to right-pad spaces
    540         if(flags & FLAGS_LEFT) {
    541             while(idx - start_idx < width) out(' ', buffer, idx++, maxlen);
    542         }
    543     }
    544     return idx;
    545 }
    546 #endif  // PRINTF_SUPPORT_EXPONENTIAL
    547 #endif  // PRINTF_SUPPORT_FLOAT
    548 
    549 // internal vsnprintf
    550 static int _vsnprintf(out_fct_type out, char * buffer, const size_t maxlen, const char * format, va_list va)
    551 {
    552     unsigned int flags, width, precision, n;
    553     size_t idx = 0U;
    554 
    555     if(!buffer) {
    556         // use null output function
    557         out = _out_null;
    558     }
    559 
    560     while(*format) {
    561         // format specifier?  %[flags][width][.precision][length]
    562         if(*format != '%') {
    563             // no
    564             out(*format, buffer, idx++, maxlen);
    565             format++;
    566             continue;
    567         }
    568         else {
    569             // yes, evaluate it
    570             format++;
    571         }
    572 
    573         // evaluate flags
    574         flags = 0U;
    575         do {
    576             switch(*format) {
    577                 case '0':
    578                     flags |= FLAGS_ZEROPAD;
    579                     format++;
    580                     n = 1U;
    581                     break;
    582                 case '-':
    583                     flags |= FLAGS_LEFT;
    584                     format++;
    585                     n = 1U;
    586                     break;
    587                 case '+':
    588                     flags |= FLAGS_PLUS;
    589                     format++;
    590                     n = 1U;
    591                     break;
    592                 case ' ':
    593                     flags |= FLAGS_SPACE;
    594                     format++;
    595                     n = 1U;
    596                     break;
    597                 case '#':
    598                     flags |= FLAGS_HASH;
    599                     format++;
    600                     n = 1U;
    601                     break;
    602                 default :
    603                     n = 0U;
    604                     break;
    605             }
    606         } while(n);
    607 
    608         // evaluate width field
    609         width = 0U;
    610         if(_is_digit(*format)) {
    611             width = _atoi(&format);
    612         }
    613         else if(*format == '*') {
    614             const int w = va_arg(va, int);
    615             if(w < 0) {
    616                 flags |= FLAGS_LEFT;    // reverse padding
    617                 width = (unsigned int) - w;
    618             }
    619             else {
    620                 width = (unsigned int)w;
    621             }
    622             format++;
    623         }
    624 
    625         // evaluate precision field
    626         precision = 0U;
    627         if(*format == '.') {
    628             flags |= FLAGS_PRECISION;
    629             format++;
    630             if(_is_digit(*format)) {
    631                 precision = _atoi(&format);
    632             }
    633             else if(*format == '*') {
    634                 const int prec = (int)va_arg(va, int);
    635                 precision = prec > 0 ? (unsigned int)prec : 0U;
    636                 format++;
    637             }
    638         }
    639 
    640         // evaluate length field
    641         switch(*format) {
    642             case 'l' :
    643                 flags |= FLAGS_LONG;
    644                 format++;
    645                 if(*format == 'l') {
    646                     flags |= FLAGS_LONG_LONG;
    647                     format++;
    648                 }
    649                 break;
    650             case 'h' :
    651                 flags |= FLAGS_SHORT;
    652                 format++;
    653                 if(*format == 'h') {
    654                     flags |= FLAGS_CHAR;
    655                     format++;
    656                 }
    657                 break;
    658 #if defined(PRINTF_SUPPORT_PTRDIFF_T)
    659             case 't' :
    660                 flags |= (sizeof(ptrdiff_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
    661                 format++;
    662                 break;
    663 #endif
    664             case 'j' :
    665                 flags |= (sizeof(intmax_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
    666                 format++;
    667                 break;
    668             case 'z' :
    669                 flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
    670                 format++;
    671                 break;
    672             default :
    673                 break;
    674         }
    675 
    676         // evaluate specifier
    677         switch(*format) {
    678             case 'd' :
    679             case 'i' :
    680             case 'u' :
    681             case 'x' :
    682             case 'X' :
    683             case 'p' :
    684             case 'P' :
    685             case 'o' :
    686             case 'b' : {
    687                     // set the base
    688                     unsigned int base;
    689                     if(*format == 'x' || *format == 'X') {
    690                         base = 16U;
    691                     }
    692                     else if(*format == 'p' || *format == 'P') {
    693                         base = 16U;
    694                         flags |= FLAGS_HASH;   // always hash for pointer format
    695 #if defined(PRINTF_SUPPORT_LONG_LONG)
    696                         if(sizeof(uintptr_t) == sizeof(long long))
    697                             flags |= FLAGS_LONG_LONG;
    698                         else
    699 #endif
    700                             flags |= FLAGS_LONG;
    701 
    702                         if(*(format + 1) == 'V')
    703                             format++;
    704                     }
    705                     else if(*format == 'o') {
    706                         base =  8U;
    707                     }
    708                     else if(*format == 'b') {
    709                         base =  2U;
    710                     }
    711                     else {
    712                         base = 10U;
    713                         flags &= ~FLAGS_HASH;   // no hash for dec format
    714                     }
    715                     // uppercase
    716                     if(*format == 'X' || *format == 'P') {
    717                         flags |= FLAGS_UPPERCASE;
    718                     }
    719 
    720                     // no plus or space flag for u, x, X, o, b
    721                     if((*format != 'i') && (*format != 'd')) {
    722                         flags &= ~(FLAGS_PLUS | FLAGS_SPACE);
    723                     }
    724 
    725                     // ignore '0' flag when precision is given
    726                     if(flags & FLAGS_PRECISION) {
    727                         flags &= ~FLAGS_ZEROPAD;
    728                     }
    729 
    730                     // convert the integer
    731                     if((*format == 'i') || (*format == 'd')) {
    732                         // signed
    733                         if(flags & FLAGS_LONG_LONG) {
    734 #if defined(PRINTF_SUPPORT_LONG_LONG)
    735                             const long long value = va_arg(va, long long);
    736                             idx = _ntoa_long_long(out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base,
    737                                                   precision, width, flags);
    738 #endif
    739                         }
    740                         else if(flags & FLAGS_LONG) {
    741                             const long value = va_arg(va, long);
    742                             idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision,
    743                                              width, flags);
    744                         }
    745                         else {
    746                             const int value = (flags & FLAGS_CHAR) ? (char)va_arg(va, int) : (flags & FLAGS_SHORT) ? (short int)va_arg(va,
    747                                                                                                                                        int) : va_arg(va, int);
    748                             idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, precision,
    749                                              width, flags);
    750                         }
    751                     }
    752                     else if(*format == 'V') {
    753                         lv_vaformat_t * vaf = va_arg(va, lv_vaformat_t *);
    754                         va_list copy;
    755 
    756                         va_copy(copy, *vaf->va);
    757                         idx += _vsnprintf(out, buffer + idx, maxlen - idx, vaf->fmt, copy);
    758                         va_end(copy);
    759                     }
    760                     else {
    761                         // unsigned
    762                         if(flags & FLAGS_LONG_LONG) {
    763 #if defined(PRINTF_SUPPORT_LONG_LONG)
    764                             idx = _ntoa_long_long(out, buffer, idx, maxlen, va_arg(va, unsigned long long), false, base, precision, width, flags);
    765 #endif
    766                         }
    767                         else if(flags & FLAGS_LONG) {
    768                             idx = _ntoa_long(out, buffer, idx, maxlen, va_arg(va, unsigned long), false, base, precision, width, flags);
    769                         }
    770                         else {
    771                             const unsigned int value = (flags & FLAGS_CHAR) ? (unsigned char)va_arg(va,
    772                                                                                                     unsigned int) : (flags & FLAGS_SHORT) ? (unsigned short int)va_arg(va, unsigned int) : va_arg(va, unsigned int);
    773                             idx = _ntoa_long(out, buffer, idx, maxlen, value, false, base, precision, width, flags);
    774                         }
    775                     }
    776                     format++;
    777                     break;
    778                 }
    779 #if defined(PRINTF_SUPPORT_FLOAT)
    780             case 'f' :
    781             case 'F' :
    782                 if(*format == 'F') flags |= FLAGS_UPPERCASE;
    783                 idx = _ftoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags);
    784                 format++;
    785                 break;
    786 #if defined(PRINTF_SUPPORT_EXPONENTIAL)
    787             case 'e':
    788             case 'E':
    789             case 'g':
    790             case 'G':
    791                 if((*format == 'g') || (*format == 'G')) flags |= FLAGS_ADAPT_EXP;
    792                 if((*format == 'E') || (*format == 'G')) flags |= FLAGS_UPPERCASE;
    793                 idx = _etoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags);
    794                 format++;
    795                 break;
    796 #endif  // PRINTF_SUPPORT_EXPONENTIAL
    797 #endif  // PRINTF_SUPPORT_FLOAT
    798             case 'c' : {
    799                     unsigned int l = 1U;
    800                     // pre padding
    801                     if(!(flags & FLAGS_LEFT)) {
    802                         while(l++ < width) {
    803                             out(' ', buffer, idx++, maxlen);
    804                         }
    805                     }
    806                     // char output
    807                     out((char)va_arg(va, int), buffer, idx++, maxlen);
    808                     // post padding
    809                     if(flags & FLAGS_LEFT) {
    810                         while(l++ < width) {
    811                             out(' ', buffer, idx++, maxlen);
    812                         }
    813                     }
    814                     format++;
    815                     break;
    816                 }
    817 
    818             case 's' : {
    819                     const char * p = va_arg(va, char *);
    820                     unsigned int l = _strnlen_s(p, precision ? precision : (size_t) -1);
    821                     // pre padding
    822                     if(flags & FLAGS_PRECISION) {
    823                         l = (l < precision ? l : precision);
    824                     }
    825                     if(!(flags & FLAGS_LEFT)) {
    826                         while(l++ < width) {
    827                             out(' ', buffer, idx++, maxlen);
    828                         }
    829                     }
    830                     // string output
    831                     while((*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) {
    832                         out(*(p++), buffer, idx++, maxlen);
    833                     }
    834                     // post padding
    835                     if(flags & FLAGS_LEFT) {
    836                         while(l++ < width) {
    837                             out(' ', buffer, idx++, maxlen);
    838                         }
    839                     }
    840                     format++;
    841                     break;
    842                 }
    843 
    844             case '%' :
    845                 out('%', buffer, idx++, maxlen);
    846                 format++;
    847                 break;
    848 
    849             default :
    850                 out(*format, buffer, idx++, maxlen);
    851                 format++;
    852                 break;
    853         }
    854     }
    855 
    856     // termination
    857     out((char)0, buffer, idx < maxlen ? idx : maxlen - 1U, maxlen);
    858 
    859     // return written chars without terminating \0
    860     return (int)idx;
    861 }
    862 
    863 ///////////////////////////////////////////////////////////////////////////////
    864 
    865 int lv_snprintf(char * buffer, size_t count, const char * format, ...)
    866 {
    867     va_list va;
    868     va_start(va, format);
    869     const int ret = _vsnprintf(_out_buffer, buffer, count, format, va);
    870     va_end(va);
    871     return ret;
    872 }
    873 
    874 int lv_vsnprintf(char * buffer, size_t count, const char * format, va_list va)
    875 {
    876     return _vsnprintf(_out_buffer, buffer, count, format, va);
    877 }
    878 
    879 #endif /*LV_SPRINTF_CUSTOM*/