aboutsummaryrefslogtreecommitdiff
path: root/lib/std/fmt.zig
blob: 81d7ce588d0fc2a4ba5301fab3920f97b9e3fdd1 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
// SPDX-License-Identifier: MIT
// Copyright (c) 2015-2020 Zig Contributors
// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
// The MIT license requires this copyright notice to be included in all copies
// and substantial portions of the software.
const std = @import("std.zig");
const math = std.math;
const assert = std.debug.assert;
const mem = std.mem;
const builtin = @import("builtin");
const errol = @import("fmt/errol.zig");
const lossyCast = std.math.lossyCast;

pub const default_max_depth = 3;

pub const Alignment = enum {
    Left,
    Center,
    Right,
};

pub const FormatOptions = struct {
    precision: ?usize = null,
    width: ?usize = null,
    alignment: Alignment = .Right,
    fill: u8 = ' ',
};

fn peekIsAlign(comptime fmt: []const u8) bool {
    // Should only be called during a state transition to the format segment.
    comptime assert(fmt[0] == ':');

    inline for (([_]u8{ 1, 2 })[0..]) |i| {
        if (fmt.len > i and (fmt[i] == '<' or fmt[i] == '^' or fmt[i] == '>')) {
            return true;
        }
    }
    return false;
}

/// Renders fmt string with args, calling output with slices of bytes.
/// If `output` returns an error, the error is returned from `format` and
/// `output` is not called again.
///
/// The format string must be comptime known and may contain placeholders following
/// this format:
/// `{[position][specifier]:[fill][alignment][width].[precision]}`
///
/// Each word between `[` and `]` is a parameter you have to replace with something:
///
/// - *position* is the index of the argument that should be inserted
/// - *specifier* is a type-dependent formatting option that determines how a type should formatted (see below)
/// - *fill* is a single character which is used to pad the formatted text
/// - *alignment* is one of the three characters `<`, `^` or `>`. they define if the text is *left*, *center*, or *right* aligned
/// - *width* is the total width of the field in characters
/// - *precision* specifies how many decimals a formatted number should have
///
/// Note that most of the parameters are optional and may be omitted. Also you can leave out separators like `:` and `.` when
/// all parameters after the separator are omitted.
/// Only exception is the *fill* parameter. If *fill* is required, one has to specify *alignment* as well, as otherwise
/// the digits after `:` is interpreted as *width*, not *fill*.
///
/// The *specifier* has several options for types:
/// - `x` and `X`:
///   - format the non-numeric value as a string of bytes in hexadecimal notation ("binary dump") in either lower case or upper case
///   - output numeric value in hexadecimal notation
/// - `s`:
///   - for pointer-to-many and C pointers of u8, print as a C-string using zero-termination
///   - for slices of u8, print the entire slice as a string without zero-termination
/// - `z`: escape the string with @"" syntax if it is not a valid Zig identifier.
/// - `Z`: print the string escaping non-printable characters using Zig escape sequences.
/// - `B` and `Bi`: output a memory size in either metric (1000) or power-of-two (1024) based notation. works for both float and integer values.
/// - `e` and `E`: if printing a string, escape non-printable characters
/// - `e`: output floating point value in scientific notation
/// - `d`: output numeric value in decimal notation
/// - `b`: output integer value in binary notation
/// - `o`: output integer value in octal notation
/// - `c`: output integer as an ASCII character. Integer type must have 8 bits at max.
/// - `*`: output the address of the value instead of the value itself.
///
/// If a formatted user type contains a function of the type
/// ```
/// pub fn format(value: ?, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void
/// ```
/// with `?` being the type formatted, this function will be called instead of the default implementation.
/// This allows user types to be formatted in a logical manner instead of dumping all fields of the type.
///
/// A user type may be a `struct`, `vector`, `union` or `enum` type.
///
/// To print literal curly braces, escape them by writing them twice, e.g. `{{` or `}}`.
pub fn format(
    writer: anytype,
    comptime fmt: []const u8,
    args: anytype,
) !void {
    const ArgSetType = u32;
    if (@typeInfo(@TypeOf(args)) != .Struct) {
        @compileError("Expected tuple or struct argument, found " ++ @typeName(@TypeOf(args)));
    }
    if (args.len > @typeInfo(ArgSetType).Int.bits) {
        @compileError("32 arguments max are supported per format call");
    }

    const State = enum {
        Start,
        Positional,
        CloseBrace,
        Specifier,
        FormatFillAndAlign,
        FormatWidth,
        FormatPrecision,
    };

    comptime var start_index = 0;
    comptime var state = State.Start;
    comptime var maybe_pos_arg: ?comptime_int = null;
    comptime var specifier_start = 0;
    comptime var specifier_end = 0;
    comptime var options = FormatOptions{};
    comptime var arg_state: struct {
        next_arg: usize = 0,
        used_args: ArgSetType = 0,
        args_len: usize = args.len,

        fn hasUnusedArgs(comptime self: *@This()) bool {
            return (@popCount(ArgSetType, self.used_args) != self.args_len);
        }

        fn nextArg(comptime self: *@This(), comptime pos_arg: ?comptime_int) comptime_int {
            const next_idx = pos_arg orelse blk: {
                const arg = self.next_arg;
                self.next_arg += 1;
                break :blk arg;
            };

            if (next_idx >= self.args_len) {
                @compileError("Too few arguments");
            }

            // Mark this argument as used
            self.used_args |= 1 << next_idx;

            return next_idx;
        }
    } = .{};

    inline for (fmt) |c, i| {
        switch (state) {
            .Start => switch (c) {
                '{' => {
                    if (start_index < i) {
                        try writer.writeAll(fmt[start_index..i]);
                    }

                    start_index = i;
                    specifier_start = i + 1;
                    specifier_end = i + 1;
                    maybe_pos_arg = null;
                    state = .Positional;
                    options = FormatOptions{};
                },
                '}' => {
                    if (start_index < i) {
                        try writer.writeAll(fmt[start_index..i]);
                    }
                    state = .CloseBrace;
                },
                else => {},
            },
            .Positional => switch (c) {
                '{' => {
                    state = .Start;
                    start_index = i;
                },
                ':' => {
                    state = if (comptime peekIsAlign(fmt[i..])) State.FormatFillAndAlign else State.FormatWidth;
                    specifier_end = i;
                },
                '0'...'9' => {
                    if (maybe_pos_arg == null) {
                        maybe_pos_arg = 0;
                    }

                    maybe_pos_arg.? *= 10;
                    maybe_pos_arg.? += c - '0';
                    specifier_start = i + 1;

                    if (maybe_pos_arg.? >= args.len) {
                        @compileError("Positional value refers to non-existent argument");
                    }
                },
                '}' => {
                    const arg_to_print = comptime arg_state.nextArg(maybe_pos_arg);

                    try formatType(
                        args[arg_to_print],
                        fmt[0..0],
                        options,
                        writer,
                        default_max_depth,
                    );

                    state = .Start;
                    start_index = i + 1;
                },
                else => {
                    state = .Specifier;
                    specifier_start = i;
                },
            },
            .CloseBrace => switch (c) {
                '}' => {
                    state = .Start;
                    start_index = i;
                },
                else => @compileError("Single '}' encountered in format string"),
            },
            .Specifier => switch (c) {
                ':' => {
                    specifier_end = i;
                    state = if (comptime peekIsAlign(fmt[i..])) State.FormatFillAndAlign else State.FormatWidth;
                },
                '}' => {
                    const arg_to_print = comptime arg_state.nextArg(maybe_pos_arg);

                    try formatType(
                        args[arg_to_print],
                        fmt[specifier_start..i],
                        options,
                        writer,
                        default_max_depth,
                    );
                    state = .Start;
                    start_index = i + 1;
                },
                else => {},
            },
            // Only entered if the format string contains a fill/align segment.
            .FormatFillAndAlign => switch (c) {
                '<' => {
                    options.alignment = Alignment.Left;
                    state = .FormatWidth;
                },
                '^' => {
                    options.alignment = Alignment.Center;
                    state = .FormatWidth;
                },
                '>' => {
                    options.alignment = Alignment.Right;
                    state = .FormatWidth;
                },
                else => {
                    options.fill = c;
                },
            },
            .FormatWidth => switch (c) {
                '0'...'9' => {
                    if (options.width == null) {
                        options.width = 0;
                    }

                    options.width.? *= 10;
                    options.width.? += c - '0';
                },
                '.' => {
                    state = .FormatPrecision;
                },
                '}' => {
                    const arg_to_print = comptime arg_state.nextArg(maybe_pos_arg);

                    try formatType(
                        args[arg_to_print],
                        fmt[specifier_start..specifier_end],
                        options,
                        writer,
                        default_max_depth,
                    );
                    state = .Start;
                    start_index = i + 1;
                },
                else => {
                    @compileError("Unexpected character in width value: " ++ [_]u8{c});
                },
            },
            .FormatPrecision => switch (c) {
                '0'...'9' => {
                    if (options.precision == null) {
                        options.precision = 0;
                    }

                    options.precision.? *= 10;
                    options.precision.? += c - '0';
                },
                '}' => {
                    const arg_to_print = comptime arg_state.nextArg(maybe_pos_arg);

                    try formatType(
                        args[arg_to_print],
                        fmt[specifier_start..specifier_end],
                        options,
                        writer,
                        default_max_depth,
                    );
                    state = .Start;
                    start_index = i + 1;
                },
                else => {
                    @compileError("Unexpected character in precision value: " ++ [_]u8{c});
                },
            },
        }
    }
    comptime {
        if (comptime arg_state.hasUnusedArgs()) {
            @compileError("Unused arguments");
        }
        if (state != State.Start) {
            @compileError("Incomplete format string: " ++ fmt);
        }
    }
    if (start_index < fmt.len) {
        try writer.writeAll(fmt[start_index..]);
    }
}

