summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/strformat.h
blob: f09965bc00220d675e8605310bfff3711ec5d5aa (plain) (blame)
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
/*
** $Id: lfunc.h $
** Auxiliary functions to manipulate prototypes and closures
** See Copyright Notice in lua.h
*/

#ifndef lfunc_h
#define lfunc_h


#include "lobject.h"


#define sizeCclosure(n)	(cast_int(offsetof(CClosure, upvalue)) + \
                         cast_int(sizeof(TValue)) * (n))

#define sizeLclosure(n)	(cast_int(offsetof(LClosure, upvals)) + \
                         cast_int(sizeof(TValue *)) * (n))


/* test whether thread is in 'twups' list */
#define isintwups(L)	(L->twups != L)


/*
** maximum number of upvalues in a closure (both C and Lua). (Value
** must fit in a VM register.)
*/
#define MAXUPVAL	255


#define upisopen(up)	((up)->v != &(up)->u.value)


#define uplevel(up)	check_exp(upisopen(up), cast(StkId, (up)->v))


/*
** maximum number of misses before giving up the cache of closures
** in prototypes
*/
#define MAXMISS		10



/* special status to close upvalues preserving the top of the stack */
#define CLOSEKTOP	(-1)


LUAI_FUNC Proto *luaF_newproto (lua_State *L);
LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nupvals);
LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nupvals);
LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl);
LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level);
LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level);
LUAI_FUNC void luaF_close (lua_State *L, StkId level, int status, int yy);
LUAI_FUNC void luaF_unlinkupval (UpVal *uv);
LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f);
LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number,
                                         int pc);


#endif
44' href='#n444'>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
// license:BSD-3-Clause
// copyright-holders:Vas Crabb
/***************************************************************************

    strformat.h

    type-safe printf substitutes

    This header provides type-safe printf substitutes that output to
    std::ostream- or std::string-like objects.  Most format strings
    supported by C99, SUS, glibc and MSVCRT are accepted.  Not all
    features are implemented, and semantics differ in some cases.  Any
    object with an appropriate stream output operator can be used as
    a format argument with the %s conversion.

    Since the functions are implemented using C++ iostream, some
    behaviour more closely resembles iostream output operator behaviour
    than printf behaviour.  You are also exposed to bugs in your C++
    iostream implementation (e.g. hexadecimal scientific format doesn't
    work properly on MinGW).

    These functions are designed to be forgiving - using an
    inappropriate conversion for an argument's type just results in the
    default conversion for the type being used.  Inappropriate types or
    out-of-range positions for parameterised field width and precision
    are treated as if no width/precision was specified.  Out-of-range
    argument positions result in the format specification being printed.

    Position specifiers for arguments (%123$), field width (*456$) and
    precision (.*789$) are supported.  Mixing explicit and implied
    positions for arguments/widths/precisions is discouraged, although
    it does produce deterministic behaviour.

    The following format flags are recognised:
    - "#": alternate format - sets showbase/showpoint, and also
           boolalpha for bool with s conversion
    - "0": pad with zeroes rather than spaces, ignored if '-' flag is
           specified or if precision is specified for d/i/u/o/x/X
           conversion
    - "-": left-align output, overrides '0'
    - " ": recognised but not implemented, ignored for u/o/x/X
           conversion
    - "+": show sign for positive numbers, overrides ' ', ignored for
           u/o/x/X conversions
    - "'": recognised for SUS compatibility but ignored (digit grouping
           is controlled by stream locale)
    - "I": recognised for glibc compatibility but ignored (digits are
           controlled by stream locale)

    Precision is supported for conversions by setting precision on the
    stream.  This works as expected for a/A/e/E/f/F/g/G conversions on
    floating-point types, and may work for objects with user-defined
    stream output operators.  Precision for d/i/u/o/x/X conversions
    (minimum digits to print) is not supported.  Precision for s
    conversions (maximum characters to print) is only honoured for
    string-like types (output character pointer/array and
    std::basic_string).

    Length specifiers are supported but not required for d/i/u/o/x/X
    conversions with integer/char/bool arguments.  They result in the
    value being cast to the desired type before being printed.  Length
    specifiers are ignored for other conversions.

    The following length specifiers are recognised:
    - hh:  cast to char/unsigned char for d/i/u/o/x/X
    - h:   cast to short/unsigned short for d/i/u/o/x/X
    - l:   cast to long/unsigned long for d/i/u/o/x/X
    - ll:  cast to long long/unsigned long long for d/i/u/o/x/X
    - L:   always ignored
    - j:   cast to intmax_t/uintmax_t for d/i/u/o/x/X
    - z:   cast to ssize_t/size_t for d/i/u/o/x/X
    - t:   cast to ptrdiff_t for d/i/u/o/x/X
    - I:   cast to ssize_t/size_t for d/i/u/o/x/X
    - I32: cast to int32_t/uint32_t for d/i/u/o/x/X
    - I64: cast to int64_t/uint64_t for d/i/u/o/x/X
    - w:   always ignored

    The following conversions are recognised:
    - d/i: signed decimal for integer/char/bool types
    - u:   unsigned decimal for integer/char/bool types
    - o:   unsigned octal for integer/char/bool types
    - x/X: lower/uppercase unsigned hexadecimal for integer/char/bool
           types or scientific hexadecimal for floating-point types
    - e/E: lower/uppercase scientific decimal for floating-point types
    - f/F: lower/uppercase fixed-point decimal for floating-point types
    - g/G: default stream output format for floating-point types (may
           differ from printf behaviour)
    - a/A: lower/uppercase scientific hexadecimal for floating-point
           types or hexadecimal for integer types
    - c/C: cast integer types to stream's character type, no automatic
           widening or narrowing
    - s/S: default stream output behaviour for argument
    - p/P: cast any integer/char/bool/pointer/array to void const *
    - n:   store characters printed so far, produces no output, argument
           must be pointer to type that std::streamoff is convertible to
    - m:   output of std::strerror(errno), no automatic widening or
           narrowing, does not consume an argument
    - %:   literal %, field width applied, does not consume an argument

    The output stream type for stream_format must be equivalent to a
    std::basic_ostream for duck-typing purposes.  The output string for
    string type for string_format must provide value_type, traits_type
    and allocator_type declarations, and must be constructible from a
    std::basic_string using the same value, traits and allocator types.

    The format string type can be a pointer to a NUL-terminated string,
    an array containing a NUL-terminated or non-terminated string, or a
    STL contiguous container holding a string (e.g. std::string,
    std::string_view, std::vector or std::array).  Note that NUL
    characters characters are only treated as terminators for pointers
    and arrays, they are treated as normal characters for other
    containers.  Using a non-contiguous container (e.g. std::list or
    std::deque) will result in undesirable behaviour likely culminating
    in a crash.

    The value type of the format string and the character type of the
    output stream/string need to match.  You can't use a wchar_t format
    to format char output and vice versa.

    The format string encoding must have contiguous decimal digits.  The
    character encoding must not use shift states or multi-byte sequences
    that could result in a format character codepoint appearing as part
    of a multi-byte sequence or being interpreted differently.  ASCII,
    ISO Latin, KOI-8, UTF-8, EUC, EBCDIC, and UTF-EBCDIC encodings meet
    these requirements, while ISO-2022, UTF-7, KOI-7 and Shift-JIS
    encodings do not.  For character types other than char and wchar_t,
    the encoding must be a strict superset of the char encoding.

    The following conditions cause assertion failures in debug builds:
    - Unsupported conversion specifier
    - Out-of-range argument/width/precision position
    - Inappropriate type for parameterised width/precision
    - Positional width/precision specifier not terminated with $
    - Inappropriate type for n conversion
    - Default conversion for type that lacks stream out operator

    Some limitations have been described in passing.  Major limitations
    and bugs include:
    - No automatic widening/narrowing support, so no simple way to
      output wide characters/strings to narrow streams/strings and vice
      versa.
    - Precision ignored for d/i/u/o/x/X conversions (should set minimum
      digits to print).
    - Precision for s/S conversion is only honoured for string-like
      types (output character pointer/array and std::basic_string).
    - If the output character type is not char, signed char or unsgined
      char, printing the a value of this type with d/i/u/o/x/X
      conversion and no length specifier causes it to be printed as a
      character.  Can be worked around by casting to another integer
      type or using length specifier.
    - Printing with d/i/u/o/x/X conversions may not behave as expected
      if the output character type is an integer type other than char,
      signed char, unsigned char, or wchar_t.
    - Only output character pointer/array is treated as a C string, any
      other pointer/array will be printed as a pointer value.  The
      signed/unsigned/default char are not handled equivalently.
    - There is no length specifier to force cast to int/unsigned.  Can
      be worked around by casting the argument.
    - MSVCRT length specifiers I/I32/I64 will not be recognised if no
      width or precision is specified, as they will be mistaken for
      glibc alternate digits flag.
    - The " " flag to prefix positive numbers with a space is not
      implemented.
    - The "'" and "I" flags are not implemented, as digit grouping and
      characters are controlled by the output stream's locale.
    - The characters used for space- and zero-padding are not locale-
      aware.

***************************************************************************/

