Overview
Comment: | natools-chunked_strings: new package for efficient storage and append on large strings |
---|---|
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
99dd3c799b97c20b148831233747063f |
User & Date: | nat on 2011-11-29 14:39:57 |
Other Links: | manifest | tags |
Context
2011-11-29
| ||
14:41 | natools-chunked_strings-tests*: test suite for Chunked_String, derived from ACATS tests for Unbounded_String check-in: 26186560cc user: nat tags: trunk | |
14:39 | natools-chunked_strings: new package for efficient storage and append on large strings check-in: 99dd3c799b user: nat tags: trunk | |
14:13 | natools-accumulators-string_accumulator_linked_lists: simple implementation of String_Accumulator_Stack check-in: e2088218f7 user: nat tags: trunk | |
Changes
Added natools-chunked_strings.adb version [a7ad3f6741].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 | ------------------------------------------------------------------------------ -- Copyright (c) 2011, Natacha Porté -- -- -- -- Permission to use, copy, modify, and distribute this software for any -- -- purpose with or without fee is hereby granted, provided that the above -- -- copyright notice and this permission notice appear in all copies. -- -- -- -- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -- -- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -- -- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -- -- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -- -- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -- -- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -- -- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -- ------------------------------------------------------------------------------ with Ada.Strings; with Ada.Strings.Fixed; with Ada.Unchecked_Deallocation; package body Natools.Chunked_Strings is package Fixed renames Ada.Strings.Fixed; type Relation is (Equal, Greater, Lesser); ----------------------- -- Local subprograms -- ----------------------- function Allocated_Size (Source : in Chunked_String) return Natural; pragma Inline (Allocated_Size); -- Return the number of Characters that can currently fit in Source function Chunks_For (Size : in Natural; Chunk_Size : in Positive; Allocation_Unit : in Positive) return Natural; pragma Inline (Chunks_For); -- Return the number of chunks to accommodate Size characters generic type Map_Type is private; with function Count (Source : in String; Pattern : in String; Mapping : in Map_Type) return Natural; function Count_Gen (Source : in Chunked_String; Pattern : in String; Mapping : in Map_Type) return Natural; -- Count the number of non-overlapping occurrences of the pattern function Compare (Left : in Chunk_Array; Left_Size : in Natural; Right : in Chunk_Array; Right_Size : in Natural) return Relation; function Compare (Left : in Chunk_Array_Access; Left_Size : in Natural; Right : in Chunk_Array_Access; Right_Size : in Natural) return Relation; function Compare (Left : in Chunk_Array; Left_Size : in Natural; Right : in String) return Relation; function Compare (Left : in Chunk_Array_Access; Left_Size : in Natural; Right : in String) return Relation; -- String comparisons procedure Fill (Data : in out Chunk_Array; From : in Positive; Count : in Natural; C : in Character; Chunk_Size : in Positive); -- Fill an area of the chunks with the given Character procedure Free (Data : in out Chunk_Array_Access); -- Free data associated to all chunks and to the chunk array generic type Map_Type is private; with function Index (Source : String; Pattern : String; From : Positive; Going : Ada.Strings.Direction; Map : Map_Type) return Natural; function Index_Gen (Source : Chunked_String; Pattern : String; From : Positive; Going : Ada.Strings.Direction; Map : Map_Type) return Natural; -- Search for a pattern in a source as described in the ARM procedure Move (Target : in out Chunk_Array; Target_Position : in Positive; Source : in out Chunk_Array; Source_Position : in Positive; Length : in Natural); -- Moves characters from one Chunk_Array to another, even when they -- do not have the same chunk size procedure Move (Target : in out Chunk_Array; Source : in String; Position : in Positive; Chunk_Size : in Positive); -- Writes the string in the chunk array, which must be large enough procedure Move (Target : out String; Source : in Chunk_Array; From : in Positive); -- Fills a string using characters from the Chunk_Array procedure Move (Data : in out Chunk_Array; Target_Position : in Positive; Source_Position : in Positive; Length : in Positive; Chunk_Size : in Positive); -- Move a slice of data inside a given chunk array procedure Resize_Chunk (Chunk : in out String_Access; Size : in Positive); -- Resize a chunk to the target set procedure Resize_Chunks (Data : in out Chunk_Array_Access; Size : in Natural; Chunk_Size : in Positive; Allocation_Unit : in Positive; Can_Shrink : in Boolean := True); -- Resize Data to fit Size characters procedure Trim_Bounds (Source : in Chunked_String; Left : in Maps.Character_Set; Right : in Maps.Character_Set; Low : out Positive; High : out Natural); -- Compute slice bounds of the trimmed result function Units_For (Size : in Natural; Chunk_Size : in Positive; Allocation_Unit : in Positive) return Natural; pragma Inline (Units_For); -- Return the number of allocation units in the last chunk --------------------------------------- -- Chunked_String memory subprograms -- --------------------------------------- function Allocated_Size (Source : in Chunked_String) return Natural is begin if Source.Data = null or else Source.Data'Last < 1 then return 0; end if; return (Source.Data'Last - 1) * Source.Chunk_Size + Source.Data (Source.Data'Last)'Last; end Allocated_Size; function Chunks_For (Size : in Natural; Chunk_Size : in Positive; Allocation_Unit : in Positive) return Natural is begin pragma Unreferenced (Allocation_Unit); return (Size + Chunk_Size - 1) / Chunk_Size; end Chunks_For; procedure Free (Data : in out Chunk_Array_Access) is procedure Deallocate is new Ada.Unchecked_Deallocation (Chunk_Array, Chunk_Array_Access); begin if Data = null then return; end if; for J in Data'Range loop Free (Data (J)); end loop; Deallocate (Data); end Free; procedure Resize_Chunk (Chunk : in out String_Access; Size : in Positive) is New_Chunk : String_Access; begin if Size /= Chunk'Length then New_Chunk := new String (1 .. Size); if Size < Chunk'Length then New_Chunk.all := Chunk (Chunk'First .. Chunk'First + Size - 1); else New_Chunk.all (1 .. Chunk'Length) := Chunk.all; end if; Free (Chunk); Chunk := New_Chunk; end if; end Resize_Chunk; procedure Resize_Chunks (Data : in out Chunk_Array_Access; Size : in Natural; Chunk_Size : in Positive; Allocation_Unit : in Positive; Can_Shrink : in Boolean := True) is procedure Deallocate is new Ada.Unchecked_Deallocation (Chunk_Array, Chunk_Array_Access); Chunk_Nb : constant Natural := Chunks_For (Size, Chunk_Size, Allocation_Unit); Last_Chunk_Size : constant Natural := Units_For (Size, Chunk_Size, Allocation_Unit) * Allocation_Unit; begin if Size = 0 then if Can_Shrink then Free (Data); end if; return; end if; pragma Assert (Chunk_Nb > 0); if Data = null or else Data'Length < Chunk_Nb then declare First_New : Positive := 1; New_Data : Chunk_Array_Access := new Chunk_Array (1 .. Chunk_Nb); begin if Data /= null then Resize_Chunk (Data (Data'Last), Chunk_Size); New_Data (1 .. Data'Last) := Data.all; First_New := Data'Last + 1; Deallocate (Data); end if; Data := New_Data; for J in First_New .. Data'Last - 1 loop Data (J) := new String (1 .. Chunk_Size); end loop; Data (Data'Last) := new String (1 .. Last_Chunk_Size); end; elsif Data'Length > Chunk_Nb then if Can_Shrink then declare New_Data : constant Chunk_Array_Access := new Chunk_Array (1 .. Chunk_Nb); begin Resize_Chunk (Data (Chunk_Nb), Last_Chunk_Size); for J in Chunk_Nb + 1 .. Data'Last loop Free (Data (J)); end loop; New_Data.all := Data (1 .. Chunk_Nb); Data := New_Data; end; end if; else -- Data'Length = Chunk_Nb if Last_Chunk_Size > Data (Data'Last).all'Last or Can_Shrink then Resize_Chunk (Data (Data'Last), Last_Chunk_Size); end if; end if; end Resize_Chunks; function Units_For (Size : in Natural; Chunk_Size : in Positive; Allocation_Unit : in Positive) return Natural is begin return (((Size + Chunk_Size - 1) mod Chunk_Size + 1) + Allocation_Unit - 1) / Allocation_Unit; end Units_For; --------------------------- -- Low-level subprograms -- --------------------------- function Compare (Left : in Chunk_Array; Left_Size : in Natural; Right : in Chunk_Array; Right_Size : in Natural) return Relation is L_Chunk : Positive := Left'First; L_Pos : Positive := Left (L_Chunk).all'First; L_Remain : Natural := Left_Size; R_Chunk : Positive := Right'First; R_Pos : Positive := Right (R_Chunk).all'First; R_Remain : Natural := Right_Size; Step : Positive; begin loop Step := Positive'Min (Natural'Min (Left (L_Chunk).all'Last - L_Pos + 1, L_Remain), Natural'Min (Right (R_Chunk).all'Last - R_Pos + 1, R_Remain)); declare L_Part : String renames Left (L_Chunk).all (L_Pos .. L_Pos + Step - 1); R_Part : String renames Right (R_Chunk).all (R_Pos .. R_Pos + Step - 1); begin if L_Part < R_Part then return Lesser; elsif L_Part > R_Part then return Greater; end if; end; L_Remain := L_Remain - Step; R_Remain := R_Remain - Step; if L_Remain = 0 and R_Remain = 0 then return Equal; elsif L_Remain = 0 then return Lesser; elsif R_Remain = 0 then return Greater; end if; L_Pos := L_Pos + Step; R_Pos := R_Pos + Step; if L_Pos > Left (L_Chunk).all'Last then if L_Chunk = Left'Last then if R_Pos <= Right (R_Chunk).all'Last or R_Chunk < Right'Last then return Lesser; else return Equal; end if; end if; L_Chunk := L_Chunk + 1; L_Pos := Left (L_Chunk).all'First; end if; if R_Pos > Right (R_Chunk).all'Last then if R_Chunk = Right'Last then return Greater; end if; R_Chunk := R_Chunk + 1; R_Pos := Right (R_Chunk).all'First; end if; end loop; end Compare; function Compare (Left : in Chunk_Array_Access; Left_Size : in Natural; Right : in Chunk_Array_Access; Right_Size : in Natural) return Relation is begin if Left = null or Left_Size = 0 then if Right = null or Right_Size = 0 then return Equal; else return Lesser; end if; else if Right = null or Right_Size = 0 then return Greater; else return Compare (Left.all, Left_Size, Right.all, Right_Size); end if; end if; end Compare; function Compare (Left : in Chunk_Array; Left_Size : in Natural; Right : in String) return Relation is Chunk : Positive := Left'First; L_Pos : Positive := Left (Chunk).all'First; L_Remain : Natural := Left_Size; R_Pos : Positive := Right'First; Step : Positive; begin loop Step := Positive'Min (Positive'Min (Left (Chunk).all'Last - L_Pos + 1, L_Remain), Right'Last - R_Pos + 1); declare L_Part : String renames Left (Chunk).all (L_Pos .. L_Pos + Step - 1); R_Part : String renames Right (R_Pos .. R_Pos + Step - 1); begin if L_Part < R_Part then return Lesser; elsif L_Part > R_Part then return Greater; end if; end; L_Remain := L_Remain - Step; if L_Remain = 0 then if R_Pos + Step > Right'Last then return Equal; else return Lesser; end if; end if; L_Pos := L_Pos + Step; R_Pos := R_Pos + Step; if L_Pos > Left (Chunk).all'Last then if Chunk = Left'Last then if R_Pos <= Right'Last then return Lesser; else return Equal; end if; end if; Chunk := Chunk + 1; L_Pos := Left (Chunk).all'First; end if; if R_Pos > Right'Last then return Greater; end if; end loop; end Compare; function Compare (Left : in Chunk_Array_Access; Left_Size : in Natural; Right : in String) return Relation is begin if Left = null or Left_Size = 0 then if Right'Length = 0 then return Equal; else return Lesser; end if; else if Right'Length = 0 then return Greater; else return Compare (Left.all, Left_Size, Right); end if; end if; end Compare; procedure Fill (Data : in out Chunk_Array; From : in Positive; Count : in Natural; C : in Character; Chunk_Size : in Positive) is Chunk : Positive := (From - 1) / Chunk_Size + 1; Offset : Positive := (From - 1) mod Chunk_Size + 1; Done : Natural := 0; Step : Positive; begin while Done < Count loop Step := Positive'Min (Count - Done, Data (Chunk).all'Last - Offset + 1); Data (Chunk).all (Offset .. Offset + Step - 1) := Ada.Strings.Fixed."*" (Step, C); Chunk := Chunk + 1; Offset := 1; Done := Done + Step; end loop; end Fill; function Is_Valid (Source : in Chunked_String) return Boolean is begin -- Null data is only acceptable when the string is empty. if Source.Data = null then return Source.Size = 0; end if; -- Data array must contain non-null chunks of even size declare D : Chunk_Array renames Source.Data.all; begin if D'First /= 1 then return False; end if; for J in D'Range loop if D (J) = null then return False; end if; if D (J).all'First /= 1 or (J < D'Last and D (J).all'Last /= Source.Chunk_Size) then return False; end if; end loop; end; -- Real size must be smaller than allocated size if Source.Size > Allocated_Size (Source) then return False; end if; return True; end Is_Valid; procedure Move (Target : in out Chunk_Array; Target_Position : in Positive; Source : in out Chunk_Array; Source_Position : in Positive; Length : in Natural) is Count : Natural := 0; S_Chunk : Positive; S_Pos : Positive; T_Chunk : Positive; T_Pos : Positive; begin S_Chunk := Target'First; S_Pos := 1; while S_Pos + Source (S_Chunk).all'Length <= Source_Position loop S_Pos := S_Pos + Source (S_Chunk).all'Length; S_Chunk := S_Chunk + 1; end loop; S_Pos := Source_Position + 1 - S_Pos; T_Chunk := Target'First; T_Pos := 1; while T_Pos + Target (T_Chunk).all'Length <= Target_Position loop T_Pos := T_Pos + Target (T_Chunk).all'Length; T_Chunk := T_Chunk + 1; end loop; T_Pos := Target_Position + 1 - T_Pos; while Count < Length loop declare S_String : String renames Source (S_Chunk).all; T_String : String renames Target (T_Chunk).all; Step_C : constant Positive := Length - Count; Step_S : constant Positive := S_String'Last - S_Pos + 1; Step_T : constant Positive := T_String'Last - T_Pos + 1; Step : constant Positive := Positive'Min (Step_C, Positive'Min (Step_S, Step_T)); begin T_String (T_Pos .. T_Pos + Step - 1) := S_String (S_Pos .. S_Pos + Step - 1); Count := Count + Step; exit when Count >= Length; S_Pos := S_Pos + Step; T_Pos := T_Pos + Step; if S_Pos > S_String'Last then S_Chunk := S_Chunk + 1; S_Pos := Source (S_Chunk).all'First; end if; if T_Pos > T_String'Last then T_Chunk := T_Chunk + 1; T_Pos := Target (T_Chunk).all'First; end if; end; end loop; end Move; procedure Move (Target : in out Chunk_Array; Source : in String; Position : in Positive; Chunk_Size : in Positive) is Last_Position : constant Positive := Position + Source'Length - 1; First_Chunk : constant Positive := (Position - 1) / Chunk_Size + 1; First_Offset : constant Positive := (Position - 1) mod Chunk_Size + 1; Last_Chunk : constant Positive := (Last_Position - 1) / Chunk_Size + 1; Last_Offset : constant Positive := (Last_Position - 1) mod Chunk_Size + 1; Current : Positive; begin if First_Chunk = Last_Chunk then Target (First_Chunk).all (First_Offset .. Last_Offset) := Source; else Current := Source'First + Chunk_Size - First_Offset + 1; Target (First_Chunk).all (First_Offset .. Chunk_Size) := Source (Source'First .. Current - 1); for J in First_Chunk + 1 .. Last_Chunk - 1 loop Target (J).all := Source (Current .. Current + Chunk_Size - 1); Current := Current + Chunk_Size; end loop; Target (Last_Chunk).all (1 .. Last_Offset) := Source (Current .. Source'Last); end if; end Move; procedure Move (Target : out String; Source : in Chunk_Array; From : in Positive) is T_Pos : Positive := Target'First; S_Pos : Positive := 1; Chunk : Positive := 1; Step : Positive; begin while S_Pos + Source (Chunk).all'Length <= From loop S_Pos := S_Pos + Source (Chunk).all'Length; Chunk := Chunk + 1; end loop; S_Pos := From - S_Pos + 1; Step := Source (Chunk).all'Last - S_Pos + 1; if Target'Length <= Step then Target := Source (Chunk).all (S_Pos .. S_Pos + Target'Length - 1); return; end if; Target (T_Pos .. T_Pos + Step - 1) := Source (Chunk).all (S_Pos .. Source (Chunk).all'Last); T_Pos := T_Pos + Step; Chunk := Chunk + 1; while T_Pos <= Target'Last loop Step := Positive'Min (Source (Chunk).all'Length, Target'Last - T_Pos + 1); Target (T_Pos .. T_Pos + Step - 1) := Source (Chunk).all (1 .. Step); T_Pos := T_Pos + Step; Chunk := Chunk + 1; end loop; end Move; procedure Move (Data : in out Chunk_Array; Target_Position : in Positive; Source_Position : in Positive; Length : in Positive; Chunk_Size : in Positive) is begin if Target_Position < Source_Position then declare S_Chunk : Positive := (Source_Position - 1) / Chunk_Size + 1; S_Pos : Positive := (Source_Position - 1) mod Chunk_Size + 1; T_Chunk : Positive := (Target_Position - 1) / Chunk_Size + 1; T_Pos : Positive := (Target_Position - 1) mod Chunk_Size + 1; Count : Natural := 0; Step : Positive; begin while Count < Length loop Step := Positive'Min (Positive'Min (Data (S_Chunk).all'Last - S_Pos + 1, Data (T_Chunk).all'Last - T_Pos + 1), Length - Count); Data (T_Chunk).all (T_Pos .. T_Pos + Step - 1) := Data (S_Chunk).all (S_Pos .. S_Pos + Step - 1); Count := Count + Step; S_Pos := S_Pos + Step; if S_Pos > Chunk_Size then S_Chunk := S_Chunk + 1; S_Pos := 1; end if; T_Pos := T_Pos + Step; if T_Pos > Chunk_Size then T_Chunk := T_Chunk + 1; T_Pos := 1; end if; end loop; end; elsif Target_Position > Source_Position then declare S_End : constant Positive := Source_Position + Length - 1; T_End : constant Positive := Target_Position + Length - 1; S_Chunk : Positive := (S_End - 1) / Chunk_Size + 1; S_Pos : Positive := (S_End - 1) mod Chunk_Size + 1; T_Chunk : Positive := (T_End - 1) / Chunk_Size + 1; T_Pos : Positive := (T_End - 1) mod Chunk_Size + 1; Count : Natural := 0; Step : Positive; begin loop Step := Positive'Min (Positive'Min (S_Pos, T_Pos), Length - Count); Data (T_Chunk).all (T_Pos - Step + 1 .. T_Pos) := Data (S_Chunk).all (S_Pos - Step + 1 .. S_Pos); Count := Count + Step; exit when Count = Length; pragma Assert (Count < Length); if S_Pos <= Step then S_Chunk := S_Chunk - 1; S_Pos := Chunk_Size; else S_Pos := S_Pos - Step; end if; if T_Pos <= Step then T_Chunk := T_Chunk - 1; T_Pos := Chunk_Size; else T_Pos := T_Pos - Step; end if; end loop; end; end if; end Move; -------------------------------------------------- -- Public interface specific to Chunked_Strings -- -------------------------------------------------- function Build (Depth : Positive) return Natools.Accumulators.String_Accumulator'Class is pragma Unreferenced (Depth); begin return Null_Chunked_String; end Build; function Duplicate (Source : in Chunked_String) return Chunked_String is Data : Chunk_Array_Access := null; begin if Source.Data /= null then Data := new Chunk_Array (Source.Data'Range); for J in Source.Data'Range loop Data (J) := new String'(Source.Data (J).all); end loop; end if; return Chunked_String'(Ada.Finalization.Controlled with Chunk_Size => Source.Chunk_Size, Allocation_Unit => Source.Allocation_Unit, Size => Source.Size, Data => Data); end Duplicate; procedure Hard_Reset (Str : in out Chunked_String) is begin Free (Str.Data); end Hard_Reset; procedure Soft_Reset (Str : in out Chunked_String) is begin Str.Size := 0; end Soft_Reset; procedure To_String (Source : Chunked_String; Output : out String) is Position : Positive := Output'First; Step : Positive; begin if Source.Size > 0 then for J in Source.Data'Range loop Step := Positive'Min (Source.Data (J).all'Length, Source.Size - Position + 1); Output (Position .. Position + Step - 1) := Source.Data (J).all (1 .. Step); Position := Position + Step; exit when Position > Source.Size; end loop; pragma Assert (Position = Source.Size + 1); end if; end To_String; ------------------------------------------- -- String_Accumulator specific interface -- ------------------------------------------- function Tail (Source : in Chunked_String; Size : in Natural) return String is Actual_Size : constant Natural := Natural'Min (Size, Source.Size); begin return Slice (Source, Source.Size - Actual_Size + 1, Source.Size); end Tail; procedure Unappend (From : in out Chunked_String; Text : in String) is begin if Text'Length <= From.Size and then String'(Tail (From, Text'Length)) = Text then From.Size := From.Size - Text'Length; end if; end Unappend; ------------------------ -- Standard interface -- ------------------------ function Length (Source : in Chunked_String) return Natural is begin return Source.Size; end Length; procedure Deallocate is new Ada.Unchecked_Deallocation (String, String_Access); procedure Free (X : in out String_Access) is begin Deallocate (X); end Free; procedure Free_Extra_Memory (From : in out Chunked_String) is begin Resize_Chunks (From.Data, From.Size, From.Chunk_Size, From.Allocation_Unit, Can_Shrink => True); end Free_Extra_Memory; procedure Preallocate (Str : in out Chunked_String; Size : Natural) is begin Resize_Chunks (Str.Data, Size, Str.Chunk_Size, Str.Allocation_Unit, Can_Shrink => False); end Preallocate; function To_Chunked_String (Source : in String; Chunk_Size : in Positive := Default_Chunk_Size; Allocation_Unit : in Positive := Default_Allocation_Unit) return Chunked_String is Data : Chunk_Array_Access := null; begin if Source'Length > 0 then Resize_Chunks (Data, Source'Length, Chunk_Size, Allocation_Unit); Move (Data.all, Source, 1, Chunk_Size); end if; return Chunked_String'(Ada.Finalization.Controlled with Chunk_Size => Chunk_Size, Allocation_Unit => Allocation_Unit, Size => Source'Length, Data => Data); end To_Chunked_String; function To_Chunked_String (Length : in Natural; Chunk_Size : in Positive := Default_Chunk_Size; Allocation_Unit : in Positive := Default_Allocation_Unit) return Chunked_String is Data : Chunk_Array_Access := null; begin Resize_Chunks (Data, Length, Chunk_Size, Allocation_Unit); return Chunked_String'(Ada.Finalization.Controlled with Chunk_Size => Chunk_Size, Allocation_Unit => Allocation_Unit, Size => Length, Data => Data); end To_Chunked_String; function To_String (Source : in Chunked_String) return String is Value : String (1 .. Source.Size); begin To_String (Source, Value); return Value; end To_String; procedure Set_Chunked_String (Target : out Chunked_String; Source : in String; Chunk_Size : in Positive := Default_Chunk_Size; Allocation_Unit : in Positive := Default_Allocation_Unit) is begin Resize_Chunks (Target.Data, Source'Length, Chunk_Size, Allocation_Unit, Can_Shrink => True); Target.Chunk_Size := Chunk_Size; Target.Allocation_Unit := Allocation_Unit; Target.Size := Source'Length; if Target.Size > 0 then Move (Target.Data.all, Source, 1, Chunk_Size); end if; end Set_Chunked_String; procedure Append (Source : in out Chunked_String; New_Item : in Chunked_String) is New_Size : constant Natural := Source.Size + New_Item.Size; begin Resize_Chunks (Source.Data, New_Size, Source.Chunk_Size, Source.Allocation_Unit, Can_Shrink => False); Move (Source.Data.all, Source.Size + 1, New_Item.Data.all, 1, New_Item.Size); Source.Size := New_Size; end Append; procedure Append (Source : in out Chunked_String; New_Item : in String) is New_Size : constant Natural := Source.Size + New_Item'Length; begin Resize_Chunks (Source.Data, New_Size, Source.Chunk_Size, Source.Allocation_Unit, Can_Shrink => False); Move (Source.Data.all, New_Item, Source.Size + 1, Source.Chunk_Size); Source.Size := New_Size; end Append; procedure Append (Source : in out Chunked_String; New_Item : in Character) is S : constant String (1 .. 1) := (1 => New_Item); begin Append (Source, S); end Append; function "&" (Left, Right : in Chunked_String) return Chunked_String is Size : constant Natural := Left.Size + Right.Size; Data : Chunk_Array_Access := null; begin Resize_Chunks (Data, Size, Default_Chunk_Size, Default_Allocation_Unit); if Left.Size > 0 then Move (Data.all, 1, Left.Data.all, 1, Left.Size); end if; if Right.Size > 0 then Move (Data.all, 1 + Left.Size, Right.Data.all, 1, Right.Size); end if; return Chunked_String'(Ada.Finalization.Controlled with Chunk_Size => Default_Chunk_Size, Allocation_Unit => Default_Allocation_Unit, Size => Size, Data => Data); end "&"; function "&" (Left : in Chunked_String; Right : in String) return Chunked_String is Size : constant Natural := Left.Size + Right'Length; Data : Chunk_Array_Access := null; begin Resize_Chunks (Data, Size, Default_Chunk_Size, Default_Allocation_Unit); if Left.Size > 0 then Move (Data.all, 1, Left.Data.all, 1, Left.Size); end if; if Right'Length > 0 then Move (Data.all, Right, 1 + Left.Size, Default_Chunk_Size); end if; return Chunked_String'(Ada.Finalization.Controlled with Chunk_Size => Default_Chunk_Size, Allocation_Unit => Default_Allocation_Unit, Size => Size, Data => Data); end "&"; function "&" (Left : in String; Right : in Chunked_String) return Chunked_String is Size : constant Natural := Left'Length + Right.Size; Data : Chunk_Array_Access := null; begin Resize_Chunks (Data, Size, Default_Chunk_Size, Default_Allocation_Unit); if Left'Length > 0 then Move (Data.all, Left, 1, Default_Chunk_Size); end if; if Right.Size > 0 then Move (Data.all, 1 + Left'Length, Right.Data.all, 1, Right.Size); end if; return Chunked_String'(Ada.Finalization.Controlled with Chunk_Size => Default_Chunk_Size, Allocation_Unit => Default_Allocation_Unit, Size => Size, Data => Data); end "&"; function "&" (Left : in Chunked_String; Right : in Character) return Chunked_String is Size : constant Natural := Left.Size + 1; Allocation_Unit : constant Positive := Default_Allocation_Unit; Chunk_Size : constant Positive := Default_Chunk_Size; Data : Chunk_Array_Access := null; begin Resize_Chunks (Data, Size, Chunk_Size, Allocation_Unit); if Left.Size > 0 then Move (Data.all, 1, Left.Data.all, 1, Left.Size); end if; declare Position : constant Positive := Left.Size + 1; Chunk : constant Positive := (Position - 1) / Chunk_Size + 1; Offset : constant Positive := (Position - 1) mod Chunk_Size + 1; begin Data (Chunk).all (Offset) := Right; end; return Chunked_String'(Ada.Finalization.Controlled with Chunk_Size => Chunk_Size, Allocation_Unit => Allocation_Unit, Size => Size, Data => Data); end "&"; function "&" (Left : in Character; Right : in Chunked_String) return Chunked_String is Size : constant Natural := 1 + Right.Size; Data : Chunk_Array_Access := null; begin Resize_Chunks (Data, Size, Default_Chunk_Size, Default_Allocation_Unit); Data (1).all (1) := Left; if Right.Size > 0 then Move (Data.all, 2, Right.Data.all, 1, Right.Size); end if; return Chunked_String'(Ada.Finalization.Controlled with Chunk_Size => Default_Chunk_Size, Allocation_Unit => Default_Allocation_Unit, Size => Size, Data => Data); end "&"; function Element (Source : in Chunked_String; Index : in Positive) return Character is Chunk : constant Positive := (Index - 1) / Source.Chunk_Size + 1; Offset : constant Positive := (Index - 1) mod Source.Chunk_Size + 1; begin if Index > Source.Size then raise Ada.Strings.Index_Error; end if; return Source.Data (Chunk).all (Offset); end Element; procedure Replace_Element (Source : in out Chunked_String; Index : in Positive; By : in Character) is Chunk : constant Positive := (Index - 1) / Source.Chunk_Size + 1; Offset : constant Positive := (Index - 1) mod Source.Chunk_Size + 1; begin if Index > Source.Size then raise Ada.Strings.Index_Error; end if; Source.Data (Chunk).all (Offset) := By; end Replace_Element; function Slice (Source : in Chunked_String; Low : in Positive; High : in Natural) return String is Returned : String (Low .. High); begin if Low > Source.Size + 1 or High > Source.Size then raise Ada.Strings.Index_Error; end if; if High >= Low then Move (Returned, Source.Data.all, Low); end if; return Returned; end Slice; function Chunked_Slice (Source : in Chunked_String; Low : in Positive; High : in Natural; Chunk_Size : in Positive := Default_Chunk_Size; Allocation_Unit : in Positive := Default_Allocation_Unit) return Chunked_String is Data : Chunk_Array_Access := null; Size : Natural := 0; begin if Low > Source.Size + 1 or High > Source.Size then raise Ada.Strings.Index_Error; end if; if Low <= High then Size := High - Low + 1; Resize_Chunks (Data, Size, Chunk_Size, Allocation_Unit); Move (Data.all, 1, Source.Data.all, Low, Size); end if; return Chunked_String'(Ada.Finalization.Controlled with Chunk_Size => Chunk_Size, Allocation_Unit => Allocation_Unit, Size => Size, Data => Data); end Chunked_Slice; procedure Chunked_Slice (Source : in Chunked_String; Target : out Chunked_String; Low : in Positive; High : in Natural; Chunk_Size : in Positive := Default_Chunk_Size; Allocation_Unit : in Positive := Default_Allocation_Unit) is begin if Low > Source.Size + 1 or High > Source.Size then raise Ada.Strings.Index_Error; end if; Target.Chunk_Size := Chunk_Size; Target.Allocation_Unit := Allocation_Unit; if Low <= High then Target.Size := High - Low + 1; Resize_Chunks (Target.Data, Target.Size, Chunk_Size, Allocation_Unit, Can_Shrink => True); Move (Target.Data.all, 1, Source.Data.all, Low, Target.Size); else Target.Size := 0; Target.Data := null; end if; end Chunked_Slice; function "=" (Left, Right : in Chunked_String) return Boolean is begin return Compare (Left.Data, Left.Size, Right.Data, Right.Size) = Equal; end "="; function "=" (Left : in Chunked_String; Right : in String) return Boolean is begin return Compare (Left.Data, Left.Size, Right) = Equal; end "="; function "=" (Left : in String; Right : in Chunked_String) return Boolean is begin return Compare (Right.Data, Right.Size, Left) = Equal; end "="; function "<" (Left, Right : in Chunked_String) return Boolean is begin return Compare (Left.Data, Left.Size, Right.Data, Right.Size) = Lesser; end "<"; function "<" (Left : in Chunked_String; Right : in String) return Boolean is begin return Compare (Left.Data, Left.Size, Right) = Lesser; end "<"; function "<" (Left : in String; Right : in Chunked_String) return Boolean is begin return Compare (Right.Data, Right.Size, Left) = Greater; end "<"; function "<=" (Left, Right : in Chunked_String) return Boolean is begin return Compare (Left.Data, Left.Size, Right.Data, Right.Size) /= Greater; end "<="; function "<=" (Left : in Chunked_String; Right : in String) return Boolean is begin return Compare (Left.Data, Left.Size, Right) /= Greater; end "<="; function "<=" (Left : in String; Right : in Chunked_String) return Boolean is begin return Compare (Right.Data, Right.Size, Left) /= Lesser; end "<="; function ">" (Left, Right : in Chunked_String) return Boolean is begin return Compare (Left.Data, Left.Size, Right.Data, Right.Size) = Greater; end ">"; function ">" (Left : in Chunked_String; Right : in String) return Boolean is begin return Compare (Left.Data, Left.Size, Right) = Greater; end ">"; function ">" (Left : in String; Right : in Chunked_String) return Boolean is begin return Compare (Right.Data, Right.Size, Left) = Lesser; end ">"; function ">=" (Left, Right : in Chunked_String) return Boolean is begin return Compare (Left.Data, Left.Size, Right.Data, Right.Size) /= Lesser; end ">="; function ">=" (Left : in Chunked_String; Right : in String) return Boolean is begin return Compare (Left.Data, Left.Size, Right) /= Lesser; end ">="; function ">=" (Left : in String; Right : in Chunked_String) return Boolean is begin return Compare (Right.Data, Right.Size, Left) /= Greater; end ">="; function Index_Gen (Source : Chunked_String; Pattern : String; From : Positive; Going : Ada.Strings.Direction; Map : Map_Type) return Natural is begin if Pattern = "" then raise Ada.Strings.Pattern_Error; end if; if Source.Size = 0 and From = 1 then return 0; end if; if From > Source.Size then raise Ada.Strings.Index_Error; end if; declare Chunk : Positive := (From - 1) / Source.Chunk_Size + 1; Offset : Positive := (From - 1) mod Source.Chunk_Size + 1; Buffer : String (1 .. Source.Chunk_Size + Pattern'Length - 1); Result : Natural; Span : Positive; begin case (Going) is when Ada.Strings.Forward => while (Chunk - 1) * Source.Chunk_Size + Pattern'Length <= Source.Size loop Span := Positive'Min (Source.Chunk_Size + Pattern'Length - 1, Source.Size - (Chunk - 1) * Source.Chunk_Size); Move (Buffer (1 .. Span), Source.Data.all, (Chunk - 1) * Source.Chunk_Size + 1); Result := Index (Buffer (1 .. Span), Pattern, Offset, Going, Map); if Result /= 0 then return (Chunk - 1) * Source.Chunk_Size + Result; end if; Chunk := Chunk + 1; Offset := 1; end loop; return 0; when Ada.Strings.Backward => loop Span := Positive'Min (Source.Chunk_Size + Pattern'Length - 1, Source.Size - (Chunk - 1) * Source.Chunk_Size); Move (Buffer (1 .. Span), Source.Data.all, (Chunk - 1) * Source.Chunk_Size + 1); Result := Index (Buffer (1 .. Span), Pattern, Offset, Going, Map); if Result /= 0 then return (Chunk - 1) * Source.Chunk_Size + Result; end if; exit when Chunk = 1; Chunk := Chunk - 1; Offset := Positive'Min (Source.Chunk_Size + Pattern'Length - 1, Source.Chunk_Size + Offset); end loop; return 0; end case; end; end Index_Gen; function Index_Mapping is new Index_Gen (Maps.Character_Mapping, Ada.Strings.Fixed.Index); function Index (Source : in Chunked_String; Pattern : in String; From : in Positive; Going : in Ada.Strings.Direction := Ada.Strings.Forward; Mapping : in Maps.Character_Mapping := Maps.Identity) return Natural renames Index_Mapping; function Index_Mapping_Function is new Index_Gen (Maps.Character_Mapping_Function, Ada.Strings.Fixed.Index); function Index (Source : in Chunked_String; Pattern : in String; From : in Positive; Going : in Ada.Strings.Direction := Ada.Strings.Forward; Mapping : in Maps.Character_Mapping_Function) return Natural renames Index_Mapping_Function; function Index (Source : in Chunked_String; Pattern : in String; Going : in Ada.Strings.Direction := Ada.Strings.Forward; Mapping : in Maps.Character_Mapping := Maps.Identity) return Natural is begin case (Going) is when Ada.Strings.Forward => return Index (Source, Pattern, 1, Going, Mapping); when Ada.Strings.Backward => return Index (Source, Pattern, Source.Size, Going, Mapping); end case; end Index; function Index (Source : in Chunked_String; Pattern : in String; Going : in Ada.Strings.Direction := Ada.Strings.Forward; Mapping : in Maps.Character_Mapping_Function) return Natural is begin case (Going) is when Ada.Strings.Forward => return Index (Source, Pattern, 1, Going, Mapping); when Ada.Strings.Backward => return Index (Source, Pattern, Source.Size, Going, Mapping); end case; end Index; function Index (Source : in Chunked_String; Set : in Maps.Character_Set; From : in Positive; Test : in Ada.Strings.Membership := Ada.Strings.Inside; Going : in Ada.Strings.Direction := Ada.Strings.Forward) return Natural is Chunk : Positive := (From - 1) / Source.Chunk_Size + 1; Offset : Positive := (From - 1) mod Source.Chunk_Size + 1; Result : Natural; begin if From > Source.Size then raise Ada.Strings.Index_Error; end if; case (Going) is when Ada.Strings.Forward => loop Result := Ada.Strings.Fixed.Index (Source.Data (Chunk).all, Set, Offset, Test, Going); if Result /= 0 then return (Chunk - 1) * Source.Chunk_Size + Result; end if; if Chunk = Source.Data'Last then return 0; end if; Chunk := Chunk + 1; Offset := 1; end loop; when Ada.Strings.Backward => loop Result := Ada.Strings.Fixed.Index (Source.Data (Chunk).all, Set, Offset, Test, Going); if Result /= 0 then return (Chunk - 1) * Source.Chunk_Size + Result; end if; if Chunk = Source.Data'First then return 0; end if; Chunk := Chunk - 1; Offset := Source.Chunk_Size; end loop; end case; end Index; function Index (Source : in Chunked_String; Set : in Maps.Character_Set; Test : in Ada.Strings.Membership := Ada.Strings.Inside; Going : in Ada.Strings.Direction := Ada.Strings.Forward) return Natural is begin case Going is when Ada.Strings.Forward => return Index (Source, Set, 1, Test, Going); when Ada.Strings.Backward => return Index (Source, Set, Source.Size, Test, Going); end case; end Index; function Index_Non_Blank (Source : in Chunked_String; From : in Positive; Going : in Ada.Strings.Direction := Ada.Strings.Forward) return Natural is begin return Index (Source, Maps.To_Set (Ada.Strings.Space), From, Ada.Strings.Outside, Going); end Index_Non_Blank; function Index_Non_Blank (Source : in Chunked_String; Going : in Ada.Strings.Direction := Ada.Strings.Forward) return Natural is begin return Index (Source, Maps.To_Set (Ada.Strings.Space), Ada.Strings.Outside, Going); end Index_Non_Blank; function Count_Gen (Source : in Chunked_String; Pattern : in String; Mapping : in Map_Type) return Natural is Buffer : String (1 .. Source.Chunk_Size + Pattern'Length - 1); Result : Natural := 0; Step : Positive; begin if Pattern = "" then raise Ada.Strings.Pattern_Error; end if; if Source.Size < Pattern'Length then return 0; end if; for J in Source.Data'Range loop Step := Positive'Min (Source.Size - (J - 1) * Source.Chunk_Size, Source.Chunk_Size + Pattern'Length - 1); Move (Buffer (1 .. Step), Source.Data.all, (J - 1) * Source.Chunk_Size + 1); Result := Result + Count (Buffer (1 .. Step), Pattern, Mapping); end loop; return Result; end Count_Gen; function Count_Mapping is new Count_Gen (Maps.Character_Mapping, Ada.Strings.Fixed.Count); function Count (Source : in Chunked_String; Pattern : in String; Mapping : in Maps.Character_Mapping := Maps.Identity) return Natural renames Count_Mapping; function Count_Mapping_Function is new Count_Gen (Maps.Character_Mapping_Function, Ada.Strings.Fixed.Count); function Count (Source : in Chunked_String; Pattern : in String; Mapping : in Maps.Character_Mapping_Function) return Natural renames Count_Mapping_Function; function Count (Source : in Chunked_String; Set : in Maps.Character_Set) return Natural is Result : Natural := 0; Done : Natural := 0; begin if Source.Size > 0 then for C in Source.Data'Range loop declare Chunk : String renames Source.Data (C).all; Step : constant Natural := Natural'Min (Source.Size - Done, Chunk'Length); begin Result := Result + Ada.Strings.Fixed.Count (Chunk (Chunk'First .. Chunk'First + Step - 1), Set); Done := Done + Step; end; end loop; end if; return Result; end Count; procedure Find_Token (Source : in Chunked_String; Set : in Maps.Character_Set; Test : in Ada.Strings.Membership; First : out Positive; Last : out Natural) is function Invert (M : Ada.Strings.Membership) return Ada.Strings.Membership; pragma Inline (Invert); N : Natural; function Invert (M : Ada.Strings.Membership) return Ada.Strings.Membership is use Ada.Strings; begin case M is when Inside => return Outside; when Outside => return Inside; end case; end Invert; begin N := Index (Source, Set, Test); if N = 0 then First := 1; Last := 0; else First := N; N := Index (Source, Set, First, Invert (Test)); if N = 0 then Last := Source.Size; else Last := N - 1; end if; end if; end Find_Token; -- String translation subprograms function Translate (Source : in Chunked_String; Mapping : in Maps.Character_Mapping) return Chunked_String is Data : Chunk_Array_Access := null; begin if Source.Data /= null then Data := new Chunk_Array (Source.Data'Range); for J in Source.Data'Range loop Data (J) := new String (Source.Data (J).all'Range); Data (J).all := Fixed.Translate (Source.Data (J).all, Mapping); end loop; end if; return Chunked_String'(Ada.Finalization.Controlled with Chunk_Size => Source.Chunk_Size, Allocation_Unit => Source.Allocation_Unit, Size => Source.Size, Data => Data); end Translate; procedure Translate (Source : in out Chunked_String; Mapping : in Maps.Character_Mapping) is begin if Source.Data /= null then for J in Source.Data'Range loop Fixed.Translate (Source.Data (J).all, Mapping); end loop; end if; end Translate; function Translate (Source : in Chunked_String; Mapping : in Maps.Character_Mapping_Function) return Chunked_String is Data : Chunk_Array_Access := null; begin if Source.Data /= null then Data := new Chunk_Array (Source.Data'Range); for J in Source.Data'Range loop Data (J) := new String (Source.Data (J).all'Range); Data (J).all := Fixed.Translate (Source.Data (J).all, Mapping); end loop; end if; return Chunked_String'(Ada.Finalization.Controlled with Chunk_Size => Source.Chunk_Size, Allocation_Unit => Source.Allocation_Unit, Size => Source.Size, Data => Data); end Translate; procedure Translate (Source : in out Chunked_String; Mapping : in Maps.Character_Mapping_Function) is begin if Source.Data /= null then for J in Source.Data'Range loop Fixed.Translate (Source.Data (J).all, Mapping); end loop; end if; end Translate; -- String transformation subprograms function Replace_Slice (Source : in Chunked_String; Low : in Positive; High : in Natural; By : in String) return Chunked_String is Size : Natural := 0; Data : Chunk_Array_Access := null; Hi : Natural := High; begin if Low > Source.Size + 1 then raise Ada.Strings.Index_Error; end if; if High < Low then Hi := Low - 1; end if; Size := (Low - 1) + By'Length + (Source.Size - Hi); Resize_Chunks (Data, Size, Source.Chunk_Size, Source.Allocation_Unit, Can_Shrink => False); if Low > 1 then Move (Data.all, 1, Source.Data.all, 1, Low - 1); end if; if By'Length > 0 then Move (Data.all, By, Low, Source.Chunk_Size); end if; if Hi < Source.Size then Move (Data.all, Low + By'Length, Source.Data.all, Hi + 1, Source.Size - Hi); end if; return Chunked_String'(Ada.Finalization.Controlled with Chunk_Size => Source.Chunk_Size, Allocation_Unit => Source.Allocation_Unit, Size => Size, Data => Data); end Replace_Slice; procedure Replace_Slice (Source : in out Chunked_String; Low : in Positive; High : in Natural; By : in String) is Size : Natural := 0; Hi : Natural := High; begin if Low > Source.Size + 1 then raise Ada.Strings.Index_Error; end if; if High < Low then Hi := Low - 1; end if; Size := (Low - 1) + By'Length + (Source.Size - Hi); Resize_Chunks (Source.Data, Size, Source.Chunk_Size, Source.Allocation_Unit, Can_Shrink => False); if Hi < Source.Size and Low + By'Length /= Hi + 1 then Move (Data => Source.Data.all, Target_Position => Low + By'Length, Source_Position => Hi + 1, Length => Source.Size - Hi, Chunk_Size => Source.Chunk_Size); end if; if By'Length > 0 then Move (Source.Data.all, By, Low, Source.Chunk_Size); end if; Source.Size := Size; end Replace_Slice; function Insert (Source : in Chunked_String; Before : in Positive; New_Item : in String) return Chunked_String is begin return Replace_Slice (Source, Before, Before - 1, New_Item); end Insert; procedure Insert (Source : in out Chunked_String; Before : in Positive; New_Item : in String) is begin Replace_Slice (Source, Before, Before - 1, New_Item); end Insert; function Overwrite (Source : in Chunked_String; Position : in Positive; New_Item : in String) return Chunked_String is begin return Replace_Slice (Source, Position, Source.Size, New_Item); end Overwrite; procedure Overwrite (Source : in out Chunked_String; Position : in Positive; New_Item : in String) is begin Replace_Slice (Source, Low => Position, High => Natural'Min (Source.Size, Position + New_Item'Length - 1), By => New_Item); end Overwrite; function Delete (Source : in Chunked_String; From : in Positive; Through : in Natural) return Chunked_String is begin if From <= Through then return Replace_Slice (Source, From, Through, ""); else return Duplicate (Source); end if; end Delete; procedure Delete (Source : in out Chunked_String; From : in Positive; Through : in Natural) is begin if From <= Through then Replace_Slice (Source, From, Through, ""); end if; end Delete; function Trim (Source : in Chunked_String; Side : in Ada.Strings.Trim_End) return Chunked_String is begin case Side is when Ada.Strings.Left => return Trim (Source, Maps.To_Set (Ada.Strings.Space), Maps.Null_Set); when Ada.Strings.Right => return Trim (Source, Maps.Null_Set, Maps.To_Set (Ada.Strings.Space)); when Ada.Strings.Both => return Trim (Source, Maps.To_Set (Ada.Strings.Space), Maps.To_Set (Ada.Strings.Space)); end case; end Trim; procedure Trim (Source : in out Chunked_String; Side : in Ada.Strings.Trim_End) is begin case Side is when Ada.Strings.Left => Trim (Source, Maps.To_Set (Ada.Strings.Space), Maps.Null_Set); when Ada.Strings.Right => Trim (Source, Maps.Null_Set, Maps.To_Set (Ada.Strings.Space)); when Ada.Strings.Both => Trim (Source, Maps.To_Set (Ada.Strings.Space), Maps.To_Set (Ada.Strings.Space)); end case; end Trim; procedure Trim_Bounds (Source : in Chunked_String; Left : in Maps.Character_Set; Right : in Maps.Character_Set; Low : out Positive; High : out Natural) is Chunk : Positive; begin Low := 1; High := Source.Size; Chunk := 1; while Low <= High and then Maps.Is_In (Source.Data (Chunk).all (Low - (Chunk - 1) * Source.Chunk_Size), Left) loop Low := Low + 1; if Low mod Source.Chunk_Size = 1 then Chunk := Chunk + 1; end if; end loop; if High > 0 then Chunk := (High - 1) / Source.Chunk_Size + 1; while Low <= High and then Maps.Is_In (Source.Data (Chunk).all (High - (Chunk - 1) * Source.Chunk_Size), Right) loop High := High - 1; if High mod Source.Chunk_Size = 0 then Chunk := Chunk - 1; end if; end loop; end if; end Trim_Bounds; function Trim (Source : in Chunked_String; Left : in Maps.Character_Set; Right : in Maps.Character_Set) return Chunked_String is Low : Positive; High : Natural; begin Trim_Bounds (Source, Left, Right, Low, High); return Chunked_Slice (Source, Low, High, Source.Chunk_Size, Source.Allocation_Unit); end Trim; procedure Trim (Source : in out Chunked_String; Left : in Maps.Character_Set; Right : in Maps.Character_Set) is Low : Positive; High : Natural; begin Trim_Bounds (Source, Left, Right, Low, High); if Low > 1 then Move (Data => Source.Data.all, Target_Position => 1, Source_Position => Low, Length => High - Low + 1, Chunk_Size => Source.Chunk_Size); end if; Source.Size := High - Low + 1; end Trim; function Head (Source : in Chunked_String; Count : in Natural; Pad : in Character := Ada.Strings.Space; Chunk_Size : in Natural := 0; -- use value from Source Allocation_Unit : in Natural := 0) -- use value from Source return Chunked_String is Real_Chunk_Size : Positive := Default_Chunk_Size; Real_Unit : Positive := Default_Allocation_Unit; Data : Chunk_Array_Access := null; begin if Chunk_Size > 0 then Real_Chunk_Size := Chunk_Size; end if; if Allocation_Unit > 0 then Real_Unit := Allocation_Unit; end if; if Count > 0 then Resize_Chunks (Data, Count, Real_Chunk_Size, Real_Unit); if Count > Source.Size then Move (Data.all, 1, Source.Data.all, 1, Source.Size); Fill (Data.all, Source.Size + 1, Count - Source.Size, Pad, Real_Chunk_Size); else Move (Data.all, 1, Source.Data.all, 1, Count); end if; end if; return Chunked_String'(Ada.Finalization.Controlled with Chunk_Size => Real_Chunk_Size, Allocation_Unit => Real_Unit, Size => Count, Data => Data); end Head; procedure Head (Source : in out Chunked_String; Count : in Natural; Pad : in Character := Ada.Strings.Space) is begin if Count > Source.Size then Resize_Chunks (Source.Data, Count, Source.Chunk_Size, Source.Allocation_Unit, Can_Shrink => False); Fill (Source.Data.all, Source.Size + 1, Count - Source.Size, Pad, Source.Chunk_Size); end if; Source.Size := Count; end Head; function Tail (Source : in Chunked_String; Count : in Natural; Pad : in Character := Ada.Strings.Space; Chunk_Size : in Natural := 0; -- use value from Source Allocation_Unit : in Natural := 0) -- use value from Source return Chunked_String is Real_Chunk_Size : Positive := Default_Chunk_Size; Real_Unit : Positive := Default_Allocation_Unit; Data : Chunk_Array_Access := null; begin if Chunk_Size > 0 then Real_Chunk_Size := Chunk_Size; end if; if Allocation_Unit > 0 then Real_Unit := Allocation_Unit; end if; if Count > 0 then Resize_Chunks (Data, Count, Real_Chunk_Size, Real_Unit); if Count > Source.Size then Fill (Data.all, 1, Count - Source.Size, Pad, Real_Chunk_Size); Move (Data.all, Count - Source.Size + 1, Source.Data.all, 1, Source.Size); else Move (Data.all, 1, Source.Data.all, Source.Size - Count + 1, Count); end if; end if; return Chunked_String'(Ada.Finalization.Controlled with Chunk_Size => Real_Chunk_Size, Allocation_Unit => Real_Unit, Size => Count, Data => Data); end Tail; procedure Tail (Source : in out Chunked_String; Count : in Natural; Pad : in Character := Ada.Strings.Space) is begin Resize_Chunks (Source.Data, Count, Source.Chunk_Size, Source.Allocation_Unit, Can_Shrink => False); if Count > Source.Size then if Source.Size > 0 then Move (Data => Source.Data.all, Target_Position => Count - Source.Size + 1, Source_Position => 1, Length => Source.Size, Chunk_Size => Source.Chunk_Size); end if; Fill (Source.Data.all, 1, Count - Source.Size, Pad, Source.Chunk_Size); elsif Count > 0 then Move (Data => Source.Data.all, Target_Position => 1, Source_Position => Source.Size - Count + 1, Length => Count, Chunk_Size => Source.Chunk_Size); end if; Source.Size := Count; end Tail; function "*" (Left : in Natural; Right : in Character) return Chunked_String is Chunk_Size : constant Positive := Default_Chunk_Size; Allocation_Unit : constant Positive := Default_Allocation_Unit; Size : constant Natural := Left; Chunk_Nb : constant Natural := Chunks_For (Size, Chunk_Size, Allocation_Unit); Last_Chunk_Size : constant Natural := Units_For (Size, Chunk_Size, Allocation_Unit) * Allocation_Unit; Data : Chunk_Array_Access := null; begin if Size > 0 then Data := new Chunk_Array (1 .. Chunk_Nb); for J in 1 .. Chunk_Nb - 1 loop Data (J) := new String'(Ada.Strings.Fixed."*" (Chunk_Size, Right)); end loop; Data (Chunk_Nb) := new String'(Ada.Strings.Fixed."*" (Last_Chunk_Size, Right)); end if; return Chunked_String'(Ada.Finalization.Controlled with Chunk_Size => Chunk_Size, Allocation_Unit => Allocation_Unit, Size => Size, Data => Data); end "*"; function "*" (Left : in Natural; Right : in String) return Chunked_String is Chunk_Size : constant Positive := Default_Chunk_Size; Allocation_Unit : constant Positive := Default_Allocation_Unit; Size : constant Natural := Left * Right'Length; Chunk_Nb : constant Natural := Chunks_For (Size, Chunk_Size, Allocation_Unit); Last_Chunk_Size : constant Natural := Units_For (Size, Chunk_Size, Allocation_Unit) * Allocation_Unit; Data : Chunk_Array_Access := null; begin if Size > 0 then if Chunk_Size mod Right'Length = 0 then Data := new Chunk_Array (1 .. Chunk_Nb); for J in 1 .. Chunk_Nb - 1 loop Data (J) := new String'(Ada.Strings.Fixed."*" (Chunk_Size / Right'Length, Right)); end loop; Data (Chunk_Nb) := new String'(Ada.Strings.Fixed."*" (Last_Chunk_Size / Right'Length, Right)); else Resize_Chunks (Data, Size, Chunk_Size, Allocation_Unit); for J in 1 .. Left loop Move (Data.all, Right, (J - 1) * Right'Length + 1, Chunk_Size); end loop; end if; end if; return Chunked_String'(Ada.Finalization.Controlled with Chunk_Size => Chunk_Size, Allocation_Unit => Allocation_Unit, Size => Size, Data => Data); end "*"; function "*" (Left : in Natural; Right : in Chunked_String) return Chunked_String is Chunk_Size : constant Positive := Default_Chunk_Size; Allocation_Unit : constant Positive := Default_Allocation_Unit; Size : constant Natural := Left * Right.Size; Data : Chunk_Array_Access := null; begin if Size > 0 then Resize_Chunks (Data, Size, Chunk_Size, Allocation_Unit); for J in 1 .. Left loop Move (Data.all, (J - 1) * Right.Size + 1, Right.Data.all, 1, Right.Size); end loop; end if; return Chunked_String'(Ada.Finalization.Controlled with Chunk_Size => Chunk_Size, Allocation_Unit => Allocation_Unit, Size => Size, Data => Data); end "*"; -- Controlled object methods overriding procedure Initialize (Object : in out Chunked_String) is begin Object.Size := 0; Object.Data := null; end Initialize; overriding procedure Adjust (Object : in out Chunked_String) is New_Data : Chunk_Array_Access; begin if Object.Data /= null then New_Data := new Chunk_Array (Object.Data'Range); for J in Object.Data'Range loop New_Data (J) := new String'(Object.Data (J).all); end loop; Object.Data := New_Data; end if; end Adjust; overriding procedure Finalize (Object : in out Chunked_String) is begin Free (Object.Data); end Finalize; end Natools.Chunked_Strings; |
Added natools-chunked_strings.ads version [e38612cf83].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | ------------------------------------------------------------------------------ -- Copyright (c) 2011, Natacha Porté -- -- -- -- Permission to use, copy, modify, and distribute this software for any -- -- purpose with or without fee is hereby granted, provided that the above -- -- copyright notice and this permission notice appear in all copies. -- -- -- -- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -- -- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -- -- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -- -- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -- -- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -- -- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -- -- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -- ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ -- Natools.Chunked_Strings is a string container designed for large amount -- -- of data and efficient accumulation (append). Most subprograms are the -- -- direct copy of Unbounded_String equivalent from LRM, with the same -- -- semantics. -- -- -- -- The implementation uses fixed-size "chunks" of memory, so that large -- -- strings do not have to be stored in a contiguous space. This also allows -- -- more efficient appends, since only the last chunk might need dynamic -- -- resize. -- -- Moreover the last chunk is constrained to have a size multiple of -- -- Allocation_Unit, so if Allocation_Unit = Chunk_Size, no string resize -- -- ever happen. -- -- -- -- The list of chunks is stored as a usual dynamic array, so append -- -- operations are still linear (when a new chunk has to be created), they -- -- are just O(Size / Chunk_Size) instead of O(Size). For suitable values of -- -- Chunk_Size, that should be a significant improuvement. -- -- -- -- Chunk_Size and Allocation_Unit are defined per Chunked_String, which -- -- allows to use suitable parameters depending on the expected string size. -- -- Generic parameters control the default values, e.g. in operations like -- -- "&" which don't allow to specify them. -- ------------------------------------------------------------------------------ with Ada.Strings.Maps; with Natools.Accumulators; private with Ada.Finalization; generic Default_Allocation_Unit : Positive := 64; Default_Chunk_Size : Positive := 4096; package Natools.Chunked_Strings is pragma Preelaborate (Chunked_Strings); package Maps renames Ada.Strings.Maps; type Chunked_String is new Natools.Accumulators.String_Accumulator with private; function Build (Depth : Positive) return Natools.Accumulators.String_Accumulator'Class; -- Returns a new empty chunked string -- Ignores its Depth argument -- Can be used with Natools.Accumulators.String_Accumulator_Linked_Lists function Duplicate (Source : in Chunked_String) return Chunked_String; -- returns a copy of the given chunked string procedure Free_Extra_Memory (From : in out Chunked_String); -- Release as much memory as possible without altering the contents procedure Hard_Reset (Str : in out Chunked_String); -- Empty the string and free all possible memory procedure Preallocate (Str : in out Chunked_String; Size : Natural); -- Allocate enough memory to reach Size without subsequent reallocation procedure Soft_Reset (Str : in out Chunked_String); -- Empty the string for reuse procedure To_String (Source : Chunked_String; Output : out String); -- Write the contents of the chunked string into the output string, -- which must be large enough. ------------------------------------------- -- String_Accumulator specific interface -- ------------------------------------------- -- Append, Length and To_String are part of the standard interface -- Hard_Reset and Soft_Reset are already in the specific interface function Tail (Source : in Chunked_String; Size : in Natural) return String; procedure Unappend (From : in out Chunked_String; Text : in String); ------------------------ -- Standard interface -- ------------------------ -- All the following declarations are copied from Unbounded_String -- interface and have exactly the same semantics. -- Subprogram that create new Chunked_String objects also have -- Chunk_Size and Allocation_Unit optional parameters. Null_Chunked_String : constant Chunked_String; function Length (Source : in Chunked_String) return Natural; type String_Access is access all String; procedure Free (X : in out String_Access); -- Conversion, Concatenation, and Selection functions function To_Chunked_String (Source : in String; Chunk_Size : in Positive := Default_Chunk_Size; Allocation_Unit : in Positive := Default_Allocation_Unit) return Chunked_String; function To_Chunked_String (Length : in Natural; Chunk_Size : in Positive := Default_Chunk_Size; Allocation_Unit : in Positive := Default_Allocation_Unit) return Chunked_String; function To_String (Source : in Chunked_String) return String; procedure Set_Chunked_String (Target : out Chunked_String; Source : in String; Chunk_Size : in Positive := Default_Chunk_Size; Allocation_Unit : in Positive := Default_Allocation_Unit); procedure Append (Source : in out Chunked_String; New_Item : in Chunked_String); procedure Append (Source : in out Chunked_String; New_Item : in String); procedure Append (Source : in out Chunked_String; New_Item : in Character); function "&" (Left, Right : in Chunked_String) return Chunked_String; function "&" (Left : in Chunked_String; Right : in String) return Chunked_String; function "&" (Left : in String; Right : in Chunked_String) return Chunked_String; function "&" (Left : in Chunked_String; Right : in Character) return Chunked_String; function "&" (Left : in Character; Right : in Chunked_String) return Chunked_String; function Element (Source : in Chunked_String; Index : in Positive) return Character; pragma Inline (Element); procedure Replace_Element (Source : in out Chunked_String; Index : in Positive; By : in Character); function Slice (Source : in Chunked_String; Low : in Positive; High : in Natural) return String; function Chunked_Slice (Source : in Chunked_String; Low : in Positive; High : in Natural; Chunk_Size : in Positive := Default_Chunk_Size; Allocation_Unit : in Positive := Default_Allocation_Unit) return Chunked_String; procedure Chunked_Slice (Source : in Chunked_String; Target : out Chunked_String; Low : in Positive; High : in Natural; Chunk_Size : in Positive := Default_Chunk_Size; Allocation_Unit : in Positive := Default_Allocation_Unit); function "=" (Left, Right : in Chunked_String) return Boolean; function "=" (Left : in Chunked_String; Right : in String) return Boolean; function "=" (Left : in String; Right : in Chunked_String) return Boolean; function "<" (Left, Right : in Chunked_String) return Boolean; function "<" (Left : in Chunked_String; Right : in String) return Boolean; function "<" (Left : in String; Right : in Chunked_String) return Boolean; function "<=" (Left, Right : in Chunked_String) return Boolean; function "<=" (Left : in Chunked_String; Right : in String) return Boolean; function "<=" (Left : in String; Right : in Chunked_String) return Boolean; function ">" (Left, Right : in Chunked_String) return Boolean; function ">" (Left : in Chunked_String; Right : in String) return Boolean; function ">" (Left : in String; Right : in Chunked_String) return Boolean; function ">=" (Left, Right : in Chunked_String) return Boolean; function ">=" (Left : in Chunked_String; Right : in String) return Boolean; function ">=" (Left : in String; Right : in Chunked_String) return Boolean; function Index (Source : in Chunked_String; Pattern : in String; From : in Positive; Going : in Ada.Strings.Direction := Ada.Strings.Forward; Mapping : in Maps.Character_Mapping := Maps.Identity) return Natural; function Index (Source : in Chunked_String; Pattern : in String; From : in Positive; Going : in Ada.Strings.Direction := Ada.Strings.Forward; Mapping : in Maps.Character_Mapping_Function) return Natural; function Index (Source : in Chunked_String; Pattern : in String; Going : in Ada.Strings.Direction := Ada.Strings.Forward; Mapping : in Maps.Character_Mapping := Maps.Identity) return Natural; function Index (Source : in Chunked_String; Pattern : in String; Going : in Ada.Strings.Direction := Ada.Strings.Forward; Mapping : in Maps.Character_Mapping_Function) return Natural; function Index (Source : in Chunked_String; Set : in Maps.Character_Set; From : in Positive; Test : in Ada.Strings.Membership := Ada.Strings.Inside; Going : in Ada.Strings.Direction := Ada.Strings.Forward) return Natural; function Index (Source : in Chunked_String; Set : in Maps.Character_Set; Test : in Ada.Strings.Membership := Ada.Strings.Inside; Going : in Ada.Strings.Direction := Ada.Strings.Forward) return Natural; function Index_Non_Blank (Source : in Chunked_String; From : in Positive; Going : in Ada.Strings.Direction := Ada.Strings.Forward) return Natural; function Index_Non_Blank (Source : in Chunked_String; Going : in Ada.Strings.Direction := Ada.Strings.Forward) return Natural; function Count (Source : in Chunked_String; Pattern : in String; Mapping : in Maps.Character_Mapping := Maps.Identity) return Natural; function Count (Source : in Chunked_String; Pattern : in String; Mapping : in Maps.Character_Mapping_Function) return Natural; function Count (Source : in Chunked_String; Set : in Maps.Character_Set) return Natural; procedure Find_Token (Source : in Chunked_String; Set : in Maps.Character_Set; Test : in Ada.Strings.Membership; First : out Positive; Last : out Natural); -- String translation subprograms function Translate (Source : in Chunked_String; Mapping : in Maps.Character_Mapping) return Chunked_String; procedure Translate (Source : in out Chunked_String; Mapping : in Maps.Character_Mapping); function Translate (Source : in Chunked_String; Mapping : in Maps.Character_Mapping_Function) return Chunked_String; procedure Translate (Source : in out Chunked_String; Mapping : in Maps.Character_Mapping_Function); -- String transformation subprograms function Replace_Slice (Source : in Chunked_String; Low : in Positive; High : in Natural; By : in String) return Chunked_String; procedure Replace_Slice (Source : in out Chunked_String; Low : in Positive; High : in Natural; By : in String); function Insert (Source : in Chunked_String; Before : in Positive; New_Item : in String) return Chunked_String; procedure Insert (Source : in out Chunked_String; Before : in Positive; New_Item : in String); function Overwrite (Source : in Chunked_String; Position : in Positive; New_Item : in String) return Chunked_String; procedure Overwrite (Source : in out Chunked_String; Position : in Positive; New_Item : in String); function Delete (Source : in Chunked_String; From : in Positive; Through : in Natural) return Chunked_String; procedure Delete (Source : in out Chunked_String; From : in Positive; Through : in Natural); function Trim (Source : in Chunked_String; Side : in Ada.Strings.Trim_End) return Chunked_String; procedure Trim (Source : in out Chunked_String; Side : in Ada.Strings.Trim_End); function Trim (Source : in Chunked_String; Left : in Maps.Character_Set; Right : in Maps.Character_Set) return Chunked_String; procedure Trim (Source : in out Chunked_String; Left : in Maps.Character_Set; Right : in Maps.Character_Set); function Head (Source : in Chunked_String; Count : in Natural; Pad : in Character := Ada.Strings.Space; Chunk_Size : in Natural := 0; -- use value from Source Allocation_Unit : in Natural := 0) -- use value from Source return Chunked_String; procedure Head (Source : in out Chunked_String; Count : in Natural; Pad : in Character := Ada.Strings.Space); function Tail (Source : in Chunked_String; Count : in Natural; Pad : in Character := Ada.Strings.Space; Chunk_Size : in Natural := 0; -- use value from Source Allocation_Unit : in Natural := 0) -- use value from Source return Chunked_String; procedure Tail (Source : in out Chunked_String; Count : in Natural; Pad : in Character := Ada.Strings.Space); function "*" (Left : in Natural; Right : in Character) return Chunked_String; function "*" (Left : in Natural; Right : in String) return Chunked_String; function "*" (Left : in Natural; Right : in Chunked_String) return Chunked_String; private type Chunk_Array is array (Positive range <>) of String_Access; type Chunk_Array_Access is access all Chunk_Array; type Chunked_String is new Ada.Finalization.Controlled and Natools.Accumulators.String_Accumulator with record Chunk_Size : Positive := Default_Chunk_Size; Allocation_Unit : Positive := Default_Allocation_Unit; Size : Natural := 0; Data : Chunk_Array_Access := null; end record; overriding procedure Initialize (Object : in out Chunked_String); overriding procedure Adjust (Object : in out Chunked_String); overriding procedure Finalize (Object : in out Chunked_String); -- Controlled type methods function Is_Valid (Source : in Chunked_String) return Boolean; -- Internal consistency checks Null_Chunked_String : constant Chunked_String := (Ada.Finalization.Controlled with Chunk_Size => Default_Chunk_Size, Allocation_Unit => Default_Allocation_Unit, Size => 0, Data => null); end Natools.Chunked_Strings; |