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 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764
|
/** @file
* IPRT - String Manipulation.
*/
/*
* Copyright (C) 2006-2025 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
* in the VirtualBox distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
*/
#ifndef IPRT_INCLUDED_string_h
#define IPRT_INCLUDED_string_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif
#include <iprt/cdefs.h>
#include <iprt/types.h>
#include <iprt/assert.h>
#include <iprt/stdarg.h>
#include <iprt/errcore.h> /* for VINF_SUCCESS */
#if defined(RT_OS_LINUX) && defined(__KERNEL__)
/* no C++ hacks ('new' etc) here anymore! */
# include <linux/string.h>
# include <iprt/linux/version.h>
#elif defined(IN_XF86_MODULE) && !defined(NO_ANSIC)
RT_C_DECLS_BEGIN
# include "xf86_ansic.h"
RT_C_DECLS_END
#elif defined(RT_OS_FREEBSD) && defined(_KERNEL)
RT_C_DECLS_BEGIN
# include <sys/libkern.h>
RT_C_DECLS_END
#elif defined(RT_OS_NETBSD) && defined(_KERNEL)
RT_C_DECLS_BEGIN
# include <lib/libkern/libkern.h>
RT_C_DECLS_END
#elif defined(RT_OS_SOLARIS) && defined(_KERNEL)
/*
* Same case as with FreeBSD kernel:
* The string.h stuff clashes with sys/system.h
* ffs = find first set bit.
*/
# define ffs ffs_string_h
# define fls fls_string_h
# include <string.h>
# undef fls
# undef ffs
# undef strpbrk
#else
# include <string.h>
#endif
/*
* Supply prototypes for standard string functions provided by
* IPRT instead of the operating environment.
*/
#if defined(RT_OS_DARWIN) && defined(KERNEL)
RT_C_DECLS_BEGIN
void *memchr(const void *pv, int ch, size_t cb);
char *strpbrk(const char *pszStr, const char *pszChars);
RT_C_DECLS_END
#endif
#if defined(RT_OS_FREEBSD) && defined(_KERNEL)
RT_C_DECLS_BEGIN
char *strpbrk(const char *pszStr, const char *pszChars);
RT_C_DECLS_END
#endif
#if defined(RT_OS_NETBSD) && defined(_KERNEL)
RT_C_DECLS_BEGIN
char *strpbrk(const char *pszStr, const char *pszChars);
RT_C_DECLS_END
#endif
#if !defined(IPRT_NO_CRT) \
&& ( defined(RT_OS_DARWIN) \
|| (defined(RT_OS_OS2) && (!defined(_GNU_SOURCE) || !defined(__GNUC__))) \
|| defined(RT_OS_SOLARIS) \
|| defined(RT_OS_WINDOWS))
RT_C_DECLS_BEGIN
# if !defined(RT_OS_DARWIN) || RT_CLANG_PREREQ(7 /* whatever post gcc-4.2 */, 0)
RTDECL(void *) mempcpy(void *pvDst, const void *pvSrc, size_t cb);
# else
void *mempcpy(void *pvDst, const void *pvSrc, size_t cb);
# endif
RT_C_DECLS_END
#endif
#if (!defined(RT_OS_LINUX) || !defined(_GNU_SOURCE)) \
&& (!defined(RT_OS_OS2) || !defined(_GNU_SOURCE)) \
&& !defined(RT_OS_FREEBSD) \
&& !defined(RT_OS_NETBSD)
RT_C_DECLS_BEGIN
void *memrchr(const void *pv, int ch, size_t cb);
RT_C_DECLS_END
#endif
/** @def RT_USE_RTC_3629
* When defined the UTF-8 range will stop at 0x10ffff. If not defined, the
* range stops at 0x7fffffff.
* @remarks Must be defined both when building and using the IPRT. */
#ifdef DOXYGEN_RUNNING
# define RT_USE_RTC_3629
#endif
/** @defgroup grp_rt_str RTStr - String Manipulation
* Mostly UTF-8 related helpers where the standard string functions won't do.
* @ingroup grp_rt
* @{
*/
RT_C_DECLS_BEGIN
/**
* The maximum string length.
*/
#define RTSTR_MAX (~(size_t)0)
/** @def RTSTR_TAG
* The default allocation tag used by the RTStr allocation APIs.
*
* When not defined before the inclusion of iprt/string.h, this will default to
* the pointer to the current file name. The string API will make of use of
* this as pointer to a volatile but read-only string.
*/
#if !defined(RTSTR_TAG) || defined(DOXYGEN_RUNNING)
# define RTSTR_TAG (__FILE__)
#endif
/**
* Byte zero the specified object.
*
* This will use sizeof(Obj) to figure the size and will call memset, bzero
* or some compiler intrinsic to perform the actual zeroing.
*
* @param Obj The object to zero. Make sure to dereference pointers.
*
* @remarks Because the macro may use memset it has been placed in string.h
* instead of cdefs.h to avoid build issues because someone forgot
* to include this header.
*
* @ingroup grp_rt_cdefs
*/
#define RT_ZERO(Obj) RT_BZERO(&(Obj), sizeof(Obj))
/**
* Byte zero the specified memory area.
*
* This will call memset, bzero or some compiler intrinsic to clear the
* specified bytes of memory.
*
* @param pv Pointer to the memory.
* @param cb The number of bytes to clear. Please, don't pass 0.
*
* @remarks Because the macro may use memset it has been placed in string.h
* instead of cdefs.h to avoid build issues because someone forgot
* to include this header.
*
* @ingroup grp_rt_cdefs
*/
#define RT_BZERO(pv, cb) do { memset((pv), 0, cb); } while (0)
/**
* For copying a volatile variable to a non-volatile one.
* @param a_Dst The non-volatile destination variable.
* @param a_VolatileSrc The volatile source variable / dereferenced pointer.
*/
#define RT_COPY_VOLATILE(a_Dst, a_VolatileSrc) \
do { \
void const volatile *a_pvVolatileSrc_BCopy_Volatile = &(a_VolatileSrc); \
AssertCompile(sizeof(a_Dst) == sizeof(a_VolatileSrc)); \
memcpy(&(a_Dst), (void const *)a_pvVolatileSrc_BCopy_Volatile, sizeof(a_Dst)); \
} while (0)
/**
* For copy a number of bytes from a volatile buffer to a non-volatile one.
*
* @param a_pDst Pointer to the destination buffer.
* @param a_pVolatileSrc Pointer to the volatile source buffer.
* @param a_cbToCopy Number of bytes to copy.
*/
#define RT_BCOPY_VOLATILE(a_pDst, a_pVolatileSrc, a_cbToCopy) \
do { \
void const volatile *a_pvVolatileSrc_BCopy_Volatile = (a_pVolatileSrc); \
memcpy((a_pDst), (void const *)a_pvVolatileSrc_BCopy_Volatile, (a_cbToCopy)); \
} while (0)
/** @def RT_BCOPY_UNFORTIFIED
* For copying a number of bytes from/to variable length structures.
*
* This is for working around false positives ("field-spanning writes") in the
* linux kernel's fortified memcpy (v5.18+) when copying from/to
* RT_FLEXIBLE_ARRAY fields and similar tricks going beyond the strict
* definition of a target or source structure.
*
* @param a_pDst Pointer to the destination buffer.
* @param a_pSrc Pointer to the source buffer.
* @param a_cbToCopy Number of bytes to copy.
* @see @bugref{10209}, @ticketref{21410}
*/
#if defined(RT_OS_LINUX) && defined(__KERNEL__)
# if (RTLNX_VER_MIN(5,18,0) || RTLNX_RHEL_RANGE(9,3, 9,99)) \
&& !defined(__NO_FORTIFY) \
&& defined(__OPTIMIZE__) \
&& defined(CONFIG_FORTIFY_SOURCE)
# define RT_BCOPY_UNFORTIFIED(a_pDst, a_pSrc, a_cbToCopy) __underlying_memcpy((a_pDst), (a_pSrc), (a_cbToCopy))
# else
# define RT_BCOPY_UNFORTIFIED(a_pDst, a_pSrc, a_cbToCopy) memcpy((a_pDst), (a_pSrc), (a_cbToCopy))
# endif
#else /* !RT_OS_LINUX && !__KERNEL__ */
# define RT_BCOPY_UNFORTIFIED(a_pDst, a_pSrc, a_cbToCopy) memcpy((a_pDst), (a_pSrc), (a_cbToCopy))
#endif /* !RT_OS_LINUX && !__KERNEL__ */
#ifdef IN_RING3
/**
* Allocates tmp buffer with default tag, translates pszString from UTF8 to
* current codepage.
*
* @returns iprt status code.
* @param ppszString Receives pointer of allocated native CP string.
* The returned pointer must be freed using RTStrFree().
* @param pszString UTF-8 string to convert.
*/
#define RTStrUtf8ToCurrentCP(ppszString, pszString) RTStrUtf8ToCurrentCPTag((ppszString), (pszString), RTSTR_TAG)
/**
* Allocates tmp buffer with custom tag, translates pszString from UTF-8 to
* current codepage.
*
* @returns iprt status code.
* @param ppszString Receives pointer of allocated native CP string.
* The returned pointer must be freed using
* RTStrFree()., const char *pszTag
* @param pszString UTF-8 string to convert.
* @param pszTag Allocation tag used for statistics and such.
*/
RTR3DECL(int) RTStrUtf8ToCurrentCPTag(char **ppszString, const char *pszString, const char *pszTag);
/**
* Allocates tmp buffer with default tag, translates pszString from UTF-8 to
* current codepage, extended version.
*
* @returns iprt status code.
* @param ppszString Receives pointer of allocated native CP string.
* The returned pointer must be freed using RTStrFree().
* @param pszString UTF-8 string to convert.
* @param cchString The maximum size in chars (the type) to convert. The conversion stop
* when it reaches cchString or the string terminator ('\\0').
* Use RTSTR_MAX to translate the entire string.
*/
#define RTStrUtf8ToCurrentCPEx(ppszString, pszString, cchString) \
RTStrUtf8ToCurrentCPExTag((ppszString), (pszString), (cchString), RTSTR_TAG)
/**
* Allocates tmp buffer with custom tag, translates pszString from UTF8 to
* current codepage.
*
* @returns iprt status code.
* @param ppszString Receives pointer of allocated native CP string.
* The returned pointer must be freed using
* RTStrFree()., const char *pszTag
* @param pszString UTF-8 string to convert.
* @param cchString The maximum size in chars (the type) to convert. The conversion stop
* when it reaches cchString or the string terminator ('\\0').
* Use RTSTR_MAX to translate the entire string.
* @param pszTag Allocation tag used for statistics and such.
*/
RTR3DECL(int) RTStrUtf8ToCurrentCPExTag(char **ppszString, const char *pszString, size_t cchString, const char *pszTag);
/**
* Allocates tmp buffer, translates pszString from current codepage to UTF-8.
*
* @returns iprt status code.
* @param ppszString Receives pointer of allocated UTF-8 string.
* The returned pointer must be freed using RTStrFree().
* @param pszString Native string to convert.
*/
#define RTStrCurrentCPToUtf8(ppszString, pszString) RTStrCurrentCPToUtf8Tag((ppszString), (pszString), RTSTR_TAG)
/**
* Allocates tmp buffer, translates pszString from current codepage to UTF-8.
*
* @returns iprt status code.
* @param ppszString Receives pointer of allocated UTF-8 string.
* The returned pointer must be freed using RTStrFree().
* @param pszString Native string to convert.
* @param pszTag Allocation tag used for statistics and such.
*/
RTR3DECL(int) RTStrCurrentCPToUtf8Tag(char **ppszString, const char *pszString, const char *pszTag);
/**
* Allocates tmp buffer, translates pszString from console codepage to UTF-8.
*
* @returns iprt status code.
* @param ppszString Receives pointer of allocated UTF-8 string.
* The returned pointer must be freed using RTStrFree().
* @param pszString Native string to convert.
*/
#define RTStrConsoleCPToUtf8(ppszString, pszString) RTStrConsoleCPToUtf8Tag((ppszString), (pszString), RTSTR_TAG)
/**
* Allocates tmp buffer, translates pszString from console codepage to UTF-8.
*
* @returns iprt status code.
* @param ppszString Receives pointer of allocated UTF-8 string.
* The returned pointer must be freed using RTStrFree().
* @param pszString Native string to convert.
* @param pszTag Allocation tag used for statistics and such.
*/
RTR3DECL(int) RTStrConsoleCPToUtf8Tag(char **ppszString, const char *pszString, const char *pszTag);
#endif /* IN_RING3 */
/**
* Free string allocated by any of the non-UCS-2 string functions.
*
* @param pszString Pointer to buffer with string to free.
* NULL is accepted.
*/
RTDECL(void) RTStrFree(char *pszString);
/**
* Allocates a new copy of the given UTF-8 string (default tag).
*
* @returns Pointer to the allocated UTF-8 string.
* @param pszString UTF-8 string to duplicate.
*/
#define RTStrDup(pszString) RTStrDupTag((pszString), RTSTR_TAG)
/**
* Allocates a new copy of the given UTF-8 string (custom tag).
*
* @returns Pointer to the allocated UTF-8 string.
* @param pszString UTF-8 string to duplicate.
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(char *) RTStrDupTag(const char *pszString, const char *pszTag);
/**
* Allocates a new copy of the given UTF-8 string (default tag).
*
* @returns iprt status code.
* @param ppszCopy Receives pointer of the allocated UTF-8 string.
* The returned pointer must be freed using RTStrFree().
* @param pszString UTF-8 string to duplicate.
*/
#define RTStrDupEx(ppszCopy, pszString) RTStrDupExTag((ppszCopy), (pszString), RTSTR_TAG)
/**
* Allocates a new copy of the given UTF-8 string (custom tag).
*
* @returns iprt status code.
* @param ppszCopy Receives pointer of the allocated UTF-8 string.
* The returned pointer must be freed using RTStrFree().
* @param pszString UTF-8 string to duplicate.
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(int) RTStrDupExTag(char **ppszCopy, const char *pszString, const char *pszTag);
/**
* Allocates a new copy of the given UTF-8 substring (default tag).
*
* @returns Pointer to the allocated UTF-8 substring.
* @param pszString UTF-8 string to duplicate.
* @param cchMax The max number of chars to duplicate, not counting
* the terminator.
*/
#define RTStrDupN(pszString, cchMax) RTStrDupNTag((pszString), (cchMax), RTSTR_TAG)
/**
* Allocates a new copy of the given UTF-8 substring (custom tag).
*
* @returns Pointer to the allocated UTF-8 substring.
* @param pszString UTF-8 string to duplicate.
* @param cchMax The max number of chars to duplicate, not counting
* the terminator.
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(char *) RTStrDupNTag(const char *pszString, size_t cchMax, const char *pszTag);
/**
* Allocates a new copy of the given UTF-8 substring (default tag).
*
* @returns iprt status code (VINF_SUCCESS or VERR_NO_STR_MEMORY).
* @param ppszCopy Receives pointer of the allocated UTF-8 substring.
* The returned pointer must be freed using RTStrFree().
* @param pszString UTF-8 string to duplicate.
* @param cchMax The max number of chars to duplicate, not counting
* the terminator.
*/
#define RTStrDupNEx(ppszCopy, pszString, cchMax) RTStrDupNExTag((ppszCopy), (pszString), (cchMax), RTSTR_TAG)
/**
* Allocates a new copy of the given UTF-8 substring (custom tag).
*
* @returns iprt status code (VINF_SUCCESS or VERR_NO_STR_MEMORY).
* @param ppszCopy Receives pointer of the allocated UTF-8 substring.
* The returned pointer must be freed using RTStrFree().
* @param pszString UTF-8 string to duplicate.
* @param cchMax The max number of chars to duplicate, not counting
* the terminator.
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(int) RTStrDupNExTag(char **ppszCopy, const char *pszString, size_t cchMax, const char *pszTag);
/**
* Appends a string onto an existing IPRT allocated string (default tag).
*
* @retval VINF_SUCCESS
* @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
* remains unchanged.
*
* @param ppsz Pointer to the string pointer. The string
* pointer must either be NULL or point to a string
* returned by an IPRT string API. (In/Out)
* @param pszAppend The string to append. NULL and empty strings
* are quietly ignored.
*/
#define RTStrAAppend(ppsz, pszAppend) RTStrAAppendTag((ppsz), (pszAppend), RTSTR_TAG)
/**
* Appends a string onto an existing IPRT allocated string (custom tag).
*
* @retval VINF_SUCCESS
* @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
* remains unchanged.
*
* @param ppsz Pointer to the string pointer. The string
* pointer must either be NULL or point to a string
* returned by an IPRT string API. (In/Out)
* @param pszAppend The string to append. NULL and empty strings
* are quietly ignored.
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(int) RTStrAAppendTag(char **ppsz, const char *pszAppend, const char *pszTag);
/**
* Appends N bytes from a strings onto an existing IPRT allocated string
* (default tag).
*
* @retval VINF_SUCCESS
* @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
* remains unchanged.
*
* @param ppsz Pointer to the string pointer. The string
* pointer must either be NULL or point to a string
* returned by an IPRT string API. (In/Out)
* @param pszAppend The string to append. Can be NULL if cchAppend
* is NULL.
* @param cchAppend The number of chars (not code points) to append
* from pszAppend. Must not be more than
* @a pszAppend contains, except for the special
* value RTSTR_MAX that can be used to indicate all
* of @a pszAppend without having to strlen it.
*/
#define RTStrAAppendN(ppsz, pszAppend, cchAppend) RTStrAAppendNTag((ppsz), (pszAppend), (cchAppend), RTSTR_TAG)
/**
* Appends N bytes from a strings onto an existing IPRT allocated string (custom
* tag).
*
* @retval VINF_SUCCESS
* @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
* remains unchanged.
*
* @param ppsz Pointer to the string pointer. The string
* pointer must either be NULL or point to a string
* returned by an IPRT string API. (In/Out)
* @param pszAppend The string to append. Can be NULL if cchAppend
* is NULL.
* @param cchAppend The number of chars (not code points) to append
* from pszAppend. Must not be more than
* @a pszAppend contains, except for the special
* value RTSTR_MAX that can be used to indicate all
* of @a pszAppend without having to strlen it.
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(int) RTStrAAppendNTag(char **ppsz, const char *pszAppend, size_t cchAppend, const char *pszTag);
/**
* Appends one or more strings onto an existing IPRT allocated string.
*
* This is a very flexible and efficient alternative to using RTStrAPrintf to
* combine several strings together.
*
* @retval VINF_SUCCESS
* @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
* remains unchanged.
*
* @param ppsz Pointer to the string pointer. The string
* pointer must either be NULL or point to a string
* returned by an IPRT string API. (In/Out)
* @param cPairs The number of string / length pairs in the
* @a va.
* @param va List of string (const char *) and length
* (size_t) pairs. The strings will be appended to
* the string in the first argument.
*/
#define RTStrAAppendExNV(ppsz, cPairs, va) RTStrAAppendExNVTag((ppsz), (cPairs), (va), RTSTR_TAG)
/**
* Appends one or more strings onto an existing IPRT allocated string.
*
* This is a very flexible and efficient alternative to using RTStrAPrintf to
* combine several strings together.
*
* @retval VINF_SUCCESS
* @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
* remains unchanged.
*
* @param ppsz Pointer to the string pointer. The string
* pointer must either be NULL or point to a string
* returned by an IPRT string API. (In/Out)
* @param cPairs The number of string / length pairs in the
* @a va.
* @param va List of string (const char *) and length
* (size_t) pairs. The strings will be appended to
* the string in the first argument.
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(int) RTStrAAppendExNVTag(char **ppsz, size_t cPairs, va_list va, const char *pszTag);
/**
* Appends one or more strings onto an existing IPRT allocated string
* (untagged).
*
* This is a very flexible and efficient alternative to using RTStrAPrintf to
* combine several strings together.
*
* @retval VINF_SUCCESS
* @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
* remains unchanged.
*
* @param ppsz Pointer to the string pointer. The string
* pointer must either be NULL or point to a string
* returned by an IPRT string API. (In/Out)
* @param cPairs The number of string / length pairs in the
* ellipsis.
* @param ... List of string (const char *) and length
* (size_t) pairs. The strings will be appended to
* the string in the first argument.
*/
DECLINLINE(int) RTStrAAppendExN(char **ppsz, size_t cPairs, ...)
{
int rc;
va_list va;
va_start(va, cPairs);
rc = RTStrAAppendExNVTag(ppsz, cPairs, va, RTSTR_TAG);
va_end(va);
return rc;
}
/**
* Appends one or more strings onto an existing IPRT allocated string (custom
* tag).
*
* This is a very flexible and efficient alternative to using RTStrAPrintf to
* combine several strings together.
*
* @retval VINF_SUCCESS
* @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
* remains unchanged.
*
* @param ppsz Pointer to the string pointer. The string
* pointer must either be NULL or point to a string
* returned by an IPRT string API. (In/Out)
* @param pszTag Allocation tag used for statistics and such.
* @param cPairs The number of string / length pairs in the
* ellipsis.
* @param ... List of string (const char *) and length
* (size_t) pairs. The strings will be appended to
* the string in the first argument.
*/
DECLINLINE(int) RTStrAAppendExNTag(char **ppsz, const char *pszTag, size_t cPairs, ...)
{
int rc;
va_list va;
va_start(va, cPairs);
rc = RTStrAAppendExNVTag(ppsz, cPairs, va, pszTag);
va_end(va);
return rc;
}
/**
* Truncates an IPRT allocated string (default tag).
*
* @retval VINF_SUCCESS.
* @retval VERR_OUT_OF_RANGE if cchNew is too long. Nothing is done.
*
* @param ppsz Pointer to the string pointer. The string
* pointer can be NULL if @a cchNew is 0, no change
* is made then. If we actually reallocate the
* string, the string pointer might be changed by
* this call. (In/Out)
* @param cchNew The new string length (excluding the
* terminator). The string must be at least this
* long or we'll return VERR_OUT_OF_RANGE and
* assert on you.
*/
#define RTStrATruncate(ppsz, cchNew) RTStrATruncateTag((ppsz), (cchNew), RTSTR_TAG)
/**
* Truncates an IPRT allocated string.
*
* @retval VINF_SUCCESS.
* @retval VERR_OUT_OF_RANGE if cchNew is too long. Nothing is done.
*
* @param ppsz Pointer to the string pointer. The string
* pointer can be NULL if @a cchNew is 0, no change
* is made then. If we actually reallocate the
* string, the string pointer might be changed by
* this call. (In/Out)
* @param cchNew The new string length (excluding the
* terminator). The string must be at least this
* long or we'll return VERR_OUT_OF_RANGE and
* assert on you.
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(int) RTStrATruncateTag(char **ppsz, size_t cchNew, const char *pszTag);
/**
* Allocates memory for string storage (default tag).
*
* You should normally not use this function, except if there is some very
* custom string handling you need doing that isn't covered by any of the other
* APIs.
*
* @returns Pointer to the allocated string. The first byte is always set
* to the string terminator char, the contents of the remainder of the
* memory is undefined. The string must be freed by calling RTStrFree.
*
* NULL is returned if the allocation failed. Please translate this to
* VERR_NO_STR_MEMORY and not VERR_NO_MEMORY. Also consider
* RTStrAllocEx if an IPRT status code is required.
*
* @param cb How many bytes to allocate. If this is zero, we
* will allocate a terminator byte anyway.
*/
#define RTStrAlloc(cb) RTStrAllocTag((cb), RTSTR_TAG)
/**
* Allocates memory for string storage (custom tag).
*
* You should normally not use this function, except if there is some very
* custom string handling you need doing that isn't covered by any of the other
* APIs.
*
* @returns Pointer to the allocated string. The first byte is always set
* to the string terminator char, the contents of the remainder of the
* memory is undefined. The string must be freed by calling RTStrFree.
*
* NULL is returned if the allocation failed. Please translate this to
* VERR_NO_STR_MEMORY and not VERR_NO_MEMORY. Also consider
* RTStrAllocEx if an IPRT status code is required.
*
* @param cb How many bytes to allocate. If this is zero, we
* will allocate a terminator byte anyway.
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(char *) RTStrAllocTag(size_t cb, const char *pszTag);
/**
* Allocates memory for string storage, with status code (default tag).
*
* You should normally not use this function, except if there is some very
* custom string handling you need doing that isn't covered by any of the other
* APIs.
*
* @retval VINF_SUCCESS
* @retval VERR_NO_STR_MEMORY
*
* @param ppsz Where to return the allocated string. This will
* be set to NULL on failure. On success, the
* returned memory will always start with a
* terminator char so that it is considered a valid
* C string, the contents of rest of the memory is
* undefined.
* @param cb How many bytes to allocate. If this is zero, we
* will allocate a terminator byte anyway.
*/
#define RTStrAllocEx(ppsz, cb) RTStrAllocExTag((ppsz), (cb), RTSTR_TAG)
/**
* Allocates memory for string storage, with status code (custom tag).
*
* You should normally not use this function, except if there is some very
* custom string handling you need doing that isn't covered by any of the other
* APIs.
*
* @retval VINF_SUCCESS
* @retval VERR_NO_STR_MEMORY
*
* @param ppsz Where to return the allocated string. This will
* be set to NULL on failure. On success, the
* returned memory will always start with a
* terminator char so that it is considered a valid
* C string, the contents of rest of the memory is
* undefined.
* @param cb How many bytes to allocate. If this is zero, we
* will allocate a terminator byte anyway.
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(int) RTStrAllocExTag(char **ppsz, size_t cb, const char *pszTag);
/**
* Reallocates the specified string (default tag).
*
* You should normally not have use this function, except perhaps to truncate a
* really long string you've got from some IPRT string API, but then you should
* use RTStrATruncate.
*
* @returns VINF_SUCCESS.
* @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
* remains unchanged.
*
* @param ppsz Pointer to the string variable containing the
* input and output string.
*
* When not freeing the string, the result will
* always have the last byte set to the terminator
* character so that when used for string
* truncation the result will be a valid C string
* (your job to keep it a valid UTF-8 string).
*
* When the input string is NULL and we're supposed
* to reallocate, the returned string will also
* have the first byte set to the terminator char
* so it will be a valid C string.
*
* @param cbNew When @a cbNew is zero, we'll behave like
* RTStrFree and @a *ppsz will be set to NULL.
*
* When not zero, this will be the new size of the
* memory backing the string, i.e. it includes the
* terminator char.
*/
#define RTStrRealloc(ppsz, cbNew) RTStrReallocTag((ppsz), (cbNew), RTSTR_TAG)
/**
* Reallocates the specified string (custom tag).
*
* You should normally not have use this function, except perhaps to truncate a
* really long string you've got from some IPRT string API, but then you should
* use RTStrATruncate.
*
* @returns VINF_SUCCESS.
* @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
* remains unchanged.
*
* @param ppsz Pointer to the string variable containing the
* input and output string.
*
* When not freeing the string, the result will
* always have the last byte set to the terminator
* character so that when used for string
* truncation the result will be a valid C string
* (your job to keep it a valid UTF-8 string).
*
* When the input string is NULL and we're supposed
* to reallocate, the returned string will also
* have the first byte set to the terminator char
* so it will be a valid C string.
*
* @param cbNew When @a cbNew is zero, we'll behave like
* RTStrFree and @a *ppsz will be set to NULL.
*
* When not zero, this will be the new size of the
* memory backing the string, i.e. it includes the
* terminator char.
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(int) RTStrReallocTag(char **ppsz, size_t cbNew, const char *pszTag);
/**
* Validates the UTF-8 encoding of the string.
*
* @returns iprt status code.
* @param psz The string.
*/
RTDECL(int) RTStrValidateEncoding(const char *psz);
/** @name Flags for RTStrValidateEncodingEx and RTUtf16ValidateEncodingEx
* @{
*/
/** Check that the string is zero terminated within the given size.
* VERR_BUFFER_OVERFLOW will be returned if the check fails. */
#define RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED RT_BIT_32(0)
/** Check that the string is exactly the given length.
* If it terminates early, VERR_BUFFER_UNDERFLOW will be returned. When used
* together with RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED, the given length must
* include the terminator or VERR_BUFFER_OVERFLOW will be returned. */
#define RTSTR_VALIDATE_ENCODING_EXACT_LENGTH RT_BIT_32(1)
/** @} */
/**
* Validates the UTF-8 encoding of the string.
*
* @returns iprt status code.
* @param psz The string.
* @param cch The max string length (/ size). Use RTSTR_MAX to
* process the entire string.
* @param fFlags Combination of RTSTR_VALIDATE_ENCODING_XXX flags.
*/
RTDECL(int) RTStrValidateEncodingEx(const char *psz, size_t cch, uint32_t fFlags);
/**
* Checks if the UTF-8 encoding is valid.
*
* @returns true / false.
* @param psz The string.
*/
RTDECL(bool) RTStrIsValidEncoding(const char *psz);
/**
* Purge all bad UTF-8 encoding in the string, replacing it with '?'.
*
* @returns The number of bad characters (0 if nothing was done).
* @param psz The string to purge.
*/
RTDECL(size_t) RTStrPurgeEncoding(char *psz);
/**
* Sanitizes a (valid) UTF-8 string by replacing all characters outside a white
* list in-place by an ASCII replacedment character.
*
* Multi-byte characters will be replaced byte by byte.
*
* @returns The number of code points replaced. In the case of an incorrectly
* encoded string -1 will be returned, and the string is not completely
* processed. In the case of puszValidPairs having an odd number of
* code points, -1 will be also return but without any modification to
* the string.
* @param psz The string to sanitise.
* @param puszValidPairs A zero-terminated array of pairs of Unicode points.
* Each pair is the start and end point of a range,
* and the union of these ranges forms the white list.
* @param chReplacement The ASCII replacement character.
*/
RTDECL(ssize_t) RTStrPurgeComplementSet(char *psz, PCRTUNICP puszValidPairs, char chReplacement);
/**
* Gets the number of code points the string is made up of, excluding
* the terminator.
*
*
* @returns Number of code points (RTUNICP).
* @returns 0 if the string was incorrectly encoded.
* @param psz The string.
*/
RTDECL(size_t) RTStrUniLen(const char *psz);
/**
* Gets the number of code points the string is made up of, excluding
* the terminator.
*
* This function will validate the string, and incorrectly encoded UTF-8
* strings will be rejected.
*
* @returns iprt status code.
* @param psz The string.
* @param cch The max string length. Use RTSTR_MAX to process the entire string.
* @param pcuc Where to store the code point count.
* This is undefined on failure.
*/
RTDECL(int) RTStrUniLenEx(const char *psz, size_t cch, size_t *pcuc);
/**
* Translate a UTF-8 string into an unicode string (i.e. RTUNICPs), allocating the string buffer.
*
* @returns iprt status code.
* @param pszString UTF-8 string to convert.
* @param ppUniString Receives pointer to the allocated unicode string.
* The returned string must be freed using RTUniFree().
*/
RTDECL(int) RTStrToUni(const char *pszString, PRTUNICP *ppUniString);
/**
* Translates pszString from UTF-8 to an array of code points, allocating the result
* array if requested.
*
* @returns iprt status code.
* @param pszString UTF-8 string to convert.
* @param cchString The maximum size in chars (the type) to convert. The conversion stop
* when it reaches cchString or the string terminator ('\\0').
* Use RTSTR_MAX to translate the entire string.
* @param ppaCps If cCps is non-zero, this must either be pointing to pointer to
* a buffer of the specified size, or pointer to a NULL pointer.
* If *ppusz is NULL or cCps is zero a buffer of at least cCps items
* will be allocated to hold the translated string.
* If a buffer was requested it must be freed using RTUtf16Free().
* @param cCps The number of code points in the unicode string. This includes the terminator.
* @param pcCps Where to store the length of the translated string,
* excluding the terminator. (Optional)
*
* This may be set under some error conditions,
* however, only for VERR_BUFFER_OVERFLOW and
* VERR_NO_STR_MEMORY will it contain a valid string
* length that can be used to resize the buffer.
*/
RTDECL(int) RTStrToUniEx(const char *pszString, size_t cchString, PRTUNICP *ppaCps, size_t cCps, size_t *pcCps);
/**
* Calculates the length of the string in RTUTF16 items.
*
* This function will validate the string, and incorrectly encoded UTF-8
* strings will be rejected. The primary purpose of this function is to
* help allocate buffers for RTStrToUtf16Ex of the correct size. For most
* other purposes RTStrCalcUtf16LenEx() should be used.
*
* @returns Number of RTUTF16 items.
* @returns 0 if the string was incorrectly encoded.
* @param psz The string.
*/
RTDECL(size_t) RTStrCalcUtf16Len(const char *psz);
/**
* Calculates the length of the string in RTUTF16 items.
*
* This function will validate the string, and incorrectly encoded UTF-8
* strings will be rejected.
*
* @returns iprt status code.
* @param psz The string.
* @param cch The max string length. Use RTSTR_MAX to process the entire string.
* @param pcwc Where to store the string length. Optional.
* This is undefined on failure.
*/
RTDECL(int) RTStrCalcUtf16LenEx(const char *psz, size_t cch, size_t *pcwc);
/**
* Translate a UTF-8 string into a UTF-16 allocating the result buffer (default
* tag).
*
* @returns iprt status code.
* @param pszString UTF-8 string to convert.
* @param ppwszString Receives pointer to the allocated UTF-16 string.
* The returned string must be freed using RTUtf16Free().
*/
#define RTStrToUtf16(pszString, ppwszString) RTStrToUtf16Tag((pszString), (ppwszString), RTSTR_TAG)
/**
* Translate a UTF-8 string into a UTF-16 allocating the result buffer (custom
* tag).
*
* This differs from RTStrToUtf16 in that it always produces a
* big-endian string.
*
* @returns iprt status code.
* @param pszString UTF-8 string to convert.
* @param ppwszString Receives pointer to the allocated UTF-16 string.
* The returned string must be freed using RTUtf16Free().
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(int) RTStrToUtf16Tag(const char *pszString, PRTUTF16 *ppwszString, const char *pszTag);
/**
* Translate a UTF-8 string into a UTF-16BE allocating the result buffer
* (default tag).
*
* This differs from RTStrToUtf16Tag in that it always produces a
* big-endian string.
*
* @returns iprt status code.
* @param pszString UTF-8 string to convert.
* @param ppwszString Receives pointer to the allocated UTF-16BE string.
* The returned string must be freed using RTUtf16Free().
*/
#define RTStrToUtf16Big(pszString, ppwszString) RTStrToUtf16BigTag((pszString), (ppwszString), RTSTR_TAG)
/**
* Translate a UTF-8 string into a UTF-16BE allocating the result buffer (custom
* tag).
*
* @returns iprt status code.
* @param pszString UTF-8 string to convert.
* @param ppwszString Receives pointer to the allocated UTF-16BE string.
* The returned string must be freed using RTUtf16Free().
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(int) RTStrToUtf16BigTag(const char *pszString, PRTUTF16 *ppwszString, const char *pszTag);
/**
* Translates pszString from UTF-8 to UTF-16, allocating the result buffer if requested.
*
* @returns iprt status code.
* @param pszString UTF-8 string to convert.
* @param cchString The maximum size in chars (the type) to convert. The conversion stop
* when it reaches cchString or the string terminator ('\\0').
* Use RTSTR_MAX to translate the entire string.
* @param ppwsz If cwc is non-zero, this must either be pointing to pointer to
* a buffer of the specified size, or pointer to a NULL pointer.
* If *ppwsz is NULL or cwc is zero a buffer of at least cwc items
* will be allocated to hold the translated string.
* If a buffer was requested it must be freed using RTUtf16Free().
* @param cwc The buffer size in RTUTF16s. This includes the terminator.
* @param pcwc Where to store the length of the translated string,
* excluding the terminator. (Optional)
*
* This may be set under some error conditions,
* however, only for VERR_BUFFER_OVERFLOW and
* VERR_NO_STR_MEMORY will it contain a valid string
* length that can be used to resize the buffer.
*/
#define RTStrToUtf16Ex(pszString, cchString, ppwsz, cwc, pcwc) \
RTStrToUtf16ExTag((pszString), (cchString), (ppwsz), (cwc), (pcwc), RTSTR_TAG)
/**
* Translates pszString from UTF-8 to UTF-16, allocating the result buffer if
* requested (custom tag).
*
* @returns iprt status code.
* @param pszString UTF-8 string to convert.
* @param cchString The maximum size in chars (the type) to convert. The conversion stop
* when it reaches cchString or the string terminator ('\\0').
* Use RTSTR_MAX to translate the entire string.
* @param ppwsz If cwc is non-zero, this must either be pointing to pointer to
* a buffer of the specified size, or pointer to a NULL pointer.
* If *ppwsz is NULL or cwc is zero a buffer of at least cwc items
* will be allocated to hold the translated string.
* If a buffer was requested it must be freed using RTUtf16Free().
* @param cwc The buffer size in RTUTF16s. This includes the terminator.
* @param pcwc Where to store the length of the translated string,
* excluding the terminator. (Optional)
*
* This may be set under some error conditions,
* however, only for VERR_BUFFER_OVERFLOW and
* VERR_NO_STR_MEMORY will it contain a valid string
* length that can be used to resize the buffer.
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(int) RTStrToUtf16ExTag(const char *pszString, size_t cchString,
PRTUTF16 *ppwsz, size_t cwc, size_t *pcwc, const char *pszTag);
/**
* Translates pszString from UTF-8 to UTF-16BE, allocating the result buffer if requested.
*
* This differs from RTStrToUtf16Ex in that it always produces a
* big-endian string.
*
* @returns iprt status code.
* @param pszString UTF-8 string to convert.
* @param cchString The maximum size in chars (the type) to convert. The conversion stop
* when it reaches cchString or the string terminator ('\\0').
* Use RTSTR_MAX to translate the entire string.
* @param ppwsz If cwc is non-zero, this must either be pointing to pointer to
* a buffer of the specified size, or pointer to a NULL pointer.
* If *ppwsz is NULL or cwc is zero a buffer of at least cwc items
* will be allocated to hold the translated string.
* If a buffer was requested it must be freed using RTUtf16Free().
* @param cwc The buffer size in RTUTF16s. This includes the terminator.
* @param pcwc Where to store the length of the translated string,
* excluding the terminator. (Optional)
*
* This may be set under some error conditions,
* however, only for VERR_BUFFER_OVERFLOW and
* VERR_NO_STR_MEMORY will it contain a valid string
* length that can be used to resize the buffer.
*/
#define RTStrToUtf16BigEx(pszString, cchString, ppwsz, cwc, pcwc) \
RTStrToUtf16BigExTag((pszString), (cchString), (ppwsz), (cwc), (pcwc), RTSTR_TAG)
/**
* Translates pszString from UTF-8 to UTF-16BE, allocating the result buffer if
* requested (custom tag).
*
* This differs from RTStrToUtf16ExTag in that it always produces a
* big-endian string.
*
* @returns iprt status code.
* @param pszString UTF-8 string to convert.
* @param cchString The maximum size in chars (the type) to convert. The conversion stop
* when it reaches cchString or the string terminator ('\\0').
* Use RTSTR_MAX to translate the entire string.
* @param ppwsz If cwc is non-zero, this must either be pointing to pointer to
* a buffer of the specified size, or pointer to a NULL pointer.
* If *ppwsz is NULL or cwc is zero a buffer of at least cwc items
* will be allocated to hold the translated string.
* If a buffer was requested it must be freed using RTUtf16Free().
* @param cwc The buffer size in RTUTF16s. This includes the terminator.
* @param pcwc Where to store the length of the translated string,
* excluding the terminator. (Optional)
*
* This may be set under some error conditions,
* however, only for VERR_BUFFER_OVERFLOW and
* VERR_NO_STR_MEMORY will it contain a valid string
* length that can be used to resize the buffer.
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(int) RTStrToUtf16BigExTag(const char *pszString, size_t cchString,
PRTUTF16 *ppwsz, size_t cwc, size_t *pcwc, const char *pszTag);
/**
* Calculates the length of the string in Latin-1 characters.
*
* This function will validate the string, and incorrectly encoded UTF-8
* strings as well as string with codepoints outside the latin-1 range will be
* rejected. The primary purpose of this function is to help allocate buffers
* for RTStrToLatin1Ex of the correct size. For most other purposes
* RTStrCalcLatin1LenEx() should be used.
*
* @returns Number of Latin-1 characters.
* @returns 0 if the string was incorrectly encoded.
* @param psz The string.
*/
RTDECL(size_t) RTStrCalcLatin1Len(const char *psz);
/**
* Calculates the length of the string in Latin-1 characters.
*
* This function will validate the string, and incorrectly encoded UTF-8
* strings as well as string with codepoints outside the latin-1 range will be
* rejected.
*
* @returns iprt status code.
* @param psz The string.
* @param cch The max string length. Use RTSTR_MAX to process the
* entire string.
* @param pcch Where to store the string length. Optional.
* This is undefined on failure.
*/
RTDECL(int) RTStrCalcLatin1LenEx(const char *psz, size_t cch, size_t *pcch);
/**
* Translate a UTF-8 string into a Latin-1 allocating the result buffer (default
* tag).
*
* @returns iprt status code.
* @param pszString UTF-8 string to convert.
* @param ppszString Receives pointer to the allocated Latin-1 string.
* The returned string must be freed using RTStrFree().
*/
#define RTStrToLatin1(pszString, ppszString) RTStrToLatin1Tag((pszString), (ppszString), RTSTR_TAG)
/**
* Translate a UTF-8 string into a Latin-1 allocating the result buffer (custom
* tag).
*
* @returns iprt status code.
* @param pszString UTF-8 string to convert.
* @param ppszString Receives pointer to the allocated Latin-1 string.
* The returned string must be freed using RTStrFree().
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(int) RTStrToLatin1Tag(const char *pszString, char **ppszString, const char *pszTag);
/**
* Translates pszString from UTF-8 to Latin-1, allocating the result buffer if requested.
*
* @returns iprt status code.
* @param pszString UTF-8 string to convert.
* @param cchString The maximum size in chars (the type) to convert.
* The conversion stop when it reaches cchString or
* the string terminator ('\\0'). Use RTSTR_MAX to
* translate the entire string.
* @param ppsz If cch is non-zero, this must either be pointing to
* pointer to a buffer of the specified size, or
* pointer to a NULL pointer. If *ppsz is NULL or cch
* is zero a buffer of at least cch items will be
* allocated to hold the translated string. If a
* buffer was requested it must be freed using
* RTStrFree().
* @param cch The buffer size in bytes. This includes the
* terminator.
* @param pcch Where to store the length of the translated string,
* excluding the terminator. (Optional)
*
* This may be set under some error conditions,
* however, only for VERR_BUFFER_OVERFLOW and
* VERR_NO_STR_MEMORY will it contain a valid string
* length that can be used to resize the buffer.
*/
#define RTStrToLatin1Ex(pszString, cchString, ppsz, cch, pcch) \
RTStrToLatin1ExTag((pszString), (cchString), (ppsz), (cch), (pcch), RTSTR_TAG)
/**
* Translates pszString from UTF-8 to Latin1, allocating the result buffer if
* requested (custom tag).
*
* @returns iprt status code.
* @param pszString UTF-8 string to convert.
* @param cchString The maximum size in chars (the type) to convert.
* The conversion stop when it reaches cchString or
* the string terminator ('\\0'). Use RTSTR_MAX to
* translate the entire string.
* @param ppsz If cch is non-zero, this must either be pointing to
* pointer to a buffer of the specified size, or
* pointer to a NULL pointer. If *ppsz is NULL or cch
* is zero a buffer of at least cch items will be
* allocated to hold the translated string. If a
* buffer was requested it must be freed using
* RTStrFree().
* @param cch The buffer size in bytes. This includes the
* terminator.
* @param pcch Where to store the length of the translated string,
* excluding the terminator. (Optional)
*
* This may be set under some error conditions,
* however, only for VERR_BUFFER_OVERFLOW and
* VERR_NO_STR_MEMORY will it contain a valid string
* length that can be used to resize the buffer.
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(int) RTStrToLatin1ExTag(const char *pszString, size_t cchString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
/**
* Get the unicode code point at the given string position.
*
* @returns unicode code point.
* @returns RTUNICP_INVALID if the encoding is invalid.
* @param psz The string.
*/
RTDECL(RTUNICP) RTStrGetCpInternal(const char *psz);
/**
* Get the unicode code point at the given string position.
*
* @returns iprt status code
* @returns VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
* @param ppsz The string cursor.
* This is advanced one character forward on failure.
* @param pCp Where to store the unicode code point.
* Stores RTUNICP_INVALID if the encoding is invalid.
*/
RTDECL(int) RTStrGetCpExInternal(const char **ppsz, PRTUNICP pCp);
/**
* Get the unicode code point at the given string position for a string of a
* given length.
*
* @returns iprt status code
* @retval VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
* @retval VERR_END_OF_STRING if *pcch is 0. *pCp is set to RTUNICP_INVALID.
*
* @param ppsz The string.
* @param pcch Pointer to the length of the string. This will be
* decremented by the size of the code point.
* @param pCp Where to store the unicode code point.
* Stores RTUNICP_INVALID if the encoding is invalid.
*/
RTDECL(int) RTStrGetCpNExInternal(const char **ppsz, size_t *pcch, PRTUNICP pCp);
/**
* Put the unicode code point at the given string position
* and return the pointer to the char following it.
*
* This function will not consider anything at or following the
* buffer area pointed to by psz. It is therefore not suitable for
* inserting code points into a string, only appending/overwriting.
*
* @returns pointer to the char following the written code point.
* @param psz The string.
* @param CodePoint The code point to write.
* This should not be RTUNICP_INVALID or any other
* character out of the UTF-8 range.
*
* @remark This is a worker function for RTStrPutCp().
*
* @note The function may write up to 6 chars (bytes) at @a psz and is not
* able to check for overflows. The caller is therefore expected to
* ensure sufficient buffer space.
*
*/
RTDECL(char *) RTStrPutCpInternal(char *psz, RTUNICP CodePoint);
/**
* Get the unicode code point at the given string position.
*
* @returns unicode code point.
* @returns RTUNICP_INVALID if the encoding is invalid.
* @param psz The string.
*
* @remark We optimize this operation by using an inline function for
* the most frequent and simplest sequence, the rest is
* handled by RTStrGetCpInternal().
*/
DECLINLINE(RTUNICP) RTStrGetCp(const char *psz)
{
const unsigned char uch = *(const unsigned char *)psz;
if (!(uch & RT_BIT(7)))
return uch;
return RTStrGetCpInternal(psz);
}
/**
* Get the unicode code point at the given string position.
*
* @returns iprt status code.
* @param ppsz Pointer to the string pointer. This will be updated to
* point to the char following the current code point.
* This is advanced one character forward on failure.
* @param pCp Where to store the code point.
* RTUNICP_INVALID is stored here on failure.
*
* @remark We optimize this operation by using an inline function for
* the most frequent and simplest sequence, the rest is
* handled by RTStrGetCpExInternal().
*/
DECLINLINE(int) RTStrGetCpEx(const char **ppsz, PRTUNICP pCp)
{
const unsigned char uch = **(const unsigned char **)ppsz;
if (!(uch & RT_BIT(7)))
{
(*ppsz)++;
*pCp = uch;
return VINF_SUCCESS;
}
return RTStrGetCpExInternal(ppsz, pCp);
}
/**
* Get the unicode code point at the given string position for a string of a
* given maximum length.
*
* @returns iprt status code.
* @retval VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
* @retval VERR_END_OF_STRING if *pcch is 0. *pCp is set to RTUNICP_INVALID.
*
* @param ppsz Pointer to the string pointer. This will be updated to
* point to the char following the current code point.
* @param pcch Pointer to the maximum string length. This will be
* decremented by the size of the code point found.
* @param pCp Where to store the code point.
* RTUNICP_INVALID is stored here on failure.
*
* @remark We optimize this operation by using an inline function for
* the most frequent and simplest sequence, the rest is
* handled by RTStrGetCpNExInternal().
*/
DECLINLINE(int) RTStrGetCpNEx(const char **ppsz, size_t *pcch, PRTUNICP pCp)
{
if (RT_LIKELY(*pcch != 0))
{
const unsigned char uch = **(const unsigned char **)ppsz;
if (!(uch & RT_BIT(7)))
{
(*ppsz)++;
(*pcch)--;
*pCp = uch;
return VINF_SUCCESS;
}
}
return RTStrGetCpNExInternal(ppsz, pcch, pCp);
}
/**
* Get the UTF-8 size in characters of a given Unicode code point.
*
* The code point is expected to be a valid Unicode one, but not necessarily in
* the range supported by UTF-8.
*
* @returns The number of chars (bytes) required to encode the code point, or
* zero if there is no UTF-8 encoding.
* @param CodePoint The unicode code point.
*/
DECLINLINE(size_t) RTStrCpSize(RTUNICP CodePoint)
{
if (CodePoint < 0x00000080)
return 1;
if (CodePoint < 0x00000800)
return 2;
if (CodePoint < 0x00010000)
return 3;
#ifdef RT_USE_RTC_3629
if (CodePoint < 0x00011000)
return 4;
#else
if (CodePoint < 0x00200000)
return 4;
if (CodePoint < 0x04000000)
return 5;
if (CodePoint < 0x7fffffff)
return 6;
#endif
return 0;
}
/**
* Put the unicode code point at the given string position
* and return the pointer to the char following it.
*
* This function will not consider anything at or following the
* buffer area pointed to by psz. It is therefore not suitable for
* inserting code points into a string, only appending/overwriting.
*
* @returns pointer to the char following the written code point.
* @param psz The string.
* @param CodePoint The code point to write.
* This should not be RTUNICP_INVALID or any other
* character out of the UTF-8 range.
*
* @remark We optimize this operation by using an inline function for
* the most frequent and simplest sequence, the rest is
* handled by RTStrPutCpInternal().
*
* @note The function may write up to 6 chars (bytes) at @a psz and is not
* able to check for overflows. The caller is therefore expected to
* ensure sufficient buffer space.
*
*/
DECLINLINE(char *) RTStrPutCp(char *psz, RTUNICP CodePoint)
{
if (CodePoint < 0x80)
{
*psz++ = (char)CodePoint;
return psz;
}
return RTStrPutCpInternal(psz, CodePoint);
}
/**
* Skips ahead, past the current code point.
*
* @returns Pointer to the char after the current code point.
* @param psz Pointer to the current code point.
* @remark This will not move the next valid code point, only past the current one.
*/
DECLINLINE(char *) RTStrNextCp(const char *psz)
{
RTUNICP Cp;
RTStrGetCpEx(&psz, &Cp);
return (char *)psz;
}
/**
* Skips back to the previous code point.
*
* @returns Pointer to the char before the current code point.
* @returns pszStart on failure.
* @param pszStart Pointer to the start of the string.
* @param psz Pointer to the current code point.
*/
RTDECL(char *) RTStrPrevCp(const char *pszStart, const char *psz);
/** @page pg_rt_str_format The IPRT Format Strings
*
* IPRT implements most of the commonly used format types and flags with the
* exception of floating point which is completely missing. In addition IPRT
* provides a number of IPRT specific format types for the IPRT typedefs and
* other useful things. Note that several of these extensions are similar to
* \%p and doesn't care much if you try add formating flags/width/precision.
*
*
* Group 0a, The commonly used format types:
* - \%s - Takes a pointer to a zero terminated string (UTF-8) and
* prints it with the optionally adjustment (width, -) and
* length restriction (precision).
* - \%ls - Same as \%s except that the input is UTF-16 (output UTF-8).
* - \%Ls - Same as \%s except that the input is UCS-32 (output UTF-8).
* - \%S - Same as \%s, used to convert to current codeset but this is
* now done by the streams code. Deprecated, use \%s.
* - \%lS - Ditto. Deprecated, use \%ls.
* - \%LS - Ditto. Deprecated, use \%Ls.
* - \%c - Takes a char and prints it.
* - \%d - Takes a signed integer and prints it as decimal. Thousand
* separator (\'), zero padding (0), adjustment (-+), width,
* precision
* - \%i - Same as \%d.
* - \%u - Takes an unsigned integer and prints it as decimal. Thousand
* separator (\'), zero padding (0), adjustment (-+), width,
* precision
* - \%x - Takes an unsigned integer and prints it as lowercased
* hexadecimal. The special hash (\#) flag causes a '0x'
* prefixed to be printed. Zero padding (0), adjustment (-+),
* width, precision.
* - \%X - Same as \%x except that it is uppercased.
* - \%o - Takes an unsigned (?) integer and prints it as octal. Zero
* padding (0), adjustment (-+), width, precision.
* - \%p - Takes a pointer (void technically) and prints it. Zero
* padding (0), adjustment (-+), width, precision.
*
* The \%d, \%i, \%u, \%x, \%X and \%o format types support the following
* argument type specifiers:
* - \%ll - long long (uint64_t).
* - \%L - long long (uint64_t).
* - \%l - long (uint32_t, uint64_t)
* - \%h - short (int16_t).
* - \%hh - char (int8_t).
* - \%H - char (int8_t).
* - \%z - size_t.
* - \%j - intmax_t (int64_t).
* - \%t - ptrdiff_t.
* The type in parentheses is typical sizes, however when printing those types
* you are better off using the special group 2 format types below (\%RX32 and
* such).
*
*
* Group 0b, IPRT format tricks:
* - %M - Replaces the format string, takes a string pointer.
* - %N - Nested formatting, takes a pointer to a format string
* followed by the pointer to a va_list variable. The va_list
* variable will not be modified and the caller must do va_end()
* on it. Make sure the va_list variable is NOT in a parameter
* list or some gcc versions/targets may get it all wrong.
*
*
* Group 1, the basic runtime typedefs (excluding those which obviously are
* pointer):
* - \%RTbool - Takes a bool value and prints 'true', 'false', or '!%d!'.
* - \%RTeic - Takes a #PCRTERRINFO value outputting 'rc: msg',
* or 'rc - msg' with the \# flag.
* - \%RTeim - Takes a #PCRTERRINFO value outputting ': msg', or
* ' - msg' with the \# flag.
* - \%RTfile - Takes a #RTFILE value.
* - \%RTfmode - Takes a #RTFMODE value.
* - \%RTfoff - Takes a #RTFOFF value.
* - \%RTfp16 - Takes a #RTFAR16 value.
* - \%RTfp32 - Takes a #RTFAR32 value.
* - \%RTfp64 - Takes a #RTFAR64 value.
* - \%RTgid - Takes a #RTGID value.
* - \%RTino - Takes a #RTINODE value.
* - \%RTint - Takes a #RTINT value.
* - \%RTiop - Takes a #RTIOPORT value.
* - \%RTldrm - Takes a #RTLDRMOD value.
* - \%RTmac - Takes a #PCRTMAC pointer.
* - \%RTnaddr - Takes a #PCRTNETADDR value.
* - \%RTnaipv4 - Takes a #RTNETADDRIPV4 value.
* - \%RTnaipv6 - Takes a #PCRTNETADDRIPV6 value.
* - \%RTnthrd - Takes a #RTNATIVETHREAD value.
* - \%RTnthrd - Takes a #RTNATIVETHREAD value.
* - \%RTproc - Takes a #RTPROCESS value.
* - \%RTptr - Takes a #RTINTPTR or #RTUINTPTR value (but not void *).
* - \%RTreg - Takes a #RTCCUINTREG value.
* - \%RTsel - Takes a #RTSEL value.
* - \%RTsem - Takes a #RTSEMEVENT, #RTSEMEVENTMULTI, #RTSEMMUTEX, #RTSEMFASTMUTEX, or #RTSEMRW value.
* - \%RTsock - Takes a #RTSOCKET value.
* - \%RTthrd - Takes a #RTTHREAD value.
* - \%RTuid - Takes a #RTUID value.
* - \%RTuint - Takes a #RTUINT value.
* - \%RTunicp - Takes a #RTUNICP value.
* - \%RTutf16 - Takes a #RTUTF16 value.
* - \%RTuuid - Takes a #PCRTUUID and will print the UUID as a string.
* - \%RTxuint - Takes a #RTUINT or #RTINT value, formatting it as hex.
* - \%RGi - Takes a #RTGCINT value.
* - \%RGp - Takes a #RTGCPHYS value.
* - \%RGr - Takes a #RTGCUINTREG value.
* - \%RGu - Takes a #RTGCUINT value.
* - \%RGv - Takes a #RTGCPTR, #RTGCINTPTR or #RTGCUINTPTR value.
* - \%RGx - Takes a #RTGCUINT or #RTGCINT value, formatting it as hex.
* - \%RHi - Takes a #RTHCINT value.
* - \%RHp - Takes a #RTHCPHYS value.
* - \%RHr - Takes a #RTHCUINTREG value.
* - \%RHu - Takes a #RTHCUINT value.
* - \%RHv - Takes a #RTHCPTR, #RTHCINTPTR or #RTHCUINTPTR value.
* - \%RHx - Takes a #RTHCUINT or #RTHCINT value, formatting it as hex.
* - \%RRv - Takes a #RTRCPTR, #RTRCINTPTR or #RTRCUINTPTR value.
* - \%RCi - Takes a #RTINT value.
* - \%RCp - Takes a #RTCCPHYS value.
* - \%RCr - Takes a #RTCCUINTREG value.
* - \%RCu - Takes a #RTUINT value.
* - \%RCv - Takes a #uintptr_t, #intptr_t, void * value.
* - \%RCx - Takes a #RTUINT or #RTINT value, formatting it as hex.
*
*
* Group 2, the generic integer types which are prefered over relying on what
* bit-count a 'long', 'short', or 'long long' has on a platform. This are
* highly prefered for the [u]intXX_t kind of types:
* - \%RI[8|16|32|64] - Signed integer value of the specifed bit count.
* - \%RU[8|16|32|64] - Unsigned integer value of the specifed bit count.
* - \%RX[8|16|32|64] - Hexadecimal integer value of the specifed bit count.
*
*
* Group 3, hex dumpers and other complex stuff which requires more than simple
* formatting:
* - \%Rhxd - Takes a pointer to the memory which is to be dumped in typical
* hex format. Use the precision to specify the length, and the width to
* set the number of bytes per line. Default width and precision is 16.
* - \%RhxD - Same as \%Rhxd, except that it skips duplicate lines.
* - \%Rhxs - Takes a pointer to the memory to be displayed as a hex string,
* i.e. a series of space separated bytes formatted as two digit hex value.
* Use the precision to specify the length. Default length is 16 bytes.
* The width, if specified, is ignored.
* The space separtor can get change to a colon by
* using the ' flag, and removed entirely using \#.
* - \%RhXd - Same as \%Rhxd, but takes an additional uint64_t
* value with the memory start address/offset after
* the memory pointer.
* - \%RhXD - Same as \%RhxD, but takes an additional uint64_t
* value with the memory start address/offset after
* the memory pointer.
* - \%RhXs - Same as \%Rhxs, but takes an additional uint64_t
* value with the memory start address/offset after
* the memory pointer.
*
* - \%Rhcb - Human readable byte size formatting, using
* binary unit prefixes (GiB, MiB and such). Takes a
* 64-bit unsigned integer as input. Does one
* decimal point by default, can do 0-3 via precision
* field. No rounding when calculating fraction.
* The space flag add a space between the value and
* unit.
* - \%RhcB - Same a \%Rhcb only the 'i' is skipped in the unit.
* - \%Rhci - SI variant of \%Rhcb, fraction is rounded.
* - \%Rhub - Human readable number formatting, using
* binary unit prefixes. Takes a 64-bit unsigned
* integer as input. Does one decimal point by
* default, can do 0-3 via precision field. No
* rounding when calculating fraction. The space
* flag add a space between the value and unit.
* - \%RhuB - Same a \%Rhub only the 'i' is skipped in the unit.
* - \%Rhui - SI variant of \%Rhub, fraction is rounded.
*
* - \%Rrc - Takes an integer iprt status code as argument. Will insert the
* status code define corresponding to the iprt status code.
* - \%Rrs - Takes an integer iprt status code as argument. Will insert the
* short description of the specified status code.
* - \%Rrf - Takes an integer iprt status code as argument. Will insert the
* full description of the specified status code.
* Note! Works like \%Rrs when IN_RT_STATIC is defined (so please avoid).
* - \%Rra - Takes an integer iprt status code as argument. Will insert the
* status code define + full description.
* Note! Reduced output when IN_RT_STATIC is defined (so please avoid).
* - \%Rwc - Takes a long Windows error code as argument. Will insert the status
* code define corresponding to the Windows error code.
* - \%Rwf - Takes a long Windows error code as argument. Will insert the
* full description of the specified status code.
* Note! Works like \%Rwc when IN_RT_STATIC is defined.
* - \%Rwa - Takes a long Windows error code as argument. Will insert the
* error code define + full description.
* Note! Reduced output when IN_RT_STATIC is defined (so please avoid).
*
* - \%Rhrc - Takes a COM/XPCOM status code as argument. Will insert the status
* code define corresponding to the Windows error code.
* - \%Rhrf - Takes a COM/XPCOM status code as argument. Will insert the
* full description of the specified status code.
* Note! Works like \%Rhrc when IN_RT_STATIC is
* defined on Windows (so please avoid).
* - \%Rhra - Takes a COM/XPCOM error code as argument. Will insert the
* error code define + full description.
* Note! Reduced output when IN_RT_STATIC is defined on Windows (so please avoid).
*
* - \%Rfn - Pretty printing of a function or method. It drops the
* return code and parameter list.
* - \%Rbn - Prints the base name. For dropping the path in
* order to save space when printing a path name.
*
* - \%lRbs - Same as \%ls except inlut is big endian UTF-16.
*
* On other platforms, \%Rw? simply prints the argument in a form of 0xXXXXXXXX.
*
*
* Group 4, structure dumpers:
* - \%RDtimespec - Takes a PCRTTIMESPEC.
*
*
* Group 5, XML / HTML, JSON and URI escapers:
* - \%RMas - Takes a string pointer (const char *) and outputs
* it as an attribute value with the proper escaping.
* This typically ends up in double quotes.
*
* - \%RMes - Takes a string pointer (const char *) and outputs
* it as an element with the necessary escaping.
*
* - \%RMjs - Takes a string pointer (const char *) and outputs
* it in quotes with proper JSON escaping.
*
* - \%RMpa - Takes a string pointer (const char *) and outputs
* it percent-encoded (RFC-3986). All reserved characters
* are encoded.
*
* - \%RMpf - Takes a string pointer (const char *) and outputs
* it percent-encoded (RFC-3986), form style. This
* means '+' is used to escape space (' ') and '%2B'
* is used to escape '+'.
*
* - \%RMpp - Takes a string pointer (const char *) and outputs
* it percent-encoded (RFC-3986), path style. This
* means '/' will not be escaped.
*
* - \%RMpq - Takes a string pointer (const char *) and outputs
* it percent-encoded (RFC-3986), query style. This
* means '+' will not be escaped.
*
*
* Group 6, CPU Architecture Register dumpers:
* - \%RAx86[reg] - Takes a 64-bit register value if the register is
* 64-bit or smaller. Check the code wrt which
* registers are implemented.
*
*/
#ifndef DECLARED_FNRTSTROUTPUT /* duplicated in iprt/log.h & errcore.h */
# define DECLARED_FNRTSTROUTPUT
/**
* Output callback.
*
* @returns number of bytes written.
* @param pvArg User argument.
* @param pachChars Pointer to an array of utf-8 characters.
* @param cbChars Number of bytes in the character array pointed to by pachChars.
*/
typedef DECLCALLBACKTYPE(size_t, FNRTSTROUTPUT,(void *pvArg, const char *pachChars, size_t cbChars));
/** Pointer to callback function. */
typedef FNRTSTROUTPUT *PFNRTSTROUTPUT;
#endif
/** @name Format flag.
* These are used by RTStrFormat extensions and RTStrFormatNumber, mind
* that not all flags makes sense to both of the functions.
* @{ */
#define RTSTR_F_CAPITAL 0x0001
#define RTSTR_F_LEFT 0x0002
#define RTSTR_F_ZEROPAD 0x0004
#define RTSTR_F_SPECIAL 0x0008
#define RTSTR_F_VALSIGNED 0x0010
#define RTSTR_F_PLUS 0x0020
#define RTSTR_F_BLANK 0x0040
#define RTSTR_F_WIDTH 0x0080
#define RTSTR_F_PRECISION 0x0100
#define RTSTR_F_THOUSAND_SEP 0x0200
#define RTSTR_F_OBFUSCATE_PTR 0x0400
#define RTSTR_F_BIT_MASK 0xf800
#define RTSTR_F_8BIT 0x0800
#define RTSTR_F_16BIT 0x1000
#define RTSTR_F_32BIT 0x2000
#define RTSTR_F_64BIT 0x4000
#define RTSTR_F_128BIT 0x8000
/** @} */
/** @def RTSTR_GET_BIT_FLAG
* Gets the bit flag for the specified type.
*/
#define RTSTR_GET_BIT_FLAG(type) \
( sizeof(type) * 8 == 32 ? RTSTR_F_32BIT \
: sizeof(type) * 8 == 64 ? RTSTR_F_64BIT \
: sizeof(type) * 8 == 16 ? RTSTR_F_16BIT \
: sizeof(type) * 8 == 8 ? RTSTR_F_8BIT \
: sizeof(type) * 8 == 128 ? RTSTR_F_128BIT \
: 0)
/**
* Callback to format non-standard format specifiers.
*
* @returns The number of bytes formatted.
* @param pvArg Formatter argument.
* @param pfnOutput Pointer to output function.
* @param pvArgOutput Argument for the output function.
* @param ppszFormat Pointer to the format string pointer. Advance this till the char
* after the format specifier.
* @param pArgs Pointer to the argument list. Use this to fetch the arguments.
* @param cchWidth Format Width. -1 if not specified.
* @param cchPrecision Format Precision. -1 if not specified.
* @param fFlags Flags (RTSTR_NTFS_*).
* @param chArgSize The argument size specifier, 'l' or 'L'.
*/
typedef DECLCALLBACKTYPE(size_t, FNSTRFORMAT,(void *pvArg, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
const char **ppszFormat, va_list *pArgs, int cchWidth,
int cchPrecision, unsigned fFlags, char chArgSize));
/** Pointer to a FNSTRFORMAT() function. */
typedef FNSTRFORMAT *PFNSTRFORMAT;
/**
* Partial implementation of a printf like formatter.
* It doesn't do everything correct, and there is no floating point support.
* However, it supports custom formats by the means of a format callback.
*
* @returns number of bytes formatted.
* @param pfnOutput Output worker.
* Called in two ways. Normally with a string and its length.
* For termination, it's called with NULL for string, 0 for length.
* @param pvArgOutput Argument to the output worker.
* @param pfnFormat Custom format worker.
* @param pvArgFormat Argument to the format worker.
* @param pszFormat Pointer to the format string, @see pg_rt_str_format.
* @param InArgs Argument list.
*/
RTDECL(size_t) RTStrFormatV(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat,
const char *pszFormat, va_list InArgs) RT_IPRT_FORMAT_ATTR(5, 0);
/**
* Partial implementation of a printf like formatter.
*
* It doesn't do everything correct, and there is no floating point support.
* However, it supports custom formats by the means of a format callback.
*
* @returns number of bytes formatted.
* @param pfnOutput Output worker.
* Called in two ways. Normally with a string and its length.
* For termination, it's called with NULL for string, 0 for length.
* @param pvArgOutput Argument to the output worker.
* @param pfnFormat Custom format worker.
* @param pvArgFormat Argument to the format worker.
* @param pszFormat Pointer to the format string, @see pg_rt_str_format.
* @param ... Argument list.
*/
RTDECL(size_t) RTStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat,
const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(5, 6);
/**
* Formats an integer number according to the parameters.
*
* @returns Length of the formatted number.
* @param psz Pointer to output string buffer of sufficient size.
* @param u64Value Value to format.
* @param uiBase Number representation base.
* @param cchWidth Width.
* @param cchPrecision Precision.
* @param fFlags Flags, RTSTR_F_XXX.
*/
RTDECL(int) RTStrFormatNumber(char *psz, uint64_t u64Value, unsigned int uiBase, signed int cchWidth, signed int cchPrecision,
unsigned int fFlags);
/**
* Formats an unsigned 8-bit number.
*
* @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
* @param pszBuf The output buffer.
* @param cbBuf The size of the output buffer.
* @param u8Value The value to format.
* @param uiBase Number representation base.
* @param cchWidth Width.
* @param cchPrecision Precision.
* @param fFlags Flags, RTSTR_F_XXX.
*/
RTDECL(ssize_t) RTStrFormatU8(char *pszBuf, size_t cbBuf, uint8_t u8Value, unsigned int uiBase,
signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
/**
* Formats an unsigned 16-bit number.
*
* @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
* @param pszBuf The output buffer.
* @param cbBuf The size of the output buffer.
* @param u16Value The value to format.
* @param uiBase Number representation base.
* @param cchWidth Width.
* @param cchPrecision Precision.
* @param fFlags Flags, RTSTR_F_XXX.
*/
RTDECL(ssize_t) RTStrFormatU16(char *pszBuf, size_t cbBuf, uint16_t u16Value, unsigned int uiBase,
signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
/**
* Formats an unsigned 32-bit number.
*
* @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
* @param pszBuf The output buffer.
* @param cbBuf The size of the output buffer.
* @param u32Value The value to format.
* @param uiBase Number representation base.
* @param cchWidth Width.
* @param cchPrecision Precision.
* @param fFlags Flags, RTSTR_F_XXX.
*/
RTDECL(ssize_t) RTStrFormatU32(char *pszBuf, size_t cbBuf, uint32_t u32Value, unsigned int uiBase,
signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
/**
* Formats an unsigned 64-bit number.
*
* @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
* @param pszBuf The output buffer.
* @param cbBuf The size of the output buffer.
* @param u64Value The value to format.
* @param uiBase Number representation base.
* @param cchWidth Width.
* @param cchPrecision Precision.
* @param fFlags Flags, RTSTR_F_XXX.
*/
RTDECL(ssize_t) RTStrFormatU64(char *pszBuf, size_t cbBuf, uint64_t u64Value, unsigned int uiBase,
signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
/**
* Formats an unsigned 128-bit number.
*
* @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
* @param pszBuf The output buffer.
* @param cbBuf The size of the output buffer.
* @param pu128Value The value to format.
* @param uiBase Number representation base.
* @param cchWidth Width.
* @param cchPrecision Precision.
* @param fFlags Flags, RTSTR_F_XXX.
* @remarks The current implementation is limited to base 16 and doesn't do
* width or precision and probably ignores few flags too.
*/
RTDECL(ssize_t) RTStrFormatU128(char *pszBuf, size_t cbBuf, PCRTUINT128U pu128Value, unsigned int uiBase,
signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
/**
* Formats an unsigned 256-bit number.
*
* @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
* @param pszBuf The output buffer.
* @param cbBuf The size of the output buffer.
* @param pu256Value The value to format.
* @param uiBase Number representation base.
* @param cchWidth Width.
* @param cchPrecision Precision.
* @param fFlags Flags, RTSTR_F_XXX.
* @remarks The current implementation is limited to base 16 and doesn't do
* width or precision and probably ignores few flags too.
*/
RTDECL(ssize_t) RTStrFormatU256(char *pszBuf, size_t cbBuf, PCRTUINT256U pu256Value, unsigned int uiBase,
signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
/**
* Formats an unsigned 512-bit number.
*
* @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
* @param pszBuf The output buffer.
* @param cbBuf The size of the output buffer.
* @param pu512Value The value to format.
* @param uiBase Number representation base.
* @param cchWidth Width.
* @param cchPrecision Precision.
* @param fFlags Flags, RTSTR_F_XXX.
* @remarks The current implementation is limited to base 16 and doesn't do
* width or precision and probably ignores few flags too.
*/
RTDECL(ssize_t) RTStrFormatU512(char *pszBuf, size_t cbBuf, PCRTUINT512U pu512Value, unsigned int uiBase,
signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
/**
* Formats an 32-bit extended floating point number.
*
* @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
* @param pszBuf The output buffer.
* @param cbBuf The size of the output buffer.
* @param pr32Value The value to format.
* @param cchWidth Width.
* @param cchPrecision Precision.
* @param fFlags Flags, RTSTR_F_XXX.
*/
RTDECL(ssize_t) RTStrFormatR32(char *pszBuf, size_t cbBuf, PCRTFLOAT32U pr32Value, signed int cchWidth,
signed int cchPrecision, uint32_t fFlags);
/**
* Formats an 64-bit extended floating point number.
*
* @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
* @param pszBuf The output buffer.
* @param cbBuf The size of the output buffer.
* @param pr64Value The value to format.
* @param cchWidth Width.
* @param cchPrecision Precision.
* @param fFlags Flags, RTSTR_F_XXX.
*/
RTDECL(ssize_t) RTStrFormatR64(char *pszBuf, size_t cbBuf, PCRTFLOAT64U pr64Value, signed int cchWidth,
signed int cchPrecision, uint32_t fFlags);
#if !defined(__IBMCPP__) && !defined(__IBMC__)
/**
* Formats an 80-bit extended floating point number.
*
* @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
* @param pszBuf The output buffer.
* @param cbBuf The size of the output buffer.
* @param pr80Value The value to format.
* @param cchWidth Width.
* @param cchPrecision Precision.
* @param fFlags Flags, RTSTR_F_XXX.
*/
RTDECL(ssize_t) RTStrFormatR80(char *pszBuf, size_t cbBuf, PCRTFLOAT80U pr80Value, signed int cchWidth,
signed int cchPrecision, uint32_t fFlags);
/**
* Formats an 80-bit extended floating point number, version 2.
*
* @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
* @param pszBuf The output buffer.
* @param cbBuf The size of the output buffer.
* @param pr80Value The value to format.
* @param cchWidth Width.
* @param cchPrecision Precision.
* @param fFlags Flags, RTSTR_F_XXX.
*/
RTDECL(ssize_t) RTStrFormatR80u2(char *pszBuf, size_t cbBuf, PCRTFLOAT80U2 pr80Value, signed int cchWidth,
signed int cchPrecision, uint32_t fFlags);
#endif /* uint16_t bitfields doesn't work */
/**
* Callback for formatting a type.
*
* This is registered using the RTStrFormatTypeRegister function and will
* be called during string formatting to handle the specified %R[type].
* The argument for this format type is assumed to be a pointer and it's
* passed in the @a pvValue argument.
*
* @returns Length of the formatted output.
* @param pfnOutput Output worker.
* @param pvArgOutput Argument to the output worker.
* @param pszType The type name.
* @param pvValue The argument value.
* @param cchWidth Width.
* @param cchPrecision Precision.
* @param fFlags Flags (NTFS_*).
* @param pvUser The user argument.
*/
typedef DECLCALLBACKTYPE(size_t, FNRTSTRFORMATTYPE,(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
const char *pszType, void const *pvValue,
int cchWidth, int cchPrecision, unsigned fFlags,
void *pvUser));
/** Pointer to a FNRTSTRFORMATTYPE. */
typedef FNRTSTRFORMATTYPE *PFNRTSTRFORMATTYPE;
/**
* Register a format handler for a type.
*
* The format handler is used to handle '%R[type]' format types, where the argument
* in the vector is a pointer value (a bit restrictive, but keeps it simple).
*
* The caller must ensure that no other thread will be making use of any of
* the dynamic formatting type facilities simultaneously with this call.
*
* @returns IPRT status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_ALREADY_EXISTS if the type has already been registered.
* @retval VERR_TOO_MANY_OPEN_FILES if all the type slots has been allocated already.
*
* @param pszType The type name.
* @param pfnHandler The handler address. See FNRTSTRFORMATTYPE for details.
* @param pvUser The user argument to pass to the handler. See RTStrFormatTypeSetUser
* for how to update this later.
*/
RTDECL(int) RTStrFormatTypeRegister(const char *pszType, PFNRTSTRFORMATTYPE pfnHandler, void *pvUser);
/**
* Deregisters a format type.
*
* The caller must ensure that no other thread will be making use of any of
* the dynamic formatting type facilities simultaneously with this call.
*
* @returns IPRT status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_FILE_NOT_FOUND if not found.
*
* @param pszType The type to deregister.
*/
RTDECL(int) RTStrFormatTypeDeregister(const char *pszType);
/**
* Sets the user argument for a type.
*
* This can be used if a user argument needs relocating in GC.
*
* @returns IPRT status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_FILE_NOT_FOUND if not found.
*
* @param pszType The type to update.
* @param pvUser The new user argument value.
*/
RTDECL(int) RTStrFormatTypeSetUser(const char *pszType, void *pvUser);
/**
* String printf.
*
* @returns The length of the returned string (in pszBuffer) excluding the
* terminator.
* @param pszBuffer Output buffer.
* @param cchBuffer Size of the output buffer.
* @param pszFormat Pointer to the format string, @see pg_rt_str_format.
* @param args The format argument.
*
* @deprecated Use RTStrPrintf2V! Problematic return value on overflow.
*/
RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(3, 0);
/**
* String printf.
*
* @returns The length of the returned string (in pszBuffer) excluding the
* terminator.
* @param pszBuffer Output buffer.
* @param cchBuffer Size of the output buffer.
* @param pszFormat Pointer to the format string, @see pg_rt_str_format.
* @param ... The format argument.
*
* @deprecated Use RTStrPrintf2! Problematic return value on overflow.
*/
RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
/**
* String printf with custom formatting.
*
* @returns The length of the returned string (in pszBuffer) excluding the
* terminator.
* @param pfnFormat Pointer to handler function for the custom formats.
* @param pvArg Argument to the pfnFormat function.
* @param pszBuffer Output buffer.
* @param cchBuffer Size of the output buffer.
* @param pszFormat Pointer to the format string, @see pg_rt_str_format.
* @param args The format argument.
*
* @deprecated Use RTStrPrintf2ExV! Problematic return value on overflow.
*/
RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer,
const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(5, 0);
/**
* String printf with custom formatting.
*
* @returns The length of the returned string (in pszBuffer) excluding the
* terminator.
* @param pfnFormat Pointer to handler function for the custom formats.
* @param pvArg Argument to the pfnFormat function.
* @param pszBuffer Output buffer.
* @param cchBuffer Size of the output buffer.
* @param pszFormat Pointer to the format string, @see pg_rt_str_format.
* @param ... The format argument.
*
* @deprecated Use RTStrPrintf2Ex! Problematic return value on overflow.
*/
RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer,
const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(5, 6);
/**
* String printf, version 2.
*
* @returns On success, positive count of formatted character excluding the
* terminator. On buffer overflow, negative number giving the required
* buffer size (including terminator char).
*
* @param pszBuffer Output buffer.
* @param cbBuffer Size of the output buffer.
* @param pszFormat Pointer to the format string, @see pg_rt_str_format.
* @param args The format argument.
*/
RTDECL(ssize_t) RTStrPrintf2V(char *pszBuffer, size_t cbBuffer, const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(3, 0);
/**
* String printf, version 2.
*
* @returns On success, positive count of formatted character excluding the
* terminator. On buffer overflow, negative number giving the required
* buffer size (including terminator char).
*
* @param pszBuffer Output buffer.
* @param cbBuffer Size of the output buffer.
* @param pszFormat Pointer to the format string, @see pg_rt_str_format.
* @param ... The format argument.
*/
RTDECL(ssize_t) RTStrPrintf2(char *pszBuffer, size_t cbBuffer, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
/**
* String printf with custom formatting, version 2.
*
* @returns On success, positive count of formatted character excluding the
* terminator. On buffer overflow, negative number giving the required
* buffer size (including terminator char).
*
* @param pfnFormat Pointer to handler function for the custom formats.
* @param pvArg Argument to the pfnFormat function.
* @param pszBuffer Output buffer.
* @param cbBuffer Size of the output buffer.
* @param pszFormat Pointer to the format string, @see pg_rt_str_format.
* @param args The format argument.
*/
RTDECL(ssize_t) RTStrPrintf2ExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cbBuffer,
const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(5, 0);
/**
* String printf with custom formatting, version 2.
*
* @returns On success, positive count of formatted character excluding the
* terminator. On buffer overflow, negative number giving the required
* buffer size (including terminator char).
*
* @param pfnFormat Pointer to handler function for the custom formats.
* @param pvArg Argument to the pfnFormat function.
* @param pszBuffer Output buffer.
* @param cbBuffer Size of the output buffer.
* @param pszFormat Pointer to the format string, @see pg_rt_str_format.
* @param ... The format argument.
*/
RTDECL(ssize_t) RTStrPrintf2Ex(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cbBuffer,
const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(5, 6);
/**
* Allocating string printf (default tag).
*
* @returns The length of the string in the returned *ppszBuffer excluding the
* terminator.
* @returns -1 on failure.
* @param ppszBuffer Where to store the pointer to the allocated output buffer.
* The buffer should be freed using RTStrFree().
* On failure *ppszBuffer will be set to NULL.
* @param pszFormat Pointer to the format string, @see pg_rt_str_format.
* @param args The format argument.
*/
#define RTStrAPrintfV(ppszBuffer, pszFormat, args) RTStrAPrintfVTag((ppszBuffer), (pszFormat), (args), RTSTR_TAG)
/**
* Allocating string printf (custom tag).
*
* @returns The length of the string in the returned *ppszBuffer excluding the
* terminator.
* @returns -1 on failure.
* @param ppszBuffer Where to store the pointer to the allocated output buffer.
* The buffer should be freed using RTStrFree().
* On failure *ppszBuffer will be set to NULL.
* @param pszFormat Pointer to the format string, @see pg_rt_str_format.
* @param args The format argument.
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(int) RTStrAPrintfVTag(char **ppszBuffer, const char *pszFormat, va_list args, const char *pszTag) RT_IPRT_FORMAT_ATTR(2, 0);
/**
* Allocating string printf.
*
* @returns The length of the string in the returned *ppszBuffer excluding the
* terminator.
* @returns -1 on failure.
* @param ppszBuffer Where to store the pointer to the allocated output buffer.
* The buffer should be freed using RTStrFree().
* On failure *ppszBuffer will be set to NULL.
* @param pszFormat Pointer to the format string, @see pg_rt_str_format.
* @param ... The format argument.
*/
DECLINLINE(int) RT_IPRT_FORMAT_ATTR(2, 3) RTStrAPrintf(char **ppszBuffer, const char *pszFormat, ...)
{
int cbRet;
va_list va;
va_start(va, pszFormat);
cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, RTSTR_TAG);
va_end(va);
return cbRet;
}
/**
* Allocating string printf (custom tag).
*
* @returns The length of the string in the returned *ppszBuffer excluding the
* terminator.
* @returns -1 on failure.
* @param ppszBuffer Where to store the pointer to the allocated output buffer.
* The buffer should be freed using RTStrFree().
* On failure *ppszBuffer will be set to NULL.
* @param pszTag Allocation tag used for statistics and such.
* @param pszFormat Pointer to the format string, @see pg_rt_str_format.
* @param ... The format argument.
*/
DECLINLINE(int) RT_IPRT_FORMAT_ATTR(3, 4) RTStrAPrintfTag(char **ppszBuffer, const char *pszTag, const char *pszFormat, ...)
{
int cbRet;
va_list va;
va_start(va, pszFormat);
cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, pszTag);
va_end(va);
return cbRet;
}
/**
* Allocating string printf, version 2.
*
* @returns Formatted string. Use RTStrFree() to free it. NULL when out of
* memory.
* @param pszFormat Pointer to the format string, @see pg_rt_str_format.
* @param args The format argument.
*/
#define RTStrAPrintf2V(pszFormat, args) RTStrAPrintf2VTag((pszFormat), (args), RTSTR_TAG)
/**
* Allocating string printf, version 2.
*
* @returns Formatted string. Use RTStrFree() to free it. NULL when out of
* memory.
* @param pszFormat Pointer to the format string, @see pg_rt_str_format.
* @param args The format argument.
* @param pszTag Allocation tag used for statistics and such.
*/
RTDECL(char *) RTStrAPrintf2VTag(const char *pszFormat, va_list args, const char *pszTag) RT_IPRT_FORMAT_ATTR(1, 0);
/**
* Allocating string printf, version 2 (default tag).
*
* @returns Formatted string. Use RTStrFree() to free it. NULL when out of
* memory.
* @param pszFormat Pointer to the format string, @see pg_rt_str_format.
* @param ... The format argument.
*/
DECLINLINE(char *) RT_IPRT_FORMAT_ATTR(1, 2) RTStrAPrintf2(const char *pszFormat, ...)
{
char *pszRet;
va_list va;
va_start(va, pszFormat);
pszRet = RTStrAPrintf2VTag(pszFormat, va, RTSTR_TAG);
va_end(va);
return pszRet;
}
/**
* Allocating string printf, version 2 (custom tag).
*
* @returns Formatted string. Use RTStrFree() to free it. NULL when out of
* memory.
* @param pszTag Allocation tag used for statistics and such.
* @param pszFormat Pointer to the format string, @see pg_rt_str_format.
* @param ... The format argument.
*/
DECLINLINE(char *) RT_IPRT_FORMAT_ATTR(2, 3) RTStrAPrintf2Tag(const char *pszTag, const char *pszFormat, ...)
{
char *pszRet;
va_list va;
va_start(va, pszFormat);
pszRet = RTStrAPrintf2VTag(pszFormat, va, pszTag);
va_end(va);
return pszRet;
}
/**
* Strips blankspaces from both ends of the string.
*
* @returns Pointer to first non-blank char in the string.
* @param psz The string to strip.
*/
RTDECL(char *) RTStrStrip(char *psz);
/**
* Strips blankspaces from the start of the string.
*
* @returns Pointer to first non-blank char in the string.
* @param psz The string to strip.
*/
RTDECL(char *) RTStrStripL(const char *psz);
/**
* Strips blankspaces from the end of the string.
*
* @returns psz.
* @param psz The string to strip.
*/
RTDECL(char *) RTStrStripR(char *psz);
/**
* String copy with overflow handling.
*
* @retval VINF_SUCCESS on success.
* @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
* buffer will contain as much of the string as it can hold, fully
* terminated.
*
* @param pszDst The destination buffer.
* @param cbDst The size of the destination buffer (in bytes).
* @param pszSrc The source string. NULL is not OK.
*/
RTDECL(int) RTStrCopy(char *pszDst, size_t cbDst, const char *pszSrc);
/**
* String copy with overflow handling.
*
* @retval VINF_SUCCESS on success.
* @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
* buffer will contain as much of the string as it can hold, fully
* terminated.
*
* @param pszDst The destination buffer.
* @param cbDst The size of the destination buffer (in bytes).
* @param pszSrc The source string. NULL is not OK.
* @param cchSrcMax The maximum number of chars (not code points) to
* copy from the source string, not counting the
* terminator as usual.
*/
RTDECL(int) RTStrCopyEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
/**
* String copy with overflow handling and buffer advancing.
*
* @retval VINF_SUCCESS on success.
* @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
* buffer will contain as much of the string as it can hold, fully
* terminated.
*
* @param ppszDst Pointer to the destination buffer pointer.
* This will be advanced to the end of the copied
* bytes (points at the terminator). This is also
* updated on overflow.
* @param pcbDst Pointer to the destination buffer size
* variable. This will be updated in accord with
* the buffer pointer.
* @param pszSrc The source string. NULL is not OK.
*/
RTDECL(int) RTStrCopyP(char **ppszDst, size_t *pcbDst, const char *pszSrc);
/**
* String copy with overflow handling.
*
* @retval VINF_SUCCESS on success.
* @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
* buffer will contain as much of the string as it can hold, fully
* terminated.
*
* @param ppszDst Pointer to the destination buffer pointer.
* This will be advanced to the end of the copied
* bytes (points at the terminator). This is also
* updated on overflow.
* @param pcbDst Pointer to the destination buffer size
* variable. This will be updated in accord with
* the buffer pointer.
* @param pszSrc The source string. NULL is not OK.
* @param cchSrcMax The maximum number of chars (not code points) to
* copy from the source string, not counting the
* terminator as usual.
*/
RTDECL(int) RTStrCopyPEx(char **ppszDst, size_t *pcbDst, const char *pszSrc, size_t cchSrcMax);
/**
* String concatenation with overflow handling.
*
* @retval VINF_SUCCESS on success.
* @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
* buffer will contain as much of the string as it can hold, fully
* terminated.
*
* @param pszDst The destination buffer.
* @param cbDst The size of the destination buffer (in bytes).
* @param pszSrc The source string. NULL is not OK.
*/
RTDECL(int) RTStrCat(char *pszDst, size_t cbDst, const char *pszSrc);
/**
* String concatenation with overflow handling.
*
* @retval VINF_SUCCESS on success.
* @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
* buffer will contain as much of the string as it can hold, fully
* terminated.
*
* @param pszDst The destination buffer.
* @param cbDst The size of the destination buffer (in bytes).
* @param pszSrc The source string. NULL is not OK.
* @param cchSrcMax The maximum number of chars (not code points) to
* copy from the source string, not counting the
* terminator as usual.
*/
RTDECL(int) RTStrCatEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
/**
* String concatenation with overflow handling.
*
* @retval VINF_SUCCESS on success.
* @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
* buffer will contain as much of the string as it can hold, fully
* terminated.
*
* @param ppszDst Pointer to the destination buffer pointer.
* This will be advanced to the end of the copied
* bytes (points at the terminator). This is also
* updated on overflow.
* @param pcbDst Pointer to the destination buffer size
* variable. This will be updated in accord with
* the buffer pointer.
* @param pszSrc The source string. NULL is not OK.
*/
RTDECL(int) RTStrCatP(char **ppszDst, size_t *pcbDst, const char *pszSrc);
/**
* String concatenation with overflow handling and buffer advancing.
*
* @retval VINF_SUCCESS on success.
* @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
* buffer will contain as much of the string as it can hold, fully
* terminated.
*
* @param ppszDst Pointer to the destination buffer pointer.
* This will be advanced to the end of the copied
* bytes (points at the terminator). This is also
* updated on overflow.
* @param pcbDst Pointer to the destination buffer size
* variable. This will be updated in accord with
* the buffer pointer.
* @param pszSrc The source string. NULL is not OK.
* @param cchSrcMax The maximum number of chars (not code points) to
* copy from the source string, not counting the
* terminator as usual.
*/
RTDECL(int) RTStrCatPEx(char **ppszDst, size_t *pcbDst, const char *pszSrc, size_t cchSrcMax);
/**
* Performs a case sensitive string compare between two UTF-8 strings.
*
* Encoding errors are ignored by the current implementation. So, the only
* difference between this and the CRT strcmp function is the handling of
* NULL arguments.
*
* @returns < 0 if the first string less than the second string.
* @returns 0 if the first string identical to the second string.
* @returns > 0 if the first string greater than the second string.
* @param psz1 First UTF-8 string. Null is allowed.
* @param psz2 Second UTF-8 string. Null is allowed.
*/
RTDECL(int) RTStrCmp(const char *psz1, const char *psz2);
/**
* Performs a case sensitive string compare between two UTF-8 strings, given
* a maximum string length.
*
* Encoding errors are ignored by the current implementation. So, the only
* difference between this and the CRT strncmp function is the handling of
* NULL arguments.
*
* @returns < 0 if the first string less than the second string.
* @returns 0 if the first string identical to the second string.
* @returns > 0 if the first string greater than the second string.
* @param psz1 First UTF-8 string. Null is allowed.
* @param psz2 Second UTF-8 string. Null is allowed.
* @param cchMax The maximum string length
*/
RTDECL(int) RTStrNCmp(const char *psz1, const char *psz2, size_t cchMax);
/**
* Performs a case insensitive string compare between two UTF-8 strings.
*
* This is a simplified compare, as only the simplified lower/upper case folding
* specified by the unicode specs are used. It does not consider character pairs
* as they are used in some languages, just simple upper & lower case compares.
*
* The result is the difference between the mismatching codepoints after they
* both have been lower cased.
*
* If the string encoding is invalid the function will assert (strict builds)
* and use RTStrCmp for the remainder of the string.
*
* @returns < 0 if the first string less than the second string.
* @returns 0 if the first string identical to the second string.
* @returns > 0 if the first string greater than the second string.
* @param psz1 First UTF-8 string. Null is allowed.
* @param psz2 Second UTF-8 string. Null is allowed.
*/
RTDECL(int) RTStrICmp(const char *psz1, const char *psz2);
/**
* Performs a case insensitive string compare between two UTF-8 strings, given a
* maximum string length.
*
* This is a simplified compare, as only the simplified lower/upper case folding
* specified by the unicode specs are used. It does not consider character pairs
* as they are used in some languages, just simple upper & lower case compares.
*
* The result is the difference between the mismatching codepoints after they
* both have been lower cased.
*
* If the string encoding is invalid the function will assert (strict builds)
* and use RTStrNCmp for the remainder of the string.
*
* @returns < 0 if the first string less than the second string.
* @returns 0 if the first string identical to the second string.
* @returns > 0 if the first string greater than the second string.
* @param psz1 First UTF-8 string. Null is allowed.
* @param psz2 Second UTF-8 string. Null is allowed.
* @param cchMax Maximum string length
*/
RTDECL(int) RTStrNICmp(const char *psz1, const char *psz2, size_t cchMax);
/**
* Performs a case insensitive string compare between a UTF-8 string and a 7-bit
* ASCII string.
*
* This is potentially faster than RTStrICmp and drags in less dependencies. It
* is really handy for hardcoded inputs.
*
* If the string encoding is invalid the function will assert (strict builds)
* and use RTStrCmp for the remainder of the string.
*
* @returns < 0 if the first string less than the second string.
* @returns 0 if the first string identical to the second string.
* @returns > 0 if the first string greater than the second string.
* @param psz1 First UTF-8 string. Null is allowed.
* @param psz2 Second string, 7-bit ASCII. Null is allowed.
* @sa RTStrICmp, RTUtf16ICmpAscii
*/
RTDECL(int) RTStrICmpAscii(const char *psz1, const char *psz2);
/**
* Performs a case insensitive string compare between a UTF-8 string and a 7-bit
* ASCII string, given a maximum string length.
*
* This is potentially faster than RTStrNICmp and drags in less dependencies.
* It is really handy for hardcoded inputs.
*
* If the string encoding is invalid the function will assert (strict builds)
* and use RTStrNCmp for the remainder of the string.
*
* @returns < 0 if the first string less than the second string.
* @returns 0 if the first string identical to the second string.
* @returns > 0 if the first string greater than the second string.
* @param psz1 First UTF-8 string. Null is allowed.
* @param psz2 Second string, 7-bit ASCII. Null is allowed.
* @param cchMax Maximum string length
* @sa RTStrNICmp, RTUtf16NICmpAscii
*/
RTDECL(int) RTStrNICmpAscii(const char *psz1, const char *psz2, size_t cchMax);
/**
* Checks whether @a pszString starts with @a pszStart.
*
* @returns true / false.
* @param pszString The string to check.
* @param pszStart The start string to check for.
*/
RTDECL(bool) RTStrStartsWith(const char *pszString, const char *pszStart);
/**
* Checks whether @a pszString starts with @a pszStart, case insensitive.
*
* @returns true / false.
* @param pszString The string to check.
* @param pszStart The start string to check for.
*/
RTDECL(bool) RTStrIStartsWith(const char *pszString, const char *pszStart);
/**
* Splits a string buffer with a given separator into separate strings.
* If no separators are found, no strings are returned. Consequtive separators will be skipped.
*
* @returns iprt status code.
* @param pcszStrings String buffer to split.
* @param cbStrings Size (in bytes) of string buffer to split, including terminator.
* @param pcszSeparator Separator to use / find for splitting strings.
* @param ppapszStrings Where to return the allocated string array on success. Needs to be free'd by the caller.
* @param pcStrings Where to return the number of split strings in \a ppapszStrings.
*/
RTDECL(int) RTStrSplit(const char *pcszStrings, size_t cbStrings,
const char *pcszSeparator, char ***ppapszStrings, size_t *pcStrings);
/**
* Locates a case sensitive substring.
*
* If any of the two strings are NULL, then NULL is returned. If the needle is
* an empty string, then the haystack is returned (i.e. matches anything).
*
* @returns Pointer to the first occurrence of the substring if found, NULL if
* not.
*
* @param pszHaystack The string to search.
* @param pszNeedle The substring to search for.
*
* @remarks The difference between this and strstr is the handling of NULL
* pointers.
*/
RTDECL(char *) RTStrStr(const char *pszHaystack, const char *pszNeedle);
/**
* Locates a case insensitive substring.
*
* If any of the two strings are NULL, then NULL is returned. If the needle is
* an empty string, then the haystack is returned (i.e. matches anything).
*
* @returns Pointer to the first occurrence of the substring if found, NULL if
* not.
*
* @param pszHaystack The string to search.
* @param pszNeedle The substring to search for.
*
*/
RTDECL(char *) RTStrIStr(const char *pszHaystack, const char *pszNeedle);
/**
* Converts the string to lower case.
*
* @returns Pointer to the converted string.
* @param psz The string to convert.
*/
RTDECL(char *) RTStrToLower(char *psz);
/**
* Converts the string to upper case.
*
* @returns Pointer to the converted string.
* @param psz The string to convert.
*/
RTDECL(char *) RTStrToUpper(char *psz);
/**
* Checks if the string is case foldable, i.e. whether it would change if
* subject to RTStrToLower or RTStrToUpper.
*
* @returns true / false
* @param psz The string in question.
*/
RTDECL(bool) RTStrIsCaseFoldable(const char *psz);
/**
* Checks if the string is upper cased (no lower case chars in it).
*
* @returns true / false
* @param psz The string in question.
*/
RTDECL(bool) RTStrIsUpperCased(const char *psz);
/**
* Checks if the string is lower cased (no upper case chars in it).
*
* @returns true / false
* @param psz The string in question.
*/
RTDECL(bool) RTStrIsLowerCased(const char *psz);
/**
* Find the length of a zero-terminated byte string, given
* a max string length.
*
* See also RTStrNLenEx.
*
* @returns The string length or cbMax. The returned length does not include
* the zero terminator if it was found.
*
* @param pszString The string.
* @param cchMax The max string length.
*/
RTDECL(size_t) RTStrNLen(const char *pszString, size_t cchMax);
/**
* Find the length of a zero-terminated byte string, given
* a max string length.
*
* See also RTStrNLen.
*
* @returns IPRT status code.
* @retval VINF_SUCCESS if the string has a length less than cchMax.
* @retval VERR_BUFFER_OVERFLOW if the end of the string wasn't found
* before cchMax was reached.
*
* @param pszString The string.
* @param cchMax The max string length.
* @param pcch Where to store the string length excluding the
* terminator. This is set to cchMax if the terminator
* isn't found.
*/
RTDECL(int) RTStrNLenEx(const char *pszString, size_t cchMax, size_t *pcch);
/** The maximum size argument of a memchr call. */
#define RTSTR_MEMCHR_MAX ((~(size_t)0 >> 1) - 15)
/**
* Find the zero terminator in a string with a limited length.
*
* @returns Pointer to the zero terminator.
* @returns NULL if the zero terminator was not found.
*
* @param pszString The string.
* @param cchMax The max string length. RTSTR_MAX is fine.
*/
RTDECL(char *) RTStrEnd(char const *pszString, size_t cchMax);
/**
* Finds the offset at which a simple character first occurs in a string.
*
* @returns The offset of the first occurence or the terminator offset.
* @param pszHaystack The string to search.
* @param chNeedle The character to search for.
*/
DECLINLINE(size_t) RTStrOffCharOrTerm(const char *pszHaystack, char chNeedle)
{
const char *psz = pszHaystack;
char ch;
while ( (ch = *psz) != chNeedle
&& ch != '\0')
psz++;
return (size_t)(psz - pszHaystack);
}
/**
* Matches a simple string pattern.
*
* @returns true if the string matches the pattern, otherwise false.
*
* @param pszPattern The pattern. Special chars are '*' and '?', where the
* asterisk matches zero or more characters and question
* mark matches exactly one character.
* @param pszString The string to match against the pattern.
*/
RTDECL(bool) RTStrSimplePatternMatch(const char *pszPattern, const char *pszString);
/**
* Matches a simple string pattern, neither which needs to be zero terminated.
*
* This is identical to RTStrSimplePatternMatch except that you can optionally
* specify the length of both the pattern and the string. The function will
* stop when it hits a string terminator or either of the lengths.
*
* @returns true if the string matches the pattern, otherwise false.
*
* @param pszPattern The pattern. Special chars are '*' and '?', where the
* asterisk matches zero or more characters and question
* mark matches exactly one character.
* @param cchPattern The pattern length. Pass RTSTR_MAX if you don't know the
* length and wish to stop at the string terminator.
* @param pszString The string to match against the pattern.
* @param cchString The string length. Pass RTSTR_MAX if you don't know the
* length and wish to match up to the string terminator.
*/
RTDECL(bool) RTStrSimplePatternNMatch(const char *pszPattern, size_t cchPattern,
const char *pszString, size_t cchString);
/**
* Matches multiple patterns against a string.
*
* The patterns are separated by the pipe character (|).
*
* @returns true if the string matches the pattern, otherwise false.
*
* @param pszPatterns The patterns.
* @param cchPatterns The lengths of the patterns to use. Pass RTSTR_MAX to
* stop at the terminator.
* @param pszString The string to match against the pattern.
* @param cchString The string length. Pass RTSTR_MAX stop stop at the
* terminator.
* @param poffPattern Offset into the patterns string of the patttern that
* matched. If no match, this will be set to RTSTR_MAX.
* This is optional, NULL is fine.
*/
RTDECL(bool) RTStrSimplePatternMultiMatch(const char *pszPatterns, size_t cchPatterns,
const char *pszString, size_t cchString,
size_t *poffPattern);
/**
* Compares two version strings RTStrICmp fashion.
*
* The version string is split up into sections at punctuation, spaces,
* underscores, dashes and plus signs. The sections are then split up into
* numeric and string sub-sections. Finally, the sub-sections are compared
* in a numeric or case insesntivie fashion depending on what they are.
*
* The following strings are considered to be equal: "1.0.0", "1.00.0", "1.0",
* "1". These aren't: "1.0.0r993", "1.0", "1.0r993", "1.0_Beta3", "1.1"
*
* @returns < 0 if the first string less than the second string.
* @returns 0 if the first string identical to the second string.
* @returns > 0 if the first string greater than the second string.
*
* @param pszVer1 First version string to compare.
* @param pszVer2 Second version string to compare first version with.
*/
RTDECL(int) RTStrVersionCompare(const char *pszVer1, const char *pszVer2);
/** @defgroup rt_str_conv String To/From Number Conversions
* @{ */
/**
* Converts a string representation of a number to a 64-bit unsigned number.
*
* @returns iprt status code.
* Warnings are used to indicate conversion problems.
* @retval VWRN_NUMBER_TOO_BIG
* @retval VWRN_NEGATIVE_UNSIGNED
* @retval VWRN_TRAILING_CHARS
* @retval VWRN_TRAILING_SPACES
* @retval VINF_SUCCESS
* @retval VERR_NO_DIGITS
*
* @param pszValue Pointer to the string value.
* @param ppszNext Where to store the pointer to the first char
* following the number. (Optional)
* @param uBaseAndMaxLen The low byte is the base of the representation, the
* upper 24 bits are the max length to parse. If the base
* is zero the function will look for known prefixes before
* defaulting to 10. A max length of zero means no length
* restriction.
* @param pu64 Where to store the converted number. (optional)
*/
RTDECL(int) RTStrToUInt64Ex(const char *pszValue, char **ppszNext, unsigned uBaseAndMaxLen, uint64_t *pu64);
/**
* Converts a string representation of a number to a 64-bit unsigned number,
* making sure the full string is converted.
*
* @returns iprt status code.
* Warnings are used to indicate conversion problems.
* @retval VWRN_NUMBER_TOO_BIG
* @retval VWRN_NEGATIVE_UNSIGNED
* @retval VINF_SUCCESS
* @retval VERR_NO_DIGITS
* @retval VERR_TRAILING_SPACES
* @retval VERR_TRAILING_CHARS
*
* @param pszValue Pointer to the string value.
* @param uBaseAndMaxLen The low byte is the base of the representation, the
* upper 24 bits are the max length to parse. If the base
* is zero the function will look for known prefixes before
* defaulting to 10. A max length of zero means no length
* restriction.
* @param pu64 Where to store the converted number. (optional)
*/
RTDECL(int) RTStrToUInt64Full(const char *pszValue, unsigned uBaseAndMaxLen, uint64_t *pu64);
/**
* Converts a string representation of a number to a 64-bit unsigned number.
* The base is guessed.
*
* @returns 64-bit unsigned number on success.
* @returns 0 on failure.
* @param pszValue Pointer to the string value.
*/
RTDECL(uint64_t) RTStrToUInt64(const char *pszValue);
/**
* Converts a string representation of a number to a 32-bit unsigned number.
*
* @returns iprt status code.
* Warnings are used to indicate conversion problems.
* @retval VWRN_NUMBER_TOO_BIG
* @retval VWRN_NEGATIVE_UNSIGNED
* @retval VWRN_TRAILING_CHARS
* @retval VWRN_TRAILING_SPACES
* @retval VINF_SUCCESS
* @retval VERR_NO_DIGITS
*
* @param pszValue Pointer to the string value.
* @param ppszNext Where to store the pointer to the first char
* following the number. (Optional)
* @param uBaseAndMaxLen The low byte is the base of the representation, the
* upper 24 bits are the max length to parse. If the base
* is zero the function will look for known prefixes before
* defaulting to 10. A max length of zero means no length
* restriction.
* @param pu32 Where to store the converted number. (optional)
*/
RTDECL(int) RTStrToUInt32Ex(const char *pszValue, char **ppszNext, unsigned uBaseAndMaxLen, uint32_t *pu32);
/**
* Converts a string representation of a number to a 32-bit unsigned number,
* making sure the full string is converted.
*
* @returns iprt status code.
* Warnings are used to indicate conversion problems.
* @retval VWRN_NUMBER_TOO_BIG
* @retval VWRN_NEGATIVE_UNSIGNED
* @retval VINF_SUCCESS
* @retval VERR_NO_DIGITS
* @retval VERR_TRAILING_SPACES
* @retval VERR_TRAILING_CHARS
*
* @param pszValue Pointer to the string value.
* @param uBaseAndMaxLen The low byte is the base of the representation, the
* upper 24 bits are the max length to parse. If the base
* is zero the function will look for known prefixes before
* defaulting to 10. A max length of zero means no length
* restriction.
* @param pu32 Where to store the converted number. (optional)
*/
RTDECL(int) RTStrToUInt32Full(const char *pszValue, unsigned uBaseAndMaxLen, uint32_t *pu32);
/**
* Converts a string representation of a number to a 32-bit unsigned number.
* The base is guessed.
*
* @returns 32-bit unsigned number on success.
* @returns 0 on failure.
* @param pszValue Pointer to the string value.
*/
RTDECL(uint32_t) RTStrToUInt32(const char *pszValue);
/**
* Converts a string representation of a number to a 16-bit unsigned number.
*
* @returns iprt status code.
* Warnings are used to indicate conversion problems.
* @retval VWRN_NUMBER_TOO_BIG
* @retval VWRN_NEGATIVE_UNSIGNED
* @retval VWRN_TRAILING_CHARS
* @retval VWRN_TRAILING_SPACES
* @retval VINF_SUCCESS
* @retval VERR_NO_DIGITS
*
* @param pszValue Pointer to the string value.
* @param ppszNext Where to store the pointer to the first char
* following the number. (Optional)
* @param uBaseAndMaxLen The low byte is the base of the representation, the
* upper 24 bits are the max length to parse. If the base
* is zero the function will look for known prefixes before
* defaulting to 10. A max length of zero means no length
* restriction.
* @param pu16 Where to store the converted number. (optional)
*/
RTDECL(int) RTStrToUInt16Ex(const char *pszValue, char **ppszNext, unsigned uBaseAndMaxLen, uint16_t *pu16);
/**
* Converts a string representation of a number to a 16-bit unsigned number,
* making sure the full string is converted.
*
* @returns iprt status code.
* Warnings are used to indicate conversion problems.
* @retval VWRN_NUMBER_TOO_BIG
* @retval VWRN_NEGATIVE_UNSIGNED
* @retval VINF_SUCCESS
* @retval VERR_NO_DIGITS
* @retval VERR_TRAILING_SPACES
* @retval VERR_TRAILING_CHARS
*
* @param pszValue Pointer to the string value.
* @param uBaseAndMaxLen The low byte is the base of the representation, the
* upper 24 bits are the max length to parse. If the base
* is zero the function will look for known prefixes before
* defaulting to 10. A max length of zero means no length
* restriction.
* @param pu16 Where to store the converted number. (optional)
*/
RTDECL(int) RTStrToUInt16Full(const char *pszValue, unsigned uBaseAndMaxLen, uint16_t *pu16);
/**
* Converts a string representation of a number to a 16-bit unsigned number.
* The base is guessed.
*
* @returns 16-bit unsigned number on success.
* @returns 0 on failure.
* @param pszValue Pointer to the string value.
*/
RTDECL(uint16_t) RTStrToUInt16(const char *pszValue);
/**
* Converts a string representation of a number to a 8-bit unsigned number.
*
* @returns iprt status code.
* Warnings are used to indicate conversion problems.
* @retval VWRN_NUMBER_TOO_BIG
* @retval VWRN_NEGATIVE_UNSIGNED
* @retval VWRN_TRAILING_CHARS
* @retval VWRN_TRAILING_SPACES
* @retval VINF_SUCCESS
* @retval VERR_NO_DIGITS
*
* @param pszValue Pointer to the string value.
* @param ppszNext Where to store the pointer to the first char
* following the number. (Optional)
* @param uBaseAndMaxLen The low byte is the base of the representation, the
* upper 24 bits are the max length to parse. If the base
* is zero the function will look for known prefixes before
* defaulting to 10. A max length of zero means no length
* restriction.
* @param pu8 Where to store the converted number. (optional)
*/
RTDECL(int) RTStrToUInt8Ex(const char *pszValue, char **ppszNext, unsigned uBaseAndMaxLen, uint8_t *pu8);
/**
* Converts a string representation of a number to a 8-bit unsigned number,
* making sure the full string is converted.
*
* @returns iprt status code.
* Warnings are used to indicate conversion problems.
* @retval VWRN_NUMBER_TOO_BIG
* @retval VWRN_NEGATIVE_UNSIGNED
* @retval VINF_SUCCESS
* @retval VERR_NO_DIGITS
* @retval VERR_TRAILING_SPACES
* @retval VERR_TRAILING_CHARS
*
* @param pszValue Pointer to the string value.
* @param uBaseAndMaxLen The low byte is the base of the representation, the
* upper 24 bits are the max length to parse. If the base
* is zero the function will look for known prefixes before
* defaulting to 10. A max length of zero means no length
* restriction.
* @param pu8 Where to store the converted number. (optional)
*/
RTDECL(int) RTStrToUInt8Full(const char *pszValue, unsigned uBaseAndMaxLen, uint8_t *pu8);
/**
* Converts a string representation of a number to a 8-bit unsigned number.
* The base is guessed.
*
* @returns 8-bit unsigned number on success.
* @returns 0 on failure.
* @param pszValue Pointer to the string value.
*/
RTDECL(uint8_t) RTStrToUInt8(const char *pszValue);
/**
* Converts a string representation of a number to a 64-bit signed number.
*
* @returns iprt status code.
* Warnings are used to indicate conversion problems.
* @retval VWRN_NUMBER_TOO_BIG
* @retval VWRN_TRAILING_CHARS
* @retval VWRN_TRAILING_SPACES
* @retval VINF_SUCCESS
* @retval VERR_NO_DIGITS
*
* @param pszValue Pointer to the string value.
* @param ppszNext Where to store the pointer to the first char
* following the number. (Optional)
* @param uBaseAndMaxLen The low byte is the base of the representation, the
* upper 24 bits are the max length to parse. If the base
* is zero the function will look for known prefixes before
* defaulting to 10. A max length of zero means no length
* restriction.
* @param pi64 Where to store the converted number. (optional)
*/
RTDECL(int) RTStrToInt64Ex(const char *pszValue, char **ppszNext, unsigned uBaseAndMaxLen, int64_t *pi64);
/**
* Converts a string representation of a number to a 64-bit signed number,
* making sure the full string is converted.
*
* @returns iprt status code.
* Warnings are used to indicate conversion problems.
* @retval VWRN_NUMBER_TOO_BIG
* @retval VINF_SUCCESS
* @retval VERR_TRAILING_CHARS
* @retval VERR_TRAILING_SPACES
* @retval VERR_NO_DIGITS
*
* @param pszValue Pointer to the string value.
* @param uBaseAndMaxLen The low byte is the base of the representation, the
* upper 24 bits are the max length to parse. If the base
* is zero the function will look for known prefixes before
* defaulting to 10. A max length of zero means no length
* restriction.
* @param pi64 Where to store the converted number. (optional)
*/
RTDECL(int) RTStrToInt64Full(const char *pszValue, unsigned uBaseAndMaxLen, int64_t *pi64);
/**
* Converts a string representation of a number to a 64-bit signed number.
* The base is guessed.
*
* @returns 64-bit signed number on success.
* @returns 0 on failure.
* @param pszValue Pointer to the string value.
*/
RTDECL(int64_t) RTStrToInt64(const char *pszValue);
/**
* Converts a string representation of a number to a 32-bit signed number.
*
* @returns iprt status code.
* Warnings are used to indicate conversion problems.
* @retval VWRN_NUMBER_TOO_BIG
* @retval VWRN_TRAILING_CHARS
* @retval VWRN_TRAILING_SPACES
* @retval VINF_SUCCESS
* @retval VERR_NO_DIGITS
*
* @param pszValue Pointer to the string value.
* @param ppszNext Where to store the pointer to the first char
* following the number. (Optional)
* @param uBaseAndMaxLen The low byte is the base of the representation, the
* upper 24 bits are the max length to parse. If the base
* is zero the function will look for known prefixes before
* defaulting to 10. A max length of zero means no length
* restriction.
* @param pi32 Where to store the converted number. (optional)
*/
RTDECL(int) RTStrToInt32Ex(const char *pszValue, char **ppszNext, unsigned uBaseAndMaxLen, int32_t *pi32);
/**
* Converts a string representation of a number to a 32-bit signed number,
* making sure the full string is converted.
*
* @returns iprt status code.
* Warnings are used to indicate conversion problems.
* @retval VWRN_NUMBER_TOO_BIG
* @retval VINF_SUCCESS
* @retval VERR_TRAILING_CHARS
* @retval VERR_TRAILING_SPACES
* @retval VERR_NO_DIGITS
*
* @param pszValue Pointer to the string value.
* @param uBaseAndMaxLen The low byte is the base of the representation, the
* upper 24 bits are the max length to parse. If the base
* is zero the function will look for known prefixes before
* defaulting to 10. A max length of zero means no length
* restriction.
* @param pi32 Where to store the converted number. (optional)
*/
RTDECL(int) RTStrToInt32Full(const char *pszValue, unsigned uBaseAndMaxLen, int32_t *pi32);
/**
* Converts a string representation of a number to a 32-bit signed number.
* The base is guessed.
*
* @returns 32-bit signed number on success.
* @returns 0 on failure.
* @param pszValue Pointer to the string value.
*/
RTDECL(int32_t) RTStrToInt32(const char *pszValue);
/**
* Converts a string representation of a number to a 16-bit signed number.
*
* @returns iprt status code.
* Warnings are used to indicate conversion problems.
* @retval VWRN_NUMBER_TOO_BIG
* @retval VWRN_TRAILING_CHARS
* @retval VWRN_TRAILING_SPACES
* @retval VINF_SUCCESS
* @retval VERR_NO_DIGITS
*
* @param pszValue Pointer to the string value.
* @param ppszNext Where to store the pointer to the first char
* following the number. (Optional)
* @param uBaseAndMaxLen The low byte is the base of the representation, the
* upper 24 bits are the max length to parse. If the base
* is zero the function will look for known prefixes before
* defaulting to 10. A max length of zero means no length
* restriction.
* @param pi16 Where to store the converted number. (optional)
*/
RTDECL(int) RTStrToInt16Ex(const char *pszValue, char **ppszNext, unsigned uBaseAndMaxLen, int16_t *pi16);
/**
* Converts a string representation of a number to a 16-bit signed number,
* making sure the full string is converted.
*
* @returns iprt status code.
* Warnings are used to indicate conversion problems.
* @retval VWRN_NUMBER_TOO_BIG
* @retval VINF_SUCCESS
* @retval VERR_TRAILING_CHARS
* @retval VERR_TRAILING_SPACES
* @retval VERR_NO_DIGITS
*
* @param pszValue Pointer to the string value.
* @param uBaseAndMaxLen The low byte is the base of the representation, the
* upper 24 bits are the max length to parse. If the base
* is zero the function will look for known prefixes before
* defaulting to 10. A max length of zero means no length
* restriction.
* @param pi16 Where to store the converted number. (optional)
*/
RTDECL(int) RTStrToInt16Full(const char *pszValue, unsigned uBaseAndMaxLen, int16_t *pi16);
/**
* Converts a string representation of a number to a 16-bit signed number.
* The base is guessed.
*
* @returns 16-bit signed number on success.
* @returns 0 on failure.
* @param pszValue Pointer to the string value.
*/
RTDECL(int16_t) RTStrToInt16(const char *pszValue);
/**
* Converts a string representation of a number to a 8-bit signed number.
*
* @returns iprt status code.
* Warnings are used to indicate conversion problems.
* @retval VWRN_NUMBER_TOO_BIG
* @retval VWRN_TRAILING_CHARS
* @retval VWRN_TRAILING_SPACES
* @retval VINF_SUCCESS
* @retval VERR_NO_DIGITS
*
* @param pszValue Pointer to the string value.
* @param ppszNext Where to store the pointer to the first char
* following the number. (Optional)
* @param uBaseAndMaxLen The low byte is the base of the representation, the
* upper 24 bits are the max length to parse. If the base
* is zero the function will look for known prefixes before
* defaulting to 10. A max length of zero means no length
* restriction.
* @param pi8 Where to store the converted number. (optional)
*/
RTDECL(int) RTStrToInt8Ex(const char *pszValue, char **ppszNext, unsigned uBaseAndMaxLen, int8_t *pi8);
/**
* Converts a string representation of a number to a 8-bit signed number,
* making sure the full string is converted.
*
* @returns iprt status code.
* Warnings are used to indicate conversion problems.
* @retval VWRN_NUMBER_TOO_BIG
* @retval VINF_SUCCESS
* @retval VERR_TRAILING_CHARS
* @retval VERR_TRAILING_SPACES
* @retval VERR_NO_DIGITS
*
* @param pszValue Pointer to the string value.
* @param uBaseAndMaxLen The low byte is the base of the representation, the
* upper 24 bits are the max length to parse. If the base
* is zero the function will look for known prefixes before
* defaulting to 10. A max length of zero means no length
* restriction.
* @param pi8 Where to store the converted number. (optional)
*/
RTDECL(int) RTStrToInt8Full(const char *pszValue, unsigned uBaseAndMaxLen, int8_t *pi8);
/**
* Converts a string representation of a number to a 8-bit signed number.
* The base is guessed.
*
* @returns 8-bit signed number on success.
* @returns 0 on failure.
* @param pszValue Pointer to the string value.
*/
RTDECL(int8_t) RTStrToInt8(const char *pszValue);
/**
* Converts a string to long double floating point, extended edition.
*
* Please note that long double can be double precision, extended precision, or
* quad precision floating point depending on the platform and architecture. See
* RT_COMPILER_WITH_128BIT_LONG_DOUBLE and RT_COMPILER_WITH_80BIT_LONG_DOUBLE.
*
* @returns IPRT status code.
* @retval VERR_NO_DIGITS if no valid digits found.
* @retval VWRN_FLOAT_UNDERFLOW on underflow with denormal/subnormal return
* value
* @retval VERR_FLOAT_UNDERFLOW on underflow, value set to +/- zero.
* @retval VERR_FLOAT_OVERFLOW on overflow, value set to +/- infinity.
* @retval VWRN_TRAILING_CHARS
* @retval VWRN_TRAILING_SPACES
*
* @param pszValue The string to parse.
* @param ppszNext Where to store the pointer to the first char following
* the number. Optional.
* @param cchMax Max number of character to parse. Zero means unlimited.
* @param plrd Where to return the number. Optional.
*
* @note This code isn't entirely perfect yet. It could exhibit rounding
* differences compared to strtold & the compiler, and extreme value
* may overflow/underflow prematurely depending on the build config.
*/
RTDECL(int) RTStrToLongDoubleEx(const char *pszValue, char **ppszNext, size_t cchMax, long double *plrd);
/**
* Converts a string to double precision floating point, extended edition.
*
* @returns IPRT status code.
* @retval VERR_NO_DIGITS if no valid digits found.
* @retval VWRN_FLOAT_UNDERFLOW on underflow with denormal/subnormal return
* value
* @retval VERR_FLOAT_UNDERFLOW on underflow, value set to +/- zero.
* @retval VERR_FLOAT_OVERFLOW on overflow, value set to +/- infinity.
* @retval VWRN_TRAILING_CHARS
* @retval VWRN_TRAILING_SPACES
*
* @param pszValue The string to parse.
* @param ppszNext Where to store the pointer to the first char following
* the number. Optional.
* @param cchMax Max number of character to parse. Zero means unlimited.
* @param prd Where to return the number. Optional.
*
* @note This code isn't entirely perfect yet. It could exhibit rounding
* differences compared to strtold & the compiler, and extreme value
* may overflow/underflow prematurely depending on the build config.
*/
RTDECL(int) RTStrToDoubleEx(const char *pszValue, char **ppszNext, size_t cchMax, double *prd);
/**
* Converts a string to single precision floating point, extended edition.
*
* @returns IPRT status code.
* @retval VERR_NO_DIGITS if no valid digits found.
* @retval VWRN_FLOAT_UNDERFLOW on underflow with denormal/subnormal return
* value
* @retval VERR_FLOAT_UNDERFLOW on underflow, value set to +/- zero.
* @retval VERR_FLOAT_OVERFLOW on overflow, value set to +/- infinity.
* @retval VWRN_TRAILING_CHARS
* @retval VWRN_TRAILING_SPACES
*
* @param pszValue The string to parse.
* @param ppszNext Where to store the pointer to the first char following
* the number. Optional.
* @param cchMax Max number of character to parse. Zero means unlimited.
* @param pr Where to return the number. Optional.
*
* @note This code isn't entirely perfect yet. It could exhibit rounding
* differences compared to strtold & the compiler, and extreme value
* may overflow/underflow prematurely depending on the build config.
*/
RTDECL(int) RTStrToFloatEx(const char *pszValue, char **ppszNext, size_t cchMax, float *pr);
/**
* Gets a long double NaN.
*
* @returns NaN value.
* @param pszTag Optional NaN tag for modifying the NaN value. We
* recognizes a string of hex digits for inserting into the
* fraction part. This may be followed 'quiet' or
* 'signaling', ignoring case and requiring at only the
* first character. The two components may be separated by
* zero or more '_' characters. Any other stuff in the tag
* will be ignored.
*
* If the tag is empty or we cannot grok any of it, we'll
* return a default quiet NaN.
* @param fPositive Whether the NaN value should be positive or negative
* (for what that's worth).
*/
RTDECL(long double) RTStrNanLongDouble(const char *pszTag, bool fPositive);
/**
* Gets a double NaN.
*
* @returns NaN value.
* @param pszTag Optional NaN tag for modifying the NaN value. We
* recognizes a string of hex digits for inserting into the
* fraction part. This may be followed 'quiet' or
* 'signaling', ignoring case and requiring at only the
* first character. The two components may be separated by
* zero or more '_' characters. Any other stuff in the tag
* will be ignored.
*
* If the tag is empty or we cannot grok any of it, we'll
* return a default quiet NaN.
* @param fPositive Whether the NaN value should be positive or negative
* (for what that's worth).
*/
RTDECL(double) RTStrNanDouble(const char *pszTag, bool fPositive);
/**
* Gets a float NaN.
*
* @returns NaN value.
* @param pszTag Optional NaN tag for modifying the NaN value. We
* recognizes a string of hex digits for inserting into the
* fraction part. This may be followed 'quiet' or
* 'signaling', ignoring case and requiring at only the
* first character. The two components may be separated by
* zero or more '_' characters. Any other stuff in the tag
* will be ignored.
*
* If the tag is empty or we cannot grok any of it, we'll
* return a default quiet NaN.
* @param fPositive Whether the NaN value should be positive or negative
* (for what that's worth).
*/
RTDECL(float) RTStrNanFloat(const char *pszTag, bool fPositive);
/**
* Formats a buffer stream as hex bytes.
*
* The default is no separating spaces or line breaks or anything.
*
* @returns IPRT status code.
* @retval VERR_INVALID_POINTER if any of the pointers are wrong.
* @retval VERR_BUFFER_OVERFLOW if the buffer is insufficent to hold the bytes.
*
* @param pszBuf Output string buffer.
* @param cbBuf The size of the output buffer.
* @param pv Pointer to the bytes to stringify.
* @param cb The number of bytes to stringify.
* @param fFlags Combination of RTSTRPRINTHEXBYTES_F_XXX values.
* @sa RTUtf16PrintHexBytes.
*/
RTDECL(int) RTStrPrintHexBytes(char *pszBuf, size_t cbBuf, void const *pv, size_t cb, uint32_t fFlags);
/** @name RTSTRPRINTHEXBYTES_F_XXX - flags for RTStrPrintHexBytes and RTUtf16PritnHexBytes.
* @{ */
/** Upper case hex digits, the default is lower case. */
#define RTSTRPRINTHEXBYTES_F_UPPER RT_BIT(0)
/** Add a space between each group. */
#define RTSTRPRINTHEXBYTES_F_SEP_SPACE RT_BIT(1)
/** Add a colon between each group. */
#define RTSTRPRINTHEXBYTES_F_SEP_COLON RT_BIT(2)
/** @} */
/**
* Converts a string of hex bytes back into binary data.
*
* @returns IPRT status code.
* @retval VERR_INVALID_POINTER if any of the pointers are wrong.
* @retval VERR_BUFFER_OVERFLOW if the string contains too many hex bytes.
* @retval VERR_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
* the output buffer.
* @retval VERR_UNEVEN_INPUT if the input contains a half byte.
* @retval VERR_NO_DIGITS
* @retval VWRN_TRAILING_CHARS
* @retval VWRN_TRAILING_SPACES
*
* @param pszHex The string containing the hex bytes.
* @param pv Output buffer.
* @param cb The size of the output buffer.
* @param fFlags RTSTRCONVERTHEXBYTES_F_XXX.
*/
RTDECL(int) RTStrConvertHexBytes(char const *pszHex, void *pv, size_t cb, uint32_t fFlags);
/** @name RTSTRCONVERTHEXBYTES_F_XXX - Flags for RTStrConvertHexBytes() and RTStrConvertHexBytesEx().
* @{ */
/** Accept colon as a byte separator. */
#define RTSTRCONVERTHEXBYTES_F_SEP_COLON RT_BIT(0)
/** @} */
/**
* Converts a string of hex bytes back into binary data, extended version.
*
* @returns IPRT status code.
* @retval VERR_INVALID_POINTER if any of the pointers are wrong.
* @retval VERR_BUFFER_OVERFLOW if the string contains too many hex bytes.
* @retval VERR_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
* the output buffer and *pcbReturned is NULL.
* @retval VINF_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
* the output buffer and *pcbReturned is not NULL, *pcbReturned holds
* the actual number of bytes.
* @retval VERR_UNEVEN_INPUT if the input contains a half byte.
* @retval VERR_NO_DIGITS
* @retval VWRN_TRAILING_CHARS
* @retval VWRN_TRAILING_SPACES
*
* @param pszHex The string containing the hex bytes.
* @param pv Output buffer.
* @param cb The size of the output buffer.
* @param fFlags RTSTRCONVERTHEXBYTES_F_XXX.
* @param ppszNext Set to point at where we stopped decoding hex bytes.
* Optional.
* @param pcbReturned Where to return the number of bytes found. Optional.
*/
RTDECL(int) RTStrConvertHexBytesEx(char const *pszHex, void *pv, size_t cb, uint32_t fFlags,
const char **ppszNext, size_t *pcbReturned);
/** @} */
/** @defgroup rt_str_space Unique String Space
* @{
*/
/** Pointer to a string name space container node core. */
typedef struct RTSTRSPACECORE *PRTSTRSPACECORE;
/** Pointer to a pointer to a string name space container node core. */
typedef PRTSTRSPACECORE *PPRTSTRSPACECORE;
/**
* String name space container node core.
*/
typedef struct RTSTRSPACECORE
{
/** Pointer to the left leaf node. Don't touch. */
PRTSTRSPACECORE pLeft;
/** Pointer to the left right node. Don't touch. */
PRTSTRSPACECORE pRight;
/** Pointer to the list of string with the same hash key value. Don't touch. */
PRTSTRSPACECORE pList;
/** Hash key. Don't touch. */
uint32_t Key;
/** Height of this tree: max(heigth(left), heigth(right)) + 1. Don't touch */
unsigned char uchHeight;
/** The string length. Read only! */
size_t cchString;
/** Pointer to the string. Read only! */
const char *pszString;
} RTSTRSPACECORE;
/** String space. (Initialize with NULL.) */
typedef PRTSTRSPACECORE RTSTRSPACE;
/** Pointer to a string space. */
typedef PPRTSTRSPACECORE PRTSTRSPACE;
/**
* Inserts a string into a unique string space.
*
* @returns true on success.
* @returns false if the string collided with an existing string.
* @param pStrSpace The space to insert it into.
* @param pStr The string node.
*/
RTDECL(bool) RTStrSpaceInsert(PRTSTRSPACE pStrSpace, PRTSTRSPACECORE pStr);
/**
* Removes a string from a unique string space.
*
* @returns Pointer to the removed string node.
* @returns NULL if the string was not found in the string space.
* @param pStrSpace The space to remove it from.
* @param pszString The string to remove.
*/
RTDECL(PRTSTRSPACECORE) RTStrSpaceRemove(PRTSTRSPACE pStrSpace, const char *pszString);
/**
* Gets a string from a unique string space.
*
* @returns Pointer to the string node.
* @returns NULL if the string was not found in the string space.
* @param pStrSpace The space to get it from.
* @param pszString The string to get.
*/
RTDECL(PRTSTRSPACECORE) RTStrSpaceGet(PRTSTRSPACE pStrSpace, const char *pszString);
/**
* Gets a string from a unique string space.
*
* @returns Pointer to the string node.
* @returns NULL if the string was not found in the string space.
* @param pStrSpace The space to get it from.
* @param pszString The string to get.
* @param cchMax The max string length to evaluate. Passing
* RTSTR_MAX is ok and makes it behave just like
* RTStrSpaceGet.
*/
RTDECL(PRTSTRSPACECORE) RTStrSpaceGetN(PRTSTRSPACE pStrSpace, const char *pszString, size_t cchMax);
/**
* Callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy().
*
* @returns 0 on continue.
* @returns Non-zero to aborts the operation.
* @param pStr The string node
* @param pvUser The user specified argument.
*/
typedef DECLCALLBACKTYPE(int, FNRTSTRSPACECALLBACK,(PRTSTRSPACECORE pStr, void *pvUser));
/** Pointer to callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy(). */
typedef FNRTSTRSPACECALLBACK *PFNRTSTRSPACECALLBACK;
/**
* Destroys the string space.
*
* The caller supplies a callback which will be called for each of the string
* nodes in for freeing their memory and other resources.
*
* @returns 0 or what ever non-zero return value pfnCallback returned
* when aborting the destruction.
* @param pStrSpace The space to destroy.
* @param pfnCallback The callback.
* @param pvUser The user argument.
*/
RTDECL(int) RTStrSpaceDestroy(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
/**
* Enumerates the string space.
* The caller supplies a callback which will be called for each of
* the string nodes.
*
* @returns 0 or what ever non-zero return value pfnCallback returned
* when aborting the destruction.
* @param pStrSpace The space to enumerate.
* @param pfnCallback The callback.
* @param pvUser The user argument.
*/
RTDECL(int) RTStrSpaceEnumerate(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
/** @} */
/** @defgroup rt_str_hash Sting hashing
* @{ */
/**
* Hashes the given string using algorithm \#1.
*
* @returns String hash.
* @param pszString The string to hash.
*/
RTDECL(uint32_t) RTStrHash1(const char *pszString);
/**
* Hashes the given string using algorithm \#1.
*
* @returns String hash.
* @param pszString The string to hash.
* @param cchString The max length to hash. Hashing will stop if the
* terminator character is encountered first. Passing
* RTSTR_MAX is fine.
*/
RTDECL(uint32_t) RTStrHash1N(const char *pszString, size_t cchString);
/**
* Hashes the given strings as if they were concatenated using algorithm \#1.
*
* @returns String hash.
* @param cPairs The number of string / length pairs in the
* ellipsis.
* @param ... List of string (const char *) and length
* (size_t) pairs. Passing RTSTR_MAX as the size is
* fine.
*/
RTDECL(uint32_t) RTStrHash1ExN(size_t cPairs, ...);
/**
* Hashes the given strings as if they were concatenated using algorithm \#1.
*
* @returns String hash.
* @param cPairs The number of string / length pairs in the @a va.
* @param va List of string (const char *) and length
* (size_t) pairs. Passing RTSTR_MAX as the size is
* fine.
*/
RTDECL(uint32_t) RTStrHash1ExNV(size_t cPairs, va_list va);
/** @} */
/** @defgroup rt_str_mem Raw memory operations.
*
* @note Following the memchr/memcpy/memcmp/memset tradition and putting these
* in the string.h header rather than in the mem.h one.
*
* @{ */
/**
* Searches @a pvHaystack for a 16-bit sized and aligned @a uNeedle.
*
* @returns Pointer to the first hit if found, NULL if not found.
* @param pvHaystack The memory to search.
* @param uNeedle The 16-bit value to find.
* @param cbHaystack Size of the memory to search.
* @sa memchr, RTStrMemFind32, RTStrMemFind64
*/
RTDECL(uint16_t *) RTStrMemFind16(const void *pvHaystack, uint16_t uNeedle, size_t cbHaystack);
/**
* Searches @a pvHaystack for a 32-bit sized and aligned @a uNeedle.
*
* @returns Pointer to the first hit if found, NULL if not found.
* @param pvHaystack The memory to search.
* @param uNeedle The 32-bit value to find.
* @param cbHaystack Size of the memory to search.
* @sa memchr, RTStrMemFind16, RTStrMemFind64
*/
RTDECL(uint32_t *) RTStrMemFind32(const void *pvHaystack, uint32_t uNeedle, size_t cbHaystack);
/**
* Searches @a pvHaystack for a 64-bit sized and aligned @a uNeedle.
*
* @returns Pointer to the first hit if found, NULL if not found.
* @param pvHaystack The memory to search.
* @param uNeedle The 64-bit value to find.
* @param cbHaystack Size of the memory to search.
* @sa memchr, RTStrMemFind16, RTStrMemFind32
*/
RTDECL(uint64_t *) RTStrMemFind64(const void *pvHaystack, uint64_t uNeedle, size_t cbHaystack);
/** @} */
/** @} */
RT_C_DECLS_END
#endif /* !IPRT_INCLUDED_string_h */
|