#ifndef MAME_UTIL_STRFORMAT_H
#define MAME_UTIL_STRFORMAT_H

#pragma once

#include <algorithm>
#include <array>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <limits>
#include <locale>
#include <sstream>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>

namespace util {

namespace detail {

//**************************************************************************
//  FORMAT CHARACTER DEFINITIONS
//**************************************************************************

template <typename Character>
class format_chars
{
public:
	typedef Character char_type;
	enum : Character {
			nul         = Character('\0'),
			space       = Character(' '),
			point       = Character('.'),
			percent     = Character('%'),
			dollar      = Character('$'),
			hash        = Character('#'),
			minus       = Character('-'),
			plus        = Character('+'),
			asterisk    = Character('*'),
			quote       = Character('\''),
			zero        = Character('0'),
			nine        = Character('9'),
			a           = Character('a'),
			A           = Character('A'),
			c           = Character('c'),
			C           = Character('C'),
			d           = Character('d'),
			e           = Character('e'),
			E           = Character('E'),
			f           = Character('f'),
			F           = Character('F'),
			g           = Character('g'),
			G           = Character('G'),
			h           = Character('h'),
			i           = Character('i'),
			I           = Character('I'),
			j           = Character('j'),
			l           = Character('l'),
			L           = Character('L'),
			m           = Character('m'),
			n           = Character('n'),
			o           = Character('o'),
			p           = Character('p'),
			s           = Character('s'),
			S           = Character('S'),
			t           = Character('t'),
			u           = Character('u'),
			w           = Character('w'),
			x           = Character('x'),
			X           = Character('X'),
			z           = Character('z')
		};
};

template <>
class format_chars<wchar_t>
{
public:
	typedef wchar_t char_type;
	enum : wchar_t {
			nul         = L'\0',
			space       = L' ',
			point       = L'.',
			percent     = L'%',
			dollar      = L'$',
			hash        = L'#',
			minus       = L'-',
			plus        = L'+',
			asterisk    = L'*',
			quote       = L'\'',
			zero        = L'0',
			nine        = L'9',
			a           = L'a',
			A           = L'A',
			c           = L'c',
			C           = L'C',
			d           = L'd',
			e           = L'e',
			E           = L'E',
			f           = L'f',
			F           = L'F',
			g           = L'g',
			G           = L'G',
			h           = L'h',
			i           = L'i',
			I           = L'I',
			j           = L'j',
			l           = L'l',
			L           = L'L',
			m           = L'm',
			n           = L'n',
			o           = L'o',
			p           = L'p',
			s           = L's',
			S           = L'S',
			t           = L't',
			u           = L'u',
			w           = L'w',
			x           = L'x',
			X           = L'X',
			z           = L'z'
		};
};


//**************************************************************************
//  FORMAT SPECIFIER ENCAPSULATION
//**************************************************************************

class format_flags
{
public:
	enum class positive_sign {
			none,
			space,                      // ' '
			plus                        // +
		};

	enum class length {
			unspecified,
			character,                  // hh
			short_integer,              // h
			long_integer,               // l
			long_long_integer,          // ll
			long_double,                // L
			integer_maximum,            // j
			size_type,                  // z, I
			pointer_difference,         // t
			integer_32,                 // I32
			integer_64,                 // I64
			wide_character              // w
		};

	enum class conversion {
			unspecified,
			signed_decimal,             // i, d
			unsigned_decimal,           // u
			octal,                      // o
			hexadecimal,                // x, X
			scientific_decimal,         // e, E
			fixed_decimal,              // f, F
			floating_decimal,           // g, G
			scientific_hexadecimal,     // a, A
			character,                  // c, C
			string,                     // s, S
			pointer,                    // p
			tell,                       // n
			strerror,                   // m
			percent                     // %
		};

	format_flags()
		: m_alternate_format(false)
		, m_zero_pad(false)
		, m_left_align(false)
		, m_positive_sign(positive_sign::none)
		, m_digit_grouping(false)
		, m_alternate_digits(false)
		, m_field_width(0)
		, m_precision(-1)
		, m_length(length::unspecified)
		, m_uppercase(false)
		, m_conversion(conversion::unspecified)
	{
	}

	template <typename Character, typename Traits = std::char_traits<Character> >
	void apply(std::basic_ostream<Character, Traits> &stream) const
	{
		using stream_type = std::basic_ostream<Character, Traits>;
		using chars = format_chars<Character>;

		stream.unsetf(
				stream_type::basefield |
				stream_type::adjustfield |
				stream_type::floatfield |
				stream_type::boolalpha |
				stream_type::showbase |
				stream_type::showpoint |
				stream_type::showpos |
				stream_type::uppercase);

		if (get_alternate_format()) stream.setf(stream_type::showbase | stream_type::showpoint);
		stream.fill(get_zero_pad() ? chars::zero : chars::space);
		stream.setf(get_left_align() ? stream_type::left : get_zero_pad() ? stream_type::internal : stream_type::right);
		if (positive_sign::plus == get_positive_sign()) stream.setf(stream_type::showpos);
		stream.precision((get_precision() < 0) ? 6 : get_precision());
		stream.width(get_field_width());
		if (get_uppercase()) stream.setf(stream_type::uppercase);
		switch (get_conversion())
		{
		case conversion::unspecified:
			break;
		case conversion::signed_decimal:
		case conversion::unsigned_decimal:
			stream.setf(stream_type::dec);
			break;
		case conversion::octal:
			stream.setf(stream_type::oct);
			break;
		case conversion::hexadecimal:
			stream.setf(stream_type::hex | stream_type::scientific | stream_type::fixed);
			break;
		case conversion::scientific_decimal:
			stream.setf(stream_type::dec | stream_type::scientific);
			break;
		case conversion::fixed_decimal:
			stream.setf(stream_type::dec | stream_type::fixed);
			break;
		case conversion::floating_decimal:
			stream.setf(stream_type::dec);
			break;
		case conversion::scientific_hexadecimal:
			stream.setf(stream_type::hex | stream_type::scientific | stream_type::fixed);
			break;
		case conversion::character:
		case conversion::string:
		case conversion::pointer:
		case conversion::tell:
		case conversion::strerror:
		case conversion::percent:
			break;
		}
	}

	bool            get_alternate_format() const    { return m_alternate_format; }
	bool            get_zero_pad() const            { return m_zero_pad; }
	bool            get_left_align() const          { return m_left_align; }
	positive_sign   get_positive_sign() const       { return m_positive_sign; }
	bool            get_digit_grouping() const      { return m_digit_grouping; }
	bool            get_alternate_digits() const    { return m_alternate_digits; }
	unsigned        get_field_width() const         { return m_field_width; }
	int             get_precision() const           { return m_precision; }
	length          get_length() const              { return m_length; }
	bool            get_uppercase() const           { return m_uppercase; }
	conversion      get_conversion() const          { return m_conversion; }

	void set_alternate_format()
	{
		m_alternate_format = true;
	}

	void set_zero_pad()
	{
		if (!m_left_align)
		{
			switch (m_conversion)
			{
			case conversion::signed_decimal:
			case conversion::unsigned_decimal:
			case conversion::octal:
			case conversion::hexadecimal:
				m_zero_pad = (0 > m_precision);
				break;
			default:
				m_zero_pad = true;
			}
		}
	}

	void set_left_align()
	{
		m_zero_pad = false;
		m_left_align = true;
	}

	void set_positive_sign_space()
	{
		switch (m_conversion)
		{
		case conversion::unsigned_decimal:
		case conversion::octal:
		case conversion::hexadecimal:
			break;
		default:
			if (positive_sign::plus != m_positive_sign)
				m_positive_sign = positive_sign::space;
		}
	}

	void set_positive_sign_plus()
	{
		switch (m_conversion)
		{
		case conversion::unsigned_decimal:
		case conversion::octal:
		case conversion::hexadecimal:
			break;
		default:
			m_positive_sign = positive_sign::plus;
		}
	}

