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