package core_kernel
Industrial strength alternative to OCaml's standard library
Install
Dune Dependency
Authors
Maintainers
Sources
core_kernel-v0.16.0.tar.gz
sha256=e37370bad978cfb71fdaf2b1a25ab1506b98ef0b91e0dbd189ffd9d853245ce2
doc/src/core_kernel.iobuf/iobuf.ml.html
Source file iobuf.ml
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 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099
open! Core module IR = Int_repr open! Iobuf_intf [%%import "include.mlh"] [%%if UNSAFE_IS_SAFE] let unsafe_is_safe = true [%%else] let unsafe_is_safe = false [%%endif] module type Accessors_common = Accessors_common module type Accessors_read = Accessors_read module type Accessors_write = Accessors_write module type Consuming_blit = Consuming_blit type nonrec ('src, 'dst) consuming_blito = ('src, 'dst) consuming_blito let arch_sixtyfour = Sys.word_size_in_bits = 64 module T = struct (* WHEN YOU CHANGE THIS, CHANGE iobuf_fields IN iobuf.h AS WELL!!! *) type t = { mutable buf : (Bigstring.t [@sexp.opaque] (* The data in [buf] is at indices [lo], [lo+1], ... [hi-1]. *)) ; mutable lo_min : int ; mutable lo : int ; mutable hi : int ; mutable hi_max : int } [@@deriving fields, globalize, sexp_of] end open T type t_repr = T.t [@@deriving globalize] type (-'read_write, +'seek) t = T.t [@@deriving sexp_of] type (_, _) t_with_shallow_sexp = T.t [@@deriving sexp_of] type seek = Iobuf_intf.seek [@@deriving sexp_of] type no_seek = Iobuf_intf.no_seek [@@deriving sexp_of] module type Bound = Iobuf_intf.Bound with type ('d, 'w) iobuf := ('d, 'w) t let globalize _ _ t = [%globalize: t_repr] t let read_only t = t let read_only_local t = t let no_seek t = t let no_seek_local t = t let[@cold] fail t message a sexp_of_a = (* Immediately convert the iobuf to sexp. Otherwise, the iobuf could be modified before conversion and printing. Since we plan to use iobufs for pooled network buffers in practice, this could be very confusing when debugging production systems. *) Error.raise (Error.create message (a, [%sexp_of: (_, _) t] ([%globalize: t_repr] t)) (Tuple.T2.sexp_of_t sexp_of_a Fn.id)) ;; module Lo_bound = struct let[@cold] stale t iobuf = fail iobuf "Iobuf.Lo_bound.restore got stale snapshot" t [%sexp_of: int] ;; type t = int [@@deriving compare, sexp_of] (* lo *) let window t = t.lo let restore t iobuf = if t < iobuf.lo_min || t > iobuf.hi then stale t iobuf; iobuf.lo <- t ;; let limit t = t.lo_min end module Hi_bound = struct let[@cold] stale t iobuf = fail iobuf "Iobuf.Hi_bound.restore got stale snapshot" t [%sexp_of: int] ;; type t = int [@@deriving compare, sexp_of] (* hi *) let window t = t.hi let restore t iobuf = if t > iobuf.hi_max || t < iobuf.lo then stale t iobuf; iobuf.hi <- t ;; let limit t = t.hi_max end let length t = t.hi - t.lo let length_lo t = t.lo - t.lo_min let length_hi t = t.hi_max - t.hi let is_empty t = length t = 0 let rewind t = t.lo <- t.lo_min let reset t = t.lo <- t.lo_min; t.hi <- t.hi_max ;; let flip_lo t = t.hi <- t.lo; t.lo <- t.lo_min ;; let[@cold] bounded_flip_lo_stale t lo_min = fail t "Iobuf.bounded_flip_lo got stale snapshot" lo_min [%sexp_of: Lo_bound.t] ;; let bounded_flip_lo t lo_min = if lo_min < t.lo_min || lo_min > t.lo then bounded_flip_lo_stale t lo_min else ( t.hi <- t.lo; t.lo <- lo_min) ;; let flip_hi t = t.lo <- t.hi; t.hi <- t.hi_max ;; let[@cold] bounded_flip_hi_stale t hi_max = fail t "Iobuf.bounded_flip_hi got stale snapshot" hi_max [%sexp_of: Hi_bound.t] ;; let bounded_flip_hi t hi_max = if hi_max > t.hi_max || hi_max < t.hi then bounded_flip_hi_stale t hi_max else ( t.lo <- t.hi; t.hi <- hi_max) ;; let capacity t = t.hi_max - t.lo_min let invariant _ _ t = try Fields.Direct.iter t ~buf:(fun _ _ _ -> ()) ~lo_min:(fun _ _ lo_min -> assert (lo_min >= 0); assert (lo_min = t.hi_max - capacity t)) ~hi_max:(fun _ _ hi_max -> assert (hi_max >= t.lo); assert (hi_max = t.lo_min + capacity t)) ~lo:(fun _ _ lo -> assert (lo >= t.lo_min); assert (lo <= t.hi)) ~hi:(fun _ _ hi -> assert (hi >= t.lo); assert (hi <= t.hi_max)) with | e -> fail t "Iobuf.invariant failed" e [%sexp_of: exn] ;; (* We want [check_range] inlined, so we don't want a string constant in there. *) let[@cold] bad_range ~pos ~len t = fail t "Iobuf got invalid range" (`pos pos, `len len) [%sexp_of: [ `pos of int ] * [ `len of int ]] ;; let check_range t ~pos ~len = if pos < 0 || len < 0 || len > length t - pos then bad_range ~pos ~len t [@@inline always] ;; let of_bigstring_local ?pos ?len buf = (let str_len = Bigstring.length buf in let pos = match pos with | None -> 0 | Some pos -> if pos < 0 || pos > str_len then raise_s [%sexp "Iobuf.of_bigstring got invalid pos", (pos : int), ~~(str_len : int)]; pos in let len = match len with | None -> str_len - pos | Some len -> let max_len = str_len - pos in if len < 0 || len > max_len then raise_s [%sexp "Iobuf.of_bigstring got invalid pos", (len : int), ~~(max_len : int)]; len in let lo = pos in let hi = pos + len in { buf; lo_min = lo; lo; hi; hi_max = hi }) ;; let of_bigstring ?pos ?len buf = [%globalize: t_repr] (of_bigstring_local ?pos ?len buf) [@nontail] ;; let ?(pos = 0) ?len t = (let len = match len with | None -> length t - pos | Some len -> len in check_range t ~pos ~len; let lo = t.lo + pos in let hi = lo + len in { buf = t.buf; lo_min = lo; lo; hi; hi_max = hi }) ;; let ?pos ?len t = [%globalize: t_repr] (sub_shared_local ?pos ?len t) [@nontail] ;; let copy t = of_bigstring (Bigstring.sub t.buf ~pos:t.lo ~len:(length t)) let clone { buf; lo_min; lo; hi; hi_max } = { buf = Bigstring.copy buf; lo_min; lo; hi; hi_max } ;; let set_bounds_and_buffer_sub ~pos ~len ~src ~dst = check_range src ~pos ~len; let lo = src.lo + pos in let hi = lo + len in dst.lo_min <- lo; dst.lo <- lo; dst.hi <- hi; dst.hi_max <- hi; if not (phys_equal dst.buf src.buf) then dst.buf <- src.buf [@@inline] ;; let set_bounds_and_buffer ~src ~dst = dst.lo_min <- src.lo_min; dst.lo <- src.lo; dst.hi <- src.hi; dst.hi_max <- src.hi_max; if not (phys_equal dst.buf src.buf) then dst.buf <- src.buf ;; let narrow_lo t = t.lo_min <- t.lo let narrow_hi t = t.hi_max <- t.hi let narrow t = narrow_lo t; narrow_hi t ;; let unsafe_resize t ~len = t.hi <- t.lo + len let resize t ~len = if len < 0 then bad_range t ~len ~pos:0; let hi = t.lo + len in if hi > t.hi_max then bad_range t ~len ~pos:0; t.hi <- hi [@@inline always] ;; let unsafe_resize = if unsafe_is_safe then resize else unsafe_resize let protect_window_bounds_and_buffer t ~f = let lo = t.lo in let hi = t.hi in let lo_min = t.lo_min in let hi_max = t.hi_max in let buf = t.buf in (* also mutable *) try t.lo_min <- lo; t.hi_max <- hi; let result = f t in t.lo <- lo; t.hi <- hi; t.lo_min <- lo_min; t.hi_max <- hi_max; if not (phys_equal buf t.buf) then t.buf <- buf; result with | exn -> t.lo <- lo; t.hi <- hi; t.lo_min <- lo_min; t.hi_max <- hi_max; if not (phys_equal buf t.buf) then t.buf <- buf; raise exn ;; let protect_window_bounds_and_buffer_local t ~f = (let lo = t.lo in let hi = t.hi in let lo_min = t.lo_min in let hi_max = t.hi_max in let buf = t.buf in (* also mutable *) try t.lo_min <- lo; t.hi_max <- hi; let result = f t in t.lo <- lo; t.hi <- hi; t.lo_min <- lo_min; t.hi_max <- hi_max; if not (phys_equal buf t.buf) then t.buf <- buf; result with | exn -> t.lo <- lo; t.hi <- hi; t.lo_min <- lo_min; t.hi_max <- hi_max; if not (phys_equal buf t.buf) then t.buf <- buf; raise exn) ;; let protect_window_bounds_and_buffer_1 t x ~f = let lo = t.lo in let hi = t.hi in let lo_min = t.lo_min in let hi_max = t.hi_max in let buf = t.buf in (* also mutable *) try t.lo_min <- lo; t.hi_max <- hi; let result = f t x in t.lo <- lo; t.hi <- hi; t.lo_min <- lo_min; t.hi_max <- hi_max; if not (phys_equal buf t.buf) then t.buf <- buf; result with | exn -> t.lo <- lo; t.hi <- hi; t.lo_min <- lo_min; t.hi_max <- hi_max; if not (phys_equal buf t.buf) then t.buf <- buf; raise exn ;; let protect_window_bounds_and_buffer_2 t x y ~f = let lo = t.lo in let hi = t.hi in let lo_min = t.lo_min in let hi_max = t.hi_max in let buf = t.buf in (* also mutable *) try t.lo_min <- lo; t.hi_max <- hi; let result = f t x y in t.lo <- lo; t.hi <- hi; t.lo_min <- lo_min; t.hi_max <- hi_max; if not (phys_equal buf t.buf) then t.buf <- buf; result with | exn -> t.lo <- lo; t.hi <- hi; t.lo_min <- lo_min; t.hi_max <- hi_max; if not (phys_equal buf t.buf) then t.buf <- buf; raise exn ;; let protect_window_bounds_and_buffer_3 t x y z ~f = let lo = t.lo in let hi = t.hi in let lo_min = t.lo_min in let hi_max = t.hi_max in let buf = t.buf in (* also mutable *) try t.lo_min <- lo; t.hi_max <- hi; let result = f t x y z in t.lo <- lo; t.hi <- hi; t.lo_min <- lo_min; t.hi_max <- hi_max; if not (phys_equal buf t.buf) then t.buf <- buf; result with | exn -> t.lo <- lo; t.hi <- hi; t.lo_min <- lo_min; t.hi_max <- hi_max; if not (phys_equal buf t.buf) then t.buf <- buf; raise exn ;; let create ~len = if len < 0 then raise_s [%sexp "Iobuf.create got negative len", (len : int)]; of_bigstring (Bigstring.create len) ;; let empty = create ~len:0 let of_string s = of_bigstring (Bigstring.of_string s) let of_bytes s = of_bigstring (Bigstring.of_bytes s) let to_stringlike ~(convert : ?pos:int -> ?len:int -> Bigstring.t -> 'a) = stage (fun ?len t : 'a -> let len = match len with | Some len -> check_range t ~pos:0 ~len; len | None -> length t in convert t.buf ~pos:t.lo ~len) ;; let to_string = to_stringlike ~convert:Bigstring.to_string |> unstage let to_bytes = to_stringlike ~convert:Bigstring.to_bytes |> unstage (* We used to do it like {v let unsafe_with_range t ~pos f = f t.buf ~pos:(t.lo + pos); ;; let with_range t ~pos ~len f = check_range t ~pos ~len; unsafe_with_range t ~pos f; ;; let inc_lo t amount = t.lo <- t.lo + amount (** [unsafe_with_advance] and [unsafe_with_range] forego range checks for code that does macro range checks, like we want to do in [Parachute_fix.Std.Protocol]. Esp. [Consume.Unsafe.int32_le] for unrolled character scanning. *) let unsafe_with_advance t ~len f = let result = unsafe_with_range t ~pos:0 f in inc_lo t len; result; ;; let with_advance t ~len f = check_range t ~pos:0 ~len; unsafe_with_advance t ~len f; ;; (* pulled out and type-constrained for inlining *) let ignore_range (_ : Bigstring.t) ~pos:(_ : int) = () let advance t len = with_advance t ~len ignore_range v} but higher order functions don't get inlined, even in simple uses like advance. Therefor, we stick to first order. *) let[@inline always] unsafe_buf_pos t ~pos ~len:_ = t.lo + pos let[@inline] buf_pos_exn t ~pos ~len = check_range t ~pos ~len; unsafe_buf_pos t ~pos ~len ;; let unsafe_buf_pos = if unsafe_is_safe then buf_pos_exn else unsafe_buf_pos let unsafe_advance t n = t.lo <- t.lo + n let advance t len = check_range t ~len ~pos:0; unsafe_advance t len [@@inline always] ;; let unsafe_advance = if unsafe_is_safe then advance else unsafe_advance module Char_elt = struct include Char let of_bool = function | true -> '0' | false -> '1' ;; end let[@inline] get_char t pos = Bigstring.unsafe_get t.buf (buf_pos_exn t ~len:1 ~pos) let[@inline] set_char t pos c = Bigstring.unsafe_set t.buf (buf_pos_exn t ~len:1 ~pos) c module T_src = struct type t = T.t [@@deriving sexp_of] let create = create let length = length let get t pos = get_char t pos let set t pos c = set_char t pos c end module Bytes_dst = struct include Bytes let unsafe_blit ~src ~src_pos ~dst ~dst_pos ~len = let blit = if unsafe_is_safe then Bigstring.To_bytes.blit else Bigstring.To_bytes.unsafe_blit in blit ~src:src.buf ~src_pos:(unsafe_buf_pos src ~pos:src_pos ~len) ~dst ~dst_pos ~len ;; let create ~len = create len end module String_dst = struct let sub src ~pos ~len = Bigstring.To_string.sub src.buf ~pos:(buf_pos_exn src ~pos ~len) ~len ;; let subo ?(pos = 0) ?len src = let len = match len with | None -> length src - pos | Some len -> len in Bigstring.To_string.subo src.buf ~pos:(buf_pos_exn src ~pos ~len) ~len ;; end module Bigstring_dst = struct include Bigstring let unsafe_blit ~src ~src_pos ~dst ~dst_pos ~len = let blit = if unsafe_is_safe then Bigstring.blit else Bigstring.unsafe_blit in blit ~src:src.buf ~src_pos:(unsafe_buf_pos src ~pos:src_pos ~len) ~dst ~dst_pos ~len ;; let create ~len = create len end let compact t = let len = t.hi - t.lo in Bigstring.blit ~src:t.buf ~src_pos:t.lo ~len ~dst:t.buf ~dst_pos:t.lo_min; t.lo <- t.lo_min + len; t.hi <- t.hi_max ;; let[@cold] bounded_compact_stale t lo_min hi_max = fail t "Iobuf.bounded_compact got stale snapshot" (lo_min, hi_max) [%sexp_of: Lo_bound.t * Hi_bound.t] ;; let bounded_compact t lo_min hi_max = let len = t.hi - t.lo in if hi_max > t.hi_max || hi_max < lo_min + len || lo_min < t.lo_min then bounded_compact_stale t lo_min hi_max else ( Bigstring.blit ~src:t.buf ~src_pos:t.lo ~len ~dst:t.buf ~dst_pos:lo_min; t.lo <- lo_min + len; t.hi <- hi_max) ;; let read_bin_prot reader t ~pos = let buf_pos = unsafe_buf_pos t ~pos ~len:0 in let pos_ref = ref buf_pos in let a = reader.Bin_prot.Type_class.read t.buf ~pos_ref in let len = !pos_ref - buf_pos in check_range t ~pos ~len; a, len ;; module Consume = struct type src = (read, seek) t module To (Dst : sig type t [@@deriving sexp_of] val create : len:int -> t val length : (t[@local]) -> int val get : t -> int -> char val set : t -> int -> char -> unit val unsafe_blit : (T.t, t) Blit.blit end) = struct include Base_for_tests.Test_blit.Make_distinct_and_test (Char_elt) (T_src) (Dst) let unsafe_blit ~src ~dst ~dst_pos ~len = let blit = if unsafe_is_safe then blit else unsafe_blit in blit ~src ~src_pos:0 ~dst ~dst_pos ~len; unsafe_advance src len ;; let blit ~src ~dst ~dst_pos ~len = blit ~src ~src_pos:0 ~dst ~dst_pos ~len; unsafe_advance src len ;; let blito ~src ?(src_len = length src) ~dst ?dst_pos () = blito ~src ~src_pos:0 ~src_len ~dst ?dst_pos (); unsafe_advance src src_len ;; let sub src ~len = let dst = sub src ~pos:0 ~len in unsafe_advance src len; dst ;; let subo ?len src = let len = match len with | None -> length src | Some len -> len in let dst = subo ~pos:0 ~len src in unsafe_advance src len; dst ;; end module To_bytes = To (Bytes_dst) module To_bigstring = To (Bigstring_dst) module To_string = struct let sub src ~len = let dst = String_dst.sub src ~len ~pos:0 in unsafe_advance src len; dst ;; let subo ?len src = let len = match len with | None -> length src | Some len -> len in let dst = String_dst.subo ~pos:0 ~len src in unsafe_advance src len; dst ;; end type nonrec ('a, 'd, 'w) t_local = (('d, seek) t[@local]) -> ('a[@local]) constraint 'd = [> read ] type nonrec ('a, 'd, 'w) t = (('d, seek) t[@local]) -> 'a constraint 'd = [> read ] let uadv t n x = unsafe_advance t n; x [@@inline always] ;; let uadv_local t n x = unsafe_advance t n; x [@@inline always] ;; let pos t len = buf_pos_exn t ~pos:0 ~len let tail_padded_fixed_string ~padding ~len t = uadv t len (Bigstring.get_tail_padded_fixed_string t.buf ~pos:(pos t len) ~padding ~len ()) ;; let head_padded_fixed_string ~padding ~len t = uadv t len (Bigstring.get_head_padded_fixed_string t.buf ~pos:(pos t len) ~padding ~len ()) ;; let bytes ~str_pos ~len t = let dst = Bytes.create (len + str_pos) in To_bytes.blit ~src:t ~dst ~len ~dst_pos:str_pos; dst ;; let string ~str_pos ~len t = Bytes.unsafe_to_string ~no_mutation_while_string_reachable:(bytes ~str_pos ~len t) ;; let bigstring ~str_pos ~len t = let dst = Bigstring.create (len + str_pos) in To_bigstring.blit ~src:t ~dst ~len ~dst_pos:str_pos; dst ;; let byteso ?(str_pos = 0) ?len t = bytes t ~str_pos ~len: (match len with | None -> length t | Some len -> len) ;; let stringo ?(str_pos = 0) ?len t = string t ~str_pos ~len: (match len with | None -> length t | Some len -> len) ;; let bigstringo ?(str_pos = 0) ?len t = bigstring t ~str_pos ~len: (match len with | None -> length t | Some len -> len) ;; let bin_prot reader t = let a, len = read_bin_prot reader t ~pos:0 in uadv t len a ;; module Local = struct let tail_padded_fixed_string ~padding ~len t = (uadv_local t len (Bigstring.get_tail_padded_fixed_string_local t.buf ~pos:(pos t len) ~padding ~len ())) ;; let head_padded_fixed_string ~padding ~len t = (uadv_local t len (Bigstring.get_head_padded_fixed_string_local t.buf ~pos:(pos t len) ~padding ~len ())) ;; let bytes ~str_pos ~len t = (let dst = Bytes.create_local (len + str_pos) in To_bytes.blit ~src:t ~dst ~len ~dst_pos:str_pos; dst) ;; let string ~str_pos ~len t = (Bytes.unsafe_to_string ~no_mutation_while_string_reachable:(bytes ~str_pos ~len t)) ;; let byteso ?(str_pos = 0) ?len t = (bytes t ~str_pos ~len: (match len with | None -> length t | Some len -> len)) ;; let stringo ?(str_pos = 0) ?len t = (string t ~str_pos ~len: (match len with | None -> length t | Some len -> len)) ;; open Bigstring let len = 8 let[@inline always] int64_t_be t = (uadv_local t len (Local.unsafe_get_int64_t_be t.buf ~pos:(pos t len)) [@nontail]) ;; let[@inline always] int64_t_le t = (uadv_local t len (Local.unsafe_get_int64_t_le t.buf ~pos:(pos t len)) [@nontail]) ;; end open Bigstring let len = 1 let[@inline always] char t = uadv t len (Bigstring.unsafe_get t.buf (pos t len)) let[@inline always] uint8 t = uadv t len (unsafe_get_uint8 t.buf ~pos:(pos t len)) let[@inline always] int8 t = uadv t len (unsafe_get_int8 t.buf ~pos:(pos t len)) let len = 2 let[@inline always] int16_be t = uadv t len (unsafe_get_int16_be t.buf ~pos:(pos t len)) let[@inline always] int16_le t = uadv t len (unsafe_get_int16_le t.buf ~pos:(pos t len)) let[@inline always] uint16_be t = uadv t len (unsafe_get_uint16_be t.buf ~pos:(pos t len)) ;; let[@inline always] uint16_le t = uadv t len (unsafe_get_uint16_le t.buf ~pos:(pos t len)) ;; let len = 4 let[@inline always] int32_be t = uadv t len (unsafe_get_int32_be t.buf ~pos:(pos t len)) let[@inline always] int32_t_be t = uadv t len (unsafe_get_int32_t_be t.buf ~pos:(pos t len)) ;; let[@inline always] int32_le t = uadv t len (unsafe_get_int32_le t.buf ~pos:(pos t len)) let[@inline always] int32_t_le t = uadv t len (unsafe_get_int32_t_le t.buf ~pos:(pos t len)) ;; let[@inline always] uint32_be t = uadv t len (unsafe_get_uint32_be t.buf ~pos:(pos t len)) ;; let[@inline always] uint32_le t = uadv t len (unsafe_get_uint32_le t.buf ~pos:(pos t len)) ;; let len = 8 let[@inline always] int64_be_exn t = uadv t len (unsafe_get_int64_be_exn t.buf ~pos:(pos t len)) ;; let[@inline always] int64_le_exn t = uadv t len (unsafe_get_int64_le_exn t.buf ~pos:(pos t len)) ;; let[@inline always] uint64_be_exn t = uadv t len (unsafe_get_uint64_be_exn t.buf ~pos:(pos t len)) ;; let[@inline always] uint64_le_exn t = uadv t len (unsafe_get_uint64_le_exn t.buf ~pos:(pos t len)) ;; let[@inline always] int64_t_be t = uadv t len (unsafe_get_int64_t_be t.buf ~pos:(pos t len)) ;; let[@inline always] int64_t_le t = uadv t len (unsafe_get_int64_t_le t.buf ~pos:(pos t len)) ;; let[@inline always] int64_be_trunc t = uadv t len (unsafe_get_int64_be_trunc t.buf ~pos:(pos t len)) ;; let[@inline always] int64_le_trunc t = uadv t len (unsafe_get_int64_le_trunc t.buf ~pos:(pos t len)) ;; module Int_repr = struct let[@inline always] uint8 t = IR.Uint8.of_base_int_trunc (uint8 t) let[@inline always] uint16_be t = IR.Uint16.of_base_int_trunc (uint16_be t) let[@inline always] uint16_le t = IR.Uint16.of_base_int_trunc (uint16_le t) let[@inline always] uint32_be t = IR.Uint32.of_base_int32_trunc (int32_t_be t) let[@inline always] uint32_le t = IR.Uint32.of_base_int32_trunc (int32_t_le t) let[@inline always] uint64_be t = IR.Uint64.of_base_int64_trunc (int64_t_be t) let[@inline always] uint64_le t = IR.Uint64.of_base_int64_trunc (int64_t_le t) let[@inline always] int8 t = IR.Int8.of_base_int_trunc (int8 t) let[@inline always] int16_be t = IR.Int16.of_base_int_trunc (int16_be t) let[@inline always] int16_le t = IR.Int16.of_base_int_trunc (int16_le t) let[@inline always] int32_be t = IR.Int32.of_base_int32 (int32_t_be t) let[@inline always] int32_le t = IR.Int32.of_base_int32 (int32_t_le t) let[@inline always] int64_be t = int64_t_be t let[@inline always] int64_le t = int64_t_le t end end let write_bin_prot writer t ~pos a = let len = writer.Bin_prot.Type_class.size a in let buf_pos = buf_pos_exn t ~pos ~len in let stop_pos = writer.Bin_prot.Type_class.write t.buf ~pos:buf_pos a in if stop_pos - buf_pos = len then len else fail t "Iobuf.write_bin_prot got unexpected number of bytes written (Bin_prot bug: \ Type_class.write disagrees with .size)" (`size_len len, `buf_pos buf_pos, `write_stop_pos stop_pos) [%sexp_of: [ `size_len of int ] * [ `buf_pos of int ] * [ `write_stop_pos of int ]] ;; (* [Itoa] provides a range of functions for integer to ASCII conversion, used by [Poke], [Fill] and their [Unsafe] versions. The implementation here is done in terms of negative decimals due to the properties of [Int.min_value]. Since the result of [Int.(abs min_value)] is [Int.min_value], an attempt to utilize a positive decimal loop by writing the sign and calling [Int.abs x] fails. The converse, with [- Int.max_value] works for both cases. *) module Itoa = struct (* [num_digits x] returns the number of digits in [x] for non-positive integers ([num_digits 0] is defined as 1). The below tends to perform better than a binary search or [/= 10 while <> 0], likely due to decimal values for our applications skewing towards smaller numbers. *) let num_digits x = if x > -10 then 1 else if x > -100 then 2 else if x > -1000 then 3 else if x > -10000 then 4 else if x > -100000 then 5 else if x > -1000000 then 6 else if x > -10000000 then 7 else if x > -100000000 then 8 else if x > -1000000000 then 9 else if arch_sixtyfour then if x > -1000000000 * 10 then 10 else if x > -1000000000 * 100 then 11 else if x > -1000000000 * 1000 then 12 else if x > -1000000000 * 10000 then 13 else if x > -1000000000 * 100000 then 14 else if x > -1000000000 * 1000000 then 15 else if x > -1000000000 * 10000000 then 16 else if x > -1000000000 * 100000000 then 17 else if x > -1000000000 * 1000000000 then 18 else 19 else 10 ;; let () = assert (String.length (Int.to_string Int.min_value) <= 19 + 1) (* Despite the div/mod by a constant optimizations, it's a slight savings to avoid a second div/mod. Note also that passing in an [int ref], rather than creating the ref locally here, results in allocation on the benchmarks. *) let unsafe_poke_negative_decimal_without_sign t ~pos ~len int = let int = ref int in for pos = pos + len - 1 downto pos do let x = !int in int := !int / 10; Bigstring.unsafe_set t.buf pos (Char.unsafe_of_int (48 + (-x + (!int * 10)))) done ;; let unsafe_poke_negative_decimal t ~pos ~len int = Bigstring.unsafe_set t.buf pos '-'; (* +1 and -1 to account for '-' *) unsafe_poke_negative_decimal_without_sign t ~pos:(pos + 1) ~len:(len - 1) int ;; let poke_decimal t ~pos int = if int < 0 then ( let len = 1 + num_digits int in unsafe_poke_negative_decimal t ~pos:(buf_pos_exn t ~pos ~len) ~len int; len) else ( let len = num_digits (-int) in unsafe_poke_negative_decimal_without_sign t ~pos:(buf_pos_exn t ~pos ~len) ~len (-int); len) ;; let unsafe_poke_decimal t ~pos int = if int < 0 then ( let len = 1 + num_digits int in unsafe_poke_negative_decimal t ~pos:(unsafe_buf_pos t ~pos ~len) ~len int; len) else ( let len = num_digits (-int) in unsafe_poke_negative_decimal_without_sign t ~pos:(unsafe_buf_pos t ~pos ~len) ~len (-int); len) ;; end module Fill = struct type nonrec ('a, 'd, 'w) t_local = ((read_write, seek) t[@local]) -> ('a[@local]) -> unit constraint 'd = [> read ] type nonrec ('a, 'd, 'w) t = ((read_write, seek) t[@local]) -> 'a -> unit constraint 'd = [> read ] let[@inline] pos t len = buf_pos_exn t ~pos:0 ~len let uadv = unsafe_advance let tail_padded_fixed_string ~padding ~len t (src [@local]) = Bigstring.set_tail_padded_fixed_string ~padding ~len t.buf ~pos:(pos t len) src; uadv t len ;; let head_padded_fixed_string ~padding ~len t (src [@local]) = Bigstring.set_head_padded_fixed_string ~padding ~len t.buf ~pos:(pos t len) src; uadv t len ;; let bytes ~str_pos ~len t (src [@local]) = Bigstring.From_bytes.blit ~src ~src_pos:str_pos ~len ~dst:t.buf ~dst_pos:(pos t len); uadv t len ;; let string ~str_pos ~len t (src [@local]) = Bigstring.From_string.blit ~src ~src_pos:str_pos ~len ~dst:t.buf ~dst_pos:(pos t len); uadv t len ;; let bigstring ~str_pos ~len t (src [@local]) = Bigstring.blit ~src ~src_pos:str_pos ~len ~dst:t.buf ~dst_pos:(pos t len); uadv t len ;; let byteso ?(str_pos = 0) ?len t (src [@local]) = bytes t src ~str_pos ~len: (match len with | None -> Bytes.length src - str_pos | Some len -> len) ;; let stringo ?(str_pos = 0) ?len t (src [@local]) = string t src ~str_pos ~len: (match len with | None -> String.length src - str_pos | Some len -> len) ;; let bigstringo ?(str_pos = 0) ?len t (src [@local]) = bigstring t src ~str_pos ~len: (match len with | None -> Bigstring.length src - str_pos | Some len -> len) ;; let bin_prot writer t a = write_bin_prot writer t ~pos:0 a |> uadv t open Bigstring let len = 1 let[@inline always] char t c = Bigstring.unsafe_set t.buf (pos t len) c; uadv t len ;; let[@inline always] uint8_trunc t i = unsafe_set_uint8 t.buf i ~pos:(pos t len); uadv t len ;; let[@inline always] int8_trunc t i = unsafe_set_int8 t.buf i ~pos:(pos t len); uadv t len ;; let len = 2 let[@inline always] int16_be_trunc t i = unsafe_set_int16_be t.buf i ~pos:(pos t len); uadv t len ;; let[@inline always] int16_le_trunc t i = unsafe_set_int16_le t.buf i ~pos:(pos t len); uadv t len ;; let[@inline always] uint16_be_trunc t i = unsafe_set_uint16_be t.buf i ~pos:(pos t len); uadv t len ;; let[@inline always] uint16_le_trunc t i = unsafe_set_uint16_le t.buf i ~pos:(pos t len); uadv t len ;; let len = 4 let[@inline always] int32_be_trunc t i = unsafe_set_int32_be t.buf i ~pos:(pos t len); uadv t len ;; let[@inline always] int32_t_be t i = unsafe_set_int32_t_be t.buf i ~pos:(pos t len); uadv t len ;; let[@inline always] int32_le_trunc t i = unsafe_set_int32_le t.buf i ~pos:(pos t len); uadv t len ;; let[@inline always] int32_t_le t i = unsafe_set_int32_t_le t.buf i ~pos:(pos t len); uadv t len ;; let[@inline always] uint32_be_trunc t i = unsafe_set_uint32_be t.buf i ~pos:(pos t len); uadv t len ;; let[@inline always] uint32_le_trunc t i = unsafe_set_uint32_le t.buf i ~pos:(pos t len); uadv t len ;; let len = 8 let[@inline always] int64_be t i = unsafe_set_int64_be t.buf i ~pos:(pos t len); uadv t len ;; let[@inline always] int64_le t i = unsafe_set_int64_le t.buf i ~pos:(pos t len); uadv t len ;; let[@inline always] uint64_be_trunc t i = unsafe_set_uint64_be t.buf i ~pos:(pos t len); uadv t len ;; let[@inline always] uint64_le_trunc t i = unsafe_set_uint64_le t.buf i ~pos:(pos t len); uadv t len ;; let[@inline always] int64_t_be t (i [@local]) = unsafe_set_int64_t_be t.buf i ~pos:(pos t len); uadv t len ;; let[@inline always] int64_t_le t (i [@local]) = unsafe_set_int64_t_le t.buf i ~pos:(pos t len); uadv t len ;; let decimal t i = uadv t (Itoa.poke_decimal t ~pos:0 i) module Int_repr = struct let[@inline always] uint8 t i = uint8_trunc t (IR.Uint8.to_base_int i) let[@inline always] uint16_be t i = uint16_be_trunc t (IR.Uint16.to_base_int i) let[@inline always] uint16_le t i = uint16_le_trunc t (IR.Uint16.to_base_int i) let[@inline always] uint32_be t i = int32_t_be t (IR.Uint32.to_base_int32_trunc i) let[@inline always] uint32_le t i = int32_t_le t (IR.Uint32.to_base_int32_trunc i) let[@inline always] uint64_be t i = int64_t_be t (IR.Uint64.to_base_int64_trunc i) let[@inline always] uint64_le t i = int64_t_le t (IR.Uint64.to_base_int64_trunc i) let[@inline always] int8 t i = int8_trunc t (IR.Int8.to_base_int i) let[@inline always] int16_be t i = int16_be_trunc t (IR.Int16.to_base_int i) let[@inline always] int16_le t i = int16_le_trunc t (IR.Int16.to_base_int i) let[@inline always] int32_be t i = int32_t_be t (IR.Int32.to_base_int32 i) let[@inline always] int32_le t i = int32_t_le t (IR.Int32.to_base_int32 i) let[@inline always] int64_be t i = int64_t_be t i let[@inline always] int64_le t i = int64_t_le t i end end module Peek = struct type 'seek src = (read, 'seek) t module To_bytes = Base_for_tests.Test_blit.Make_distinct_and_test (Char_elt) (T_src) (Bytes_dst) module To_bigstring = Base_for_tests.Test_blit.Make_distinct_and_test (Char_elt) (T_src) (Bigstring_dst) module To_string = String_dst type nonrec ('a, 'd, 'w) t_local = (('d, 'w) t[@local]) -> pos:int -> ('a[@local]) constraint 'd = [> read ] type nonrec ('a, 'd, 'w) t = (('d, 'w) t[@local]) -> pos:int -> 'a constraint 'd = [> read ] let spos = buf_pos_exn (* "safe position" *) let tail_padded_fixed_string ~padding ~len t ~pos = Bigstring.get_tail_padded_fixed_string t.buf ~padding ~len ~pos:(spos t ~len ~pos) () ;; let head_padded_fixed_string ~padding ~len t ~pos = Bigstring.get_head_padded_fixed_string t.buf ~padding ~len ~pos:(spos t ~len ~pos) () ;; let bytes ~str_pos ~len t ~pos = let dst = Bytes.create (len + str_pos) in Bigstring.To_bytes.blit ~src:t.buf ~src_pos:(spos t ~len ~pos) ~len ~dst ~dst_pos:str_pos; dst ;; let string ~str_pos ~len t ~pos = Bytes.unsafe_to_string ~no_mutation_while_string_reachable:(bytes ~str_pos ~len t ~pos) ;; let bigstring ~str_pos ~len t ~pos = let dst = Bigstring.create (len + str_pos) in Bigstring.blit ~src:t.buf ~src_pos:(spos t ~len ~pos) ~len ~dst ~dst_pos:str_pos; dst ;; let byteso ?(str_pos = 0) ?len t ~pos = bytes t ~pos ~str_pos ~len: (match len with | None -> length t - pos | Some len -> len) ;; let stringo ?(str_pos = 0) ?len t ~pos = string t ~pos ~str_pos ~len: (match len with | None -> length t - pos | Some len -> len) ;; let bigstringo ?(str_pos = 0) ?len t ~pos = bigstring t ~pos ~str_pos ~len: (match len with | None -> length t - pos | Some len -> len) ;; let bin_prot reader t ~pos = read_bin_prot reader t ~pos |> fst let index t ?(pos = 0) ?(len = length t - pos) c = let pos = spos t ~len ~pos in Option.map (Bigstring.find ~pos ~len c t.buf) ~f:(fun x -> x - t.lo) [@nontail] ;; module Local = struct let tail_padded_fixed_string ~padding ~len t ~pos = (Bigstring.get_tail_padded_fixed_string_local t.buf ~padding ~len ~pos:(spos t ~len ~pos) ()) ;; let head_padded_fixed_string ~padding ~len t ~pos = (Bigstring.get_head_padded_fixed_string_local t.buf ~padding ~len ~pos:(spos t ~len ~pos) ()) ;; let bytes ~str_pos ~len t ~pos = (let dst = Bytes.create_local (len + str_pos) in Bigstring.To_bytes.blit ~src:t.buf ~src_pos:(spos t ~len ~pos) ~len ~dst ~dst_pos:str_pos; dst) ;; let string ~str_pos ~len t ~pos = (Bytes.unsafe_to_string ~no_mutation_while_string_reachable:(bytes ~str_pos ~len t ~pos)) ;; let byteso ?(str_pos = 0) ?len t ~pos = (bytes t ~pos ~str_pos ~len: (match len with | None -> length t - pos | Some len -> len)) ;; let stringo ?(str_pos = 0) ?len t ~pos = (string t ~pos ~str_pos ~len: (match len with | None -> length t - pos | Some len -> len)) ;; open Bigstring let len = 8 let[@inline always] int64_t_be t ~pos = (Local.unsafe_get_int64_t_be t.buf ~pos:(spos t ~len ~pos) [@nontail]) ;; let[@inline always] int64_t_le t ~pos = (Local.unsafe_get_int64_t_le t.buf ~pos:(spos t ~len ~pos) [@nontail]) ;; end open Bigstring let[@inline always] char t ~pos = get_char t pos let len = 1 let[@inline always] uint8 t ~pos = unsafe_get_uint8 t.buf ~pos:(spos t ~len ~pos) let[@inline always] int8 t ~pos = unsafe_get_int8 t.buf ~pos:(spos t ~len ~pos) let len = 2 let[@inline always] int16_be t ~pos = unsafe_get_int16_be t.buf ~pos:(spos t ~len ~pos) let[@inline always] int16_le t ~pos = unsafe_get_int16_le t.buf ~pos:(spos t ~len ~pos) let[@inline always] uint16_be t ~pos = unsafe_get_uint16_be t.buf ~pos:(spos t ~len ~pos) ;; let[@inline always] uint16_le t ~pos = unsafe_get_uint16_le t.buf ~pos:(spos t ~len ~pos) ;; let len = 4 let[@inline always] int32_be t ~pos = unsafe_get_int32_be t.buf ~pos:(spos t ~len ~pos) let[@inline always] int32_t_be t ~pos = unsafe_get_int32_t_be t.buf ~pos:(spos t ~len ~pos) ;; let[@inline always] int32_le t ~pos = unsafe_get_int32_le t.buf ~pos:(spos t ~len ~pos) let[@inline always] int32_t_le t ~pos = unsafe_get_int32_t_le t.buf ~pos:(spos t ~len ~pos) ;; let[@inline always] uint32_be t ~pos = unsafe_get_uint32_be t.buf ~pos:(spos t ~len ~pos) ;; let[@inline always] uint32_le t ~pos = unsafe_get_uint32_le t.buf ~pos:(spos t ~len ~pos) ;; let len = 8 let[@inline always] int64_be_exn t ~pos = unsafe_get_int64_be_exn t.buf ~pos:(spos t ~len ~pos) ;; let[@inline always] int64_le_exn t ~pos = unsafe_get_int64_le_exn t.buf ~pos:(spos t ~len ~pos) ;; let[@inline always] uint64_be_exn t ~pos = unsafe_get_uint64_be_exn t.buf ~pos:(spos t ~len ~pos) ;; let[@inline always] uint64_le_exn t ~pos = unsafe_get_uint64_le_exn t.buf ~pos:(spos t ~len ~pos) ;; let[@inline always] int64_t_be t ~pos = unsafe_get_int64_t_be t.buf ~pos:(spos t ~len ~pos) ;; let[@inline always] int64_t_le t ~pos = unsafe_get_int64_t_le t.buf ~pos:(spos t ~len ~pos) ;; let[@inline always] int64_be_trunc t ~pos = unsafe_get_int64_be_trunc t.buf ~pos:(spos t ~len ~pos) ;; let[@inline always] int64_le_trunc t ~pos = unsafe_get_int64_le_trunc t.buf ~pos:(spos t ~len ~pos) ;; module Int_repr = struct let[@inline always] uint8 t ~pos = IR.Uint8.of_base_int_trunc (uint8 t ~pos) let[@inline always] uint16_be t ~pos = IR.Uint16.of_base_int_trunc (uint16_be t ~pos) let[@inline always] uint16_le t ~pos = IR.Uint16.of_base_int_trunc (uint16_le t ~pos) let[@inline always] uint32_be t ~pos = IR.Uint32.of_base_int32_trunc (int32_t_be t ~pos) ;; let[@inline always] uint32_le t ~pos = IR.Uint32.of_base_int32_trunc (int32_t_le t ~pos) ;; let[@inline always] uint64_be t ~pos = IR.Uint64.of_base_int64_trunc (int64_t_be t ~pos) ;; let[@inline always] uint64_le t ~pos = IR.Uint64.of_base_int64_trunc (int64_t_le t ~pos) ;; let[@inline always] int8 t ~pos = IR.Int8.of_base_int_trunc (int8 t ~pos) let[@inline always] int16_be t ~pos = IR.Int16.of_base_int_trunc (int16_be t ~pos) let[@inline always] int16_le t ~pos = IR.Int16.of_base_int_trunc (int16_le t ~pos) let[@inline always] int32_be t ~pos = IR.Int32.of_base_int32 (int32_t_be t ~pos) let[@inline always] int32_le t ~pos = IR.Int32.of_base_int32 (int32_t_le t ~pos) let[@inline always] int64_be t ~pos = int64_t_be t ~pos let[@inline always] int64_le t ~pos = int64_t_le t ~pos end end module Poke = struct type nonrec ('a, 'd, 'w) t_local = ((read_write, 'w) t[@local]) -> pos:int -> ('a[@local]) -> unit constraint 'd = [> read ] type nonrec ('a, 'd, 'w) t = ((read_write, 'w) t[@local]) -> pos:int -> 'a -> unit constraint 'd = [> read ] let spos = buf_pos_exn (* "safe position" *) let tail_padded_fixed_string ~padding ~len t ~pos src = Bigstring.set_tail_padded_fixed_string ~padding ~len t.buf ~pos:(spos t ~len ~pos) src ;; let head_padded_fixed_string ~padding ~len t ~pos src = Bigstring.set_head_padded_fixed_string ~padding ~len t.buf ~pos:(spos t ~len ~pos) src ;; let bytes ~str_pos ~len t ~pos src = Bigstring.From_bytes.blit ~src ~src_pos:str_pos ~len ~dst:t.buf ~dst_pos:(spos t ~len ~pos) ;; let string ~str_pos ~len t ~pos src = Bigstring.From_string.blit ~src ~src_pos:str_pos ~len ~dst:t.buf ~dst_pos:(spos t ~len ~pos) ;; let bigstring ~str_pos ~len t ~pos src = Bigstring.blit ~src ~src_pos:str_pos ~len ~dst:t.buf ~dst_pos:(spos t ~len ~pos) ;; let byteso ?(str_pos = 0) ?len t ~pos src = bytes t ~str_pos ~pos src ~len: (match len with | None -> Bytes.length src - str_pos | Some len -> len) ;; let stringo ?(str_pos = 0) ?len t ~pos src = string t ~str_pos ~pos src ~len: (match len with | None -> String.length src - str_pos | Some len -> len) ;; let bigstringo ?(str_pos = 0) ?len t ~pos src = bigstring t ~str_pos ~pos src ~len: (match len with | None -> Bigstring.length src - str_pos | Some len -> len) ;; let bin_prot_size = write_bin_prot let bin_prot writer t ~pos a = ignore (bin_prot_size writer t ~pos a : int) open Bigstring let len = 1 let[@inline always] char t ~pos c = set_char t pos c let[@inline always] uint8_trunc t ~pos i = unsafe_set_uint8 t.buf ~pos:(spos t ~len ~pos) i ;; let[@inline always] int8_trunc t ~pos i = unsafe_set_int8 t.buf ~pos:(spos t ~len ~pos) i ;; let len = 2 let[@inline always] int16_be_trunc t ~pos i = unsafe_set_int16_be t.buf ~pos:(spos t ~len ~pos) i ;; let[@inline always] int16_le_trunc t ~pos i = unsafe_set_int16_le t.buf ~pos:(spos t ~len ~pos) i ;; let[@inline always] uint16_be_trunc t ~pos i = unsafe_set_uint16_be t.buf ~pos:(spos t ~len ~pos) i ;; let[@inline always] uint16_le_trunc t ~pos i = unsafe_set_uint16_le t.buf ~pos:(spos t ~len ~pos) i ;; let len = 4 let[@inline always] int32_be_trunc t ~pos i = unsafe_set_int32_be t.buf ~pos:(spos t ~len ~pos) i ;; let[@inline always] int32_t_be t ~pos i = unsafe_set_int32_t_be t.buf ~pos:(spos t ~len ~pos) i ;; let[@inline always] int32_le_trunc t ~pos i = unsafe_set_int32_le t.buf ~pos:(spos t ~len ~pos) i ;; let[@inline always] int32_t_le t ~pos i = unsafe_set_int32_t_le t.buf ~pos:(spos t ~len ~pos) i ;; let[@inline always] uint32_be_trunc t ~pos i = unsafe_set_uint32_be t.buf ~pos:(spos t ~len ~pos) i ;; let[@inline always] uint32_le_trunc t ~pos i = unsafe_set_uint32_le t.buf ~pos:(spos t ~len ~pos) i ;; let len = 8 let[@inline always] int64_be t ~pos i = unsafe_set_int64_be t.buf ~pos:(spos t ~len ~pos) i ;; let[@inline always] int64_le t ~pos i = unsafe_set_int64_le t.buf ~pos:(spos t ~len ~pos) i ;; let[@inline always] uint64_be_trunc t ~pos i = unsafe_set_uint64_be t.buf ~pos:(spos t ~len ~pos) i ;; let[@inline always] uint64_le_trunc t ~pos i = unsafe_set_uint64_le t.buf ~pos:(spos t ~len ~pos) i ;; let[@inline always] int64_t_be t ~pos i = unsafe_set_int64_t_be t.buf ~pos:(spos t ~len ~pos) i ;; let[@inline always] int64_t_le t ~pos i = unsafe_set_int64_t_le t.buf ~pos:(spos t ~len ~pos) i ;; let decimal = Itoa.poke_decimal module Int_repr = struct let[@inline always] uint8 t ~pos i = uint8_trunc t ~pos (IR.Uint8.to_base_int i) let[@inline always] uint16_be t ~pos i = uint16_be_trunc t ~pos (IR.Uint16.to_base_int i) ;; let[@inline always] uint16_le t ~pos i = uint16_le_trunc t ~pos (IR.Uint16.to_base_int i) ;; let[@inline always] uint32_be t ~pos i = int32_t_be t ~pos (IR.Uint32.to_base_int32_trunc i) ;; let[@inline always] uint32_le t ~pos i = int32_t_le t ~pos (IR.Uint32.to_base_int32_trunc i) ;; let[@inline always] uint64_be t ~pos i = int64_t_be t ~pos (IR.Uint64.to_base_int64_trunc i) ;; let[@inline always] uint64_le t ~pos i = int64_t_le t ~pos (IR.Uint64.to_base_int64_trunc i) ;; let[@inline always] int8 t ~pos i = int8_trunc t ~pos (IR.Int8.to_base_int i) let[@inline always] int16_be t ~pos i = int16_be_trunc t ~pos (IR.Int16.to_base_int i) let[@inline always] int16_le t ~pos i = int16_le_trunc t ~pos (IR.Int16.to_base_int i) let[@inline always] int32_be t ~pos i = int32_t_be t ~pos (IR.Int32.to_base_int32 i) let[@inline always] int32_le t ~pos i = int32_t_le t ~pos (IR.Int32.to_base_int32 i) let[@inline always] int64_be t ~pos i = int64_t_be t ~pos i let[@inline always] int64_le t ~pos i = int64_t_le t ~pos i end end module Blit = struct module T_dst = struct include T_src let unsafe_blit ~src ~src_pos ~dst ~dst_pos ~len = (* Unlike other blitting functions, we use [Bigstring.unsafe_blit] here (regardless of the value of [unsafe_is_safe]), since we have two [Iobuf.t]s and can therefore bounds-check both buffers before calling [Bigstring.unsafe_blit]. *) Bigstring.unsafe_blit ~len ~src:src.buf ~src_pos:(unsafe_buf_pos src ~pos:src_pos ~len) ~dst:dst.buf ~dst_pos:(unsafe_buf_pos dst ~pos:dst_pos ~len) ;; end include Base_for_tests.Test_blit.Make_and_test (Char_elt) (T_dst) (* Workaround the inability of the compiler to inline in the presence of functors. *) let unsafe_blit = T_dst.unsafe_blit let blit_maximal ~src ?(src_pos = 0) ~dst ?(dst_pos = 0) () = let len = min (length src - src_pos) (length dst - dst_pos) in blit ~src ~src_pos ~dst ~dst_pos ~len; len ;; end module Blit_consume = struct let unsafe_blit ~src ~dst ~dst_pos ~len = Blit.unsafe_blit ~src ~src_pos:0 ~dst ~dst_pos ~len; unsafe_advance src len ;; let blit ~src ~dst ~dst_pos ~len = Blit.blit ~src ~src_pos:0 ~dst ~dst_pos ~len; unsafe_advance src len ;; let blito ~src ?(src_len = length src) ~dst ?(dst_pos = 0) () = blit ~src ~dst ~dst_pos ~len:src_len ;; let sub src ~len = let dst = Blit.sub src ~pos:0 ~len in unsafe_advance src len; dst ;; let subo ?len src = let len = match len with | None -> length src | Some len -> len in sub src ~len ;; let blit_maximal ~src ~dst ?(dst_pos = 0) () = let len = min (length src) (length dst - dst_pos) in blit ~src ~dst ~dst_pos ~len; len ;; end module Blit_fill = struct let unsafe_blit ~src ~src_pos ~dst ~len = Blit.unsafe_blit ~src ~src_pos ~dst ~dst_pos:0 ~len; unsafe_advance dst len ;; let blit ~src ~src_pos ~dst ~len = Blit.blit ~src ~src_pos ~dst ~dst_pos:0 ~len; unsafe_advance dst len ;; let blito ~src ?(src_pos = 0) ?(src_len = length src - src_pos) ~dst () = blit ~src ~src_pos ~dst ~len:src_len ;; let blit_maximal ~src ?(src_pos = 0) ~dst () = let len = min (length src - src_pos) (length dst) in blit ~src ~src_pos ~dst ~len; len ;; end module Blit_consume_and_fill = struct let unsafe_blit ~src ~dst ~len = if phys_equal src dst then advance src len else ( Blit.unsafe_blit ~src ~src_pos:0 ~dst ~dst_pos:0 ~len; unsafe_advance src len; unsafe_advance dst len) ;; let blit ~src ~dst ~len = if phys_equal src dst then advance src len else ( Blit.blit ~src ~src_pos:0 ~dst ~dst_pos:0 ~len; unsafe_advance src len; unsafe_advance dst len) ;; let blito ~src ?(src_len = length src) ~dst () = blit ~src ~dst ~len:src_len let blit_maximal ~src ~dst = let len = min (length src) (length dst) in (* [len] is naturally validated to be correct; don't double-check it. Sadly, we can't do this for the other [Blit_*] modules, as they can have invalid [src_pos]/[dst_pos] values which a) have to be checked on their own and b) can lead to the construction of unsafe [len] values. *) unsafe_blit ~src ~dst ~len; len ;; end let transfer ~src ~dst = reset dst; Blit_fill.blito ~src ~dst (); flip_lo dst ;; let bin_prot_length_prefix_bytes = 4 let consume_bin_prot t bin_prot_reader = let result = if length t < bin_prot_length_prefix_bytes then error "Iobuf.consume_bin_prot not enough data to read length" ([%globalize: t_repr] t) [%sexp_of: (_, _) t] else ( let mark = t.lo in let v_len = Consume.int32_be t in if v_len > length t then ( t.lo <- mark; error "Iobuf.consume_bin_prot not enough data to read value" (v_len, [%globalize: t_repr] t) [%sexp_of: int * (_, _) t]) else Ok (Consume.bin_prot bin_prot_reader t)) in result ;; let fill_bin_prot t writer v = let v_len = writer.Bin_prot.Type_class.size v in let need = v_len + bin_prot_length_prefix_bytes in let result = if need > length t then error "Iobuf.fill_bin_prot not enough space" (need, [%globalize: t_repr] t) [%sexp_of: int * (_, _) t] else ( Fill.int32_be_trunc t v_len; Fill.bin_prot writer t v; Ok ()) in result ;; module Expert = struct let buf t = t.buf let hi_max t = t.hi_max let hi t = t.hi let lo t = t.lo let lo_min t = t.lo_min let set_buf t buf = t.buf <- buf let set_hi_max t hi_max = t.hi_max <- hi_max let set_hi t hi = t.hi <- hi let set_lo t lo = t.lo <- lo let set_lo_min t lo_min = t.lo_min <- lo_min let buf_pos_exn = buf_pos_exn let unsafe_buf_pos = unsafe_buf_pos let ?pos ?len t = let pos, len = Ordered_collection_common.get_pos_len_exn () ?pos ?len ~total_length:(length t) in Bigstring.sub_shared t.buf ~pos:(t.lo + pos) ~len ;; let unsafe_reinitialize t ~lo_min ~lo ~hi ~hi_max buf = (* avoid [caml_modify], if possible *) if not (phys_equal t.buf buf) then t.buf <- buf; t.lo_min <- lo_min; t.lo <- lo; t.hi <- hi; t.hi_max <- hi_max ;; let reinitialize t ~lo_min ~lo ~hi ~hi_max buf = if not (0 <= lo_min && lo_min <= lo && lo <= hi && hi <= hi_max && hi_max <= Bigstring.length buf) then raise_s [%message "Expert.reinitialize got invalid bounds" (lo_min : int) (lo : int) (hi : int) (hi_max : int) (Bigstring.length buf : int)]; unsafe_reinitialize t ~lo_min ~lo ~hi ~hi_max buf ;; let unsafe_reinitialize = if unsafe_is_safe then reinitialize else unsafe_reinitialize let _remember_to_update_unsafe_reinitialize : (_, _) t -> buf:Bigstring.t -> lo_min:int -> lo:int -> hi:int -> hi_max:int -> unit = Fields.Direct.set_all_mutable_fields ;; let reinitialize_of_bigstring t ~pos ~len buf = let str_len = Bigstring.length buf in if pos < 0 || pos > str_len then raise_s [%message "Expert.reinitialize_of_bigstring got invalid pos" (pos : int) (str_len : int)]; let max_len = str_len - pos in if len < 0 || len > max_len then raise_s [%message "Expert.reinitialize_of_bigstring got invalid len" (len : int) (max_len : int)]; let lo = pos in let hi = pos + len in unsafe_reinitialize t ~lo_min:lo ~lo ~hi ~hi_max:hi buf ;; let set_bounds_and_buffer = set_bounds_and_buffer let set_bounds_and_buffer_sub = set_bounds_and_buffer_sub let protect_window t ~f = let lo = t.lo in let hi = t.hi in try let result = f t in t.lo <- lo; t.hi <- hi; result with | exn -> t.lo <- lo; t.hi <- hi; raise exn ;; let protect_window_1 t x ~f = let lo = t.lo in let hi = t.hi in try let result = f t x in t.lo <- lo; t.hi <- hi; result with | exn -> t.lo <- lo; t.hi <- hi; raise exn ;; let protect_window_2 t x y ~f = let lo = t.lo in let hi = t.hi in try let result = f t x y in t.lo <- lo; t.hi <- hi; result with | exn -> t.lo <- lo; t.hi <- hi; raise exn ;; end module Unsafe = struct module Consume = struct (* copy of Consume with pos replaced by an unsafe version *) type src = Consume.src module To_bytes = struct include Consume.To_bytes let blit = unsafe_blit end module To_bigstring = struct include Consume.To_bigstring let blit = unsafe_blit end module To_string = Consume.To_string type ('a, 'd, 'w) t = ('a, 'd, 'w) Consume.t type ('a, 'd, 'w) t_local = ('a, 'd, 'w) Consume.t_local let uadv t n x = unsafe_advance t n; x [@@inline always] ;; let uadv_local t n (x [@local]) = unsafe_advance t n; x [@@inline always] ;; let upos t len = unsafe_buf_pos t ~pos:0 ~len let tail_padded_fixed_string ~padding ~len t = uadv t len (Bigstring.get_tail_padded_fixed_string t.buf ~pos:(upos t len) ~padding ~len ()) ;; let head_padded_fixed_string ~padding ~len t = uadv t len (Bigstring.get_head_padded_fixed_string t.buf ~pos:(upos t len) ~padding ~len ()) ;; let bytes = Consume.bytes let string = Consume.string let bigstring = Consume.bigstring let byteso = Consume.byteso let stringo = Consume.stringo let bigstringo = Consume.bigstringo let bin_prot = Consume.bin_prot module Local = struct let tail_padded_fixed_string ~padding ~len t = (uadv_local t len (Bigstring.get_tail_padded_fixed_string_local t.buf ~pos:(upos t len) ~padding ~len ())) ;; let head_padded_fixed_string ~padding ~len t = (uadv_local t len (Bigstring.get_head_padded_fixed_string_local t.buf ~pos:(upos t len) ~padding ~len ())) ;; let bytes = Consume.Local.bytes let string = Consume.Local.string let byteso = Consume.Local.byteso let stringo = Consume.Local.stringo open Bigstring let len = 8 let[@inline always] int64_t_be t = (uadv_local t len (Local.unsafe_get_int64_t_be t.buf ~pos:(upos t len)) [@nontail]) ;; let[@inline always] int64_t_le t = (uadv_local t len (Local.unsafe_get_int64_t_le t.buf ~pos:(upos t len)) [@nontail]) ;; end open Bigstring let len = 1 let[@inline always] char t = uadv t len (Bigstring.unsafe_get t.buf (upos t len)) let[@inline always] uint8 t = uadv t len (unsafe_get_uint8 t.buf ~pos:(upos t len)) let[@inline always] int8 t = uadv t len (unsafe_get_int8 t.buf ~pos:(upos t len)) let len = 2 let[@inline always] int16_be t = uadv t len (unsafe_get_int16_be t.buf ~pos:(upos t len)) ;; let[@inline always] int16_le t = uadv t len (unsafe_get_int16_le t.buf ~pos:(upos t len)) ;; let[@inline always] uint16_be t = uadv t len (unsafe_get_uint16_be t.buf ~pos:(upos t len)) ;; let[@inline always] uint16_le t = uadv t len (unsafe_get_uint16_le t.buf ~pos:(upos t len)) ;; let len = 4 let[@inline always] int32_be t = uadv t len (unsafe_get_int32_be t.buf ~pos:(upos t len)) ;; let[@inline always] int32_t_be t = uadv t len (unsafe_get_int32_t_be t.buf ~pos:(upos t len)) ;; let[@inline always] int32_le t = uadv t len (unsafe_get_int32_le t.buf ~pos:(upos t len)) ;; let[@inline always] int32_t_le t = uadv t len (unsafe_get_int32_t_le t.buf ~pos:(upos t len)) ;; let[@inline always] uint32_be t = uadv t len (unsafe_get_uint32_be t.buf ~pos:(upos t len)) ;; let[@inline always] uint32_le t = uadv t len (unsafe_get_uint32_le t.buf ~pos:(upos t len)) ;; let len = 8 let[@inline always] int64_be_exn t = uadv t len (unsafe_get_int64_be_exn t.buf ~pos:(upos t len)) ;; let[@inline always] int64_le_exn t = uadv t len (unsafe_get_int64_le_exn t.buf ~pos:(upos t len)) ;; let[@inline always] uint64_be_exn t = uadv t len (unsafe_get_uint64_be_exn t.buf ~pos:(upos t len)) ;; let[@inline always] uint64_le_exn t = uadv t len (unsafe_get_uint64_le_exn t.buf ~pos:(upos t len)) ;; let[@inline always] int64_t_be t = uadv t len (unsafe_get_int64_t_be t.buf ~pos:(upos t len)) ;; let[@inline always] int64_t_le t = uadv t len (unsafe_get_int64_t_le t.buf ~pos:(upos t len)) ;; let[@inline always] int64_be_trunc t = uadv t len (unsafe_get_int64_be_trunc t.buf ~pos:(upos t len)) ;; let[@inline always] int64_le_trunc t = uadv t len (unsafe_get_int64_le_trunc t.buf ~pos:(upos t len)) ;; module Int_repr = struct let[@inline always] uint8 t = IR.Uint8.of_base_int_trunc (uint8 t) let[@inline always] uint16_be t = IR.Uint16.of_base_int_trunc (uint16_be t) let[@inline always] uint16_le t = IR.Uint16.of_base_int_trunc (uint16_le t) let[@inline always] uint32_be t = IR.Uint32.of_base_int32_trunc (int32_t_be t) let[@inline always] uint32_le t = IR.Uint32.of_base_int32_trunc (int32_t_le t) let[@inline always] uint64_be t = IR.Uint64.of_base_int64_trunc (int64_t_be t) let[@inline always] uint64_le t = IR.Uint64.of_base_int64_trunc (int64_t_le t) let[@inline always] int8 t = IR.Int8.of_base_int_trunc (int8 t) let[@inline always] int16_be t = IR.Int16.of_base_int_trunc (int16_be t) let[@inline always] int16_le t = IR.Int16.of_base_int_trunc (int16_le t) let[@inline always] int32_be t = IR.Int32.of_base_int32 (int32_t_be t) let[@inline always] int32_le t = IR.Int32.of_base_int32 (int32_t_le t) let[@inline always] int64_be t = int64_t_be t let[@inline always] int64_le t = int64_t_le t end end module Fill = struct type ('a, 'd, 'w) t = ('a, 'd, 'w) Fill.t type ('a, 'd, 'w) t_local = ('a, 'd, 'w) Fill.t_local (* copy with unsafe pos *) let upos t len = unsafe_buf_pos t ~pos:0 ~len let uadv t n = unsafe_advance t n let tail_padded_fixed_string ~padding ~len t src = Bigstring.set_tail_padded_fixed_string ~padding ~len t.buf ~pos:(upos t len) src; uadv t len ;; let head_padded_fixed_string ~padding ~len t src = Bigstring.set_head_padded_fixed_string ~padding ~len t.buf ~pos:(upos t len) src; uadv t len ;; let bytes ~str_pos ~len t src = Bigstring.From_bytes.blit ~src ~src_pos:str_pos ~len ~dst:t.buf ~dst_pos:(upos t len); uadv t len ;; let string ~str_pos ~len t src = Bigstring.From_string.blit ~src ~src_pos:str_pos ~len ~dst:t.buf ~dst_pos:(upos t len); uadv t len ;; let bigstring ~str_pos ~len t src = Bigstring.blit ~src ~src_pos:str_pos ~len ~dst:t.buf ~dst_pos:(upos t len); uadv t len ;; let byteso ?(str_pos = 0) ?len t src = bytes t src ~str_pos ~len: (match len with | None -> Bytes.length src - str_pos | Some len -> len) ;; let stringo ?(str_pos = 0) ?len t src = string t src ~str_pos ~len: (match len with | None -> String.length src - str_pos | Some len -> len) ;; let bigstringo ?(str_pos = 0) ?len t src = bigstring t src ~str_pos ~len: (match len with | None -> Bigstring.length src - str_pos | Some len -> len) ;; let bin_prot = Fill.bin_prot open Bigstring let len = 1 let[@inline always] char t c = Bigstring.unsafe_set t.buf (upos t len) c; uadv t len ;; let len = 2 let[@inline always] int16_be_trunc t i = unsafe_set_int16_be t.buf i ~pos:(upos t len); uadv t len ;; let[@inline always] int16_le_trunc t i = unsafe_set_int16_le t.buf i ~pos:(upos t len); uadv t len ;; let[@inline always] uint16_be_trunc t i = unsafe_set_uint16_be t.buf i ~pos:(upos t len); uadv t len ;; let[@inline always] uint16_le_trunc t i = unsafe_set_uint16_le t.buf i ~pos:(upos t len); uadv t len ;; let len = 4 let[@inline always] int32_be_trunc t i = unsafe_set_int32_be t.buf i ~pos:(upos t len); uadv t len ;; let[@inline always] int32_t_be t i = unsafe_set_int32_t_be t.buf i ~pos:(upos t len); uadv t len ;; let[@inline always] int32_le_trunc t i = unsafe_set_int32_le t.buf i ~pos:(upos t len); uadv t len ;; let[@inline always] int32_t_le t i = unsafe_set_int32_t_le t.buf i ~pos:(upos t len); uadv t len ;; let[@inline always] uint32_be_trunc t i = unsafe_set_uint32_be t.buf i ~pos:(upos t len); uadv t len ;; let[@inline always] uint32_le_trunc t i = unsafe_set_uint32_le t.buf i ~pos:(upos t len); uadv t len ;; let len = 8 let[@inline always] int64_be t i = unsafe_set_int64_be t.buf i ~pos:(upos t len); uadv t len ;; let[@inline always] int64_le t i = unsafe_set_int64_le t.buf i ~pos:(upos t len); uadv t len ;; let[@inline always] uint64_be_trunc t i = unsafe_set_uint64_be t.buf i ~pos:(upos t len); uadv t len ;; let[@inline always] uint64_le_trunc t i = unsafe_set_uint64_le t.buf i ~pos:(upos t len); uadv t len ;; let[@inline always] int64_t_be t i = unsafe_set_int64_t_be t.buf i ~pos:(upos t len); uadv t len ;; let[@inline always] int64_t_le t i = unsafe_set_int64_t_le t.buf i ~pos:(upos t len); uadv t len ;; (* Bigstring int8 accessors are slow C calls. Use the fast char primitive. *) let[@inline always] uint8_trunc t i = char t (Char.unsafe_of_int i) let[@inline always] int8_trunc t i = char t (Char.unsafe_of_int i) let decimal t i = uadv t (Itoa.unsafe_poke_decimal t ~pos:0 i) module Int_repr = struct let[@inline always] uint8 t i = char t (Char.unsafe_of_int (IR.Uint8.to_base_int i)) let[@inline always] uint16_be t i = uint16_be_trunc t (IR.Uint16.to_base_int i) let[@inline always] uint16_le t i = uint16_le_trunc t (IR.Uint16.to_base_int i) let[@inline always] uint32_be t i = int32_t_be t (IR.Uint32.to_base_int32_trunc i) let[@inline always] uint32_le t i = int32_t_le t (IR.Uint32.to_base_int32_trunc i) let[@inline always] uint64_be t i = int64_t_be t (IR.Uint64.to_base_int64_trunc i) let[@inline always] uint64_le t i = int64_t_le t (IR.Uint64.to_base_int64_trunc i) let[@inline always] int8 t i = char t (Char.unsafe_of_int (IR.Int8.to_base_int i)) let[@inline always] int16_be t i = int16_be_trunc t (IR.Int16.to_base_int i) let[@inline always] int16_le t i = int16_le_trunc t (IR.Int16.to_base_int i) let[@inline always] int32_be t i = int32_t_be t (IR.Int32.to_base_int32 i) let[@inline always] int32_le t i = int32_t_le t (IR.Int32.to_base_int32 i) let[@inline always] int64_be t i = int64_t_be t i let[@inline always] int64_le t i = int64_t_le t i end end module Peek = struct type 'seek src = 'seek Peek.src module To_bytes = struct include Peek.To_bytes let blit = unsafe_blit end module To_bigstring = struct include Peek.To_bigstring let blit = unsafe_blit end module To_string = Peek.To_string type ('a, 'd, 'w) t = ('a, 'd, 'w) Peek.t type ('a, 'd, 'w) t_local = ('a, 'd, 'w) Peek.t_local let upos = unsafe_buf_pos let tail_padded_fixed_string ~padding ~len t ~pos = Bigstring.get_tail_padded_fixed_string t.buf ~padding ~len ~pos:(upos t ~len ~pos) () ;; let head_padded_fixed_string ~padding ~len t ~pos = Bigstring.get_head_padded_fixed_string t.buf ~padding ~len ~pos:(upos t ~len ~pos) () ;; let bytes ~str_pos ~len t ~pos = let dst = Bytes.create (len + str_pos) in Bigstring.To_bytes.unsafe_blit ~src:t.buf ~src_pos:(upos t ~len ~pos) ~len ~dst ~dst_pos:str_pos; dst ;; let string ~str_pos ~len t ~pos = Bytes.unsafe_to_string ~no_mutation_while_string_reachable:(bytes ~str_pos ~len t ~pos) ;; let bigstring ~str_pos ~len t ~pos = let dst = Bigstring.create (len + str_pos) in Bigstring.unsafe_blit ~src:t.buf ~src_pos:(upos t ~len ~pos) ~len ~dst ~dst_pos:str_pos; dst ;; let byteso ?(str_pos = 0) ?len t ~pos = bytes t ~pos ~str_pos ~len: (match len with | None -> length t - pos | Some len -> len) ;; let stringo ?(str_pos = 0) ?len t ~pos = string t ~pos ~str_pos ~len: (match len with | None -> length t - pos | Some len -> len) ;; let bigstringo ?(str_pos = 0) ?len t ~pos = bigstring t ~pos ~str_pos ~len: (match len with | None -> length t - pos | Some len -> len) ;; let bin_prot = Peek.bin_prot let index_or_neg t ~pos ~len c = let pos = unsafe_buf_pos t ~pos ~len in let idx = Bigstring.unsafe_find ~pos ~len t.buf c in if idx < 0 then -1 else idx - t.lo ;; module Local = struct let tail_padded_fixed_string ~padding ~len t ~pos = (Bigstring.get_tail_padded_fixed_string_local t.buf ~padding ~len ~pos:(upos t ~len ~pos) ()) ;; let head_padded_fixed_string ~padding ~len t ~pos = (Bigstring.get_head_padded_fixed_string_local t.buf ~padding ~len ~pos:(upos t ~len ~pos) ()) ;; let bytes ~str_pos ~len t ~pos = (let dst = Bytes.create_local (len + str_pos) in Bigstring.To_bytes.unsafe_blit ~src:t.buf ~src_pos:(upos t ~len ~pos) ~len ~dst ~dst_pos:str_pos; dst) ;; let string ~str_pos ~len t ~pos = (Bytes.unsafe_to_string ~no_mutation_while_string_reachable:(bytes ~str_pos ~len t ~pos)) ;; let byteso ?(str_pos = 0) ?len t ~pos = (bytes t ~pos ~str_pos ~len: (match len with | None -> length t - pos | Some len -> len)) ;; let stringo ?(str_pos = 0) ?len t ~pos = (string t ~pos ~str_pos ~len: (match len with | None -> length t - pos | Some len -> len)) ;; open Bigstring let len = 8 let[@inline always] int64_t_be t ~pos = (Local.unsafe_get_int64_t_be t.buf ~pos:(upos t ~len ~pos) [@nontail]) ;; let[@inline always] int64_t_le t ~pos = (Local.unsafe_get_int64_t_le t.buf ~pos:(upos t ~len ~pos) [@nontail]) ;; end open Bigstring let len = 1 let[@inline always] char t ~pos = Bigstring.unsafe_get t.buf (upos t ~len ~pos) let[@inline always] uint8 t ~pos = unsafe_get_uint8 t.buf ~pos:(upos t ~len ~pos) let[@inline always] int8 t ~pos = unsafe_get_int8 t.buf ~pos:(upos t ~len ~pos) let len = 2 let[@inline always] int16_be t ~pos = unsafe_get_int16_be t.buf ~pos:(upos t ~len ~pos) ;; let[@inline always] int16_le t ~pos = unsafe_get_int16_le t.buf ~pos:(upos t ~len ~pos) ;; let[@inline always] uint16_be t ~pos = unsafe_get_uint16_be t.buf ~pos:(upos t ~len ~pos) ;; let[@inline always] uint16_le t ~pos = unsafe_get_uint16_le t.buf ~pos:(upos t ~len ~pos) ;; let len = 4 let[@inline always] int32_be t ~pos = unsafe_get_int32_be t.buf ~pos:(upos t ~len ~pos) ;; let[@inline always] int32_t_be t ~pos = unsafe_get_int32_t_be t.buf ~pos:(upos t ~len ~pos) ;; let[@inline always] int32_le t ~pos = unsafe_get_int32_le t.buf ~pos:(upos t ~len ~pos) ;; let[@inline always] int32_t_le t ~pos = unsafe_get_int32_t_le t.buf ~pos:(upos t ~len ~pos) ;; let[@inline always] uint32_be t ~pos = unsafe_get_uint32_be t.buf ~pos:(upos t ~len ~pos) ;; let[@inline always] uint32_le t ~pos = unsafe_get_uint32_le t.buf ~pos:(upos t ~len ~pos) ;; let len = 8 let[@inline always] int64_be_exn t ~pos = unsafe_get_int64_be_exn t.buf ~pos:(upos t ~len ~pos) ;; let[@inline always] int64_le_exn t ~pos = unsafe_get_int64_le_exn t.buf ~pos:(upos t ~len ~pos) ;; let[@inline always] uint64_be_exn t ~pos = unsafe_get_uint64_be_exn t.buf ~pos:(upos t ~len ~pos) ;; let[@inline always] uint64_le_exn t ~pos = unsafe_get_uint64_le_exn t.buf ~pos:(upos t ~len ~pos) ;; let[@inline always] int64_t_be t ~pos = unsafe_get_int64_t_be t.buf ~pos:(upos t ~len ~pos) ;; let[@inline always] int64_t_le t ~pos = unsafe_get_int64_t_le t.buf ~pos:(upos t ~len ~pos) ;; let[@inline always] int64_be_trunc t ~pos = unsafe_get_int64_be_trunc t.buf ~pos:(upos t ~len ~pos) ;; let[@inline always] int64_le_trunc t ~pos = unsafe_get_int64_le_trunc t.buf ~pos:(upos t ~len ~pos) ;; module Int_repr = struct let[@inline always] uint8 t ~pos = IR.Uint8.of_base_int_trunc (uint8 t ~pos) let[@inline always] uint16_be t ~pos = IR.Uint16.of_base_int_trunc (uint16_be t ~pos) ;; let[@inline always] uint16_le t ~pos = IR.Uint16.of_base_int_trunc (uint16_le t ~pos) ;; let[@inline always] uint32_be t ~pos = IR.Uint32.of_base_int32_trunc (int32_t_be t ~pos) ;; let[@inline always] uint32_le t ~pos = IR.Uint32.of_base_int32_trunc (int32_t_le t ~pos) ;; let[@inline always] uint64_be t ~pos = IR.Uint64.of_base_int64_trunc (int64_t_be t ~pos) ;; let[@inline always] uint64_le t ~pos = IR.Uint64.of_base_int64_trunc (int64_t_le t ~pos) ;; let[@inline always] int8 t ~pos = IR.Int8.of_base_int_trunc (int8 t ~pos) let[@inline always] int16_be t ~pos = IR.Int16.of_base_int_trunc (int16_be t ~pos) let[@inline always] int16_le t ~pos = IR.Int16.of_base_int_trunc (int16_le t ~pos) let[@inline always] int32_be t ~pos = IR.Int32.of_base_int32 (int32_t_be t ~pos) let[@inline always] int32_le t ~pos = IR.Int32.of_base_int32 (int32_t_le t ~pos) let[@inline always] int64_be t ~pos = int64_t_be t ~pos let[@inline always] int64_le t ~pos = int64_t_le t ~pos end end module Poke = struct type ('a, 'd, 'w) t = ('a, 'd, 'w) Poke.t type ('a, 'd, 'w) t_local = ('a, 'd, 'w) Poke.t_local let upos = unsafe_buf_pos let tail_padded_fixed_string ~padding ~len t ~pos src = Bigstring.set_tail_padded_fixed_string ~padding ~len t.buf ~pos:(upos t ~len ~pos) src ;; let head_padded_fixed_string ~padding ~len t ~pos src = Bigstring.set_head_padded_fixed_string ~padding ~len t.buf ~pos:(upos t ~len ~pos) src ;; let bytes ~str_pos ~len t ~pos src = let blit = if unsafe_is_safe then Bigstring.From_bytes.blit else Bigstring.From_bytes.unsafe_blit in blit ~src ~src_pos:str_pos ~len ~dst:t.buf ~dst_pos:(upos t ~len ~pos) ;; let string ~str_pos ~len t ~pos src = let blit = if unsafe_is_safe then Bigstring.From_string.blit else Bigstring.From_string.unsafe_blit in blit ~src ~src_pos:str_pos ~len ~dst:t.buf ~dst_pos:(upos t ~len ~pos) ;; let bigstring ~str_pos ~len t ~pos src = let blit = if unsafe_is_safe then Bigstring.blit else Bigstring.unsafe_blit in blit ~src ~src_pos:str_pos ~len ~dst:t.buf ~dst_pos:(upos t ~len ~pos) ;; let byteso ?(str_pos = 0) ?len t ~pos src = bytes t ~str_pos ~pos src ~len: (match len with | None -> Bytes.length src - str_pos | Some len -> len) ;; let stringo ?(str_pos = 0) ?len t ~pos src = string t ~str_pos ~pos src ~len: (match len with | None -> String.length src - str_pos | Some len -> len) ;; let bigstringo ?(str_pos = 0) ?len t ~pos src = bigstring t ~str_pos ~pos src ~len: (match len with | None -> Bigstring.length src - str_pos | Some len -> len) ;; let bin_prot = Poke.bin_prot let bin_prot_size = Poke.bin_prot_size open Bigstring let len = 1 let[@inline always] char t ~pos c = Bigstring.unsafe_set t.buf (upos t ~len ~pos) c let[@inline always] uint8_trunc t ~pos i = unsafe_set_uint8 t.buf ~pos:(upos t ~len ~pos) i ;; let[@inline always] int8_trunc t ~pos i = unsafe_set_int8 t.buf ~pos:(upos t ~len ~pos) i ;; let len = 2 let[@inline always] int16_be_trunc t ~pos i = unsafe_set_int16_be t.buf ~pos:(upos t ~len ~pos) i ;; let[@inline always] int16_le_trunc t ~pos i = unsafe_set_int16_le t.buf ~pos:(upos t ~len ~pos) i ;; let[@inline always] uint16_be_trunc t ~pos i = unsafe_set_uint16_be t.buf ~pos:(upos t ~len ~pos) i ;; let[@inline always] uint16_le_trunc t ~pos i = unsafe_set_uint16_le t.buf ~pos:(upos t ~len ~pos) i ;; let len = 4 let[@inline always] int32_be_trunc t ~pos i = unsafe_set_int32_be t.buf ~pos:(upos t ~len ~pos) i ;; let[@inline always] int32_t_be t ~pos i = unsafe_set_int32_t_be t.buf ~pos:(upos t ~len ~pos) i ;; let[@inline always] int32_le_trunc t ~pos i = unsafe_set_int32_le t.buf ~pos:(upos t ~len ~pos) i ;; let[@inline always] int32_t_le t ~pos i = unsafe_set_int32_t_le t.buf ~pos:(upos t ~len ~pos) i ;; let[@inline always] uint32_be_trunc t ~pos i = unsafe_set_uint32_be t.buf ~pos:(upos t ~len ~pos) i ;; let[@inline always] uint32_le_trunc t ~pos i = unsafe_set_uint32_le t.buf ~pos:(upos t ~len ~pos) i ;; let len = 8 let[@inline always] int64_be t ~pos i = unsafe_set_int64_be t.buf ~pos:(upos t ~len ~pos) i ;; let[@inline always] int64_le t ~pos i = unsafe_set_int64_le t.buf ~pos:(upos t ~len ~pos) i ;; let[@inline always] uint64_be_trunc t ~pos i = unsafe_set_uint64_be t.buf ~pos:(upos t ~len ~pos) i ;; let[@inline always] uint64_le_trunc t ~pos i = unsafe_set_uint64_le t.buf ~pos:(upos t ~len ~pos) i ;; let[@inline always] int64_t_be t ~pos i = unsafe_set_int64_t_be t.buf ~pos:(upos t ~len ~pos) i ;; let[@inline always] int64_t_le t ~pos i = unsafe_set_int64_t_le t.buf ~pos:(upos t ~len ~pos) i ;; let decimal = Itoa.unsafe_poke_decimal module Int_repr = struct let[@inline always] uint8 t ~pos i = uint8_trunc t ~pos (IR.Uint8.to_base_int i) let[@inline always] uint16_be t ~pos i = int16_be_trunc t ~pos (IR.Uint16.to_base_int i) ;; let[@inline always] uint16_le t ~pos i = int16_le_trunc t ~pos (IR.Uint16.to_base_int i) ;; let[@inline always] uint32_be t ~pos i = int32_t_be t ~pos (IR.Uint32.to_base_int32_trunc i) ;; let[@inline always] uint32_le t ~pos i = int32_t_le t ~pos (IR.Uint32.to_base_int32_trunc i) ;; let[@inline always] uint64_be t ~pos i = int64_t_be t ~pos (IR.Uint64.to_base_int64_trunc i) ;; let[@inline always] uint64_le t ~pos i = int64_t_le t ~pos (IR.Uint64.to_base_int64_trunc i) ;; let[@inline always] int8 t ~pos i = int8_trunc t ~pos (IR.Int8.to_base_int i) let[@inline always] int16_be t ~pos i = int16_be_trunc t ~pos (IR.Int16.to_base_int i) ;; let[@inline always] int16_le t ~pos i = int16_le_trunc t ~pos (IR.Int16.to_base_int i) ;; let[@inline always] int32_be t ~pos i = int32_t_be t ~pos (IR.Int32.to_base_int32 i) let[@inline always] int32_le t ~pos i = int32_t_le t ~pos (IR.Int32.to_base_int32 i) let[@inline always] int64_be t ~pos i = int64_t_be t ~pos i let[@inline always] int64_le t ~pos i = int64_t_le t ~pos i end end end module For_hexdump = struct module T2 = struct type nonrec ('rw, 'seek) t = ('rw, 'seek) t end module Window_indexable = struct include T2 let length t = length t let get t pos = Peek.char t ~pos end module Limits_indexable = struct include T2 let length t = t.hi_max - t.lo_min let get t pos = Bigstring.get t.buf (t.lo_min + pos) end module Buffer_indexable = struct include T2 let length t = Bigstring.length t.buf let get t pos = Bigstring.get t.buf pos end module Window = Hexdump.Of_indexable2 (Window_indexable) module Limits = Hexdump.Of_indexable2 (Limits_indexable) module Buffer = Hexdump.Of_indexable2 (Buffer_indexable) module type Relative_indexable = sig val name : string val lo : (_, _) t -> int val hi : (_, _) t -> int end module type Compound_indexable = sig include Hexdump.S2 with type ('rw, 'seek) t := ('rw, 'seek) t val parts : (module Relative_indexable) list end module Make_compound_hexdump (Compound : Compound_indexable) = struct module Hexdump = struct include T2 let relative_sequence ?max_lines t (module Relative : Relative_indexable) = let lo = Relative.lo t in let hi = Relative.hi t in Compound.Hexdump.to_sequence ?max_lines ~pos:lo ~len:(hi - lo) t ;; let to_sequence ?max_lines t = List.concat_map Compound.parts ~f:(fun (module Relative) -> [ Sequence.singleton (String.capitalize Relative.name) ; relative_sequence ?max_lines t (module Relative) |> Sequence.map ~f:(fun line -> " " ^ line) ]) |> Sequence.of_list |> Sequence.concat ;; let to_string_hum ?max_lines t = let t = globalize () () t in to_sequence ?max_lines t |> Sequence.to_list |> String.concat ~sep:"\n" ;; let sexp_of_t _ _ t = List.map Compound.parts ~f:(fun (module Relative) -> Relative.name, Sequence.to_list (relative_sequence t (module Relative))) |> [%sexp_of: (string * string list) list] ;; end end module Window_within_limits = struct let name = "window" let lo t = t.lo - t.lo_min let hi t = t.hi - t.lo_min end module Limits_within_limits = struct let name = "limits" let lo _ = 0 let hi t = t.hi_max - t.lo_min end module Window_within_buffer = struct let name = "window" let lo t = t.lo let hi t = t.hi end module Limits_within_buffer = struct let name = "limits" let lo t = t.lo_min let hi t = t.hi_max end module Buffer_within_buffer = struct let name = "buffer" let lo _ = 0 let hi t = Bigstring.length t.buf end module Window_and_limits = Make_compound_hexdump (struct include Limits let parts = [ (module Window_within_limits : Relative_indexable) ; (module Limits_within_limits : Relative_indexable) ] ;; end) module Window_and_limits_and_buffer = Make_compound_hexdump (struct include Buffer let parts = [ (module Window_within_buffer : Relative_indexable) ; (module Limits_within_buffer : Relative_indexable) ; (module Buffer_within_buffer : Relative_indexable) ] ;; end) end module Window = For_hexdump.Window module Limits = For_hexdump.Limits module Debug = For_hexdump.Window_and_limits_and_buffer include For_hexdump.Window_and_limits let to_string_hum = Hexdump.to_string_hum let memcmp a b = let len = length a in let c = Int.compare len (length b) in if c <> 0 then c else Bigstring.memcmp ~pos1:a.lo a.buf ~pos2:b.lo b.buf ~len ;; let memset t ~pos ~len c = Bigstring.memset ~pos:(buf_pos_exn t ~pos ~len) ~len t.buf c let zero t = memset t ~pos:0 ~len:(length t) '\000' let concat bufs = let total_length = ref 0 in let n = Array.length bufs in for i = 0 to n - 1 do (* This can overflow in 32 bit and javascript, so safe blits below. *) total_length := !total_length + length (Array.unsafe_get bufs i) done; let t = create ~len:!total_length in let pos = ref 0 in for i = 0 to n - 1 do let src = Array.unsafe_get bufs i in let len = length src in Blit.blit ~src ~dst:t ~src_pos:0 ~dst_pos:!pos ~len; pos := !pos + len done; t ;; let contains t ~substring = Bigstring.unsafe_memmem ~haystack:(buf t) ~haystack_pos:t.lo ~haystack_len:(length t) ~needle:substring ~needle_pos:0 ~needle_len:(Bigstring.length substring) >= 0 ;;
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>