	void set_digit_grouping()
	{
		m_digit_grouping = true;
	}

	void set_alternate_digits()
	{
		m_alternate_digits = true;
	}

	void set_field_width(int value)
	{
		if (0 > value)
		{
			set_left_align();
			m_field_width = unsigned(-value);
		}
		else
		{
			m_field_width = unsigned(value);
		}
	}

	void set_precision(int value)
	{
		m_precision = value;
		if (0 <= value)
		{
			switch (m_conversion)
			{
			case conversion::signed_decimal:
			case conversion::unsigned_decimal:
			case conversion::octal:
			case conversion::hexadecimal:
				m_zero_pad = false;
				break;
			default:
				break;
			}
		}
	}

	void set_length(length value)
	{
		m_length = value;
	}

	void set_uppercase()
	{
		m_uppercase = true;
	}

	void set_conversion(conversion value)
	{
		m_conversion = value;
		switch (value)
		{
		case conversion::unsigned_decimal:
		case conversion::octal:
		case conversion::hexadecimal:
			m_positive_sign = positive_sign::none;
			[[fallthrough]];
		case conversion::signed_decimal:
			if (0 <= m_precision)
				m_zero_pad = false;
			break;
		default:
			break;
		}
	}

private:
	bool            m_alternate_format; // #
	bool            m_zero_pad;         // 0
	bool            m_left_align;       // -
	positive_sign   m_positive_sign;    // ' ', +
	bool            m_digit_grouping;   // '
	bool            m_alternate_digits; // I
	unsigned        m_field_width;
	int             m_precision;        // .
	length          m_length;           // hh, h, l, ll, L, j, z, I, t, w
	bool            m_uppercase;        // X, E, F, G, A
	conversion      m_conversion;       // i, d, u, o, x, X, e, E, f, F, g, G, a, A, c, C, s, S, p, m, %
};


//**************************************************************************
//  FORMAT OUTPUT HELPERS
//**************************************************************************

template <typename Stream, typename T>
class format_output
{
private:
	template <typename U>
	struct string_semantics : public std::false_type { };
	template <typename CharT, typename Traits, typename Allocator>
	struct string_semantics<std::basic_string<CharT, Traits, Allocator> > : public std::true_type { };
	template <typename CharT, typename Traits>
	struct string_semantics<std::basic_string_view<CharT, Traits> > : public std::true_type { };
	template <typename U>
	using signed_integer_semantics = std::bool_constant<std::is_integral_v<U> && std::is_signed_v<U> >;
	template <typename U>
	using unsigned_integer_semantics = std::bool_constant<std::is_integral_v<U> && !std::is_signed_v<U> >;

	template <typename U>
	static std::enable_if_t<std::is_same_v<std::make_signed_t<U>, std::make_signed_t<char> > || std::is_integral_v<U> > apply_signed(Stream &str, U const &value)
	{
		if constexpr (std::is_same_v<std::make_signed_t<U>, std::make_signed_t<char> >)
			str << int(std::make_signed_t<U>(value));
		else if constexpr (!std::is_signed_v<U> || std::is_same_v<typename Stream::char_type, U>)
			str << std::make_signed_t<U>(value);
#if __cplusplus > 201703L
		else if constexpr (!std::is_invocable_v<decltype([] (auto &x, auto &y) -> decltype(x << y) { return x << y; }), Stream &, U const &>)
			str << std::make_signed_t<U>(value);
#endif
		else
			str << value;
	}