pub fn formatType(
    value: anytype,
    comptime fmt: []const u8,
    options: FormatOptions,
    writer: anytype,
    max_depth: usize,
) @TypeOf(writer).Error!void {
    if (comptime std.mem.eql(u8, fmt, "*")) {
        try writer.writeAll(@typeName(std.meta.Child(@TypeOf(value))));
        try writer.writeAll("@");
        try formatInt(@ptrToInt(value), 16, false, FormatOptions{}, writer);
        return;
    }

    const T = @TypeOf(value);
    if (comptime std.meta.trait.hasFn("format")(T)) {
        return try value.format(fmt, options, writer);
    }

    switch (@typeInfo(T)) {
        .ComptimeInt, .Int, .ComptimeFloat, .Float => {
            return formatValue(value, fmt, options, writer);
        },
        .Void => {
            return formatBuf("void", options, writer);
        },
        .Bool => {
            return formatBuf(if (value) "true" else "false", options, writer);
        },
        .Optional => {
            if (value) |payload| {
                return formatType(payload, fmt, options, writer, max_depth);
            } else {
                return formatBuf("null", options, writer);
            }
        },
        .ErrorUnion => {
            if (value) |payload| {
                return formatType(payload, fmt, options, writer, max_depth);
            } else |err| {
                return formatType(err, fmt, options, writer, max_depth);
            }
        },
        .ErrorSet => {
            try writer.writeAll("error.");
            return writer.writeAll(@errorName(value));
        },
        .Enum => |enumInfo| {
            try writer.writeAll(@typeName(T));
            if (enumInfo.is_exhaustive) {
                try writer.writeAll(".");
                try writer.writeAll(@tagName(value));
                return;
            }

            // Use @tagName only if value is one of known fields
            @setEvalBranchQuota(3 * enumInfo.fields.len);
            inline for (enumInfo.fields) |enumField| {
                if (@enumToInt(value) == enumField.value) {
                    try writer.writeAll(".");
                    try writer.writeAll(@tagName(value));
                    return;
                }
            }

            try writer.writeAll("(");
            try formatType(@enumToInt(value), fmt, options, writer, max_depth);
            try writer.writeAll(")");
        },
        .Union => {
            try writer.writeAll(@typeName(T));
            if (max_depth == 0) {
                return writer.writeAll("{ ... }");
            }
            const info = @typeInfo(T).Union;
            if (info.tag_type) |UnionTagType| {
                try writer.writeAll("{ .");
                try writer.writeAll(@tagName(@as(UnionTagType, value)));
                try writer.writeAll(" = ");
                inline for (info.fields) |u_field| {
                    if (value == @field(UnionTagType, u_field.name)) {
                        try formatType(@field(value, u_field.name), fmt, options, writer, max_depth - 1);
                    }
                }
                try writer.writeAll(" }");
            } else {
                try format(writer, "@{x}", .{@ptrToInt(&value)});
            }
        },
        .Struct => |StructT| {
            try writer.writeAll(@typeName(T));
            if (max_depth == 0) {
                return writer.writeAll("{ ... }");
            }
            try writer.writeAll("{");
            inline for (StructT.fields) |f, i| {
                if (i == 0) {
                    try writer.writeAll(" .");
                } else {
                    try writer.writeAll(", .");
                }
                try writer.writeAll(f.name);
                try writer.writeAll(" = ");
                try formatType(@field(value, f.name), fmt, options, writer, max_depth - 1);
            }
            try writer.writeAll(" }");
        },
        .Pointer => |ptr_info| switch (ptr_info.size) {
            .One => switch (@typeInfo(ptr_info.child)) {
                .Array => |info| {
                    if (info.child == u8) {
                        return formatText(value, fmt, options, writer);
                    }
                    return format(writer, "{}@{x}", .{ @typeName(@typeInfo(T).Pointer.child), @ptrToInt(value) });
                },
                .Enum, .Union, .Struct => {
                    return formatType(value.*, fmt, options, writer, max_depth);
                },
                else => return format(writer, "{}@{x}", .{ @typeName(@typeInfo(T).Pointer.child), @ptrToInt(value) }),
            },
            .Many, .C => {
                if (ptr_info.sentinel) |sentinel| {
                    return formatType(mem.span(value), fmt, options, writer, max_depth);
                }
                if (ptr_info.child == u8) {
                    if (fmt.len > 0 and fmt[0] == 's') {
                        return formatText(mem.span(value), fmt, options, writer);
                    }
                }
                return format(writer, "{}@{x}", .{ @typeName(@typeInfo(T).Pointer.child), @ptrToInt(value) });
            },
            .Slice => {
                if (fmt.len > 0 and ((fmt[0] == 'x') or (fmt[0] == 'X'))) {
                    return formatText(value, fmt, options, writer);
                }
                if (ptr_info.child == u8) {
                    return formatText(value, fmt, options, writer);
                }
                return format(writer, "{}@{x}", .{ @typeName(ptr_info.child), @ptrToInt(value.ptr) });
            },
        },
        .Array => |info| {
            const Slice = @Type(builtin.TypeInfo{
                .Pointer = .{
                    .size = .Slice,
                    .is_const = true,
                    .is_volatile = false,
                    .is_allowzero = false,
                    .alignment = @alignOf(info.child),
                    .child = info.child,
                    .sentinel = null,
                },
            });
            return formatType(@as(Slice, &value), fmt, options, writer, max_depth);
        },
        .Vector => {
            const len = @typeInfo(T).Vector.len;
            try writer.writeAll("{ ");
            var i: usize = 0;
            while (i < len) : (i += 1) {
                try formatValue(value[i], fmt, options, writer);
                if (i < len - 1) {
                    try writer.writeAll(", ");
                }
            }
            try writer.writeAll(" }");
        },
        .Fn => {
            return format(writer, "{}@{x}", .{ @typeName(T), @ptrToInt(value) });
        },
        .Type => return writer.writeAll(@typeName(T)),
        .EnumLiteral => {
            const buffer = [_]u8{'.'} ++ @tagName(value);
            return formatType(buffer, fmt, options, writer, max_depth);
        },
        .Null => return formatBuf("null", options, writer),
        else => @compileError("Unable to format type '" ++ @typeName(T) ++ "'"),
    }
}

fn formatValue(
    value: anytype,
    comptime fmt: []const u8,
    options: FormatOptions,
    writer: anytype,
) !void {
    if (comptime std.mem.eql(u8, fmt, "B")) {
        return formatBytes(value, options, 1000, writer);
    } else if (comptime std.mem.eql(u8, fmt, "Bi")) {
        return formatBytes(value, options, 1024, writer);
    }

    const T = @TypeOf(value);
    switch (@typeInfo(T)) {
        .Float, .ComptimeFloat => return formatFloatValue(value, fmt, options, writer),
        .Int, .ComptimeInt => return formatIntValue(value, fmt, options, writer),
        .Bool => return formatBuf(if (value) "true" else "false", options, writer),
        else => comptime unreachable,
    }
}

pub fn formatIntValue(
    value: anytype,
    comptime fmt: []const u8,
    options: FormatOptions,
    writer: anytype,
) !void {
    comptime var radix = 10;
    comptime var uppercase = false;

    const int_value = if (@TypeOf(value) == comptime_int) blk: {
        const Int = math.IntFittingRange(value, value);
        break :blk @as(Int, value);
    } else
        value;

    if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "d")) {
        radix = 10;
        uppercase = false;
    } else if (comptime std.mem.eql(u8, fmt, "c")) {
        if (@typeInfo(@TypeOf(int_value)).Int.bits <= 8) {
            return formatAsciiChar(@as(u8, int_value), options, writer);
        } else {
            @compileError("Cannot print integer that is larger than 8 bits as a ascii");
        }
    } else if (comptime std.mem.eql(u8, fmt, "Z")) {
        if (@typeInfo(@TypeOf(int_value)).Int.bits <= 8) {
            const c: u8 = int_value;
            return formatZigEscapes(@as(*const [1]u8, &c), options, writer);
        } else {
            @compileError("Cannot escape character with more than 8 bits");
        }
    } else if (comptime std.mem.eql(u8, fmt, "b")) {
        radix = 2;
        uppercase = false;
    } else if (comptime std.mem.eql(u8, fmt, "x")) {
        radix = 16;
        uppercase = false;
    } else if (comptime std.mem.eql(u8, fmt, "X")) {
        radix = 16;
        uppercase = true;
    } else if (comptime std.mem.eql(u8, fmt, "o")) {
        radix = 8;
        uppercase = false;
    } else {
        @compileError("Unknown format string: '" ++ fmt ++ "'");
    }

    return formatInt(int_value, radix, uppercase, options, writer);
}

