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
|
{
*********************************************************************
Copyright (C) 1997, 1998 Gertjan Schouten
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
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.
**********************************************************************
System Utilities For Free Pascal
}
{ NewStr creates a new PString and assigns S to it
if length(s) = 0 NewStr returns Nil }
function NewStr(const S: string): PString;
begin
if (S='') then
Result:=nil
else
begin
new(result);
if (Result<>nil) then
Result^:=s;
end;
end;
{$ifdef dummy}
{ declaring this breaks delphi compatibility and e.g. tw3721.pp }
FUNCTION NewStr (Const S: ShortString): PShortString;
VAR P: PShortString;
BEGIN
If (S = '') Then
P := Nil
Else
Begin { Return nil }
GetMem(P, Length(S) + 1); { Allocate memory }
If (P<>Nil) Then P^ := S; { Hold string }
End;
NewStr := P; { Return result }
END;
{$endif dummy}
{ DisposeStr frees the memory occupied by S }
procedure DisposeStr(S: PString);
begin
if S <> Nil then
begin
dispose(s);
S:=nil;
end;
end;
PROCEDURE DisposeStr (S: PShortString);
BEGIN
If (S <> Nil) Then FreeMem(S, Length(S^) + 1); { Release memory }
END;
{ AssignStr assigns S to P^ }
procedure AssignStr(var P: PString; const S: string);
begin
P^ := s;
end ;
{ AppendStr appends S to Dest }
procedure AppendStr(var Dest: String; const S: string);
begin
Dest := Dest + S;
end ;
function IsLeadChar(C: AnsiChar): Boolean; inline;
begin
Result:=C in LeadBytes;
end;
function IsLeadChar(B: Byte): Boolean; inline;
begin
Result:=Char(B) in LeadBytes;
end;
Function InternalChangeCase(Const S : AnsiString; const Chars: TSysCharSet; const Adjustment: Longint): AnsiString;
var
i : Integer;
P : PChar;
Unique : Boolean;
begin
Result := S;
if Result='' then
exit;
Unique:=false;
P:=PChar(Result);
for i:=1 to Length(Result) do
begin
if CharInSet(P^,Chars) then
begin
if not Unique then
begin
UniqueString(Result);
p:=@Result[i];
Unique:=true;
end;
P^:=Char(Ord(P^)+Adjustment);
end;
Inc(P);
end;
end;
{ UpperCase returns a copy of S where all lowercase characters ( from a to z )
have been converted to uppercase }
Function UpperCase(Const S : AnsiString) : AnsiString;
begin
Result:=InternalChangeCase(S,['a'..'z'],-32);
end;
function UpperCase(const s: string; LocaleOptions: TLocaleOptions): string; overload;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
case LocaleOptions of
loInvariantLocale: Result:=UpperCase(s);
loUserLocale: Result:=AnsiUpperCase(s);
end;
end;
{ LowerCase returns a copy of S where all uppercase characters ( from A to Z )
have been converted to lowercase }
Function Lowercase(Const S : AnsiString) : AnsiString;
begin
Result:=InternalChangeCase(S,['A'..'Z'],32);
end;
function LowerCase(const s: string; LocaleOptions: TLocaleOptions): string; overload;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
case LocaleOptions of
loInvariantLocale: Result:=LowerCase(s);
loUserLocale: Result:=AnsiLowerCase(s);
end;
end;
function LowerCase(const V: variant): string; overload;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
result:=LowerCase(ansistring(V));
end;
{ CompareStr compares S1 and S2, the result is the based on
substraction of the ascii values of the characters in S1 and S2
case result
S1 < S2 < 0
S1 > S2 > 0
S1 = S2 = 0 }
{$IF SIZEOF(SIZEINT)>SIZEOF(INTEGER)}
Function DoCapSizeInt(SI : SizeInt) : Integer; inline;
begin
if (SI<0) then
result:=-1
else if (SI>0) then
result:=1
else
result:=0;
end;
{$DEFINE CAPSIZEINT:=DoCapSizeInt}
{$ELSE}
{$DEFINE CAPSIZEINT:=}
{$ENDIF}
function CompareStr(const S1, S2: string): Integer;
var res,count, count1, count2: SizeInt;
begin
result := 0;
Count1 := Length(S1);
Count2 := Length(S2);
if Count1>Count2 then
Count:=Count2
else
Count:=Count1;
result := CompareMemRange(Pointer(S1),Pointer(S2), Count);
if result=0 then
// CAPSIZEINT is no-op if Sizeof(Sizeint)<=SizeOF(Integer)
result:=CAPSIZEINT(Count1-Count2);
end;
function CompareStr(const S1, S2: string; LocaleOptions: TLocaleOptions): Integer; overload;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
case LocaleOptions of
loInvariantLocale: Result:=CompareStr(S1,S2);
loUserLocale: Result:=AnsiCompareStr(S1,S2);
end;
end;
{ CompareMemRange returns the result of comparison of Length bytes at P1 and P2
case result
P1 < P2 < 0
P1 > P2 > 0
P1 = P2 = 0 }
function CompareMemRange(P1, P2: Pointer; Length: PtrUInt): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
If P1=P2 then
Result:=0
else
Result:=CompareByte(P1^,P2^,Length);
end;
function CompareMem(P1, P2: Pointer; Length: PtrUInt): Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
if P1=P2 then
Result:=True
else
Result:=CompareByte(P1^,P2^,Length)=0;
end;
{ CompareText compares S1 and S2, the result is the based on
substraction of the ascii values of characters in S1 and S2
comparison is case-insensitive
case result
S1 < S2 < 0
S1 > S2 > 0
S1 = S2 = 0 }
function CompareText(const S1, S2: string): Integer; overload;
var
i, count, count1, count2: sizeint;
Chr1, Chr2: byte;
P1, P2: PChar;
begin
Count1 := Length(S1);
Count2 := Length(S2);
if (Count1>Count2) then
Count := Count2
else
Count := Count1;
i := 0;
if count>0 then
begin
P1 := @S1[1];
P2 := @S2[1];
while i < Count do
begin
Chr1 := byte(p1^);
Chr2 := byte(p2^);
if Chr1 <> Chr2 then
begin
if Chr1 in [97..122] then
dec(Chr1,32);
if Chr2 in [97..122] then
dec(Chr2,32);
if Chr1 <> Chr2 then
Break;
end;
Inc(P1); Inc(P2); Inc(I);
end;
end;
if i < Count then
result := Chr1-Chr2
else
// CAPSIZEINT is no-op if Sizeof(Sizeint)<=SizeOF(Integer)
result:=CAPSIZEINT(Count1-Count2);
end;
function CompareText(const S1, S2: string; LocaleOptions: TLocaleOptions): Integer; overload;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
case LocaleOptions of
loInvariantLocale: Result:=CompareText(S1,S2);
loUserLocale: Result:=AnsiCompareText(S1,S2);
end;
end;
function SameText(const s1,s2:String):Boolean; overload;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
Result:=CompareText(S1,S2)=0;
end;
function SameText(const s1,s2:String; LocaleOptions: TLocaleOptions):Boolean; overload;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
case LocaleOptions of
loInvariantLocale: Result:=SameText(S1,S2);
loUserLocale: Result:=AnsiSameText(S1,S2);
end;
end;
function SameStr(const s1,s2:String):Boolean; overload;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
Result:=CompareStr(S1,S2)=0;
end;
function SameStr(const s1,s2:String; LocaleOptions: TLocaleOptions):Boolean; overload;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
case LocaleOptions of
loInvariantLocale: Result:=SameStr(S1,S2);
loUserLocale: Result:=AnsiSameStr(S1,S2);
end;
end;
{$ifndef FPC_NOGENERICANSIROUTINES}
{==============================================================================}
{ Ansi string functions }
{ these functions rely on the character set loaded by the OS }
{==============================================================================}
type
TCaseTranslationTable = array[0..255] of char;
var
{ Tables with upper and lowercase forms of character sets.
MUST be initialized with the correct code-pages }
UpperCaseTable: TCaseTranslationTable;
LowerCaseTable: TCaseTranslationTable;
function GenericAnsiUpperCase(const s: string): string;
var
len, i: integer;
begin
len := length(s);
SetLength(result, len);
for i := 1 to len do
result[i] := UpperCaseTable[ord(s[i])];
end;
function GenericAnsiLowerCase(const s: string): string;
var
len, i: integer;
begin
len := length(s);
SetLength(result, len);
for i := 1 to len do
result[i] := LowerCaseTable[ord(s[i])];
end;
function GenericAnsiCompareStr(const S1, S2: string): PtrInt;
Var
I,L1,L2 : SizeInt;
begin
Result:=0;
L1:=Length(S1);
L2:=Length(S2);
I:=1;
While (Result=0) and ((I<=L1) and (I<=L2)) do
begin
Result:=Ord(S1[I])-Ord(S2[I]); //!! Must be replaced by ansi characters !!
Inc(I);
end;
If Result=0 Then
Result:=L1-L2;
end;
function GenericAnsiCompareText(const S1, S2: string): PtrInt;
Var
I,L1,L2 : SizeInt;
begin
Result:=0;
L1:=Length(S1);
L2:=Length(S2);
I:=1;
While (Result=0) and ((I<=L1) and (I<=L2)) do
begin
Result:=Ord(LowerCaseTable[Ord(S1[I])])-Ord(LowerCaseTable[Ord(S2[I])]); //!! Must be replaced by ansi characters !!
Inc(I);
end;
If Result=0 Then
Result:=L1-L2;
end;
function GenericAnsiStrComp(S1, S2: PChar): PtrInt;
begin
Result:=0;
If S1=Nil then
begin
If S2=Nil Then Exit;
result:=-1;
exit;
end;
If S2=Nil then
begin
Result:=1;
exit;
end;
While (Result=0) and (S1^<>#0) and (S2^<>#0) do begin
Result:=Ord(S1^)-Ord(S2^); //!! Must be replaced by ansi characters !!
Inc(S1);
Inc(S2);
end;
if (Result=0) and (S1^<>S2^) then // loop ended because exactly one has #0
if S1^=#0 then // shorter string is smaller
result:=-1
else
result:=1;
end;
function GenericAnsiStrIComp(S1, S2: PChar): PtrInt;
begin
Result:=0;
If S1=Nil then
begin
If S2=Nil Then Exit;
result:=-1;
exit;
end;
If S2=Nil then
begin
Result:=1;
exit;
end;
While (Result=0) and (S1^<>#0) and (S2^<>#0) do begin
Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
Inc(S1);
Inc(S2);
end;
if (Result=0) and (s1[0]<>s2[0]) then //length(s1)<>length(s2)
if s1[0]=#0 then
Result:=-1 //s1 shorter than s2
else
Result:=1; //s1 longer than s2
end;
function GenericAnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
Var I : PtrUInt;
begin
Result:=0;
If MaxLen=0 then exit;
If S1=Nil then
begin
If S2=Nil Then Exit;
result:=-1;
exit;
end;
If S2=Nil then
begin
Result:=1;
exit;
end;
I:=0;
Repeat
Result:=Ord(S1[0])-Ord(S2[0]); //!! Must be replaced by ansi characters !!
Inc(S1);
Inc(S2);
Inc(I);
Until (Result<>0) or (I=MaxLen)
end;
function GenericAnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
Var I : PtrUInt;
begin
Result:=0;
If MaxLen=0 then exit;
If S1=Nil then
begin
If S2=Nil Then Exit;
result:=-1;
exit;
end;
If S2=Nil then
begin
Result:=1;
exit;
end;
I:=0;
Repeat
Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
Inc(S1);
Inc(S2);
Inc(I);
Until (Result<>0) or (I=MaxLen)
end;
function GenericAnsiStrLower(Str: PChar): PChar;
begin
result := Str;
if Str <> Nil then begin
while Str^ <> #0 do begin
Str^ := LowerCaseTable[byte(Str^)];
Str := Str + 1;
end;
end;
end;
function GenericAnsiStrUpper(Str: PChar): PChar;
begin
result := Str;
if Str <> Nil then begin
while Str^ <> #0 do begin
Str^ := UpperCaseTable[byte(Str^)];
Str := Str + 1;
end ;
end ;
end ;
{$endif FPC_NOGENERICANSIROUTINES}
function AnsiSameText(const s1,s2:String):Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
AnsiSameText:=AnsiCompareText(S1,S2)=0;
end;
function AnsiSameStr(const s1,s2:String):Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
AnsiSameStr:=AnsiCompareStr(S1,S2)=0;
end;
function AnsiLastChar(const S: string): PChar;
begin
//!! No multibyte yet, so we return the last one.
result:=StrEnd(Pchar(pointer(S))); // strend checks for nil
Dec(Result);
end ;
function AnsiStrLastChar(Str: PChar): PChar;
begin
//!! No multibyte yet, so we return the last one.
result:=StrEnd(Str);
Dec(Result);
end ;
function AnsiUpperCase(const s: string): string;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
result:=widestringmanager.UpperAnsiStringProc(s);
end;
function AnsiLowerCase(const s: string): string;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
result:=widestringmanager.LowerAnsiStringProc(s);
end;
function AnsiCompareStr(const S1, S2: string): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
// CAPSIZEINT is no-op if Sizeof(Sizeint)<=SizeOF(Integer)
result:=CAPSIZEINT(widestringmanager.CompareStrAnsiStringProc(s1,s2));
end;
function AnsiCompareText(const S1, S2: string): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
// CAPSIZEINT is no-op if Sizeof(Sizeint)<=SizeOF(Integer)
result:=CAPSIZEINT(widestringmanager.CompareTextAnsiStringProc(s1,s2));
end;
function AnsiStrComp(S1, S2: PChar): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
// CAPSIZEINT is no-op if Sizeof(Sizeint)<=SizeOF(Integer)
result:=CAPSIZEINT(widestringmanager.StrCompAnsiStringProc(s1,s2));
end;
function AnsiStrIComp(S1, S2: PChar): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
// CAPSIZEINT is no-op if Sizeof(Sizeint)<=SizeOF(Integer)
result:=CAPSIZEINT(widestringmanager.StrICompAnsiStringProc(s1,s2));
end;
function AnsiStrLComp(S1, S2: PChar; MaxLen: SizeUInt): Integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
// CAPSIZEINT is no-op if Sizeof(Sizeint)<=SizeOF(Integer)
result:=CAPSIZEINT(widestringmanager.StrLCompAnsiStringProc(s1,s2,maxlen));
end;
function AnsiStrLIComp(S1, S2: PChar; MaxLen: SizeUint): Integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
// CAPSIZEINT is no-op if Sizeof(Sizeint)<=SizeOF(Integer)
result:=CAPSIZEINT(widestringmanager.StrLICompAnsiStringProc(s1,s2,maxlen));
end;
function AnsiStrLower(Str: PChar): PChar;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
result:=widestringmanager.StrLowerAnsiStringProc(Str);
end;
function AnsiStrUpper(Str: PChar): PChar;{$ifdef SYSUTILSINLINE}inline;{$endif}
begin
result:=widestringmanager.StrUpperAnsiStringProc(Str);
end;
{==============================================================================}
{ End of Ansi functions }
{==============================================================================}
{ Trim returns a copy of S with blanks characters on the left and right stripped off }
Const WhiteSpace = [#0..' '];
function Trim(const S: string): string;
var Ofs, Len: integer;
begin
len := Length(S);
while (Len>0) and (S[Len] in WhiteSpace) do
dec(Len);
Ofs := 1;
while (Ofs<=Len) and (S[Ofs] in WhiteSpace) do
Inc(Ofs);
result := Copy(S, Ofs, 1 + Len - Ofs);
end ;
{ TrimLeft returns a copy of S with all blank characters on the left stripped off }
function TrimLeft(const S: string): string;
var i,l:integer;
begin
l := length(s);
i := 1;
while (i<=l) and (s[i] in whitespace) do
inc(i);
Result := copy(s, i, l);
end ;
{ TrimRight returns a copy of S with all blank characters on the right stripped off }
function TrimRight(const S: string): string;
var l:integer;
begin
l := length(s);
while (l>0) and (s[l] in whitespace) do
dec(l);
result := copy(s,1,l);
end ;
{ QuotedStr returns S quoted left and right and every single quote in S
replaced by two quotes }
function QuotedStr(const S: string): string;
begin
result := AnsiQuotedStr(s, '''');
end ;
{ AnsiQuotedStr returns S quoted left and right by Quote,
and every single occurance of Quote replaced by two }
function AnsiQuotedStr(const S: string; Quote: char): string;
var i, j, count: integer;
begin
result := '' + Quote;
count := length(s);
i := 0;
j := 0;
while i < count do begin
i := i + 1;
if S[i] = Quote then begin
result := result + copy(S, 1 + j, i - j) + Quote;
j := i;
end ;
end ;
if i <> j then
result := result + copy(S, 1 + j, i - j);
result := result + Quote;
end ;
{ AnsiExtractQuotedStr returns a copy of Src with quote characters
deleted to the left and right and double occurances
of Quote replaced by a single Quote }
function AnsiExtractQuotedStr(var Src: PChar; Quote: Char): string;
var
P,Q,R: PChar;
begin
result:='';
if Src=Nil then exit;
P := Src;
Q := StrEnd(P);
if P=Q then
exit;
if P^<>quote then
exit(strpas(P));
inc(p);
setlength(result,(Q-P)+1);
R:=@Result[1];
while P <> Q do
begin
R^:=P^;
inc(R);
if (P^ = Quote) then
begin
P := P + 1;
if (p^ <> Quote) then
begin
dec(R);
break;
end;
end;
P := P + 1;
end ;
src:=p;
SetLength(result, (R-pchar(@Result[1])));
end ;
{ Change CRLF, CR or LF with the default for the current platform }
function AdjustLineBreaks(const S: string): string;
begin
Result:=AdjustLineBreaks(S,DefaultTextLineBreakStyle);
end;
{ Change CRLF, CR or LF with the indicated style }
function AdjustLineBreaks(const S: string; Style: TTextLineBreakStyle): string;
var
Source,Dest: PChar;
DestLen: Integer;
I,J,L: Longint;
begin
Source:=Pointer(S);
L:=Length(S);
DestLen:=L;
I:=1;
while (I<=L) do
begin
case S[i] of
#10: if (Style=tlbsCRLF) then
Inc(DestLen);
#13: if (Style=tlbsCRLF) then
if (I<L) and (S[i+1]=#10) then
Inc(I)
else
Inc(DestLen)
else if (I<L) and (S[I+1]=#10) then
Dec(DestLen);
end;
Inc(I);
end;
if (DestLen=L) then
Result:=S
else
begin
SetLength(Result, DestLen);
FillChar(Result[1],DestLen,0);
Dest := Pointer(Result);
J:=0;
I:=0;
While I<L do
case Source[I] of
#10: begin
if Style=tlbsCRLF then
begin
Dest[j]:=#13;
Inc(J);
end;
Dest[J] := #10;
Inc(J);
Inc(I);
end;
#13: begin
if Style=tlbsCRLF then
begin
Dest[j] := #13;
Inc(J);
end;
Dest[j]:=#10;
Inc(J);
Inc(I);
if Source[I]=#10 then
Inc(I);
end;
else
Dest[j]:=Source[i];
Inc(J);
Inc(I);
end;
end;
end;
{ IsValidIdent returns true if the first character of Ident is in:
'A' to 'Z', 'a' to 'z' or '_' and the following characters are
on of: 'A' to 'Z', 'a' to 'z', '0'..'9' or '_' }
function IsValidIdent(const Ident: string; AllowDots: Boolean = False; StrictDots: Boolean = False): Boolean;
const
Alpha = ['A'..'Z', 'a'..'z', '_'];
AlphaNum = Alpha + ['0'..'9'];
Dot = '.';
var
First: Boolean;
I, Len: Integer;
begin
Len := Length(Ident);
if Len < 1 then
Exit(False);
First := True;
for I := 1 to Len do
begin
if First then
begin
Result := Ident[I] in Alpha;
First := False;
end
else if AllowDots and (Ident[I] = Dot) then
begin
if StrictDots then
begin
Result := I < Len;
First := True;
end;
end
else
Result := Ident[I] in AlphaNum;
if not Result then
Break;
end;
end;
{ IntToStr returns a string representing the value of Value }
function IntToStr(Value: Longint): string;
begin
System.Str(Value, result);
end ;
function IntToStr(Value: int64): string;
begin
System.Str(Value, result);
end ;
function IntToStr(Value: QWord): string;
begin
System.Str(Value, result);
end ;
function UIntToStr(Value: QWord): string;
begin
result:=IntTostr(Value);
end;
function UIntToStr(Value: Cardinal): string;
begin
System.Str(Value, result);
end;
{ IntToHex returns a string representing the hexadecimal value of Value }
const
HexDigits: array[0..15] of char = '0123456789ABCDEF';
function IntToHex(Value: Longint; Digits: integer): string;
var i: integer;
begin
If Digits=0 then
Digits:=1;
SetLength(result, digits);
for i := 0 to digits - 1 do
begin
result[digits - i] := HexDigits[value and 15];
value := value shr 4;
end ;
while value <> 0 do begin
result := HexDigits[value and 15] + result;
value := value shr 4;
end;
end ;
function IntToHex(Value: int64; Digits: integer): string;
var i: integer;
begin
If Digits=0 then
Digits:=1;
SetLength(result, digits);
for i := 0 to digits - 1 do
begin
result[digits - i] := HexDigits[value and 15];
value := value shr 4;
end ;
while value <> 0 do begin
result := HexDigits[value and 15] + result;
value := value shr 4;
end;
end ;
function IntToHex(Value: QWord; Digits: integer): string;
begin
result:=IntToHex(Int64(Value),Digits);
end;
function IntToHex(Value: Int8): string;
begin
Result:=IntToHex(Value, 2*SizeOf(Int8));
end;
function IntToHex(Value: UInt8): string;
begin
Result:=IntToHex(Value, 2*SizeOf(UInt8));
end;
function IntToHex(Value: Int16): string;
begin
Result:=IntToHex(Value, 2*SizeOf(Int16));
end;
function IntToHex(Value: UInt16): string;
begin
Result:=IntToHex(Value, 2*SizeOf(UInt16));
end;
function IntToHex(Value: Int32): string;
begin
Result:=IntToHex(Value, 2*SizeOf(Int32));
end;
function IntToHex(Value: UInt32): string;
begin
Result:=IntToHex(Value, 2*SizeOf(UInt32));
end;
function IntToHex(Value: Int64): string;
begin
Result:=IntToHex(Value, 2*SizeOf(Int64));
end;
function IntToHex(Value: UInt64): string;
begin
Result:=IntToHex(Value, 2*SizeOf(UInt64));
end;
function TryStrToInt(const s: string; out i : Longint) : boolean;
var Error : word;
begin
Val(s, i, Error);
TryStrToInt:=Error=0
end;
{ StrToInt converts the string S to an integer value,
if S does not represent a valid integer value EConvertError is raised }
function StrToInt(const S: string): Longint;
var Error: word;
begin
Val(S, result, Error);
if Error <> 0 then raise EConvertError.createfmt(SInvalidInteger,[S]);
end ;
function StrToInt64(const S: string): int64;
var Error: word;
begin
Val(S, result, Error);
if Error <> 0 then raise EConvertError.createfmt(SInvalidInteger,[S]);
end;
function TryStrToInt64(const s: string; Out i : int64) : boolean;
var Error : word;
begin
Val(s, i, Error);
TryStrToInt64:=Error=0
end;
function StrToQWord(const s: string): QWord;
var Error: word;
begin
Val(S, result, Error);
if Error <> 0 then raise EConvertError.createfmt(SInvalidInteger,[S]);
end;
function StrToUInt64(const s: string): UInt64;
begin
result:=StrToQWord(s);
end;
function StrToDWord(const s: string): DWord;
var Error: word;
begin
Val(S, result, Error);
if Error <> 0 then raise EConvertError.createfmt(SInvalidInteger,[S]);
end;
function TryStrToDWord(const s: string; Out D: DWord): boolean;
var Error : word;
begin
Val(s, D, Error);
TryStrToDWord:=Error=0
end;
function StrToUInt(const s: string): Cardinal;
begin
StrToUInt:=StrToDWord(s);
end;
function TryStrToUInt(const s: string; out C: Cardinal): Boolean;
begin
TryStrToUInt:=TryStrToDWord(s, C);
end;
function TryStrToQWord(const s: string; Out Q: QWord): boolean;
var Error : word;
begin
Val(s, Q, Error);
TryStrToQWord:=Error=0
end;
function TryStrToUInt64(const s: string; Out u: UInt64): boolean;
begin
result:=TryStrToQWord(s,u);
end;
{ StrToIntDef converts the string S to an integer value,
Default is returned in case S does not represent a valid integer value }
function StrToIntDef(const S: string; Default: Longint): Longint;
var Error: word;
begin
Val(S, result, Error);
if Error <> 0 then result := Default;
end ;
{ StrToDWordDef converts the string S to an DWord value,
Default is returned in case S does not represent a valid DWord value }
function StrToDWordDef(const S: string; Default: DWord): DWord;
var Error: word;
begin
Val(S, result, Error);
if Error <> 0 then result := Default;
end;
function StrToUIntDef(const S: string; Default: Cardinal): Cardinal;
begin
Result:=StrToDWordDef(S, Default);
end;
{ StrToInt64Def converts the string S to an int64 value,
Default is returned in case S does not represent a valid int64 value }
function StrToInt64Def(const S: string; Default: int64): int64;
var Error: word;
begin
Val(S, result, Error);
if Error <> 0 then result := Default;
end ;
{ StrToQWordDef converts the string S to an QWord value,
Default is returned in case S does not represent a valid QWord value }
function StrToQWordDef(const S: string; Default: QWord): QWord;
var Error: word;
begin
Val(S, result, Error);
if Error <> 0 then result := Default;
end;
function StrToUInt64Def(const S: string; Default: UInt64): UInt64;
begin
result:=StrToQWordDef(S,Default);
end;
{ LoadStr returns the string resource Ident. }
function LoadStr(Ident: integer): string;
begin
result:='';
end ;
{ FmtLoadStr returns the string resource Ident and formats it accordingly }
function FmtLoadStr(Ident: integer; const Args: array of const): string;
begin
result:='';
end;
Const
feInvalidFormat = 1;
feMissingArgument = 2;
feInvalidArgIndex = 3;
{$ifdef fmtdebug}
Procedure Log (Const S: String);
begin
Writeln (S);
end;
{$endif}
Procedure DoFormatError (ErrCode : Longint;const fmt:ansistring);
Var
S : String;
begin
//!! must be changed to contain format string...
S:=fmt;
Case ErrCode of
feInvalidFormat : raise EConvertError.Createfmt(SInvalidFormat,[s]);
feMissingArgument : raise EConvertError.Createfmt(SArgumentMissing,[s]);
feInvalidArgIndex : raise EConvertError.Createfmt(SInvalidArgIndex,[s]);
end;
end;
{ we've no templates, but with includes we can simulate this :) }
{$macro on}
{$define INFORMAT}
{$define TFormatString:=ansistring}
{$define TFormatChar:=char}
Function Format (Const Fmt : AnsiString; const Args : Array of const; const FormatSettings: TFormatSettings) : AnsiString;
{$i sysformt.inc}
{$undef TFormatString}
{$undef TFormatChar}
{$undef INFORMAT}
{$macro off}
Function Format (Const Fmt : AnsiString; const Args : Array of const) : AnsiString;
begin
Result:=Format(Fmt,Args,DefaultFormatSettings);
end;
Function FormatBuf (Var Buffer; BufLen : Cardinal; Const Fmt; fmtLen : Cardinal; Const Args : Array of const; Const FormatSettings: TFormatSettings) : Cardinal;
Var S,F : String;
begin
Setlength(F,fmtlen);
if fmtlen > 0 then
Move(fmt,F[1],fmtlen);
S:=Format (F,Args,FormatSettings);
If Cardinal(Length(S))<Buflen then
Result:=Length(S)
else
Result:=Buflen;
Move(S[1],Buffer,Result);
end;
Function FormatBuf (Var Buffer; BufLen : Cardinal;
Const Fmt; fmtLen : Cardinal;
Const Args : Array of const) : Cardinal;
begin
Result:=FormatBuf(Buffer,BufLen,Fmt,FmtLen,Args,DefaultFormatSettings);
end;
Procedure FmtStr(Var Res: string; const Fmt : string; Const args: Array of const; Const FormatSettings: TFormatSettings);
begin
Res:=Format(fmt,Args,FormatSettings);
end;
Procedure FmtStr(Var Res: String; Const Fmt : String; Const args: Array of const);
begin
FmtStr(Res,Fmt,Args,DefaultFormatSettings);
end;
Function StrFmt(Buffer,Fmt : PChar; Const args: Array of const) : Pchar;
begin
Result:=StrFmt(Buffer,Fmt,Args,DefaultFormatSettings);
end;
Function StrFmt(Buffer,Fmt : PChar; Const Args: Array of const; Const FormatSettings: TFormatSettings): PChar;
begin
Buffer[FormatBuf(Buffer^,Maxint,Fmt^,strlen(fmt),args,FormatSettings)]:=#0;
Result:=Buffer;
end;
Function StrLFmt(Buffer : PCHar; Maxlen : Cardinal;Fmt : PChar; Const args: Array of const) : Pchar;
begin
Result:=StrLFmt(Buffer,MaxLen,Fmt,Args,DefaultFormatSettings);
end;
Function StrLFmt(Buffer : PCHar; Maxlen : Cardinal;Fmt : PChar; Const args: Array of const; Const FormatSettings: TFormatSettings) : Pchar;
begin
Buffer[FormatBuf(Buffer^,MaxLen,Fmt^,strlen(fmt),args,FormatSettings)]:=#0;
Result:=Buffer;
end;
{$ifndef FPUNONE}
Function StrToFloat(Const S: String): Extended;
begin
Result:=StrToFloat(S,DefaultFormatSettings);
end;
Function StrToFloat(Const S : String; Const FormatSettings: TFormatSettings) : Extended;
Begin // texttofloat handles NIL properly
If Not TextToFloat(Pchar(pointer(S)),Result,FormatSettings) then
Raise EConvertError.createfmt(SInValidFLoat,[S]);
End;
function StrToFloatDef(const S: string; const Default: Extended): Extended;
begin
Result:=StrToFloatDef(S,Default,DefaultFormatSettings);
end;
Function StrToFloatDef(Const S: String; Const Default: Extended; Const FormatSettings: TFormatSettings): Extended;
begin
if not TextToFloat(PChar(pointer(S)),Result,fvExtended,FormatSettings) then
Result:=Default;
end;
Function TextToFloat(Buffer: PChar; Out Value: Extended; Const FormatSettings: TFormatSettings): Boolean;
Var
E,P : Integer;
S : String;
Begin
S:=StrPas(Buffer);
//ThousandSeparator not allowed as by Delphi specs
if (FormatSettings.ThousandSeparator <> FormatSettings.DecimalSeparator) and
(Pos(FormatSettings.ThousandSeparator, S) <> 0) then
begin
Result := False;
Exit;
end;
if (FormatSettings.DecimalSeparator <> '.') and
(Pos('.', S) <>0) then
begin
Result := False;
Exit;
end;
P:=Pos(FormatSettings.DecimalSeparator,S);
If (P<>0) Then
S[P] := '.';
try
Val(trim(S),Value,E);
{ on x87, a floating point exception may be pending in case of an invalid
input value -> trigger it now }
{$if defined(cpui386) or (defined(cpux86_64) and not(defined(win64))) or defined(cpui8086)}
asm
fwait
end;
{$endif}
except
E:=1;
end;
Result:=(E=0);
End;
Function TextToFloat(Buffer: PChar; Out Value: Extended): Boolean;
begin
Result:=TextToFloat(Buffer,Value,DefaultFormatSettings);
end;
Function TextToFloat(Buffer: PChar; Out Value; ValueType: TFloatValue): Boolean;
begin
Result:=TextToFloat(Buffer,Value,ValueType,DefaultFormatSettings);
end;
Function TextToFloat(Buffer: PChar; Out Value; ValueType: TFloatValue; Const FormatSettings: TFormatSettings): Boolean;
Var
E,P : Integer;
S : String;
Begin
S:=StrPas(Buffer);
//ThousandSeparator not allowed as by Delphi specs
if (FormatSettings.ThousandSeparator <> FormatSettings.DecimalSeparator) and
(Pos(FormatSettings.ThousandSeparator, S) <> 0) then
begin
Result := False;
Exit;
end;
if (FormatSettings.DecimalSeparator <> '.') and
(Pos('.', S) <>0) then
begin
Result := False;
Exit;
end;
P:=Pos(FormatSettings.DecimalSeparator,S);
If (P<>0) Then
S[P] := '.';
s:=Trim(s);
try
case ValueType of
fvCurrency:
Val(S,Currency(Value),E);
fvExtended:
Val(S,Extended(Value),E);
fvDouble:
Val(S,Double(Value),E);
fvSingle:
Val(S,Single(Value),E);
fvComp:
Val(S,Comp(Value),E);
fvReal:
Val(S,Real(Value),E);
end;
{ on x87, a floating point exception may be pending in case of an invalid
input value -> trigger it now }
{$if defined(cpui386) or (defined(cpux86_64) and not(defined(win64))) or defined(cpui8086)}
asm
fwait
end;
{$endif}
except
E:=1;
end;
Result:=(E=0);
End;
Function TryStrToFloat(Const S : String; Out Value: Single): Boolean;
begin
Result:=TryStrToFloat(S,Value,DefaultFormatSettings);
end;
Function TryStrToFloat(Const S : String; Out Value: Single; Const FormatSettings: TFormatSettings): Boolean;
Begin
Result := TextToFloat(PChar(pointer(S)), Value, fvSingle,FormatSettings);
End;
Function TryStrToFloat(Const S : String; Out Value: Double): Boolean;
begin
Result:=TryStrToFloat(S,Value,DefaultFormatSettings);
end;
Function TryStrToFloat(Const S : String; Out Value: Double; Const FormatSettings: TFormatSettings): Boolean;
Begin
Result := TextToFloat(PChar(pointer(S)), Value, fvDouble,FormatSettings);
End;
{$ifdef FPC_HAS_TYPE_EXTENDED}
Function TryStrToFloat(Const S : String; Out Value: Extended): Boolean;
begin
Result:=TryStrToFloat(S,Value,DefaultFormatSettings);
end;
Function TryStrToFloat(Const S : String; Out Value: Extended; Const FormatSettings: TFormatSettings): Boolean;
Begin
Result := TextToFloat(PChar(pointer(S)), Value,FormatSettings);
End;
{$endif FPC_HAS_TYPE_EXTENDED}
const
{$ifdef FPC_HAS_TYPE_EXTENDED}
maxdigits = 17;
{$else}
maxdigits = 15;
{$endif}
{ deactive aligned function for 2.6 }
{$ifdef VER2_6}
{$macro on}
{$define aligned:= }
{$endif VER2_6}
Function FloatToStrFIntl(const Value; format: TFloatFormat; Precision, Digits: Integer; ValueType: TFloatValue; Const FormatSettings: TFormatSettings): String;
Var
P, PE, Q, Exponent: Integer;
Negative: Boolean;
DS: Char;
function RemoveLeadingNegativeSign(var AValue: String): Boolean;
// removes negative sign in case when result is zero eg. -0.00
var
i: PtrInt;
TS: Char;
StartPos: PtrInt;
begin
Result := False;
if Format = ffCurrency then
StartPos := 1
else
StartPos := 2;
TS := FormatSettings.ThousandSeparator;
for i := StartPos to length(AValue) do
begin
Result := (AValue[i] in ['0', DS, 'E', '+', TS]);
if not Result then
break;
end;
if (Result) and (Format <> ffCurrency) then
Delete(AValue, 1, 1);
end;
Begin
DS:=FormatSettings.DecimalSeparator;
Case format Of
ffGeneral:
Begin
case ValueType of
fvCurrency:
If (Precision = -1) Or (Precision > 19) Then Precision := 19;
else
If (Precision = -1) Or (Precision > maxdigits) Then Precision := maxdigits;
end;
{ First convert to scientific format, with correct precision }
case ValueType of
fvDouble:
Str(Double(Extended(Aligned(Value))):precision+7, Result);
fvSingle:
Str(Single(Extended(Aligned(Value))):precision+6, Result);
fvCurrency:
Str(Currency(Aligned(Value)):precision+6, Result);
else
Str(Extended(Aligned(Value)):precision+8, Result);
end;
{ Delete leading spaces }
while Result[1] = ' ' do
System.Delete(Result, 1, 1);
P := Pos('.', Result);
if P<>0 then
Result[P] := DS
else
Exit; { NAN or other special case }
{ Consider removing exponent }
PE:=Pos('E',Result);
if PE > 0 then begin
{ Read exponent }
Q := PE+2;
Exponent := 0;
while (Q <= Length(Result)) do begin
Exponent := Exponent*10 + Ord(Result[Q])-Ord('0');
Inc(Q);
end;
if Result[PE+1] = '-' then
Exponent := -Exponent;
if (P+Exponent < PE) and (Exponent > -6) then begin
{ OK to remove exponent }
SetLength(Result,PE-1); { Trim exponent }
if Exponent >= 0 then begin
{ Shift point to right }
for Q := 0 to Exponent-1 do begin
Result[P] := Result[P+1];
Inc(P);
end;
Result[P] := DS;
P := 1;
if Result[P] = '-' then
Inc(P);
while (Result[P] = '0') and (P < Length(Result)) and (Result[P+1] <> DS) do
{ Trim leading zeros; conversion above should not give any, but occasionally does
because of rounding }
System.Delete(Result,P,1);
end else begin
{ Add zeros at start }
Insert(Copy('00000',1,-Exponent),Result,P-1);
Result[P-Exponent] := Result[P-Exponent-1]; { Copy leading digit }
Result[P] := DS;
if Exponent <> -1 then
Result[P-Exponent-1] := '0';
end;
{ Remove trailing zeros }
Q := Length(Result);
while (Q > 0) and (Result[Q] = '0') do
Dec(Q);
if Result[Q] = DS then
Dec(Q); { Remove trailing decimal point }
if (Q = 0) or ((Q=1) and (Result[1] = '-')) then
Result := '0'
else
SetLength(Result,Q);
end else begin
{ Need exponent, but remove superfluous characters }
{ Delete trailing zeros }
while Result[PE-1] = '0' do begin
System.Delete(Result,PE-1,1);
Dec(PE);
end;
{ If number ends in decimal point, remove it }
if Result[PE-1] = DS then begin
System.Delete(Result,PE-1,1);
Dec(PE);
end;
{ delete superfluous + in exponent }
if Result[PE+1]='+' then
System.Delete(Result,PE+1,1)
else
Inc(PE);
while Result[PE+1] = '0' do
{ Delete leading zeros in exponent }
System.Delete(Result,PE+1,1)
end;
end;
End;
ffExponent:
Begin
If (Precision = -1) Or (Precision > maxdigits) Then Precision := maxdigits;
case ValueType of
fvDouble:
Str(Double(Extended(Aligned(Value))):Precision+7, Result);
fvSingle:
Str(Single(Extended(Aligned(Value))):Precision+6, Result);
fvCurrency:
Str(Currency(Aligned(Value)):Precision+6, Result);
else
Str(Extended(Aligned(Value)):Precision+8, Result);
end;
{ Delete leading spaces }
while Result[1] = ' ' do
System.Delete(Result, 1, 1);
if (Result[1]='-') and
{ not Nan etc.? }
(Result[3]='.') then
Result[3] := DS
else if Result[2]='.' then
Result[2] := DS;
P:=Pos('E',Result);
if P <> 0 then
begin
Inc(P, 2);
if Digits > 4 then
Digits:=4;
Digits:=Length(Result) - P - Digits + 1;
if Digits < 0 then
insert(copy('0000',1,-Digits),Result,P)
else
while (Digits > 0) and (Result[P] = '0') do
begin
System.Delete(Result, P, 1);
if P > Length(Result) then
begin
System.Delete(Result, P - 2, 2);
break;
end;
Dec(Digits);
end;
end;
End;
ffFixed:
Begin
If Digits = -1 Then Digits := 2
Else If Digits > 18 Then Digits := 18;
case ValueType of
fvDouble:
Str(Double(Extended(Aligned(Value))):0:Digits, Result);
fvSingle:
Str(Single(Extended(Aligned(Value))):0:Digits, Result);
fvCurrency:
Str(Currency(Aligned(Value)):0:Digits, Result);
else
Str(Extended(Aligned(Value)):0:Digits, Result);
end;
If Result[1] = ' ' Then
System.Delete(Result, 1, 1);
P := Pos('.', Result);
If P <> 0 Then Result[P] := DS;
End;
ffNumber:
Begin
If Digits = -1 Then Digits := 2
Else If Digits > maxdigits Then Digits := maxdigits;
case ValueType of
fvDouble:
Str(Double(Extended(Aligned(Value))):0:Digits, Result);
fvSingle:
Str(Single(Extended(Aligned(Value))):0:Digits, Result);
fvCurrency:
Str(Currency(Aligned(Value)):0:Digits, Result);
else
Str(Extended(Aligned(Value)):0:Digits, Result);
end;
If Result[1] = ' ' Then System.Delete(Result, 1, 1);
P := Pos('.', Result);
If P <> 0 Then
Result[P] := DS
else
P := Length(Result)+1;
Dec(P, 3);
While (P > 1) Do
Begin
If (Result[P - 1] <> '-') And (FormatSettings.ThousandSeparator <> #0) Then
Insert(FormatSettings.ThousandSeparator, Result, P);
Dec(P, 3);
End;
End;
ffCurrency:
Begin
If Digits = -1 Then Digits := FormatSettings.CurrencyDecimals
Else If Digits > 18 Then Digits := 18;
case ValueType of
fvDouble:
Str(Double(Extended(Aligned(Value))):0:Digits, Result);
fvSingle:
Str(Single(Extended(Aligned(Value))):0:Digits, Result);
fvCurrency:
Str(Currency(Aligned(Value)):0:Digits, Result);
else
Str(Extended(Aligned(Value)):0:Digits, Result);
end;
Negative:=Result[1] = '-';
if Negative then
System.Delete(Result, 1, 1);
P := Pos('.', Result);
If P <> 0 Then Result[P] := DS else P := Length(Result)+1;
Dec(P, 3);
While (P > 1) Do
Begin
If FormatSettings.ThousandSeparator<>#0 Then
Insert(FormatSettings.ThousandSeparator, Result, P);
Dec(P, 3);
End;
if (length(Result) > 1) and Negative then
Negative := not RemoveLeadingNegativeSign(Result);
If Not Negative Then
Begin
Case FormatSettings.CurrencyFormat Of
0: Result := FormatSettings.CurrencyString + Result;
1: Result := Result + FormatSettings.CurrencyString;
2: Result := FormatSettings.CurrencyString + ' ' + Result;
3: Result := Result + ' ' + FormatSettings.CurrencyString;
End
End
Else
Begin
Case FormatSettings.NegCurrFormat Of
0: Result := '(' + FormatSettings.CurrencyString + Result + ')';
1: Result := '-' + FormatSettings.CurrencyString + Result;
2: Result := FormatSettings.CurrencyString + '-' + Result;
3: Result := FormatSettings.CurrencyString + Result + '-';
4: Result := '(' + Result + FormatSettings.CurrencyString + ')';
5: Result := '-' + Result + FormatSettings.CurrencyString;
6: Result := Result + '-' + FormatSettings.CurrencyString;
7: Result := Result + FormatSettings.CurrencyString + '-';
8: Result := '-' + Result + ' ' + FormatSettings.CurrencyString;
9: Result := '-' + FormatSettings.CurrencyString + ' ' + Result;
10: Result := Result + ' ' + FormatSettings.CurrencyString + '-';
11: Result := FormatSettings.CurrencyString + ' ' + Result + '-';
12: Result := FormatSettings.CurrencyString + ' ' + '-' + Result;
13: Result := Result + '-' + ' ' + FormatSettings.CurrencyString;
14: Result := '(' + FormatSettings.CurrencyString + ' ' + Result + ')';
15: Result := '(' + Result + ' ' + FormatSettings.CurrencyString + ')';
End;
End;
End;
End;
if not (format in [ffCurrency]) and (length(Result) > 1) and (Result[1] = '-') then
RemoveLeadingNegativeSign(Result);
End;
{$macro off}
{$ifdef FPC_HAS_TYPE_EXTENDED}
Function FloatToStr(Value: Extended; Const FormatSettings: TFormatSettings): String;
Begin
Result := FloatToStrFIntl(Value, ffGeneral, 15, 0, fvExtended,FormatSettings);
End;
Function FloatToStr(Value: Extended): String;
begin
Result:=FloatToStr(Value,DefaultFormatSettings);
end;
{$endif FPC_HAS_TYPE_EXTENDED}
Function FloatToStr(Value: Currency; Const FormatSettings: TFormatSettings): String;
Begin
Result := FloatToStrFIntl(Value, ffGeneral, 15, 0, fvCurrency,FormatSettings);
End;
Function FloatToStr(Value: Currency): String;
begin
Result:=FloatToStr(Value,DefaultFormatSettings);
end;
Function FloatToStr(Value: Double; Const FormatSettings: TFormatSettings): String;
var
e: Extended;
Begin
e := Value;
Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvDouble,FormatSettings);
End;
Function FloatToStr(Value: Double): String;
begin
Result:=FloatToStr(Value,DefaultFormatSettings);
end;
Function FloatToStr(Value: Single; Const FormatSettings: TFormatSettings): String;
var
e: Extended;
Begin
e := Value;
Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvSingle,FormatSettings);
End;
Function FloatToStr(Value: Single): String;
begin
Result:=FloatToStr(Value,DefaultFormatSettings);
end;
Function FloatToStr(Value: Comp; Const FormatSettings: TFormatSettings): String;
var
e: Extended;
Begin
e := Value;
Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvComp,FormatSettings);
End;
Function FloatToStr(Value: Comp): String;
begin
Result:=FloatToStr(Value,DefaultFormatSettings);
end;
{$ifndef FPC_COMP_IS_INT64}
Function FloatToStr(Value: Int64): String;
begin
Result:=FloatToStr(Value,DefaultFormatSettings);
end;
Function FloatToStr(Value: Int64; Const FormatSettings: TFormatSettings): String;
var
e: Extended;
Begin
e := Comp(Value);
Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvComp,FormatSettings);
End;
{$endif FPC_COMP_IS_INT64}
Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): Longint;
Var
Tmp: String[40];
Begin
Tmp := FloatToStrF(Value, format, Precision, Digits,FormatSettings);
Result := Length(Tmp);
Move(Tmp[1], Buffer[0], Result);
End;
Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer): Longint;
begin
Result:=FloatToText(Buffer,Value,Format,Precision,Digits,DefaultFormatSettings);
end;
{$ifdef FPC_HAS_TYPE_EXTENDED}
Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
begin
Result := FloatToStrFIntl(value,format,precision,digits,fvExtended,FormatSettings);
end;
Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer): String;
begin
Result:=FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
end;
{$endif}
Function FloatToStrF(Value: Currency; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
begin
Result := FloatToStrFIntl(value,format,precision,digits,fvCurrency,FormatSettings);
end;
Function FloatToStrF(Value: Currency; format: TFloatFormat; Precision, Digits: Integer): String;
begin
Result:=FloatToStrF(Value,format,Precision,Digits,DefaultFormatSettings);
end;
Function FloatToStrF(Value: Double; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
var
e: Extended;
begin
e := Value;
result := FloatToStrFIntl(e,format,precision,digits,fvDouble,FormatSettings);
end;
Function FloatToStrF(Value: Double; format: TFloatFormat; Precision, Digits: Integer): String;
begin
Result:= FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
end;
Function FloatToStrF(Value: Single; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
var
e: Extended;
begin
e:=Value;
result := FloatToStrFIntl(e,format,precision,digits,fvSingle,FormatSettings);
end;
Function FloatToStrF(Value: Single; format: TFloatFormat; Precision, Digits: Integer): String;
begin
Result:= FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
end;
Function FloatToStrF(Value: Comp; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
var
e: Extended;
begin
e := Value;
Result := FloatToStrFIntl(e,format,precision,digits,fvComp,FormatSettings);
end;
Function FloatToStrF(Value: Comp; format: TFloatFormat; Precision, Digits: Integer): String;
begin
Result:=FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
end;
{$ifndef FPC_COMP_IS_INT64}
Function FloatToStrF(Value: Int64; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
var
e: Extended;
begin
e := Comp(Value);
result := FloatToStrFIntl(e,format,precision,digits,fvComp,FormatSettings);
end;
Function FloatToStrF(Value: Int64; format: TFloatFormat; Precision, Digits: Integer): String;
begin
Result:=FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
end;
{$endif FPC_COMP_IS_INT64}
Function CurrToStrF(Value: Currency; Format: TFloatFormat; Digits: Integer; Const FormatSettings: TFormatSettings): string;
begin
result:=FloatToStrF(Value,Format,19,Digits,FormatSettings);
end;
Function CurrToStrF(Value: Currency; Format: TFloatFormat; Digits: Integer): string;
begin
Result:=CurrToStrF(Value,Format,Digits,DefaultFormatSettings);
end;
Function FloatToDateTime (Const Value : Extended) : TDateTime;
begin
If (Value<MinDateTime) or (Value>MaxDateTime) then
Raise EConvertError.CreateFmt (SInvalidDateTimeFloat,[Value]);
Result:=Value;
end;
function TryFloatToCurr(const Value: Extended; var AResult: Currency): Boolean;
begin
Result:=(Value>=MinCurrency) and (Value<=MaxCurrency);
if Result then
AResult := Value;
end;
function FloatToCurr(const Value: Extended): Currency;
begin
if not TryFloatToCurr(Value, Result) then
Raise EConvertError.CreateFmt(SInvalidCurrency, [FloatToStr(Value)]);
end;
Function CurrToStr(Value: Currency): string;
begin
Result:=FloatToStrF(Value,ffGeneral,-1,0);
end;
Function CurrToStr(Value: Currency; Const FormatSettings: TFormatSettings): string;
begin
Result:=FloatToStrF(Value,ffGeneral,-1,0,FormatSettings);
end;
function StrToCurr(const S: string): Currency;
begin
if not TextToFloat(PChar(pointer(S)), Result, fvCurrency) then
Raise EConvertError.createfmt(SInValidFLoat,[S]);
end;
function StrToCurr(const S: string; Const FormatSettings: TFormatSettings): Currency;
begin
if not TextToFloat(PChar(pointer(S)), Result, fvCurrency,FormatSettings) then
Raise EConvertError.createfmt(SInValidFLoat,[S]);
end;
Function TryStrToCurr(Const S : String; Out Value: Currency): Boolean;
Begin
Result := TextToFloat(PChar(pointer(S)), Value, fvCurrency);
End;
function TryStrToCurr(const S: string;Out Value : Currency; Const FormatSettings: TFormatSettings): Boolean;
Begin
Result := TextToFloat(PChar(pointer(S)), Value, fvCurrency,FormatSettings);
End;
function StrToCurrDef(const S: string; Default : Currency): Currency;
begin
if not TextToFloat(PChar(pointer(S)), Result, fvCurrency) then
Result:=Default;
end;
function StrToCurrDef(const S: string; Default : Currency; Const FormatSettings: TFormatSettings): Currency;
begin
if not TextToFloat(PChar(pointer(S)), Result, fvCurrency,FormatSettings) then
Result:=Default;
end;
{$endif FPUNONE}
function AnsiDequotedStr(const S: string; AQuote: Char): string;
var p : pchar;
begin
p:=pchar(pointer(s)); // work around CONST. Ansiextract is safe for nil
result:=AnsiExtractquotedStr(p,AQuote);
end;
function StrToBool(const S: string): Boolean;
begin
if not(TryStrToBool(S,Result,DefaultFormatSettings)) then
Raise EConvertError.CreateFmt(SInvalidBoolean,[S]);
end;
function StrToBool(const S: string; const FormatSettings: TFormatSettings): Boolean;
begin
if not(TryStrToBool(S,Result,FormatSettings)) then
Raise EConvertError.CreateFmt(SInvalidBoolean,[S]);
end;
procedure CheckBoolStrs;
begin
If Length(TrueBoolStrs)=0 then
begin
SetLength(TrueBoolStrs,1);
TrueBoolStrs[0]:='True';
end;
If Length(FalseBoolStrs)=0 then
begin
SetLength(FalseBoolStrs,1);
FalseBoolStrs[0]:='False';
end;
end;
function BoolToStr(B: Boolean;UseBoolStrs:Boolean=False): string;
begin
if UseBoolStrs Then
begin
CheckBoolStrs;
if B then
Result:=TrueBoolStrs[0]
else
Result:=FalseBoolStrs[0];
end
else
If B then
Result:='-1'
else
Result:='0';
end;
// from textmode IDE util funcs.
function BoolToStr(B: boolean; const TrueS, FalseS: string): string;
begin
if B then Result:=TrueS else BoolToStr:=FalseS;
end;
function StrToBoolDef(const S: string; Default: Boolean): Boolean;
begin
if not(TryStrToBool(S,Result)) then
Result:=Default;
end;
function StrToBoolDef(const S: string; Default: Boolean; const FormatSettings: TFormatSettings): Boolean;
begin
if not(TryStrToBool(S,Result,FormatSettings)) then
Result:=Default;
end;
function TryStrToBool(const S: string; out Value: Boolean): Boolean;
begin
Result:=TryStrToBool(S,Value,DefaultFormatSettings);
end;
function TryStrToBool(const S: string; out Value: Boolean; const FormatSettings: TFormatSettings): Boolean;
Var
Temp : String;
I : Longint;
{$ifdef FPUNONE}
D : Longint;
{$else}
D : Double;
{$endif}
Code: word;
begin
Temp:=upcase(S);
Val(temp,D,code);
Result:=true;
If (Code=0) or TryStrToFloat(S,D,FormatSettings) then
{$ifdef FPUNONE}
Value:=(D<>0)
{$else}
Value:=(D<>0.0)
{$endif}
else
begin
CheckBoolStrs;
for I:=low(TrueBoolStrs) to High(TrueBoolStrs) do
if Temp=upcase(TrueBoolStrs[I]) then
begin
Value:=true;
exit;
end;
for I:=low(FalseBoolStrs) to High(FalseBoolStrs) do
if Temp=upcase(FalseBoolStrs[I]) then
begin
Value:=false;
exit;
end;
Result:=false;
end;
end;
{$ifndef FPUNONE}
Function FloatToTextFmt(Buffer: PChar; Value: Extended; format: PChar): Integer;
begin
Result:=FloatToTextFmt(Buffer,Value,Format,DefaultFormatSettings);
end;
{$MACRO ON}
{$define FPChar:=PAnsiChar}
{$define FChar:=AnsiChar}
{$define FString:=AnsiString}
{$I fmtflt.inc}
Function FloatToTextFmt(Buffer: PChar; Value: Extended; format: PChar; FormatSettings : TFormatSettings): Integer;
begin
Result:=IntFloatToTextFmt(Buffer,Value,fvExtended,Format,FormatSettings);
end;
Procedure FloatToDecimal(Out Result: TFloatRec; const Value; ValueType: TFloatValue; Precision, Decimals : integer);
var
Buffer: String[254]; //Though str func returns only 25 chars, this might change in the future
InfNan: string[3];
Error, N, L, Start, C: Integer;
GotNonZeroBeforeDot, BeforeDot : boolean;
begin
case ValueType of
fvExtended:
Str(Extended(Value):25, Buffer);
fvDouble,
fvReal:
Str(Double(Value):23, Buffer);
fvSingle:
Str(Single(Value):16, Buffer);
fvCurrency:
Str(Currency(Value):25, Buffer);
fvComp:
Str(Currency(Value):23, Buffer);
end;
N := 1;
L := Byte(Buffer[0]);
while Buffer[N]=' ' do
Inc(N);
Result.Negative := (Buffer[N] = '-');
if Result.Negative then
Inc(N)
else if (Buffer[N] = '+') then
inc(N);
{ special cases for Inf and Nan }
if (L>=N+2) then
begin
InfNan:=copy(Buffer,N,3);
if (InfNan='Inf') then
begin
Result.Digits[0]:=#0;
Result.Exponent:=32767;
exit
end;
if (InfNan='Nan') then
begin
Result.Digits[0]:=#0;
Result.Exponent:=-32768;
exit
end;
end;
Start := N; //Start of digits
Result.Exponent := 0; BeforeDot := true;
GotNonZeroBeforeDot := false;
while (L>=N) and (Buffer[N]<>'E') do
begin
if Buffer[N]='.' then
BeforeDot := false
else
begin
if BeforeDot then
begin // Currently this is always 1 char
Inc(Result.Exponent);
Result.Digits[N-Start] := Buffer[N];
if Buffer[N] <> '0' then
GotNonZeroBeforeDot := true;
end
else
Result.Digits[N-Start-1] := Buffer[N]
end;
Inc(N);
end;
Inc(N); // Pass through 'E'
if N<=L then
begin
Val(Copy(Buffer, N, L-N+1), C, Error); // Get exponent after 'E'
Inc(Result.Exponent, C);
end;
// Calculate number of digits we have from str
if BeforeDot then
N := N - Start - 1
else
N := N - Start - 2;
L := SizeOf(Result.Digits);
if N<L then
FillChar(Result.Digits[N], L-N, '0'); //Zero remaining space
if Decimals + Result.Exponent < Precision Then //After this it is the same as in FloatToDecimal
N := Decimals + Result.Exponent
Else
N := Precision;
if N >= L Then
N := L-1;
if N = 0 Then
begin
if Result.Digits[0] >= '5' Then
begin
Result.Digits[0] := '1';
Result.Digits[1] := #0;
Inc(Result.Exponent);
end
Else
Result.Digits[0] := #0;
end //N=0
Else if N > 0 Then
begin
if Result.Digits[N] >= '5' Then
begin
Repeat
Result.Digits[N] := #0;
Dec(N);
Inc(Result.Digits[N]);
Until (N = 0) Or (Result.Digits[N] < ':');
If Result.Digits[0] = ':' Then
begin
Result.Digits[0] := '1';
Inc(Result.Exponent);
end;
end
Else
begin
Result.Digits[N] := '0';
While (N > -1) And (Result.Digits[N] = '0') Do
begin
Result.Digits[N] := #0;
Dec(N);
end;
end;
end //N>0
Else
Result.Digits[0] := #0;
if (Result.Digits[0] = #0) and
not GotNonZeroBeforeDot then
begin
Result.Exponent := 0;
Result.Negative := False;
end;
end;
Procedure FloatToDecimal(Out Result: TFloatRec; Value: Extended; Precision, Decimals : integer);
begin
FloatToDecimal(Result,Value,fvExtended,Precision,Decimals);
end;
Function FormatFloat(Const Format : String; Value : Extended; Const FormatSettings: TFormatSettings) : String;
Var
buf : Array[0..1024] of char;
Begin // not changed to pchar(pointer(). Possibly not safe
Buf[FloatToTextFmt(@Buf[0],Value,Pchar(Format),FormatSettings)]:=#0;
Result:=StrPas(@Buf[0]);
End;
Function FormatFloat(Const format: String; Value: Extended): String;
begin
Result:=FormatFloat(Format,Value,DefaultFormatSettings);
end;
Function FormatCurr(const Format: string; Value: Currency; Const FormatSettings: TFormatSettings): string;
begin
Result := FormatFloat(Format, Value,FormatSettings);
end;
function FormatCurr(const Format: string; Value: Currency): string;
begin
Result:=FormatCurr(Format,Value,DefaultFormatSettings);
end;
{$endif}
{==============================================================================}
{ extra functions }
{==============================================================================}
{ LeftStr returns Count left-most characters from S }
function LeftStr(const S: string; Count: integer): string;
begin
result := Copy(S, 1, Count);
end ;
{ RightStr returns Count right-most characters from S }
function RightStr(const S: string; Count: integer): string;
begin
If Count>Length(S) then
Count:=Length(S);
result := Copy(S, 1 + Length(S) - Count, Count);
end;
{ BCDToInt converts the BCD value Value to an integer }
function BCDToInt(Value: integer): integer;
var i, j, digit: integer;
begin
result := 0;
j := 1;
for i := 0 to SizeOf(Value) shl 1 - 1 do begin
digit := Value and 15;
if digit > $9 then
begin
if i = 0 then
begin
if digit in [$B, $D] then j := -1
end
else raise EConvertError.createfmt(SInvalidBCD,[Value]);
end
else
begin
result := result + j * digit;
j := j * 10;
end ;
Value := Value shr 4;
end ;
end ;
Function LastDelimiter(const Delimiters, S: string): SizeInt;
var
chs: TSysCharSet;
I: SizeInt;
begin
chs := [];
for I := 1 to Length(Delimiters) do
Include(chs, Delimiters[I]);
Result:=Length(S);
While (Result>0) and not (S[Result] in chs) do
Dec(Result);
end;
{$macro on}
{$define INSTRINGREPLACE}
{$define SRString:=String}
{$define SRUpperCase:=AnsiUppercase}
{$define SRPCHAR:=PChar}
{$define SRCHAR:=Char}
Function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;
Var
C : Integer;
begin
Result:=StringReplace(S,OldPattern,NewPattern,Flags,C);
end;
function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags; Out aCount : Integer): string;
{$i syssr.inc}
{$undef INSTRINGREPLACE}
{$undef SRString}
{$undef SRUpperCase}
{$undef SRPCHAR}
{$undef SRCHAR}
Function IsDelimiter(const Delimiters, S: string; Index: SizeInt): Boolean;
begin
Result:=False;
If (Index>0) and (Index<=Length(S)) then
Result:=Pos(S[Index],Delimiters)<>0; // Note we don't do MBCS yet
end;
Function ByteToCharLen(const S: string; MaxLen: SizeInt): SizeInt;
begin
Result:=Length(S);
If Result>MaxLen then
Result:=MaxLen;
end;
Function ByteToCharIndex(const S: string; Index: SizeInt): SizeInt;
begin
Result:=Index;
end;
Function CharToByteLen(const S: string; MaxLen: SizeInt): SizeInt;
begin
Result:=Length(S);
If Result>MaxLen then
Result:=MaxLen;
end;
Function CharToByteIndex(const S: string; Index: SizeInt): SizeInt;
begin
Result:=Index;
end;
Function ByteType(const S: string; Index: SizeUInt): TMbcsByteType;
begin
Result:=mbSingleByte;
end;
Function StrByteType(Str: PChar; Index: SizeUInt): TMbcsByteType;
begin
Result:=mbSingleByte;
end;
Function StrCharLength(const Str: PChar): SizeInt;
begin
result:=widestringmanager.CharLengthPCharProc(Str);
end;
function StrNextChar(const Str: PChar): PChar;
begin
result:=Str+StrCharLength(Str);
end;
Function FindCmdLineSwitch(const Switch: string; const Chars: TSysCharSet;IgnoreCase: Boolean): Boolean;
Var
I,L : Integer;
S,T : String;
begin
Result:=False;
S:=Switch;
If IgnoreCase then
S:=UpperCase(S);
I:=ParamCount;
While (Not Result) and (I>0) do
begin
L:=Length(Paramstr(I));
If (L>0) and (ParamStr(I)[1] in Chars) then
begin
T:=Copy(ParamStr(I),2,L-1);
If IgnoreCase then
T:=UpperCase(T);
Result:=S=T;
end;
Dec(i);
end;
end;
Function FindCmdLineSwitch(const Switch: string; IgnoreCase: Boolean): Boolean;
begin
Result:=FindCmdLineSwitch(Switch,SwitchChars,IgnoreCase);
end;
Function FindCmdLineSwitch(const Switch: string): Boolean;
begin
Result:=FindCmdLineSwitch(Switch,SwitchChars,False);
end;
function WrapText(const Line, BreakStr: string; const BreakChars: TSysCharSet; MaxCol: Integer): string;
const
Quotes = ['''', '"'];
Var
L : String;
C,LQ,BC : Char;
P,BLen,Len : Integer;
HB,IBC : Boolean;
begin
Result:='';
L:=Line;
Blen:=Length(BreakStr);
If (BLen>0) then
BC:=BreakStr[1]
else
BC:=#0;
Len:=Length(L);
While (Len>0) do
begin
P:=1;
LQ:=#0;
HB:=False;
IBC:=False;
While ((P<=Len) and ((P<=MaxCol) or not IBC)) and ((LQ<>#0) or Not HB) do
begin
C:=L[P];
If (C=LQ) then
LQ:=#0
else If (C in Quotes) then
LQ:=C;
If (LQ<>#0) then
Inc(P)
else
begin
HB:=((C=BC) and (BreakStr=Copy(L,P,BLen)));
If HB then
Inc(P,Blen)
else
begin
If (P>=MaxCol) then
IBC:=C in BreakChars;
Inc(P);
end;
end;
// Writeln('"',C,'" : IBC : ',IBC,' HB : ',HB,' LQ : ',LQ,' P>MaxCol : ',P>MaxCol);
end;
Result:=Result+Copy(L,1,P-1);
Delete(L,1,P-1);
Len:=Length(L);
If (Len>0) and Not HB then
Result:=Result+BreakStr;
end;
end;
function WrapText(const Line: string; MaxCol: Integer): string;
begin
Result:=WrapText(Line,sLineBreak, [' ', '-', #9], MaxCol);
end;
{$ifndef FPC_NOGENERICANSIROUTINES}
{
Case Translation Tables
Can be used in internationalization support.
Although these tables can be obtained through system calls
cd it is better to not use those, since most implementation are not 100%
WARNING:
before modifying a translation table make sure that the current codepage
of the OS corresponds to the one you make changes to
}
const
{$if defined(MSDOS) or defined(GO32V2) or defined(WATCOM) or defined(WIN16) }
{ upper case translation table for character set 850 }
CP850UCT: array[128..255] of char =
(#128,#154,#144,#182,#142,#182,#143,#128,#210,#211,#212,#216,#215,#222,#142,#143,
#144,#146,#146,#226,#153,#227,#234,#235,'Y',#153,#154,#157,#156,#157,#158,#159,
#181,#214,#224,#233,#165,#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,#199,#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,#229,#229,#230,#237,#232,#233,#234,#235,#237,#237,#238,#239,
#240,#241,#242,#243,#244,#245,#246,#247,#248,#249,#250,#251,#252,#253,#254,#255);
{ lower case translation table for character set 850 }
CP850LCT: array[128..255] of char =
(#135,#129,#130,#131,#132,#133,#134,#135,#136,#137,#138,#139,#140,#141,#132,#134,
#130,#145,#145,#147,#148,#149,#150,#151,#152,#148,#129,#155,#156,#155,#158,#159,
#160,#161,#162,#163,#164,#164,#166,#167,#168,#169,#170,#171,#172,#173,#174,#175,
#176,#177,#178,#179,#180,#160,#131,#133,#184,#185,#186,#187,#188,#189,#190,#191,
#192,#193,#194,#195,#196,#197,#198,#198,#200,#201,#202,#203,#204,#205,#206,#207,
#208,#209,#136,#137,#138,#213,#161,#140,#139,#217,#218,#219,#220,#221,#141,#223,
#162,#225,#147,#149,#228,#228,#230,#237,#232,#163,#150,#151,#236,#236,#238,#239,
#240,#241,#242,#243,#244,#245,#246,#247,#248,#249,#250,#251,#252,#253,#254,#255);
{$endif}
{ upper case translation table for character set ISO 8859/1 Latin 1 }
CPISO88591UCT: array[192..255] of char =
( #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,
#192, #193, #194, #195, #196, #197, #198, #199,
#200, #201, #202, #203, #204, #205, #206, #207,
#208, #209, #210, #211, #212, #213, #214, #247,
#216, #217, #218, #219, #220, #221, #222, #89 );
{ lower case translation table for character set ISO 8859/1 Latin 1 }
CPISO88591LCT: array[192..255] of char =
( #224, #225, #226, #227, #228, #229, #230, #231,
#232, #233, #234, #235, #236, #237, #238, #239,
#240, #241, #242, #243, #244, #245, #246, #215,
#248, #249, #250, #251, #252, #253, #254, #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 );
{$endif FPC_NOGENERICANSIROUTINES}
function sscanf(const s: string; const fmt : string;const Pointers : array of Pointer) : Integer;
var
i,j,n,m : SizeInt;
s1 : string;
function GetInt(unsigned : boolean=false) : Integer;
begin
s1 := '';
while (Length(s) > n) and (s[n] = ' ') do
inc(n);
{ read sign }
if (Length(s)>= n) and (s[n] in ['+', '-']) then
begin
{ don't accept - when reading unsigned }
if unsigned and (s[n]='-') then
begin
result:=length(s1);
exit;
end
else
begin
s1:=s1+s[n];
inc(n);
end;
end;
{ read numbers }
while (Length(s) >= n) and
(s[n] in ['0'..'9']) do
begin
s1 := s1+s[n];
inc(n);
end;
Result := Length(s1);
end;
function GetFloat : Integer;
begin
s1 := '';
while (Length(s) > n) and (s[n] = ' ') do
inc(n);
while (Length(s) >= n) and
(s[n] in ['0'..'9', '+', '-', FormatSettings.DecimalSeparator, 'e', 'E']) do
begin
s1 := s1+s[n];
inc(n);
end;
Result := Length(s1);
end;
function GetString : Integer;
begin
s1 := '';
while (Length(s) > n) and (s[n] = ' ') do
inc(n);
while (Length(s) >= n) and (s[n] <> ' ')do
begin
s1 := s1+s[n];
inc(n);
end;
Result := Length(s1);
end;
function ScanStr(c : Char) : Boolean;
begin
while (Length(s) > n) and (s[n] <> c) do
inc(n);
inc(n);
If (n <= Length(s)) then
Result := True
else
Result := False;
end;
function GetFmt : Integer;
begin
Result := -1;
while true do
begin
while (Length(fmt) > m) and (fmt[m] = ' ') do
inc(m);
if (m >= Length(fmt)) then
break;
if (fmt[m] = '%') then
begin
inc(m);
case fmt[m] of
'd':
Result:=vtInteger;
{$ifndef FPUNONE}
'f':
Result:=vtExtended;
{$endif}
's':
Result:=vtString;
'c':
Result:=vtChar;
else
raise EFormatError.CreateFmt(SInvalidFormat,[fmt]);
end;
inc(m);
break;
end;
if not(ScanStr(fmt[m])) then
break;
inc(m);
end;
end;
begin
n := 1;
m := 1;
Result := 0;
for i:=0 to High(Pointers) do
begin
j := GetFmt;
case j of
vtInteger :
begin
if GetInt>0 then
begin
pLongint(Pointers[i])^:=StrToInt(s1);
inc(Result);
end
else
break;
end;
vtchar :
begin
if Length(s)>n then
begin
pchar(Pointers[i])^:=s[n];
inc(n);
inc(Result);
end
else
break;
end;
{$ifndef FPUNONE}
vtExtended :
begin
if GetFloat>0 then
begin
pextended(Pointers[i])^:=StrToFloat(s1);
inc(Result);
end
else
break;
end;
{$endif}
vtString :
begin
if GetString > 0 then
begin
pansistring(Pointers[i])^:=s1;
inc(Result);
end
else
break;
end;
else
break;
end;
end;
end;
{$macro on}
// Ansi version declaration
{$UNDEF SBUNICODE}
{$define SBChar:=AnsiChar}
{$define SBString:=AnsiString}
{$define TSBCharArray:=Array of SBChar}
{$define PSBChar:=PAnsiChar}
{$define SBRAWString:=RawByteString}
{$define TStringBuilder:=TAnsiStringBuilder}
{$i syssb.inc}
{$undef SBChar}
{$undef SBString}
{$undef TSBCharArray}
{$undef PSBChar}
{$undef SBRAWString}
{$undef TStringBuilder}
// Unicode version declaration
{$define SBUNICODE}
{$define SBChar:=WideChar}
{$define SBString:=UnicodeString}
{$define TSBCharArray:=Array of SBChar}
{$define PSBChar:=PWideChar}
{$define SBRAWString:=UnicodeString}
{$define TStringBuilder:=TUnicodeStringBuilder}
{$i syssb.inc}
{$undef SBChar}
{$undef SBString}
{$undef TSBCharArray}
{$undef PSBChar}
{$undef SBRAWString}
{$undef TStringBuilder}
{$undef SBUNICODE}
|