	template <typename U>
	static std::enable_if_t<std::is_same_v<std::make_unsigned_t<U>, std::make_unsigned_t<char> > || std::is_integral_v<U> > apply_unsigned(Stream &str, U const &value)
	{
		if constexpr (std::is_same_v<std::make_unsigned_t<U>, std::make_unsigned_t<char> >)
			str << unsigned(std::make_unsigned_t<U>(value));
		else if constexpr (!std::is_unsigned_v<U> || std::is_same_v<typename Stream::char_type, U>)
			str << std::make_unsigned_t<U>(value);
#if __cplusplus > 201703L
		else if constexpr (!std::is_invocable_v<decltype([] (auto &x, auto &y) -> decltype(x << y) { return x << y; }), Stream &, U const &>)
			str << std::make_unsigned_t<U>(value);
#endif
		else
			str << value;
	}

public:
	template <typename U>
	static void apply(Stream &str, format_flags const &flags, U const &value)
	{
		if constexpr (string_semantics<U>::value)
		{
			int const precision(flags.get_precision());
			if ((0 <= precision) && (value.size() > unsigned(precision)))
			{
				if constexpr (std::is_same_v<typename U::value_type, typename Stream::char_type>)
				{
					unsigned width(flags.get_field_width());
					bool const pad(unsigned(precision) < width);
					typename Stream::fmtflags const adjust(str.flags() & Stream::adjustfield);
					if (!pad || (Stream::left == adjust)) str.write(&*value.begin(), unsigned(precision));
					if (pad)
					{
						for (width -= precision; 0U < width; --width) str.put(str.fill());
						if (Stream::left != adjust) str.write(&*value.begin(), unsigned(precision));
					}
					str.width(0);
				}
				else
				{
					str << value.substr(0, unsigned(precision));
				}
			}
			else
			{
				str << value;
			}
		}
		else if constexpr (signed_integer_semantics<U>::value)
		{
			switch (flags.get_conversion())
			{
			case format_flags::conversion::signed_decimal:
				switch (flags.get_length())
				{
				case format_flags::length::character:
					str << int(static_cast<signed char>(value));
					break;
				case format_flags::length::short_integer:
					str << short(value);
					break;
				case format_flags::length::long_integer:
					str << long(value);
					break;
				case format_flags::length::long_long_integer:
					str << static_cast<long long>(value);
					break;
				case format_flags::length::integer_maximum:
					str << std::intmax_t(value);
					break;
				case format_flags::length::size_type:
					str << std::make_signed_t<std::size_t>(value);
					break;
				case format_flags::length::pointer_difference:
					str << std::make_signed_t<std::ptrdiff_t>(value);
					break;
				case format_flags::length::integer_32:
					str << std::uint32_t(std::int32_t(value));
					break;
				case format_flags::length::integer_64:
					str << std::int64_t(value);
					break;
				default:
					apply_signed(str, value);
				}
				break;
			case format_flags::conversion::unsigned_decimal:
			case format_flags::conversion::octal:
			case format_flags::conversion::hexadecimal:
				switch (flags.get_length())
				{
				case format_flags::length::character:
					str << unsigned(static_cast<unsigned char>(static_cast<signed char>(value)));
					break;
				case format_flags::length::short_integer:
					str << static_cast<unsigned short>(short(value));
					break;
				case format_flags::length::long_integer:
					str << static_cast<unsigned long>(long(value));
					break;
				case format_flags::length::long_long_integer:
					str << static_cast<unsigned long long>(static_cast<long long>(value));
					break;
				case format_flags::length::integer_maximum:
					str << std::uintmax_t(std::intmax_t(value));
					break;
				case format_flags::length::size_type:
					str << std::make_unsigned_t<std::size_t>(std::make_signed_t<std::size_t>(value));
					break;
				case format_flags::length::pointer_difference:
					str << std::make_unsigned_t<std::ptrdiff_t>(std::make_signed_t<std::ptrdiff_t>(value));
					break;
				case format_flags::length::integer_32:
					str << std::uint32_t(std::int32_t(value));
					break;
				case format_flags::length::integer_64:
					str << std::uint64_t(std::int64_t(value));
					break;
				default:
					apply_unsigned(str, value);
				}
				break;
			case format_flags::conversion::character:
				if (std::is_signed<typename Stream::char_type>::value)
					str << typename Stream::char_type(value);
				else
					str << typename Stream::char_type(std::make_signed_t<typename Stream::char_type>(value));
				break;
			case format_flags::conversion::pointer:
				str << reinterpret_cast<void const *>(std::uintptr_t(std::intptr_t(value)));
				break;
			default:
				str << value;
			}
		}
		else if constexpr (unsigned_integer_semantics<U>::value)
		{
			switch (flags.get_conversion())
			{
			case format_flags::conversion::signed_decimal:
				switch (flags.get_length())
				{
				case format_flags::length::character:
					str << int(static_cast<signed char>(static_cast<unsigned char>(value)));
					break;
				case format_flags::length::short_integer:
					str << short(static_cast<unsigned short>(value));
					break;
				case format_flags::length::long_integer:
					str << long(static_cast<unsigned long>(value));
					break;
				case format_flags::length::long_long_integer:
					str << static_cast<long long>(static_cast<unsigned long long>(value));
					break;
				case format_flags::length::integer_maximum:
					str << std::intmax_t(std::uintmax_t(value));
					break;
				case format_flags::length::size_type:
					str << std::make_signed_t<std::size_t>(std::make_unsigned_t<std::size_t>(value));
					break;
				case format_flags::length::pointer_difference:
					str << std::make_signed_t<std::ptrdiff_t>(std::make_unsigned_t<std::ptrdiff_t>(value));
					break;
				case format_flags::length::integer_32:
					str << std::int32_t(std::uint32_t(value));
					break;
				case format_flags::length::integer_64:
					str << std::int64_t(std::uint64_t(value));
					break;
				default:
					apply_signed(str, value);
				}
				break;
			case format_flags::conversion::unsigned_decimal:
			case format_flags::conversion::octal:
			case format_flags::conversion::hexadecimal:
				switch (flags.get_length())
				{
				case format_flags::length::character:
					str << unsigned(static_cast<unsigned char>(value));
					break;
				case format_flags::length::short_integer:
					str << static_cast<unsigned short>(value);
					break;
				case format_flags::length::long_integer:
					str << static_cast<unsigned long>(value);
					break;
				case format_flags::length::long_long_integer:
					str << static_cast<unsigned long long>(value);
					break;
				case format_flags::length::integer_maximum:
					str << std::uintmax_t(value);
					break;
				case format_flags::length::size_type:
					str << std::make_unsigned_t<std::size_t>(value);
					break;
				case format_flags::length::pointer_difference:
					str << std::make_unsigned_t<std::ptrdiff_t>(value);
					break;
				case format_flags::length::integer_32:
					str << std::uint32_t(std::int32_t(value));
					break;
				case format_flags::length::integer_64:
					str << std::int64_t(value);
					break;
				default:
					apply_unsigned(str, value);
				}
				break;
			case format_flags::conversion::character:
				if (std::is_signed<typename Stream::char_type>::value)
					str << typename Stream::char_type(value);
				else
					str << typename Stream::char_type(std::make_signed_t<typename Stream::char_type>(value));
				break;
			case format_flags::conversion::pointer:
				str << reinterpret_cast<void const *>(std::uintptr_t(value));
				break;
			default:
#if __cplusplus > 201703L
				if constexpr (!std::is_invocable_v<decltype([] (auto &x, auto &y) -> decltype(x << y) { return x << y; }), Stream &, U const &>)
				{
					assert(false); // stream out operator not declared or declared deleted
					str << '?';
				}
				else
#endif
				{
					str << value;
				}
			}
		}
		else
		{
			str << value;
		}
	}
	static void apply(Stream &str, format_flags const &flags, bool value)
	{
		switch (flags.get_conversion())
		{
		case format_flags::conversion::signed_decimal:
		case format_flags::conversion::unsigned_decimal:
		case format_flags::conversion::octal:
		case format_flags::conversion::hexadecimal:
		case format_flags::conversion::scientific_decimal:
		case format_flags::conversion::fixed_decimal:
		case format_flags::conversion::floating_decimal:
		case format_flags::conversion::scientific_hexadecimal:
		case format_flags::conversion::character:
		case format_flags::conversion::pointer:
			apply(str, flags, unsigned(value));
			break;
		default:
			if (flags.get_alternate_format()) str.setf(Stream::boolalpha);
			str << value;
		}
	}
};

template <typename Stream, typename T>
class format_output<Stream, T *>
{
protected:
	template <typename U>
	using string_semantics = std::bool_constant<std::is_same_v<std::remove_const_t<U>, typename Stream::char_type> >;

public:
	template <typename U>
	static void apply(Stream &str, format_flags const &flags, U const *value)
	{
		if constexpr (string_semantics<U>::value)
		{
			switch (flags.get_conversion())
			{
			case format_flags::conversion::string:
				{
					int precision(flags.get_precision());
					if (0 <= flags.get_precision())
					{
						std::streamsize cnt(0);
						for ( ; (0 < precision) && (U(format_chars<U>::nul) != value[cnt]); --precision, ++cnt) { }
						unsigned width(flags.get_field_width());
						bool const pad(std::make_unsigned_t<std::streamsize>(cnt) < width);
						typename Stream::fmtflags const adjust(str.flags() & Stream::adjustfield);
						if (!pad || (Stream::left == adjust)) str.write(value, cnt);
						if (pad)
						{
							for (width -= cnt; 0U < width; --width) str.put(str.fill());
							if (Stream::left != adjust) str.write(value, cnt);
						}
						str.width(0);
					}
					else
					{
						str << value;
					}
				}
				break;
			case format_flags::conversion::pointer:
				str << reinterpret_cast<void const *>(const_cast<std::remove_volatile_t<U> *>(value));
				break;
			default:
				str << value;
			}
		}
		else
		{
			str << reinterpret_cast<void const *>(const_cast<std::remove_volatile_t<U> *>(value));
		}
	}
};

template <typename Stream, typename T, std::size_t N>
class format_output<Stream, T[N]> : protected format_output<Stream, T *>
{
public:
	template <typename U>
	static void apply(Stream &str, format_flags const &flags, U const *value)
	{
		static_assert(
				!format_output::template string_semantics<U>::value || (N <= size_t(unsigned((std::numeric_limits<int>::max)()))),
				"C string array length must not exceed maximum integer value");
		format_flags f(flags);
		if (format_output::template string_semantics<U>::value && ((0 > f.get_precision()) || (N < unsigned(f.get_precision()))))
			f.set_precision(int(unsigned(N)));
		format_output<Stream, T *>::apply(str, f, value);
	}
};


//**************************************************************************
//  INTEGER INPUT HELPERS
//**************************************************************************

template <typename T>
class format_make_integer
{
private:
	template <typename U>
	using use_unsigned_cast = std::bool_constant<std::is_convertible_v<U const, unsigned> && std::is_unsigned_v<U> >;
	template <typename U>
	using use_signed_cast = std::bool_constant<!use_unsigned_cast<U>::value && std::is_convertible_v<U const, int> >;

public:
	template <typename U> static bool apply(U const &value, int &result)
	{
		if constexpr (use_unsigned_cast<U>::value)
		{
			result = int(unsigned(value));
			return true;
		}
		else if constexpr (use_signed_cast<U>::value)
		{
			result = int(value);
			return true;
		}
		else
		{
			return false;
		}
	}
};


//**************************************************************************
//  INTEGER OUTPUT HELPERS
//**************************************************************************

template <typename T>
class format_store_integer
{
private:
	template <typename U>
	using is_non_const_ptr = std::bool_constant<std::is_pointer_v<U> && !std::is_const_v<std::remove_pointer_t<U> > >;
	template <typename U>
	using is_unsigned_ptr = std::bool_constant<std::is_pointer_v<U> && std::is_unsigned_v<std::remove_pointer_t<U> > >;
	template <typename U>
	using use_unsigned_cast = std::bool_constant<is_non_const_ptr<U>::value && is_unsigned_ptr<U>::value && std::is_convertible_v<std::make_unsigned_t<std::streamoff>, std::remove_pointer_t<U> > >;
	template <typename U>
	using use_signed_cast = std::bool_constant<is_non_const_ptr<U>::value && !use_unsigned_cast<U>::value && std::is_convertible_v<std::streamoff, std::remove_pointer_t<U> > >;

public:
	template <typename U> static bool apply(U const &value, std::streamoff data)
	{
		if constexpr (use_unsigned_cast<U>::value)
		{
			*value = std::remove_pointer_t<U>(std::make_unsigned_t<std::streamoff>(data));
			return true;
		}
		else if constexpr (use_signed_cast<U>::value)
		{
			*value = std::remove_pointer_t<U>(std::make_signed_t<std::streamoff>(data));
			return true;
		}
		else
		{
			assert(false); // inappropriate type for storing characters written so far
			return false;
		}
	}
};


//**************************************************************************
//  NON-POLYMORPHIC ARGUMENT WRAPPER
//**************************************************************************

template <typename Character, typename Traits = std::char_traits<Character> >
class format_argument
{
public:
	using stream_type = std::basic_ostream<Character, Traits>;

	format_argument()
		: m_value(nullptr)
		, m_output_function(nullptr)
		, m_make_integer_function(nullptr)
		, m_store_integer_function(nullptr)
	{
	}