fn formatFloatValue(
    value: anytype,
    comptime fmt: []const u8,
    options: FormatOptions,
    writer: anytype,
) !void {
    // this buffer should be enough to display all decimal places of a decimal f64 number.
    var buf: [512]u8 = undefined;
    var buf_stream = std.io.fixedBufferStream(&buf);

    if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "e")) {
        formatFloatScientific(value, options, buf_stream.writer()) catch |err| switch (err) {
            error.NoSpaceLeft => unreachable,
            else => |e| return e,
        };
    } else if (comptime std.mem.eql(u8, fmt, "d")) {
        formatFloatDecimal(value, options, buf_stream.writer()) catch |err| switch (err) {
            error.NoSpaceLeft => unreachable,
            else => |e| return e,
        };
    } else {
        @compileError("Unknown format string: '" ++ fmt ++ "'");
    }

    return formatBuf(buf_stream.getWritten(), options, writer);
}

pub fn formatText(
    bytes: []const u8,
    comptime fmt: []const u8,
    options: FormatOptions,
    writer: anytype,
) !void {
    if (comptime std.mem.eql(u8, fmt, "s") or (fmt.len == 0)) {
        return formatBuf(bytes, options, writer);
    } else if (comptime (std.mem.eql(u8, fmt, "x") or std.mem.eql(u8, fmt, "X"))) {
        for (bytes) |c| {
            try formatInt(c, 16, fmt[0] == 'X', FormatOptions{ .width = 2, .fill = '0' }, writer);
        }
        return;
    } else if (comptime (std.mem.eql(u8, fmt, "e") or std.mem.eql(u8, fmt, "E"))) {
        for (bytes) |c| {
            if (std.ascii.isPrint(c)) {
                try writer.writeByte(c);
            } else {
                try writer.writeAll("\\x");
                try formatInt(c, 16, fmt[0] == 'E', FormatOptions{ .width = 2, .fill = '0' }, writer);
            }
        }
        return;
    } else if (comptime std.mem.eql(u8, fmt, "z")) {
        return formatZigIdentifier(bytes, options, writer);
    } else if (comptime std.mem.eql(u8, fmt, "Z")) {
        return formatZigEscapes(bytes, options, writer);
    } else {
        @compileError("Unknown format string: '" ++ fmt ++ "'");
    }
}

pub fn formatAsciiChar(
    c: u8,
    options: FormatOptions,
    writer: anytype,
) !void {
    return writer.writeAll(@as(*const [1]u8, &c));
}

pub fn formatBuf(
    buf: []const u8,
    options: FormatOptions,
    writer: anytype,
) !void {
    const width = options.width orelse buf.len;
    const padding = if (width > buf.len) (width - buf.len) else 0;

    switch (options.alignment) {
        .Left => {
            try writer.writeAll(buf);
            try writer.writeByteNTimes(options.fill, padding);
        },
        .Center => {
            const left_padding = padding / 2;
            const right_padding = (padding + 1) / 2;
            try writer.writeByteNTimes(options.fill, left_padding);
            try writer.writeAll(buf);
            try writer.writeByteNTimes(options.fill, right_padding);
        },
        .Right => {
            try writer.writeByteNTimes(options.fill, padding);
            try writer.writeAll(buf);
        },
    }
}

/// Print the string as a Zig identifier escaping it with @"" syntax if needed.
pub fn formatZigIdentifier(
    bytes: []const u8,
    options: FormatOptions,
    writer: anytype,
) !void {
    if (isValidZigIdentifier(bytes)) {
        return writer.writeAll(bytes);
    }
    try writer.writeAll("@\"");
    try formatZigEscapes(bytes, options, writer);
    try writer.writeByte('"');
}

fn isValidZigIdentifier(bytes: []const u8) bool {
    for (bytes) |c, i| {
        switch (c) {
            '_', 'a'...'z', 'A'...'Z' => {},
            '0'...'9' => if (i == 0) return false,
            else => return false,
        }
    }
    return std.zig.Token.getKeyword(bytes) == null;
}

pub fn formatZigEscapes(
    bytes: []const u8,
    options: FormatOptions,
    writer: anytype,
) !void {
    for (bytes) |byte| switch (byte) {
        '\n' => try writer.writeAll("\\n"),
        '\r' => try writer.writeAll("\\r"),
        '\t' => try writer.writeAll("\\t"),
        '\\' => try writer.writeAll("\\\\"),
        '"' => try writer.writeAll("\\\""),
        '\'' => try writer.writeAll("\\'"),
        ' ', '!', '#'...'&', '('...'[', ']'...'~' => try writer.writeByte(byte),
        // Use hex escapes for rest any unprintable characters.
        else => {
            try writer.writeAll("\\x");
            try formatInt(byte, 16, false, .{ .width = 2, .fill = '0' }, writer);
        },
    };
}

/// Print a float in scientific notation to the specified precision. Null uses full precision.
/// It should be the case that every full precision, printed value can be re-parsed back to the
/// same type unambiguously.
pub fn formatFloatScientific(
    value: anytype,
    options: FormatOptions,
    writer: anytype,
) !void {
    var x = @floatCast(f64, value);

    // Errol doesn't handle these special cases.
    if (math.signbit(x)) {
        try writer.writeAll("-");
        x = -x;
    }

    if (math.isNan(x)) {
        return writer.writeAll("nan");
    }
    if (math.isPositiveInf(x)) {
        return writer.writeAll("inf");
    }
    if (x == 0.0) {
        try writer.writeAll("0");

        if (options.precision) |precision| {
            if (precision != 0) {
                try writer.writeAll(".");
                var i: usize = 0;
                while (i < precision) : (i += 1) {
                    try writer.writeAll("0");
                }
            }
        } else {
            try writer.writeAll(".0");
        }

        try writer.writeAll("e+00");
        return;
    }

    var buffer: [32]u8 = undefined;
    var float_decimal = errol.errol3(x, buffer[0..]);

    if (options.precision) |precision| {
        errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Scientific);

        try writer.writeAll(float_decimal.digits[0..1]);

        // {e0} case prints no `.`
        if (precision != 0) {
            try writer.writeAll(".");

            var printed: usize = 0;
            if (float_decimal.digits.len > 1) {
                const num_digits = math.min(float_decimal.digits.len, precision + 1);
                try writer.writeAll(float_decimal.digits[1..num_digits]);
                printed += num_digits - 1;
            }

            while (printed < precision) : (printed += 1) {
                try writer.writeAll("0");
            }
        }
    } else {
        try writer.writeAll(float_decimal.digits[0..1]);
        try writer.writeAll(".");
        if (float_decimal.digits.len > 1) {
            const num_digits = if (@TypeOf(value) == f32) math.min(@as(usize, 9), float_decimal.digits.len) else float_decimal.digits.len;

            try writer.writeAll(float_decimal.digits[1..num_digits]);
        } else {
            try writer.writeAll("0");
        }
    }

    try writer.writeAll("e");
    const exp = float_decimal.exp - 1;

    if (exp >= 0) {
        try writer.writeAll("+");
        if (exp > -10 and exp < 10) {
            try writer.writeAll("0");
        }
        try formatInt(exp, 10, false, FormatOptions{ .width = 0 }, writer);
    } else {
        try writer.writeAll("-");
        if (exp > -10 and exp < 10) {
            try writer.writeAll("0");
        }
        try formatInt(-exp, 10, false, FormatOptions{ .width = 0 }, writer);
    }
}

