-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
nanoprintf.h
1140 lines (1001 loc) · 37.9 KB
/
nanoprintf.h
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
34
35
36
37
38
39
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* nanoprintf v0.5.3: a tiny embeddable printf replacement written in C.
https://github.com/charlesnicholson/nanoprintf
dual-licensed under 0bsd and unlicense, take your pick. see eof for details. */
#ifndef NANOPRINTF_H_INCLUDED
#define NANOPRINTF_H_INCLUDED
#include <stdarg.h>
#include <stddef.h>
// Define this to fully sandbox nanoprintf inside of a translation unit.
#ifdef NANOPRINTF_VISIBILITY_STATIC
#define NPF_VISIBILITY static
#else
#define NPF_VISIBILITY extern
#endif
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
#define NPF_PRINTF_ATTR(FORMAT_INDEX, VARGS_INDEX) \
__attribute__((format(printf, FORMAT_INDEX, VARGS_INDEX)))
#else
#define NPF_PRINTF_ATTR(FORMAT_INDEX, VARGS_INDEX)
#endif
// Public API
#ifdef __cplusplus
extern "C" {
#endif
// The npf_ functions all return the number of bytes required to express the
// fully-formatted string, not including the null terminator character.
// The npf_ functions do not return negative values, since the lack of 'l' length
// modifier support makes encoding errors impossible.
NPF_VISIBILITY int npf_snprintf(
char *buffer, size_t bufsz, const char *format, ...) NPF_PRINTF_ATTR(3, 4);
NPF_VISIBILITY int npf_vsnprintf(
char *buffer, size_t bufsz, char const *format, va_list vlist) NPF_PRINTF_ATTR(3, 0);
typedef void (*npf_putc)(int c, void *ctx);
NPF_VISIBILITY int npf_pprintf(
npf_putc pc, void *pc_ctx, char const *format, ...) NPF_PRINTF_ATTR(3, 4);
NPF_VISIBILITY int npf_vpprintf(
npf_putc pc, void *pc_ctx, char const *format, va_list vlist) NPF_PRINTF_ATTR(3, 0);
#ifdef __cplusplus
}
#endif
#endif // NANOPRINTF_H_INCLUDED
/* The implementation of nanoprintf begins here, to be compiled only if
NANOPRINTF_IMPLEMENTATION is defined. In a multi-file library what follows would
be nanoprintf.c. */
#ifdef NANOPRINTF_IMPLEMENTATION
#ifndef NANOPRINTF_IMPLEMENTATION_INCLUDED
#define NANOPRINTF_IMPLEMENTATION_INCLUDED
#include <limits.h>
#include <stdint.h>
// The conversion buffer must fit at least UINT64_MAX in octal format with the leading '0'.
#ifndef NANOPRINTF_CONVERSION_BUFFER_SIZE
#define NANOPRINTF_CONVERSION_BUFFER_SIZE 23
#endif
#if NANOPRINTF_CONVERSION_BUFFER_SIZE < 23
#error The size of the conversion buffer must be at least 23 bytes.
#endif
// Pick reasonable defaults if nothing's been configured.
#if !defined(NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS) && \
!defined(NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS) && \
!defined(NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS) && \
!defined(NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS) && \
!defined(NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS) && \
!defined(NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS)
#define NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS 0
#define NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS 0
#define NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS 0
#endif
// If anything's been configured, everything must be configured.
#ifndef NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS
#error NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS must be #defined to 0 or 1
#endif
#ifndef NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS
#error NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS must be #defined to 0 or 1
#endif
#ifndef NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS
#error NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS must be #defined to 0 or 1
#endif
#ifndef NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS
#error NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS must be #defined to 0 or 1
#endif
#ifndef NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS
#error NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS must be #defined to 0 or 1
#endif
#ifndef NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS
#error NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS must be #defined to 0 or 1
#endif
// Ensure flags are compatible.
#if (NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1) && \
(NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 0)
#error Precision format specifiers must be enabled if float support is enabled.
#endif
// intmax_t / uintmax_t require stdint from c99 / c++11
#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1
#ifndef _MSC_VER
#ifdef __cplusplus
#if __cplusplus < 201103L
#error large format specifier support requires C++11 or later.
#endif
#else
#if __STDC_VERSION__ < 199409L
#error nanoprintf requires C99 or later.
#endif
#endif
#endif
#endif
// Figure out if we can disable warnings with pragmas.
#ifdef __clang__
#define NANOPRINTF_CLANG 1
#define NANOPRINTF_GCC_PAST_4_6 0
#else
#define NANOPRINTF_CLANG 0
#if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 6)))
#define NANOPRINTF_GCC_PAST_4_6 1
#else
#define NANOPRINTF_GCC_PAST_4_6 0
#endif
#endif
#if NANOPRINTF_CLANG || NANOPRINTF_GCC_PAST_4_6
#define NANOPRINTF_HAVE_GCC_WARNING_PRAGMAS 1
#else
#define NANOPRINTF_HAVE_GCC_WARNING_PRAGMAS 0
#endif
#if NANOPRINTF_HAVE_GCC_WARNING_PRAGMAS
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#ifdef __cplusplus
#pragma GCC diagnostic ignored "-Wold-style-cast"
#endif
#pragma GCC diagnostic ignored "-Wpadded"
#pragma GCC diagnostic ignored "-Wfloat-equal"
#if NANOPRINTF_CLANG
#pragma GCC diagnostic ignored "-Wc++98-compat-pedantic"
#pragma GCC diagnostic ignored "-Wcovered-switch-default"
#pragma GCC diagnostic ignored "-Wdeclaration-after-statement"
#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
#ifndef __APPLE__
#pragma GCC diagnostic ignored "-Wunsafe-buffer-usage"
#endif
#elif NANOPRINTF_GCC_PAST_4_6
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
#endif
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4619) // there is no warning number 'number'
// C4619 has to be disabled first!
#pragma warning(disable:4127) // conditional expression is constant
#pragma warning(disable:4505) // unreferenced local function has been removed
#pragma warning(disable:4514) // unreferenced inline function has been removed
#pragma warning(disable:4701) // potentially uninitialized local variable used
#pragma warning(disable:4706) // assignment within conditional expression
#pragma warning(disable:4710) // function not inlined
#pragma warning(disable:4711) // function selected for inline expansion
#pragma warning(disable:4820) // padding added after struct member
#pragma warning(disable:5039) // potentially throwing function passed to extern C function
#pragma warning(disable:5045) // compiler will insert Spectre mitigation for memory load
#pragma warning(disable:5262) // implicit switch fall-through
#pragma warning(disable:26812) // enum type is unscoped
#endif
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
#define NPF_NOINLINE __attribute__((noinline))
#elif defined(_MSC_VER)
#define NPF_NOINLINE __declspec(noinline)
#else
#define NPF_NOINLINE
#endif
#if (NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1) || \
(NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1)
enum {
NPF_FMT_SPEC_OPT_NONE,
NPF_FMT_SPEC_OPT_LITERAL,
NPF_FMT_SPEC_OPT_STAR,
};
#endif
enum {
NPF_FMT_SPEC_LEN_MOD_NONE,
NPF_FMT_SPEC_LEN_MOD_SHORT, // 'h'
NPF_FMT_SPEC_LEN_MOD_LONG_DOUBLE, // 'L'
NPF_FMT_SPEC_LEN_MOD_CHAR, // 'hh'
NPF_FMT_SPEC_LEN_MOD_LONG, // 'l'
#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1
NPF_FMT_SPEC_LEN_MOD_LARGE_LONG_LONG, // 'll'
NPF_FMT_SPEC_LEN_MOD_LARGE_INTMAX, // 'j'
NPF_FMT_SPEC_LEN_MOD_LARGE_SIZET, // 'z'
NPF_FMT_SPEC_LEN_MOD_LARGE_PTRDIFFT, // 't'
#endif
};
enum {
NPF_FMT_SPEC_CONV_NONE,
NPF_FMT_SPEC_CONV_PERCENT, // '%'
NPF_FMT_SPEC_CONV_CHAR, // 'c'
NPF_FMT_SPEC_CONV_STRING, // 's'
NPF_FMT_SPEC_CONV_SIGNED_INT, // 'i', 'd'
#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1
NPF_FMT_SPEC_CONV_BINARY, // 'b'
#endif
NPF_FMT_SPEC_CONV_OCTAL, // 'o'
NPF_FMT_SPEC_CONV_HEX_INT, // 'x', 'X'
NPF_FMT_SPEC_CONV_UNSIGNED_INT, // 'u'
NPF_FMT_SPEC_CONV_POINTER, // 'p'
#if NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS == 1
NPF_FMT_SPEC_CONV_WRITEBACK, // 'n'
#endif
#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1
NPF_FMT_SPEC_CONV_FLOAT_DEC, // 'f', 'F'
NPF_FMT_SPEC_CONV_FLOAT_SCI, // 'e', 'E'
NPF_FMT_SPEC_CONV_FLOAT_SHORTEST, // 'g', 'G'
NPF_FMT_SPEC_CONV_FLOAT_HEX, // 'a', 'A'
#endif
};
typedef struct npf_format_spec {
#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1
int field_width;
uint8_t field_width_opt;
char left_justified; // '-'
char leading_zero_pad; // '0'
#endif
#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1
int prec;
uint8_t prec_opt;
#endif
char prepend; // ' ' or '+'
char alt_form; // '#'
char case_adjust; // 'a' - 'A'
uint8_t length_modifier;
uint8_t conv_spec;
} npf_format_spec_t;
#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 0
typedef long npf_int_t;
typedef unsigned long npf_uint_t;
#else
typedef intmax_t npf_int_t;
typedef uintmax_t npf_uint_t;
#endif
typedef struct npf_bufputc_ctx {
char *dst;
size_t len;
size_t cur;
} npf_bufputc_ctx_t;
#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1
typedef char npf_size_is_ptrdiff[(sizeof(size_t) == sizeof(ptrdiff_t)) ? 1 : -1];
typedef ptrdiff_t npf_ssize_t;
#endif
#ifdef _MSC_VER
#include <intrin.h>
#endif
static int npf_max(int x, int y) { return (x > y) ? x : y; }
static int npf_parse_format_spec(char const *format, npf_format_spec_t *out_spec) {
char const *cur = format;
#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1
out_spec->left_justified = 0;
out_spec->leading_zero_pad = 0;
#endif
out_spec->case_adjust = 'a' - 'A'; // lowercase
out_spec->prepend = 0;
out_spec->alt_form = 0;
while (*++cur) { // cur points at the leading '%' character
switch (*cur) { // Optional flags
#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1
case '-': out_spec->left_justified = '-'; out_spec->leading_zero_pad = 0; continue;
case '0': out_spec->leading_zero_pad = !out_spec->left_justified; continue;
#endif
case '+': out_spec->prepend = '+'; continue;
case ' ': if (out_spec->prepend == 0) { out_spec->prepend = ' '; } continue;
case '#': out_spec->alt_form = '#'; continue;
default: break;
}
break;
}
#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1
out_spec->field_width_opt = NPF_FMT_SPEC_OPT_NONE;
if (*cur == '*') {
out_spec->field_width_opt = NPF_FMT_SPEC_OPT_STAR;
++cur;
} else {
out_spec->field_width = 0;
while ((*cur >= '0') && (*cur <= '9')) {
out_spec->field_width_opt = NPF_FMT_SPEC_OPT_LITERAL;
out_spec->field_width = (out_spec->field_width * 10) + (*cur++ - '0');
}
}
#endif
#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1
out_spec->prec = 0;
out_spec->prec_opt = NPF_FMT_SPEC_OPT_NONE;
if (*cur == '.') {
++cur;
if (*cur == '*') {
out_spec->prec_opt = NPF_FMT_SPEC_OPT_STAR;
++cur;
} else {
if (*cur == '-') {
++cur;
} else {
out_spec->prec_opt = NPF_FMT_SPEC_OPT_LITERAL;
}
while ((*cur >= '0') && (*cur <= '9')) {
out_spec->prec = (out_spec->prec * 10) + (*cur++ - '0');
}
}
}
#endif
uint_fast8_t tmp_conv = NPF_FMT_SPEC_CONV_NONE;
out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_NONE;
switch (*cur++) { // Length modifier
case 'h':
out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_SHORT;
if (*cur == 'h') {
out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_CHAR;
++cur;
}
break;
case 'l':
out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LONG;
#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1
if (*cur == 'l') {
out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LARGE_LONG_LONG;
++cur;
}
#endif
break;
#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1
case 'L': out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LONG_DOUBLE; break;
#endif
#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1
case 'j': out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LARGE_INTMAX; break;
case 'z': out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LARGE_SIZET; break;
case 't': out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LARGE_PTRDIFFT; break;
#endif
default: --cur; break;
}
switch (*cur++) { // Conversion specifier
case '%': out_spec->conv_spec = NPF_FMT_SPEC_CONV_PERCENT;
#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1
out_spec->prec_opt = NPF_FMT_SPEC_OPT_NONE;
#endif
break;
case 'c': out_spec->conv_spec = NPF_FMT_SPEC_CONV_CHAR;
#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1
out_spec->prec_opt = NPF_FMT_SPEC_OPT_NONE;
#endif
break;
case 's': out_spec->conv_spec = NPF_FMT_SPEC_CONV_STRING;
#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1
out_spec->leading_zero_pad = 0;
#endif
break;
case 'i':
case 'd': tmp_conv = NPF_FMT_SPEC_CONV_SIGNED_INT;
case 'o':
if (tmp_conv == NPF_FMT_SPEC_CONV_NONE) { tmp_conv = NPF_FMT_SPEC_CONV_OCTAL; }
case 'u':
if (tmp_conv == NPF_FMT_SPEC_CONV_NONE) { tmp_conv = NPF_FMT_SPEC_CONV_UNSIGNED_INT; }
case 'X':
if (tmp_conv == NPF_FMT_SPEC_CONV_NONE) { out_spec->case_adjust = 0; }
case 'x':
if (tmp_conv == NPF_FMT_SPEC_CONV_NONE) { tmp_conv = NPF_FMT_SPEC_CONV_HEX_INT; }
out_spec->conv_spec = (uint8_t)tmp_conv;
#if (NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1) && \
(NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1)
if (out_spec->prec_opt != NPF_FMT_SPEC_OPT_NONE) { out_spec->leading_zero_pad = 0; }
#endif
break;
#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1
case 'F': out_spec->case_adjust = 0;
case 'f':
out_spec->conv_spec = NPF_FMT_SPEC_CONV_FLOAT_DEC;
if (out_spec->prec_opt == NPF_FMT_SPEC_OPT_NONE) { out_spec->prec = 6; }
break;
case 'E': out_spec->case_adjust = 0;
case 'e':
out_spec->conv_spec = NPF_FMT_SPEC_CONV_FLOAT_SCI;
if (out_spec->prec_opt == NPF_FMT_SPEC_OPT_NONE) { out_spec->prec = 6; }
break;
case 'G': out_spec->case_adjust = 0;
case 'g':
out_spec->conv_spec = NPF_FMT_SPEC_CONV_FLOAT_SHORTEST;
if (out_spec->prec_opt == NPF_FMT_SPEC_OPT_NONE) { out_spec->prec = 6; }
break;
case 'A': out_spec->case_adjust = 0;
case 'a':
out_spec->conv_spec = NPF_FMT_SPEC_CONV_FLOAT_HEX;
if (out_spec->prec_opt == NPF_FMT_SPEC_OPT_NONE) { out_spec->prec = 6; }
break;
#endif
#if NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS == 1
case 'n':
// todo: reject string if flags or width or precision exist
out_spec->conv_spec = NPF_FMT_SPEC_CONV_WRITEBACK;
#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1
out_spec->prec_opt = NPF_FMT_SPEC_OPT_NONE;
#endif
break;
#endif
case 'p':
out_spec->conv_spec = NPF_FMT_SPEC_CONV_POINTER;
#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1
out_spec->prec_opt = NPF_FMT_SPEC_OPT_NONE;
#endif
break;
#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1
case 'B':
out_spec->case_adjust = 0;
case 'b':
out_spec->conv_spec = NPF_FMT_SPEC_CONV_BINARY;
break;
#endif
default: return 0;
}
return (int)(cur - format);
}
static NPF_NOINLINE int npf_utoa_rev(
npf_uint_t val, char *buf, uint_fast8_t base, char case_adj) {
uint_fast8_t n = 0;
do {
int_fast8_t const d = (int_fast8_t)(val % base);
*buf++ = (char)(((d < 10) ? '0' : ('A' - 10 + case_adj)) + d);
++n;
val /= base;
} while (val);
return (int)n;
}
#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1
#include <float.h>
#if (DBL_MANT_DIG <= 11) && (DBL_MAX_EXP <= 16)
typedef uint_fast16_t npf_double_bin_t;
typedef int_fast8_t npf_ftoa_exp_t;
#elif (DBL_MANT_DIG <= 24) && (DBL_MAX_EXP <= 128)
typedef uint_fast32_t npf_double_bin_t;
typedef int_fast8_t npf_ftoa_exp_t;
#elif (DBL_MANT_DIG <= 53) && (DBL_MAX_EXP <= 1024)
typedef uint_fast64_t npf_double_bin_t;
typedef int_fast16_t npf_ftoa_exp_t;
#else
#error Unsupported width of the double type.
#endif
// The floating point conversion code works with an unsigned integer type of any size.
#ifndef NANOPRINTF_CONVERSION_FLOAT_TYPE
#define NANOPRINTF_CONVERSION_FLOAT_TYPE unsigned int
#endif
typedef NANOPRINTF_CONVERSION_FLOAT_TYPE npf_ftoa_man_t;
#if (NANOPRINTF_CONVERSION_BUFFER_SIZE <= UINT_FAST8_MAX) && (UINT_FAST8_MAX <= INT_MAX)
typedef uint_fast8_t npf_ftoa_dec_t;
#else
typedef int npf_ftoa_dec_t;
#endif
enum {
NPF_DOUBLE_EXP_MASK = DBL_MAX_EXP * 2 - 1,
NPF_DOUBLE_EXP_BIAS = DBL_MAX_EXP - 1,
NPF_DOUBLE_MAN_BITS = DBL_MANT_DIG - 1,
NPF_DOUBLE_BIN_BITS = sizeof(npf_double_bin_t) * CHAR_BIT,
NPF_FTOA_MAN_BITS = sizeof(npf_ftoa_man_t) * CHAR_BIT,
NPF_FTOA_SHIFT_BITS =
((NPF_FTOA_MAN_BITS < DBL_MANT_DIG) ? NPF_FTOA_MAN_BITS : DBL_MANT_DIG) - 1
};
/* Generally, floating-point conversion implementations use
grisu2 (https://bit.ly/2JgMggX) and ryu (https://bit.ly/2RLXSg0) algorithms,
which are mathematically exact and fast, but require large lookup tables.
This implementation was inspired by Wojciech Muła's (zdję[email protected])
algorithm (http://0x80.pl/notesen/2015-12-29-float-to-string.html) and
extended further by adding dynamic scaling and configurable integer width by
Oskars Rubenis (https://github.com/Okarss). */
static int npf_ftoa_rev(char *buf, npf_format_spec_t const *spec, double f) {
char const *ret = NULL;
npf_double_bin_t bin; { // Union-cast is UB pre-C11, compiler optimizes byte-copy loop.
char const *src = (char const *)&f;
char *dst = (char *)&bin;
for (uint_fast8_t i = 0; i < sizeof(f); ++i) { dst[i] = src[i]; }
}
// Unsigned -> signed int casting is IB and can raise a signal but generally doesn't.
npf_ftoa_exp_t exp =
(npf_ftoa_exp_t)((npf_ftoa_exp_t)(bin >> NPF_DOUBLE_MAN_BITS) & NPF_DOUBLE_EXP_MASK);
bin &= ((npf_double_bin_t)0x1 << NPF_DOUBLE_MAN_BITS) - 1;
if (exp == (npf_ftoa_exp_t)NPF_DOUBLE_EXP_MASK) { // special value
ret = (bin) ? "NAN" : "FNI";
goto exit;
}
if (spec->prec > (NANOPRINTF_CONVERSION_BUFFER_SIZE - 2)) { goto exit; }
if (exp) { // normal number
bin |= (npf_double_bin_t)0x1 << NPF_DOUBLE_MAN_BITS;
} else { // subnormal number
++exp;
}
exp = (npf_ftoa_exp_t)(exp - NPF_DOUBLE_EXP_BIAS);
uint_fast8_t carry; carry = 0;
npf_ftoa_dec_t end, dec; dec = (npf_ftoa_dec_t)spec->prec;
if (dec || spec->alt_form) {
buf[dec++] = '.';
}
{ // Integer part
npf_ftoa_man_t man_i;
if (exp >= 0) {
int_fast8_t shift_i =
(int_fast8_t)((exp > NPF_FTOA_SHIFT_BITS) ? (int)NPF_FTOA_SHIFT_BITS : exp);
npf_ftoa_exp_t exp_i = (npf_ftoa_exp_t)(exp - shift_i);
shift_i = (int_fast8_t)(NPF_DOUBLE_MAN_BITS - shift_i);
man_i = (npf_ftoa_man_t)(bin >> shift_i);
if (exp_i) {
if (shift_i) {
carry = (bin >> (shift_i - 1)) & 0x1;
}
exp = NPF_DOUBLE_MAN_BITS; // invalidate the fraction part
}
// Scale the exponent from base-2 to base-10.
for (; exp_i; --exp_i) {
if (!(man_i & ((npf_ftoa_man_t)0x1 << (NPF_FTOA_MAN_BITS - 1)))) {
man_i = (npf_ftoa_man_t)(man_i << 1);
man_i = (npf_ftoa_man_t)(man_i | carry); carry = 0;
} else {
if (dec >= NANOPRINTF_CONVERSION_BUFFER_SIZE) { goto exit; }
buf[dec++] = '0';
carry = (((uint_fast8_t)(man_i % 5) + carry) > 2);
man_i /= 5;
}
}
} else {
man_i = 0;
}
end = dec;
do { // Print the integer
if (end >= NANOPRINTF_CONVERSION_BUFFER_SIZE) { goto exit; }
buf[end++] = (char)('0' + (char)(man_i % 10));
man_i /= 10;
} while (man_i);
}
{ // Fraction part
npf_ftoa_man_t man_f;
npf_ftoa_dec_t dec_f = (npf_ftoa_dec_t)spec->prec;
if (exp < NPF_DOUBLE_MAN_BITS) {
int_fast8_t shift_f = (int_fast8_t)((exp < 0) ? -1 : exp);
npf_ftoa_exp_t exp_f = (npf_ftoa_exp_t)(exp - shift_f);
npf_double_bin_t bin_f =
bin << ((NPF_DOUBLE_BIN_BITS - NPF_DOUBLE_MAN_BITS) + shift_f);
// This if-else statement can be completely optimized at compile time.
if (NPF_DOUBLE_BIN_BITS > NPF_FTOA_MAN_BITS) {
man_f = (npf_ftoa_man_t)(bin_f >> ((unsigned)(NPF_DOUBLE_BIN_BITS -
NPF_FTOA_MAN_BITS) %
NPF_DOUBLE_BIN_BITS));
carry = (uint_fast8_t)((bin_f >> ((unsigned)(NPF_DOUBLE_BIN_BITS -
NPF_FTOA_MAN_BITS - 1) %
NPF_DOUBLE_BIN_BITS)) & 0x1);
} else {
man_f = (npf_ftoa_man_t)((npf_ftoa_man_t)bin_f
<< ((unsigned)(NPF_FTOA_MAN_BITS -
NPF_DOUBLE_BIN_BITS) % NPF_FTOA_MAN_BITS));
carry = 0;
}
// Scale the exponent from base-2 to base-10 and prepare the first digit.
for (uint_fast8_t digit = 0; dec_f && (exp_f < 4); ++exp_f) {
if ((man_f > ((npf_ftoa_man_t)-4 / 5)) || digit) {
carry = (uint_fast8_t)(man_f & 0x1);
man_f = (npf_ftoa_man_t)(man_f >> 1);
} else {
man_f = (npf_ftoa_man_t)(man_f * 5);
if (carry) { man_f = (npf_ftoa_man_t)(man_f + 3); carry = 0; }
if (exp_f < 0) {
buf[--dec_f] = '0';
} else {
++digit;
}
}
}
man_f = (npf_ftoa_man_t)(man_f + carry);
carry = (exp_f >= 0);
dec = 0;
} else {
man_f = 0;
}
if (dec_f) {
// Print the fraction
for (;;) {
buf[--dec_f] = (char)('0' + (char)(man_f >> (NPF_FTOA_MAN_BITS - 4)));
man_f = (npf_ftoa_man_t)(man_f & ~((npf_ftoa_man_t)0xF << (NPF_FTOA_MAN_BITS - 4)));
if (!dec_f) { break; }
man_f = (npf_ftoa_man_t)(man_f * 10);
}
man_f = (npf_ftoa_man_t)(man_f << 4);
}
if (exp < NPF_DOUBLE_MAN_BITS) {
carry &= (uint_fast8_t)(man_f >> (NPF_FTOA_MAN_BITS - 1));
}
}
// Round the number
for (; carry; ++dec) {
if (dec >= NANOPRINTF_CONVERSION_BUFFER_SIZE) { goto exit; }
if (dec >= end) { buf[end++] = '0'; }
if (buf[dec] == '.') { continue; }
carry = (buf[dec] == '9');
buf[dec] = (char)(carry ? '0' : (buf[dec] + 1));
}
return (int)end;
exit:
if (!ret) { ret = "RRE"; }
uint_fast8_t i;
for (i = 0; ret[i]; ++i) { buf[i] = (char)(ret[i] + spec->case_adjust); }
return (int)i;
}
#endif // NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS
#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1
static int npf_bin_len(npf_uint_t u) {
// Return the length of the binary string format of 'u', preferring intrinsics.
if (!u) { return 1; }
#ifdef _MSC_VER // Win64, use _BSR64 for everything. If x86, use _BSR when non-large.
#ifdef _M_X64
#define NPF_HAVE_BUILTIN_CLZ
#define NPF_CLZ _BitScanReverse64
#elif NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 0
#define NPF_HAVE_BUILTIN_CLZ
#define NPF_CLZ _BitScanReverse
#endif
#ifdef NPF_HAVE_BUILTIN_CLZ
unsigned long idx;
NPF_CLZ(&idx, u);
return (int)(idx + 1);
#endif
#elif NANOPRINTF_CLANG || NANOPRINTF_GCC_PAST_4_6
#define NPF_HAVE_BUILTIN_CLZ
#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1
#define NPF_CLZ(X) ((sizeof(long long) * CHAR_BIT) - (size_t)__builtin_clzll(X))
#else
#define NPF_CLZ(X) ((sizeof(long) * CHAR_BIT) - (size_t)__builtin_clzl(X))
#endif
return (int)NPF_CLZ(u);
#endif
#ifndef NPF_HAVE_BUILTIN_CLZ
int n;
for (n = 0; u; ++n, u >>= 1); // slow but small software fallback
return n;
#else
#undef NPF_HAVE_BUILTIN_CLZ
#undef NPF_CLZ
#endif
}
#endif
static void npf_bufputc(int c, void *ctx) {
npf_bufputc_ctx_t *bpc = (npf_bufputc_ctx_t *)ctx;
if (bpc->cur < bpc->len) { bpc->dst[bpc->cur++] = (char)c; }
}
static void npf_bufputc_nop(int c, void *ctx) { (void)c; (void)ctx; }
typedef struct npf_cnt_putc_ctx {
npf_putc pc;
void *ctx;
int n;
} npf_cnt_putc_ctx_t;
static void npf_putc_cnt(int c, void *ctx) {
npf_cnt_putc_ctx_t *pc_cnt = (npf_cnt_putc_ctx_t *)ctx;
++pc_cnt->n;
pc_cnt->pc(c, pc_cnt->ctx); // sibling-call optimization
}
#define NPF_PUTC(VAL) do { npf_putc_cnt((int)(VAL), &pc_cnt); } while (0)
#define NPF_EXTRACT(MOD, CAST_TO, EXTRACT_AS) \
case NPF_FMT_SPEC_LEN_MOD_##MOD: val = (CAST_TO)va_arg(args, EXTRACT_AS); break
#define NPF_WRITEBACK(MOD, TYPE) \
case NPF_FMT_SPEC_LEN_MOD_##MOD: *(va_arg(args, TYPE *)) = (TYPE)pc_cnt.n; break
int npf_vpprintf(npf_putc pc, void *pc_ctx, char const *format, va_list args) {
npf_format_spec_t fs;
char const *cur = format;
npf_cnt_putc_ctx_t pc_cnt;
pc_cnt.pc = pc;
pc_cnt.ctx = pc_ctx;
pc_cnt.n = 0;
while (*cur) {
int const fs_len = (*cur != '%') ? 0 : npf_parse_format_spec(cur, &fs);
if (!fs_len) { NPF_PUTC(*cur++); continue; }
cur += fs_len;
// Extract star-args immediately
#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1
if (fs.field_width_opt == NPF_FMT_SPEC_OPT_STAR) {
fs.field_width = va_arg(args, int);
if (fs.field_width < 0) {
fs.field_width = -fs.field_width;
fs.left_justified = 1;
}
}
#endif
#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1
if (fs.prec_opt == NPF_FMT_SPEC_OPT_STAR) {
fs.prec = va_arg(args, int);
if (fs.prec < 0) { fs.prec_opt = NPF_FMT_SPEC_OPT_NONE; }
}
#endif
union { char cbuf_mem[NANOPRINTF_CONVERSION_BUFFER_SIZE]; npf_uint_t binval; } u;
char *cbuf = u.cbuf_mem, sign_c = 0;
int cbuf_len = 0, need_0x = 0;
#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1
int field_pad = 0;
char pad_c = 0;
#endif
#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1
int prec_pad = 0;
#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1
int zero = 0;
#endif
#endif
// Extract and convert the argument to string, point cbuf at the text.
switch (fs.conv_spec) {
case NPF_FMT_SPEC_CONV_PERCENT:
*cbuf = '%';
cbuf_len = 1;
break;
case NPF_FMT_SPEC_CONV_CHAR:
*cbuf = (char)va_arg(args, int);
cbuf_len = 1;
break;
case NPF_FMT_SPEC_CONV_STRING: {
cbuf = va_arg(args, char *);
#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1
for (char const *s = cbuf;
((fs.prec_opt == NPF_FMT_SPEC_OPT_NONE) || (cbuf_len < fs.prec)) && *s;
++s, ++cbuf_len);
#else
for (char const *s = cbuf; *s; ++s, ++cbuf_len); // strlen
#endif
} break;
case NPF_FMT_SPEC_CONV_SIGNED_INT: {
npf_int_t val = 0;
switch (fs.length_modifier) {
NPF_EXTRACT(NONE, int, int);
NPF_EXTRACT(SHORT, short, int);
NPF_EXTRACT(LONG_DOUBLE, int, int);
NPF_EXTRACT(CHAR, char, int);
NPF_EXTRACT(LONG, long, long);
#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1
NPF_EXTRACT(LARGE_LONG_LONG, long long, long long);
NPF_EXTRACT(LARGE_INTMAX, intmax_t, intmax_t);
NPF_EXTRACT(LARGE_SIZET, npf_ssize_t, npf_ssize_t);
NPF_EXTRACT(LARGE_PTRDIFFT, ptrdiff_t, ptrdiff_t);
#endif
default: break;
}
sign_c = (val < 0) ? '-' : fs.prepend;
#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1
#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1
zero = !val;
#endif
// special case, if prec and value are 0, skip
if (!val && (fs.prec_opt != NPF_FMT_SPEC_OPT_NONE) && !fs.prec) {
cbuf_len = 0;
} else
#endif
{
npf_uint_t uval = (npf_uint_t)val;
if (val < 0) { uval = 0 - uval; }
cbuf_len = npf_utoa_rev(uval, cbuf, 10, fs.case_adjust);
}
} break;
#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1
case NPF_FMT_SPEC_CONV_BINARY:
#endif
case NPF_FMT_SPEC_CONV_OCTAL:
case NPF_FMT_SPEC_CONV_HEX_INT:
case NPF_FMT_SPEC_CONV_UNSIGNED_INT: {
npf_uint_t val = 0;
switch (fs.length_modifier) {
NPF_EXTRACT(NONE, unsigned, unsigned);
NPF_EXTRACT(SHORT, unsigned short, unsigned);
NPF_EXTRACT(LONG_DOUBLE, unsigned, unsigned);
NPF_EXTRACT(CHAR, unsigned char, unsigned);
NPF_EXTRACT(LONG, unsigned long, unsigned long);
#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1
NPF_EXTRACT(LARGE_LONG_LONG, unsigned long long, unsigned long long);
NPF_EXTRACT(LARGE_INTMAX, uintmax_t, uintmax_t);
NPF_EXTRACT(LARGE_SIZET, size_t, size_t);
NPF_EXTRACT(LARGE_PTRDIFFT, size_t, size_t);
#endif
default: break;
}
#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1
#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1
zero = !val;
#endif
if (!val && (fs.prec_opt != NPF_FMT_SPEC_OPT_NONE) && !fs.prec) {
// Zero value and explicitly-requested zero precision means "print nothing".
if ((fs.conv_spec == NPF_FMT_SPEC_CONV_OCTAL) && fs.alt_form) {
fs.prec = 1; // octal special case, print a single '0'
}
} else
#endif
#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1
if (fs.conv_spec == NPF_FMT_SPEC_CONV_BINARY) {
cbuf_len = npf_bin_len(val); u.binval = val;
} else
#endif
{
uint_fast8_t const base = (fs.conv_spec == NPF_FMT_SPEC_CONV_OCTAL) ?
8u : ((fs.conv_spec == NPF_FMT_SPEC_CONV_HEX_INT) ? 16u : 10u);
cbuf_len = npf_utoa_rev(val, cbuf, base, fs.case_adjust);
}
if (val && fs.alt_form && (fs.conv_spec == NPF_FMT_SPEC_CONV_OCTAL)) {
cbuf[cbuf_len++] = '0'; // OK to add leading octal '0' immediately.
}
if (val && fs.alt_form) { // 0x or 0b but can't write it yet.
if (fs.conv_spec == NPF_FMT_SPEC_CONV_HEX_INT) { need_0x = 'X'; }
#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1
else if (fs.conv_spec == NPF_FMT_SPEC_CONV_BINARY) { need_0x = 'B'; }
#endif
if (need_0x) { need_0x += fs.case_adjust; }
}
} break;
case NPF_FMT_SPEC_CONV_POINTER: {
cbuf_len =
npf_utoa_rev((npf_uint_t)(uintptr_t)va_arg(args, void *), cbuf, 16, 'a' - 'A');
need_0x = 'x';
} break;
#if NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS == 1
case NPF_FMT_SPEC_CONV_WRITEBACK:
switch (fs.length_modifier) {
NPF_WRITEBACK(NONE, int);
NPF_WRITEBACK(SHORT, short);
NPF_WRITEBACK(LONG, long);
NPF_WRITEBACK(LONG_DOUBLE, double);
NPF_WRITEBACK(CHAR, signed char);
#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1
NPF_WRITEBACK(LARGE_LONG_LONG, long long);
NPF_WRITEBACK(LARGE_INTMAX, intmax_t);
NPF_WRITEBACK(LARGE_SIZET, size_t);
NPF_WRITEBACK(LARGE_PTRDIFFT, ptrdiff_t);
#endif
default: break;
} break;
#endif
#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1
case NPF_FMT_SPEC_CONV_FLOAT_DEC:
case NPF_FMT_SPEC_CONV_FLOAT_SCI:
case NPF_FMT_SPEC_CONV_FLOAT_SHORTEST:
case NPF_FMT_SPEC_CONV_FLOAT_HEX: {
double val;
if (fs.length_modifier == NPF_FMT_SPEC_LEN_MOD_LONG_DOUBLE) {
val = (double)va_arg(args, long double);
} else {
val = va_arg(args, double);
}
sign_c = (val < 0.) ? '-' : fs.prepend;
#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1
zero = (val == 0.);
#endif
cbuf_len = npf_ftoa_rev(cbuf, &fs, val);
} break;
#endif
default: break;
}
#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1
// Compute the field width pad character
if (fs.field_width_opt != NPF_FMT_SPEC_OPT_NONE) {
if (fs.leading_zero_pad) { // '0' flag is only legal with numeric types
if ((fs.conv_spec != NPF_FMT_SPEC_CONV_STRING) &&
(fs.conv_spec != NPF_FMT_SPEC_CONV_CHAR) &&
(fs.conv_spec != NPF_FMT_SPEC_CONV_PERCENT)) {
#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1
if ((fs.prec_opt != NPF_FMT_SPEC_OPT_NONE) && !fs.prec && zero) {
pad_c = ' ';
} else
#endif
{ pad_c = '0'; }
}
} else { pad_c = ' '; }
}
#endif
// Compute the number of bytes to truncate or '0'-pad.
#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1
if (fs.conv_spec != NPF_FMT_SPEC_CONV_STRING) {
#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1
// float precision is after the decimal point
if ((fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_DEC) &&
(fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_SCI) &&
(fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_SHORTEST) &&
(fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_HEX))
#endif
{ prec_pad = npf_max(0, fs.prec - cbuf_len); }
}
#endif
#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1
// Given the full converted length, how many pad bytes?
field_pad = fs.field_width - cbuf_len - !!sign_c;
if (need_0x) { field_pad -= 2; }
#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1
field_pad -= prec_pad;
#endif
field_pad = npf_max(0, field_pad);
// Apply right-justified field width if requested
if (!fs.left_justified && pad_c) { // If leading zeros pad, sign goes first.
if (pad_c == '0') {