	template <typename T>
	format_argument(T const &value)
		: m_value(reinterpret_cast<void const *>(&value))
		, m_output_function(&static_output<T>)
		, m_make_integer_function(&static_make_integer<T>)
		, m_store_integer_function(&static_store_integer<T>)
	{
	}

	void output(stream_type &str, format_flags const &flags) const { m_output_function(str, flags, m_value); }
	bool make_integer(int &result) const { return m_make_integer_function(m_value, result); }
	void store_integer(std::streamoff data) const { m_store_integer_function(m_value, data); }

private:
	typedef void (*output_function)(stream_type &str, format_flags const &flags, void const *value);
	typedef bool (*make_integer_function)(void const *value, int &result);
	typedef void (*store_integer_function)(void const *value, std::streamoff data);

	template <typename T> static void static_output(stream_type &str, format_flags const &flags, void const *value)
	{
		format_output<stream_type, T>::apply(str, flags, *reinterpret_cast<T const *>(value));
	}

	template <typename T> static bool static_make_integer(void const *value, int &result)
	{
		return format_make_integer<T>::apply(*reinterpret_cast<T const *>(value), result);
	}

	template <typename T> static void static_store_integer(void const *value, std::streamoff data)
	{
		format_store_integer<T>::apply(*reinterpret_cast<T const *>(value), data);
	}

	void const              *m_value;
	output_function         m_output_function;
	make_integer_function   m_make_integer_function;
	store_integer_function  m_store_integer_function;
};


//**************************************************************************
//  NON-POLYMORPHIC ARGUMENT PACK WRAPPER BASE
//**************************************************************************

template <typename Character = char, typename Traits = std::char_traits<Character> >
class format_argument_pack
{
public:
	using stream_type = std::basic_ostream<Character, Traits>;
	using char_type = Character;
	typedef char_type const *iterator;
	iterator format_begin() const
	{
		return m_begin;
	}
	bool format_at_end(iterator it) const
	{
		return (m_end && (m_end == it)) || (m_check_nul && (format_chars<char_type>::nul == *it));
	}
	std::size_t argument_count() const
	{
		return m_argument_count;
	}
	format_argument<char_type, Traits> const &operator[](std::size_t index) const
	{
		assert(m_argument_count > index);
		return m_arguments[index];
	}

protected:
	template <typename T>
	using handle_char_ptr = std::bool_constant<std::is_pointer_v<T> && std::is_same_v<std::remove_cv_t<std::remove_pointer_t<T> >, char_type> >;
	template <typename T>
	using handle_char_array = std::bool_constant<std::is_array_v<T> && std::is_same_v<std::remove_cv_t<std::remove_extent_t<T> >, char_type> >;
	template <typename T>
	using handle_container = std::bool_constant<!handle_char_ptr<T>::value && !handle_char_array<T>::value>;

	template <typename Format>
	format_argument_pack(
			Format &&fmt,
			format_argument<char_type, Traits> const *arguments,
			std::enable_if_t<handle_char_ptr<std::remove_reference_t<Format> >::value, std::size_t> argument_count)
		: m_begin(fmt)
		, m_end(nullptr)
		, m_check_nul(true)
		, m_arguments(arguments)
		, m_argument_count(argument_count)
	{
		assert(m_begin);
		assert(m_end || m_check_nul);
		assert(!m_end || (m_end > m_begin));
		assert(m_arguments || !m_argument_count);
	}
	template <typename Format>
	format_argument_pack(
			Format &&fmt,
			format_argument<char_type, Traits> const *arguments,
			std::enable_if_t<handle_char_array<std::remove_reference_t<Format> >::value, std::size_t> argument_count)
		: m_begin(std::cbegin(fmt))
		, m_end(std::cend(fmt))
		, m_check_nul(true)
		, m_arguments(arguments)
		, m_argument_count(argument_count)
	{
		assert(m_begin);
		assert(m_end || m_check_nul);
		assert(!m_end || (m_end > m_begin));
		assert(m_arguments || !m_argument_count);
	}
	template <typename Format>
	format_argument_pack(
			Format &&fmt,
			format_argument<char_type, Traits> const *arguments,
			std::enable_if_t<handle_container<std::remove_reference_t<Format> >::value, std::size_t> argument_count)
		: m_begin(fmt.empty() ? nullptr : &*std::cbegin(fmt))
		, m_end(fmt.empty() ? nullptr : (m_begin + std::distance(std::cbegin(fmt), std::cend(fmt))))
		, m_check_nul(true)
		, m_arguments(arguments)
		, m_argument_count(argument_count)
	{
		assert(m_begin);
		assert(m_end || m_check_nul);
		assert(!m_end || (m_end > m_begin));
		assert(m_arguments || !m_argument_count);
	}

