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 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337
|
{
***************************************************************************
* *
* This source 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; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code 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. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA. *
* *
***************************************************************************
Author: Mattias Gaertner
Abstract:
Basic pascal code functions. Many of the functions have counterparts in the
code tools, which are faster, more flexible and aware of compiler settings
and directives.
}
unit BasicCodeTools;
{$ifdef FPC}
{$mode objfpc}
{$else}
{$ifdef MSWindows}
{$define Windows}
{$endif}
{$endif}{$H+}
{$inline on}
interface
uses
Classes, SysUtils, strutils, Laz_AVL_Tree,
// LazUtils
LazFileUtils, LazUTF8,
// Codetools
SourceLog, KeywordFuncLists, FileProcs;
//----------------------------------------------------------------------------
{ These functions are used by the codetools }
// comments
function FindNextNonSpace(const ASource: string; StartPos: integer): integer;
function FindPrevNonSpace(const ASource: string; StartPos: integer): integer;
function FindCommentEnd(const ASource: string; StartPos: integer;
NestedComments: boolean): integer; overload;
function FindCommentEnd(Src: PChar; NestedComments: boolean): PChar; overload;
function IsCommentEnd(const ASource: string; EndPos: integer): boolean;
function FindNextComment(const ASource: string;
StartPos: integer; MaxPos: integer = 0): integer;
procedure FindCommentsInRange(const Src: string; StartPos, EndPos: integer;
out FirstCommentStart, FirstAtomStart, LastCommentEnd, LastAtomEnd: integer;
NestedComments: boolean = false);
function FindNextCompilerDirective(const ASource: string; StartPos: integer;
NestedComments: boolean): integer;
function FindNextCompilerDirectiveWithName(const ASource: string;
StartPos: integer; const DirectiveName: string;
NestedComments: boolean; out ParamPos: integer): integer;
function FindNextIncludeDirective(const ASource: string;
StartPos: integer; NestedComments: boolean;
out FilenameStartPos, FileNameEndPos,
CommentStartPos, CommentEndPos: integer): integer;
function FindNextIDEDirective(const ASource: string; StartPos: integer;
NestedComments: boolean; EndPos: integer = 0): integer;
function CleanCodeFromComments(const Src: string;
NestedComments: boolean; KeepDirectives: boolean = false;
KeepVerbosityDirectives: boolean = false): string;
function ExtractCommentContent(const ASource: string; CommentStart: integer;
NestedComments: boolean;
TrimStart: boolean = false; TrimEnd: boolean = false;
TrimPasDoc: boolean = false): string;
function FindMainUnitHint(const ASource: string; out Filename: string): boolean;
function InEmptyLine(const ASource: string; StartPos: integer): boolean;
function SkipResourceDirective(const ASource: string; StartPos, EndPos: integer;
NestedComments: boolean): integer;
// indent
function GetLineIndent(const Source: string; Position: integer): integer;
function GetLineIndentWithTabs(const Source: string; Position: integer;
TabWidth: integer): integer;
function GetPosInLine(const Source: string; Position: integer): integer; // 0 based
function GetBlockMinIndent(const Source: string;
StartPos, EndPos: integer): integer;
function GetIndentStr(Indent: integer; TabWidth: integer = 0): string;
procedure IndentText(const Source: string; Indent, TabWidth: integer;
out NewSource: string);
function FindFirstNonSpaceCharInLine(const Source: string;
Position: integer): integer;
function IsFirstNonSpaceCharInLine(const Source: string;
Position: integer): boolean;
procedure GuessIndentSize(const Source: string;
var IndentSize: integer; TabWidth: integer = 2; MaxLineCount: integer = 10000);
function ReIndent(const Source: string; OldIndent, OldTabWidth,
NewIndent, NewTabWidth: integer): string;
// identifiers
procedure GetIdentStartEndAtPosition(const Source:string; Position:integer;
out IdentStart,IdentEnd:integer);
function GetIdentStartPosition(const Source:string; Position:integer): integer;
function GetIdentLen(Identifier: PChar): integer;
function GetIdentifier(Identifier: PChar): string;
function FindNextIdentifier(const Source: string; StartPos, MaxPos: integer): integer;
function FindNextIdentifierSkipStrings(const Source: string;
StartPos, MaxPos: integer): integer;
function IsValidIdentPair(const NamePair: string): boolean;
function IsValidIdentPair(const NamePair: string; out First, Second: string): boolean;
function ExtractPasIdentifier(const Ident: string; AllowDots: Boolean): string;
// line/code ends
function SrcPosToLineCol(const s: string; Position: integer;
out Line, Col: integer): boolean;
procedure GetLineStartEndAtPosition(const Source: string; Position:integer;
out LineStart,LineEnd:integer); // LineEnd at first line break character
function GetLineStartPosition(const Source: string; Position:integer): integer;
function GetLineInSrc(const Source: string; Position:integer): string;
function LineEndCount(const Txt: string): integer; inline;
function LineEndCount(const Txt: string; out LengthOfLastLine:integer): integer; inline;
function LineEndCount(const Txt: string; StartPos, EndPos: integer;
out LengthOfLastLine:integer): integer;
function EmptyCodeLineCount(const Source: string; StartPos, EndPos: integer;
NestedComments: boolean): integer;
function PositionsInSameLine(const Source: string;
Pos1, Pos2: integer): boolean;
function FindLineEndOrCodeInFrontOfPosition(const Source: string;
Position, MinPosition: integer; NestedComments: boolean;
StopAtDirectives: boolean = true; SkipSemicolonComma: boolean = true;
SkipEmptyLines: boolean = false): integer;
function FindLineEndOrCodeAfterPosition(const Source: string;
Position, MaxPosition: integer; NestedComments: boolean;
StopAtDirectives: boolean = true; SkipEmptyLines: boolean = false;
IncludeLineEnd: boolean = false): integer;
function FindFirstLineEndInFrontOfInCode(const Source: string;
Position, MinPosition: integer; NestedComments: boolean): integer;
function FindFirstLineEndAfterInCode(const Source: string;
Position, MaxPosition: integer; NestedComments: boolean): integer;
function ChompLineEndsAtEnd(const s: string): string;
function ChompOneLineEndAtEnd(const s: string): string;
function TrimLineEnds(const s: string; TrimStart, TrimEnd: boolean): string;
// brackets
function GetBracketLvl(const Src: string; StartPos, EndPos: integer;
NestedComments: boolean): integer;
// replacements
function ReplacementNeedsLineEnd(const Source: string;
FromPos, ToPos, NewLength, MaxLineLength: integer): boolean;
function CountNeededLineEndsToAddForward(const Src: string;
StartPos, MinLineEnds: integer): integer;
function CountNeededLineEndsToAddBackward(const Src: string;
StartPos, MinLineEnds: integer): integer;
procedure AdjustPositionAfterInsert(var p: integer; IsStart: boolean;
FromPos, ToPos, DiffPos: integer);
// comparison
function CompareText(Txt1: PChar; Len1: integer; Txt2: PChar; Len2: integer;
CaseSensitive: boolean): integer; overload;
function CompareTextCT(const Txt1, Txt2: string;
CaseSensitive: boolean = false): integer; overload;
function CompareText(Txt1: PChar; Len1: integer; Txt2: PChar; Len2: integer;
CaseSensitive, IgnoreSpace: boolean): integer; overload;
function CompareTextIgnoringSpace(const Txt1, Txt2: string;
CaseSensitive: boolean): integer;
function CompareTextIgnoringSpace(Txt1: PChar; Len1: integer;
Txt2: PChar; Len2: integer; CaseSensitive: boolean): integer;
function CompareAnsiStringIgnoringSpaceIgnoreCase(Txt1, Txt2: pointer): integer;
function CompareSubStrings(const Find, Txt: string;
FindStartPos, TxtStartPos, Len: integer; CaseSensitive: boolean): integer;
function CompareIdentifiers(Identifier1, Identifier2: PChar): integer; {$IFDEF HasInline}inline;{$ENDIF}
function CompareIdentifiersCaseSensitive(Identifier1, Identifier2: PChar): integer;
function CompareIdentifierPtrs(Identifier1, Identifier2: Pointer): integer; {$IFDEF HasInline}inline;{$ENDIF}
function ComparePrefixIdent(PrefixIdent, Identifier: PChar): boolean;
function TextBeginsWith(Txt: PChar; TxtLen: integer; StartTxt: PChar;
StartTxtLen: integer; CaseSensitive: boolean): boolean;
function StrBeginsWith(const s, Prefix: string): boolean;
function IdentifierPos(Search, Identifier: PChar): PtrInt; // search Search in Identifier
function CompareAtom(p1, p2: PChar; NestedComments: boolean): integer;
function CompareStringConstants(p1, p2: PChar): integer; // compare case sensitive
function CompareComments(p1, p2: PChar; NestedComments: boolean): integer; // compare case insensitive
function FindDiff(const s1, s2: string): integer;
function dbgsDiff(Expected, Actual: string): string; overload;
// dotted identifiers
function DottedIdentifierLength(Identifier: PChar): integer;
function GetDottedIdentifier(Identifier: PChar): string;
function IsDottedIdentifier(const Identifier: string): boolean;
function CompareDottedIdentifiers(Identifier1, Identifier2: PChar): integer;
function ChompDottedIdentifier(const Identifier: string): string;
// space and special chars
function TrimCodeSpace(const ACode: string): string;
function CodeIsOnlySpace(const ACode: string; FromPos, ToPos: integer): boolean;
function StringToPascalConst(const s: string): string;
// string constants
function SplitStringConstant(const StringConstant: string;
FirstLineLength, OtherLineLengths, Indent: integer;
const aLineBreak: string): string;
procedure ImproveStringConstantStart(const ACode: string; var StartPos: integer);
procedure ImproveStringConstantEnd(const ACode: string; var EndPos: integer);
function HexStrToIntDef(p: PChar; Def: integer): integer;
// search
function SearchNextInText(Search: PChar; SearchLen: PtrInt;
Src: PChar; SrcLen: PtrInt;
StartPos: PtrInt;// 0 based
out MatchStart, MatchEnd: PtrInt;// 0 based
WholeWords: boolean = false; MultiLine: boolean = false): boolean;
procedure HasTxtWord(SearchWord, Txt: PChar; out WholeWord: boolean;
out Count: SizeInt);
// misc
function SubString(p: PChar; Count: SizeInt): string; overload;
// files
type
{ TUnitFileInfo }
TUnitFileInfo = class
private
FFilename: string;
FUnitName: string;
function GetFileUnitNameWithoutNamespace: string;
function GetIdentifierStartInUnitName: Integer;
public
constructor Create(const TheUnitName, TheFilename: string);
property FileUnitName: string read FUnitName;
property FileUnitNameWithoutNamespace: string read GetFileUnitNameWithoutNamespace;
property Filename: string read FFilename;
property IdentifierStartInUnitName: Integer read GetIdentifierStartInUnitName;
end;
{ TNameSpaceInfo }
TNameSpaceInfo = class
private
FUnitName: string;
FNamespace: string;
FIdentifierStartInUnitName: Integer;
public
constructor Create(const TheNamespace, TheUnitName: string; TheIdentifierStartInUnitName: Integer);
property UnitName: string read FUnitName;
property Namespace: string read FNamespace;
property IdentifierStartInUnitName: Integer read FIdentifierStartInUnitName;
end;
function ExtractFileNamespace(const Filename: string): string;
procedure AddToTreeOfUnitFilesOrNamespaces(
var TreeOfUnitFiles, TreeOfNameSpaces: TAVLTree;
const NameSpacePath, Filename: string;
CaseInsensitive, KeepDoubles: boolean);
function GatherUnitFiles(const BaseDir, SearchPath,
Extensions, NameSpacePath: string; KeepDoubles, CaseInsensitive: boolean;
var TreeOfUnitFiles, TreeOfNamespaces: TAVLTree): boolean;
procedure FreeTreeOfUnitFiles(TreeOfUnitFiles: TAVLTree);
procedure AddToTreeOfUnitFiles(var TreeOfUnitFiles: TAVLTree;
const Filename, Unitname: string;
KeepDoubles: boolean);
procedure AddToTreeOfNamespaces(var TreeOfNameSpaces: TAVLTree;
const UnitName, ParentNameSpacePath: string;
KeepDoubles: boolean);
function CompareUnitFileInfos(Data1, Data2: Pointer): integer;
function CompareNameSpaceInfos(Data1, Data2: Pointer): integer;
function CompareUnitNameAndUnitFileInfo(UnitnamePAnsiString,
UnitFileInfo: Pointer): integer;
function CompareNameSpaceAndNameSpaceInfo(NamespacePAnsiString,
NamespaceInfo: Pointer): integer;
// function filled by CodeToolManager to find inc files
var
FindIncFileInCfgCache: function(const Name: string; out ExpFilename: string): boolean;
//-----------------------------------------------------------------------------
// functions / procedures
{ These functions are not context sensitive. Especially they ignore compiler
settings and compiler directives. They exist only for basic usage.
}
// source type
function FindSourceType(const Source: string;
var SrcNameStart, SrcNameEnd: integer; NestedComments: boolean = false): string;
// identifier
function ReadDottedIdentifier(const Source: string; var Position: integer;
NestedComments: boolean = false): string;
function ReadDottedIdentifier(var Position: PChar; SrcEnd: PChar;
NestedComments: boolean = false): string;
// program name
function RenameProgramInSource(Source:TSourceLog;
const NewProgramName:string):boolean;
function FindProgramNameInSource(const Source:string;
out ProgramNameStart,ProgramNameEnd:integer):string;
// unit name
function RenameUnitInSource(Source:TSourceLog;const NewUnitName:string):boolean;
function FindUnitNameInSource(const Source:string;
out UnitNameStart,UnitNameEnd: integer; NestedComments: boolean = false):string;
function FindModuleNameInSource(const Source:string;
out ModuleType: string; out NameStart,NameEnd: integer;
NestedComments: boolean = false):string;
// uses sections
function UnitIsUsedInSource(const Source,SrcUnitName:string):boolean;
function RenameUnitInProgramUsesSection(Source:TSourceLog;
const OldUnitName, NewUnitName, NewInFile:string): boolean;
function AddToProgramUsesSection(Source:TSourceLog;
const AUnitName,InFileName:string):boolean;
function RemoveFromProgramUsesSection(Source:TSourceLog;
const AUnitName:string):boolean;
function RenameUnitInInterfaceUsesSection(Source:TSourceLog;
const OldUnitName, NewUnitName, NewInFile:string): boolean;
function AddToInterfaceUsesSection(Source:TSourceLog;
const AUnitName,InFileName:string):boolean;
function RemoveFromInterfaceUsesSection(Source:TSourceLog;
const AUnitName:string):boolean;
// single uses section
function IsUnitUsedInUsesSection(const Source,SrcUnitName:string;
UsesStart:integer):boolean;
function RenameUnitInUsesSection(Source:TSourceLog; UsesStart: integer;
const OldUnitName, NewUnitName, NewInFile:string): boolean;
function AddUnitToUsesSection(Source:TSourceLog;
const AnUnitName,InFilename:string; UsesStart:integer):boolean;
function RemoveUnitFromUsesSection(Source:TSourceLog;
const AnUnitName:string; UsesStart:integer):boolean;
// compiler directives
function FindIncludeDirective(const Source,Section:string; Index:integer;
out IncludeStart,IncludeEnd:integer):boolean;
function ExtractLongParamDirective(const Source: string; CommentStartPos: integer;
out DirectiveName, FileParam: string): boolean;
function SplitCompilerDirective(const Directive:string;
out DirectiveName,Parameters:string):boolean;
// createform
function AddCreateFormToProgram(Source:TSourceLog;
const AClassName,AName:string):boolean;
function RemoveCreateFormFromProgram(Source:TSourceLog;
const AClassName,AName:string):boolean;
function CreateFormExistsInProgram(const Source,
AClassName,AName:string):boolean;
function ListAllCreateFormsInProgram(const Source:string):TStrings;
// resource code
function FindResourceInCode(const Source, AddCode:string;
out Position,EndPosition:integer):boolean;
function AddResourceCode(Source:TSourceLog; const AddCode:string):boolean;
// form components
function FindFormClassDefinitionInSource(const Source, FormClassName:string;
var FormClassNameStartPos, FormBodyStartPos: integer):boolean;
function FindFormComponentInSource(const Source: string;
FormBodyStartPos: integer;
const ComponentName, ComponentClassName: string): integer;
function AddFormComponentToSource(Source:TSourceLog; FormBodyStartPos: integer;
const ComponentName, ComponentClassName: string): boolean;
function RemoveFormComponentFromSource(Source:TSourceLog;
FormBodyStartPos: integer;
ComponentName, ComponentClassName: string): boolean;
function FindClassAncestorName(const Source, FormClassName: string): string;
// procedure specifiers
function FindFirstProcSpecifier(const ProcText: string;
NestedComments: boolean = false): integer;
function SearchProcSpecifier(const ProcText, Specifier: string;
out SpecifierEndPosition: integer;
NestedComments: boolean = false;
WithSpaceBehindSemicolon: boolean = true): integer;
function RemoveProcSpecifier(const ProcText, Specifier: string;
NestedComments: boolean = false): string;
// code search
function SearchCodeInSource(const Source, Find: string; StartPos: integer;
out EndFoundPosition: integer; CaseSensitive: boolean;
NestedComments: boolean = false): integer;
function ReadNextPascalAtom(const Source: string;
var Position: integer; out AtomStart: integer; NestedComments: boolean = false;
SkipDirectives: boolean = false): string;
procedure ReadRawNextPascalAtom(const Source: string;
var Position: integer; out AtomStart: integer;
NestedComments: boolean = false; SkipDirectives: boolean = false);
procedure ReadRawNextPascalAtom(var Position: PChar; out AtomStart: PChar;
const SrcEnd: PChar = nil; NestedComments: boolean = false;
SkipDirectives: boolean = false);
procedure ReadPriorPascalAtom(const Source: string;
var Position: integer; out AtomEnd: integer; NestedComments: boolean = false);
function ReadTilPascalBracketClose(const Source: string;
var Position: integer; NestedComments: boolean = false): boolean;
function GetAtomLength(p: PChar; NestedComments: boolean): integer;
function GetAtomString(p: PChar; NestedComments: boolean): string;
function FindStartOfAtom(const Source: string; Position: integer): integer;
function FindEndOfAtom(const Source: string; Position: integer): integer;
//-----------------------------------------------------------------------------
const
MaxLineLength: integer = 80;
//=============================================================================
implementation
function Min(i1, i2: integer): integer; inline;
begin
if i1<=i2 then Result:=i1 else Result:=i2;
end;
function Max(i1, i2: integer): integer; inline;
begin
if i1>=i2 then Result:=i1 else Result:=i2;
end;
{ most simple code tools - just methods }
function FindIncludeDirective(const Source,Section:string; Index:integer;
out IncludeStart,IncludeEnd:integer):boolean;
var Atom,DirectiveName:string;
Position,EndPos,AtomStart:integer;
Filename:string;
begin
Result:=false;
// find section
Position:=SearchCodeInSource(Source,Section,1,EndPos,false);
if (Position<1) or (EndPos<1) then exit;
// search for include directives
repeat
Atom:=ReadNextPascalAtom(Source,Position,AtomStart);
if (copy(Atom,1,2)='{$') or (copy(Atom,1,3)='(*$') then begin
SplitCompilerDirective(Atom,DirectiveName,Filename);
DirectiveName:=lowercase(DirectiveName);
if (DirectiveName='i') or (DirectiveName='include') then begin
// include directive
dec(Index);
if Index=0 then begin
IncludeStart:=AtomStart;
IncludeEnd:=Position;
Result:=true;
exit;
end;
end;
end;
until Atom='';
end;
function ExtractLongParamDirective(const Source: string; CommentStartPos: integer;
out DirectiveName, FileParam: string): boolean;
var
p, StartPos: PChar;
begin
Result:=false;
FileParam:='';
if CommentStartPos>length(Source) then exit;
p:=@Source[CommentStartPos];
if (p^<>'{') or (p[1]<>'$') then exit;
inc(p,2);
StartPos:=p;
if not IsIdentStartChar[p^] then exit;
while IsIdentChar[p^] do inc(p);
DirectiveName:=copy(Source,StartPos-PChar(Source)+1,p-StartPos);
Result:=true;
while p^ in [' ',#9] do inc(p);
if p^='''' then begin
// 'param with spaces'
inc(p);
StartPos:=p;
while not (p^ in [#0,#10,#13,'''']) do inc(p);
end else begin
// param without spaces
StartPos:=p;
while not (p^ in [#0,#9,#10,#13,' ','}']) do inc(p);
end;
FileParam:=copy(Source,StartPos-PChar(Source)+1,p-StartPos);
end;
function SplitCompilerDirective(const Directive:string;
out DirectiveName,Parameters:string):boolean;
var EndPos,DirStart,DirEnd:integer;
begin
if (copy(Directive,1,2)='{$') or (copy(Directive,1,3)='(*$') then begin
if copy(Directive,1,2)='{$' then begin
DirStart:=3;
DirEnd:=length(Directive);
end else begin
DirStart:=4;
DirEnd:=length(Directive)-1;
end;
EndPos:=DirStart;
while (EndPos<DirEnd) and (IsIdentChar[Directive[EndPos]]) do
inc(EndPos);
DirectiveName:=lowercase(copy(Directive,DirStart,EndPos-DirStart));
Parameters:=copy(Directive,EndPos+1,DirEnd-EndPos-1);
Result:=true;
end else
Result:=false;
end;
function FindSourceType(const Source: string; var SrcNameStart,
SrcNameEnd: integer; NestedComments: boolean): string;
var
u: String;
p, AtomStart: Integer;
begin
// read first atom for type
SrcNameStart:=0;
SrcNameEnd:=0;
p:=1;
Result:=ReadNextPascalAtom(Source,p,AtomStart,NestedComments);
u:=Uppercase(Result);
if (u='UNIT') or (u='PROGRAM') or (u='LIBRARY') or (u='PACKAGE') then begin
// read name
ReadNextPascalAtom(Source,p,AtomStart,NestedComments);
if p<=AtomStart then exit;
if not IsIdentStartChar[Source[AtomStart]] then exit;
SrcNameStart:=AtomStart;
SrcNameEnd:=p;
repeat
ReadRawNextPascalAtom(Source,p,AtomStart,NestedComments);
if (AtomStart=p+1) and (Source[AtomStart]='.') then begin
ReadRawNextPascalAtom(Source,p,AtomStart,NestedComments);
if p<=AtomStart then exit;
if not IsIdentStartChar[Source[AtomStart]] then exit;
SrcNameEnd:=p;
end else
break;
until false;
end else begin
Result:='';
end;
end;
function RenameUnitInSource(Source:TSourceLog;const NewUnitName:string):boolean;
var UnitNameStart,UnitNameEnd:integer;
begin
UnitNameStart:=0;
UnitNameEnd:=0;
Result:=(FindUnitNameInSource(Source.Source,UnitNameStart,UnitNameEnd)<>'');
if Result then
Source.Replace(UnitNameStart,UnitNameEnd-UnitNameStart,NewUnitName);
end;
function FindUnitNameInSource(const Source: string; out UnitNameStart,
UnitNameEnd: integer; NestedComments: boolean): string;
var
ModuleType: string;
begin
Result:=FindModuleNameInSource(Source,ModuleType,UnitNameStart,UnitNameEnd,NestedComments);
if CompareText(ModuleType,'UNIT')<>0 then
Result:='';
end;
function FindModuleNameInSource(const Source: string; out ModuleType: string;
out NameStart, NameEnd: integer; NestedComments: boolean): string;
var
u: String;
p, AtomStart: Integer;
begin
// read first atom for type
Result:='';
NameStart:=0;
NameEnd:=0;
p:=1;
ModuleType:=ReadNextPascalAtom(Source,p,AtomStart,NestedComments);
u:=UpperCase(ModuleType);
if (u='UNIT') or (u='PROGRAM') or (u='LIBRARY') or (u='PACKAGE') then begin
// read name
ReadNextPascalAtom(Source,p,AtomStart,NestedComments);
if p<=AtomStart then exit;
if not IsIdentStartChar[Source[AtomStart]] then exit;
NameStart:=AtomStart;
NameEnd:=AtomStart;
Result:=ReadDottedIdentifier(Source,NameEnd,NestedComments);
end else
ModuleType:='';
end;
function ReadDottedIdentifier(const Source: string; var Position: integer;
NestedComments: boolean): string;
var
p: PChar;
begin
if (Position<1) or (Position>length(Source)) then exit('');
p:=@Source[Position];
Result:=ReadDottedIdentifier(p,PChar(Source)+length(Source),NestedComments);
Position:=p-PChar(Source)+1;
end;
function ReadDottedIdentifier(var Position: PChar; SrcEnd: PChar;
NestedComments: boolean): string;
var
AtomStart, p: PChar;
begin
Result:='';
p:=Position;
ReadRawNextPascalAtom(p,AtomStart,SrcEnd,NestedComments);
Position:=AtomStart;
if (AtomStart>=p) or not IsIdentStartChar[AtomStart^] then exit;
Result:=GetIdentifier(AtomStart);
repeat
ReadRawNextPascalAtom(p,AtomStart,SrcEnd,NestedComments);
if (AtomStart+1<>p) or (AtomStart^<>'.') then exit;
ReadRawNextPascalAtom(p,AtomStart,SrcEnd,NestedComments);
if (AtomStart>=p) or not IsIdentStartChar[AtomStart^] then exit;
Position:=AtomStart;
Result:=Result+'.'+GetIdentifier(AtomStart);
until false;
end;
function RenameProgramInSource(Source: TSourceLog;
const NewProgramName:string):boolean;
var ProgramNameStart,ProgramNameEnd:integer;
begin
Result:=(FindProgramNameInSource(Source.Source,
ProgramNameStart,ProgramNameEnd)<>'');
if Result then
Source.Replace(ProgramNameStart,
ProgramNameEnd-ProgramNameStart,NewProgramName)
end;
function FindProgramNameInSource(const Source:string;
out ProgramNameStart,ProgramNameEnd:integer):string;
begin
ProgramNameStart:=0;
ProgramNameEnd:=0;
if uppercasestr(FindSourceType(Source,ProgramNameStart,ProgramNameEnd))=
'PROGRAM'
then
Result:=copy(Source,ProgramNameStart,ProgramNameEnd-ProgramNameStart)
else
Result:='';
end;
function UnitIsUsedInSource(const Source,SrcUnitName:string):boolean;
// search in all uses sections
var UsesStart,UsesEnd:integer;
begin
Result:=false;
repeat
UsesStart:=SearchCodeInSource(Source,'uses',1,UsesEnd,false);
if UsesEnd=0 then ;
if UsesStart>0 then begin
if IsUnitUsedInUsesSection(Source,SrcUnitName,UsesStart) then begin
Result:=true;
exit;
end;
end;
until UsesStart<1;
end;
function RenameUnitInProgramUsesSection(Source:TSourceLog;
const OldUnitName, NewUnitName, NewInFile:string): boolean;
var
ProgramTermStart,ProgramTermEnd,
UsesStart,UsesEnd:integer;
begin
Result:=false;
// search Program section
ProgramTermStart:=SearchCodeInSource(Source.Source,'program',1,ProgramTermEnd
,false);
if ProgramTermStart<1 then exit;
// search programname
ReadNextPascalAtom(Source.Source,ProgramTermEnd,ProgramTermStart);
// search semicolon after programname
if not (ReadNextPascalAtom(Source.Source,ProgramTermEnd,ProgramTermStart)=';')
then exit;
UsesEnd:=ProgramTermEnd;
ReadNextPascalAtom(Source.Source,UsesEnd,UsesStart);
if UsesEnd>length(Source.Source) then exit;
if not (lowercase(copy(Source.Source,UsesStart,UsesEnd-UsesStart))='uses')
then begin
// no uses section in interface -> add one
Source.Insert(ProgramTermEnd,LineEnding+LineEnding+'uses'+LineEnding+' ;');
UsesEnd:=ProgramTermEnd;
ReadNextPascalAtom(Source.Source,UsesEnd,UsesStart);
end;
if not (lowercase(copy(Source.Source,UsesStart,UsesEnd-UsesStart))='uses')
then exit;
Result:=RenameUnitInUsesSection(Source,UsesStart,OldUnitName
,NewUnitName,NewInFile);
end;
function AddToProgramUsesSection(Source:TSourceLog;
const AUnitName,InFileName:string):boolean;
var
ProgramTermStart,ProgramTermEnd,
UsesStart,UsesEnd:integer;
begin
Result:=false;
if (AUnitName='') or (AUnitName=';') then exit;
// search program
ProgramTermStart:=SearchCodeInSource(Source.Source,'program',1,ProgramTermEnd
,false);
if ProgramTermStart<1 then exit;
// search programname
ReadNextPascalAtom(Source.Source,ProgramTermEnd,ProgramTermStart);
// search semicolon after programname
if not (ReadNextPascalAtom(Source.Source,ProgramTermEnd,ProgramTermStart)=';')
then exit;
// search uses section
UsesEnd:=ProgramTermEnd;
ReadNextPascalAtom(Source.Source,UsesEnd,UsesStart);
if UsesEnd>length(Source.Source) then exit;
if not (lowercase(copy(Source.Source,UsesStart,UsesEnd-UsesStart))='uses')
then begin
// no uses section after program term -> add one
Source.Insert(ProgramTermEnd,LineEnding+LineEnding+'uses'+LineEnding+' ;');
UsesEnd:=ProgramTermEnd;
ReadNextPascalAtom(Source.Source,UsesEnd,UsesStart);
end;
if not (lowercase(copy(Source.Source,UsesStart,UsesEnd-UsesStart))='uses')
then exit;
Result:=AddUnitToUsesSection(Source,AUnitName,InFileName,UsesStart);
end;
function RenameUnitInInterfaceUsesSection(Source:TSourceLog;
const OldUnitName, NewUnitName, NewInFile:string): boolean;
var
InterfaceStart,InterfaceWordEnd,
UsesStart,UsesEnd:integer;
begin
Result:=false;
// search interface section
InterfaceStart:=SearchCodeInSource(Source.Source,'interface',1
,InterfaceWordEnd,false);
if InterfaceStart<1 then exit;
UsesEnd:=InterfaceWordEnd;
ReadNextPascalAtom(Source.Source,UsesEnd,UsesStart);
if UsesEnd>length(Source.Source) then exit;
if not (lowercase(copy(Source.Source,UsesStart,UsesEnd-UsesStart))='uses')
then begin
// no uses section in interface -> add one
Source.Insert(InterfaceWordEnd,LineEnding+LineEnding+'uses'+LineEnding+' ;');
UsesEnd:=InterfaceWordEnd;
ReadNextPascalAtom(Source.Source,UsesEnd,UsesStart);
end;
if not (lowercase(copy(Source.Source,UsesStart,UsesEnd-UsesStart))='uses')
then exit;
Result:=RenameUnitInUsesSection(Source,UsesStart,OldUnitName
,NewUnitName,NewInFile);
end;
function AddToInterfaceUsesSection(Source:TSourceLog;
const AUnitName,InFileName:string):boolean;
var
InterfaceStart,InterfaceWordEnd,
UsesStart,UsesEnd:integer;
begin
Result:=false;
if AUnitName='' then exit;
// search interface section
InterfaceStart:=SearchCodeInSource(Source.Source,'interface',1
,InterfaceWordEnd,false);
if InterfaceStart<1 then exit;
UsesEnd:=InterfaceWordEnd;
ReadNextPascalAtom(Source.Source,UsesEnd,UsesStart);
if UsesEnd>length(Source.Source) then exit;
if not (lowercase(copy(Source.Source,UsesStart,UsesEnd-UsesStart))='uses')
then begin
// no uses section in interface -> add one
Source.Insert(InterfaceWordEnd,LineEnding+LineEnding+'uses'+LineEnding+' ;');
UsesEnd:=InterfaceWordEnd;
ReadNextPascalAtom(Source.Source,UsesEnd,UsesStart);
end;
if not (lowercase(copy(Source.Source,UsesStart,UsesEnd-UsesStart))='uses')
then exit;
Result:=AddUnitToUsesSection(Source,AUnitName,InFileName,UsesStart);
end;
function RemoveFromProgramUsesSection(Source:TSourceLog;
const AUnitName:string):boolean;
var
ProgramTermStart,ProgramTermEnd,
UsesStart,UsesEnd:integer;
Atom:string;
begin
Result:=false;
if AUnitName='' then exit;
// search program
ProgramTermStart:=SearchCodeInSource(Source.Source,'program',1
,ProgramTermEnd,false);
if ProgramtermStart<1 then exit;
// search programname
ReadNextPascalAtom(Source.Source,ProgramTermEnd,ProgramTermStart);
// search semicolon after programname
if not (ReadNextPascalAtom(Source.Source,ProgramTermEnd,ProgramTermStart)=';')
then exit;
UsesEnd:=ProgramTermEnd;
Atom:=ReadNextPascalAtom(Source.Source,UsesEnd,UsesStart);
if UsesEnd>length(Source.Source) then exit;
if not (lowercase(Atom)='uses') then exit;
Result:=RemoveUnitFromUsesSection(Source,AUnitName,UsesStart);
end;
function RemoveFromInterfaceUsesSection(Source:TSourceLog;
const AUnitName:string):boolean;
var
InterfaceStart,InterfaceWordEnd,
UsesStart,UsesEnd:integer;
Atom:string;
begin
Result:=false;
if AUnitName='' then exit;
// search interface section
InterfaceStart:=SearchCodeInSource(Source.Source,'interface',1
,InterfaceWordEnd,false);
if InterfaceStart<1 then exit;
UsesEnd:=InterfaceWordEnd;
Atom:=ReadNextPascalAtom(Source.Source,UsesEnd,UsesStart);
if UsesEnd>length(Source.Source) then exit;
if not (lowercase(Atom)='uses') then exit;
Result:=RemoveUnitFromUsesSection(Source,AUnitName,UsesStart);
end;
function IsUnitUsedInUsesSection(const Source,SrcUnitName:string;
UsesStart:integer):boolean;
var UsesEnd:integer;
Atom:string;
begin
Result:=false;
if SrcUnitName='' then exit;
if UsesStart<1 then exit;
if not (lowercase(copy(Source,UsesStart,4))='uses') then exit;
UsesEnd:=UsesStart+4;
// parse through all used units and see if it is there
repeat
Atom:=ReadNextPascalAtom(Source,UsesEnd,UsesStart);
if (lowercase(Atom)=lowercase(SrcUnitName)) then begin
// unit found
Result:=true;
exit;
end;
// read til next comma or semicolon
repeat
Atom:=ReadNextPascalAtom(Source,UsesEnd,UsesStart);
until (Atom=',') or (Atom=';') or (Atom='');
until Atom<>',';
// unit not used
Result:=true;
end;
function RenameUnitInUsesSection(Source:TSourceLog; UsesStart: integer;
const OldUnitName, NewUnitName, NewInFile:string): boolean;
var UsesEnd:integer;
LineStart,LineEnd,OldUsesStart:integer;
s,Atom,NewUnitTerm:string;
begin
Result:=false;
if (OldUnitName='') then begin
Result:=AddUnitToUsesSection(Source,NewUnitName,NewInFile,UsesStart);
exit;
end;
if (NewUnitName='') or (NewUnitName=';')
or (OldUnitName=';') or (UsesStart<1) then exit;
UsesEnd:=UsesStart+4;
if not (lowercase(copy(Source.Source,UsesStart,4))='uses') then exit;
// parse through all used units and see if it is already there
if NewInFile<>'' then
NewUnitTerm:=NewUnitName+' in '''+NewInFile+''''
else
NewUnitTerm:=NewUnitName;
s:=', ';
repeat
Atom:=ReadNextPascalAtom(Source.Source,UsesEnd,UsesStart);
if (lowercase(Atom)=lowercase(OldUnitName)) then begin
// unit already used
OldUsesStart:=UsesStart;
// find comma or semicolon
repeat
Atom:=ReadNextPascalAtom(Source.Source,UsesEnd,UsesStart);
until (Atom=',') or (Atom=';') or (Atom='');
Source.Replace(OldUsesStart,UsesStart-OldUsesStart,NewUnitTerm);
Result:=true;
exit;
end else if (Atom=';') then begin
s:=' ';
break;
end;
// read til next comma or semicolon
while (Atom<>',') and (Atom<>';') and (Atom<>'') do
Atom:=ReadNextPascalAtom(Source.Source,UsesEnd,UsesStart);
until Atom<>',';
// unit not used yet -> add it
Source.Insert(UsesStart,s+NewUnitTerm);
GetLineStartEndAtPosition(Source.Source,UsesStart,LineStart,LineEnd);
if (LineEnd-LineStart>MaxLineLength) or (NewInFile<>'') then
Source.Insert(UsesStart,LineEnding+' ');
Result:=true;
end;
function AddUnitToUsesSection(Source:TSourceLog;
const AnUnitName,InFilename:string; UsesStart:integer):boolean;
var UsesEnd:integer;
LineStart,LineEnd:integer;
s,Atom,NewUnitTerm:string;
begin
Result:=false;
if (AnUnitName='') or (AnUnitName=';') or (UsesStart<1) then exit;
UsesEnd:=UsesStart+4;
if not (lowercase(copy(Source.Source,UsesStart,4))='uses') then exit;
// parse through all used units and see if it is already there
s:=', ';
repeat
Atom:=ReadNextPascalAtom(Source.Source,UsesEnd,UsesStart);
if (lowercase(Atom)=lowercase(AnUnitName)) then begin
// unit found
Result:=true;
exit;
end else if (Atom=';') then begin
s:=' ';
break;
end;
// read til next comma or semicolon
while (Atom<>',') and (Atom<>';') and (Atom<>'') do
Atom:=ReadNextPascalAtom(Source.Source,UsesEnd,UsesStart);
until Atom<>',';
// unit not used yet -> add it
if InFilename<>'' then
NewUnitTerm:=AnUnitName+' in '''+InFileName+''''
else
NewUnitTerm:=AnUnitName;
Source.Insert(UsesStart,s+NewUnitTerm);
GetLineStartEndAtPosition(Source.Source,UsesStart,LineStart,LineEnd);
if (LineEnd-LineStart>MaxLineLength) or (InFileName<>'') then
Source.Insert(UsesStart,LineEnding+' ');
Result:=true;
end;
function RemoveUnitFromUsesSection(Source:TSourceLog; const AnUnitName:string;
UsesStart:integer):boolean;
var UsesEnd,OldUsesStart,OldUsesEnd:integer;
Atom:string;
begin
Result:=false;
if (UsesStart<1) or (AnUnitName='') or (AnUnitName=',') or (AnUnitName=';') then
exit;
// search interface section
UsesEnd:=UsesStart+4;
if not (lowercase(copy(Source.Source,UsesStart,4))='uses') then exit;
// parse through all used units and see if it is there
OldUsesEnd:=-1;
repeat
Atom:=ReadNextPascalAtom(Source.Source,UsesEnd,UsesStart);
if (lowercase(Atom)=lowercase(AnUnitName)) then begin
// unit found
OldUsesStart:=UsesStart;
// find comma or semicolon
repeat
Atom:=ReadNextPascalAtom(Source.Source,UsesEnd,UsesStart);
until (Atom=',') or (Atom=';') or (Atom='');
if OldUsesEnd<1 then
// first used unit
Source.Delete(OldUsesStart,UsesStart-OldUsesStart)
else
// not first used unit (remove comma in front of AnUnitName too)
Source.Delete(OldUsesEnd,UsesStart-OldUsesEnd);
Result:=true;
exit;
end else
OldUsesEnd:=UsesEnd;
// read til next comma or semicolon
while (Atom<>',') and (Atom<>';') and (Atom<>'') do
Atom:=ReadNextPascalAtom(Source.Source,UsesEnd,UsesStart);
until Atom<>',';
// unit not used
end;
function AddCreateFormToProgram(Source:TSourceLog;
const AClassName,AName:string):boolean;
// insert 'Application.CreateForm(<AClassName>,<AName>);'
// in front of 'Application.Run;'
var Position, EndPosition: integer;
begin
Result:=false;
Position:=SearchCodeInSource(Source.Source,'application.run',1
,EndPosition,false);
if Position<1 then exit;
if EndPosition=0 then ;
Source.Insert(Position,
'Application.CreateForm('+AClassName+','+AName+');'+LineEnding+' ');
Result:=true;
end;
function RemoveCreateFormFromProgram(Source:TSourceLog;
const AClassName,AName:string):boolean;
// remove 'Application.CreateForm(<AClassName>,<AName>);'
var Position,EndPosition,AtomStart:integer;
begin
Result:=false;
Position:=SearchCodeInSource(Source.Source,
'application.createform('+AClassName+','+AName+')',1,EndPosition,false);
if Position<1 then exit;
if ReadNextPascalAtom(Source.Source,EndPosition,AtomStart)=';' then
ReadNextPascalAtom(Source.Source,EndPosition,AtomStart);
EndPosition:=AtomStart;
Source.Delete(Position,EndPosition-Position);
Result:=true;
end;
function CreateFormExistsInProgram(const Source,
AClassName,AName:string):boolean;
var Position,EndPosition:integer;
begin
Position:=SearchCodeInSource(Source,
'application.createform('+AClassName+','+AName+')',1,EndPosition,false);
Result:=Position>0;
if EndPosition=0 then ;
end;
function ListAllCreateFormsInProgram(const Source:string):TStrings;
// list format: <formname>:<formclassname>
var Position, EndPosition: integer;
s:string;
begin
Result:=TStringList.Create;
Position:=1;
repeat
Position:=SearchCodeInSource(Source,
'application.createform(',Position,EndPosition,false);
if Position>0 then begin
s:=ReadNextPascalAtom(Source,EndPosition,Position);
ReadNextPascalAtom(Source,EndPosition,Position);
s:=ReadNextPascalAtom(Source,EndPosition,Position)+':'+s;
Result.Add(s);
end;
until Position<1;
end;
function FindResourceInCode(const Source, AddCode:string;
out Position,EndPosition:integer):boolean;
var Find,Atom:string;
FindPosition,FindAtomStart,SemicolonPos:integer;
begin
Result:=false;
if AddCode='' then begin
Result:=true;
exit;
end;
if Source='' then exit;
// search "LazarusResources.Add('<ResourceName>',"
FindPosition:=1;
repeat
Atom:=ReadNextPascalAtom(AddCode,FindPosition,FindAtomStart);
until (Atom='') or (Atom=',');
if Atom='' then exit;
// search the resource start in code
Find:=copy(AddCode,1,FindPosition-1);
Position:=SearchCodeInSource(Source,Find,1,EndPosition,false);
if Position<1 then exit;
// search resource end in code
SemicolonPos:=SearchCodeInSource(Source,');',EndPosition,EndPosition,false);
if SemicolonPos<1 then exit;
Result:=true;
end;
function AddResourceCode(Source:TSourceLog; const AddCode:string):boolean;
var StartPos,EndPos:integer;
begin
if FindResourceInCode(Source.Source,AddCode,StartPos,EndPos) then begin
// resource exists already -> replace it
Source.Replace(StartPos,EndPos-StartPos,AddCode);
end else begin
// add resource
Source.Insert(length(Source.Source)+1,LineEnding+AddCode);
end;
Result:=true;
end;
function FindFormClassDefinitionInSource(const Source, FormClassName:string;
var FormClassNameStartPos, FormBodyStartPos: integer):boolean;
var AtomEnd,AtomStart: integer;
begin
Result:=false;
if FormClassName='' then exit;
repeat
FormClassNameStartPos:=SearchCodeInSource(Source,
FormClassName+'=class(TForm)',1,FormBodyStartPos,false);
if FormClassNameStartPos<1 then exit;
AtomEnd:=FormBodyStartPos;
until ReadNextPascalAtom(Source,AtomEnd,AtomStart)<>';';
Result:=true;
end;
function FindFormComponentInSource(const Source: string;
FormBodyStartPos: integer;
const ComponentName, ComponentClassName: string): integer;
var AtomStart, OldPos: integer;
Atom,LowComponentName,LowComponentClassName: string;
begin
LowComponentName:=lowercase(ComponentName);
LowComponentClassName:=lowercase(ComponentClassName);
Result:=FormBodyStartPos;
repeat
Atom:=lowercase(ReadNextPascalAtom(Source,Result,AtomStart));
if (Atom='public') or (Atom='private') or (Atom='end')
or (Atom='protected') or (Atom='') then begin
Result:=-1;
exit;
end;
OldPos:=Result;
if (lowercase(ReadNextPascalAtom(Source,Result,AtomStart))=LowComponentName)
and (ReadNextPascalAtom(Source,Result,AtomStart)=':')
and (lowercase(ReadNextPascalAtom(Source,Result,AtomStart))=
LowComponentClassName)
and (ReadNextPascalAtom(Source,Result,AtomStart)=';') then begin
Result:=OldPos;
exit;
end;
until Result>length(Source);
Result:=-1;
end;
function AddFormComponentToSource(Source:TSourceLog; FormBodyStartPos: integer;
const ComponentName, ComponentClassName: string): boolean;
var Position, AtomStart: integer;
Atom: string;
PriorSpaces, NextSpaces: string;
begin
Result:=false;
if FindFormComponentInSource(Source.Source,FormBodyStartPos
,ComponentName,ComponentClassName)>0 then begin
Result:=true;
exit;
end;
Position:=FormBodyStartPos;
repeat
// find a good position to insert the component
// in front of next section and in front of procedures/functions
Atom:=lowercase(ReadNextPascalAtom(Source.Source,Position,AtomStart));
if (Atom='procedure') or (Atom='function') or (Atom='end') or (Atom='class')
or (Atom='constructor') or (Atom='destructor')
or (Atom='public') or (Atom='private') or (Atom='protected')
or (Atom='published') or (Atom='class') or (Atom='property') then begin
// insert component definition in source
if (Atom='public') or (Atom='private') or (Atom='protected')
or (Atom='published') then begin
PriorSpaces:=' ';
NextSpaces:=' ';
end else begin
PriorSpaces:='';
NextSpaces:=' ';
end;
Source.Insert(AtomStart,
PriorSpaces+ComponentName+': '+ComponentClassName+';'+LineEnding
+NextSpaces);
Result:=true;
exit;
end;
until Position>length(Source.Source);
Result:=false;
end;
function RemoveFormComponentFromSource(Source:TSourceLog;
FormBodyStartPos: integer;
ComponentName, ComponentClassName: string): boolean;
var AtomStart, Position, ComponentStart, LineStart, LineEnd: integer;
Atom: string;
begin
ComponentName:=lowercase(ComponentName);
ComponentClassName:=lowercase(ComponentClassName);
Position:=FormBodyStartPos;
repeat
Atom:=lowercase(ReadNextPascalAtom(Source.Source,Position,AtomStart));
if (Atom='public') or (Atom='private') or (Atom='end')
or (Atom='protected') or (Atom='') then begin
Result:=false;
exit;
end;
if (Atom=ComponentName) then begin
ComponentStart:=AtomStart;
if (ReadNextPascalAtom(Source.Source,Position,AtomStart)=':')
and (lowercase(ReadNextPascalAtom(Source.Source,Position,AtomStart))=
ComponentClassName)
then begin
GetLineStartEndAtPosition(Source.Source,ComponentStart,LineStart,LineEnd);
if (LineEnd<=length(Source.Source))
and (Source.Source[LineEnd] in [#10,#13]) then begin
inc(LineEnd);
if (LineEnd<=length(Source.Source))
and (Source.Source[LineEnd] in [#10,#13])
and (Source.Source[LineEnd]<>Source.Source[LineEnd-1]) then
inc(LineEnd);
end;
Source.Delete(LineStart,LineEnd-LineStart);
Result:=true;
exit;
end;
end;
until Atom='';
Result:=true;
end;
function FindClassAncestorName(const Source, FormClassName: string): string;
var
SrcPos, AtomStart: integer;
begin
Result:='';
if SearchCodeInSource(Source,FormClassName+'=class(',1,SrcPos,false)<1 then
exit;
Result:=ReadNextPascalAtom(Source,SrcPos,AtomStart);
if not IsValidIdent(Result) then
Result:='';
end;
function SearchCodeInSource(const Source, Find: string; StartPos: integer;
out EndFoundPosition: integer; CaseSensitive: boolean;
NestedComments: boolean):integer;
// search pascal atoms of <Find> in <Source>
// returns the start pos
// -1 on failure
var
FindLen: Integer;
SrcLen: Integer;
Position: Integer;
FirstFindPos: Integer;
FindAtomStart: Integer;
AtomStart: Integer;
FindAtomLen: Integer;
AtomLen: Integer;
SrcPos: Integer;
FindPos: Integer;
SrcAtomStart: Integer;
FirstFindAtomStart: Integer;
begin
Result:=-1;
if (Find='') or (StartPos>length(Source)) then exit;
FindLen:=length(Find);
SrcLen:=length(Source);
Position:=StartPos;
AtomStart:=StartPos;
FirstFindPos:=1;
FirstFindAtomStart:=1;
// search first atom in find
ReadRawNextPascalAtom(Find,FirstFindPos,FirstFindAtomStart,NestedComments);
FindAtomLen:=FirstFindPos-FirstFindAtomStart;
if FirstFindAtomStart>FindLen then exit;
repeat
// read next atom
ReadRawNextPascalAtom(Source,Position,AtomStart,NestedComments);
if AtomStart>SrcLen then exit;
AtomLen:=Position-AtomStart;
if (AtomLen=FindAtomLen)
and (CompareText(@Find[FirstFindAtomStart],FindAtomLen,
@Source[AtomStart],AtomLen,CaseSensitive)=0)
then begin
// compare all atoms
SrcPos:=Position;
SrcAtomStart:=SrcPos;
FindPos:=FirstFindPos;
FindAtomStart:=FindPos;
repeat
// read the next atom from the find
ReadRawNextPascalAtom(Find,FindPos,FindAtomStart,NestedComments);
if FindAtomStart>FindLen then begin
// found !
EndFoundPosition:=SrcPos;
Result:=AtomStart;
exit;
end;
// read the next atom from the source
ReadRawNextPascalAtom(Source,SrcPos,SrcAtomStart,NestedComments);
// compare
if (CompareText(@Find[FindAtomStart],FindPos-FindAtomStart,
@Source[SrcAtomStart],SrcPos-SrcAtomStart,
CaseSensitive)<>0)
then
break;
until false;
end;
until false;
end;
function FindCommentEnd(Src: PChar; NestedComments: boolean): PChar;
// returns position after the comment end, e.g. after }
var
CommentLvl: integer;
begin
Result:=Src;
if Result=nil then exit;
case Result^ of
'/':
if Result[1]='/' then begin
inc(Result,2);
while not (Result^ in [#0,#10,#13]) do inc(Result);
end;
'{':
begin
inc(Result);
if Result^=#3 then begin
// codetools skip comment
inc(Result);
repeat
case Result^ of
#0: break;
#3:
if Result[1]='}' then begin
inc(Result,2);
break;
end;
end;
inc(Result);
until false;
end else begin
// pascal comment {}
CommentLvl:=1;
repeat
case Result^ of
#0: break;
'{':
if NestedComments then
inc(CommentLvl);
'}':
begin
dec(CommentLvl);
if CommentLvl=0 then begin
inc(Result);
break;
end;
end;
end;
inc(Result);
until false;
end;
end;
'(':
if Result[1]='*' then begin
inc(Result,2);
CommentLvl:=1;
repeat
if Result^=#0 then break;
if (Result^='*') and (Result[1]=')') then begin
inc(Result,2);
dec(CommentLvl);
if CommentLvl=0 then
break;
end else if (Result^='(') and (Result[1]='*') and NestedComments then begin
inc(Result,2);
inc(CommentLvl);
end else
inc(Result);
until false;
end;
end;
end;
function IsCommentEnd(const ASource: string; EndPos: integer): boolean;
// return true if EndPos on } or on *) or in a // comment
var
LineStart: LongInt;
begin
Result:=false;
if EndPos<1 then exit;
if EndPos>length(ASource) then exit;
if ASource[EndPos]='}' then begin
// delphi or codetools comment end
Result:=true;
exit;
end;
if (EndPos>1) and (ASource[EndPos]=')') and (ASource[EndPos-1]='*') then begin
// TP comment end
Result:=true;
exit;
end;
// test for Delphi comment //
// skip line end
LineStart:=EndPos;
if ASource[LineStart] in [#10,#13] then begin
dec(LineStart);
if (LineStart>=1) and (ASource[LineStart] in [#10,#13])
and (ASource[LineStart]<>ASource[LineStart+1]) then
dec(LineStart);
if LineStart<1 then exit;
end;
// find line start
while (LineStart>1) and (not (ASource[LineStart-1] in [#10,#13])) do
dec(LineStart);
// find first non space char in line
while (LineStart<=EndPos) and (ASource[LineStart] in [' ',#9]) do
inc(LineStart);
if (LineStart<EndPos)
and (ASource[LineStart]='/') and (ASource[LineStart+1]='/') then begin
// Delphi comment end
Result:=true;
exit;
end;
end;
function FindNextComment(const ASource: string; StartPos: integer;
MaxPos: integer): integer;
// if not found: Result=MaxPos+1
var
NotFoundPos: Integer;
begin
NotFoundPos:=MaxPos;
if (MaxPos>length(ASource)) or (MaxPos<1) then
MaxPos:=length(ASource);
Result:=StartPos;
while (Result<=MaxPos) do begin
case ASource[Result] of
'''':
begin
inc(Result);
while (Result<=MaxPos) do begin
if (ASource[Result] in ['''',#10,#13]) then
break;
inc(Result);
end;
end;
'/':
if (Result<MaxPos) and (ASource[Result+1]='/') then
exit;
'{':
exit;
'(':
if (Result<MaxPos) and (ASource[Result+1]='*') then
exit;
end;
inc(Result);
end;
if Result>MaxPos then
if NotFoundPos>=1 then
Result:=NotFoundPos+1
else
Result:=MaxPos+1;
end;
procedure FindCommentsInRange(const Src: string; StartPos, EndPos: integer;
out FirstCommentStart, FirstAtomStart, LastCommentEnd, LastAtomEnd: integer;
NestedComments: boolean);
var
p: PChar;
i: integer;
AtomStart: integer;
SrcLen: Integer;
begin
FirstCommentStart:=0;
FirstAtomStart:=0;
LastCommentEnd:=0;
LastAtomEnd:=0;
SrcLen:=length(Src);
if (StartPos<1) then StartPos:=1;
if StartPos>SrcLen then exit;
if EndPos>SrcLen then EndPos:=SrcLen+1;
i:=StartPos;
while i<EndPos do begin
p:=@Src[i];
// skip space
while IsSpaceChar[p^] do inc(p);
i:=p-PChar(Src)+1;
if i>=EndPos then exit;
if (p^='{') or ((p^='(') and (p[1]='*')) or ((p^='/') and (p[1]='/')) then
begin
// a comment
if FirstCommentStart=0 then
FirstCommentStart:=i;
i:=FindCommentEnd(Src,i,NestedComments);
if LastCommentEnd=0 then
LastCommentEnd:=i;
end else begin
// normal atom
if FirstAtomStart=0 then
FirstAtomStart:=i;
ReadRawNextPascalAtom(Src,i,AtomStart);
if LastAtomEnd=0 then
LastAtomEnd:=i;
end;
end;
end;
function FindNextCompilerDirective(const ASource: string; StartPos: integer;
NestedComments: boolean): integer;
var
MaxPos: integer;
begin
MaxPos:=length(ASource);
Result:=StartPos;
while (Result<=MaxPos) do begin
case ASource[Result] of
'''':
begin
inc(Result);
while (Result<=MaxPos) do begin
if (ASource[Result]<>'''') then
inc(Result)
else begin
inc(Result);
break;
end;
end;
end;
'/':
begin
inc(Result);
if (Result<=MaxPos) and (ASource[Result]='/') then begin
// skip Delphi comment
while (Result<=MaxPos) and (not (ASource[Result] in [#10,#13])) do
inc(Result);
end;
end;
'{':
begin
if (Result<MaxPos) and (ASource[Result+1]='$') then
exit;
// skip pascal comment
Result:=FindCommentEnd(ASource,Result,NestedComments);
end;
'(':
begin
if (Result<MaxPos) and (ASource[Result+1]='*') then begin
if (Result+2<=MaxPos) and (ASource[Result+2]='$') then
exit;
// skip TP comment
Result:=FindCommentEnd(ASource,Result,NestedComments);
end else
inc(Result);
end;
else
inc(Result);
end;
end;
if Result>MaxPos+1 then Result:=MaxPos+1;
end;
function FindNextCompilerDirectiveWithName(const ASource: string;
StartPos: integer; const DirectiveName: string;
NestedComments: boolean; out ParamPos: integer): integer;
var
Offset: Integer;
SrcLen: Integer;
begin
Result:=StartPos;
ParamPos:=0;
SrcLen:=length(ASource);
repeat
Result:=FindNextCompilerDirective(ASource,Result,NestedComments);
if (Result<1) or (Result>SrcLen) then break;
if (ASource[Result]='{') then
Offset:=2
else if ASource[Result]='(' then
Offset:=3
else
Offset:=-1;
if Offset>0 then begin
if (CompareIdentifiers(PChar(Pointer(DirectiveName)),// pointer type cast avoids #0 check
@ASource[Result+Offset])=0)
then begin
ParamPos:=FindNextNonSpace(ASource,Result+Offset+length(DirectiveName));
exit;
end;
end;
Result:=FindCommentEnd(ASource,Result,NestedComments);
until false;
Result:=-1;
end;
function FindNextNonSpace(const ASource: string; StartPos: integer
): integer;
var
SrcLen: integer;
begin
SrcLen:=length(ASource);
Result:=StartPos;
while (Result<=SrcLen) and (ASource[Result] in [' ',#9,#10,#13]) do
inc(Result);
end;
function FindPrevNonSpace(const ASource: string; StartPos: integer
): integer;
begin
Result:=StartPos;
while (Result>=1) and (ASource[Result] in [' ',#9,#10,#13]) do
dec(Result);
end;
function FindCommentEnd(const ASource: string; StartPos: integer;
NestedComments: boolean): integer;
// returns position after the comment end, e.g. after }
// failure: returns length(ASource)+1
var
CommentLvl: integer;
p: PChar;
begin
Result:=StartPos;
if Result<1 then exit;
if Result>length(ASource) then exit;
p:=@ASource[Result];
case p^ of
'/':
begin
if p[1]='/' then begin
// skip Delphi comment
while (not (p^ in [#0,#10,#13])) do
inc(p);
end;
end;
'{':
begin
inc(p);
if p^=#3 then begin
// Codetools skip comment {#3 #3}
inc(p);
repeat
case p^ of
#0:
if p-PChar(ASource)>=length(ASource) then break;
#3:
if p[1]='}' then begin
inc(p,2);
break;
end;
end;
inc(p);
until false;
end else begin
// Pascal comment {}
CommentLvl:=1;
repeat
case p^ of
#0:
if p-PChar(ASource)>=length(ASource) then break;
'{':
if NestedComments then
inc(CommentLvl);
'}':
begin
dec(CommentLvl);
if CommentLvl=0 then begin
inc(p);
break;
end;
end;
end;
inc(p);
until false;
end;
end;
'(':
if (p[1]='*') then begin
inc(p,2);
CommentLvl:=1;
repeat
if (p^=#0) then begin
if p-PChar(ASource)>=length(ASource) then break;
inc(p);
end else if (p^='(') and (p[1]='*') and NestedComments then begin
inc(p,2);
inc(CommentLvl);
end else if (p^='*') and (p[1]=')') then begin
inc(p,2);
dec(CommentLvl);
if CommentLvl=0 then break;
end else
inc(p);
until false;
end;
end;
Result:=p-PChar(ASource)+1;
end;
procedure GetLineStartEndAtPosition(const Source:string; Position:integer;
out LineStart,LineEnd:integer);
begin
if Position<1 then begin
LineStart:=0;
LineEnd:=0;
exit;
end;
if Position>length(Source)+1 then begin
LineStart:=length(Source)+1;
LineEnd:=LineStart;
exit;
end;
LineStart:=Position;
while (LineStart>1) and (not (Source[LineStart-1] in [#10,#13])) do
dec(LineStart);
LineEnd:=Position;
while (LineEnd<=length(Source)) and (not (Source[LineEnd] in [#10,#13])) do
inc(LineEnd);
end;
function LineEndCount(const Txt: string; StartPos, EndPos: integer; out
LengthOfLastLine: integer): integer;
var
l: Integer;
p, LineStart: PChar;
begin
Result:=0;
LengthOfLastLine:=0;
l:=length(Txt);
if l=0 then exit;
if StartPos>l then exit;
if EndPos>l then EndPos:=l+1;
if StartPos>=EndPos then exit;
p:=@Txt[StartPos];
LineStart:=p;
repeat
if p^ in [#0,#10,#13] then begin
if p-PChar(Txt)+1>=EndPos then
break;
if p^<>#0 then begin
inc(Result);
if (p[1] in [#10,#13]) and (p^<>p[1]) and (p-PChar(Txt)+1<EndPos) then
inc(p,2)
else
inc(p);
LineStart:=p;
continue;
end;
end;
inc(p);
until false;
LengthOfLastLine:=EndPos-(LineStart-PChar(Txt)+1);
end;
function EmptyCodeLineCount(const Source: string; StartPos, EndPos: integer;
NestedComments: boolean): integer;
{ search forward for a line end or code
ignore line ends in comments
Result is Position of Start of Line End
}
var
SrcLen: integer;
SrcPos: Integer;
CommentEndPos: Integer;
begin
Result:=0;
SrcLen:=length(Source);
if EndPos>SrcLen then EndPos:=SrcLen+1;
SrcPos:=StartPos;
while (SrcPos<EndPos) do begin
case Source[SrcPos] of
'{','(','/':
begin
CommentEndPos:=FindCommentEnd(Source,SrcPos,NestedComments);
if CommentEndPos>SrcPos then
SrcPos:=CommentEndPos
else
inc(SrcPos); // not a comment start => skip char
end;
#10,#13:
begin
// skip line end
inc(SrcPos);
if (SrcPos<EndPos) and (Source[SrcPos] in [#10,#13])
and (Source[SrcPos]<>Source[SrcPos-1]) then
inc(SrcPos);
// count empty lines
if (SrcPos<EndPos) and (Source[SrcPos] in [#10,#13]) then
inc(Result);
end;
else
inc(SrcPos);
end;
end;
end;
function PositionsInSameLine(const Source: string;
Pos1, Pos2: integer): boolean;
var
StartPos: Integer;
EndPos: Integer;
begin
if Pos1<Pos2 then begin
StartPos:=Pos1;
EndPos:=Pos2;
end else begin
StartPos:=Pos2;
EndPos:=Pos1;
end;
if EndPos>length(Source) then EndPos:=length(Source);
while StartPos<EndPos do begin
if Source[StartPos] in [#10,#13] then begin
Result:=false;
exit;
end else
inc(StartPos);
end;
Result:=true;
end;
procedure GetIdentStartEndAtPosition(const Source: string; Position: integer;
out IdentStart, IdentEnd: integer);
// on success: IdentStart<IdentEnd
begin
IdentStart:=Position;
IdentEnd:=Position;
if (Position<1) or (Position>length(Source)+1) then exit;
while (IdentStart>1)
and (IsIdentChar[Source[IdentStart-1]]) do
dec(IdentStart);
while (IdentEnd<=length(Source))
and (IsIdentChar[Source[IdentEnd]]) do
inc(IdentEnd);
while (IdentStart<Position)
and (not IsIdentStartChar[Source[IdentStart]]) do
inc(IdentStart);
if (IdentStart>1) and (Source[IdentStart-1]='&') then
dec(IdentStart);
if (IdentStart>length(Source)) or not IsIdentStartChar[Source[IdentStart]] then
IdentEnd:=IdentStart;
end;
function GetIdentStartPosition(const Source: string; Position: integer
): integer;
begin
Result:=Position;
if (Result<1) or (Result>length(Source)+1) then exit;
while (Result>1)
and (IsIdentChar[Source[Result-1]]) do
dec(Result);
while (Result<Position)
and (not IsIdentStartChar[Source[Result]]) do
inc(Result);
if (Result>1) and (Source[Result-1]='&') then
dec(Result);
end;
function GetIdentLen(Identifier: PChar): integer;
begin
Result:=0;
if Identifier=nil then exit;
if not IsIdentStartChar[Identifier^] then exit;
while (IsIdentChar[Identifier[Result]]) do inc(Result);
end;
function FindFirstProcSpecifier(const ProcText: string; NestedComments: boolean
): integer;
// length(ProcText)+1 on failure
var
AtomStart: integer;
p: Integer;
begin
Result:=length(ProcText)+1;
// read till first semicolon
p:=1;
while p<=length(ProcText) do begin
ReadRawNextPascalAtom(ProcText,p,AtomStart,NestedComments,true);
if AtomStart>length(ProcText) then exit;
if ProcText[AtomStart] in ['[','('] then begin
if not ReadTilPascalBracketClose(ProcText,p,NestedComments) then
exit;
end else if ProcText[AtomStart]=';' then begin
ReadRawNextPascalAtom(ProcText,p,AtomStart,NestedComments,true);
Result:=AtomStart;
exit;
end;
end;
end;
function SearchProcSpecifier(const ProcText, Specifier: string; out
SpecifierEndPosition: integer; NestedComments: boolean;
WithSpaceBehindSemicolon: boolean): integer;
// Result = -1 on failure
// Result = start of Specifier on success
// SpecifierEndPosition on semicolon or >length(ProcText)
// if WithSpaceBehindSemicolon then SpecifierEndPosition is start of next specifier
var
AtomStart: integer;
begin
Result:=FindFirstProcSpecifier(ProcText,NestedComments);
repeat
if Result>length(ProcText) then exit(-1);
ReadRawNextPascalAtom(ProcText,Result,AtomStart,NestedComments,true);
if AtomStart>length(ProcText) then exit(-1);
if CompareIdentifiers(@ProcText[AtomStart],@Specifier[1])=0 then begin
Result:=AtomStart;
break;
end;
if ProcText[AtomStart] in ['[','('] then begin
if not ReadTilPascalBracketClose(ProcText,Result,NestedComments)
then
exit(-1);
end;
until false;
SpecifierEndPosition:=Result;
while (SpecifierEndPosition<=length(ProcText))
and (ProcText[SpecifierEndPosition]<>';') do begin
ReadRawNextPascalAtom(ProcText,SpecifierEndPosition,AtomStart,NestedComments,true);
if AtomStart>length(ProcText) then exit;
if ProcText[AtomStart] in ['[','('] then begin
if not ReadTilPascalBracketClose(ProcText,SpecifierEndPosition,NestedComments)
then
exit(-1);
end;
end;
if WithSpaceBehindSemicolon and (SpecifierEndPosition<=length(ProcText)) then
begin
SpecifierEndPosition:=FindLineEndOrCodeAfterPosition(ProcText,
SpecifierEndPosition+1,0,NestedComments);
end;
//DebugLn(['SearchProcSpecifier ',copy(ProcText,Result,SpecifierEndPosition-Result)]);
end;
function RemoveProcSpecifier(const ProcText, Specifier: string;
NestedComments: boolean): string;
var
EndPos: integer;
StartPos: LongInt;
begin
Result:=ProcText;
StartPos:=SearchProcSpecifier(Result,Specifier,EndPos,NestedComments);
if StartPos>=1 then
Result:=copy(Result,1,StartPos-1)+copy(Result,EndPos,length(Result));
end;
function ReadNextPascalAtom(const Source: string; var Position: integer; out
AtomStart: integer; NestedComments: boolean; SkipDirectives: boolean): string;
begin
ReadRawNextPascalAtom(Source,Position,AtomStart,NestedComments,SkipDirectives);
Result:=copy(Source,AtomStart,Position-AtomStart);
end;
{$IFOPT R+}{$DEFINE RangeChecking}{$ENDIF}
{$R-}
procedure ReadRawNextPascalAtom(const Source: string;
var Position: integer; out AtomStart: integer; NestedComments: boolean;
SkipDirectives: boolean);
var
Len:integer;
SrcPos, SrcStart, SrcAtomStart: PChar;
begin
Len:=length(Source);
if Position>Len then begin
Position:=Len+1;
AtomStart:=Position;
exit;
end;
SrcStart:=PChar(Source);
SrcPos:=@Source[Position];
ReadRawNextPascalAtom(SrcPos,SrcAtomStart,SrcStart+len,NestedComments,SkipDirectives);
Position:=SrcPos-SrcStart+1;
AtomStart:=SrcAtomStart-SrcStart+1;
end;
{$IFDEF RangeChecking}{$R+}{$UNDEF RangeChecking}{$ENDIF}
procedure ReadRawNextPascalAtom(var Position: PChar; out AtomStart: PChar;
const SrcEnd: PChar; NestedComments: boolean; SkipDirectives: boolean);
var
c1,c2:char;
CommentLvl: Integer;
Src: PChar;
begin
Src:=Position;
// read till next atom
while true do begin
case Src^ of
#0:
if (SrcEnd=nil) or (Src>=SrcEnd) then
break
else
inc(Src);
#1..#32: // spaces and special characters
inc(Src);
#$EF:
if (Src[1]=#$BB)
and (Src[2]=#$BF) then begin
// skip UTF BOM
inc(Src,3);
end else begin
break;
end;
'{': // comment start or compiler directive
begin
if (Src[1]='$') and (not SkipDirectives) then
// compiler directive
break
else if Src[1]=#3 then begin
// codetools comment => skip
inc(Src,2);
repeat
case Src^ of
#0:
if (SrcEnd=nil) or (Src>=SrcEnd) then
break;
#3:
if Src[1]='}' then begin
inc(Src,2);
break;
end;
end;
inc(Src);
until false;
end else begin
// Pascal comment => skip
CommentLvl:=1;
while true do begin
inc(Src);
case Src^ of
#0:
if (SrcEnd=nil) or (Src>=SrcEnd) then
break;
'{':
if NestedComments then
inc(CommentLvl);
'}':
begin
dec(CommentLvl);
if CommentLvl=0 then begin
inc(Src);
break;
end;
end;
end;
end;
end;
end;
'/': // comment or real division
if (Src[1]='/') then begin
// comment start -> read til line end
inc(Src);
while not (Src^ in [#0,#10,#13]) do
inc(Src);
end else
break;
'(': // comment, bracket or compiler directive
if (Src[1]='*') then begin
if (Src[2]='$') and (not SkipDirectives) then
// compiler directive
break
else begin
// comment start -> read til comment end
inc(Src,2);
CommentLvl:=1;
while true do begin
case Src^ of
#0:
if (SrcEnd=nil) or (Src>=SrcEnd) then
break
else
inc(Src);
'(':
if NestedComments and (Src[1]='*') then
inc(CommentLvl);
'*':
if (Src[1]=')') then begin
dec(CommentLvl);
if CommentLvl=0 then begin
inc(Src,2);
break;
end;
inc(Position);
end;
end;
inc(Src);
end;
end;
end else
// round bracket open
break;
else
break;
end;
end;
// read atom
AtomStart:=Src;
c1:=Src^;
case c1 of
#0:
;
'A'..'Z','a'..'z','_':
begin
// identifier
inc(Src);
while IsIdentChar[Src^] do
inc(Src);
end;
'0'..'9': // number
begin
inc(Src);
// read numbers
while (Src^ in ['0'..'9']) do
inc(Src);
if (Src^='.')
and (Src[1]<>'.') then begin
// real type number
inc(Src);
while (Src^ in ['0'..'9']) do
inc(Src);
end;
if (Src^ in ['e','E']) then begin
// read exponent
inc(Src);
if (Src^='-') then inc(Src);
while (Src^ in ['0'..'9']) do
inc(Src);
end;
end;
'''','#': // string constant
begin
while true do begin
case Src^ of
#0:
if (SrcEnd=nil) or (Src>=SrcEnd) then
break
else
inc(Src);
'#':
begin
inc(Src);
while Src^ in ['0'..'9'] do
inc(Src);
end;
'''':
begin
inc(Src);
while not (Src^ in ['''',#0]) do
inc(Src);
if Src^='''' then
inc(Src);
end;
else
break;
end;
end;
end;
'$': // hex constant
begin
inc(Src);
while IsHexNumberChar[Src^] do
inc(Src);
end;
'&': // octal constant or keyword as identifier (e.g. &label)
begin
inc(Src);
if Src^ in ['0'..'7'] then begin
while Src^ in ['0'..'7'] do
inc(Src);
end else begin
while IsIdentChar[Src^] do
inc(Src);
end;
end;
'{': // compiler directive (it can not be a comment, because see above)
begin
CommentLvl:=1;
while true do begin
inc(Src);
case Src^ of
#0:
if (SrcEnd=nil) or (Src>=SrcEnd) then
break;
'{':
if NestedComments then
inc(CommentLvl);
'}':
begin
dec(CommentLvl);
if CommentLvl=0 then begin
inc(Src);
break;
end;
end;
end;
end;
end;
'(': // bracket or compiler directive
if (Src[1]='*') then begin
// compiler directive -> read til comment end
inc(Src,2);
while (Src^<>#0) and ((Src^<>'*') or (Src[1]<>')')) do
inc(Src);
inc(Src,2);
end else
// round bracket open
inc(Src);
#192..#255:
begin
// read UTF8 character
inc(Src);
if ((ord(c1) and %11100000) = %11000000) then begin
// could be 2 byte character
if (ord(Src[0]) and %11000000) = %10000000 then
inc(Src);
end
else if ((ord(c1) and %11110000) = %11100000) then begin
// could be 3 byte character
if ((ord(Src[0]) and %11000000) = %10000000)
and ((ord(Src[1]) and %11000000) = %10000000) then
inc(Src,2);
end
else if ((ord(c1) and %11111000) = %11110000) then begin
// could be 4 byte character
if ((ord(Src[0]) and %11000000) = %10000000)
and ((ord(Src[1]) and %11000000) = %10000000)
and ((ord(Src[2]) and %11000000) = %10000000) then
inc(Src,3);
end;
end;
else
inc(Src);
c2:=Src^;
// test for double char operators :=, +=, -=, /=, *=, <>, <=, >=, **
if ((c2='=') and (IsEqualOperatorStartChar[c1]))
or ((c1='<') and (c2='>'))
or ((c1='.') and (c2='.'))
or ((c1='*') and (c2='*'))
then
inc(Src)
else if ((c1='@') and (c2='@')) then begin
// @@ label
repeat
inc(Src);
until (not IsIdentChar[Src^]);
end;
end;
Position:=Src;
end;
procedure ReadPriorPascalAtom(const Source: string; var Position: integer; out
AtomEnd: integer; NestedComments: boolean);
var
CommentLvl, PrePos, OldPrePos: integer;
IsStringConstant: boolean;
procedure ReadStringConstantBackward;
var PrePos: integer;
begin
while (Position>1) do begin
case Source[Position-1] of
'''':
begin
dec(Position);
repeat
dec(Position);
until (Position<1) or (Source[Position] in [#0,#10,#13,'''']);
end;
'0'..'9','A'..'Z','a'..'z':
begin
// test if char constant
PrePos:=Position-1;
while (PrePos>1) and (IsHexNumberChar[Source[PrePos]]) do
dec(PrePos);
if (PrePos<1) then break;
if (Source[PrePos]='$') then begin
dec(PrePos);
if (PrePos<1) then break;
end;
if (Source[PrePos]='#') then
Position:=PrePos
else
break;
end;
else
break;
end;
end;
end;
procedure ReadBackTilCodeLineEnd;
begin
dec(Position);
if (Position>=1) and (Source[Position] in [#10,#13])
and (Source[Position+1]<>Source[Position]) then
dec(Position);
// read backwards till line start
PrePos:=Position;
while (PrePos>=1) and (not (Source[PrePos] in [#10,#13])) do
dec(PrePos);
// read line forward to find out,
// if line ends in comment or string constant
IsStringConstant:=false;
repeat
inc(PrePos);
case Source[PrePos] of
'/':
if Source[PrePos+1]='/' then begin
// this was a delphi comment -> skip comment
Position:=PrePos-1;
break;
end;
'{':
begin
inc(PrePos);
if (PrePos<=Position) and (Source[PrePos]=#3) then begin
// skip codetools comment
inc(PrePos);
while (PrePos<=Position) do begin
if (Source[PrePos]=#3) and (PrePos<Position)
and (Source[PrePos+1]='}') then begin
inc(PrePos,2);
break;
end;
inc(PrePos);
end;
end else begin
// skip pascal comment
CommentLvl:=1;
while (PrePos<=Position) do begin
case Source[PrePos] of
'{': if NestedComments then inc(CommentLvl);
'}':
begin
dec(CommentLvl);
if CommentLvl=0 then break;
end;
end;
inc(PrePos);
end;
end;
end;
'(':
if Source[PrePos+1]='*' then begin
// skip turbo pascal comment
inc(PrePos,2);
while (PrePos<Position)
and ((Source[PrePos]<>'*') or (Source[PrePos+1]<>')')) do
inc(PrePos);
inc(PrePos);
end;
'''':
begin
// a string constant -> skip it
OldPrePos:=PrePos;
while (PrePos<Position) do begin
inc(PrePos);
case Source[PrePos] of
'''':
break;
#0,#10,#13:
begin
// string constant right border is the line end
// -> last atom of line found
IsStringConstant:=true;
break;
end;
end;
end;
if IsStringConstant then break;
end;
#10,#13:
// no comment and no string constant found
break;
end;
until PrePos>=Position;
end;
type
TNumberType = (ntDecimal, ntHexadecimal, ntBinary, ntIdentifier,
ntCharConstant, ntFloat, ntFloatWithExponent);
TNumberTypes = set of TNumberType;
const
AllNumberTypes: TNumberTypes = [ntDecimal, ntHexadecimal, ntBinary,
ntIdentifier, ntCharConstant, ntFloat, ntFloatWithExponent];
var c1, c2: char;
ForbiddenNumberTypes: TNumberTypes;
begin
// Skip all spaces and comments
CommentLvl:=0;
dec(Position);
IsStringConstant:=false;
OldPrePos:=0;
while Position>=1 do begin
if IsCommentEndChar[Source[Position]] then begin
case Source[Position] of
'}':
begin
dec(Position);
if (Position>=1) and (Source[Position]=#3) then begin
// codetools skip comment {#3 #3}
dec(Position);
while (Position>=1) do begin
if (Source[Position]=#3) and (Position>1)
and (Source[Position-1]='}') then begin
dec(Position,2);
break;
end;
dec(Position);
end;
end else begin
// pascal comment {}
CommentLvl:=1;
while (Position>=1) and (CommentLvl>0) do begin
case Source[Position] of
'}': if NestedComments then inc(CommentLvl);
'{': dec(CommentLvl);
end;
dec(Position);
end;
end;
end;
#10,#13: // possible Delphi comment
ReadBackTilCodeLineEnd;
')': // old turbo pascal comment
if (Position>1) and (Source[Position-1]='*') then begin
dec(Position,3);
while (Position>=1)
and ((Source[Position]<>'(') or (Source[Position+1]<>'*')) do
dec(Position);
dec(Position);
end else
break;
end;
end else if IsSpaceChar[Source[Position]] then begin
repeat
dec(Position);
until (Position<1) or (Source[Position] in [#10,#13])
or (not (IsSpaceChar[Source[Position]]));
end else begin
break;
end;
end;
// Position now points to the last char of the prior atom
AtomEnd:=Position+1;
if Position<1 then begin
Position:=1;
AtomEnd:=1;
exit;
end;
// read atom
if IsStringConstant then begin
Position:=OldPrePos;
if (Position>1) and (Source[Position-1]='''') then begin
ReadStringConstantBackward;
end;
exit;
end;
c2:=Source[Position];
case c2 of
'_','A'..'Z','a'..'z':
begin
// identifier or keyword or hexnumber
while (Position>1) do begin
if (IsIdentChar[Source[Position-1]]) then
dec(Position)
else begin
case UpChars[Source[Position-1]] of
'@':
// assembler label
if (Position>2)
and (Source[Position-2]='@') then
dec(Position,2);
'$':
// hex number
dec(Position);
end;
break;
end;
end;
end;
'''':
begin
inc(Position);
ReadStringConstantBackward;
end;
'0'..'9':
begin
// could be a decimal number, an identifier, a hex number,
// a binary number, a char constant, a float, a float with exponent
ForbiddenNumberTypes:=[];
while true do begin
case UpChars[Source[Position]] of
'0'..'1':
;
'2'..'9':
ForbiddenNumberTypes:=ForbiddenNumberTypes+[ntBinary];
'A'..'D','F':
ForbiddenNumberTypes:=ForbiddenNumberTypes
+[ntBinary,ntDecimal,ntCharConstant,ntFloat,ntFloatWithExponent];
'E':
ForbiddenNumberTypes:=ForbiddenNumberTypes
+[ntBinary,ntDecimal,ntCharConstant,ntFloat];
'G'..'Z','_':
ForbiddenNumberTypes:=AllNumberTypes-[ntIdentifier];
'.':
begin
// could be the point of a float
if (ntFloat in ForbiddenNumberTypes)
or (Position<=1) or (Source[Position-1]='.') then begin
inc(Position);
break;
end;
dec(Position);
// this was the part of a float after the point
// -> read decimal in front
ForbiddenNumberTypes:=AllNumberTypes-[ntDecimal];
end;
'+','-':
begin
// could be part of an exponent
if (ntFloatWithExponent in ForbiddenNumberTypes)
or (Position<=1)
or (not (Source[Position-1] in ['e','E']))
then begin
inc(Position);
break;
end;
dec(Position);
// this was the exponent of a float -> read the float
ForbiddenNumberTypes:=AllNumberTypes-[ntFloat];
end;
'#': // char constant found
begin
if (ntCharConstant in ForbiddenNumberTypes) then
inc(Position);
ReadStringConstantBackward;
break;
end;
'$':
begin
// hexadecimal number found
if (ntHexadecimal in ForbiddenNumberTypes) then
inc(Position);
break;
end;
'%':
begin
// binary number found
if (ntBinary in ForbiddenNumberTypes) then
inc(Position);
break;
end;
'@':
begin
if (Position=1) or (Source[Position-1]<>'@')
or (([ntIdentifier,ntDecimal]*ForbiddenNumberTypes)=[]) then
// atom start found
inc(Position)
else
// label found
dec(Position);
break;
end;
else
begin
inc(Position);
break;
end;
end;
if ForbiddenNumberTypes=AllNumberTypes then begin
inc(Position);
break;
end;
if Position<=1 then break;
dec(Position);
end;
if IsIdentStartChar[Source[Position]] then begin
// it is an identifier
end;
end;
';': ;
':': ;
',': ;
'(': ;
')': ;
'[': ;
']': ;
else
begin
if Position>1 then begin
c1:=Source[Position-1];
// test for double char operators :=, +=, -=, /=, *=, <>, <=, >=, **, ><
if ((c2='=') and (IsEqualOperatorStartChar[c1]))
or ((c1='<') and (c2='>'))
or ((c1='>') and (c2='<'))
or ((c1='.') and (c2='.'))
or ((c1='*') and (c2='*'))
or ((c1='@') and (c2='@'))
then begin
dec(Position);
end;
end;
end;
end;
end;
function ReadTilPascalBracketClose(const Source: string; var Position: integer;
NestedComments: boolean): boolean;
// Input: Position points right after the opening bracket
// Output: Position points right after the closing bracket
var
CloseBracket: Char;
AtomStart: LongInt;
Len: Integer;
begin
Result:=false;
Len:=length(Source);
if Position>Len+1 then
exit; // no bracket open found
case Source[Position-1] of
'{': CloseBracket:='}';
'(': CloseBracket:=')';
'[': CloseBracket:=']';
else
exit; // no bracket open found
end;
AtomStart:=Position;
while Position<=Len do begin
ReadRawNextPascalAtom(Source,Position,AtomStart,NestedComments,true);
//DebugLn(['ReadTilPascalBracketClose ',copy(Source,AtomStart,Position-AtomStart)]);
if Position>Len then
exit; // CloseBracket not found
case Source[AtomStart] of
'{','(','[':
begin
if not ReadTilPascalBracketClose(Source,Position) then exit;
end;
'}',')',']':
if Source[AtomStart]=CloseBracket then begin
// CloseBracket found
Result:=true;
exit;
end else begin
exit; // a bracket is closed, that was never opened
end;
end;
end;
end;
function GetAtomLength(p: PChar; NestedComments: boolean): integer;
var
c1: Char;
CommentLvl: Integer;
c2: Char;
OldP: PChar;
begin
OldP:=p;
// read atom
c1:=p^;
case c1 of
'A'..'Z','a'..'z','_':
begin
// identifier
inc(p);
while (IsIdentChar[p^]) do
inc(p);
end;
'0'..'9': // number
begin
inc(p);
// read numbers
while (p^ in ['0'..'9']) do
inc(p);
if (p^='.')
and (p[1]<>'.') then begin
// real type number
inc(p);
while (p^ in ['0'..'9']) do
inc(p);
end;
if (p^ in ['e','E']) then begin
// read exponent
inc(p);
if (p^='-') then inc(p);
while (p^ in ['0'..'9']) do
inc(p);
end;
end;
'''','#': // string constant
begin
while true do begin
case p^ of
'#':
begin
inc(p);
while (p^ in ['0'..'9']) do
inc(p);
end;
'''':
begin
inc(p);
while not (p^ in ['''',#10,#13]) do
inc(p);
inc(p);
end;
else
break;
end;
end;
end;
'$': // hex constant
begin
inc(p);
while (IsHexNumberChar[p^]) do
inc(p);
end;
'{': // compiler directive
begin
CommentLvl:=1;
while true do begin
inc(p);
case p^ of
#0: break;
'{': if NestedComments then
inc(CommentLvl);
'}':
begin
dec(CommentLvl);
if CommentLvl=0 then begin
inc(p);
break;
end;
end;
end;
end;
end;
'(': // bracket or compiler directive
begin
inc(p);
if (p^<>'*') then begin
// round bracket open
end else begin
// comment
CommentLvl:=1;
inc(p);
while true do begin
case p^ of
#0: break;
'*':
if p[1]=')' then begin
inc(p,2);
dec(CommentLvl);
if CommentLvl=0 then
break;
end else
inc(p);
'(':
if (p[1]='*') and NestedComments then begin
inc(CommentLvl);
inc(p,2);
end else
inc(p);
else
inc(p);
end;
end;
end;
end;
else
inc(p);
c2:=p^;
// test for double char operators :=, +=, -=, /=, *=, <>, <=, >=, **, .., ><
if ((c2='=') and (IsEqualOperatorStartChar[c1]))
or ((c1='<') and (c2='>'))
or ((c1='>') and (c2='<'))
or ((c1='.') and (c2='.'))
or ((c1='*') and (c2='*'))
then
inc(p)
else if ((c1='@') and (c2='@')) then begin
// @@ label
repeat
inc(p);
until (not IsIdentChar[p^]);
end;
end;
Result:=P-OldP;
end;
function GetAtomString(p: PChar; NestedComments: boolean): string;
var
l: LongInt;
begin
if p=nil then exit('');
l:=GetAtomLength(p,NestedComments);
SetLength(Result,l);
if l>0 then
System.Move(p^,Result[1],length(Result));
end;
function FindStartOfAtom(const Source: string; Position: integer): integer;
procedure ReadStringConstantBackward(var p: integer);
var
PrePos: integer;
StartPos: LongInt;
begin
StartPos:=p;
while (p>1) do begin
case Source[p-1] of
'''':
begin
PrePos:=p;
dec(PrePos);
repeat
dec(PrePos);
if (PrePos<1) or (Source[PrePos] in [#10,#13]) then begin
// the StartPos was the start of a string constant
p:=StartPos-1;
exit;
end;
until (Source[PrePos]='''');
p:=PrePos;
end;
'0'..'9','A'..'Z','a'..'z':
begin
// test if char constant
PrePos:=p-1;
while (PrePos>1) and (IsHexNumberChar[Source[PrePos]]) do
dec(PrePos);
if (PrePos<1) then break;
if (Source[PrePos]='$') then begin
dec(PrePos);
if (PrePos<1) then break;
end;
if (Source[PrePos]='#') then
p:=PrePos
else
break;
end;
else
break;
end;
end;
end;
type
TNumberType = (ntDecimal, ntHexadecimal, ntBinary, ntIdentifier,
ntCharConstant, ntFloat, ntFloatWithExponent);
TNumberTypes = set of TNumberType;
const
AllNumberTypes: TNumberTypes = [ntDecimal, ntHexadecimal, ntBinary,
ntIdentifier, ntCharConstant, ntFloat, ntFloatWithExponent];
var
c: Char;
ForbiddenNumberTypes: TNumberTypes;
c2: Char;
begin
Result:=Position;
if (Result<1) then exit;
if Result>length(Source) then begin
Result:=length(Source);
exit;
end;
c:=Source[Result];
case c of
'_','A'..'Z','a'..'z':
begin
// identifier or keyword or hexnumber
while (Result>1) do begin
if (IsIdentChar[Source[Result-1]]) then
dec(Result)
else begin
case UpChars[Source[Result-1]] of
'@':
// assembler label
if (Result>2)
and (Source[Result-2]='@') then
dec(Result,2);
'$':
// hex number
dec(Result);
'&':
// &keyword
dec(Result);
end;
break;
end;
end;
end;
'''':
begin
// could be start or end
inc(Result);
ReadStringConstantBackward(Result);
end;
'0'..'9':
begin
// could be a decimal number, an identifier, a hex number,
// a binary number, a char constant, a float, a float with exponent
ForbiddenNumberTypes:=[];
while true do begin
case UpChars[Source[Result]] of
'0'..'1':
;
'2'..'9':
ForbiddenNumberTypes:=ForbiddenNumberTypes+[ntBinary];
'A'..'D','F':
ForbiddenNumberTypes:=ForbiddenNumberTypes
+[ntBinary,ntDecimal,ntCharConstant,ntFloat,ntFloatWithExponent];
'E':
ForbiddenNumberTypes:=ForbiddenNumberTypes
+[ntBinary,ntDecimal,ntCharConstant,ntFloat];
'G'..'Z','_':
ForbiddenNumberTypes:=AllNumberTypes-[ntIdentifier];
'.':
begin
// could be the point of a float
if (ntFloat in ForbiddenNumberTypes)
or (Result<=1) or (Source[Result-1]='.') then begin
inc(Result);
break;
end;
dec(Result);
// this was the part of a float after the point
// -> read decimal in front
ForbiddenNumberTypes:=AllNumberTypes-[ntDecimal];
end;
'+','-':
begin
// could be part of an exponent
if (ntFloatWithExponent in ForbiddenNumberTypes)
or (Result<=1)
or (not (Source[Result-1] in ['e','E']))
then begin
inc(Result);
break;
end;
dec(Result);
// this was the exponent of a float -> read the float
ForbiddenNumberTypes:=AllNumberTypes-[ntFloat];
end;
'#': // char constant found
begin
if (ntCharConstant in ForbiddenNumberTypes) then
inc(Result);
ReadStringConstantBackward(Result);
break;
end;
'$':
begin
// hexadecimal number found
if (ntHexadecimal in ForbiddenNumberTypes) then
inc(Result);
break;
end;
'%':
begin
// binary number found
if (ntBinary in ForbiddenNumberTypes) then
inc(Result);
break;
end;
'@':
begin
if (Result=1) or (Source[Result-1]<>'@')
or (([ntIdentifier,ntDecimal]*ForbiddenNumberTypes)=[]) then
// atom start found
inc(Result)
else
// label found
dec(Result);
break;
end;
else
begin
inc(Result);
break;
end;
end;
if ForbiddenNumberTypes=AllNumberTypes then begin
inc(Result);
break;
end;
if Result<=1 then break;
dec(Result);
end;
if IsIdentStartChar[Source[Result]] then begin
// it is an identifier
end;
end;
else
if Result>1 then begin
c2:=Source[Result-1];
// test for double char operators :=, +=, -=, /=, *=, <>, <=, >=, **, ><, @@ ..
if ((c2='=') and (IsEqualOperatorStartChar[c]))
or ((c='<') and (c2='>'))
or ((c='>') and (c2='<'))
or ((c='.') and (c2='.'))
or ((c='*') and (c2='*'))
or ((c='@') and (c2='@'))
then begin
dec(Result);
end;
end;
end;
end;
function FindEndOfAtom(const Source: string; Position: integer): integer;
// comments have length 0
var
p: PChar;
begin
Result:=FindStartOfAtom(Source,Position);
if (Result>=1) and (Result<=length(Source)) then begin
p:=@Source[Result];
case p^ of
#0..#31,' ': exit;
'{': exit;
'/': if p[1]='/' then exit;
'(': if p[1]='*' then exit;
end;
inc(Result,GetAtomLength(p,false));
end;
end;
function LineEndCount(const Txt: string;
out LengthOfLastLine: integer): integer;
begin
Result:=LineEndCount(Txt,1,length(Txt)+1,LengthOfLastLine);
end;
function FindFirstNonSpaceCharInLine(const Source: string;
Position: integer): integer;
begin
Result:=Position;
if (Result<0) then Result:=1;
if (Result>length(Source)) then Result:=length(Source);
if Result=0 then exit;
// search beginning of line
while (Result>1) and (not (Source[Result-1] in [#10,#13])) do
dec(Result);
// search
while (Result<=length(Source)) and (Source[Result] in [' ',#9]) do inc(Result);
end;
function GetLineIndent(const Source: string; Position: integer): integer;
var LineStart: integer;
begin
Result:=0;
LineStart:=Position;
if LineStart=0 then exit;
if (LineStart<0) then LineStart:=1;
if (LineStart>length(Source)+1) then LineStart:=length(Source)+1;
// search beginning of line
while (LineStart>1) and not (Source[LineStart-1] in [#10,#13]) do
dec(LineStart);
// search code
Result:=LineStart;
while (Result<=length(Source)) and (Source[Result]=' ') do inc(Result);
dec(Result,LineStart);
end;
function FindLineEndOrCodeAfterPosition(const Source: string;
Position, MaxPosition: integer; NestedComments: boolean;
StopAtDirectives: boolean; SkipEmptyLines: boolean;
IncludeLineEnd: boolean): integer;
{ search forward for a line end or code
ignore line ends in comments
Result is Position of Start of Line End
if SkipEmptyLines=true, it will skip empty lines at the end
Examples: | is the Position and # is the Result
1. var i: integer;|#
var j: integer;
If IncludeLineEnd then
var i: integer;|
# var j: integer;
2. var i: integer;| (*
*) #var j: integer;
3. SkipEmptyLines=false
var i: integer;|
#
// comment
var j: integer;
if IncludeLineEnd then the # will be one line below
4. SkipEmptyLines=true
var i: integer;|
#// comment
var j: integer;
}
var SrcLen: integer;
procedure DoSkipEmptyLines(var p: integer);
var
r: LongInt;
begin
r:=p;
repeat
while (r<=SrcLen) and (Source[r] in [' ',#9]) do inc(r);
if (r<=SrcLen) and (Source[r] in [#10,#13]) then begin
// an empty line => skip
p:=r;// remember position in front of new line characters
inc(r);
if (r<=SrcLen) and (Source[r] in [#10,#13]) and (Source[r]<>Source[r-1])
then
inc(r);
end else begin
exit;
end;
until false;
end;
begin
SrcLen:=length(Source);
if SrcLen>MaxPosition then SrcLen:=MaxPosition;
Result:=Position;
if Result=0 then exit;
while (Result<=SrcLen) do begin
case Source[Result] of
'/':
if (Result<SrcLen) and (Source[Result+1]='/') then
Result:=FindCommentEnd(Source,Result,NestedComments)
else
inc(Result);
'{':
if (Result<SrcLen) and (Source[Result+1]='$') and StopAtDirectives then
exit // stop at directive {$ }
else
Result:=FindCommentEnd(Source,Result,NestedComments);
'(':
if (Result<SrcLen) and (Source[Result+1]='*') then begin
if (Result+1<SrcLen) and (Source[Result+2]='$') and StopAtDirectives then
exit; // stop at directive (*$ *)
Result:=FindCommentEnd(Source,Result,NestedComments);
end else
inc(Result); // normal bracket (
#10,#13:
begin
if SkipEmptyLines then DoSkipEmptyLines(Result);
if IncludeLineEnd and (Result<=SrcLen) and (Source[Result] in [#10,#13])
then begin
inc(Result);
if (Result<=SrcLen) and (Source[Result] in [#10,#13])
and (Source[Result-1]<>Source[Result]) then
inc(Result);
end;
exit;
end;
#9,' ',';':
inc(Result);
else
exit;
end;
end;
end;
function IsFirstNonSpaceCharInLine(const Source: string; Position: integer
): boolean;
begin
while (Position>1) and (Source[Position-1] in [' ',#9]) do
dec(Position);
Result:=(Position=1) or (Source[Position-1] in [#10,#13]);
end;
procedure GuessIndentSize(const Source: string; var IndentSize: integer;
TabWidth: integer; MaxLineCount: integer = 10000);
{ check all line indents and return the most common.
Stop after MaxLineCount lines. Ignore empty lines.
}
const
MaxIndentSize = 20;
var
IndentCounts: PSizeInt;
BestCount: SizeInt;
procedure AddIndent(CurIndent: integer);
var
i: SizeInt;
begin
if (CurIndent<1) or (CurIndent>MaxIndentSize) then exit;
i:=IndentCounts[CurIndent-1]+1;
IndentCounts[CurIndent-1]:=i;
if BestCount>=i then
exit;
IndentSize:=CurIndent;
BestCount:=i;
end;
var
LineNumber: Integer;
p: PChar;
CurLineIndent: Integer;
LastLineIndent: Integer;
begin
LineNumber:=0;
if Source='' then exit;
if TabWidth<=0 then TabWidth:=8;
Getmem(IndentCounts,SizeOf(SizeInt)*MaxIndentSize);
try
FillByte(IndentCounts[0],SizeOf(SizeInt)*MaxIndentSize,0);
BestCount:=0;
p:=PChar(Source);
LastLineIndent:=0;
repeat
inc(LineNumber);
// read indent
CurLineIndent:=0;
repeat
case p^ of
' ': inc(CurLineIndent);
#9:
begin
CurLineIndent+=TabWidth;
CurLineIndent-=(CurLineIndent mod TabWidth);
end;
else break;
end;
inc(p);
until false;
if not (p^ in [#0,#10,#13]) then begin
// not an empty line
AddIndent(CurLineIndent-LastLineIndent);
LastLineIndent:=CurLineIndent;
end;
// skip to next line
repeat
case p^ of
#0:
if p-PChar(Source)=length(Source) then begin
// end of soure
exit;
end;
#10,#13:
begin
// line break
repeat
inc(p)
until not (p^ in [#10,#13]);
break;
end;
end;
inc(p);
until false;
until LineNumber>MaxLineCount;
finally
FreeMem(IndentCounts);
end;
end;
function ReIndent(const Source: string; OldIndent, OldTabWidth,
NewIndent, NewTabWidth: integer): string;
{ NewTabWidth = 0 means using spaces
}
var
Src: PChar;
SrcIndent: Integer;
DstIndent: Integer;
i: Integer;
Dst: PChar;
procedure Grow;
var
Old: PtrUInt;
begin
if (Dst^<>#0) or (Dst-PChar(Result)<>length(Result)) then exit;
// grow
Old:=Dst-PChar(Result);
SetLength(Result,(length(Result)*3) div 2);
FillByte(Result[Old+1],length(Result)-Old,ord('A'));
Dst:=PChar(Result)+Old;
end;
procedure Add(c: char); inline;
begin
//debugln(['Add c="',DbgStr(c),'"']);
if (Dst^=#0) then
Grow;
Dst^:=c;
inc(Dst);
end;
begin
Result:=Source;
if (Result='') or (OldIndent<=0) or (OldTabWidth<0)
or (NewIndent<0) or (NewTabWidth<0) then exit;
UniqueString(Result);
Src:=PChar(Source);
Dst:=PChar(Result);
repeat
// read indent
SrcIndent:=0;
repeat
case Src^ of
' ': inc(SrcIndent);
#9:
begin
SrcIndent:=SrcIndent+OldTabWidth;
SrcIndent:=SrcIndent-(SrcIndent mod SrcIndent);
end;
else break;
end;
inc(Src);
until false;
// write indent
DstIndent:=((SrcIndent+OldIndent-1) div OldIndent)*NewIndent;
//debugln(['ReIndent DstIndent=',DstIndent,' Src=',dbgstr(Src^),' at ',Src-PChar(Source)]);
if NewTabWidth>0 then begin
for i:=1 to (DstIndent div NewTabWidth) do
Add(#9);
for i:=1 to (DstIndent mod NewTabWidth) do
Add(' ');
end else begin
for i:=1 to DstIndent do
Add(' ');
end;
// copy line
repeat
case Src^ of
#0: if Src-PChar(Source)=length(Source) then break;
#10,#13: break;
end;
Add(Src^);
inc(Src);
until false;
// copy line break
while Src^ in [#10,#13] do begin
Add(Src^);
inc(Src);
end;
until (Src^=#0) and (Src-PChar(Source)=length(Source));
SetLength(Result,Dst-PChar(Result));
end;
function FindLineEndOrCodeInFrontOfPosition(const Source: string;
Position, MinPosition: integer; NestedComments: boolean;
StopAtDirectives: boolean; SkipSemicolonComma: boolean;
SkipEmptyLines: boolean): integer;
{ search backward for a line end or code
ignore line ends in comments or at the end of comment lines
(comment lines are lines without code and at least one comment)
comment lines directly in front are skipped too
if SkipEmptyLines=true then empty lines are skipped too.
Result is Position of Start of Line End
examples: Position points at char 'a'
1: |
2: a:=1;
1: b:=1; |
2: // comment for below
3: // comment for below
4: a:=1;
1: |
2: (* comment belongs to the following line *)
3: a:=1;
1: end; (* comment belongs to the first line
2: *)| a:=1;
1: b:=1; // comment |
2: a:=1;
1: b:=1; (*
2: comment *) |
3: a:=1;
}
var SrcStart: integer;
function IsSpace(c: char): boolean;
begin
if SkipSemicolonComma then
Result:=c in [' ',#9,';',',']
else
Result:=c in [' ',#9];
end;
function ReadComment(var p: integer; SubComment: boolean): boolean;
// true if comment was skipped
// false if not skipped, because comment is compiler directive or simple bracket
var OldP: integer;
IsDirective: Boolean;
begin
//debugln(['ReadComment ',dbgstr(copy(Source,p-5,5))+'|'+Source[p]+dbgstr(copy(Source,p+1,5))]);
OldP:=p+1;
repeat
IsDirective:=false;
case Source[p] of
'}':
begin
dec(p);
if (p>SrcStart) and (Source[p]=#3) then begin
// codetools skip comment
dec(p);
while (p>SrcStart) do begin
if (Source[p]=#3)
and (Source[p-1]='{') then begin
dec(p);
break;
end;
dec(p);
end;
end else begin
// Pascal comment {}
while (p>SrcStart) and (Source[p]<>'{') do begin
if NestedComments and (Source[p]='}') then
ReadComment(p,true)
else
dec(p);
end;
IsDirective:=(p>=SrcStart) and (Source[p+1]='$');
dec(p);
end;
end;
')':
begin
dec(p);
if (p>SrcStart) and (Source[p]='*') then begin
// tp comment (* *)
dec(p);
while (p>SrcStart)
and ((Source[p-1]<>'(') or (Source[p]<>'*')) do begin
if NestedComments and ((Source[p]=')') and (Source[p-1]='*')) then
ReadComment(p,true)
else
dec(p);
end;
IsDirective:=(p>=SrcStart) and (Source[p+1]='$');
dec(p,2);
end else begin
// normal bracket
// => position behind code
p:=OldP;
exit(false);
end;
end;
else
exit(true);
end;
if SubComment then exit(true); // always skip nested comments
if IsDirective and StopAtDirectives then begin
// directive can not be skipped
p:=OldP;
exit(false);
end;
// it is a normal comment
// check if it belongs to the code in front
while (p>=SrcStart) and IsSpace(Source[p]) do
dec(p);
if (p<SrcStart) or (Source[p] in [#10,#13]) then begin
// empty line in front of comment => comment can be skipped
exit(true);
end;
if not (Source[p] in [')','}']) then begin
// code => comment belongs to code in front
p:=OldP;
exit(false);
end;
// read next comment
until false;
end;
var
TestPos: integer;
LineStartPos: LongInt;
IsEmpty: Boolean;
LineEndPos: Integer;
p: LongInt;
begin
SrcStart:=MinPosition;
if SrcStart<1 then SrcStart:=1;
if (Position<=SrcStart) then begin
Result:=SrcStart;
exit;
end;
// simple heuristic
// will fail on lines: // }
Result:=-1;
p:=Position-1;
if p>length(Source) then p:=length(Source);
while (p>=SrcStart) do begin
case Source[p] of
#10,#13:
begin
// line end found (outside comments)
if (p>SrcStart) and (Source[p-1] in [#10,#13])
and (Source[p]<>Source[p-1]) then
dec(p);
LineEndPos:=p; // start of line end
Result:=LineEndPos;
// test if in a // comment
LineStartPos:=p;
IsEmpty:=true;
while (LineStartPos>SrcStart) do begin
case Source[LineStartPos-1] of
#10,#13: break;
' ',#9: ;
';',',': if not SkipSemicolonComma then IsEmpty:=false;
else IsEmpty:=false;
end;
dec(LineStartPos);
end;
if IsEmpty then begin
// the line is empty => return start of line end
p:=LineEndPos;
if SkipEmptyLines then begin
// skip all empty lines
LineStartPos:=p;
while (LineStartPos>SrcStart) do begin
case Source[LineStartPos-1] of
#10,#13:
begin
// empty line
LineEndPos:=LineStartPos-1;
if (LineEndPos>SrcStart) and (Source[LineEndPos-1] in [#10,#13])
and (Source[LineEndPos]<>Source[LineEndPos-1]) then
dec(LineEndPos);
p:=LineEndPos;
end;
' ',#9: ;
else
// not empty
break;
end;
dec(LineStartPos);
end;
end;
break;
end;
// line is not empty
TestPos:=LineStartPos;
while (Source[TestPos] in [' ',#9]) do inc(TestPos);
if (Source[TestPos]='/') and (Source[TestPos+1]='/') then begin
// the whole line is a // comment
// this comment belongs to the code behind
// => continue on next line
p:=LineStartPos-1;
continue;
end;
dec(p);
end;
'}',')':
if not ReadComment(p,false) then break;
' ',#9:
dec(p);
';',',':
begin
if not SkipSemicolonComma then begin
// code found
inc(p);
break;
end;
dec(p);
end;
else
// code found
inc(p);
break;
end;
end;
if Result<1 then Result:=p;
if Result<SrcStart then Result:=SrcStart;
end;
function FindFirstLineEndAfterInCode(const Source: string;
Position, MaxPosition: integer; NestedComments: boolean): integer;
{ search forward for a line end
ignore line ends in comments
Result is Position of Start of Line End
}
var
SrcLen: integer;
CommentEndPos: Integer;
begin
SrcLen:=length(Source);
if SrcLen>MaxPosition then SrcLen:=MaxPosition;
Result:=Position;
while (Result<=SrcLen) do begin
case Source[Result] of
'{','(','/':
begin
CommentEndPos:=FindCommentEnd(Source,Result,NestedComments);
if CommentEndPos>Result then
Result:=CommentEndPos
else
inc(Result);
end;
#10,#13:
exit;
else
inc(Result);
end;
end;
end;
function ChompLineEndsAtEnd(const s: string): string;
var
EndPos: Integer;
begin
EndPos:=length(s)+1;
while (EndPos>1) and (s[EndPos-1] in [#10,#13]) do dec(EndPos);
Result:=s;
SetLength(Result,EndPos-1);
end;
function ChompOneLineEndAtEnd(const s: string): string;
var
EndPos: Integer;
begin
Result:=s;
EndPos:=length(s)+1;
if (EndPos>1) and (s[EndPos-1] in [#10,#13]) then begin
dec(EndPos);
if (EndPos>1) and (s[EndPos-1] in [#10,#13]) and (s[EndPos]<>s[EndPos-1])
then
dec(EndPos);
SetLength(Result,EndPos-1);
end;
end;
function TrimLineEnds(const s: string; TrimStart, TrimEnd: boolean): string;
var
StartPos: Integer;
EndPos: Integer;
LineEnd: Integer;
begin
StartPos:=1;
if TrimStart then begin
// trim empty lines at start
while (StartPos<=length(s))
and (s[StartPos] in [#10,#13]) do begin
inc(StartPos);
if (StartPos<=length(s))
and (s[StartPos] in [#10,#13])
and (s[StartPos]<>s[StartPos-1]) then
inc(StartPos);
end;
end;
EndPos:=length(s)+1;
if TrimEnd then begin
// trim empty lines at end
while (EndPos>StartPos)
and (s[EndPos-1] in [#10,#13]) do begin
LineEnd:=EndPos-1;
if (LineEnd>StartPos) and (s[LineEnd-1] in [#10,#13])
and (s[LineEnd-1]<>s[LineEnd]) then begin
dec(LineEnd);
end;
if (LineEnd>StartPos) and (s[LineEnd-1] in [#10,#13]) then
EndPos:=LineEnd
else
break;
end;
end;
if EndPos-StartPos<length(s) then
Result:=copy(s,StartPos,EndPos-StartPos)
else
Result:=s;
end;
function SrcPosToLineCol(const s: string; Position: integer;
out Line, Col: integer): boolean;
// returns false if Postion<1 or >length(s)+1
var
p: LongInt;
l: Integer;
begin
if (Position<1) then begin
Line:=1;
Col:=1;
Result:=false;
exit;
end;
l:=length(s);
if l>Position then l:=Position;
Line:=1;
Col:=1;
p:=1;
while (p<l) do begin
case s[p] of
#10,#13:
begin
inc(p);
if (p<=length(s)) and (s[p] in [#10,#13]) and (s[p-1]<>s[p]) then
begin
if p=Position then exit(true);
inc(p);
end;
// new line
inc(Line);
Col:=1;
end;
else
inc(p);
inc(Col);
end;
end;
Result:=p=Position;
end;
function GetBracketLvl(const Src: string; StartPos, EndPos: integer;
NestedComments: boolean): integer;
var
SrcLen: Integer;
procedure ReadComment;
var
CommentEndPos: Integer;
begin
CommentEndPos:=FindCommentEnd(Src,StartPos,NestedComments);
if CommentEndPos>=EndPos then begin
// EndPos is in a comment
// -> count bracket lvl in comment
Result:=0;
case Src[StartPos] of
'{': inc(StartPos);
'(','/': inc(StartPos,2);
end;
end else
// continue after the comment
StartPos:=CommentEndPos;
end;
procedure ReadBrackets(ClosingBracket: Char);
begin
while StartPos<EndPos do begin
case Src[StartPos] of
'{':
ReadComment;
'/':
if (StartPos<SrcLen) and (Src[StartPos]='/') then
ReadComment
else
inc(StartPos);
'(':
if (StartPos<SrcLen) and (Src[StartPos]='*') then
ReadComment
else begin
inc(Result);
inc(StartPos);
ReadBrackets(')');
end;
'[':
begin
inc(Result);
inc(StartPos);
ReadBrackets(']');
end;
')',']':
if (Result>0) then begin
if ClosingBracket=Src[StartPos] then
dec(Result) // for example: ()
else
Result:=0; // for example: [)
exit;
end;
end;
inc(StartPos);
end;
end;
begin
Result:=0;
SrcLen:=length(Src);
if (StartPos<1) then StartPos:=1;
if (StartPos>SrcLen) or (EndPos<StartPos) then exit;
if (EndPos>SrcLen) then EndPos:=SrcLen;
ReadBrackets(#0);
end;
function FindFirstLineEndInFrontOfInCode(const Source: string;
Position, MinPosition: integer; NestedComments: boolean): integer;
{ search backward for a line end
ignore line ends in comments
Result will be at the Start of the Line End
}
var
SrcStart: integer;
procedure ReadComment(var P: integer);
begin
case Source[P] of
'}':
begin
dec(P);
if (P>=SrcStart) and (Source[P]=#3) then begin
// codetools comment {#3 #3}
dec(p);
while (P>SrcStart) do begin
if (Source[P]=#3) and (Source[P-1]='{') then begin
dec(P,2);
break;
end;
dec(P);
end;
end else begin
// Pascal comment {}
while (P>=SrcStart) and (Source[P]<>'{') do begin
if NestedComments and (Source[P] in ['}',')']) then
ReadComment(P)
else
dec(P);
end;
dec(P);
end;
end;
')':
begin
dec(P);
if (P>=SrcStart) and (Source[P]='*') then begin
dec(P);
while (P>SrcStart)
and ((Source[P-1]<>'(') or (Source[P]<>'*')) do begin
if NestedComments and (Source[P] in ['}',')']) then
ReadComment(P)
else
dec(P);
end;
dec(P,2);
end;
end;
end;
end;
var TestPos: integer;
begin
Result:=Position;
SrcStart:=MinPosition;
if SrcStart<1 then SrcStart:=1;
while (Result>=SrcStart) do begin
case Source[Result] of
'}',')':
ReadComment(Result);
#10,#13:
begin
// test if it is a '//' comment
if (Result>SrcStart) and (Source[Result-1] in [#10,#13])
and (Source[Result]<>Source[Result-1]) then dec(Result);
TestPos:=Result-1;
while (TestPos>SrcStart) do begin
if (Source[TestPos]='/') and (Source[TestPos-1]='/') then begin
// this is a comment line end -> search further
break;
end else if Source[TestPos] in [#10,#13] then begin
// no comment, the line end ist really there :)
exit;
end else
dec(TestPos);
end;
Result:=TestPos;
end;
else
dec(Result);
end;
end;
end;
function ReplacementNeedsLineEnd(const Source: string;
FromPos, ToPos, NewLength, MaxLineLength: integer): boolean;
// test if old text contains a line end
// or if new line is too long
var LineStart, LineEnd: integer;
begin
GetLineStartEndAtPosition(Source,FromPos,LineStart,LineEnd);
Result:=((LineEnd>=FromPos) and (LineEnd<ToPos))
or ((LineEnd-LineStart-(ToPos-FromPos)+NewLength)>MaxLineLength);
end;
function CompareTextIgnoringSpace(const Txt1, Txt2: string;
CaseSensitive: boolean): integer;
begin
Result:=CompareTextIgnoringSpace(
PChar(Pointer(Txt1)),length(Txt1),// pointer type cast avoids #0 check
PChar(Pointer(Txt2)),length(Txt2),
CaseSensitive);
end;
function CompareTextIgnoringSpace(Txt1: PChar; Len1: integer;
Txt2: PChar; Len2: integer; CaseSensitive: boolean): integer;
{ Txt1 Txt2 Result
A A 0
A B 1
A AB 1
A; A -1
}
var P1, P2: integer;
begin
P1:=0;
P2:=0;
while (P1<Len1) and (P2<Len2) do begin
if (CaseSensitive and (Txt1[P1]=Txt2[P2]))
or ((not CaseSensitive) and (UpChars[Txt1[P1]]=UpChars[Txt2[P2]])) then
begin
inc(P1);
inc(P2);
end else begin
// different chars found
if (P1>0) and (IsIdentChar[Txt1[P1-1]])
and (IsIdentChar[Txt1[P1]] xor IsIdentChar[Txt2[P2]]) then begin
// one identifier is longer than the other
if IsIdentChar[Txt1[P1]] then
// identifier in Txt1 is longer than in Txt2
Result:=-1
else
// identifier in Txt2 is longer than in Txt1
Result:=+1;
exit;
end else if (ord(Txt1[P1])<=ord(' ')) then begin
// ignore/skip spaces in Txt1
repeat
inc(P1);
until (P1>=Len1) or (ord(Txt1[P1])>ord(' '));
if (ord(Txt2[P2])<=ord(' ')) then begin
// ignore/skip spaces in Txt2
repeat
inc(P2);
until (P2>=Len2) or (ord(Txt2[P2])>ord(' '));
end;
end else if (ord(Txt2[P2])<=ord(' ')) then begin
// ignore/skip spaces in Txt2
repeat
inc(P2);
until (P2>=Len2) or (ord(Txt2[P2])>ord(' '));
end else begin
// Txt1<>Txt2
if (CaseSensitive and (Txt1[P1]>Txt2[P2]))
or ((not CaseSensitive) and (UpChars[Txt1[P1]]>UpChars[Txt2[P2]])) then
Result:=-1
else
Result:=+1;
exit;
end;
end;
end;
// one text was totally read -> check the rest of the other one
// skip spaces
while (P1<Len1) and (ord(Txt1[P1])<=ord(' ')) do
inc(P1);
while (P2<Len2) and (ord(Txt2[P2])<=ord(' ')) do
inc(P2);
if (P1>=Len1) then begin
// rest of P1 was only space
if (P2>=Len2) then
// rest of P2 was only space
Result:=0
else
// there is some text at the end of P2
Result:=1;
end else begin
// there is some text at the end of P1
Result:=-1
end;
end;
function CompareAnsiStringIgnoringSpaceIgnoreCase(Txt1, Txt2: pointer): integer;
// Txt1, Txt2 are type casted AnsiString
begin
Result:=CompareTextIgnoringSpace(Txt1,length(AnsiString(Txt1)),
Txt2,length(AnsiString(Txt2)),false);
end;
function CompareSubStrings(const Find, Txt: string;
FindStartPos, TxtStartPos, Len: integer; CaseSensitive: boolean): integer;
begin
Result:=CompareText(@Find[FindStartPos],Min(length(Find)-FindStartPos+1,Len),
@Txt[TxtStartPos],Min(length(Txt)-TxtStartPos+1,Len),
CaseSensitive);
end;
function FindNextIncludeDirective(const ASource: string; StartPos: integer;
NestedComments: boolean; out FilenameStartPos, FileNameEndPos,
CommentStartPos, CommentEndPos: integer): integer;
var
MaxPos: Integer;
Offset: Integer;
begin
Result:=StartPos;
MaxPos:=length(ASource);
repeat
Result:=FindNextCompilerDirective(ASource,Result,NestedComments);
if (Result<1) or (Result>MaxPos) then exit;
if (ASource[Result]='{') then
Offset:=2
else if ASource[Result]='(' then
Offset:=3
else
Offset:=-1;
if (Offset>0) then begin
if ((UpChars[ASource[Result+Offset]]='I')
and (ASource[Result+Offset+1]=' '))
or (CompareIdentifiers('include',@ASource[Result+Offset])=0) then begin
CommentEndPos:=FindCommentEnd(ASource,Result,NestedComments);
if ASource[Result]='{' then
dec(CommentEndPos)
else
dec(CommentEndPos,2);
// skip directive name
FilenameStartPos:=Result+Offset;
while (FilenameStartPos<=CommentEndPos)
and (IsIdentChar[ASource[FilenameStartPos]]) do
inc(FilenameStartPos);
// skip space after name
while (FilenameStartPos<=CommentEndPos)
and (IsSpaceChar[ASource[FilenameStartPos]]) do
inc(FilenameStartPos);
// find end of filename
FilenameEndPos:=FilenameStartPos;
if (FilenameEndPos<=CommentEndPos) and (ASource[FilenameEndPos]='''')
then begin
// quoted filename
inc(FilenameStartPos);
while (FilenameEndPos<=CommentEndPos) do begin
if (ASource[FilenameEndPos]<>'''') then
inc(FilenameEndPos)
else begin
inc(FilenameEndPos);
break;
end;
end;
end else begin
// normal filename
while (FilenameEndPos<=CommentEndPos)
and (not IsSpaceChar[ASource[FilenameEndPos]])
and (not (ASource[FilenameEndPos] in ['*','}'])) do
inc(FilenameEndPos);
end;
// skip space behind filename
CommentStartPos:=FilenameEndPos;
while (CommentStartPos<=CommentEndPos)
and (IsSpaceChar[ASource[CommentStartPos]]) do inc(CommentStartPos);
// success
exit;
end;
end;
// try next comment
Result:=FindCommentEnd(ASource,Result,NestedComments);
until Result>MaxPos;
end;
function FindNextIDEDirective(const ASource: string; StartPos: integer;
NestedComments: boolean; EndPos: integer): integer;
var
MaxPos: integer;
begin
MaxPos:=length(ASource);
if (EndPos>0) and (EndPos<=MaxPos) then
MaxPos:=EndPos-1;
Result:=StartPos;
while (Result<=MaxPos) do begin
case ASource[Result] of
'''':
begin
inc(Result);
while (Result<=MaxPos) do begin
if (ASource[Result]<>'''') then
inc(Result)
else begin
inc(Result);
break;
end;
end;
end;
'/':
begin
inc(Result);
if (Result<=MaxPos) and (ASource[Result]='/') then begin
// skip Delphi comment
while (Result<=MaxPos) and (not (ASource[Result] in [#10,#13])) do
inc(Result);
end;
end;
'{':
begin
if (Result<MaxPos) and (ASource[Result+1]='%') then
exit;
// skip pascal comment
Result:=FindCommentEnd(ASource,Result,NestedComments);
end;
'(':
begin
if (Result<MaxPos) and (ASource[Result+1]='*') then begin
// skip TP comment
Result:=FindCommentEnd(ASource,Result,NestedComments);
end else
inc(Result);
end;
else
inc(Result);
end;
end;
Result:=-1;
end;
function CleanCodeFromComments(const Src: string; NestedComments: boolean;
KeepDirectives: boolean; KeepVerbosityDirectives: boolean): string;
// KeepVerbosityDirectives=true requires KeepDirectives=true
var
SrcPos: Integer;
ResultPos: Integer;
StartPos: Integer;
l: Integer;
p: PChar;
begin
SetLength(Result,length(Src));
SrcPos:=1;
ResultPos:=1;
while SrcPos<=length(Src) do begin
StartPos:=FindNextComment(Src,SrcPos);
l:=StartPos-SrcPos;
if (l>0) then begin
System.Move(Src[SrcPos],Result[ResultPos],l);
inc(ResultPos,l);
end;
if StartPos>length(Src) then break;
SrcPos:=FindCommentEnd(Src,StartPos,NestedComments);
if KeepDirectives then begin
p:=@Src[StartPos];
if (p^<>'{') or (p[1]<>'$') then continue;
if not KeepVerbosityDirectives then begin
inc(p,2);
if (CompareIdentifiers(p,'warn')=0)
or (CompareIdentifiers(p,'hint')=0) then continue;
end;
l:=SrcPos-StartPos;
System.Move(Src[StartPos],Result[ResultPos],l);
inc(ResultPos,l);
end;
end;
SetLength(Result,ResultPos-1);
end;
function ExtractCommentContent(const ASource: string; CommentStart: integer;
NestedComments: boolean; TrimStart: boolean; TrimEnd: boolean;
TrimPasDoc: boolean): string;
var
CommentEnd: LongInt;
StartPos: LongInt;
EndPos: LongInt;
begin
Result:='';
if (CommentStart<1) or (CommentStart>length(ASource)) then exit;
CommentEnd:=FindCommentEnd(ASource,CommentStart,NestedComments);
StartPos:=CommentStart;
EndPos:=CommentEnd;
if (ASource[StartPos]='/') then begin
inc(StartPos);
if (StartPos<=length(ASource)) and (ASource[StartPos]='/') then
inc(StartPos);
if (EndPos<=length(ASource)) then begin
while (EndPos>StartPos) and (ASource[EndPos-1] in [#10,#13]) do
dec(EndPos);
end;
end else if (ASource[StartPos]='{') then begin
inc(StartPos);
if (StartPos<=length(ASource)) and (ASource[StartPos]=#3) then begin
// codetools skip comment {#3#3}
inc(StartPos);
if (EndPos<=length(ASource)) and (ASource[EndPos-1]='}') then begin
dec(EndPos);
if (EndPos<=length(ASource)) and (ASource[EndPos-1]=#3) then
dec(EndPos);
end;
end else begin
// Pascal comment {}
if (EndPos<=length(ASource)) and (ASource[EndPos-1]='}') then
dec(EndPos);
end;
end else if (ASource[StartPos]='(') then begin
inc(StartPos);
if (StartPos<=length(ASource)) and (ASource[StartPos]='*') then
inc(StartPos);
if (EndPos<=length(ASource)) and (ASource[EndPos-1]=')') then begin
dec(EndPos);
if (ASource[EndPos-1]='*') then
dec(EndPos);
end;
end;
if TrimPasDoc then begin
if (StartPos<EndPos) and (ASource[StartPos]='<') then
inc(StartPos);
end;
if TrimStart then begin
while (StartPos<EndPos) and (ASource[StartPos] in [' ',#9,#10,#13]) do
inc(StartPos);
end;
if TrimEnd then begin
while (StartPos<EndPos) and (ASource[Endpos-1] in [' ',#9,#10,#13]) do
dec(EndPos);
end;
Result:=copy(ASource,StartPos,EndPos-StartPos);
end;
function FindMainUnitHint(const ASource: string; out Filename: string
): boolean;
const
IncludeByHintStart = '{%MainUnit ';
var
MaxPos: Integer;
StartPos: Integer;
EndPos: LongInt;
begin
Result:=false;
Filename:='';
MaxPos:=length(ASource);
StartPos:=length(IncludeByHintStart);
if not TextBeginsWith(PChar(Pointer(ASource)),// pointer type cast avoids #0 check
MaxPos,IncludeByHintStart,StartPos,false)
then
exit;
while (StartPos<=MaxPos) and (ASource[StartPos]=' ') do inc(StartPos);
EndPos:=StartPos;
while (EndPos<=MaxPos) and (ASource[EndPos]<>'}') do inc(EndPos);
if (EndPos=StartPos) or (EndPos>MaxPos) then exit;
Filename:=GetForcedPathDelims(copy(ASource,StartPos,EndPos-StartPos));
Result:=true;
end;
function InEmptyLine(const ASource: string; StartPos: integer): boolean;
var
p: LongInt;
SrcLen: Integer;
begin
Result:=false;
SrcLen:=length(ASource);
if (StartPos<1) or (StartPos>SrcLen) or (not IsSpaceChar[ASource[StartPos]])
then exit;
p:=StartPos;
while (p>1) do begin
case ASource[p-1] of
' ',#9: dec(p);
#10,#13: break;
else exit;
end;
end;
p:=StartPos;
while p<=SrcLen do begin
case ASource[p] of
' ',#9: inc(p);
#10,#13: break;
else exit;
end;
end;
Result:=true;
end;
function SkipResourceDirective(const ASource: string;
StartPos, EndPos: integer; NestedComments: boolean): integer;
var
MaxPos: integer;
function IsResourceDirective(DirNamePos: integer): boolean;
begin
if UpChars[ASource[DirNamePos]]<>'R' then exit(false);
if (DirNamePos < MaxPos)
and (UpChars[ASource[DirNamePos+1]] in [' ',#9]) then exit(true);
result:=CompareIdentifiers(@ASource[DirNamePos],'RESOURCE')=0;
end;
var
i: integer;
begin
MaxPos:=length(ASource);
if (EndPos>0) and (EndPos<=MaxPos) then
MaxPos:=EndPos-1;
Result:=StartPos;
i:=StartPos;
while (i<=MaxPos) do begin
case ASource[i] of
'{':
if (i+1<MaxPos) and (ASource[i+1]='$')
and IsResourceDirective(i+2) then begin
Result:=FindCommentEnd(ASource,i,NestedComments);
exit;
end
else exit;
'(':
if (i+2<MaxPos) and (ASource[i+1]='*') and (ASource[i+2]='$')
and IsResourceDirective(i+3) then begin
Result:=FindCommentEnd(ASource,i,NestedComments);
exit;
end
else exit;
#9,#10,#13,' ': inc(i);
else
exit;
end;
end;
end;
function CompareIdentifiers(Identifier1, Identifier2: PChar): integer;
begin
Result:=KeywordFuncLists.CompareIdentifiers(Identifier1,Identifier2);
end;
function CompareIdentifierPtrs(Identifier1, Identifier2: Pointer): integer;
begin
Result:=CompareIdentifiers(PChar(Identifier1), PChar(Identifier2));
end;
function CompareIdentifiersCaseSensitive(Identifier1, Identifier2: PChar
): integer;
begin
if (Identifier1<>nil) then begin
if (Identifier2<>nil) then begin
while (Identifier1[0]=Identifier2[0]) do begin
if (IsIdentChar[Identifier1[0]]) then begin
inc(Identifier1);
inc(Identifier2);
end else begin
Result:=0; // for example 'aaA;' 'aAa;'
exit;
end;
end;
if (IsIdentChar[Identifier1[0]]) then begin
if (IsIdentChar[Identifier2[0]]) then begin
if Identifier1[0]>Identifier2[0] then
Result:=-1 // for example 'aab' 'aaa'
else
Result:=1; // for example 'aaa' 'aab'
end else begin
Result:=-1; // for example 'aaa' 'aa;'
end;
end else begin
if (IsIdentChar[Identifier2[0]]) then
Result:=1 // for example 'aa;' 'aaa'
else
Result:=0; // for example 'aa;' 'aa,'
end;
end else begin
Result:=-1; // for example 'aaa' nil
end;
end else begin
if (Identifier2<>nil) then begin
Result:=1; // for example nil 'bbb'
end else begin
Result:=0; // for example nil nil
end;
end;
end;
function ComparePrefixIdent(PrefixIdent, Identifier: PChar): boolean;
begin
if PrefixIdent<>nil then begin
if Identifier<>nil then begin
while (UpChars[PrefixIdent^]=UpChars[Identifier^]) and (PrefixIdent^>#0) do
begin
inc(PrefixIdent);
inc(Identifier);
end;
Result:=not IsIdentChar[PrefixIdent^];
end else begin
Result:=false;
end;
end else begin
Result:=true;
end;
end;
function TextBeginsWith(Txt: PChar; TxtLen: integer; StartTxt: PChar;
StartTxtLen: integer; CaseSensitive: boolean): boolean;
begin
if TxtLen<StartTxtLen then exit(false);
Result:=CompareText(Txt,StartTxtLen,StartTxt,StartTxtLen,CaseSensitive)=0;
end;
function StrBeginsWith(const s, Prefix: string): boolean;
var
p1: PChar;
p2: PChar;
i: Integer;
begin
Result:=false;
if length(s)<length(Prefix) then exit;
if (s='') then exit(true);
p1:=PChar(s);
p2:=PChar(Prefix);
for i:=1 to length(Prefix) do begin
if p1^<>p2^ then exit;
inc(p1);
inc(p2);
end;
Result:=true;
end;
function IdentifierPos(Search, Identifier: PChar): PtrInt;
var
i: Integer;
begin
if Identifier=nil then exit(-1);
if (Search=nil) or (Search^=#0) then exit(0);
Result:=0;
while (IsIdentChar[Identifier[Result]]) do begin
if UpChars[Search^]=UpChars[Identifier[Result]] then begin
i:=1;
repeat
if IsIdentChar[Search[i]] then begin
if (UpChars[Search[i]]=UpChars[Identifier[Result+i]]) then
inc(i)
else
break;
end else begin
// whole found
exit;
end;
until false;
end;
inc(Result);
end;
Result:=-1;
end;
function CompareAtom(p1, p2: PChar; NestedComments: boolean): integer;
var
Len1: LongInt;
Len2: LongInt;
l: LongInt;
c1: Char;
c2: Char;
begin
// quick test for the common case:
if (p1^<>p2^) then begin
c1:=UpChars[p1^];
c2:=UpChars[p2^];
if c1<c2 then
exit(1)
else if c1>c2 then
exit(-1);
end;
case p1^ of
'''':
// compare string constants case sensitive
exit(CompareStringConstants(p1,p2));
'{':
exit(CompareComments(p1,p2,NestedComments));
'(':
if (p1[1]='*') and (p2[1]='*') then
exit(CompareComments(p1,p2,NestedComments));
'/':
if (p1[1]='/') and (p2[1]='/') then
exit(CompareComments(p1,p2,NestedComments));
end;
// full comparison
Len1:=GetAtomLength(p1,NestedComments);
Len2:=GetAtomLength(p2,NestedComments);
l:=Len1;
if l>Len2 then l:=Len2;
while l>0 do begin
if (p1^<>p2^) then begin
c1:=UpChars[p1^];
c2:=UpChars[p2^];
if c1<c2 then
exit(1)
else if c1>c2 then
exit(-1);
end;
inc(p1);
inc(p2);
dec(l);
end;
if Len1>Len2 then
Result:=1
else if Len1<Len2 then
Result:=-1
else
Result:=0;
end;
function CompareStringConstants(p1, p2: PChar): integer;
// 1: 'aa' 'ab' because bigger
// 1: 'aa' 'a' because longer
begin
Result := 0;
if (p1^='''') and (p2^='''') then begin
inc(p1);
inc(p2);
repeat
if p1^<p2^ then
exit(1) // p1 bigger
else if p1^>p2 then
exit(-1); // p2 bigger
inc(p1);
inc(p2);
if p1^='''' then begin
// maybe ''
inc(p1);
inc(p2);
if p1^='''' then begin
if p2^='''' then begin
inc(p1);
inc(p2);
end else begin
// p1 is longer (e.g.: 'a''b' 'a')
exit(1);
end;
end else if p2^='''' then begin
// p2 is longer (e.g. 'a' 'a''b')
exit(-1);
end else begin
// same
exit(0);
end;
end;
if p1^ in [#0,#10,#13] then begin
// end of p1 found
if p2^ in [#0,#10,#13] then begin
// same
exit(0);
end else begin
// p2 is longer
exit(-1);
end;
end else if p2^ in [#0,#10,#13] then begin
// p1 is longer
exit(1);
end;
until false;
end else begin
if p1^='''' then
// p1 longer
exit(1)
else if p2^='''' then
// p2 longer
exit(-1)
else
// both empty
exit(0);
end;
end;
function CompareComments(p1, p2: PChar; NestedComments: boolean): integer;
var
CommentLvl: Integer;
IsCodetoolsComment: Boolean;
begin
if p1^<>p2^ then begin
if p1^<p2^ then
exit(1)
else
exit(-1);
end;
case p1^ of
'/':
begin
inc(p1);
inc(p2);
if p1^<>p2^ then begin
if p1^<p2^ then
exit(1)
else
exit(-1);
end;
if p1^<>'/' then exit(0);
repeat
inc(p1);
inc(p2);
if p1^ in [#0,#10,#13] then begin
if p2^ in [#0,#10,#13] then begin
exit(0);
end else begin
// p2 is longer
exit(-1);
end;
end;
if p1^<>p2^ then begin
if p2^ in [#0,#10,#13] then begin
// p1 is longer
exit(1);
end;
if p1^<p2^ then
exit(1)
else
exit(-1);
end;
until false;
end;
'{':
begin
inc(p1);
inc(p2);
CommentLvl:=1;
IsCodetoolsComment:=p1^=#3;
while true do begin
if p1^<>p2^ then begin
if p1^<p2^ then
exit(1)
else
exit(-1);
end;
inc(p1);
inc(p2);
if IsCodetoolsComment then begin
case p1^ of
#0: exit(0);
'}': if p1[-1]=#3 then exit(0);
end;
end else begin
case p1^ of
#0: exit(0);
'{': if NestedComments then
inc(CommentLvl);
'}':
begin
dec(CommentLvl);
if CommentLvl=0 then
exit(0);
end;
end;
end;
end;
end;
'(': // comment
begin
inc(p1);
inc(p2);
if p1^<>p2^ then begin
if p1^<p2^ then
exit(1)
else
exit(-1);
end;
if p1^<>'*' then exit(0);
CommentLvl:=1;
repeat
inc(p1);
inc(p2);
if p1^<>p2^ then begin
if p1^<p2^ then
exit(1)
else
exit(-1);
end;
case p1^ of
#0: exit(0);
'*':
if (p1[1]=')') then begin
inc(p1);
inc(p2);
if p2^=')' then begin
dec(CommentLvl);
if CommentLvl=0 then
exit(0);
end else
// p2 longer
exit(-1);
end;
'(':
if (p1[1]='*') and NestedComments then begin
inc(CommentLvl);
inc(p1);
inc(p2);
if p1^<>p2^ then begin
if p1^<p2^ then
exit(1)
else
exit(-1);
end;
end;
end;
until false;
end;
end;
Result:=0;
end;
function FindDiff(const s1, s2: string): integer;
begin
Result:=1;
while (Result<=length(s1)) and (Result<=length(s2)) and (s1[Result]=s2[Result]) do
inc(Result);
end;
function dbgsDiff(Expected, Actual: string): string;
var
d: Integer;
begin
Expected:=dbgstr(Expected);
Actual:=dbgstr(Actual);
d:=FindDiff(Expected, Actual);
Result:='Expected: '+dbgstr(Expected,1,d-1)+'|'+dbgstr(Expected,d,length(Expected))+LineEnding
+'Actual: '+dbgstr(Actual,1,d-1)+'|'+dbgstr(Actual,d,length(Actual));
end;
function GetIdentifier(Identifier: PChar): string;
var len: integer;
begin
if (Identifier=nil) then begin
Result:='';
exit;
end;
if (Identifier^='&') and (IsIdentChar[Identifier[1]]) then
inc(Identifier);
if IsIdentStartChar[Identifier^] then begin
len:=0;
while (IsIdentChar[Identifier[len]]) do inc(len);
SetLength(Result,len);
if len>0 then
Move(Identifier[0],Result[1],len);
end else
Result:='';
end;
function FindNextIdentifier(const Source: string; StartPos, MaxPos: integer
): integer;
begin
Result:=StartPos;
while (Result<=MaxPos) and (not IsIdentStartChar[Source[Result]]) do
inc(Result);
end;
function FindNextIdentifierSkipStrings(const Source: string; StartPos,
MaxPos: integer): integer;
var
c: Char;
begin
Result:=StartPos;
while (Result<=MaxPos) do begin
c:=Source[Result];
if IsIdentStartChar[c] then exit;
if c='''' then begin
// skip string constant
inc(Result);
while (Result<=MaxPos) and (not (Source[Result] in ['''',#10,#13])) do
inc(Result);
end;
inc(Result);
end;
end;
function IsValidIdentPair(const NamePair: string): boolean;
var
p: Integer;
begin
Result:=false;
p:=1;
if (p>length(NamePair)) or (not IsIdentStartChar[NamePair[p]]) then exit;
repeat
inc(p);
if p>length(NamePair) then exit;
if NamePair[p]='.' then break;
if not IsIdentChar[NamePair[p]] then exit;
until false;
inc(p);
if (p>length(NamePair)) or (not IsIdentStartChar[NamePair[p]]) then exit;
repeat
inc(p);
if p>length(NamePair) then exit(true);
if not IsIdentChar[NamePair[p]] then exit;
until false;
end;
function IsValidIdentPair(const NamePair: string; out First, Second: string
): boolean;
var
p: Integer;
StartPos: LongInt;
begin
Result:=false;
First:='';
Second:='';
p:=1;
if (p>length(NamePair)) or (not IsIdentStartChar[NamePair[p]]) then exit;
StartPos:=p;
repeat
inc(p);
if p>length(NamePair) then exit;
if NamePair[p]='.' then break;
if not IsIdentChar[NamePair[p]] then exit;
until false;
First:=copy(NamePair,StartPos,p-StartPos);
inc(p);
if (p>length(NamePair)) or (not IsIdentStartChar[NamePair[p]]) then exit;
StartPos:=p;
repeat
inc(p);
if p>length(NamePair) then begin
Second:=copy(NamePair,StartPos,p-StartPos);
exit(true);
end;
if not IsIdentChar[NamePair[p]] then exit;
until false;
end;
function ExtractPasIdentifier(const Ident: string; AllowDots: Boolean): string;
var
p: Integer;
begin
p:=1;
Result:=Ident;
while p<=length(Result) do begin
if Result[p] in ['a'..'z','A'..'Z','_'] then begin
inc(p);
while p<=length(Result) do begin
case Result[p] of
'a'..'z','A'..'Z','_','0'..'9': inc(p);
'.':
if AllowDots then
break
else
Delete(Result,p,1);
else
Delete(Result,p,1);
end;
end;
if p>length(Result) then exit;
// p is now on the '.'
inc(p);
end else
Delete(Result,p,1);
end;
p:=length(Result);
if (p>0) and (Result[p]='.') then
Delete(Result,p,1);
end;
function GetLineIndentWithTabs(const Source: string; Position: integer;
TabWidth: integer): integer;
var p: integer;
begin
Result:=0;
p:=Position;
if p=0 then exit;
if (p<0) then p:=1;
if (p>length(Source)+1) then p:=length(Source)+1;
// search beginning of line
while (p>1) and not (Source[p-1] in [#10,#13]) do
dec(p);
// search code
Result:=0;
while (p<=length(Source)) do begin
case Source[p] of
' ': inc(Result);
#9:
begin
Result:=Result+TabWidth;
Result:=Result-(Result mod TabWidth);
end;
else break;
end;
inc(p);
end;
end;
function GetPosInLine(const Source: string; Position: integer): integer;
begin
Result:=0;
while (Position>1) and (not (Source[Position-1] in [#10,#13])) do begin
inc(Result);
dec(Position);
end;
end;
function GetBlockMinIndent(const Source: string;
StartPos, EndPos: integer): integer;
var
SrcLen: Integer;
p: Integer;
CurIndent: Integer;
begin
SrcLen:=length(Source);
if EndPos>SrcLen then EndPos:=SrcLen+1;
Result:=EndPos-StartPos;
p:=StartPos;
while p<=EndPos do begin
// skip line end and empty lines
while (p<EndPos) and (Source[p] in [#10,#13]) do
inc(p);
if (p>=EndPos) then break;
// count spaces at line start
CurIndent:=0;
while (p<EndPos) and (Source[p] in [#9,' ']) do begin
inc(p);
inc(CurIndent);
end;
if CurIndent<Result then Result:=CurIndent;
// skip rest of line
while (p<EndPos) and (not (Source[p] in [#10,#13])) do
inc(p);
end;
end;
function GetIndentStr(Indent: integer; TabWidth: integer): string;
var
TabCnt: Integer;
SpaceCnt: Integer;
i: Integer;
begin
if TabWidth<=0 then begin
SetLength(Result,Indent);
if Indent>0 then
FillChar(Result[1],length(Result),' ');
end else begin
TabCnt:=Indent div TabWidth;
SpaceCnt:=Indent mod TabWidth;
SetLength(Result,TabCnt+SpaceCnt);
for i:=1 to TabCnt do
Result[i]:=#9;
for i:=TabCnt+1 to TabCnt+SpaceCnt do
Result[i]:=' ';
end;
end;
procedure IndentText(const Source: string; Indent, TabWidth: integer;
out NewSource: string);
function UnindentTxt(CopyChars: boolean): integer;
var
Unindent: Integer;
SrcPos: Integer;
SrcLen: Integer;
NewSrcPos: Integer;
SkippedSpaces: Integer;
c: Char;
begin
Unindent:=-Indent;
SrcPos:=1;
SrcLen:=length(Source);
NewSrcPos:=1;
while SrcPos<=SrcLen do begin
// skip spaces at start of line
SkippedSpaces:=0;
while (SrcPos<=SrcLen) and (SkippedSpaces<Unindent) do begin
c:=Source[SrcPos];
if c=' ' then begin
inc(SkippedSpaces);
inc(SrcPos);
end else if c=#9 then begin
inc(SkippedSpaces,TabWidth);
inc(SrcPos);
end else
break;
end;
// deleting a tab can unindent too much, so insert some spaces
while SkippedSpaces>Unindent do begin
if CopyChars then
NewSource[NewSrcPos]:=' ';
inc(NewSrcPos);
dec(SkippedSpaces);
end;
// copy the rest of the line
while (SrcPos<=SrcLen) do begin
c:=Source[SrcPos];
// copy char
if CopyChars then
NewSource[NewSrcPos]:=Source[SrcPos];
inc(NewSrcPos);
inc(SrcPos);
if (c in [#10,#13]) then begin
// line end
if (SrcPos<=SrcLen) and (Source[SrcPos] in [#10,#13])
and (Source[SrcPos]<>Source[SrcPos-1]) then begin
if CopyChars then
NewSource[NewSrcPos]:=Source[SrcPos];
inc(NewSrcPos);
inc(SrcPos);
end;
break;
end;
end;
end;
Result:=NewSrcPos-1;
end;
var
LengthOfLastLine: integer;
LineEndCnt: Integer;
SrcPos: Integer;
SrcLen: Integer;
NewSrcPos: Integer;
c: Char;
NewSrcLen: Integer;
procedure AddIndent;
var
i: Integer;
begin
for i:=1 to Indent do begin
NewSource[NewSrcPos]:=' ';
inc(NewSrcPos);
end;
end;
begin
if (Indent=0) or (Source='') then begin
NewSource:=Source;
exit;
end;
if Indent>0 then begin
// indent text
LineEndCnt:=LineEndCount(Source,LengthOfLastLine);
if LengthOfLastLine>0 then inc(LineEndCnt);
SetLength(NewSource,LineEndCnt*Indent+length(Source));
SrcPos:=1;
SrcLen:=length(Source);
NewSrcPos:=1;
AddIndent;
while SrcPos<=SrcLen do begin
c:=Source[SrcPos];
// copy char
NewSource[NewSrcPos]:=Source[SrcPos];
inc(NewSrcPos);
inc(SrcPos);
if (c in [#10,#13]) then begin
// line end
if (SrcPos<=SrcLen) and (Source[SrcPos] in [#10,#13])
and (Source[SrcPos]<>Source[SrcPos-1]) then begin
NewSource[NewSrcPos]:=Source[SrcPos];
inc(NewSrcPos);
inc(SrcPos);
end;
if (SrcPos<=SrcLen) and (not (Source[SrcPos] in [#10,#13])) then begin
// next line is not empty -> indent
AddIndent;
end;
end;
end;
SetLength(NewSource,NewSrcPos-1);
end else begin
// unindent text
NewSrcLen:=UnindentTxt(false);
SetLength(NewSource,NewSrcLen);
UnindentTxt(true);
end;
end;
function GetLineStartPosition(const Source: string; Position: integer): integer;
begin
Result:=Position;
while (Result>1) and (not (Source[Result-1] in [#10,#13])) do
dec(Result);
end;
function GetLineInSrc(const Source: string; Position: integer): string;
var
LineStart, LineEnd: integer;
begin
GetLineStartEndAtPosition(Source,Position,LineStart,LineEnd);
//debugln(['GetLineInSrc ',Position,' ',LineStart,' ',LineEnd]);
Result:=copy(Source,LineStart,LineEnd-LineStart);
end;
function LineEndCount(const Txt: string): integer;
var
LengthOfLastLine: integer;
begin
Result:=LineEndCount(Txt,LengthOfLastLine);
if LengthOfLastLine=0 then ;
end;
function DottedIdentifierLength(Identifier: PChar): integer;
var
p: PChar;
begin
Result:=0;
if Identifier=nil then exit;
p:=Identifier;
repeat
if not IsIdentStartChar[p^] then exit;
repeat
inc(p);
until not IsIdentChar[p^];
if p^<>'.' then break;
inc(p);
until false;
Result:=p-Identifier;
end;
function GetDottedIdentifier(Identifier: PChar): string;
var
l: Integer;
begin
l:=DottedIdentifierLength(Identifier);
SetLength(Result,l);
if l>0 then
System.Move(Identifier^,Result[1],l);
end;
function IsDottedIdentifier(const Identifier: string): boolean;
var
p: PChar;
begin
Result:=false;
if Identifier='' then exit;
p:=PChar(Identifier);
repeat
if not IsIdentStartChar[p^] then exit;
repeat
inc(p);
until not IsIdentChar[p^];
if p^<>'.' then break;
inc(p);
until false;
Result:=(p-PChar(Identifier))=length(Identifier);
end;
function CompareDottedIdentifiers(Identifier1, Identifier2: PChar): integer;
begin
if (Identifier1<>nil) then begin
if (Identifier2<>nil) then begin
while (UpChars[Identifier1[0]]=UpChars[Identifier2[0]]) do begin
if (IsDottedIdentChar[Identifier1[0]]) then begin
inc(Identifier1);
inc(Identifier2);
end else begin
Result:=0; // for example 'aaA;' 'aAa;'
exit;
end;
end;
if (IsDottedIdentChar[Identifier1[0]]) then begin
if (IsDottedIdentChar[Identifier2[0]]) then begin
if UpChars[Identifier1[0]]>UpChars[Identifier2[0]] then
Result:=-1 // for example 'aab' 'aaa'
else
Result:=1; // for example 'aaa' 'aab'
end else begin
Result:=-1; // for example 'aaa' 'aa;'
end;
end else begin
if (IsDottedIdentChar[Identifier2[0]]) then
Result:=1 // for example 'aa;' 'aaa'
else
Result:=0; // for example 'aa;' 'aa,'
end;
end else begin
Result:=-1; // for example 'aaa' nil
end;
end else begin
if (Identifier2<>nil) then begin
Result:=1; // for example nil 'bbb'
end else begin
Result:=0; // for example nil nil
end;
end;
end;
function ChompDottedIdentifier(const Identifier: string): string;
var
p: Integer;
begin
p:=length(Identifier);
while (p>0) and (Identifier[p]<>'.') do dec(p);
Result:=LeftStr(Identifier,p-1);
end;
function TrimCodeSpace(const ACode: string): string;
// turn all lineends and special chars to space
// space is combined to one char
// space which is not needed is removed.
// space is only needed between two words or between 2-char operators
var CodePos, ResultPos, CodeLen, SpaceEndPos: integer;
c1, c2: char;
begin
CodeLen:=length(ACode);
SetLength(Result,CodeLen);
CodePos:=1;
ResultPos:=1;
while CodePos<=CodeLen do begin
if ACode[CodePos]>#32 then begin
Result[ResultPos]:=ACode[CodePos];
inc(ResultPos);
inc(CodePos);
end else begin
SpaceEndPos:=CodePos;
while (SpaceEndPos<=CodeLen) and (ACode[SpaceEndPos]<=#32) do
inc(SpaceEndPos);
if (CodePos>1) and (SpaceEndPos<=CodeLen) then begin
c1:=ACode[CodePos-1];
c2:=ACode[SpaceEndPos];
if (IsIdentChar[c1] and IsIdentChar[c2])
// test for double char operators :=, +=, -=, /=, *=, <>, <=, >=, **, ><
or ((c2='=') and (c1 in [':','+','-','/','*','>','<']))
or ((c1='<') and (c2='>'))
or ((c1='>') and (c2='<'))
or ((c1='.') and (c2='.'))
or ((c1='*') and (c2='*'))
or ((c1='@') and (c2='@')) then
begin
// keep one space
Result[ResultPos]:=' ';
inc(ResultPos);
end;
end;
// skip space
CodePos:=SpaceEndPos;
end;
end;
SetLength(Result,ResultPos-1);
end;
function CodeIsOnlySpace(const ACode: string; FromPos, ToPos: integer): boolean;
// from FromPos to including ToPos
var
SrcLen: integer;
CodePos: integer;
begin
Result:=true;
SrcLen:=length(ACode);
if ToPos>SrcLen then ToPos:=SrcLen;
CodePos:=FromPos;
while (CodePos<=ToPos) do begin
if ACode[CodePos] in [' ',#9,#10,#13] then
inc(CodePos)
else begin
Result:=false;
exit;
end;
end;
end;
function StringToPascalConst(const s: string): string;
// converts s to a Pascal string literal
// e.g. foo becomes 'foo', bytes 0..31 become #ord
function Convert(var DestStr: string): integer;
var
SrcLen, SrcPos, DestPos: integer;
c: char;
i: integer;
InString: Boolean;
begin
SrcLen:=length(s);
DestPos:=1;
if DestStr<>'' then DestStr[DestPos]:='''';
InString:=true;
for SrcPos:=1 to SrcLen do begin
inc(DestPos);
c:=s[SrcPos];
if c>=' ' then begin
// normal char
if not InString then begin
if DestStr<>'' then DestStr[DestPos]:='''';
inc(DestPos);
InString:=true;
end;
if DestStr<>'' then
DestStr[DestPos]:=c;
if c='''' then begin
inc(DestPos);
if DestStr<>'' then DestStr[DestPos]:='''';
end;
end else begin
// special char
if InString then begin
if DestStr<>'' then DestStr[DestPos]:='''';
inc(DestPos);
InString:=false;
end;
if DestStr<>'' then
DestStr[DestPos]:='#';
inc(DestPos);
i:=ord(c);
if i>=100 then begin
if DestStr<>'' then
DestStr[DestPos]:=chr((i div 100)+ord('0'));
inc(DestPos);
end;
if i>=10 then begin
if DestStr<>'' then
DestStr[DestPos]:=chr(((i div 10) mod 10)+ord('0'));
inc(DestPos);
end;
if DestStr<>'' then
DestStr[DestPos]:=chr((i mod 10)+ord('0'));
end;
end;
if InString then begin
inc(DestPos);
if DestStr<>'' then DestStr[DestPos]:='''';
InString:=false;
end;
Result:=DestPos;
end;
var
NewLen: integer;
begin
Result:='';
NewLen:=Convert(Result);
if NewLen=length(s) then begin
Result:=s;
exit;
end;
SetLength(Result,NewLen);
Convert(Result);
end;
function SplitStringConstant(const StringConstant: string;
FirstLineLength, OtherLineLengths, Indent: integer;
const aLineBreak: string): string;
{ Split long string constants
If possible it tries to split on word boundaries.
Examples:
1.
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',15,20,6
becomes: |'ABCDEFGHIJKLM'|
| +'NOPQRSTUVWX'|
| +'YZ'|
Result:
'ABCDEFGHIJKLM'#13#10 +'NOPQRSTUVWX'#13#10 +'YZ'
2.
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',5,20,6
}
const
// string constant character types:
stctStart = 'S'; // ' start char
stctEnd = 'E'; // ' end char
stctWordStart = 'W'; // word char after non word char
stctQuotation1 = '1'; // first ' of a double ''
stctQuotation2 = '2'; // second ' of a double ''
stctChar = 'C'; // normal character
stctMBC = 'M'; // follow character of multi byte char
stctHash = '#'; // hash
stctHashNumber = '0'; // hash number
stctLineEnd10 = #10; // hash number is 10
stctLineEnd13 = #13; // hash number is 13
stctJunk = 'j'; // junk
var
SrcLen: Integer;
Src: String;
CurLineMax: Integer;
ParsedSrc: string;
ParsedLen: integer;
SplitPos: integer;
i: Integer;
procedure ParseSrc;
var
APos: Integer;
procedure MarkMBC;
var
l: LongInt;
begin
l:=UTF8CodepointSize(@Src[APos]);
inc(APos);
dec(l);
while (l>0) and (APos<ParsedLen) do begin
ParsedSrc[APos]:=stctMBC;
inc(APos);
dec(l);
end;
end;
var
NumberStart: Integer;
Number: Integer;
begin
APos:=1;
ParsedLen:=CurLineMax+1;
if ParsedLen>SrcLen then ParsedLen:=SrcLen;
SetLength(ParsedSrc,CurLineMax+1);
while APos<=ParsedLen do begin
if Src[APos]='''' then begin
ParsedSrc[APos]:=stctStart;
inc(APos);
while APos<=ParsedLen do begin
if (Src[APos]='''') then begin
inc(APos);
if (APos<=ParsedLen) and (Src[APos]='''') then begin
// double ''
ParsedSrc[APos-1]:=stctQuotation1;
ParsedSrc[APos]:=stctQuotation2;
inc(APos);
end else begin
// end of string
ParsedSrc[APos-1]:=stctEnd;
break;
end;
end else if Src[APos] in ['A'..'Z','a'..'z',#128..#255] then begin
// normal word char
if (APos>1) and (Src[APos-1] in ['A'..'Z','a'..'z',#128..#255]) then
ParsedSrc[APos]:=stctChar
else
ParsedSrc[APos]:=stctWordStart;
MarkMBC;
end else begin
// other char in string constant
ParsedSrc[APos]:=stctWordStart;
inc(APos);
end;
end;
end else if Src[APos]='#' then begin
ParsedSrc[APos]:=stctHash;
inc(APos);
NumberStart:=APos;
if (APos<=ParsedLen) then begin
// parse character number
if IsNumberChar[Src[APos]] then begin
// parse decimal number
while (APos<=ParsedLen) and IsNumberChar[Src[APos]] do begin
ParsedSrc[APos]:=stctHashNumber;
inc(APos);
end;
end else if Src[APos]='$' then begin
// parse hex number
while (APos<=ParsedLen) and IsHexNumberChar[Src[APos]] do begin
ParsedSrc[APos]:=stctHashNumber;
inc(APos);
end;
end;
Number:=StrToIntDef(copy(Src,NumberStart,APos-NumberStart),-1);
if (Number=10) or (Number=13) then begin
while NumberStart<APos do begin
ParsedSrc[NumberStart]:=chr(Number);
inc(NumberStart);
end;
end;
end;
end else begin
// junk
ParsedSrc[APos]:=stctJunk;
MarkMBC;
end;
end;
end;
function SearchCharLeftToRight(c: char): integer;
begin
Result:=1;
while (Result<=ParsedLen) and (ParsedSrc[Result]<>c) do
inc(Result);
if Result>ParsedLen then Result:=-1;
end;
function SearchDiffCharLeftToRight(StartPos: integer): integer;
begin
Result:=StartPos+1;
while (Result<=ParsedLen) and (ParsedSrc[Result]=ParsedSrc[StartPos]) do
inc(Result);
end;
procedure SplitAtNewLineCharConstant;
var
HashPos: Integer;
NewSplitPos: Integer;
begin
if SplitPos>0 then exit;
// check if there is a aLineBreak character constant
HashPos:=SearchCharLeftToRight(stctLineEnd10)-1;
if (HashPos<1) then begin
HashPos:=SearchCharLeftToRight(stctLineEnd13)-1;
if HashPos<1 then exit;
end;
NewSplitPos:=SearchDiffCharLeftToRight(HashPos+1);
if NewSplitPos>CurLineMax then exit;
// check if this is a double new line char const #13#10
if (NewSplitPos<ParsedLen) and (ParsedSrc[NewSplitPos]=stctHash)
and (ParsedSrc[NewSplitPos+1] in [stctLineEnd10,stctLineEnd13])
and (ParsedSrc[NewSplitPos+1]<>ParsedSrc[NewSplitPos-1])
then begin
NewSplitPos:=SearchDiffCharLeftToRight(NewSplitPos+1);
if NewSplitPos>CurLineMax then exit;
end;
SplitPos:=NewSplitPos;
end;
procedure SplitBetweenConstants;
var
APos: Integer;
begin
if SplitPos>0 then exit;
APos:=CurLineMax;
while (APos>=2) do begin
if (ParsedSrc[APos] in [stctHash,stctStart]) then begin
SplitPos:=APos;
exit;
end;
dec(APos);
end;
end;
procedure SplitAtWordBoundary;
var
APos: Integer;
begin
if SplitPos>0 then exit;
APos:=CurLineMax-1;
while (APos>2) and (APos>(CurLineMax shr 1)) do begin
if (ParsedSrc[APos]=stctWordStart) then begin
SplitPos:=APos;
exit;
end;
dec(APos);
end;
end;
procedure SplitDefault;
begin
if SplitPos>0 then exit;
SplitPos:=CurLineMax;
while (SplitPos>1) do begin
if (ParsedSrc[SplitPos]
in [stctStart,stctWordStart,stctChar,stctHash,stctJunk])
then
break;
dec(SplitPos);
end;
end;
procedure Split;
var
CurIndent: Integer;
begin
// move left split side from Src to Result
//DebugLn('Split: SplitPos=',SplitPos,' ',copy(Src,SplitPos-5,6));
Result:=Result+copy(Src,1,SplitPos-1);
Src:=copy(Src,SplitPos,length(Src)-SplitPos+1);
if ParsedSrc[SplitPos] in [stctWordStart,stctChar] then begin
// line break in string constant
// -> add ' to end of last line and to start of next
Result:=Result+'''';
Src:=''''+Src;
end;
SrcLen:=length(Src);
// calculate indent size for next line
CurLineMax:=OtherLineLengths;
CurIndent:=Indent;
if CurIndent>(CurLineMax-10) then
CurIndent:=CurLineMax-10;
if CurIndent<0 then CurIndent:=0;
// add indent spaces to Result
Result:=Result+aLineBreak+GetIndentStr(CurIndent)+'+';
// calculate next maximum line length
CurLineMax:=CurLineMax-CurIndent-1;
end;
begin
Result:='';
if FirstLineLength<5 then FirstLineLength:=5;
if OtherLineLengths<5 then OtherLineLengths:=5;
Src:=StringConstant;
SrcLen:=length(Src);
CurLineMax:=FirstLineLength;
//DebugLn('SplitStringConstant FirstLineLength=',FirstLineLength,
//' OtherLineLengths=',OtherLineLengths,' Indent=',Indent,' ');
i:=0;
repeat
//DebugLn(['SrcLen=',SrcLen,' CurMaxLine=',CurLineMax]);
//DebugLn('Src="',Src,'"');
//DebugLn('Result="',Result,'"');
if SrcLen<=CurLineMax then begin
// line fits
Result:=Result+Src;
break;
end;
// split line -> search nice split position
ParseSrc;
//debugln(['ParsedSrc=',ParsedSrc]);
SplitPos:=0;
SplitAtNewLineCharConstant;
SplitBetweenConstants;
SplitAtWordBoundary;
SplitDefault;
if SplitPos<=1 then begin
// no split possible
Result:=Result+Src;
break;
end;
//debugln(['SplitStringConstant SplitPos=',SplitPos]);
Split;
inc(i);
if i>10 then break;
until false;
//DebugLn('END Result="',Result,'"');
//DebugLn('SplitStringConstant END---------------------------------');
end;
procedure ImproveStringConstantStart(const ACode: string; var StartPos: integer);
// if StartPos is on the first character of a string constant it will be moved
// one in front, that means on the start of the string constant.
// Example: 'A' StartPos=2 -> StartPos:=1
var
AtomStartPos, AtomEndPos: Integer;
Len: Integer;
SubTokenStart: LongInt;
begin
AtomEndPos:=1;
repeat
AtomStartPos:=AtomEndPos;
ReadRawNextPascalAtom(ACode,AtomEndPos,AtomStartPos,true);
if (AtomEndPos>StartPos) then begin
// token found
Len:=length(ACode);
while (AtomStartPos<=Len) do begin
case (ACode[AtomStartPos]) of
'#':
begin
SubTokenStart:=AtomStartPos;
inc(AtomStartPos);
while (AtomStartPos<=Len)
and (ACode[AtomStartPos] in ['0'..'9']) do
inc(AtomStartPos);
if StartPos<AtomStartPos then begin
StartPos:=SubTokenStart;
exit;
end;
end;
'''':
begin
inc(AtomStartPos);
if StartPos=AtomStartPos then begin
StartPos:=AtomStartPos-1;
exit;
end;
while (AtomStartPos<=Len) do begin
if (ACode[AtomStartPos]<>'''') then
inc(AtomStartPos)
else begin
if (AtomStartPos<Len) and (ACode[AtomStartPos+1]='''') then
inc(AtomStartPos)
else
break;
end;
end;
inc(AtomStartPos);
end;
else
break;
end;
end;
end;
until AtomEndPos>StartPos;
end;
procedure ImproveStringConstantEnd(const ACode: string; var EndPos: integer);
// if EndPos is on the last character of a string constant it will be moved
// to the end, that means on the end of the string constant.
// Example: 'A' EndPos=3 -> EndPos:=4
var
AtomStartPos, AtomEndPos: Integer;
Len: Integer;
begin
AtomEndPos:=1;
repeat
AtomStartPos:=AtomEndPos;
ReadRawNextPascalAtom(ACode,AtomEndPos,AtomStartPos,true);
if (AtomEndPos>=EndPos) then begin
// token found
Len:=length(ACode);
while (AtomStartPos<=Len) do begin
case (ACode[AtomStartPos]) of
'#':
begin
inc(AtomStartPos);
while (AtomStartPos<=Len)
and (ACode[AtomStartPos] in ['0'..'9']) do
inc(AtomStartPos);
if EndPos<AtomStartPos then begin
EndPos:=AtomStartPos;
exit;
end;
end;
'''':
begin
inc(AtomStartPos);
while (AtomStartPos<=Len) do begin
if (ACode[AtomStartPos]<>'''') then
inc(AtomStartPos)
else begin
if (AtomStartPos<Len) and (ACode[AtomStartPos+1]='''') then
inc(AtomStartPos)
else
break;
end;
end;
inc(AtomStartPos);
if EndPos=AtomStartPos-1 then begin
EndPos:=AtomStartPos;
exit;
end;
end;
else
break;
end;
end;
end;
until AtomEndPos>=EndPos;
end;
function HexStrToIntDef(p: PChar; Def: integer): integer;
var
OldP: PChar;
i: Integer;
begin
Result:=0;
OldP:=p;
while true do begin
case p^ of
'0'..'9': i:=ord(p^)-ord('0');
'A'..'Z': i:=ord(p^)-ord('A')+10;
'a'..'z': i:=ord(p^)-ord('a')+10;
else
exit;
end;
if Result>(High(Result) shr 4) then exit(Def);
Result:=(Result shl 4)+i;
inc(p);
end;
if OldP=p then exit(Def);
end;
function SearchNextInText(Search: PChar; SearchLen: PtrInt; Src: PChar;
SrcLen: PtrInt; StartPos: PtrInt; out MatchStart, MatchEnd: PtrInt;
WholeWords: boolean; MultiLine: boolean): boolean;
{ search Search in Src starting at StartPos.
MatchEnd will be the position of the first character after the found pattern.
if WholeWords then in front of MatchStart and behind MatchEnd will be
a non word character.
if MultiLine then newline characters are the same #13#10 = #10 = #13. }
var
EndSrc: PChar;
EndSearch: PChar;
FirstChar: Char;
CurPos: PChar;
CmpSearch: PChar;
CmpSrc: PChar;
begin
Result:=false;
MatchStart:=0;
MatchEnd:=0;
if (Search=nil) or (Src=nil) then exit;
EndSrc:=@Src[SrcLen];
EndSearch:=@Search[SearchLen];
FirstChar:=Search^;
CurPos:=@Src[StartPos];
while (CurPos<EndSrc) do begin
if (FirstChar=CurPos^)
and ((not WholeWords) or (CurPos=Src) or (IsNonWordChar[PChar(CurPos-1)^]))
then begin
CmpSearch:=Search;
CmpSrc:=CurPos;
while (CmpSearch<EndSearch) and (CmpSrc<EndSrc) do begin
if CmpSearch^=CmpSrc^ then begin
inc(CmpSearch);
inc(CmpSrc);
end else if MultiLine
and (CmpSrc^ in [#13,#10]) and (CmpSearch^ in [#13,#10]) then begin
if (CmpSrc+1<EndSrc) and (CmpSrc[1] in [#13,#10])
and (CmpSrc^<>CmpSrc[1]) then
inc(CmpSrc,2)
else
inc(CmpSrc);
if (CmpSearch+1<EndSearch) and (CmpSearch[1] in [#13,#10])
and (CmpSearch^<>CmpSearch[1]) then
inc(CmpSearch,2)
else
inc(CmpSearch);
end else begin
break;
end;
end;
if (CmpSearch=EndSearch)
and ((not WholeWords) or (CmpSrc=EndSrc) or (IsNonWordChar[CmpSrc^])) then
begin
// pattern found
Result:=true;
MatchStart:=CurPos-Src;
MatchEnd:=CmpSrc-Src;
exit;
end;
end;
inc(CurPos);
end;
end;
procedure HasTxtWord(SearchWord, Txt: PChar; out WholeWord: boolean; out
Count: SizeInt);
var
StartChar: Char;
CurSearchP: PChar;
CurTxtP: PChar;
TxtRun: PChar;
begin
WholeWord:=false;
Count:=0;
if (SearchWord=nil) or (SearchWord^=#0) then exit;
if (Txt=nil) or (Txt^=#0) then exit;
TxtRun:=Txt;
StartChar:=SearchWord^;
while TxtRun^<>#0 do begin
if TxtRun^=StartChar then begin
CurSearchP:=SearchWord+1;
CurTxtP:=TxtRun+1;
while (CurTxtP^=CurSearchP^) and (CurTxtP^<>#0) do begin
inc(CurTxtP);
inc(CurSearchP);
end;
if CurSearchP^=#0 then begin
// word found
if ((TxtRun=Txt) or IsNonWordChar[TxtRun[-1]])
and IsNonWordChar[CurTxtP^] then begin
// word boundaries
if not WholeWord then begin
WholeWord:=true;
Count:=1;
end else
inc(Count);
end else
inc(Count);
TxtRun:=CurTxtP;
continue;
end;
end;
inc(TxtRun);
end;
end;
function SubString(p: PChar; Count: SizeInt): string;
var
l: SizeInt;
begin
if (p=nil) or (Count=0) then exit('');
l:=IndexByte(p^,Count,0);
if l<0 then l:=Count;
if l=0 then exit('');
SetLength(Result,l);
System.Move(p^,Result[1],l);
end;
function ExtractFileNamespace(const Filename: string): string;
begin
Result:=ExtractFileNameOnly(Filename);
if Result='' then exit;
Result:=ChompDottedIdentifier(Result);
end;
procedure AddToTreeOfUnitFilesOrNamespaces(var TreeOfUnitFiles,
TreeOfNameSpaces: TAVLTree; const NameSpacePath, Filename: string;
CaseInsensitive, KeepDoubles: boolean);
procedure FileAndNameSpaceFits(const UnitName: string;
out FileNameFits, NameSpaceFits: Boolean);
var
CompareCaseInsensitive: Boolean;
begin
FileNameFits := False;
NameSpaceFits := False;
if NameSpacePath = '' then begin
FileNameFits := true;
NameSpaceFits := true;
Exit;
end;
if Length(UnitName) < Length(NameSpacePath) then Exit;
CompareCaseInsensitive:=CaseInsensitive;
{$IFDEF Windows}
CompareCaseInsensitive:=true;
{$ENDIF}
if CompareText(PChar(UnitName), Length(NameSpacePath), PChar(NameSpacePath), Length(NameSpacePath), not CompareCaseInsensitive) = 0 then
begin
FileNameFits := PosEx('.', UnitName, Length(NameSpacePath)+1) = 0;
NameSpaceFits := not FileNameFits;
end;
end;
var
FileNameFits, NameSpaceFits: Boolean;
UnitName: string;
begin
UnitName := ExtractFileNameOnly(Filename);
if not IsDottedIdentifier(UnitName) then exit;
FileAndNameSpaceFits(UnitName, FileNameFits, NameSpaceFits);
if FileNameFits then
AddToTreeOfUnitFiles(TreeOfUnitFiles,FileName,UnitName,KeepDoubles);
if NameSpaceFits then
AddToTreeOfNamespaces(TreeOfNamespaces,UnitName,NameSpacePath,KeepDoubles)
end;
function GatherUnitFiles(const BaseDir, SearchPath, Extensions,
NameSpacePath: string; KeepDoubles, CaseInsensitive: boolean;
var TreeOfUnitFiles, TreeOfNamespaces: TAVLTree): boolean;
{ BaseDir: base directory, used when SearchPath is relative
SearchPath: semicolon separated list of directories
Extensions: semicolon separated list of extensions (e.g. 'pas;.pp;ppu')
NameSpacePath: gather files only from this namespace path, empty '' for all
KeepDoubles: false to return only the first match of each unit
CaseInsensitive: true to ignore case on comparing extensions
TreeOfUnitFiles: tree of TUnitFileInfo
TreeOfNamespaces: tree of TNameSpaceInfo }
var
SearchedDirectories: TAVLTree; // tree of AnsiString
function DirectoryAlreadySearched(const ADirectory: string): boolean;
begin
Result:=(SearchedDirectories<>nil)
and (SearchedDirectories.Find(Pointer(ADirectory))<>nil);
end;
procedure MarkDirectoryAsSearched(const ADirectory: string);
var
s: String;
begin
// increase refcount
//DebugLn('MarkDirectoryAsSearched ',ADirectory);
s:=ADirectory; // increase refcount
if SearchedDirectories=nil then
SearchedDirectories:=TAVLTree.Create(@CompareAnsiStringFilenames);
SearchedDirectories.Add(Pointer(s));
Pointer(s):=nil; // keep refcount
end;
procedure FreeSearchedDirectories;
var
ANode: TAVLTreeNode;
s: String;
begin
if SearchedDirectories=nil then exit;
s:='';
ANode:=SearchedDirectories.FindLowest;
while ANode<>nil do begin
Pointer(s):=ANode.Data;
//DebugLn('FreeSearchedDirectories ',s);
s:=''; // decrease refcount
ANode:=SearchedDirectories.FindSuccessor(ANode);
end;
if s='' then ;
SearchedDirectories.Free;
end;
function ExtensionFits(const Filename: string): boolean;
var
ExtStart: Integer;
ExtLen: Integer; // length without '.'
CurExtStart: Integer;
CurExtEnd: LongInt;
CompareCaseInsensitive: Boolean;
p: Integer;
begin
CompareCaseInsensitive:=CaseInsensitive;
{$IFDEF Windows}
CompareCaseInsensitive:=true;
{$ENDIF}
ExtStart:=length(Filename);
while (ExtStart>=1) and (not (Filename[ExtStart] in [PathDelim,'.'])) do
dec(ExtStart);
if (ExtStart>0) and (Filename[ExtStart]='.') then begin
// filename has an extension
ExtLen:=length(Filename)-ExtStart;
inc(ExtStart);
CurExtStart:=1;
while (CurExtStart<=length(Extensions)) do begin
// skip '.'
if Extensions[CurExtStart]='.' then inc(CurExtStart);
// read till semicolon
CurExtEnd:=CurExtStart;
while (CurExtEnd<=length(Extensions)) and (Extensions[CurExtEnd]<>';')
do
inc(CurExtEnd);
if (CurExtEnd>CurExtStart) and (CurExtEnd-CurExtStart=ExtLen) then begin
// compare extension
p:=ExtLen-1;
while (p>=0) do begin
if CompareCaseInsensitive then begin
if UpChars[Filename[ExtStart+p]]
<>UpChars[Extensions[CurExtStart+p]]
then
break;
end else begin
if Filename[ExtStart+p]<>Extensions[CurExtStart+p] then
break;
end;
dec(p);
end;
if p<0 then begin
// extension fit
Result:=true;
exit;
end;
end;
CurExtStart:=CurExtEnd+1;
end;
end;
Result:=false;
end;
function SearchDirectory(const ADirectory: string): boolean;
var
FileInfo: TSearchRec;
begin
Result:=true;
//DebugLn('SearchDirectory ADirectory="',ADirectory,'"');
if DirectoryAlreadySearched(ADirectory) then exit;
MarkDirectoryAsSearched(ADirectory);
//DebugLn('SearchDirectory searching ...');
if not DirPathExists(ADirectory) then exit;
if FindFirstUTF8(ADirectory+FileMask,faAnyFile,FileInfo)=0 then begin
repeat
// check if special file
if (FileInfo.Name='.') or (FileInfo.Name='..') or (FileInfo.Name='')
then
continue;
if ExtensionFits(FileInfo.Name) then begin
AddToTreeOfUnitFilesOrNamespaces(TreeOfUnitFiles, TreeOfNamespaces,
NameSpacePath, ADirectory+FileInfo.Name, CaseInsensitive, KeepDoubles);
end;
until FindNextUTF8(FileInfo)<>0;
end;
FindCloseUTF8(FileInfo);
end;
var
PathStartPos: Integer;
PathEndPos: LongInt;
CurDir: String;
begin
Result:=false;
SearchedDirectories:=nil;
try
// search all paths in SearchPath
PathStartPos:=1;
while PathStartPos<=length(SearchPath) do begin
PathEndPos:=PathStartPos;
while (PathEndPos<=length(SearchPath)) and (SearchPath[PathEndPos]<>';')
do
inc(PathEndPos);
if PathEndPos>PathStartPos then begin
CurDir:=AppendPathDelim(TrimFilename(
copy(SearchPath,PathStartPos,PathEndPos-PathStartPos)));
if not FilenameIsAbsolute(CurDir) then
CurDir:=AppendPathDelim(BaseDir)+CurDir;
if not SearchDirectory(CurDir) then exit;
end;
PathStartPos:=PathEndPos;
while (PathStartPos<=length(SearchPath))
and (SearchPath[PathStartPos]=';') do
inc(PathStartPos);
end;
Result:=true;
finally
FreeSearchedDirectories;
end;
end;
procedure FreeTreeOfUnitFiles(TreeOfUnitFiles: TAVLTree);
begin
if TreeOfUnitFiles=nil then exit;
TreeOfUnitFiles.FreeAndClear;
TreeOfUnitFiles.Free;
end;
procedure AddToTreeOfUnitFiles(var TreeOfUnitFiles: TAVLTree; const Filename,
Unitname: string; KeepDoubles: boolean);
var
NewItem: TUnitFileInfo;
begin
if (not KeepDoubles) then begin
if (TreeOfUnitFiles<>nil)
and (TreeOfUnitFiles.FindKey(Pointer(UnitName),
@CompareUnitNameAndUnitFileInfo)<>nil)
then begin
// an unit with the same name was already found and doubles are not
// wanted
exit;
end;
end;
// add
if TreeOfUnitFiles=nil then
TreeOfUnitFiles:=TAVLTree.Create(@CompareUnitFileInfos);
NewItem:=TUnitFileInfo.Create(UnitName,Filename);
TreeOfUnitFiles.Add(NewItem);
end;
procedure AddToTreeOfNamespaces(var TreeOfNameSpaces: TAVLTree; const UnitName,
ParentNameSpacePath: string; KeepDoubles: boolean);
var
AnNameSpace: String;
NewItem: TNameSpaceInfo;
PointPos: Integer;
begin
PointPos := PosEx('.', UnitName, Length(ParentNameSpacePath)+1);
if PointPos = 0 then Exit;
AnNameSpace:=Copy(UnitName, Length(ParentNameSpacePath)+1, PointPos - Length(ParentNameSpacePath) - 1);
if AnNameSpace = '' then Exit;
if (not KeepDoubles) then begin
if (TreeOfNameSpaces<>nil)
and (TreeOfNameSpaces.FindKey(Pointer(AnNameSpace),
@CompareNameSpaceAndNameSpaceInfo)<>nil)
then begin
// a namespace with the same name was already found and doubles are not
// wanted
exit;
end;
end;
// add
if TreeOfNameSpaces=nil then
TreeOfNameSpaces:=TAVLTree.Create(@CompareNameSpaceInfos);
NewItem:=TNameSpaceInfo.Create(AnNameSpace,UnitName,Length(ParentNameSpacePath)+1);
TreeOfNameSpaces.Add(NewItem);
end;
function CompareUnitFileInfos(Data1, Data2: Pointer): integer;
begin
Result:=CompareIdentifiers(PChar(TUnitFileInfo(Data1).FileUnitName),
PChar(TUnitFileInfo(Data2).FileUnitName));
end;
function CompareNameSpaceInfos(Data1, Data2: Pointer): integer;
begin
Result:=CompareIdentifiers(PChar(TNameSpaceInfo(Data1).NameSpace),
PChar(TNameSpaceInfo(Data2).NameSpace));
end;
function CompareUnitNameAndUnitFileInfo(UnitnamePAnsiString,
UnitFileInfo: Pointer): integer;
begin
//do not use CompareIdentifiers - they compare only to the first "."
Result:=CompareText(PChar(UnitnamePAnsiString),
PChar(TUnitFileInfo(UnitFileInfo).FileUnitName));
end;
function CompareNameSpaceAndNameSpaceInfo(NamespacePAnsiString,
NamespaceInfo: Pointer): integer;
begin
//do not use CompareIdentifiers - they compare only to the first "."
Result:=CompareText(PChar(NamespacePAnsiString),
PChar(TNameSpaceInfo(NamespaceInfo).NameSpace));
end;
function CountNeededLineEndsToAddForward(const Src: string;
StartPos, MinLineEnds: integer): integer;
var c:char;
SrcLen: integer;
begin
Result:=MinLineEnds;
if (StartPos<1) or (Result=0) then exit;
SrcLen:=length(Src);
while (StartPos<=SrcLen) do begin
c:=Src[StartPos];
if c in [#10,#13] then begin
dec(Result);
if Result=0 then break;
inc(StartPos);
if (StartPos<=SrcLen)
and (Src[StartPos] in [#10,#13])
and (Src[StartPos]<>c) then
inc(StartPos);
end else if IsSpaceChar[c] then
inc(StartPos)
else
break;
end;
end;
function CountNeededLineEndsToAddBackward(
const Src: string; StartPos, MinLineEnds: integer): integer;
var c:char;
SrcLen: integer;
begin
Result:=MinLineEnds;
SrcLen:=length(Src);
if (StartPos>SrcLen) or (Result=0) then exit;
while (StartPos>=1) do begin
c:=Src[StartPos];
if c in [#10,#13] then begin
dec(Result);
if Result=0 then break;
dec(StartPos);
if (StartPos>=1)
and (Src[StartPos] in [#10,#13])
and (Src[StartPos]<>c) then
dec(StartPos);
end else if IsSpaceChar[c] then
dec(StartPos)
else
break;
end;
end;
procedure AdjustPositionAfterInsert(var p: integer; IsStart: boolean; FromPos,
ToPos, DiffPos: integer);
begin
if (ToPos>FromPos) then begin
// replace
if p>FromPos then begin
if p>ToPos then
inc(p,DiffPos)
else
p:=FromPos;
end;
end else begin
// insert
if IsStart then begin
if p>=FromPos then inc(p,DiffPos);
end else begin
if p>FromPos then inc(p,DiffPos);
end;
end;
end;
function CompareText(Txt1: PChar; Len1: integer; Txt2: PChar; Len2: integer;
CaseSensitive: boolean): integer;
begin
if CaseSensitive then begin
while (Len1>0) and (Len2>0) do begin
if Txt1^=Txt2^ then begin
inc(Txt1);
dec(Len1);
inc(Txt2);
dec(Len2);
end else begin
if Txt1^<Txt2^ then
Result:=1
else
Result:=-1;
exit;
end;
end;
end else begin
while (Len1>0) and (Len2>0) do begin
if UpChars[Txt1^]=UpChars[Txt2^] then begin
inc(Txt1);
dec(Len1);
inc(Txt2);
dec(Len2);
end else begin
if UpChars[Txt1^]<UpChars[Txt2^] then
Result:=1
else
Result:=-1;
exit;
end;
end;
end;
if Len1>Len2 then
Result:=-1
else if Len1<Len2 then
Result:=1
else
Result:=0;
end;
function CompareTextCT(const Txt1, Txt2: string; CaseSensitive: boolean): integer;
begin
Result:=CompareText(PChar(Pointer(Txt1)),length(Txt1),
PChar(Pointer(Txt2)),length(Txt2),CaseSensitive);
end;
function CompareText(Txt1: PChar; Len1: integer; Txt2: PChar; Len2: integer;
CaseSensitive, IgnoreSpace: boolean): integer;
begin
if IgnoreSpace then
Result:=CompareTextIgnoringSpace(Txt1,Len1,Txt2,Len2,CaseSensitive)
else
Result:=CompareText(Txt1,Len1,Txt2,Len2,CaseSensitive);
end;
{ TNameSpaceInfo }
constructor TNameSpaceInfo.Create(const TheNamespace, TheUnitName: string;
TheIdentifierStartInUnitName: Integer);
begin
FNamespace:=TheNamespace;
FUnitName:=TheUnitName;
FIdentifierStartInUnitName:=TheIdentifierStartInUnitName;
end;
{ TUnitFileInfo }
constructor TUnitFileInfo.Create(const TheUnitName, TheFilename: string);
begin
FUnitName:=TheUnitName;
FFilename:=TheFilename;
end;
function TUnitFileInfo.GetFileUnitNameWithoutNamespace: string;
var
LastPoint: Integer;
begin
LastPoint := LastDelimiter('.', FUnitName);
if LastPoint > 0 then
Result := Copy(FUnitName, LastPoint+1, High(Integer))
else
Result := FUnitName;
end;
function TUnitFileInfo.GetIdentifierStartInUnitName: Integer;
var
LastPoint: Integer;
begin
LastPoint := LastDelimiter('.', FUnitName);
if LastPoint > 0 then
Result := LastPoint+1
else
Result := 1;
end;
//=============================================================================
end.
|