/// Print a float of the format x.yyyyy where the number of y is specified by the precision argument.
/// By default floats are printed at full precision (no rounding).
pub fn formatFloatDecimal(
    value: anytype,
    options: FormatOptions,
    writer: anytype,
) !void {
    var x = @as(f64, value);

    // Errol doesn't handle these special cases.
    if (math.signbit(x)) {
        try writer.writeAll("-");
        x = -x;
    }

    if (math.isNan(x)) {
        return writer.writeAll("nan");
    }
    if (math.isPositiveInf(x)) {
        return writer.writeAll("inf");
    }
    if (x == 0.0) {
        try writer.writeAll("0");

        if (options.precision) |precision| {
            if (precision != 0) {
                try writer.writeAll(".");
                var i: usize = 0;
                while (i < precision) : (i += 1) {
                    try writer.writeAll("0");
                }
            } else {
                try writer.writeAll(".0");
            }
        }

        return;
    }

    // non-special case, use errol3
    var buffer: [32]u8 = undefined;
    var float_decimal = errol.errol3(x, buffer[0..]);

    if (options.precision) |precision| {
        errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Decimal);

        // exp < 0 means the leading is always 0 as errol result is normalized.
        var num_digits_whole = if (float_decimal.exp > 0) @intCast(usize, float_decimal.exp) else 0;

        // the actual slice into the buffer, we may need to zero-pad between num_digits_whole and this.
        var num_digits_whole_no_pad = math.min(num_digits_whole, float_decimal.digits.len);

        if (num_digits_whole > 0) {
            // We may have to zero pad, for instance 1e4 requires zero padding.
            try writer.writeAll(float_decimal.digits[0..num_digits_whole_no_pad]);

            var i = num_digits_whole_no_pad;
            while (i < num_digits_whole) : (i += 1) {
                try writer.writeAll("0");
            }
        } else {
            try writer.writeAll("0");
        }

        // {.0} special case doesn't want a trailing '.'
        if (precision == 0) {
            return;
        }

        try writer.writeAll(".");

        // Keep track of fractional count printed for case where we pre-pad then post-pad with 0's.
        var printed: usize = 0;

        // Zero-fill until we reach significant digits or run out of precision.
        if (float_decimal.exp <= 0) {
            const zero_digit_count = @intCast(usize, -float_decimal.exp);
            const zeros_to_print = math.min(zero_digit_count, precision);

            var i: usize = 0;
            while (i < zeros_to_print) : (i += 1) {
                try writer.writeAll("0");
                printed += 1;
            }

            if (printed >= precision) {
                return;
            }
        }

        // Remaining fractional portion, zero-padding if insufficient.
        assert(precision >= printed);
        if (num_digits_whole_no_pad + precision - printed < float_decimal.digits.len) {
            try writer.writeAll(float_decimal.digits[num_digits_whole_no_pad .. num_digits_whole_no_pad + precision - printed]);
            return;
        } else {
            try writer.writeAll(float_decimal.digits[num_digits_whole_no_pad..]);
            printed += float_decimal.digits.len - num_digits_whole_no_pad;

            while (printed < precision) : (printed += 1) {
                try writer.writeAll("0");
            }
        }
    } else {
        // exp < 0 means the leading is always 0 as errol result is normalized.
        var num_digits_whole = if (float_decimal.exp > 0) @intCast(usize, float_decimal.exp) else 0;

        // the actual slice into the buffer, we may need to zero-pad between num_digits_whole and this.
        var num_digits_whole_no_pad = math.min(num_digits_whole, float_decimal.digits.len);

        if (num_digits_whole > 0) {
            // We may have to zero pad, for instance 1e4 requires zero padding.
            try writer.writeAll(float_decimal.digits[0..num_digits_whole_no_pad]);

            var i = num_digits_whole_no_pad;
            while (i < num_digits_whole) : (i += 1) {
                try writer.writeAll("0");
            }
        } else {
            try writer.writeAll("0");
        }

        // Omit `.` if no fractional portion
        if (float_decimal.exp >= 0 and num_digits_whole_no_pad == float_decimal.digits.len) {
            return;
        }

        try writer.writeAll(".");

        // Zero-fill until we reach significant digits or run out of precision.
        if (float_decimal.exp < 0) {
            const zero_digit_count = @intCast(usize, -float_decimal.exp);

            var i: usize = 0;
            while (i < zero_digit_count) : (i += 1) {
                try writer.writeAll("0");
            }
        }

        try writer.writeAll(float_decimal.digits[num_digits_whole_no_pad..]);
    }
}

pub fn formatBytes(
    value: anytype,
    options: FormatOptions,
    comptime radix: usize,
    writer: anytype,
) !void {
    if (value == 0) {
        return writer.writeAll("0B");
    }

    const is_float = comptime std.meta.trait.is(.Float)(@TypeOf(value));
    const mags_si = " kMGTPEZY";
    const mags_iec = " KMGTPEZY";

    const log2 = if (is_float) @floatToInt(usize, math.log2(value)) else math.log2(value);
    const magnitude = switch (radix) {
        1000 => math.min(log2 / comptime math.log2(1000), mags_si.len - 1),
        1024 => math.min(log2 / 10, mags_iec.len - 1),
        else => unreachable,
    };
    const new_value = lossyCast(f64, value) / math.pow(f64, lossyCast(f64, radix), lossyCast(f64, magnitude));
    const suffix = switch (radix) {
        1000 => mags_si[magnitude],
        1024 => mags_iec[magnitude],
        else => unreachable,
    };

    try formatFloatDecimal(new_value, options, writer);

    if (suffix == ' ') {
        return writer.writeAll("B");
    }

    const buf = switch (radix) {
        1000 => &[_]u8{ suffix, 'B' },
        1024 => &[_]u8{ suffix, 'i', 'B' },
        else => unreachable,
    };
    return writer.writeAll(buf);
}

pub fn formatInt(
    value: anytype,
    base: u8,
    uppercase: bool,
    options: FormatOptions,
    writer: anytype,
) !void {
    assert(base >= 2);

    const int_value = if (@TypeOf(value) == comptime_int) blk: {
        const Int = math.IntFittingRange(value, value);
        break :blk @as(Int, value);
    } else
        value;

    const value_info = @typeInfo(@TypeOf(int_value)).Int;

    // The type must have the same size as `base` or be wider in order for the
    // division to work
    const min_int_bits = comptime math.max(value_info.bits, 8);
    const MinInt = std.meta.Int(.unsigned, min_int_bits);

    const abs_value = math.absCast(int_value);
    // The worst case in terms of space needed is base 2, plus 1 for the sign
    var buf: [1 + math.max(value_info.bits, 1)]u8 = undefined;

    var a: MinInt = abs_value;
    var index: usize = buf.len;
    while (true) {
        const digit = a % base;
        index -= 1;
        buf[index] = digitToChar(@intCast(u8, digit), uppercase);
        a /= base;
        if (a == 0) break;
    }

    if (value_info.is_signed) {
        if (value < 0) {
            // Negative integer
            index -= 1;
            buf[index] = '-';
        } else if (options.width == null or options.width.? == 0) {
            // Positive integer, omit the plus sign
        } else {
            // Positive integer
            index -= 1;
            buf[index] = '+';
        }
    }

    return formatBuf(buf[index..], options, writer);
}

pub fn formatIntBuf(out_buf: []u8, value: anytype, base: u8, uppercase: bool, options: FormatOptions) usize {
    var fbs = std.io.fixedBufferStream(out_buf);
    formatInt(value, base, uppercase, options, fbs.writer()) catch unreachable;
    return fbs.pos;
}

pub const ParseIntError = error{
    /// The result cannot fit in the type specified
    Overflow,

    /// The input was empty or had a byte that was not a digit
    InvalidCharacter,
};

/// Parses the string `buf` as signed or unsigned representation in the
/// specified radix of an integral value of type `T`.
///
/// When `radix` is zero the string prefix is examined to detect the true radix:
///  * A prefix of "0b" implies radix=2,
///  * A prefix of "0o" implies radix=8,
///  * A prefix of "0x" implies radix=16,
///  * Otherwise radix=10 is assumed.
///
/// See also `parseUnsigned`.
pub fn parseInt(comptime T: type, buf: []const u8, radix: u8) ParseIntError!T {
    if (buf.len == 0) return error.InvalidCharacter;
    if (buf[0] == '+') return parseWithSign(T, buf[1..], radix, .Pos);
    if (buf[0] == '-') return parseWithSign(T, buf[1..], radix, .Neg);
    return parseWithSign(T, buf, radix, .Pos);
}