	format_argument_pack(format_argument_pack<char_type, Traits> const &) = default;
	format_argument_pack(format_argument_pack<char_type, Traits> &&) = default;
	format_argument_pack &operator=(format_argument_pack<char_type, Traits> const &) = default;
	format_argument_pack &operator=(format_argument_pack<char_type, Traits> &&) = default;

private:
	iterator                                    m_begin;
	iterator                                    m_end;
	bool                                        m_check_nul;
	format_argument<char_type, Traits> const    *m_arguments;
	std::size_t                                 m_argument_count;
};


//**************************************************************************
//  ARGUMENT PACK WRAPPER IMPLEMENTATION
//**************************************************************************

template <typename Character, typename Traits, std::size_t Count>
class format_argument_pack_impl
	: private std::array<format_argument<Character, Traits>, Count>
	, public format_argument_pack<Character, Traits>
{
public:
	using typename format_argument_pack<Character, Traits>::iterator;
	using format_argument_pack<Character, Traits>::operator[];

	template <typename Format, typename... Params>
	format_argument_pack_impl(Format &&fmt, Params &&... args)
		: std::array<format_argument<Character, Traits>, Count>({ { format_argument<Character, Traits>(std::forward<Params>(args))... } })
		, format_argument_pack<Character, Traits>(std::forward<Format>(fmt), Count ? &*this->cbegin() : nullptr, Count)
	{
		static_assert(sizeof...(Params) == Count, "Wrong number of constructor arguments");
	}

	format_argument_pack_impl(format_argument_pack_impl<Character, Traits, Count> const &) = default;
	format_argument_pack_impl(format_argument_pack_impl<Character, Traits, Count> &&) = default;
	format_argument_pack_impl &operator=(format_argument_pack_impl<Character, Traits, Count> const &) = default;
	format_argument_pack_impl &operator=(format_argument_pack_impl<Character, Traits, Count> &&) = default;
};


//**************************************************************************
//  ARGUMENT PACK CREATOR FUNCTION
//**************************************************************************

template <typename Stream = std::ostream, typename Format, typename... Params>
inline auto make_format_argument_pack(Format &&fmt, Params &&... args)
{
	return format_argument_pack_impl<typename Stream::char_type, typename Stream::traits_type, sizeof...(Params)>(std::forward<Format>(fmt), std::forward<Params>(args)...);
}


//**************************************************************************
//  FORMAT STRING PARSING HELPER
//**************************************************************************

template <typename Format>
class format_helper : public format_chars<typename Format::char_type>
{
public:
	static bool parse_format(
			Format const &fmt,
			typename Format::iterator &it,
			format_flags &flags,
			int &next_position,
			int &argument_position,
			int &width_position,
			int &precision_position)
	{
		static_assert((format_helper::nine - format_helper::zero) == 9, "Digits must be contiguous");
		assert(!fmt.format_at_end(it));
		assert(format_helper::percent == *it);

		int num;
		int nxt(next_position);
		++it;
		flags = format_flags();
		argument_position = -1;
		width_position = -1;
		precision_position = -1;

		// Leading zeroes are tricky - they could be a zero-pad flag or part of a position specifier
		bool const leading_zero(!fmt.format_at_end(it) && (format_helper::zero == *it));
		while (!fmt.format_at_end(it) && (format_helper::zero == *it)) ++it;

		// Digits encountered at this point could be a field width or a position specifier
		num = 0;
		bool leading_num(have_digit(fmt, it));
		while (have_digit(fmt, it)) add_digit(num, *it++);
		if (leading_num && !have_dollar(fmt, it))
		{
			// No dollar sign, leading number is field width
			if (leading_zero) flags.set_zero_pad();
			flags.set_field_width(num);
		}
		else
		{
			// If we hit a dollar sign after a number, that's a position specifier
			if ((leading_zero || leading_num) && have_dollar(fmt, it))
			{
				argument_position = num;
				++it;
			}
			else if (leading_zero)
			{
				flags.set_zero_pad();
			}

			// Parse flag characters
			while (!fmt.format_at_end(it))
			{
				switch (*it)
				{
				case format_helper::hash:   ++it;   flags.set_alternate_format();       continue;
				case format_helper::zero:   ++it;   flags.set_zero_pad();               continue;
				case format_helper::minus:  ++it;   flags.set_left_align();             continue;
				case format_helper::space:  ++it;   flags.set_positive_sign_space();    continue;
				case format_helper::plus:   ++it;   flags.set_positive_sign_plus();     continue;
				case format_helper::quote:  ++it;   flags.set_digit_grouping();         continue;
				case format_helper::I:      ++it;   flags.set_alternate_digits();       continue;
				default: break;
				}
				break;
			}

			// Check for literal or parameterised field width
			if (!fmt.format_at_end(it))
			{
				if (is_digit(*it))
				{
					flags.set_field_width(read_number(fmt, it));
				}
				else if (format_helper::asterisk == *it)
				{
					++it;
					if (have_digit(fmt, it))
					{
						num = read_number(fmt, it);
						assert(have_dollar(fmt, it)); // invalid positional width
						if (!have_dollar(fmt, it)) return false;
						width_position = num;
						nxt = width_position + 1;
						++it;
					}
					else
					{
						width_position = nxt++;
					}
				}
			}
		}

		// Check for literal or parameterised precision
		if (!fmt.format_at_end(it) && (*it == format_helper::point))
		{
			++it;
			if (have_digit(fmt, it))
			{
				flags.set_precision(read_number(fmt, it));
			}
			else if (!fmt.format_at_end(it) && (format_helper::asterisk == *it))
			{
				++it;
				if (have_digit(fmt, it))
				{
					num = read_number(fmt, it);
					assert(have_dollar(fmt, it)); // invalid positional precision
					if (!have_dollar(fmt, it)) return false;
					precision_position = num;
					nxt = precision_position + 1;
					++it;
				}
				else
				{
					precision_position = nxt++;
				}
			}
			else
			{
				flags.set_precision(0);
			}
		}

		// Check for length modifiers
		if (!fmt.format_at_end(it)) switch (*it)
		{
		case format_helper::h:
			++it;
			if (!fmt.format_at_end(it) && (format_helper::h == *it))
			{
				++it;
				flags.set_length(format_flags::length::character);
			}
			else
			{
				flags.set_length(format_flags::length::short_integer);
			}
			break;
		case format_helper::l:
			++it;
			if (!fmt.format_at_end(it) && (format_helper::l == *it))
			{
				++it;
				flags.set_length(format_flags::length::long_long_integer);
			}
			else
			{
				flags.set_length(format_flags::length::long_integer);
			}
			break;
		case format_helper::L:
			++it;
			flags.set_length(format_flags::length::long_double);
			break;
		case format_helper::j:
			++it;
			flags.set_length(format_flags::length::integer_maximum);
			break;
		case format_helper::z:
			++it;
			flags.set_length(format_flags::length::size_type);
			break;
		case format_helper::t:
			++it;
			flags.set_length(format_flags::length::pointer_difference);
			break;
		case format_helper::I:
			{
				++it;
				format_flags::length length = format_flags::length::size_type;
				if (!fmt.format_at_end(it))
				{
					if ((typename format_helper::char_type(format_helper::zero) + 3) == *it)
					{
						typename Format::iterator tmp(it);
						++tmp;
						if (!fmt.format_at_end(tmp) && ((typename format_helper::char_type(format_helper::zero) + 2) == *tmp))
						{
							length = format_flags::length::integer_32;
							it = ++tmp;
						}
					}
					else if ((typename format_helper::char_type(format_helper::zero) + 6) == *it)
					{
						typename Format::iterator tmp(it);
						++tmp;
						if (!fmt.format_at_end(tmp) && ((typename format_helper::char_type(format_helper::zero) + 4) == *tmp))
						{
							length = format_flags::length::integer_64;
							it = ++tmp;
						}
					}
				}
				flags.set_length(length);
			}
			break;
		case format_helper::w:
			++it;
			flags.set_length(format_flags::length::wide_character);
			break;
		default:
			break;
		}

		// Now we should find a conversion specifier
		assert(!fmt.format_at_end(it)); // missing conversion
		if (fmt.format_at_end(it)) return false;
		switch (*it)
		{
		case format_helper::d:
		case format_helper::i:
			flags.set_conversion(format_flags::conversion::signed_decimal);
			break;
		case format_helper::o:
			flags.set_conversion(format_flags::conversion::octal);
			break;
		case format_helper::u:
			flags.set_conversion(format_flags::conversion::unsigned_decimal);
			break;
		case format_helper::X:
			flags.set_uppercase();
			[[fallthrough]];
		case format_helper::x:
			flags.set_conversion(format_flags::conversion::hexadecimal);
			break;
		case format_helper::E:
			flags.set_uppercase();
			[[fallthrough]];
		case format_helper::e:
			flags.set_conversion(format_flags::conversion::scientific_decimal);
			break;
		case format_helper::F:
			flags.set_uppercase();
			[[fallthrough]];
		case format_helper::f:
			flags.set_conversion(format_flags::conversion::fixed_decimal);
			break;
		case format_helper::G:
			flags.set_uppercase();
			[[fallthrough]];
		case format_helper::g:
			flags.set_conversion(format_flags::conversion::floating_decimal);
			break;
		case format_helper::A:
			flags.set_uppercase();
			[[fallthrough]];
		case format_helper::a:
			flags.set_conversion(format_flags::conversion::scientific_hexadecimal);
			break;
		case format_helper::C:
			if (format_flags::length::unspecified == flags.get_length())
				flags.set_length(format_flags::length::long_integer);
			[[fallthrough]];
		case format_helper::c:
			flags.set_conversion(format_flags::conversion::character);
			break;
		case format_helper::S:
			if (format_flags::length::unspecified == flags.get_length())
				flags.set_length(format_flags::length::long_integer);
			[[fallthrough]];
		case format_helper::s:
			flags.set_conversion(format_flags::conversion::string);
			break;
		case format_helper::p:
			flags.set_conversion(format_flags::conversion::pointer);
			break;
		case format_helper::n:
			flags.set_conversion(format_flags::conversion::tell);
			break;
		case format_helper::m:
			flags.set_conversion(format_flags::conversion::strerror);
			break;
		case format_helper::percent:
			flags.set_conversion(format_flags::conversion::percent);
			break;
		default:
			assert(false); // unsupported conversion
			return false;
		}
		++it;

		// Finalise argument position
		if (argument_position < 0) argument_position = nxt;
		next_position = argument_position;
		switch (flags.get_conversion())
		{
		case format_flags::conversion::strerror:
		case format_flags::conversion::percent:
			break;
		default:
			++next_position;
		}
		return true;
	}

private:
	static bool have_dollar(Format const &fmt, typename Format::iterator const &it)
	{
		return !fmt.format_at_end(it) && (*it == format_helper::dollar);
	}

	static bool have_digit(Format const &fmt, typename Format::iterator const &it)
	{
		return !fmt.format_at_end(it) && is_digit(*it);
	}

	static bool is_digit(typename format_helper::char_type value)
	{
		return (format_helper::zero <= value) && (format_helper::nine >= value);
	}

	static int digit_value(typename format_helper::char_type value)
	{
		assert(is_digit(value));
		return int(std::make_signed_t<decltype(value)>(value - format_helper::zero));
	}

	static void add_digit(int &num, typename format_helper::char_type digit)
	{
		num = (num * 10) + digit_value(digit);
	}

