1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
108
109
110
111
112
113
114
119
120
121
122
126
127
128
133
134
135
141
142
143
151
152
153
154
155
160
161
162
163
164
167
168
169
170
177
178
179
180
181
182
183
184
185
190
191
192
195
196
197
202
203
204
205
206
207
208
209
210
211
212
223
224
225
243
244
253
254
255
256
257
258
259
260
261
262
263
264
265
268
269
270
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
295
296
297
304
305
306
307
308
309
310
311
312
313
314
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
349
350
351
352
356
357
358
361
362
366
367
368
369
370
371
372
384
385
393
394
395
402
403
406
410
411
412
413
419
420
421
429
430
439
440
441
442
443
444
445
446
447
448
449
450
453
454
455
456
459
460
461
464
465
466
467
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
491
494
495
496
497
498
499
518
519
520
521
528
532
533
534
537
538
539
540
541
542
543
544
545
546
547
548
549
550
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
572
573
574
575
584
585
586
587
588
589
594
599
604
609
614
617
618
619
620
621
622
634
635
636
637
648
649
650
651
659
668
673
677
681
683
684
685
686
687
688
689
690
691
692
693
694
695
696
706
707
710
711
712
715
716
717
720
721
722
743
744
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
769
770
772
773
774
775
776
777
778
779
780
783
784
786
787
788
789
790
791
792
797
798
799
800
805
806
807
808
809
810
811
812
813
816
821
822
825
826
831
832
833
834
835
836
837
838
839
840
849
850
851
852
853
854
859
863
864
865
866
867
868
869
870
871
872
873
874
875
876
883
884
891
892
895
896
900
901
902
903
904
905
906
907
908
911
912
913
921
922
929
930
940
941
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "pico.h"
#include "pico/printf.h"
5 includes
#ifndef PICO_PRINTF_NTOA_BUFFER_SIZE
#define PICO_PRINTF_NTOA_BUFFER_SIZE 32U
#endif
#ifndef PICO_PRINTF_FTOA_BUFFER_SIZE
#define PICO_PRINTF_FTOA_BUFFER_SIZE 32U
#endif
#ifndef PICO_PRINTF_SUPPORT_FLOAT
#define PICO_PRINTF_SUPPORT_FLOAT 1
#endif
#ifndef PICO_PRINTF_SUPPORT_EXPONENTIAL
#define PICO_PRINTF_SUPPORT_EXPONENTIAL 1
#endif
#ifndef PICO_PRINTF_DEFAULT_FLOAT_PRECISION
#define PICO_PRINTF_DEFAULT_FLOAT_PRECISION 6U
#endif
#ifndef PICO_PRINTF_MAX_FLOAT
#define PICO_PRINTF_MAX_FLOAT 1e9
#endif
#ifndef PICO_PRINTF_SUPPORT_LONG_LONG
#define PICO_PRINTF_SUPPORT_LONG_LONG 1
#endif
#ifndef PICO_PRINTF_SUPPORT_PTRDIFF_T
#define PICO_PRINTF_SUPPORT_PTRDIFF_T 1
#endif
#define FLAGS_ZEROPAD (1U << 0U)
#define FLAGS_LEFT (1U << 1U)
#define FLAGS_PLUS (1U << 2U)
#define FLAGS_SPACE (1U << 3U)
#define FLAGS_HASH (1U << 4U)
#define FLAGS_UPPERCASE (1U << 5U)
#define FLAGS_CHAR (1U << 6U)
#define FLAGS_SHORT (1U << 7U)
#define FLAGS_LONG (1U << 8U)
#define FLAGS_LONG_LONG (1U << 9U)
#define FLAGS_PRECISION (1U << 10U)
#define FLAGS_ADAPT_EXP (1U << 11U)
12 defines
#if PICO_PRINTF_SUPPORT_FLOAT
#include <float.h>
/* ... */
#endif
typedef void (*out_fct_type)(char character, void *buffer, size_t idx, size_t maxlen);
#if !PICO_PRINTF_ALWAYS_INCLUDED
static int (*lazy_vsnprintf)(out_fct_type out, char *buffer, const size_t maxlen, const char *format, va_list va);/* ... */
#endif
typedef struct {
void (*fct)(char character, void *arg);
void *arg;
...} out_fct_wrap_type;
static inline void _out_buffer(char character, void *buffer, size_t idx, size_t maxlen) {
if (idx < maxlen) {
((char *) buffer)[idx] = character;
}if (idx < maxlen) { ... }
}{ ... }
static inline void _out_null(char character, void *buffer, size_t idx, size_t maxlen) {
(void) character;
(void) buffer;
(void) idx;
(void) maxlen;
}{ ... }
static inline void _out_fct(char character, void *buffer, size_t idx, size_t maxlen) {
(void) idx;
(void) maxlen;
if (character) {
((out_fct_wrap_type *) buffer)->fct(character, ((out_fct_wrap_type *) buffer)->arg);
}if (character) { ... }
}{ ... }
static inline unsigned int _strnlen_s(const char *str, size_t maxsize) {
const char *s;
for (s = str; *s && maxsize--; ++s);
return (unsigned int) (s - str);
}{ ... }
static inline bool _is_digit(char ch) {
return (ch >= '0') && (ch <= '9');
}{ ... }
static unsigned int _atoi(const char **str) {
unsigned int i = 0U;
while (_is_digit(**str)) {
i = i * 10U + (unsigned int) (*((*str)++) - '0');
}while (_is_digit(**str)) { ... }
return i;
}{ ... }
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) {
const size_t start_idx = idx;
if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) {
for (size_t i = len; i < width; i++) {
out(' ', buffer, idx++, maxlen);
}for (size_t i = len; i < width; i++) { ... }
}if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) { ... }
while (len) {
out(buf[--len], buffer, idx++, maxlen);
}while (len) { ... }
if (flags & FLAGS_LEFT) {
while (idx - start_idx < width) {
out(' ', buffer, idx++, maxlen);
}while (idx - start_idx < width) { ... }
}if (flags & FLAGS_LEFT) { ... }
return idx;
}{ ... }
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) {
if (!(flags & FLAGS_LEFT)) {
if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
width--;
}if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) { ... }
while ((len < prec) && (len < PICO_PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = '0';
}while ((len < prec) && (len < PICO_PRINTF_NTOA_BUFFER_SIZE)) { ... }
while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PICO_PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = '0';
}while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PICO_PRINTF_NTOA_BUFFER_SIZE)) { ... }
}if (!(flags & FLAGS_LEFT)) { ... }
if (flags & FLAGS_HASH) {
if (!(flags & FLAGS_PRECISION) && len && ((len == prec) || (len == width))) {
len--;
if (len && (base == 16U)) {
len--;
}if (len && (base == 16U)) { ... }
}if (!(flags & FLAGS_PRECISION) && len && ((len == prec) || (len == width))) { ... }
if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PICO_PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = 'x';
}if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PICO_PRINTF_NTOA_BUFFER_SIZE)) { ... } else if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PICO_PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = 'X';
}else if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PICO_PRINTF_NTOA_BUFFER_SIZE)) { ... } else if ((base == 2U) && (len < PICO_PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = 'b';
}else if ((base == 2U) && (len < PICO_PRINTF_NTOA_BUFFER_SIZE)) { ... }
if (len < PICO_PRINTF_NTOA_BUFFER_SIZE) {
buf[len++] = '0';
}if (len < PICO_PRINTF_NTOA_BUFFER_SIZE) { ... }
}if (flags & FLAGS_HASH) { ... }
if (len < PICO_PRINTF_NTOA_BUFFER_SIZE) {
if (negative) {
buf[len++] = '-';
}if (negative) { ... } else if (flags & FLAGS_PLUS) {
buf[len++] = '+';
}else if (flags & FLAGS_PLUS) { ... } else if (flags & FLAGS_SPACE) {
buf[len++] = ' ';
}else if (flags & FLAGS_SPACE) { ... }
}if (len < PICO_PRINTF_NTOA_BUFFER_SIZE) { ... }
return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags);
}{ ... }
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) {
char buf[PICO_PRINTF_NTOA_BUFFER_SIZE];
size_t len = 0U;
if (!value) {
flags &= ~FLAGS_HASH;
}if (!value) { ... }
if (!(flags & FLAGS_PRECISION) || value) {
do {
const char digit = (char) (value % base);
buf[len++] = (char)(digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10);
value /= base;
...} while (value && (len < PICO_PRINTF_NTOA_BUFFER_SIZE));
}if (!(flags & FLAGS_PRECISION) || value) { ... }
return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int) base, prec, width, flags);
}{ ... }
#if PICO_PRINTF_SUPPORT_LONG_LONG
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) {
char buf[PICO_PRINTF_NTOA_BUFFER_SIZE];
size_t len = 0U;
if (!value) {
flags &= ~FLAGS_HASH;
}if (!value) { ... }
if (!(flags & FLAGS_PRECISION) || value) {
do {
const char digit = (char) (value % base);
buf[len++] = (char)(digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10);
value /= base;
...} while (value && (len < PICO_PRINTF_NTOA_BUFFER_SIZE));
}if (!(flags & FLAGS_PRECISION) || value) { ... }
return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int) base, prec, width, flags);
}{ ... }
/* ... */#endif
#if PICO_PRINTF_SUPPORT_FLOAT
#if PICO_PRINTF_SUPPORT_EXPONENTIAL
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);/* ... */
#endif
#define is_nan __builtin_isnan
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) {
char buf[PICO_PRINTF_FTOA_BUFFER_SIZE];
size_t len = 0U;
double diff = 0.0;
static const double pow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
if (is_nan(value))
return _out_rev(out, buffer, idx, maxlen, "nan", 3, width, flags);
if (value < -DBL_MAX)
return _out_rev(out, buffer, idx, maxlen, "fni-", 4, width, flags);
if (value > DBL_MAX)
return _out_rev(out, buffer, idx, maxlen, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4U : 3U,
width, flags);
if ((value > PICO_PRINTF_MAX_FLOAT) || (value < -PICO_PRINTF_MAX_FLOAT)) {
#if PICO_PRINTF_SUPPORT_EXPONENTIAL
return _etoa(out, buffer, idx, maxlen, value, prec, width, flags);
#else
return 0U;
#endif
}if ((value > PICO_PRINTF_MAX_FLOAT) || (value < -PICO_PRINTF_MAX_FLOAT)) { ... }
bool negative = false;
if (value < 0) {
negative = true;
value = 0 - value;
}if (value < 0) { ... }
if (!(flags & FLAGS_PRECISION)) {
prec = PICO_PRINTF_DEFAULT_FLOAT_PRECISION;
}if (!(flags & FLAGS_PRECISION)) { ... }
while ((len < PICO_PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) {
buf[len++] = '0';
prec--;
}while ((len < PICO_PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) { ... }
int whole = (int) value;
double tmp = (value - whole) * pow10[prec];
unsigned long frac = (unsigned long) tmp;
diff = tmp - frac;
if (diff > 0.5) {
++frac;
if (frac >= pow10[prec]) {
frac = 0;
++whole;
}if (frac >= pow10[prec]) { ... }
}if (diff > 0.5) { ... } else if (diff < 0.5) {
}else if (diff < 0.5) { ... } else if ((frac == 0U) || (frac & 1U)) {
++frac;
}else if ((frac == 0U) || (frac & 1U)) { ... }
if (prec == 0U) {
diff = value - (double) whole;
if (!((diff < 0.5) || (diff > 0.5)) && (whole & 1)) {
++whole;
}if (!((diff < 0.5) || (diff > 0.5)) && (whole & 1)) { ... }
}if (prec == 0U) { ... } else {
unsigned int count = prec;
while (len < PICO_PRINTF_FTOA_BUFFER_SIZE) {
--count;
buf[len++] = (char) (48U + (frac % 10U));
if (!(frac /= 10U)) {
break;
}if (!(frac /= 10U)) { ... }
}while (len < PICO_PRINTF_FTOA_BUFFER_SIZE) { ... }
while ((len < PICO_PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) {
buf[len++] = '0';
}while ((len < PICO_PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) { ... }
if (len < PICO_PRINTF_FTOA_BUFFER_SIZE) {
buf[len++] = '.';
}if (len < PICO_PRINTF_FTOA_BUFFER_SIZE) { ... }
}else { ... }
while (len < PICO_PRINTF_FTOA_BUFFER_SIZE) {
buf[len++] = (char) (48 + (whole % 10));
if (!(whole /= 10)) {
break;
}if (!(whole /= 10)) { ... }
}while (len < PICO_PRINTF_FTOA_BUFFER_SIZE) { ... }
if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) {
if (width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
width--;
}if (width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) { ... }
while ((len < width) && (len < PICO_PRINTF_FTOA_BUFFER_SIZE)) {
buf[len++] = '0';
}while ((len < width) && (len < PICO_PRINTF_FTOA_BUFFER_SIZE)) { ... }
}if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) { ... }
if (len < PICO_PRINTF_FTOA_BUFFER_SIZE) {
if (negative) {
buf[len++] = '-';
}if (negative) { ... } else if (flags & FLAGS_PLUS) {
buf[len++] = '+';
}else if (flags & FLAGS_PLUS) { ... } else if (flags & FLAGS_SPACE) {
buf[len++] = ' ';
}else if (flags & FLAGS_SPACE) { ... }
}if (len < PICO_PRINTF_FTOA_BUFFER_SIZE) { ... }
return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags);
}{ ... }
#if PICO_PRINTF_SUPPORT_EXPONENTIAL
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) {
if (is_nan(value) || (value > DBL_MAX) || (value < -DBL_MAX)) {
return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags);
}if (is_nan(value) || (value > DBL_MAX) || (value < -DBL_MAX)) { ... }
const bool negative = value < 0;
if (negative) {
value = -value;
}if (negative) { ... }
if (!(flags & FLAGS_PRECISION)) {
prec = PICO_PRINTF_DEFAULT_FLOAT_PRECISION;
}if (!(flags & FLAGS_PRECISION)) { ... }
union {
uint64_t U;
double F;
...} conv;
conv.F = value;
int expval;
if (conv.U) {
int exp2 = (int) ((conv.U >> 52U) & 0x07FFU) - 1023;
conv.U = (conv.U & ((1ULL << 52U) - 1U)) | (1023ULL << 52U);
expval = (int) (0.1760912590558 + exp2 * 0.301029995663981 + (conv.F - 1.5) * 0.289529654602168);
exp2 = (int) (expval * 3.321928094887362 + 0.5);
const double z = expval * 2.302585092994046 - exp2 * 0.6931471805599453;
const double z2 = z * z;
conv.U = (uint64_t) (exp2 + 1023) << 52U;
conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14)))));
if (value < conv.F) {
expval--;
conv.F /= 10;
}if (value < conv.F) { ... }
}if (conv.U) { ... } else {
expval = 0;
}else { ... }
unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U;
if (flags & FLAGS_ADAPT_EXP) {
if ((conv.U == 0) || ((value >= 1e-4) && (value < 1e6))) {
if ((int) prec > expval) {
prec = (unsigned) ((int) prec - expval - 1);
}if ((int) prec > expval) { ... } else {
prec = 0;
}else { ... }
flags |= FLAGS_PRECISION;
minwidth = 0U;
expval = 0;
}if ((conv.U == 0) || ((value >= 1e-4) && (value < 1e6))) { ... } else {
if ((prec > 0) && (flags & FLAGS_PRECISION)) {
--prec;
}if ((prec > 0) && (flags & FLAGS_PRECISION)) { ... }
}else { ... }
}if (flags & FLAGS_ADAPT_EXP) { ... }
unsigned int fwidth = width;
if (width > minwidth) {
fwidth -= minwidth;
}if (width > minwidth) { ... } else {
fwidth = 0U;
}else { ... }
if ((flags & FLAGS_LEFT) && minwidth) {
fwidth = 0U;
}if ((flags & FLAGS_LEFT) && minwidth) { ... }
if (expval) {
value /= conv.F;
}if (expval) { ... }
const size_t start_idx = idx;
idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP);
if (minwidth) {
out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen);
idx = _ntoa_long(out, buffer, idx, maxlen, (uint)((expval < 0) ? -expval : expval), expval < 0, 10, 0, minwidth - 1,
FLAGS_ZEROPAD | FLAGS_PLUS);
if (flags & FLAGS_LEFT) {
while (idx - start_idx < width) out(' ', buffer, idx++, maxlen);
}if (flags & FLAGS_LEFT) { ... }
}if (minwidth) { ... }
return idx;
}{ ... }
/* ... */#endif /* ... */
#endif
static int _vsnprintf(out_fct_type out, char *buffer, const size_t maxlen, const char *format, va_list va) {
#if !PICO_PRINTF_ALWAYS_INCLUDED
lazy_vsnprintf = _vsnprintf;
#endif
unsigned int flags, width, precision, n;
size_t idx = 0U;
if (!buffer) {
out = _out_null;
}if (!buffer) { ... }
while (*format) {
if (*format != '%') {
out(*format, buffer, idx++, maxlen);
format++;
continue;
}if (*format != '%') { ... } else {
format++;
}else { ... }
flags = 0U;
do {
switch (*format) {
case '0':
flags |= FLAGS_ZEROPAD;
format++;
n = 1U;
break;case '0':
case '-':
flags |= FLAGS_LEFT;
format++;
n = 1U;
break;case '-':
case '+':
flags |= FLAGS_PLUS;
format++;
n = 1U;
break;case '+':
case ' ':
flags |= FLAGS_SPACE;
format++;
n = 1U;
break;case ' ':
case '#':
flags |= FLAGS_HASH;
format++;
n = 1U;
break;case '#':
default :
n = 0U;
break;default
}switch (*format) { ... }
...} while (n);
width = 0U;
if (_is_digit(*format)) {
width = _atoi(&format);
}if (_is_digit(*format)) { ... } else if (*format == '*') {
const int w = va_arg(va, int);
if (w < 0) {
flags |= FLAGS_LEFT;
width = (unsigned int) -w;
}if (w < 0) { ... } else {
width = (unsigned int) w;
}else { ... }
format++;
}else if (*format == '*') { ... }
precision = 0U;
if (*format == '.') {
flags |= FLAGS_PRECISION;
format++;
if (_is_digit(*format)) {
precision = _atoi(&format);
}if (_is_digit(*format)) { ... } else if (*format == '*') {
const int prec = (int) va_arg(va, int);
precision = prec > 0 ? (unsigned int) prec : 0U;
format++;
}else if (*format == '*') { ... }
}if (*format == '.') { ... }
switch (*format) {
case 'l' :
flags |= FLAGS_LONG;
format++;
if (*format == 'l') {
flags |= FLAGS_LONG_LONG;
format++;
}if (*format == 'l') { ... }
break;case 'l' :
case 'h' :
flags |= FLAGS_SHORT;
format++;
if (*format == 'h') {
flags |= FLAGS_CHAR;
format++;
}if (*format == 'h') { ... }
break;
#if PICO_PRINTF_SUPPORT_PTRDIFF_Tcase 'h' :
case 't' :
flags |= (sizeof(ptrdiff_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
format++;
break;/* ... */
#endifcase 't' :
case 'j' :
flags |= (sizeof(intmax_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
format++;
break;case 'j' :
case 'z' :
flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
format++;
break;case 'z' :
default :
break;default
}switch (*format) { ... }
switch (*format) {
case 'd' :
case 'i' :
case 'u' :
case 'x' :
case 'X' :
case 'o' :
case 'b' : {
unsigned int base;
if (*format == 'x' || *format == 'X') {
base = 16U;
}if (*format == 'x' || *format == 'X') { ... } else if (*format == 'o') {
base = 8U;
}else if (*format == 'o') { ... } else if (*format == 'b') {
base = 2U;
}else if (*format == 'b') { ... } else {
base = 10U;
flags &= ~FLAGS_HASH;
}else { ... }
if (*format == 'X') {
flags |= FLAGS_UPPERCASE;
}if (*format == 'X') { ... }
if ((*format != 'i') && (*format != 'd')) {
flags &= ~(FLAGS_PLUS | FLAGS_SPACE);
}if ((*format != 'i') && (*format != 'd')) { ... }
if (flags & FLAGS_PRECISION) {
flags &= ~FLAGS_ZEROPAD;
}if (flags & FLAGS_PRECISION) { ... }
if ((*format == 'i') || (*format == 'd')) {
if (flags & FLAGS_LONG_LONG) {
#if PICO_PRINTF_SUPPORT_LONG_LONG
const long long value = va_arg(va, long long);
idx = _ntoa_long_long(out, buffer, idx, maxlen,
(unsigned long long) (value > 0 ? value : 0 - value), value < 0, base,
precision, width, flags);/* ... */
#endif
}if (flags & FLAGS_LONG_LONG) { ... } else if (flags & FLAGS_LONG) {
const long value = va_arg(va, long);
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long) (value > 0 ? value : 0 - value),
value < 0, base, precision, width, flags);
}else if (flags & FLAGS_LONG) { ... } else {
const int value = (flags & FLAGS_CHAR) ? (char) va_arg(va, int) : (flags & FLAGS_SHORT)
? (short int) va_arg(va, int)
: va_arg(va, int);
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int) (value > 0 ? value : 0 - value),
value < 0, base, precision, width, flags);
}else { ... }
}if ((*format == 'i') || (*format == 'd')) { ... } else {
if (flags & FLAGS_LONG_LONG) {
#if PICO_PRINTF_SUPPORT_LONG_LONG
idx = _ntoa_long_long(out, buffer, idx, maxlen, va_arg(va, unsigned long long), false, base,
precision, width, flags);/* ... */
#endif
}if (flags & FLAGS_LONG_LONG) { ... } else if (flags & FLAGS_LONG) {
idx = _ntoa_long(out, buffer, idx, maxlen, va_arg(va, unsigned long), false, base, precision,
width, flags);
}else if (flags & FLAGS_LONG) { ... } else {
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);
idx = _ntoa_long(out, buffer, idx, maxlen, value, false, base, precision, width, flags);
}else { ... }
}else { ... }
format++;
break;
...}case 'b' :
case 'f' :
case 'F' :
#if PICO_PRINTF_SUPPORT_FLOAT
if (*format == 'F') flags |= FLAGS_UPPERCASE;
idx = _ftoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags);/* ... */
#else
for(int i=0;i<2;i++) out('?', buffer, idx++, maxlen);
va_arg(va, double);/* ... */
#endif
format++;
break;case 'F' :
case 'e':
case 'E':
case 'g':
case 'G':
#if PICO_PRINTF_SUPPORT_FLOAT && PICO_PRINTF_SUPPORT_EXPONENTIAL
if ((*format == 'g') || (*format == 'G')) flags |= FLAGS_ADAPT_EXP;
if ((*format == 'E') || (*format == 'G')) flags |= FLAGS_UPPERCASE;
idx = _etoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags);/* ... */
#else
for(int i=0;i<2;i++) out('?', buffer, idx++, maxlen);
va_arg(va, double);/* ... */
#endif
format++;
break;case 'G':
case 'c' : {
unsigned int l = 1U;
if (!(flags & FLAGS_LEFT)) {
while (l++ < width) {
out(' ', buffer, idx++, maxlen);
}while (l++ < width) { ... }
}if (!(flags & FLAGS_LEFT)) { ... }
out((char) va_arg(va, int), buffer, idx++, maxlen);
if (flags & FLAGS_LEFT) {
while (l++ < width) {
out(' ', buffer, idx++, maxlen);
}while (l++ < width) { ... }
}if (flags & FLAGS_LEFT) { ... }
format++;
break;
...}
case 'c' :
case 's' : {
const char *p = va_arg(va, char*);
unsigned int l = _strnlen_s(p, precision ? precision : (size_t) -1);
if (flags & FLAGS_PRECISION) {
l = (l < precision ? l : precision);
}if (flags & FLAGS_PRECISION) { ... }
if (!(flags & FLAGS_LEFT)) {
while (l++ < width) {
out(' ', buffer, idx++, maxlen);
}while (l++ < width) { ... }
}if (!(flags & FLAGS_LEFT)) { ... }
while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) {
out(*(p++), buffer, idx++, maxlen);
}while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) { ... }
if (flags & FLAGS_LEFT) {
while (l++ < width) {
out(' ', buffer, idx++, maxlen);
}while (l++ < width) { ... }
}if (flags & FLAGS_LEFT) { ... }
format++;
break;
...}
case 's' :
case 'p' : {
width = sizeof(void *) * 2U;
flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE;
#if PICO_PRINTF_SUPPORT_LONG_LONG
const bool is_ll = sizeof(uintptr_t) == sizeof(long long);
if (is_ll) {
idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t) va_arg(va, void*), false, 16U,
precision, width, flags);
}if (is_ll) { ... } else {
#endif
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long) ((uintptr_t) va_arg(va, void*)), false,
16U, precision, width, flags);
#if PICO_PRINTF_SUPPORT_LONG_LONG
}else { ... }
#endif
format++;
break;
...}
case 'p' :
case '%' :
out('%', buffer, idx++, maxlen);
format++;
break;
case '%' :
default :
out(*format, buffer, idx++, maxlen);
format++;
break;default
}switch (*format) { ... }
}while (*format) { ... }
out((char) 0, buffer, idx < maxlen ? idx : maxlen - 1U, maxlen);
return (int) idx;
}{ ... }
int WRAPPER_FUNC(sprintf)(char *buffer, const char *format, ...) {
va_list va;
va_start(va, format);
const int ret = _vsnprintf(_out_buffer, buffer, (size_t) -1, format, va);
va_end(va);
return ret;
...}
int WRAPPER_FUNC(snprintf)(char *buffer, size_t count, const char *format, ...) {
va_list va;
va_start(va, format);
const int ret = _vsnprintf(_out_buffer, buffer, count, format, va);
va_end(va);
return ret;
...}
int WRAPPER_FUNC(vsnprintf)(char *buffer, size_t count, const char *format, va_list va) {
return _vsnprintf(_out_buffer, buffer, count, format, va);
...}
int vfctprintf(void (*out)(char character, void *arg), void *arg, const char *format, va_list va) {
const out_fct_wrap_type out_fct_wrap = {out, arg};
return _vsnprintf(_out_fct, (char *) (uintptr_t) &out_fct_wrap, (size_t) -1, format, va);
}{ ... }
#if LIB_PICO_PRINTF_PICO
#if !PICO_PRINTF_ALWAYS_INCLUDED
/* ... */
static void _putchar(char character) {
putchar(character);
}_putchar (char character) { ... }
static inline void _out_char(char character, void *buffer, size_t idx, size_t maxlen) {
(void) buffer;
(void) idx;
(void) maxlen;
if (character) {
_putchar(character);
}if (character) { ... }
}_out_char (char character, void *buffer, size_t idx, size_t maxlen) { ... }
bool weak_raw_printf(const char *fmt, ...) {
va_list va;
va_start(va, fmt);
bool rc = weak_raw_vprintf(fmt, va);
va_end(va);
return rc;
}weak_raw_printf (const char *fmt, ...) { ... }
bool weak_raw_vprintf(const char *fmt, va_list args) {
if (lazy_vsnprintf) {
char buffer[1];
lazy_vsnprintf(_out_char, buffer, (size_t) -1, fmt, args);
return true;
}if (lazy_vsnprintf) { ... } else {
puts(fmt);
return false;
}else { ... }
}weak_raw_vprintf (const char *fmt, va_list args) { ... }
/* ... */#endif/* ... */
#endif