test "parseInt" {
    std.testing.expect((try parseInt(i32, "-10", 10)) == -10);
    std.testing.expect((try parseInt(i32, "+10", 10)) == 10);
    std.testing.expect((try parseInt(u32, "+10", 10)) == 10);
    std.testing.expectError(error.Overflow, parseInt(u32, "-10", 10));
    std.testing.expectError(error.InvalidCharacter, parseInt(u32, " 10", 10));
    std.testing.expectError(error.InvalidCharacter, parseInt(u32, "10 ", 10));
    std.testing.expect((try parseInt(u8, "255", 10)) == 255);
    std.testing.expectError(error.Overflow, parseInt(u8, "256", 10));

    // +0 and -0 should work for unsigned
    std.testing.expect((try parseInt(u8, "-0", 10)) == 0);
    std.testing.expect((try parseInt(u8, "+0", 10)) == 0);

    // ensure minInt is parsed correctly
    std.testing.expect((try parseInt(i8, "-128", 10)) == math.minInt(i8));
    std.testing.expect((try parseInt(i43, "-4398046511104", 10)) == math.minInt(i43));

    // empty string or bare +- is invalid
    std.testing.expectError(error.InvalidCharacter, parseInt(u32, "", 10));
    std.testing.expectError(error.InvalidCharacter, parseInt(i32, "", 10));
    std.testing.expectError(error.InvalidCharacter, parseInt(u32, "+", 10));
    std.testing.expectError(error.InvalidCharacter, parseInt(i32, "+", 10));
    std.testing.expectError(error.InvalidCharacter, parseInt(u32, "-", 10));
    std.testing.expectError(error.InvalidCharacter, parseInt(i32, "-", 10));

    // autodectect the radix
    std.testing.expect((try parseInt(i32, "111", 0)) == 111);
    std.testing.expect((try parseInt(i32, "+0b111", 0)) == 7);
    std.testing.expect((try parseInt(i32, "+0o111", 0)) == 73);
    std.testing.expect((try parseInt(i32, "+0x111", 0)) == 273);
    std.testing.expect((try parseInt(i32, "-0b111", 0)) == -7);
    std.testing.expect((try parseInt(i32, "-0o111", 0)) == -73);
    std.testing.expect((try parseInt(i32, "-0x111", 0)) == -273);

    // bare binary/octal/decimal prefix is invalid
    std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0b", 0));
    std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0o", 0));
    std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0x", 0));
}

fn parseWithSign(
    comptime T: type,
    buf: []const u8,
    radix: u8,
    comptime sign: enum { Pos, Neg },
) ParseIntError!T {
    if (buf.len == 0) return error.InvalidCharacter;

    var buf_radix = radix;
    var buf_start = buf;
    if (radix == 0) {
        // Treat is as a decimal number by default.
        buf_radix = 10;
        // Detect the radix by looking at buf prefix.
        if (buf.len > 2 and buf[0] == '0') {
            switch (buf[1]) {
                'b' => {
                    buf_radix = 2;
                    buf_start = buf[2..];
                },
                'o' => {
                    buf_radix = 8;
                    buf_start = buf[2..];
                },
                'x' => {
                    buf_radix = 16;
                    buf_start = buf[2..];
                },
                else => {},
            }
        }
    }

    const add = switch (sign) {
        .Pos => math.add,
        .Neg => math.sub,
    };

    var x: T = 0;

    for (buf_start) |c| {
        const digit = try charToDigit(c, buf_radix);

        if (x != 0) x = try math.mul(T, x, try math.cast(T, buf_radix));
        x = try add(T, x, try math.cast(T, digit));
    }

    return x;
}

/// Parses the string `buf` as  unsigned representation in the specified radix
/// of an integral value of type `T`.
///
/// When `radix` is zero the string prefix is examined to detect the true radix:
///  * A prefix of "0b" implies radix=2,
///  * A prefix of "0o" implies radix=8,
///  * A prefix of "0x" implies radix=16,
///  * Otherwise radix=10 is assumed.
///
/// See also `parseInt`.
pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) ParseIntError!T {
    return parseWithSign(T, buf, radix, .Pos);
}

test "parseUnsigned" {
    std.testing.expect((try parseUnsigned(u16, "050124", 10)) == 50124);
    std.testing.expect((try parseUnsigned(u16, "65535", 10)) == 65535);
    std.testing.expectError(error.Overflow, parseUnsigned(u16, "65536", 10));

    std.testing.expect((try parseUnsigned(u64, "0ffffffffffffffff", 16)) == 0xffffffffffffffff);
    std.testing.expectError(error.Overflow, parseUnsigned(u64, "10000000000000000", 16));

    std.testing.expect((try parseUnsigned(u32, "DeadBeef", 16)) == 0xDEADBEEF);

    std.testing.expect((try parseUnsigned(u7, "1", 10)) == 1);
    std.testing.expect((try parseUnsigned(u7, "1000", 2)) == 8);

    std.testing.expectError(error.InvalidCharacter, parseUnsigned(u32, "f", 10));
    std.testing.expectError(error.InvalidCharacter, parseUnsigned(u8, "109", 8));

    std.testing.expect((try parseUnsigned(u32, "NUMBER", 36)) == 1442151747);

    // these numbers should fit even though the radix itself doesn't fit in the destination type
    std.testing.expect((try parseUnsigned(u1, "0", 10)) == 0);
    std.testing.expect((try parseUnsigned(u1, "1", 10)) == 1);
    std.testing.expectError(error.Overflow, parseUnsigned(u1, "2", 10));
    std.testing.expect((try parseUnsigned(u1, "001", 16)) == 1);
    std.testing.expect((try parseUnsigned(u2, "3", 16)) == 3);
    std.testing.expectError(error.Overflow, parseUnsigned(u2, "4", 16));

    // parseUnsigned does not expect a sign
    std.testing.expectError(error.InvalidCharacter, parseUnsigned(u8, "+0", 10));
    std.testing.expectError(error.InvalidCharacter, parseUnsigned(u8, "-0", 10));

    // test empty string error
    std.testing.expectError(error.InvalidCharacter, parseUnsigned(u8, "", 10));
}

pub const parseFloat = @import("fmt/parse_float.zig").parseFloat;

test "parseFloat" {
    _ = @import("fmt/parse_float.zig");
}

pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) {
    const value = switch (c) {
        '0'...'9' => c - '0',
        'A'...'Z' => c - 'A' + 10,
        'a'...'z' => c - 'a' + 10,
        else => return error.InvalidCharacter,
    };

    if (value >= radix) return error.InvalidCharacter;

    return value;
}

pub fn digitToChar(digit: u8, uppercase: bool) u8 {
    return switch (digit) {
        0...9 => digit + '0',
        10...35 => digit + ((if (uppercase) @as(u8, 'A') else @as(u8, 'a')) - 10),
        else => unreachable,
    };
}

pub const BufPrintError = error{
    /// As much as possible was written to the buffer, but it was too small to fit all the printed bytes.
    NoSpaceLeft,
};
pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintError![]u8 {
    var fbs = std.io.fixedBufferStream(buf);
    try format(fbs.writer(), fmt, args);
    return fbs.getWritten();
}

pub fn bufPrintZ(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintError![:0]u8 {
    const result = try bufPrint(buf, fmt ++ "\x00", args);
    return result[0 .. result.len - 1 :0];
}

/// Count the characters needed for format. Useful for preallocating memory
pub fn count(comptime fmt: []const u8, args: anytype) u64 {
    var counting_writer = std.io.countingWriter(std.io.null_writer);
    format(counting_writer.writer(), fmt, args) catch |err| switch (err) {};
    return counting_writer.bytes_written;
}

pub const AllocPrintError = error{OutOfMemory};

pub fn allocPrint(allocator: *mem.Allocator, comptime fmt: []const u8, args: anytype) AllocPrintError![]u8 {
    const size = math.cast(usize, count(fmt, args)) catch |err| switch (err) {
        // Output too long. Can't possibly allocate enough memory to display it.
        error.Overflow => return error.OutOfMemory,
    };
    const buf = try allocator.alloc(u8, size);
    return bufPrint(buf, fmt, args) catch |err| switch (err) {
        error.NoSpaceLeft => unreachable, // we just counted the size above
    };
}

/// Deprecated, use allocPrintZ
pub const allocPrint0 = allocPrintZ;

pub fn allocPrintZ(allocator: *mem.Allocator, comptime fmt: []const u8, args: anytype) AllocPrintError![:0]u8 {
    const result = try allocPrint(allocator, fmt ++ "\x00", args);
    return result[0 .. result.len - 1 :0];
}

test "bufPrintInt" {
    var buffer: [100]u8 = undefined;
    const buf = buffer[0..];

    std.testing.expectEqualSlices(u8, "-1", bufPrintIntToSlice(buf, @as(i1, -1), 10, false, FormatOptions{}));

    std.testing.expectEqualSlices(u8, "-101111000110000101001110", bufPrintIntToSlice(buf, @as(i32, -12345678), 2, false, FormatOptions{}));
    std.testing.expectEqualSlices(u8, "-12345678", bufPrintIntToSlice(buf, @as(i32, -12345678), 10, false, FormatOptions{}));
    std.testing.expectEqualSlices(u8, "-bc614e", bufPrintIntToSlice(buf, @as(i32, -12345678), 16, false, FormatOptions{}));
    std.testing.expectEqualSlices(u8, "-BC614E", bufPrintIntToSlice(buf, @as(i32, -12345678), 16, true, FormatOptions{}));

    std.testing.expectEqualSlices(u8, "12345678", bufPrintIntToSlice(buf, @as(u32, 12345678), 10, true, FormatOptions{}));

    std.testing.expectEqualSlices(u8, "   666", bufPrintIntToSlice(buf, @as(u32, 666), 10, false, FormatOptions{ .width = 6 }));
    std.testing.expectEqualSlices(u8, "  1234", bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 6 }));
    std.testing.expectEqualSlices(u8, "1234", bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 1 }));

    std.testing.expectEqualSlices(u8, "+42", bufPrintIntToSlice(buf, @as(i32, 42), 10, false, FormatOptions{ .width = 3 }));
    std.testing.expectEqualSlices(u8, "-42", bufPrintIntToSlice(buf, @as(i32, -42), 10, false, FormatOptions{ .width = 3 }));
}