	static int read_number(Format const &fmt, typename Format::iterator &it)
	{
		assert(have_digit(fmt, it));
		int value = 0;
		do add_digit(value, *it++); while (have_digit(fmt, it));
		return value;
	}
};


//**************************************************************************
//  CORE FORMATTING FUNCTION
//**************************************************************************

template <typename Character, typename Traits = std::char_traits<Character> >
typename std::basic_ostream<Character, Traits>::off_type stream_format(std::basic_ostream<Character, Traits> &str, format_argument_pack<Character, Traits> const &args)
{
	using stream_type = std::basic_ostream<Character, Traits>;
	using format_helper = format_helper<format_argument_pack<Character, Traits> >;
	using iterator = typename format_argument_pack<Character, Traits>::iterator;
	class stream_preserver
	{
	public:
		stream_preserver(stream_type &stream)
			: m_stream(stream)
			, m_fill(stream.fill())
			, m_flags(stream.flags())
			, m_precision(stream.precision())
			, m_width(stream.width())
		{
		}
		~stream_preserver()
		{
			m_stream.width(m_width);
			m_stream.precision(m_precision);
			m_stream.flags(m_flags);
			m_stream.fill(m_fill);
		}
	private:
		stream_type                     &m_stream;
		typename stream_type::char_type m_fill;
		typename stream_type::fmtflags  m_flags;
		std::streamsize                 m_precision;
		std::streamsize                 m_width;
	};

	typename stream_type::pos_type const begin(str.tellp());
	stream_preserver const preserver(str);
	int next_pos(1);
	iterator start = args.format_begin();
	for (iterator it = start; !args.format_at_end(start); )
	{
		while (!args.format_at_end(it) && (format_helper::percent != *it)) ++it;
		if (start != it)
		{
			str.write(&*start, it - start);
			start = it;
		}
		if (!args.format_at_end(it))
		{
			// Try to parse a percent format specification
			format_flags flags;
			int arg_pos, width_pos, prec_pos;
			if (!format_helper::parse_format(args, it, flags, next_pos, arg_pos, width_pos, prec_pos))
				continue;

			// Handle parameterised width
			if (0 <= width_pos)
			{
				assert(flags.get_field_width() == 0U);
				assert(0 < width_pos);
				assert(args.argument_count() >= unsigned(width_pos));
				if ((0 < width_pos) && (args.argument_count() >= unsigned(width_pos)))
				{
					int width;
					if (args[width_pos - 1].make_integer(width))
					{
						if (0 > width)
						{
							flags.set_left_align();
							flags.set_field_width(unsigned(-width));
						}
						else
						{
							flags.set_field_width(unsigned(width));
						}
					}
					else
					{
						assert(false); // inappropriate type passed as width argument
					}
				}
			}

			// Handle parameterised precision
			if (0 <= prec_pos)
			{
				assert(flags.get_precision() < 0);
				assert(0 < prec_pos);
				assert(args.argument_count() >= unsigned(prec_pos));
				if ((0 < prec_pos) && (args.argument_count() >= unsigned(prec_pos)))
				{
					int precision;
					if (args[prec_pos - 1].make_integer(precision))
						flags.set_precision(precision);
					else
						assert(false); // inappropriate type passed as precision argument
				}
			}

			// Some conversions don't actually take an argument - get them out of the way
			flags.apply(str);
			if (format_flags::conversion::strerror == flags.get_conversion())
			{
				str << std::strerror(errno);
				start = it;
			}
			else if (format_flags::conversion::percent == flags.get_conversion())
			{
				str << Character(format_chars<Character>::percent);
				start = it;
			}
			else
			{
				assert(0 < arg_pos);
				assert(args.argument_count() >= unsigned(arg_pos));
				if ((0 >= arg_pos) || (args.argument_count() < unsigned(arg_pos)))
					continue;
				if (format_flags::conversion::tell == flags.get_conversion())
				{
					typename stream_type::pos_type const current(str.tellp());
					args[arg_pos - 1].store_integer(
							((typename stream_type::pos_type(-1) == begin) || (typename stream_type::pos_type(-1) == current))
								? typename stream_type::off_type(-1)
								: (current - begin));
				}
				else
				{
					args[arg_pos - 1].output(str, flags);
				}
				start = it;
			}
		}
	}
	typename stream_type::pos_type const end(str.tellp());
	return ((typename stream_type::pos_type(-1) == begin) || (typename stream_type::pos_type(-1) == end))
			? typename stream_type::off_type(-1)
			: (end - begin);
}

} // namespace detail


//**************************************************************************
//  FORMAT TO STREAM FUNCTIONS
//**************************************************************************

template <typename Stream, typename Format, typename... Params>
inline typename Stream::off_type stream_format(Stream &str, Format const &fmt, Params &&... args)
{
	return detail::stream_format(str, detail::make_format_argument_pack<Stream>(fmt, std::forward<Params>(args)...));
}

template <typename Stream, typename Character, typename Traits>
inline typename Stream::off_type stream_format(Stream &str, detail::format_argument_pack<Character, Traits> const &args)
{
	return detail::stream_format(str, args);
}

template <typename Stream, typename Character, typename Traits>
inline typename Stream::off_type stream_format(Stream &str, detail::format_argument_pack<Character, Traits> &&args)
{
	return detail::stream_format(str, args);
}


//**************************************************************************
//  FORMAT TO NEW STRING FUNCTIONS
//**************************************************************************

template <typename String = std::string, typename Format, typename... Params>
inline String string_format(Format &&fmt, Params &&... args)
{
	using ostream = std::basic_ostringstream<typename String::value_type, typename String::traits_type, typename String::allocator_type>;
	ostream str;
	stream_format(str, fmt, std::forward<Params>(args)...);
	return str.str();
};

template <typename String = std::string, typename Format, typename... Params>
inline String string_format(std::locale const &locale, Format &&fmt, Params &&... args)
{
	using ostream = std::basic_ostringstream<typename String::value_type, typename String::traits_type, typename String::allocator_type>;
	ostream str;
	str.imbue(locale);
	stream_format(str, fmt, std::forward<Params>(args)...);
	return str.str();
};

template <typename String = std::string>
inline String string_format(detail::format_argument_pack<typename String::value_type, typename String::traits_type> const &args)
{
	using ostream = std::basic_ostringstream<typename String::value_type, typename String::traits_type, typename String::allocator_type>;
	ostream str;
	detail::stream_format(str, args);
	return str.str();
};

template <typename String = std::string>
inline String string_format(detail::format_argument_pack<typename String::value_type, typename String::traits_type> &&args)
{
	using ostream = std::basic_ostringstream<typename String::value_type, typename String::traits_type, typename String::allocator_type>;
	ostream str;
	detail::stream_format(str, std::move(args));
	return str.str();
};

template <typename String = std::string>
inline String string_format(std::locale const &locale, detail::format_argument_pack<typename String::value_type, typename String::traits_type> const &args)
{
	using ostream = std::basic_ostringstream<typename String::value_type, typename String::traits_type, typename String::allocator_type>;
	ostream str;
	str.imbue(locale);
	detail::stream_format(str, args);
	return str.str();
};

template <typename String = std::string>
inline String string_format(std::locale const &locale, detail::format_argument_pack<typename String::value_type, typename String::traits_type> &&args)
{
	using ostream = std::basic_ostringstream<typename String::value_type, typename String::traits_type, typename String::allocator_type>;
	ostream str;
	str.imbue(locale);
	detail::stream_format(str, std::move(args));
	return str.str();
};


//**************************************************************************
//  CREATING ARGUMENT PACKS
//**************************************************************************

using detail::format_argument_pack;
using detail::make_format_argument_pack;

} // namespace util


//**************************************************************************
//  EXTERNAL TEMPLATE INSTANTIATIONS
//**************************************************************************

