-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintf.c
1380 lines (1145 loc) · 33.3 KB
/
printf.c
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
/* printf.c - miniature printf-style string formatting without dependencies.
This file is part of yalloc, yet another memory allocator providing affordable safety in a compact package.
For obvious reasons, any string formatting within yalloc cannot invoke malloc(), and any other dependency will make yalloc inherit such.
Thus, this miniature version is used, being reasonably standards-conformant and has no more dependencies than yalloc has.
SPDX-FileCopyrightText: © 2024 Joris van der Geer
SPDX-License-Identifier: GPL-3.0-or-later
mini snprintf(). All C11 features except multibyte / wide char are supported. C23 features are recognised, yet only wN supported
A nonstandard, yet standards-compatible backtick *after* a conversion modifiies the behaviour as follows :
- After an integer conversion spec, formats a human-readable int with K/M/G/T suffix. "%u`",123_456_789 = '12.3M'
- After a string conversion spec, adds a 's' plural char if the previous integer converted was not '1'. %u %s``,itemcnt,"item" = '2 items' for item == 2
A nonstandard leading zero in a precision for %u translates into "%u.%u",val >> 16,val & 0xffff), or 32 bits for %lu
For %x and %b it enables grouping in 4 digits
These two in order to have the compiler's format diagnostics work whilst having a way to use extensions.
TODO floating point rounding is inaccurate, last digit may be wrong
The base function is :
unsigned int snprintf_mini(char * restrict str, unsigned int offset, unsigned int len, const char * restrict format, ...)
and its related mini_vsnprintf(... va_list ap)
Formatted output, including a null byte, is written into 'str' starting at 'offset' up to the maximum string length 'len'.
The offset facilitates stepwise appending to a string without adjusting the length parameter
In contrast with snprintf, at most (len - 1) bytes are written and a null always appended, thus the actual buffer length can be passed.
The number of chars - excluding the null - actually written is returned.
Example:
char buffer[Buflen];
unsigned int current;
current = snprintf_mini(buffer, 0, Buflen, "bla %a %b %c %d %e %f %g %hu \n ", 1.2, 3, '4', -5, 6e7, 0.89, 1e5, 11);
current += snprintf_mini(buffer, current, Buflen, "more stories %u \n", 1234);
write(1, buffer, current);
format string is parsed into conversion spec, flags, modifiers, width and precision. The latter are fetched for '*'
all are interpreted and normalized
args are fetched according to modifiers
conversion specs, width and precision are further normalized / changed based on value
conversion is performed
negative values are converted as positive and sign prepended
zero values are handled specially
padding and decimal separators are added at the end
*/
/* Floating-point support
0 - print as integer
1 - elementary support, can be slow
2 - full support - do not use for yalloc
*/
#define Mini_printf_float_formats 0 // %f %g %a
#ifdef Miniprint_inc
#define Static_if_included static
#else
#define Static_if_included
#include <stddef.h> // size_t
#include <stdarg.h>
#include <string.h> // memcpy
#include <errno.h> // %m
#include "base.h"
#include "printf.h"
#endif
#ifndef LONG_MAX
#include <limits.h>
#endif
#if Mini_printf_float_formats
// #include <float.h> // mant-dig
#include <math.h> // fpclassify, frexp,pow for formats = 2
#else
enum Dumfpcls { FP_NORMAL,FP_SUBNORMAL,FP_INFINITE,FP_NAN,FP_ZERO };
#define fpclassify(x) (x) == 0 ? FP_ZERO : FP_NORMAL
#endif
#include <stdint.h> // intmax_t
typedef long long sb8;
// max single conversion length without padding, except %s
#define Maxfmt 256
enum Radix { Radixdec,Radixoct,Radixhex,Radixbin };
enum Packed8 Fmt { // note the value assignments
Fmtinv,
Flgws=1,
Flgpls=2, // +
Flgpad0=4, // %02d
Fmtdot,
Fmtast, // %*d
Flgleft=8, // %-3u
// len mods
Modh,Modl,ModL,
Modj, // intmax_t
Modz, // size_t
Modt, // ptrdiff_t
Modw, // wN
Flgalt=16,
ModD, // Decimal
ModH,
Flghr=32, // nonstandard human-readable 2.3M
// 0-9
Dig0,Dig1,Dig2,Dig3,Dig4,Dig5,Dig6,Dig7,Dig8,Dig9,
Flgsep=64, // %'
Fmtc,Fmts,Fmtm,
Fmtd,Fmtu,Fmtb,FmtB,Fmto,Fmtx,FmtX,Fmtp,
Fmtf=76,FmtF,Fmtg,FmtG,Fmte,FmtE,Fmta,FmtA, // odd for uppercase
Fmtpct, // %%
Fmtn, // %n
Fmteof
};
enum Packed8 Cnv {
Cnvinv,
Cnvu, // all int
Cnvc, // %c
Cnve, // all float
Cnvs, // %s
Cnvp, // %p
Cnvn, // %n
Cnvm, // %m
// derived
Cnvlu,Cnvllu,
Cnvle,
Cnvju
};
static const ub1 hextab[16] = "0123456789abcdef";
static const ub1 Hextab[16] = "0123456789ABCDEF";
// %u
static ub1 *ucnv(ub1 *end,unsigned int x)
{
do *--end = (ub1)((x % 10) + '0'); while (x /= 10);
return end;
}
// %x
static ub1 *hexcnv(ub1 *end,unsigned int x,ub1 Case)
{
const unsigned char *tab = Case ? Hextab : hextab;
do {
*--end = tab[x & 0xf];
} while (x >>= 4);
return end;
}
// %o %b
static ub1 *xcnv(ub1 *end,unsigned int x,enum Radix rdx,enum Fmt flags)
{
ub1 msk=1,shr=1;
switch (rdx) {
case Radixdec:
case Radixhex: return end;
case Radixbin: break;
case Radixoct: msk = 7; shr = 3; break;
}
do *--end = (x & msk) + '0'; while (x >>= shr);
if ( (flags & Flgalt) && *end != '0') *--end = '0';
return end;
}
static const unsigned char cnvtab[200] =
"00010203040506070809"
"10111213141516171819"
"20212223242526272829"
"30313233343536373839"
"40414243444546474849"
"50515253545556575859"
"60616263646566676869"
"70717273747576777879"
"80818283848586878889"
"90919293949596979899"
;
// %lu
static ub1 *ulcnv(ub1 *end,unsigned long x)
{
const unsigned char *p;
if (x == 0) { *--end = '0'; return end; }
while (x >= 10) {
p = cnvtab + 2 * (x % 100);
*--end = p[1];
*--end = p[0];
x /= 100;
}
if (x) *--end = (ub1)(x + '0');
return end;
}
// %lx
static ub1 *hexlcnv(ub1 *end,unsigned long x,ub1 Case)
{
const unsigned char *tab = Case ? Hextab : hextab;
do {
*--end = tab[x & 0xf];
} while (x >>= 4);
return end;
}
// %lo %lb
static ub1 *xlcnv(ub1 *end,unsigned long x,enum Radix rdx,enum Fmt flags)
{
ub1 msk=1,shr=1;
switch (rdx) {
case Radixhex:
case Radixdec: return end;
case Radixbin: break;
case Radixoct: msk = 7; shr = 3; break;
}
do *--end = (x & msk) + '0'; while (x >>= shr);
if ( (flags & Flgalt) && *end != '0') *--end = '0';
return end;
}
#if ULLONG_MAX > ULONG_MAX
// ll mod
static ub1 *ullcnv(ub1 *end,unsigned long long x)
{
const unsigned char *p;
ub4 n = 0;
if (x == 0) { *--end = '0'; return end; }
while (x >= 10) {
p = cnvtab + 2 * (x % 100);
*--end = p[1];
*--end = p[0];
x /= 100;
}
if (x) *--end = (ub1)((x % 10) + '0');
return end;
}
static ub1 *hexllcnv(ub1 *end,unsigned long long x,ub1 Case)
{
const unsigned char *tab = Case ? Hextab : hextab;
do {
*--end = tab[x & 0xf];
} while (x >>= 4);
return end;
}
static ub1 *xllcnv(ub1 *end,unsigned long long x,enum Radix rdx,enum Fmt flags,ub1 Case)
{
ub1 msk=1,shr=1;
switch (rdx) {
case Radixhex:
case Radixdec: return end;
case Radixbin: break;
case Radixoct: msk = 7; shr = 3; break;
}
do *--end = (x & msk) + '0'; while (x >>= shr);
if ( (flags & Flgalt) && *end != '0') *--end = '0';
return end;
}
#elif Mini_printf_float_formats > 0
#define ullcnv(end,x) ulcnv(end,x)
#define hexllcnv(end,x,case) hexlcnv(end,x,case)
#endif
// human-readable %u, 2.3G
static ub1 *hrcnv(ub1 *end,ub4 x1,ub4 x2,ub1 scale)
{
*--end = scale;
*--end = ' ';
x2 &= 0x3ff;
if (x2 > 999) { x1++; x2 = 0; }
else x2 /= 100;
if (x2) {
*--end = (ub1)x2 + '0';
*--end = '.';
}
return ucnv(end,x1);
}
static ub1 *Ucnv(ub1 *end,ub4 x)
{
ub4 x1,x2;
ub1 scale;
if (x >= 1024u * 1024u * 1024u) { x1 = x >> 30; x2 = x >> 20; scale = 'G'; }
else if (x >= 1024u * 1024u) { x1 = x >> 20; x2 = x >> 10; scale = 'M'; }
else if (x > 9999) { x1 = x >> 10; x2 = x; scale = 'k'; }
else {
return ucnv(end,x);
}
return hrcnv(end,x1,x2,scale);
}
// idem, long
static ub1 *Ulcnv(ub1 *end,unsigned long x)
{
ub4 x1,x2;
ub1 scale;
ub2 shift;
#if ULONG_MAX > 0xffffffff
if (x >= (1ul << 60)) { shift = 60; scale = 'E'; }
else if (x >= (1ul << 50)) { shift = 50; scale = 'P'; }
else if (x >= (1ul << 40)) { shift = 40; scale = 'T'; }
else
#endif
if (x >= (1ul << 30)) { shift = 30; scale = 'G'; }
else {
return Ucnv(end,(ub4)x);
}
x1 = (ub4)(x >> shift);
x2 = (ub4)(x >> (shift - 10));
return hrcnv(end,x1,x2,scale);
}
#if Mini_printf_float_formats
// for all floating point formats, zero, inf and nan are handled separately and negatives converted
// for %f
static ub1 *ullcnv_dot(ub1 *end,unsigned long long x,ub4 dotpos)
{
const unsigned char *p;
ub4 n = 0;
while (x >= 10) {
p = cnvtab + 2 * (x % 100);
*--end = p[1]; if (++n == dotpos) *--end = '.';
*--end = p[0]; if (++n == dotpos) *--end = '.';
x /= 100;
}
if (x) *--end = (ub1)(x + '0');
return end;
}
static double tentab[19] = { 10.0,100.0,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13,1e14,1e15,1e16,1e17,1e18 };
static double itentab[19] = { 0.1,0.01,1e-3,1e-4,1e-5,1e-6,1e-7,1e-8,1e-9,1e-10,1e-11,1e-12,1e-13,1e-14,1e-15,1e-16,1e-17,1e-18 };
static double sixteentab[17] = { 1.0,16.0,256.0,4096.0,65536.0,1.04857600e+06,1.677721600e+07,2.6843545600e+08,4.29496729600e+09,6.871947673600e+10,1.09951162777600e+12,1.759218604441600e+13,2.8147497671065600e+14,4.50359962737049600e+15,7.2057594037927936e+16,1.1529215046068472e+18,1.8446744073709556e+19 };
// %a x > 0 < inf
static ub1 *acnv(ub1 *end,double x,enum Fmt flags,ub4 prec,ub1 Case)
{
int exp;
ub4 p;
ub4 uexp = 0,nexp;
double mant;
unsigned long long imant;
if (prec == Hi32) p = 16;
else if (prec > 16) {
uexp = prec - 16;
p = 16;
} else p = prec;
mant = frexp(x,&exp); // x = frac * 2^iexp
if (exp < 0) nexp = (ub4)-exp;
else nexp = (ub4)exp;
end = hexcnv(end,nexp,Case);
*--end = Case ? 'P' : 'p';
*--end = exp < 0 ? '-' : '+';
mant *= sixteentab[p];
imant = (unsigned long long)mant;
if (uexp > 1) {
do *--end = '0'; while (--uexp);
end -= uexp;
}
end = hexllcnv(end,imant,Case);
if (prec == 0 && (flags & Flgalt) ) return end;
end--;
*end = end[1];
end[1] = '.';
*--end = Case ? 'X' : 'x';
*--end = '0';
return end;
}
#endif // Mini_printf_float_formats
// Primitive version
#if Mini_printf_float_formats == 1
// %e x > 0 < inf
static ub1 *ecnv(ub1 *end,double x,enum Fmt flags,ub1 casemsk,ub4 prec)
{
ub4 pexp,exp = 0;
ub8 ix;
double xx,ten;
bool eneg = 0;
prec = min(prec,17);
if (x < 1.0) {
eneg = 1;
if (x > 1e-17) {
while (x < itentab[exp]) exp++;
xx = x * tentab[exp];
exp++;
} else {
xx = x;
while (xx < 1.0) { exp++; xx *= 10.0; }
}
while (prec) { xx *= 10.0; prec--; }
} else if (x > 1e16) {
ten = x;
while (ten >= 10.0) { exp++; ten *= 0.1; }
xx = x; pexp = 0;
while (xx >= 10.0 && pexp + prec < exp) { pexp++; xx *= 0.1; }
} else {
if (x < tentab[17 - prec]) {
xx = x;
while (x >= tentab[exp]) exp++;
// exp++;
pexp = exp;
while (pexp < prec) { pexp++; xx *= 10.0; }
} else {
xx = 10.0;
while (x > xx) { exp++; xx *= 10.0; }
xx = x;
pexp = prec;
while (xx < tentab[pexp]) { pexp++; xx *= 10.0; }
}
}
end = ucnv(end,exp);
if (exp < 10) *--end = '0';
*--end = eneg ? '-' : '+';
*--end = 'E' | casemsk;
ix = (ub8)xx;
if (xx - (double)ix >= 0.5) ix++;
end = ullcnv(end,ix);
if (prec == 0 && x >= 1.0&& (flags & Flgalt) ) return end;
end--;
*end = end[1]; // insert dot
end[1] = '.';
return end;
}
// %f - only between 1e-18 and 1e+18
static ub1 *fcnv(ub1 *end,double x,enum Fmt flags,ub1 casemsk,ub4 prec)
{
ub8 ix;
ub4 exp = 0,dig = 0;
if (x < 1.0) {
if (x < 1e-18) return ecnv(end,x,flags,casemsk,prec);
if (prec == 0) {
*--end = (x >= 0.5) ? '1' : '0';
return end;
}
while (dig < prec - 1 && x < itentab[dig]) dig++;
while (exp < prec - dig && x < itentab[exp]) exp++;
if (exp + prec > 18) { *--end = '0'; return end; } // return ecnv(end,x,flags,prec);
x *= tentab[exp + prec];
ix = (ub8)x;
if (x - (double)ix >= 0.5) ix++;
end = ullcnv(end,ix);
while (exp) { *--end = '0'; exp--; } // 0.000 ...
*--end = '.';
*--end = '0';
return end;
}
if (x > 1e18) return ecnv(end,x,flags,casemsk,prec);
if (prec) {
while (x > tentab[exp]) exp++;
if (exp + prec > 18) return ecnv(end,x,flags,casemsk,prec);
prec = min(prec,16);
x *= tentab[prec - 1];
}
ix = (ub8)(x);
if (x - (double)ix >= 0.5) ix++;
return ullcnv_dot(end,ix,prec);
}
#elif Mini_printf_float_formats == 2
// normalize x to mant in [0.5..1] and 10^exp10
static double fpnorm(double x,int *piexp10)
{
int iexp2,iexp10;
double frac2 = frexp(x,&iexp2); // x = frac * 2^iexp
static double tenlog2 = 0.30102999566398119521373889472449;
double exp10 = (long double)iexp2 * tenlog2;
double exp10f = floorl(exp10);
double dexp = exp10 - exp10f;
frac2 *= pow(10.0,dexp);
iexp10 = (int)exp10;
// if (frac2 >=10.0) { frac2 *= 0.1; iexp10++; }
// else if (frac2 <1.0) { frac2 *= 10.0; iexp10--; }
*piexp10 = iexp10;
return frac2;
}
// %f x > 0 < inf
static ub1 *fcnv(ub1 *end,double x,enum Fmt flags,ub1 casemsk,ub4 prec)
{
int iexp,ixexp;
ub4 uexp,exp,xexp,dotpos,digits;
double mant,xmant;
unsigned long long imant;
mant = fpnorm(x,&iexp);
end = ullcnv(end,iexp);
if (iexp >= 0) {
exp = (ub4)iexp;
if (exp > 16) {
xexp = exp - 16;
exp -= xexp;
memset(end - xexp,'0',xexp); end -= xexp;
}
dotpos = (ub4)(16 - exp);
digits = min(prec,dotpos);
mant *= tentab[dotpos - digits];
xmant = mant - floor(mant);
imant = (unsigned long long)mant;
if (xmant >= 0.5) imant++;
end = ullcnv_dot(end,imant,digits);
return end;
} else { // exp < 0
if (-iexp <= prec) {
if (iexp < -16) {
ixexp = iexp + 16;
iexp += ixexp;
}
uexp = (ub4)-iexp;
mant *= tentab[16 - prec];
xmant = mant - floor(mant);
imant = (unsigned long)xmant;
if (xmant >= 0.5) imant++;
end = ullcnv(end,imant);
} else uexp = prec;
if (uexp > 1) {
memset(end - uexp - 1,'0',uexp);
end -= uexp - 1; // 0.000 ...
}
*--end = '.';
*--end = '0';
}
return end;
}
// %e x > 0 < inf
static ub1 *ecnv(ub1 *end,double x,enum Fmt flags,ub1 casemsk,ub4 prec)
{
int exp;
ub4 uexp,nexp;
double mant,xmant;
unsigned long long imant;
if (prec > 16) {
uexp = prec - 16;
} else uexp = 0;
mant = fpnorm(x,&exp);
mant *= tentab[16 - prec];
xmant = mant - floor(mant);
imant = (unsigned long long)mant;
if (xmant >= 0.5) imant++;
if (exp < 0) nexp = (ub4)-exp;
else nexp = (ub4)exp;
end = ucnv(end,nexp);
*--end = 'E' | casemsk;
*--end = exp < 0 ? '-' : '+';
if (uexp > 1) {
memset(end - uexp,'0',uexp);
end -= uexp;
}
end = ullcnv(end,imant);
if (prec == 0 && (flags & Flgalt) ) return end;
end--;
*end = end[1]; // insert dot
end[1] = '.';
return end;
}
#endif // Mini_printf_float_formats == 2
static const enum Fmt fmtmap[128] = { // format template for parsing
[0] = Fmteof,
// flags
['-'] = Flgleft,
['+'] = Flgpls,
[' '] = Flgws,
['#'] = Flgalt,
['0'] = Dig0,
['I'] = Flghr,
['\''] = Flgsep,
['.'] = Fmtdot,
['*'] = Fmtast,
['1'] = Dig1, ['2'] = Dig2, ['3'] = Dig3, ['4'] = Dig4, ['5'] = Dig5, ['6'] = Dig6, ['7'] = Dig7, ['8'] = Dig8, ['9'] = Dig9,
// length mods
['l'] = Modl, ['h'] = Modh, ['L'] = ModL,
['z'] = Modz,
['t'] = Modt,
['j'] = Modj,
['w'] = Modw,
['D'] = ModD,
['H'] = ModH,
// integer
['b'] = Fmtb, ['B'] = FmtB, ['d'] = Fmtd, ['i'] = Fmtd, ['u'] = Fmtu, ['o'] = Fmto, ['x'] = Fmtx, ['X'] = FmtX,
// float
['f'] = Fmtf, ['F'] = FmtF,
['g'] = Fmtg, ['G'] = FmtG,
['e'] = Fmte, ['E'] = FmtE,
['a'] = Fmta, ['A'] = FmtA,
// char / string / ptr
['c'] = Fmtc, ['s'] = Fmts, ['p'] = Fmtp,
['n'] = Fmtn,
['m'] = Fmtm,
['%'] = Fmtpct
};
#define Pluralbuf 256
// writes into dst + pos, upto dst + dlen from fmt and args. Always zero-terminates. Returns #chars written
Static_if_included ub4 Cold mini_vsnprintf(char *dst,ub4 pos,ub4 dlen,const char *fmt,va_list ap)
{
// width and precision
ub4 wid,prec,mindig,P,pad0,slen,sndx;
// modifiers
// enum Lenmod mods;
ub4 modw = 0;
ub1 modl,modh;
ub1 modD;
// cnv specs
enum Cnv cnv;
enum Fmt fx,mod,fmtf = Fmtinv;
enum Radix rdx;
// flags
enum Fmt flags;
ub1 sign;
bool isneg,iszero=0,isone=0,prvone=0;
bool flgdon;
ub1 dotseen,lzprec;
ub1 Case,casemsk;
// args
sb4 s4=0;
long s8=0;
ub4 u4=0;
unsigned long u8=0;
#if LLONG_MAX > LONG_MAX
unsigned long long ull = 0;
#endif
double f8=0;
long double lf8;
int fpclas = 0;
void *vp=nil;
unsigned int *uip;
const char *p8;
// cnv buf
ub1 *org,*org2;
ub1 cnvbuf[Maxfmt];
ub1 pluralbuf[Pluralbuf];
ub1 cnvbuf2[Maxfmt];
ub1 *end2,*end = cnvbuf + Maxfmt - 1;
// misc
int local_err = 0;
ub4 pi=0;
ub2 dig,grp;
unsigned char c;
char d;
ub4 len,xlen;
ub4 sorg,padorg;
ub4 n = 0;
cchar *p = fmt;
dst += pos;
*dst = 0;
while (pos + n + 2 < dlen) {
c = (ub1)p[pi++];
// all conversions
if (c == '%') {
cnv = Cnvinv;
prec = Hi32;
wid = Hi32;
mindig = Hi32;
modl = modh = modD = 0;
mod = 0;
flags = 0;
rdx = Radixdec;
sign = 0;
flgdon = 0;
dotseen = 0;
Case = 0;
isone = 0;
lzprec = 0;
do { // while cnv
c = (ub1)p[pi++];
if (c < 128) fx = fmtmap[c];
else fx = Fmtinv;
switch (fx) {
// flags:
case Flgleft:
case Flgpls: case Flgws: case Flgalt: case Flgsep: case Flghr:
flags |= fx; // check flag assignment
break;
// width and precision
case Dig0:
if (flgdon == 0) { flags |= Flgpad0; flgdon = 1; }
else if (dotseen == 1) { lzprec++; break; }
Fallthrough
case Dig1: case Dig2: case Dig3: case Dig4: case Dig5: case Dig6: case Dig7: case Dig8: case Dig9:
u4 = fx - Dig0;
if (dotseen) {
dotseen++;
if (prec < 65534 / 10) prec = prec * 10 + u4;
} else {
if (wid == Hi32) wid = u4;
else if (wid < 65534 / 10) wid = wid * 10 + u4;
}
flgdon = 1; // no more flags
break;
case Fmtdot: // between width and precision
dotseen = flgdon = 1;
prec = 0;
break;
case Fmtast: // %*.*d
flgdon = 1;
s4 = va_arg(ap,int);
if (dotseen) {
if (s4 >= 0) prec = (ub4)s4;
} else {
dotseen = 1;
if (s4 < 0) { wid = (ub4)-s4; flags |= Flgleft; }
else wid = (ub4)s4;
}
break;
// l, h and D modifiers
case Modh: modh = (modh + 1) & 7; break;
case Modl: modl = (modl + 1) & 7; break;
case ModD: modD = (modD + 1) & 3; break;
// c23 wN
case Modw:
if (p[pi] == 'f') pi++; // ignore
modw = 0;
while ( (c = (ub1)p[pi]) >= '1' && c <= '9') { modw = (modw & Hi16) * 10 + c - '0'; pi++; }
Fallthrough
case Modj:
case Modz:
case Modt:
case ModH:
case ModL: mod = fx; break;
// integer conversions
case Fmtc: cnv = Cnvc; break;
case Fmtd: sign = 1; Fallthrough
case Fmtu: cnv = Cnvu; break;
case FmtB: Case = 1; Fallthrough
case Fmtb: rdx = Radixbin; cnv = Cnvu; break;
case Fmto: rdx = Radixoct; cnv = Cnvu; break;
case FmtX: Case = 1; Fallthrough
case Fmtx: rdx = Radixhex; cnv = Cnvu; break;
// string
case Fmts: prec = min(prec,dlen - pos - n); cnv = Cnvs; break;
case Fmtm: prec = min(prec,dlen - pos - n); cnv = Cnvm; break;
// float
case FmtA: case Fmta: flags |= Flgalt; cnv = Cnve; break;
case FmtF: case FmtG: case FmtE:
case Fmtf: case Fmtg: case Fmte: cnv = Cnve; break;
// misc
case Fmtp: cnv = Cnvu; rdx = Radixhex; mod = Modz; flags |= Flgalt; break;
case Fmtn: cnv = Cnvn; break;
case Fmtpct: dst[n++] = (char)c; break;
case Fmtinv: dst[n++] = '%'; dst[n++] = (char)c; dst[n++] = '!'; break;
case Fmteof: case Flgpad0: break;
} // switch fx
if (cnv == Cnve) {
Case = fx & 1;
fmtf = fx & ~1;
if (prec == Hi32 && fmtf != Fmta) prec = 6;
else if (prec > Maxfmt - 32) prec = Maxfmt - 32;
}
} while (fx != 0 && fx < Fmtc); // end at cnvspec
if (p[pi] == '`') { // nonstandard human-readable ints or plurals
if (cnv == Cnvu || cnv == Cnvs) { pi++; flags |= Flghr; }
}
// apply modifiers
if (cnv == Cnvu) {
if (mod == Modw) {
switch (modw) {
case 8: modh = 2; break;
case 16: modh = 1; break;
case 32: if (sizeof(long) == 32) modl = 1; break;
case 64:if (sizeof(long) == 32) modl = 2; else modl = 1; break;
case 128: modl = 2; break;
}
} // modw
#if LLONG_MAX > LONG_MAX
if (modl == 1) cnv = Cnvlu; // ld
else if (modl) cnv = Cnvllu; // lld
#else
if (modl) cnv = Cnvlu;
#endif
#if SIZE_MAX > UINT_MAX
else if (mod == Modz) cnv = Cnvlu; // zd
#endif
#if PTRDIFF_MAX > INT_MAX
else if (mod == Modt) cnv = Cnvlu; // td
#endif
#if LLONG_MAX > LONG_MAX
else if (mod == Modj ) cnv = Cnvllu; // jd
#else
else if (mod == Modj ) cnv = Cnvlu;
#endif
if (prec == Hi32) prec = 1;
else {
flags = flags & ~Flgpad0;
if (prec > Maxfmt - 32) prec = Maxfmt - 32;
}
mindig = prec;
} else if (cnv == Cnve) { // Cnvu
sign = 1;
#if Mini_printf_float_formats == 2
#if LDBL_MANT_DIG > DBL_MANT_DIG
if (mod == ModL) cnv = Cnvle;
#endif
#endif
} else {
flags = flags & ~(Flgpad0 | Flgsep); // %s
} // each base cnvspec
isneg = 0; iszero = 0;
casemsk = Case ? 0 : 0x20;
org = end;
switch (cnv) { // get args
case Cnvc: // %c
s4 = va_arg(ap,int);
break;
case Cnvu: // %u %x %o %b
u4 = va_arg(ap,unsigned int);
iszero = (u4 == 0);
isone = (u4 == 1);
if (sign) {
s4 = (sb4)u4;
if (s4 < 0) { u4 = (ub4)-s4; isneg = 1; }
}
if (modh == 1) u4 = (unsigned short)u4;
else if (modh) u4 = (unsigned char)u4;
break;
case Cnvlu: // %lu
u8 = va_arg(ap,unsigned long);
iszero = (u8 == 0);
isone = (u8 == 1);
if (sign) {
s8 = (sb8)u8;
if (s8 < 0) { u8 = (ub8)-s8; isneg = 1; }
}
if (u8 <= UINT_MAX) { u4 = (ub4)u8; cnv = Cnvu; }
break;
case Cnvllu: // %llu
#if LLONG_MAX > LONG_MAX
ull = va_arg(ap,unsigned long long);
iszero = (ull == 0);
isone = (ull == 1);
if (sign && (long long)ull < 0) { ull = -(long long)ull; isneg = 1; }
if (ull <= UINT_MAX) { u4 = (ub4)ull; cnv = Cnvu; }
else if (ull <= ULONG_MAX) { u8 = (ub8)ull; cnv = Cnvlu; }
#endif
break;
case Cnve: // %f,e,a
case Cnvle:
if (cnv == Cnve) {
f8 = va_arg(ap,double);
fpclas = fpclassify(f8);
switch (fpclas) {
case FP_NORMAL: case FP_SUBNORMAL: if (f8 < 0) { isneg = 1; f8 = -f8; } break;
case FP_INFINITE: if (f8 < 0) isneg = 1; break;
case FP_NAN: case FP_ZERO: break;
}
} else {
lf8 = va_arg(ap,long double);
fpclas = fpclassify(lf8);
switch (fpclas) {
case FP_NORMAL: case FP_SUBNORMAL: if (lf8 < 0) { isneg = 1; lf8 = -lf8; } break;
case FP_INFINITE: if (f8 < 0) isneg = 1; break;
case FP_NAN: case FP_ZERO: break;
}
}
switch (fpclas) {
case FP_INFINITE: vp = Case ? "INF" : "inf"; cnv = Cnvs; prec = Hi32; break;
case FP_NAN: vp = Case ? "NAN" : "nan"; cnv = Cnvs; prec = Hi32; break;
case FP_NORMAL: case FP_SUBNORMAL: break;
case FP_ZERO: break;
}
break;
// %n,s,p
case Cnvn:
case Cnvs:
case Cnvp: vp = va_arg(ap,void *);
break;
case Cnvm: // %m -> 'no such file: 2'
local_err = errno;
if (local_err) {
p8 = strerror(local_err);
u4 = (ub4)strlen(p8);