pub fn bufPrintIntToSlice(buf: []u8, value: anytype, base: u8, uppercase: bool, options: FormatOptions) []u8 {
    return buf[0..formatIntBuf(buf, value, base, uppercase, options)];
}

pub fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt, args):0]u8 {
    comptime {
        var buf: [count(fmt, args):0]u8 = undefined;
        _ = bufPrint(&buf, fmt, args) catch unreachable;
        buf[buf.len] = 0;
        return &buf;
    }
}

test "comptimePrint" {
    @setEvalBranchQuota(2000);
    std.testing.expectEqual(*const [3:0]u8, @TypeOf(comptime comptimePrint("{}", .{100})));
    std.testing.expectEqualSlices(u8, "100", comptime comptimePrint("{}", .{100}));
}

test "parse u64 digit too big" {
    _ = parseUnsigned(u64, "123a", 10) catch |err| {
        if (err == error.InvalidCharacter) return;
        unreachable;
    };
    unreachable;
}

test "parse unsigned comptime" {
    comptime {
        std.testing.expect((try parseUnsigned(usize, "2", 10)) == 2);
    }
}

test "optional" {
    {
        const value: ?i32 = 1234;
        try testFmt("optional: 1234\n", "optional: {}\n", .{value});
    }
    {
        const value: ?i32 = null;
        try testFmt("optional: null\n", "optional: {}\n", .{value});
    }
    {
        const value = @intToPtr(?*i32, 0xf000d000);
        try testFmt("optional: *i32@f000d000\n", "optional: {*}\n", .{value});
    }
}

test "error" {
    {
        const value: anyerror!i32 = 1234;
        try testFmt("error union: 1234\n", "error union: {}\n", .{value});
    }
    {
        const value: anyerror!i32 = error.InvalidChar;
        try testFmt("error union: error.InvalidChar\n", "error union: {}\n", .{value});
    }
}

test "int.small" {
    {
        const value: u3 = 0b101;
        try testFmt("u3: 5\n", "u3: {}\n", .{value});
    }
}

test "int.specifier" {
    {
        const value: u8 = 'a';
        try testFmt("u8: a\n", "u8: {c}\n", .{value});
    }
    {
        const value: u8 = 0b1100;
        try testFmt("u8: 0b1100\n", "u8: 0b{b}\n", .{value});
    }
    {
        const value: u16 = 0o1234;
        try testFmt("u16: 0o1234\n", "u16: 0o{o}\n", .{value});
    }
}

test "int.padded" {
    try testFmt("u8: '   1'", "u8: '{:4}'", .{@as(u8, 1)});
    try testFmt("u8: '1000'", "u8: '{:0<4}'", .{@as(u8, 1)});
    try testFmt("u8: '0001'", "u8: '{:0>4}'", .{@as(u8, 1)});
    try testFmt("u8: '0100'", "u8: '{:0^4}'", .{@as(u8, 1)});
    try testFmt("i8: '-1  '", "i8: '{:<4}'", .{@as(i8, -1)});
    try testFmt("i8: '  -1'", "i8: '{:>4}'", .{@as(i8, -1)});
    try testFmt("i8: ' -1 '", "i8: '{:^4}'", .{@as(i8, -1)});
    try testFmt("i16: '-1234'", "i16: '{:4}'", .{@as(i16, -1234)});
    try testFmt("i16: '+1234'", "i16: '{:4}'", .{@as(i16, 1234)});
    try testFmt("i16: '-12345'", "i16: '{:4}'", .{@as(i16, -12345)});
    try testFmt("i16: '+12345'", "i16: '{:4}'", .{@as(i16, 12345)});
    try testFmt("u16: '12345'", "u16: '{:4}'", .{@as(u16, 12345)});
}

test "buffer" {
    {
        var buf1: [32]u8 = undefined;
        var fbs = std.io.fixedBufferStream(&buf1);
        try formatType(1234, "", FormatOptions{}, fbs.writer(), default_max_depth);
        std.testing.expect(mem.eql(u8, fbs.getWritten(), "1234"));

        fbs.reset();
        try formatType('a', "c", FormatOptions{}, fbs.writer(), default_max_depth);
        std.testing.expect(mem.eql(u8, fbs.getWritten(), "a"));

        fbs.reset();
        try formatType(0b1100, "b", FormatOptions{}, fbs.writer(), default_max_depth);
        std.testing.expect(mem.eql(u8, fbs.getWritten(), "1100"));
    }
}

test "array" {
    {
        const value: [3]u8 = "abc".*;
        try testFmt("array: abc\n", "array: {}\n", .{value});
        try testFmt("array: abc\n", "array: {}\n", .{&value});

        var buf: [100]u8 = undefined;
        try testFmt(
            try bufPrint(buf[0..], "array: [3]u8@{x}\n", .{@ptrToInt(&value)}),
            "array: {*}\n",
            .{&value},
        );
    }
}

test "slice" {
    {
        const value: []const u8 = "abc";
        try testFmt("slice: abc\n", "slice: {}\n", .{value});
    }
    {
        var runtime_zero: usize = 0;
        const value = @intToPtr([*]align(1) const []const u8, 0xdeadbeef)[runtime_zero..runtime_zero];
        try testFmt("slice: []const u8@deadbeef\n", "slice: {}\n", .{value});
    }
    {
        const null_term_slice: [:0]const u8 = "\x00hello\x00";
        try testFmt("buf: \x00hello\x00\n", "buf: {s}\n", .{null_term_slice});
    }

    try testFmt("buf:  Test\n", "buf: {s:5}\n", .{"Test"});
    try testFmt("buf: Test\n Other text", "buf: {s}\n Other text", .{"Test"});
}

test "escape non-printable" {
    try testFmt("abc", "{e}", .{"abc"});
    try testFmt("ab\\xffc", "{e}", .{"ab\xffc"});
    try testFmt("ab\\xFFc", "{E}", .{"ab\xffc"});
}

test "escape invalid identifiers" {
    try testFmt("@\"while\"", "{z}", .{"while"});
    try testFmt("hello", "{z}", .{"hello"});
    try testFmt("@\"11\\\"23\"", "{z}", .{"11\"23"});
    try testFmt("@\"11\\x0f23\"", "{z}", .{"11\x0F23"});
    try testFmt("\\x0f", "{Z}", .{0x0f});
    try testFmt(
        \\" \\ hi \x07 \x11 \" derp \'"
    , "\"{Z}\"", .{" \\ hi \x07 \x11 \" derp '"});
}

test "pointer" {
    {
        const value = @intToPtr(*align(1) i32, 0xdeadbeef);
        try testFmt("pointer: i32@deadbeef\n", "pointer: {}\n", .{value});
        try testFmt("pointer: i32@deadbeef\n", "pointer: {*}\n", .{value});
    }
    {
        const value = @intToPtr(fn () void, 0xdeadbeef);
        try testFmt("pointer: fn() void@deadbeef\n", "pointer: {}\n", .{value});
    }
    {
        const value = @intToPtr(fn () void, 0xdeadbeef);
        try testFmt("pointer: fn() void@deadbeef\n", "pointer: {}\n", .{value});
    }
}

test "cstr" {
    try testFmt(
        "cstr: Test C\n",
        "cstr: {s}\n",
        .{@ptrCast([*c]const u8, "Test C")},
    );
    try testFmt(
        "cstr:     Test C\n",
        "cstr: {s:10}\n",
        .{@ptrCast([*c]const u8, "Test C")},
    );
}

test "filesize" {
    try testFmt("file size: 63MiB\n", "file size: {Bi}\n", .{@as(usize, 63 * 1024 * 1024)});
    try testFmt("file size: 66.06MB\n", "file size: {B:.2}\n", .{@as(usize, 63 * 1024 * 1024)});
}

test "struct" {
    {
        const Struct = struct {
            field: u8,
        };
        const value = Struct{ .field = 42 };
        try testFmt("struct: Struct{ .field = 42 }\n", "struct: {}\n", .{value});
        try testFmt("struct: Struct{ .field = 42 }\n", "struct: {}\n", .{&value});
    }
    {
        const Struct = struct {
            a: u0,
            b: u1,
        };
        const value = Struct{ .a = 0, .b = 1 };
        try testFmt("struct: Struct{ .a = 0, .b = 1 }\n", "struct: {}\n", .{value});
    }
}