namespace util {

namespace detail {

extern template class format_chars<char>;
extern template class format_chars<wchar_t>;

extern template void format_flags::apply(std::ostream &) const;
extern template void format_flags::apply(std::wostream &) const;

extern template class format_argument<char>;
extern template void format_argument<char>::static_output<bool>(std::ostream &, format_flags const &, void const *);
extern template void format_argument<char>::static_output<char>(std::ostream &, format_flags const &, void const *);
extern template void format_argument<char>::static_output<signed char>(std::ostream &, format_flags const &, void const *);
extern template void format_argument<char>::static_output<unsigned char>(std::ostream &, format_flags const &, void const *);
extern template void format_argument<char>::static_output<short>(std::ostream &, format_flags const &, void const *);
extern template void format_argument<char>::static_output<unsigned short>(std::ostream &, format_flags const &, void const *);
extern template void format_argument<char>::static_output<int>(std::ostream &, format_flags const &, void const *);
extern template void format_argument<char>::static_output<unsigned int>(std::ostream &, format_flags const &, void const *);
extern template void format_argument<char>::static_output<long>(std::ostream &, format_flags const &, void const *);
extern template void format_argument<char>::static_output<unsigned long>(std::ostream &, format_flags const &, void const *);
extern template void format_argument<char>::static_output<long long>(std::ostream &, format_flags const &, void const *);
extern template void format_argument<char>::static_output<unsigned long long>(std::ostream &, format_flags const &, void const *);
extern template void format_argument<char>::static_output<char *>(std::ostream &, format_flags const &, void const *);
extern template void format_argument<char>::static_output<char const *>(std::ostream &, format_flags const &, void const *);
extern template void format_argument<char>::static_output<std::string>(std::ostream &, format_flags const &, void const *);
extern template void format_argument<char>::static_output<std::string_view>(std::ostream &, format_flags const &, void const *);
extern template bool format_argument<char>::static_make_integer<bool>(void const *, int &);
extern template bool format_argument<char>::static_make_integer<char>(void const *, int &);
extern template bool format_argument<char>::static_make_integer<signed char>(void const *, int &);
extern template bool format_argument<char>::static_make_integer<unsigned char>(void const *, int &);
extern template bool format_argument<char>::static_make_integer<short>(void const *, int &);
extern template bool format_argument<char>::static_make_integer<unsigned short>(void const *, int &);
extern template bool format_argument<char>::static_make_integer<int>(void const *, int &);
extern template bool format_argument<char>::static_make_integer<unsigned int>(void const *, int &);
extern template bool format_argument<char>::static_make_integer<long>(void const *, int &);
extern template bool format_argument<char>::static_make_integer<unsigned long>(void const *, int &);
extern template bool format_argument<char>::static_make_integer<long long>(void const *, int &);
extern template bool format_argument<char>::static_make_integer<unsigned long long>(void const *, int &);
extern template bool format_argument<char>::static_make_integer<char *>(void const *, int &);
extern template bool format_argument<char>::static_make_integer<char const *>(void const *, int &);
extern template bool format_argument<char>::static_make_integer<std::string>(void const *, int &);
extern template bool format_argument<char>::static_make_integer<std::string_view>(void const *, int &);
extern template void format_argument<char>::static_store_integer<bool>(void const *, std::streamoff);
extern template void format_argument<char>::static_store_integer<char>(void const *, std::streamoff);
extern template void format_argument<char>::static_store_integer<signed char>(void const *, std::streamoff);
extern template void format_argument<char>::static_store_integer<unsigned char>(void const *, std::streamoff);
extern template void format_argument<char>::static_store_integer<short>(void const *, std::streamoff);
extern template void format_argument<char>::static_store_integer<unsigned short>(void const *, std::streamoff);
extern template void format_argument<char>::static_store_integer<int>(void const *, std::streamoff);
extern template void format_argument<char>::static_store_integer<unsigned int>(void const *, std::streamoff);
extern template void format_argument<char>::static_store_integer<long>(void const *, std::streamoff);
extern template void format_argument<char>::static_store_integer<unsigned long>(void const *, std::streamoff);
extern template void format_argument<char>::static_store_integer<long long>(void const *, std::streamoff);
extern template void format_argument<char>::static_store_integer<unsigned long long>(void const *, std::streamoff);
extern template void format_argument<char>::static_store_integer<char *>(void const *, std::streamoff);
extern template void format_argument<char>::static_store_integer<char const *>(void const *, std::streamoff);
extern template void format_argument<char>::static_store_integer<std::string>(void const *, std::streamoff);
extern template void format_argument<char>::static_store_integer<std::string_view>(void const *, std::streamoff);

extern template class format_argument<wchar_t>;
extern template void format_argument<wchar_t>::static_output<bool>(std::wostream &, format_flags const &, void const *);
extern template void format_argument<wchar_t>::static_output<char>(std::wostream &, format_flags const &, void const *);
extern template void format_argument<wchar_t>::static_output<signed char>(std::wostream &, format_flags const &, void const *);
extern template void format_argument<wchar_t>::static_output<unsigned char>(std::wostream &, format_flags const &, void const *);
extern template void format_argument<wchar_t>::static_output<wchar_t>(std::wostream &, format_flags const &, void const *);
extern template void format_argument<wchar_t>::static_output<short>(std::wostream &, format_flags const &, void const *);
extern template void format_argument<wchar_t>::static_output<unsigned short>(std::wostream &, format_flags const &, void const *);
extern template void format_argument<wchar_t>::static_output<int>(std::wostream &, format_flags const &, void const *);
extern template void format_argument<wchar_t>::static_output<unsigned int>(std::wostream &, format_flags const &, void const *);
extern template void format_argument<wchar_t>::static_output<long>(std::wostream &, format_flags const &, void const *);
extern template void format_argument<wchar_t>::static_output<unsigned long>(std::wostream &, format_flags const &, void const *);
extern template void format_argument<wchar_t>::static_output<long long>(std::wostream &, format_flags const &, void const *);
extern template void format_argument<wchar_t>::static_output<unsigned long long>(std::wostream &, format_flags const &, void const *);
extern template void format_argument<wchar_t>::static_output<wchar_t *>(std::wostream &, format_flags const &, void const *);
extern template void format_argument<wchar_t>::static_output<wchar_t const *>(std::wostream &, format_flags const &, void const *);
extern template void format_argument<wchar_t>::static_output<std::wstring>(std::wostream &, format_flags const &, void const *);
extern template void format_argument<wchar_t>::static_output<std::wstring_view>(std::wostream &, format_flags const &, void const *);
extern template bool format_argument<wchar_t>::static_make_integer<bool>(void const *, int &);
extern template bool format_argument<wchar_t>::static_make_integer<char>(void const *, int &);
extern template bool format_argument<wchar_t>::static_make_integer<signed char>(void const *, int &);
extern template bool format_argument<wchar_t>::static_make_integer<unsigned char>(void const *, int &);
extern template bool format_argument<wchar_t>::static_make_integer<wchar_t>(void const *, int &);
extern template bool format_argument<wchar_t>::static_make_integer<short>(void const *, int &);
extern template bool format_argument<wchar_t>::static_make_integer<unsigned short>(void const *, int &);
extern template bool format_argument<wchar_t>::static_make_integer<int>(void const *, int &);
extern template bool format_argument<wchar_t>::static_make_integer<unsigned int>(void const *, int &);
extern template bool format_argument<wchar_t>::static_make_integer<long>(void const *, int &);
extern template bool format_argument<wchar_t>::static_make_integer<unsigned long>(void const *, int &);
extern template bool format_argument<wchar_t>::static_make_integer<long long>(void const *, int &);
extern template bool format_argument<wchar_t>::static_make_integer<unsigned long long>(void const *, int &);
extern template bool format_argument<wchar_t>::static_make_integer<wchar_t *>(void const *, int &);
extern template bool format_argument<wchar_t>::static_make_integer<wchar_t const *>(void const *, int &);
extern template bool format_argument<wchar_t>::static_make_integer<std::wstring>(void const *, int &);
extern template bool format_argument<wchar_t>::static_make_integer<std::wstring_view>(void const *, int &);
extern template void format_argument<wchar_t>::static_store_integer<bool>(void const *, std::streamoff);
extern template void format_argument<wchar_t>::static_store_integer<char>(void const *, std::streamoff);
extern template void format_argument<wchar_t>::static_store_integer<signed char>(void const *, std::streamoff);
extern template void format_argument<wchar_t>::static_store_integer<unsigned char>(void const *, std::streamoff);
extern template void format_argument<wchar_t>::static_store_integer<wchar_t>(void const *, std::streamoff);
extern template void format_argument<wchar_t>::static_store_integer<short>(void const *, std::streamoff);
extern template void format_argument<wchar_t>::static_store_integer<unsigned short>(void const *, std::streamoff);
extern template void format_argument<wchar_t>::static_store_integer<int>(void const *, std::streamoff);
extern template void format_argument<wchar_t>::static_store_integer<unsigned int>(void const *, std::streamoff);
extern template void format_argument<wchar_t>::static_store_integer<long>(void const *, std::streamoff);
extern template void format_argument<wchar_t>::static_store_integer<unsigned long>(void const *, std::streamoff);
extern template void format_argument<wchar_t>::static_store_integer<long long>(void const *, std::streamoff);
extern template void format_argument<wchar_t>::static_store_integer<unsigned long long>(void const *, std::streamoff);
extern template void format_argument<wchar_t>::static_store_integer<wchar_t *>(void const *, std::streamoff);
extern template void format_argument<wchar_t>::static_store_integer<wchar_t const *>(void const *, std::streamoff);
extern template void format_argument<wchar_t>::static_store_integer<std::wstring>(void const *, std::streamoff);
extern template void format_argument<wchar_t>::static_store_integer<std::wstring_view>(void const *, std::streamoff);

extern template class format_argument_pack<char>;
extern template class format_argument_pack<wchar_t>;

extern template std::ostream::off_type stream_format(std::ostream &, format_argument_pack<char> const &);
extern template std::wostream::off_type stream_format(std::wostream &, format_argument_pack<wchar_t> const &);

} // namespace detail

} // namespace util

#endif // MAME_UTIL_STRFORMAT_H