-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfmt.h
More file actions
4933 lines (4504 loc) · 163 KB
/
fmt.h
File metadata and controls
4933 lines (4504 loc) · 163 KB
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
/* https://github.com/JaMo42/fmt */
#ifndef FMT_H
#define FMT_H
#ifdef _MSC_VER
# define FMT_NO_LANGINFO
#endif
#include <stddef.h>
#if __STDC_VERSION__ <= 201710L
# include <stdbool.h>
#endif
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <locale.h>
#ifndef FMT_NO_LANGINFO
# include <langinfo.h>
#endif
#include <wctype.h>
#include <assert.h>
////////////////////////////////////////////////////////////////////////////////
// MARK: - Compatibility & Configuration
////////////////////////////////////////////////////////////////////////////////
// Users can use any library that provides the threads.h interface, as long as
// they provide a definition for FMT__THREAD_LOCAL, so that can also be used as
// check. Afterwards FMT_HAS_THREADS must be used as thread local can exist
// without the rest of the threads library. s
#ifndef FMT__THREAD_LOCAL
# ifdef __STDC_NO_THREADS__
# if __STDC_VERSION__ <= 201710L
# define FMT__THREAD_LOCAL _Thread_local
# else
# define FMT__THREAD_LOCAL thread_local
# endif
# ifdef FMT_LOCKED_DEFAULT_PRINTERS
# undef FMT_LOCKED_DEFAULT_PRINTERS
# error "FMT_LOCKED_DEFAULT_PRINTERS not supported because <threads.h> is not available"
# endif
# else // __STDC_NO_THREADS__
# include <threads.h>
# define FMT__THREAD_LOCAL thread_local
# define FMT_HAS_THREADS
# endif // __STDC_NO_THREADS__
#else // FMT__THREAD_LOCAL
# define FMT_HAS_THREADS
#endif // FMT__THREAD_LOCAL
// In theory we don't need to wrap char16_t and char32_t, as uchar.h defines them
// if they don't exist, but Apple's clang does not have uchar.h for some reason.
#ifdef __cplusplus
// In C++ these are distinct types that can get first class type IDs;
// doesn't really matter as this library is not intended for C++ and the
// compatibility just exists to easy multi-language projects, but it's
// still nice to have.
typedef char32_t fmt_char32_t;
typedef char16_t fmt_char16_t;
#ifdef __cpp_char8_t
#define FMT_CPP_CHAR8_DISTINCT
typedef char8_t fmt_char8_t;
#else
typedef uint_least8_t fmt_char8_t;
#endif
#else
// In C however they are always just integers, even if uchar.h is available;
// they can be used as strings but will always need an explicit type in the
// format string to be printed as characters.
typedef uint_least8_t fmt_char8_t;
typedef uint_least32_t fmt_char32_t;
typedef uint_least16_t fmt_char16_t;
#endif
#if defined(__cplusplus) || __STDC_VERSION__ > 201710L
# define FMT__NORETURN [[noreturn]]
# define FMT__STATIC_ASSERT static_assert
#else
# define FMT__NORETURN _Noreturn
# define FMT__STATIC_ASSERT _Static_assert
#endif
#ifndef FMT_DEFAULT_TIME_FORMAT
# define FMT_DEFAULT_TIME_FORMAT "{a} {b} {d:0} {H}:{M}:{S} {Y}"
#endif
// Detecting empty integer macro: https://stackoverflow.com/a/48540034
#if defined(FMT_DEFAULT_FLOAT_PRECISION) \
&& ((0 - FMT_DEFAULT_FLOAT_PRECISION - 1) == 1 \
&& (FMT_DEFAULT_FLOAT_PRECISION + 0) != -2)
# error "FMT_DEFAULT_FLOAT_PRECISION is empty, reverting to default"
# undef FMT_DEFAULT_FLOAT_PRECISION
#endif
#ifndef FMT_DEFAULT_FLOAT_PRECISION
# define FMT_DEFAULT_FLOAT_PRECISION -1
#endif
#if defined(FMT_BUFFERED_WRITER_CAPACITY) \
&& ((0 - FMT_BUFFERED_WRITER_CAPACITY - 1) == 1 \
&& (FMT_BUFFERED_WRITER_CAPACITY + 0) != -2)
# error "FMT_BUFFERED_WRITER_CAPACITY is empty, reverting to default"
# undef FMT_BUFFERED_WRITER_CAPACITY
#endif
#ifndef FMT_BUFFERED_WRITER_CAPACITY
# define FMT_BUFFERED_WRITER_CAPACITY 32
#endif
#define FMT_TIME_DELIM '%'
// Apparently C++ doesn't have this.
#if defined(__cplusplus) && !defined(restrict)
# define FMT__MY_RESTRICT
# define restrict
#endif
// _WIN32 may not be the correct check here, only tested with clang 16.0.5 on
// Windows. On that compiler we can't just copy the `va_list`s as that would
// keep giving us the first argument over and over. On linux however we can't
// take the address of a `va_list`.
#if defined(_WIN32) || defined(__APPLE__)
typedef va_list *fmt__va_list_ref;
#define FMT__VA_LIST_REF(ap) &ap
#define FMT__VA_LIST_DEREF(ap) *ap
#else
typedef va_list fmt__va_list_ref;
#define FMT__VA_LIST_REF(ap) ap
#define FMT__VA_LIST_DEREF(ap) ap
#endif
////////////////////////////////////////////////////////////////////////////////
// MARK: - Short Names
////////////////////////////////////////////////////////////////////////////////
#ifndef FMT_NO_SHORT_NAMES
#define print fmt_print
#define println fmt_println
#define eprint fmt_eprint
#define eprintln fmt_eprintln
#define panic fmt_panic
#define todo fmt_todo
#define unimplemented fmt_unimplemented
#define unreachable fmt_unreachable
#endif
////////////////////////////////////////////////////////////////////////////////
// MARK: - Recursive macros
////////////////////////////////////////////////////////////////////////////////
#define FMT__CAT(a, b) FMT__CAT_1(a, b)
#define FMT__CAT_1(a, b) a##b
#define FMT__FOR_EACH_0(what)
#define FMT__FOR_EACH_1(what, x) what(x)
#define FMT__FOR_EACH_2(what, x, ...) what(x), FMT__FOR_EACH_1(what, __VA_ARGS__)
#define FMT__FOR_EACH_3(what, x, ...) what(x), FMT__FOR_EACH_2(what, __VA_ARGS__)
#define FMT__FOR_EACH_4(what, x, ...) what(x), FMT__FOR_EACH_3(what, __VA_ARGS__)
#define FMT__FOR_EACH_5(what, x, ...) what(x), FMT__FOR_EACH_4(what, __VA_ARGS__)
#define FMT__FOR_EACH_6(what, x, ...) what(x), FMT__FOR_EACH_5(what, __VA_ARGS__)
#define FMT__FOR_EACH_7(what, x, ...) what(x), FMT__FOR_EACH_6(what, __VA_ARGS__)
#define FMT__FOR_EACH_8(what, x, ...) what(x), FMT__FOR_EACH_7(what, __VA_ARGS__)
#define FMT__FOR_EACH_9(what, x, ...) what(x), FMT__FOR_EACH_8(what, __VA_ARGS__)
#define FMT__FOR_EACH_10(what, x, ...) what(x), FMT__FOR_EACH_9(what, __VA_ARGS__)
#define FMT__FOR_EACH_11(what, x, ...) what(x), FMT__FOR_EACH_10(what, __VA_ARGS__)
#define FMT__FOR_EACH_12(what, x, ...) what(x), FMT__FOR_EACH_11(what, __VA_ARGS__)
#define FMT__FOR_EACH_13(what, x, ...) what(x), FMT__FOR_EACH_12(what, __VA_ARGS__)
#define FMT__FOR_EACH_14(what, x, ...) what(x), FMT__FOR_EACH_13(what, __VA_ARGS__)
#define FMT__FOR_EACH_15(what, x, ...) what(x), FMT__FOR_EACH_14(what, __VA_ARGS__)
#define FMT__FOR_EACH_16(what, x, ...) what(x), FMT__FOR_EACH_15(what, __VA_ARGS__)
#define FMT__FOR_EACH_17(what, x, ...) what(x), FMT__FOR_EACH_16(what, __VA_ARGS__)
#define FMT__FOR_EACH_18(what, x, ...) what(x), FMT__FOR_EACH_17(what, __VA_ARGS__)
#define FMT__FOR_EACH_19(what, x, ...) what(x), FMT__FOR_EACH_18(what, __VA_ARGS__)
#define FMT__FOR_EACH_20(what, x, ...) what(x), FMT__FOR_EACH_19(what, __VA_ARGS__)
#define FMT__FOR_EACH_21(what, x, ...) what(x), FMT__FOR_EACH_20(what, __VA_ARGS__)
#define FMT__FOR_EACH_22(what, x, ...) what(x), FMT__FOR_EACH_21(what, __VA_ARGS__)
#define FMT__FOR_EACH_23(what, x, ...) what(x), FMT__FOR_EACH_22(what, __VA_ARGS__)
#define FMT__FOR_EACH_24(what, x, ...) what(x), FMT__FOR_EACH_23(what, __VA_ARGS__)
#define FMT__FOR_EACH_25(what, x, ...) what(x), FMT__FOR_EACH_24(what, __VA_ARGS__)
#define FMT__FOR_EACH_26(what, x, ...) what(x), FMT__FOR_EACH_25(what, __VA_ARGS__)
#define FMT__FOR_EACH_27(what, x, ...) what(x), FMT__FOR_EACH_26(what, __VA_ARGS__)
#define FMT__FOR_EACH_28(what, x, ...) what(x), FMT__FOR_EACH_27(what, __VA_ARGS__)
#define FMT__FOR_EACH_29(what, x, ...) what(x), FMT__FOR_EACH_28(what, __VA_ARGS__)
#define FMT__FOR_EACH_30(what, x, ...) what(x), FMT__FOR_EACH_29(what, __VA_ARGS__)
#define FMT__FOR_EACH_31(what, x, ...) what(x), FMT__FOR_EACH_30(what, __VA_ARGS__)
#define FMT__FOR_EACH_32(what, x, ...) what(x), FMT__FOR_EACH_31(what, __VA_ARGS__)
#define FMT__FOR_EACH_33(what, x, ...) what(x), FMT__FOR_EACH_32(what, __VA_ARGS__)
#define FMT__FOR_EACH_34(what, x, ...) what(x), FMT__FOR_EACH_33(what, __VA_ARGS__)
#define FMT__FOR_EACH_35(what, x, ...) what(x), FMT__FOR_EACH_34(what, __VA_ARGS__)
#define FMT__FOR_EACH_36(what, x, ...) what(x), FMT__FOR_EACH_35(what, __VA_ARGS__)
#define FMT__FOR_EACH_37(what, x, ...) what(x), FMT__FOR_EACH_36(what, __VA_ARGS__)
#define FMT__FOR_EACH_38(what, x, ...) what(x), FMT__FOR_EACH_37(what, __VA_ARGS__)
#define FMT__FOR_EACH_39(what, x, ...) what(x), FMT__FOR_EACH_38(what, __VA_ARGS__)
#define FMT__FOR_EACH_40(what, x, ...) what(x), FMT__FOR_EACH_39(what, __VA_ARGS__)
#define FMT__FOR_EACH_41(what, x, ...) what(x), FMT__FOR_EACH_40(what, __VA_ARGS__)
#define FMT__FOR_EACH_42(what, x, ...) what(x), FMT__FOR_EACH_41(what, __VA_ARGS__)
#define FMT__FOR_EACH_43(what, x, ...) what(x), FMT__FOR_EACH_42(what, __VA_ARGS__)
#define FMT__FOR_EACH_44(what, x, ...) what(x), FMT__FOR_EACH_43(what, __VA_ARGS__)
#define FMT__FOR_EACH_45(what, x, ...) what(x), FMT__FOR_EACH_44(what, __VA_ARGS__)
#define FMT__FOR_EACH_46(what, x, ...) what(x), FMT__FOR_EACH_45(what, __VA_ARGS__)
#define FMT__FOR_EACH_47(what, x, ...) what(x), FMT__FOR_EACH_46(what, __VA_ARGS__)
#define FMT__FOR_EACH_48(what, x, ...) what(x), FMT__FOR_EACH_47(what, __VA_ARGS__)
#define FMT__FOR_EACH_49(what, x, ...) what(x), FMT__FOR_EACH_48(what, __VA_ARGS__)
#define FMT__FOR_EACH_50(what, x, ...) what(x), FMT__FOR_EACH_49(what, __VA_ARGS__)
#define FMT__FOR_EACH_51(what, x, ...) what(x), FMT__FOR_EACH_50(what, __VA_ARGS__)
#define FMT__FOR_EACH_52(what, x, ...) what(x), FMT__FOR_EACH_51(what, __VA_ARGS__)
#define FMT__FOR_EACH_53(what, x, ...) what(x), FMT__FOR_EACH_52(what, __VA_ARGS__)
#define FMT__FOR_EACH_54(what, x, ...) what(x), FMT__FOR_EACH_53(what, __VA_ARGS__)
#define FMT__FOR_EACH_55(what, x, ...) what(x), FMT__FOR_EACH_54(what, __VA_ARGS__)
#define FMT__FOR_EACH_56(what, x, ...) what(x), FMT__FOR_EACH_55(what, __VA_ARGS__)
#define FMT__FOR_EACH_57(what, x, ...) what(x), FMT__FOR_EACH_56(what, __VA_ARGS__)
#define FMT__FOR_EACH_58(what, x, ...) what(x), FMT__FOR_EACH_57(what, __VA_ARGS__)
#define FMT__FOR_EACH_59(what, x, ...) what(x), FMT__FOR_EACH_58(what, __VA_ARGS__)
#define FMT__FOR_EACH_60(what, x, ...) what(x), FMT__FOR_EACH_59(what, __VA_ARGS__)
#define FMT__FOR_EACH_61(what, x, ...) what(x), FMT__FOR_EACH_60(what, __VA_ARGS__)
#define FMT__FOR_EACH_62(what, x, ...) what(x), FMT__FOR_EACH_61(what, __VA_ARGS__)
#define FMT__FOR_EACH_63(what, x, ...) what(x), FMT__FOR_EACH_62(what, __VA_ARGS__)
#define FMT__FOR_EACH_64(what, x, ...) what(x), FMT__FOR_EACH_63(what, __VA_ARGS__)
#define FMT__FOR_EACH_ARG_N( _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, N, ...) N
#define FMT__FOR_EACH_RSEQ_N 64, 63, 62, 61, 60, \
59, 58, 57, 56, 55, 54, 53, 52, 51, 50, \
49, 48, 47, 46, 45, 44, 43, 42, 41, 40, \
39, 38, 37, 36, 35, 34, 33, 32, 31, 30, \
29, 28, 27, 26, 25, 24, 23, 22, 21, 20, \
19, 18, 17, 16, 15, 14, 13, 12, 11, 10, \
9, 8, 7, 6, 5, 4, 3, 2, 1, 0
#define FMT__FOR_EACH_NARG_(...) FMT__FOR_EACH_ARG_N(__VA_ARGS__)
#define FMT__FOR_EACH_NARG(...) FMT__FOR_EACH_NARG_(__VA_ARGS__ __VA_OPT__(,) FMT__FOR_EACH_RSEQ_N)
#define FMT__FOR_EACH_(N, what, ...) \
FMT__CAT(FMT__FOR_EACH_, N)(what __VA_OPT__(,) __VA_ARGS__)
#define FMT__FOR_EACH(what, ...) \
FMT__FOR_EACH_(FMT__FOR_EACH_NARG(__VA_ARGS__), what __VA_OPT__(,) __VA_ARGS__)
/// Returns the number of arguments given.
#define FMT_VA_ARG_COUNT(...) FMT__FOR_EACH_NARG(__VA_ARGS__)
////////////////////////////////////////////////////////////////////////////////
// MARK: - Types IDs
////////////////////////////////////////////////////////////////////////////////
typedef enum {
fmt__TYPE_UNKNOWN,
fmt__TYPE_CHAR,
fmt__TYPE_WCHAR,
fmt__TYPE_SIGNED_CHAR,
fmt__TYPE_SHORT,
fmt__TYPE_INT,
fmt__TYPE_LONG,
fmt__TYPE_LONG_LONG,
fmt__TYPE_UNSIGNED_CHAR,
fmt__TYPE_UNSIGNED_SHORT,
fmt__TYPE_UNSIGNED,
fmt__TYPE_UNSIGNED_LONG,
fmt__TYPE_UNSIGNED_LONG_LONG,
fmt__TYPE_FLOAT,
fmt__TYPE_DOUBLE,
fmt__TYPE_BOOL,
fmt__TYPE_STRING,
fmt__TYPE_STRING_16,
fmt__TYPE_STRING_32,
fmt__TYPE_POINTER,
fmt__TYPE_TIME,
fmt__TYPE_FMT_STRING,
fmt__TYPE_FMT_STRING_TAKE,
fmt__TYPE_ID_COUNT,
} fmt_Type_Id;
static const char *fmt_Type_Names[fmt__TYPE_ID_COUNT] = {
"(unknown)",
"char",
"wchar_t",
"signed char",
"short",
"int",
"long",
"long long",
"unsigned char",
"unsigned short",
"unsigned",
"unsigned long",
"unsigned long long",
"float",
"double",
"bool",
"char *", // shared with `char8_t *`
"char16_t *",
"char32_t *",
"void *",
"struct tm *",
"fmt_String",
"fmt_String",
};
#ifdef __cplusplus
// Need to define fmt_String here so we can use it to overload the type id function.
struct fmt_String_Take {
char *data;
size_t size;
};
struct fmt_String {
union {
fmt_String_Take take;
struct {
char *data;
size_t size;
size_t capacity;
};
};
};
#define FMT_TYPE_ID(_T, _id) \
static constexpr fmt_Type_Id FMT__TYPE_ID([[maybe_unused]] _T _) { return _id; }
FMT_TYPE_ID(char, fmt__TYPE_CHAR);
#ifdef FMT_CPP_CHAR8_DISTINCT
#undef FMT_CPP_CHAR8_DISTINCT
FMT_TYPE_ID(char8_t, fmt__TYPE_STRING);
#endif
FMT_TYPE_ID(char16_t, fmt__TYPE_CHAR);
FMT_TYPE_ID(char32_t, fmt__TYPE_CHAR);
FMT_TYPE_ID(wchar_t, fmt__TYPE_WCHAR);
FMT_TYPE_ID(signed char, fmt__TYPE_SIGNED_CHAR);
FMT_TYPE_ID(short, fmt__TYPE_SHORT);
FMT_TYPE_ID(int, fmt__TYPE_INT);
FMT_TYPE_ID(long, fmt__TYPE_LONG);
FMT_TYPE_ID(long long, fmt__TYPE_LONG_LONG);
FMT_TYPE_ID(unsigned char, fmt__TYPE_UNSIGNED_CHAR);
FMT_TYPE_ID(unsigned short, fmt__TYPE_UNSIGNED_SHORT);
FMT_TYPE_ID(unsigned, fmt__TYPE_UNSIGNED);
FMT_TYPE_ID(unsigned long, fmt__TYPE_UNSIGNED_LONG);
FMT_TYPE_ID(unsigned long long, fmt__TYPE_UNSIGNED_LONG_LONG);
FMT_TYPE_ID(float, fmt__TYPE_FLOAT);
FMT_TYPE_ID(double, fmt__TYPE_DOUBLE);
FMT_TYPE_ID(bool, fmt__TYPE_BOOL);
FMT_TYPE_ID(char *, fmt__TYPE_STRING);
FMT_TYPE_ID(const char *, fmt__TYPE_STRING);
// Note: this is a different type from char*, but since we require all strings
// to be UTF-8 this difference does not matter.
FMT_TYPE_ID(fmt_char8_t *, fmt__TYPE_STRING);
FMT_TYPE_ID(const fmt_char8_t *, fmt__TYPE_STRING);
FMT_TYPE_ID(const fmt_char16_t *, fmt__TYPE_STRING_16);
FMT_TYPE_ID(fmt_char16_t *, fmt__TYPE_STRING_16);
FMT_TYPE_ID(const fmt_char32_t *, fmt__TYPE_STRING_32);
FMT_TYPE_ID(fmt_char32_t *, fmt__TYPE_STRING_32);
#ifdef _WIN32
FMT_TYPE_ID(wchar_t *, fmt__TYPE_STRING_16);
FMT_TYPE_ID(const wchar_t *, fmt__TYPE_STRING_16);
#else
FMT_TYPE_ID(wchar_t *, fmt__TYPE_STRING_32);
FMT_TYPE_ID(const wchar_t *, fmt__TYPE_STRING_32);
#endif
FMT_TYPE_ID(void *, fmt__TYPE_POINTER);
FMT_TYPE_ID(const void *, fmt__TYPE_POINTER);
FMT_TYPE_ID(tm *, fmt__TYPE_TIME);
FMT_TYPE_ID(const tm *, fmt__TYPE_TIME);
FMT_TYPE_ID(fmt_String, fmt__TYPE_FMT_STRING);
FMT_TYPE_ID(fmt_String_Take, fmt__TYPE_FMT_STRING_TAKE);
template<class Else>
FMT_TYPE_ID(Else, fmt__TYPE_UNKNOWN);
#undef FMT_TYPE_ID
#else // __cplusplus
#define FMT__TYPE_ID(x) \
_Generic((x), \
char: fmt__TYPE_CHAR, \
signed char: fmt__TYPE_SIGNED_CHAR, \
short: fmt__TYPE_SHORT, \
int: fmt__TYPE_INT, \
long: fmt__TYPE_LONG, \
long long: fmt__TYPE_LONG_LONG, \
unsigned char: fmt__TYPE_UNSIGNED_CHAR, \
unsigned short: fmt__TYPE_UNSIGNED_SHORT, \
unsigned: fmt__TYPE_UNSIGNED, \
unsigned long: fmt__TYPE_UNSIGNED_LONG, \
unsigned long long: fmt__TYPE_UNSIGNED_LONG_LONG, \
float: fmt__TYPE_FLOAT, \
double: fmt__TYPE_DOUBLE, \
bool: fmt__TYPE_BOOL, \
char *: fmt__TYPE_STRING, \
const char *: fmt__TYPE_STRING, \
fmt_char8_t *: fmt__TYPE_STRING, \
const fmt_char8_t *: fmt__TYPE_STRING, \
const fmt_char16_t *: fmt__TYPE_STRING_16, \
fmt_char16_t *: fmt__TYPE_STRING_16, \
const fmt_char32_t *: fmt__TYPE_STRING_32, \
fmt_char32_t *: fmt__TYPE_STRING_32, \
/* These match wchar_t strings on linux, on windows they are already
matched by fmt_char16_t. */ \
int *: fmt__TYPE_STRING_32, \
const int*: fmt__TYPE_STRING_32, \
void *: fmt__TYPE_POINTER, \
const void *: fmt__TYPE_POINTER, \
struct tm *: fmt__TYPE_TIME, \
const struct tm *: fmt__TYPE_TIME, \
fmt_String: fmt__TYPE_FMT_STRING, \
fmt_String_Take: fmt__TYPE_FMT_STRING_TAKE, \
default: fmt__TYPE_UNKNOWN \
)
#endif // __cplusplus
#define FMT__TYPE_ID_AND_VALUE(x) FMT__TYPE_ID(x), (x)
/// Turns a list of parameters into a list of their type IDs and the parameter:
/// `param1, param2, param3` -> `type1, param1, type2, param2, type3, param3`
#define FMT_ARGS(...) FMT__FOR_EACH(FMT__TYPE_ID_AND_VALUE, __VA_ARGS__)
////////////////////////////////////////////////////////////////////////////////
// MARK: - Writer interface
////////////////////////////////////////////////////////////////////////////////
/// "vtable" for writers
typedef struct fmt_Writer {
/// Writes a single byte
int (*write_byte)(
struct fmt_Writer *self, char byte
);
/// Writes `n` bytes of `data`
int (*write_data)(
struct fmt_Writer *restrict self, const char *restrict data, size_t n
);
/// Writes `str` until a null-byte is encountered
int (*write_str)(
struct fmt_Writer *restrict self, const char *restrict str
);
} fmt_Writer;
typedef struct {
fmt_Writer base;
FILE *stream;
} fmt_Stream_Writer;
typedef struct {
fmt_Writer base;
const char *const string;
char *at;
const char *const end;
} fmt_String_Writer;
#ifndef __cplusplus
/// Proxy type what when passed to a fmt function will cause the `data`
/// member to be free'd after printing it.
typedef struct {
char *data;
size_t size;
} fmt_String_Take;
typedef struct {
union {
fmt_String_Take take;
struct {
char *data;
size_t size;
size_t capacity;
};
};
} fmt_String;
#endif
/// Returns an empty string, does not allocate.
/// Useful for quick and dirty code using `fmt_format`.
///
/// Example
/// -------
/// ```
/// fmt_println("{}", (condition ? fmt_format(...) : fmt_string_new()).take);
/// ```
fmt_String fmt_string_new();
typedef struct {
fmt_Writer base;
fmt_String string;
} fmt_Allocating_String_Writer;
/// Writer that discards the data written but keeps track of the number of
/// bytes, number of codepoints, and display width of the data.
typedef struct {
fmt_Writer base;
size_t bytes;
size_t characters;
size_t width;
} fmt_Metric_Writer;
/// Wraps another writer and only writes up to a limited number of codepoints.
typedef struct {
fmt_Writer base;
fmt_Writer *inner;
int characters_left;
} fmt_Limited_Writer;
/// Buffers the data written to it and only writes it to the inner writer when
/// the buffer is full or when flushed. You must always call `fmt_bw_flush`
/// after using this writer to ensure all data is written. Note that the
/// buffer is rather small and stored in-line inside the struct.
typedef struct {
fmt_Writer base;
union {
fmt_Writer *inner;
// For the case of being used for writing to a stream, so creating an
// extra stream writer and keeping it in scope for the lifetime of the
// buffered writer can be avoided. The `is_stream` flag would likely
// be padding bytes anyways so there is no space overhead.
FILE *stream;
};
char buffer[FMT_BUFFERED_WRITER_CAPACITY];
uint8_t used;
bool is_stream;
} fmt_Buffered_Writer;
/// Default implementation for the writers `write_str` function which just
/// calls `write_data` with the string and its length using `strlen`.
int fmt__write_any_str(fmt_Writer *restrict writer, const char *restrict str);
int fmt__write_stream_byte(fmt_Writer *p_self, char byte);
int fmt__write_stream_data(
fmt_Writer *restrict p_self, const char *restrict data, size_t n
);
int fmt__write_string_byte(fmt_Writer *p_self, char byte);
int fmt__write_string_data(
fmt_Writer *restrict p_self, const char *restrict data, size_t n
);
int fmt__write_alloc_byte(fmt_Writer *p_self, char byte);
int fmt__write_alloc_data(
fmt_Writer *restrict p_self, const char *restrict data, size_t n
);
int fmt__write_metric_byte(fmt_Writer *p_self, char byte);
int fmt__write_metric_data(
fmt_Writer *restrict p_self, const char *restrict data, size_t n
);
int fmt__write_limited_byte(fmt_Writer *p_self, char byte);
int fmt__write_limited_data(
fmt_Writer *restrict p_self, const char *restrict data, size_t n
);
int fmt__write_buffered_byte(fmt_Writer *p_self, char byte);
int fmt__write_buffered_data(
fmt_Writer *restrict p_self, const char *restrict data, size_t n
);
static const fmt_Writer fmt_STREAM_WRITER_FUNCTIONS = {
.write_byte = fmt__write_stream_byte,
.write_data = fmt__write_stream_data,
.write_str = fmt__write_any_str,
};
static const fmt_Writer fmt_STRING_WRITER_FUNCTIONS = {
.write_byte = fmt__write_string_byte,
.write_data = fmt__write_string_data,
.write_str = fmt__write_any_str,
};
static const fmt_Writer fmt_ALLOC_WRITER_FUNCTIONS = {
.write_byte = fmt__write_alloc_byte,
.write_data = fmt__write_alloc_data,
.write_str = fmt__write_any_str,
};
static const fmt_Writer fmt_METRIC_WRITER_FUNCTIONS = {
.write_byte = fmt__write_metric_byte,
.write_data = fmt__write_metric_data,
.write_str = fmt__write_any_str,
};
static const fmt_Writer fmt_LIMITED_WRITER_FUNCTIONS = {
.write_byte = fmt__write_limited_byte,
.write_data = fmt__write_limited_data,
.write_str = fmt__write_any_str,
};
static const fmt_Writer fmt_BUFFERED_WRITER_FUNCTIONS = {
.write_byte = fmt__write_buffered_byte,
.write_data = fmt__write_buffered_data,
.write_str = fmt__write_any_str,
};
// Note: these macros don't work in C++ because you can't take the address of
// an rvalue.
/// Creates a `fmt_Stream_Writer` on the stack and returns it's address as a
/// `fmt_Writer *`.
#define FMT_NEW_STREAM_WRITER(_stream) \
((fmt_Writer *)&(fmt_Stream_Writer){ \
.base = fmt_STREAM_WRITER_FUNCTIONS, \
.stream = (_stream), \
})
/// Creates a `fmt_String_Writer` on the stack and returns it's address as a
/// `fmt_Writer *`.
#define FMT_NEW_STRING_WRITER(_string, _n) \
((fmt_Writer *)&(fmt_String_Writer) { \
.base = fmt_STRING_WRITER_FUNCTIONS, \
.string = (_string), \
.at = (_string), \
.end = (_string) + (_n), \
})
fmt_Stream_Writer fmt_fw_new(FILE *stream);
fmt_String_Writer fmt_sw_new(char *string, size_t n);
fmt_Allocating_String_Writer fmt_aw_new();
fmt_String fmt_aw_finish(fmt_Allocating_String_Writer writer);
fmt_Buffered_Writer fmt_bw_new(fmt_Writer *inner);
fmt_Buffered_Writer fmt_bw_new_stream(FILE *stream);
void fmt_bw_flush(fmt_Buffered_Writer *bw);
////////////////////////////////////////////////////////////////////////////////
// MARK: - Core functions
////////////////////////////////////////////////////////////////////////////////
extern void fmt_init_threading();
/// The core function of this library that all other formatting functions/macros
/// eventually call.
///
/// `ap` should contain pairs of type IDs and arguments.
/// `arg_count` is the number of those pairs.
extern int fmt_va_write(
fmt_Writer *restrict writer,
const char *restrict format,
int arg_count,
va_list ap
);
/// Implementation for the `fmt_write` macro.
///
/// Examples:
/// ```c
/// FILE *log_file = fopen("log.txt", "w");
/// fmt_Writer *log_writer = FMT_NEW_STREAM_WRITER(log_file);
/// #define log(_format, ...)
/// fmt__write(
/// log_writer,
/// "{}: " _format,
/// 1 + FMT_VA_ARG_COUNT(__VA_ARGS__),
/// current_time()
/// __VA_OPT__(, FMT_ARGS(__VA_ARGS__)
/// )
///
/// log("something {}", "happened");
/// ```
/// (note that this macro would need backslashes for multiple lines).
extern int fmt__write(
fmt_Writer *restrict writer, const char *restrict format, int arg_count, ...
);
/// Implementation for the stdout and stderr printers which handles locking and
/// adding a newline for the `-ln` variants. It takes a stream because stdout
/// and stderr are not const so we can't easily use static variables for them.
extern int fmt__std_print(
FILE *restrict stream,
const char *restrict format,
bool newline,
int arg_count,
...
);
/// Implementation for the `fmt_panic` macro.
FMT__NORETURN extern void fmt__panic(
const char *restrict file,
int line,
const char *restrict format,
int arg_count,
...
);
/// Like fmt__format but takes a `va_list`, see `fmt_va_write`.
extern fmt_String fmt_va_format(const char *format, int arg_count, va_list ap);
/// Implementation for the `fmt_format` macro.
extern fmt_String fmt__format(const char *format, int arg_count, ...);
/// Like fmt__sprint but takes a `va_list`, see `fmt_va_write`.
extern int fmt_va_sprint(
char *restrict string,
size_t size,
const char *restrict format,
int arg_count,
va_list ap
);
/// Implementation for the `fmt_sprint` macro, this only exists for C++ builds
/// which prevent usage of the `FMT_NEW_STRING_WRITER` macro.
extern int fmt__sprint(
char *restrict string,
size_t size,
const char *restrict format,
int arg_count,
...
);
/// Like fmt__fprint but takes a `va_list`, see `fmt_va_write`.
extern int fmt_va_fprint(
FILE *restrict stream, const char *restrict format, int arg_count, va_list ap
);
/// Implementation for the `fmt_fprint` macro.
extern int fmt__fprint(
FILE *restrict stream, const char *restrict format, int arg_count, ...
);
/// Formats the broken-down time `datetime` per the specified format string.
extern int fmt_write_time(
fmt_Writer *restrict writer,
const char *restrict format,
const struct tm *restrict datetime
);
/// Like `fmt_write_time` with a string writer, but unlike `fmt_write_time` this
/// will also add a null terminator.
extern int fmt_format_time_to(
char *restrict buf,
size_t size,
const char *restrict format,
const struct tm *restrict datetime
);
/// Like `fmt_write_time` but writes to an allocated string.
extern fmt_String fmt_format_time(
const char *restrict format, const struct tm *restrict datetime
);
/// Translates a strftime format string into the fmt time format.
///
/// The format specifiers introduced by glibc are not supported.
///
/// Modifiers ('E' and 'O') are not supported (fields can only be 1 character).
extern void fmt_translate_strftime(
const char *restrict strftime, char *restrict translated, int size
);
////////////////////////////////////////////////////////////////////////////////
// MARK: - Macros
// User-facing wrapper macros
////////////////////////////////////////////////////////////////////////////////
/// Returns `true` if the given variable can be printed.
#define fmt_can_print(x) (FMT__TYPE_ID(x) != fmt__TYPE_UNKNOWN)
/// Writes formatted data into a buffer.
///
/// This macro accepts a ‘writer’, a format string, and a list of arguments.
/// Arguments will be formatted according to the specified format string and the
/// result will be passed to the writer.
///
/// If you already have a `va_list` with the type IDs and arguments use
/// `fmt_va_write` instead.
///
/// Examples:
/// ```c
/// FILE *outf = fopen("output.txt", "w");
/// fmt_Writer *writer = FMT_NEW_STREAM_WRITER(outf);
/// fmt_write(writer, "test");
/// fmt_write(writer, "formatted {}", "arguments");
/// // File content: "testformatted arguments"
/// ```
#define fmt_write(_writer, _format, ...) \
fmt__write( \
_writer, \
_format, \
FMT_VA_ARG_COUNT(__VA_ARGS__) \
__VA_OPT__(, FMT_ARGS(__VA_ARGS__)) \
)
/// Prints to the standard output.
///
/// Equivalent to the fmt_println macro except that a newline is not printed at the
/// end of the message.
///
/// Note that stdout is frequently line-buffered by default so it may be
/// necessary to use `fflush(stdout)` to ensure the output is emitted
/// immediately.
///
/// If `FMT_LOCKED_DEFAULT_PRINTERS` is defined this will lock the internal
/// mutex on each call.
///
/// Use `fmt_print` only for the primary output of your program. Use
/// `fmt_eprint` instead to print error and progress messages.
///
/// Examples:
/// ```c
/// fmt_print("this ");
/// fmt_print("will ");
/// fmt_print("be ");
/// fmt_print("on ");
/// fmt_print("the ");
/// fmt_print("same ");
/// fmt_print("line ");
/// fflush(stdout);
///
/// fmt_print("this string has a newline, why not choose fmt_println instead?\n");
/// fflush(stdout);
/// ```
#define fmt_print(_format, ...) \
fmt__std_print( \
stdout, \
_format, \
false, \
FMT_VA_ARG_COUNT(__VA_ARGS__) \
__VA_OPT__(, FMT_ARGS(__VA_ARGS__)) \
)
/// Prints to the standard output, with a newline.
///
/// On all platforms, the newline is the LINE FEED character (`\n`/`U+000A`)
/// alone (no additional CARRIAGE RETURN (`\r`/`U+000D`)).
///
/// If `FMT_LOCKED_DEFAULT_PRINTERS` is defined this will lock the internal
/// mutex on each call.
///
/// Examples:
/// ```c
/// fmt_println("hello there!");
/// fmt_println("format {} arguments", "some");
/// const char *variable = "some";
/// fmt_println("format {} arguments", variable);
/// ```
#define fmt_println(_format, ...) \
fmt__std_print( \
stdout, \
_format, \
true, \
FMT_VA_ARG_COUNT(__VA_ARGS__) \
__VA_OPT__(, FMT_ARGS(__VA_ARGS__)) \
)
/// Prints to the standard error.
///
/// Equivalent to the `fmt_print` macro, except that output goes to `stderr`
/// instead of `stdout`. See `fmt_print` for example usage.
///
/// Use `fmt_eprint` only for error and progress messages. Use `fmt_print`
/// instead for the primary output of your program.
#define fmt_eprint(_format, ...) \
fmt__std_print( \
stderr, \
_format, \
false, \
FMT_VA_ARG_COUNT(__VA_ARGS__) \
__VA_OPT__(, FMT_ARGS(__VA_ARGS__)) \
)
/// Prints to the standard error, with a newline.
///
/// Equivalent to the `fmt_println` macro, except that output goes to `stderr`
/// instead of `stdout`. See `fmt_println` for example usage.
///
/// Use `fmt_eprintln` only for error and progress messages. Use `fmt_println`
/// instead for the primary output of your program.
#define fmt_eprintln(_format, ...) \
fmt__std_print( \
stderr, \
_format, \
true, \
FMT_VA_ARG_COUNT(__VA_ARGS__) \
__VA_OPT__(, FMT_ARGS(__VA_ARGS__)) \
)
/// Accumulates formatted data in an allocated string.
///
/// String structure:
/// ```c
/// typedef struct {
/// char *data;
/// size_t capacity;
/// size_t size;
/// } fmt_String;
/// ```
///
/// The `data` field must be released using `free`.
/// The `capacity` field contains the amount of usable allocated data, actual
/// allocated size is 1 more byte which is not considered as part of the
/// capacity so it can always contain the null terminator without needing to
/// reallocate just to add it.
/// The `size` field contains the length of the formatted text (without the null
/// terminator).
#define fmt_format(_format, ...) \
fmt__format( \
_format, \
FMT_VA_ARG_COUNT(__VA_ARGS__) \
__VA_OPT__(, FMT_ARGS(__VA_ARGS__)) \
)
/// Writes formatted data into an existing buffer.
/// Panics if more than `n - 1` characters are required, as null terminator is
/// always added after the formatted data.
///
/// Examples:
/// ```c
/// char buffer[16];
/// fmt_sprint(buffer, 16, "Hello {}", "World");
/// assert(memcmp(buffer, "Hello World", 12) == 0);
/// ```
#define fmt_sprint(_string, _n, _format, ...) \
fmt__sprint( \
(_string), \
(_n), \
_format, \
FMT_VA_ARG_COUNT(__VA_ARGS__) \
__VA_OPT__(, FMT_ARGS(__VA_ARGS__)) \
)
/// Convenience function for writing to a file.
///
/// Unlike the default functions for writing to stdout and stderr this is not
/// affected by `FMT_LOCKED_DEFAULT_PRINTERS`.
///
/// Equivalent to:
/// ```c
/// fmt_Writer *writer = FMT_NEW_STREAM_WRITER(stream);
/// fmt_write(writer, format, ...);
/// ```
///
/// Examples:
/// ```c
/// FILE *log_file = fopen("log.txt", "w");
/// fmt_fprint(log_file, "Hello {}\n", "World");
/// ```
#define fmt_fprint(_stream, _format, ...) \
fmt__fprint( \
_stream, \
_format, \
FMT_VA_ARG_COUNT(__VA_ARGS__) \
__VA_OPT__(, FMT_ARGS(__VA_ARGS__)) \
)
/// Aborts the program with an error message.
///
/// The error message will look like this: `file:line: message[\n]`.
/// The newline is only added if the message does not already end with a newline.
///
/// The message is formatted as with all the other macros.
///
/// Examples:
/// ```c
/// fmt_panic("this is a terrible mistake!");
/// fmt_panic("this is a {} {}", "fancy", "message");
/// ```
#define fmt_panic(_format, ...) \
fmt__panic( \
__FILE__, \
__LINE__, \
_format, \
FMT_VA_ARG_COUNT(__VA_ARGS__) \
__VA_OPT__(, FMT_ARGS(__VA_ARGS__)) \
)
/// Indicates unimplemented code by panicking with a message of “not implemented”.
///
/// Wraps `fmt_panic`.
///
/// Examples:
/// ```c
/// fmt_todo();
/// fmt_todo("string");
/// fmt_todo("format {}", 123);
/// ```
#define fmt_todo(...) \
fmt_panic("not yet implemented" __VA_OPT__(": ") __VA_ARGS__)
/// Indicates unfinished code.
///
/// Wraps `fmt_panic`.
///
/// Examples:
/// ```c
/// fmt_unimplemented();
/// fmt_unimplemented("string");
/// fmt_unimplemented("format {}", 123);
/// ```