test "enum" {
    const Enum = enum {
        One,
        Two,
    };
    const value = Enum.Two;
    try testFmt("enum: Enum.Two\n", "enum: {}\n", .{value});
    try testFmt("enum: Enum.Two\n", "enum: {}\n", .{&value});
    try testFmt("enum: Enum.One\n", "enum: {x}\n", .{Enum.One});
    try testFmt("enum: Enum.Two\n", "enum: {X}\n", .{Enum.Two});

    // test very large enum to verify ct branch quota is large enough
    try testFmt("enum: Win32Error.INVALID_FUNCTION\n", "enum: {}\n", .{std.os.windows.Win32Error.INVALID_FUNCTION});
}

test "non-exhaustive enum" {
    const Enum = enum(u16) {
        One = 0x000f,
        Two = 0xbeef,
        _,
    };
    try testFmt("enum: Enum.One\n", "enum: {}\n", .{Enum.One});
    try testFmt("enum: Enum.Two\n", "enum: {}\n", .{Enum.Two});
    try testFmt("enum: Enum(4660)\n", "enum: {}\n", .{@intToEnum(Enum, 0x1234)});
    try testFmt("enum: Enum.One\n", "enum: {x}\n", .{Enum.One});
    try testFmt("enum: Enum.Two\n", "enum: {x}\n", .{Enum.Two});
    try testFmt("enum: Enum.Two\n", "enum: {X}\n", .{Enum.Two});
    try testFmt("enum: Enum(1234)\n", "enum: {x}\n", .{@intToEnum(Enum, 0x1234)});
}

test "float.scientific" {
    try testFmt("f32: 1.34000003e+00", "f32: {e}", .{@as(f32, 1.34)});
    try testFmt("f32: 1.23400001e+01", "f32: {e}", .{@as(f32, 12.34)});
    try testFmt("f64: -1.234e+11", "f64: {e}", .{@as(f64, -12.34e10)});
    try testFmt("f64: 9.99996e-40", "f64: {e}", .{@as(f64, 9.999960e-40)});
}

test "float.scientific.precision" {
    try testFmt("f64: 1.40971e-42", "f64: {e:.5}", .{@as(f64, 1.409706e-42)});
    try testFmt("f64: 1.00000e-09", "f64: {e:.5}", .{@as(f64, @bitCast(f32, @as(u32, 814313563)))});
    try testFmt("f64: 7.81250e-03", "f64: {e:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1006632960)))});
    // libc rounds 1.000005e+05 to 1.00000e+05 but zig does 1.00001e+05.
    // In fact, libc doesn't round a lot of 5 cases up when one past the precision point.
    try testFmt("f64: 1.00001e+05", "f64: {e:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1203982400)))});
}

test "float.special" {
    try testFmt("f64: nan", "f64: {}", .{math.nan_f64});
    // negative nan is not defined by IEE 754,
    // and ARM thus normalizes it to positive nan
    if (builtin.arch != builtin.Arch.arm) {
        try testFmt("f64: -nan", "f64: {}", .{-math.nan_f64});
    }
    try testFmt("f64: inf", "f64: {}", .{math.inf_f64});
    try testFmt("f64: -inf", "f64: {}", .{-math.inf_f64});
}

test "float.decimal" {
    try testFmt("f64: 152314000000000000000000000000", "f64: {d}", .{@as(f64, 1.52314e+29)});
    try testFmt("f32: 0", "f32: {d}", .{@as(f32, 0.0)});
    try testFmt("f32: 1.1", "f32: {d:.1}", .{@as(f32, 1.1234)});
    try testFmt("f32: 1234.57", "f32: {d:.2}", .{@as(f32, 1234.567)});
    // -11.1234 is converted to f64 -11.12339... internally (errol3() function takes f64).
    // -11.12339... is rounded back up to -11.1234
    try testFmt("f32: -11.1234", "f32: {d:.4}", .{@as(f32, -11.1234)});
    try testFmt("f32: 91.12345", "f32: {d:.5}", .{@as(f32, 91.12345)});
    try testFmt("f64: 91.1234567890", "f64: {d:.10}", .{@as(f64, 91.12345678901235)});
    try testFmt("f64: 0.00000", "f64: {d:.5}", .{@as(f64, 0.0)});
    try testFmt("f64: 6", "f64: {d:.0}", .{@as(f64, 5.700)});
    try testFmt("f64: 10.0", "f64: {d:.1}", .{@as(f64, 9.999)});
    try testFmt("f64: 1.000", "f64: {d:.3}", .{@as(f64, 1.0)});
    try testFmt("f64: 0.00030000", "f64: {d:.8}", .{@as(f64, 0.0003)});
    try testFmt("f64: 0.00000", "f64: {d:.5}", .{@as(f64, 1.40130e-45)});
    try testFmt("f64: 0.00000", "f64: {d:.5}", .{@as(f64, 9.999960e-40)});
}

test "float.libc.sanity" {
    try testFmt("f64: 0.00001", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 916964781)))});
    try testFmt("f64: 0.00001", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 925353389)))});
    try testFmt("f64: 0.10000", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1036831278)))});
    try testFmt("f64: 1.00000", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1065353133)))});
    try testFmt("f64: 10.00000", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1092616192)))});

    // libc differences
    //
    // This is 0.015625 exactly according to gdb. We thus round down,
    // however glibc rounds up for some reason. This occurs for all
    // floats of the form x.yyyy25 on a precision point.
    try testFmt("f64: 0.01563", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1015021568)))});
    // errol3 rounds to ... 630 but libc rounds to ...632. Grisu3
    // also rounds to 630 so I'm inclined to believe libc is not
    // optimal here.
    try testFmt("f64: 18014400656965630.00000", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1518338049)))});
}

test "custom" {
    const Vec2 = struct {
        const SelfType = @This();
        x: f32,
        y: f32,

        pub fn format(
            self: SelfType,
            comptime fmt: []const u8,
            options: FormatOptions,
            writer: anytype,
        ) !void {
            if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "p")) {
                return std.fmt.format(writer, "({d:.3},{d:.3})", .{ self.x, self.y });
            } else if (comptime std.mem.eql(u8, fmt, "d")) {
                return std.fmt.format(writer, "{d:.3}x{d:.3}", .{ self.x, self.y });
            } else {
                @compileError("Unknown format character: '" ++ fmt ++ "'");
            }
        }
    };

    var buf1: [32]u8 = undefined;
    var value = Vec2{
        .x = 10.2,
        .y = 2.22,
    };
    try testFmt("point: (10.200,2.220)\n", "point: {}\n", .{&value});
    try testFmt("dim: 10.200x2.220\n", "dim: {d}\n", .{&value});

    // same thing but not passing a pointer
    try testFmt("point: (10.200,2.220)\n", "point: {}\n", .{value});
    try testFmt("dim: 10.200x2.220\n", "dim: {d}\n", .{value});
}

test "struct" {
    const S = struct {
        a: u32,
        b: anyerror,
    };

    const inst = S{
        .a = 456,
        .b = error.Unused,
    };

    try testFmt("S{ .a = 456, .b = error.Unused }", "{}", .{inst});
}

test "union" {
    const TU = union(enum) {
        float: f32,
        int: u32,
    };

    const UU = union {
        float: f32,
        int: u32,
    };

    const EU = extern union {
        float: f32,
        int: u32,
    };

    const tu_inst = TU{ .int = 123 };
    const uu_inst = UU{ .int = 456 };
    const eu_inst = EU{ .float = 321.123 };

    try testFmt("TU{ .int = 123 }", "{}", .{tu_inst});

    var buf: [100]u8 = undefined;
    const uu_result = try bufPrint(buf[0..], "{}", .{uu_inst});
    std.testing.expect(mem.eql(u8, uu_result[0..3], "UU@"));

    const eu_result = try bufPrint(buf[0..], "{}", .{eu_inst});
    std.testing.expect(mem.eql(u8, uu_result[0..3], "EU@"));
}

test "enum" {
    const E = enum {
        One,
        Two,
        Three,
    };

    const inst = E.Two;

    try testFmt("E.Two", "{}", .{inst});
}

test "struct.self-referential" {
    const S = struct {
        const SelfType = @This();
        a: ?*SelfType,
    };

    var inst = S{
        .a = null,
    };
    inst.a = &inst;

    try testFmt("S{ .a = S{ .a = S{ .a = S{ ... } } } }", "{}", .{inst});
}

test "struct.zero-size" {
    const A = struct {
        fn foo() void {}
    };
    const B = struct {
        a: A,
        c: i32,
    };

    const a = A{};
    const b = B{ .a = a, .c = 0 };

    try testFmt("B{ .a = A{ }, .c = 0 }", "{}", .{b});
}

test "bytes.hex" {
    const some_bytes = "\xCA\xFE\xBA\xBE";
    try testFmt("lowercase: cafebabe\n", "lowercase: {x}\n", .{some_bytes});
    try testFmt("uppercase: CAFEBABE\n", "uppercase: {X}\n", .{some_bytes});
    //Test Slices
    try testFmt("uppercase: CAFE\n", "uppercase: {X}\n", .{some_bytes[0..2]});
    try testFmt("lowercase: babe\n", "lowercase: {x}\n", .{some_bytes[2..]});
    const bytes_with_zeros = "\x00\x0E\xBA\xBE";
    try testFmt("lowercase: 000ebabe\n", "lowercase: {x}\n", .{bytes_with_zeros});
}

fn testFmt(expected: []const u8, comptime template: []const u8, args: anytype) !void {
    var buf: [100]u8 = undefined;
    const result = try bufPrint(buf[0..], template, args);
    if (mem.eql(u8, result, expected)) return;

    std.debug.warn("\n====== expected this output: =========\n", .{});
    std.debug.warn("{}", .{expected});
    std.debug.warn("\n======== instead found this: =========\n", .{});
    std.debug.warn("{}", .{result});
    std.debug.warn("\n======================================\n", .{});
    return error.TestFailed;
}

pub const trim = @compileError("deprecated; use std.mem.trim with std.ascii.spaces instead");
pub const isWhiteSpace = @compileError("deprecated; use std.ascii.isSpace instead");

pub fn hexToBytes(out: []u8, input: []const u8) !void {
    if (out.len * 2 < input.len)
        return error.InvalidLength;

    var in_i: usize = 0;
    while (in_i != input.len) : (in_i += 2) {
        const hi = try charToDigit(input[in_i], 16);
        const lo = try charToDigit(input[in_i + 1], 16);
        out[in_i / 2] = (hi << 4) | lo;
    }
}

test "hexToBytes" {
    const test_hex_str = "909A312BB12ED1F819B3521AC4C1E896F2160507FFC1C8381E3B07BB16BD1706";
    var pb: [32]u8 = undefined;
    try hexToBytes(pb[0..], test_hex_str);
    try testFmt(test_hex_str, "{X}", .{pb});
}

test "formatIntValue with comptime_int" {
    const value: comptime_int = 123456789123456789;

    var buf: [20]u8 = undefined;
    var fbs = std.io.fixedBufferStream(&buf);
    try formatIntValue(value, "", FormatOptions{}, fbs.writer());
    std.testing.expect(mem.eql(u8, fbs.getWritten(), "123456789123456789"));
}

test "formatFloatValue with comptime_float" {
    const value: comptime_float = 1.0;

    var buf: [20]u8 = undefined;
    var fbs = std.io.fixedBufferStream(&buf);
    try formatFloatValue(value, "", FormatOptions{}, fbs.writer());
    std.testing.expect(mem.eql(u8, fbs.getWritten(), "1.0e+00"));

    try testFmt("1.0e+00", "{}", .{value});
    try testFmt("1.0e+00", "{}", .{1.0});
}

test "formatType max_depth" {
    const Vec2 = struct {
        const SelfType = @This();
        x: f32,
        y: f32,

        pub fn format(
            self: SelfType,
            comptime fmt: []const u8,
            options: FormatOptions,
            writer: anytype,
        ) !void {
            if (fmt.len == 0) {
                return std.fmt.format(writer, "({d:.3},{d:.3})", .{ self.x, self.y });
            } else {
                @compileError("Unknown format string: '" ++ fmt ++ "'");
            }
        }
    };
    const E = enum {
        One,
        Two,
        Three,
    };
    const TU = union(enum) {
        const SelfType = @This();
        float: f32,
        int: u32,
        ptr: ?*SelfType,
    };
    const S = struct {
        const SelfType = @This();
        a: ?*SelfType,
        tu: TU,
        e: E,
        vec: Vec2,
    };

    var inst = S{
        .a = null,
        .tu = TU{ .ptr = null },
        .e = E.Two,
        .vec = Vec2{ .x = 10.2, .y = 2.22 },
    };
    inst.a = &inst;
    inst.tu.ptr = &inst.tu;

    var buf: [1000]u8 = undefined;
    var fbs = std.io.fixedBufferStream(&buf);
    try formatType(inst, "", FormatOptions{}, fbs.writer(), 0);
    std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ ... }"));

    fbs.reset();
    try formatType(inst, "", FormatOptions{}, fbs.writer(), 1);
    std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }"));

    fbs.reset();
    try formatType(inst, "", FormatOptions{}, fbs.writer(), 2);
    std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ .a = S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ ... } }, .e = E.Two, .vec = (10.200,2.220) }"));

    fbs.reset();
    try formatType(inst, "", FormatOptions{}, fbs.writer(), 3);
    std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ .a = S{ .a = S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ ... } }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ .ptr = TU{ ... } } }, .e = E.Two, .vec = (10.200,2.220) }"));
}

test "positional" {
    try testFmt("2 1 0", "{2} {1} {0}", .{ @as(usize, 0), @as(usize, 1), @as(usize, 2) });
    try testFmt("2 1 0", "{2} {1} {}", .{ @as(usize, 0), @as(usize, 1), @as(usize, 2) });
    try testFmt("0 0", "{0} {0}", .{@as(usize, 0)});
    try testFmt("0 1", "{} {1}", .{ @as(usize, 0), @as(usize, 1) });
    try testFmt("1 0 0 1", "{1} {} {0} {}", .{ @as(usize, 0), @as(usize, 1) });
}

test "positional with specifier" {
    try testFmt("10.0", "{0d:.1}", .{@as(f64, 9.999)});
}

test "positional/alignment/width/precision" {
    try testFmt("10.0", "{0d: >3.1}", .{@as(f64, 9.999)});
}

test "vector" {
    if (builtin.arch == .mipsel or builtin.arch == .mips) {
        // https://github.com/ziglang/zig/issues/3317
        return error.SkipZigTest;
    }
    if (builtin.arch == .riscv64) {
        // https://github.com/ziglang/zig/issues/4486
        return error.SkipZigTest;
    }
    if (builtin.arch == .wasm32) {
        // https://github.com/ziglang/zig/issues/5339
        return error.SkipZigTest;
    }

    const vbool: std.meta.Vector(4, bool) = [_]bool{ true, false, true, false };
    const vi64: std.meta.Vector(4, i64) = [_]i64{ -2, -1, 0, 1 };
    const vu64: std.meta.Vector(4, u64) = [_]u64{ 1000, 2000, 3000, 4000 };

    try testFmt("{ true, false, true, false }", "{}", .{vbool});
    try testFmt("{ -2, -1, 0, 1 }", "{}", .{vi64});
    try testFmt("{    -2,    -1,    +0,    +1 }", "{d:5}", .{vi64});
    try testFmt("{ 1000, 2000, 3000, 4000 }", "{}", .{vu64});
    try testFmt("{ 3e8, 7d0, bb8, fa0 }", "{x}", .{vu64});
    try testFmt("{ 1kB, 2kB, 3kB, 4kB }", "{B}", .{vu64});
    try testFmt("{ 1000B, 1.953125KiB, 2.9296875KiB, 3.90625KiB }", "{Bi}", .{vu64});
}

test "enum-literal" {
    try testFmt(".hello_world", "{}", .{.hello_world});
}

test "padding" {
    try testFmt("Simple", "{}", .{"Simple"});
    try testFmt("      true", "{:10}", .{true});
    try testFmt("      true", "{:>10}", .{true});
    try testFmt("======true", "{:=>10}", .{true});
    try testFmt("true======", "{:=<10}", .{true});
    try testFmt("   true   ", "{:^10}", .{true});
    try testFmt("===true===", "{:=^10}", .{true});
    try testFmt("           Minimum width", "{:18} width", .{"Minimum"});
    try testFmt("==================Filled", "{:=>24}", .{"Filled"});
    try testFmt("        Centered        ", "{:^24}", .{"Centered"});
    try testFmt("-", "{:-^1}", .{""});
}

test "decimal float padding" {
    var number: f32 = 3.1415;
    try testFmt("left-pad:   **3.141\n", "left-pad:   {d:*>7.3}\n", .{number});
    try testFmt("center-pad: *3.141*\n", "center-pad: {d:*^7.3}\n", .{number});
    try testFmt("right-pad:  3.141**\n", "right-pad:  {d:*<7.3}\n", .{number});
}

test "sci float padding" {
    var number: f32 = 3.1415;
    try testFmt("left-pad:   **3.141e+00\n", "left-pad:   {e:*>11.3}\n", .{number});
    try testFmt("center-pad: *3.141e+00*\n", "center-pad: {e:*^11.3}\n", .{number});
    try testFmt("right-pad:  3.141e+00**\n", "right-pad:  {e:*<11.3}\n", .{number});
}

test "null" {
    const inst = null;
    try testFmt("null", "{}", .{inst});
}