[r7648]: sandbox / jlf / samples / rgf_util2 / rgf_util2.rex  Maximize  Restore  History

Download this file

3325 lines (2560 with data), 113.5 kB

   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
/*
url to documentation and reference card (as of 2010-05-14):
http://wi.wu.ac.at/rgf/rexx/orx20/
author: Rony G. Flatscher, copyright 2008-2011, all rights reserved
date: 2007-06-01 - 2007-06-02; 2007-07-30; 2007-08-02, 2007-08-07, 2007-08-15,
2007-10-20, 2007-10-23
2007-12-30 - made NumberComparator more flexible, allows to use
intermixed non-numbers, if programmer wishes to do so
- MessageComparator: now allows collection of message-names
and/or message objects to use for comparisons
2008-01-08 - added "/numeric" hint for message names
2008-02-15 - changed StringComparator to be more flexible with its
argument
2008-02-16 - changed StringComparator to be simpler & more flexible,
as well as sort2, stableSort2; created samples (f:\test\orx\rgf_util2)
2008-02-17 - changed/improved StringColumnComparator, created samples for it
2008-02-19 - removed usage of built-in comparators in sort2() and
stableSort2(), so none of the 3.2.0 specialized
Comparators are needed
- added "-length" to ABBREV2(),
2008-02-20 - added "-count" to CHANGESTR2() ("change last 'count' needle occurrences)
- added negative starting position to lastPos2()
- added left2()-BIF, handling with negative start position
- added right2()-BIF, handling with negative start position
- added/enhanced pos2()-BIF with negative start position
2008-02-21 - added DELSTR2(), LOWER2(), SUBSTR2(), UPPER2()
which now all accept negative numbers
2008-02-22 - added OVERLAY2(), which now accepts negative numbers
2008-02-25 - added PARSEWORDS2()
2008-03-13 - changed DUMP2 to cater for the different kind of collections,
ones), will not sort OrderedCollections, but honor their order
[will show exact array-index values (including multi-dimensional],
will sort by index-value otherwise, in the case of "allAt"-collections
(e.g. Relation) will give a list of the items associated with the
same index
2008-03-14 - changed DUMP2() to display item, if allAt() returns a collection
containing only one item; added SUBCHAR2() allowing negative
positions as well
added negative position to WORDPOS2()
added DELWORD2, SUBWORD2(), WORD2(), WORDINDEX2(), WORDLENGTH2()
2008-03-16 - tested and fixed StringOfWords class
2008-03-19 - recoded sort2() and stableSort2() to take advantage of .StringComparator
and .StringColumnComparator
2008-03-27 - allow in list of messages array-elements with two entries, where
- arr[1]=messageString|messageObject
- arr[2]=flagString
- added option "M" (message sort) to sort2() and stableSort2()
2008-03-29, - sort2() and stableSort2() now accept as their first argument
an object with a "makeArray" method in addition to instances
of array
- .MessageComparator: if an array-element is given, then index 3 in
addition to index 2 are regarded to be flag (parts)
2008-03-16: - dump2 now gracefully deals with non-collection and non-supplier
objects: an appropriate hint is given, as well as the type and
(string) value of the argument
2009-03-15: - changed default of string-routines to use the "caseless"
version
2009-03-20: - changed NumericComparator to use caseless comparison in case
relaxed comparisons are carried out
- change default sort2() and stableSort2() to use "N", i.e.
ascending sort with numeric comparisons, and caseless comparisons
- added MakeArray to class StringOfWords
2009-12-14: - make sure all public routines have a trailing "2" to indicate that
they come from this package and to avoid name clashes with earlier
implementations
2009-12-19: - changed default for sort2() and stableSort2() to "ignore case", if
string objects are to be sorted
2009-12-22: - When creating a NumberComparator one can now determine the order (A|D)
and kind of comparison (I | C)
2009-12-26: - default to "I"gnore case in .StringColumnComparator
- parseWords2: if returning position array, supply third array element
2009-12-27: - .StringOfWords:
- delWord(): make sure dirty flags are set
- subWord(): don't change string itself, if returning subwords
- wordPos(): default for compare now "I[gnore]" case
2009-12-28: - .StringOfWords:
- delWord(): do not edit string in place, return an edited copy
2010-01-16, change "rgf.numbers" to "rgf.digits" (thanks to Walter Pachl!)
2010-08-15, .NumberComparator ignored second argument (order) in constructor,
if first argument was set to .true (thanks to Glenn Knickerbocker
on comp.lang.rexx)
2011-05-30, - fix error not allowing suppliers to be shown in dump2(); this
follows Jean-Louis fix in his ooRexx sandbox as of 2011-05-30;
also changed sequence of argument checking to follow sequence
of arguments as seen in Jean-Louis' version of rgf_util2.rex
2011-06-08, - new routine ppCondition2(co[,bShowPackage=.false [,indent1="09"x [,indent2="0909"x [,indent3="090909"x [,lf=.endOfLine]]]]]]):
returns a string rendering of the supplied condition object "co"
- new routine ppPackage2(package[,indent1=""[, indent2="09"x[, lf==.endOfLine]]):
returns a string rendering of the supplied package object
2011-08-03, - ppCondition2(): make sure that length is only calculated, if a string in hand
purpose: set of 3.2 utilities to ease programming of 3.2.0, e.g. offer sort2()- and
stableSort2()-BIFs that handle all kind of standard sorting needs, thereby
removing the need for "low level" coding in ooRexx itself
TODO: - ? create a DateTime2 class with renaming existing conversion
methods to start with "to"; also supply epoch-related
conversions (from/to); also allow to define the date
when Julian calendar took effect; supply method to determine
Easter Sunday (depending on the calendar in use)
- create routines "leftWord([-]n)", "rightWord([-]n)"
license: Choice of
ASF 2.0, <http://www.apache.org/licenses/LICENSE-2.0>:
--------------- cut here ----------------
Copyright 2008-2011 Rony G. Flatscher
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--------------- cut here ----------------
or
LGPL 3.0, <http://www.fsf.org/licensing/licenses/lgpl.html> (as of: 2008-02-17)
version: 1.0.6
*/
.local~rgf.non.printable=xrange("00"x,"1F"x)||"FF"x
.local~rgf.alpha.low="abcdefghijklmnopqrstuvwxyz"
.local~rgf.alpha.upper =.rgf.alpha.low~upper
.local~rgf.alpha =.rgf.alpha.low || .rgf.alpha.upper
.local~rgf.digits ="0123456789"
.local~rgf.alphanumeric=.rgf.alpha || .rgf.digits
.local~rgf.symbol.chars=".!_?"
parse version "_" v "("
if v<4 then -- ooRexx smaller than 4.0.0, then use ".public_routines"
do
-- make version"2" BIFs globally available
do idx over .methods
.public_routines~put(.methods[idx], idx)
end
-- make the classes seen globally via .local directory
.local~messageComparator =.messageComparator
.local~NumberComparator =.NumberComparator
.local~StringComparator =.StringComparator
.local~StringColumnComparator=.StringColumnComparator
.local~StringOfWords =.StringOfWords
end
else -- running under ooRexx 4.0.0 or higher
do
thisPackage=.context~package
do idx over .methods
-- routine=.routine~new(idx, .methods[idx]~source)
routine=.routine~new("DYNAMICALLY_CREATED_"idx, .methods[idx]~source)
thisPackage~addPublicRoutine(idx, routine)
end
-- make the classes seen globally via the package's public classes
thisPackage~addPublicClass("MessageComparator" , .messageComparator )
thisPackage~addPublicClass("NumberComparator" , .NumberComparator )
thisPackage~addPublicClass("StringComparator" , .StringComparator )
thisPackage~addPublicClass("StringColumnComparator", .StringColumnComparator)
thisPackage~addPublicClass("StringOfWords" , .StringOfWords )
end
/* ======================================================================= */
/* === methods to be used for new BIFs === */
/* ======================================================================= */
-- 2008-02-19, rgf: abbrev info, string [, n-length]
/* if length is negative, then */
/* ======================================================================= */
::method "abbrev2"
use strict arg arg1, arg2, ...
argNr=arg() -- get maximum number of arguments
BIFpos=3 -- last classic BIF argument position
maxArgs=4
signal on syntax
if argNr>maxArgs then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArgs)
methName="abbrev" -- base name for the message to send
bCaseDependent =.false -- default to caseless version
if argNr>=BIFpos, \datatype(arg(argNr),"N") then
do
letter=arg(argNr)~strip~left(1)~upper
if pos(letter,"CI")=0 then -- illegal argument!
raise syntax 93.914 array (argNr, "C, I", arg(argNr))
bCaseDependent=(letter="C")
argNr-=1 -- decrease one from total number of arguments
end
newArr=.array~new -- create new array for the arguments
newArr[1]=arg2 -- save info
if arg(3,"Exists"), datatype(arg(3),"N") then
do
arg3=arg(3) -- negative?
if arg3<0 then -- length, i.e. extract from right
do
newArr[1]=arg2~right(-arg3) -- get the chars from the right
end
else
do
newArr[2]=arg3
end
end
-- now invoke the operation
if bCaseDependent then
return .message~new(arg(1), methName, "A", newArr)~send
else
return .message~new(arg(1), "caseless"methName, "A", newArr)~send
syntax:
raise propagate
/* ======================================================================= */
/* if count is negative, then the number of changes occur from the right side
("change the last 'count' of 'needle' occurrences in string")
*/
::method "changeStr2" -- (needle,haystack,newNeedle[,[-]count][,CI])
use strict arg arg1needle, arg2haystack, arg3newNeedle, ... -- make sure at least three args are supplied
parse arg arg1needle, arg2haystack, arg3newNeedle, arg4count
argNr=arg() -- get maximum number of arguments
BIFpos=3 -- last mandatory BIF argument position
maxArgs=5
signal on syntax
if argNr>maxArgs then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArgs)
methName="changeStr" -- base name for the message to send
bCaseDependent =.false -- default to caseless version
newArr=.array~new -- create new array for the arguments
newArr[1]=arg1needle
newArr[2]=arg3newNeedle
signal on syntax
if argNr>(BIFpos) then -- either count or "CI"
do
count=.nil
if \datatype(arg(argNr),"W") then -- "C" or "I"
do
letter=arg(argNr)~strip~left(1)~upper
if pos(letter,"CI")=0 then -- illegal argument!
raise syntax 93.914 array (argNr, "C, I", arg(argNr))
bCaseDependent=(letter="C")
if argNr>4, arg(4,"E") then -- check for "count" argument
count=arg4count -- save count-value
end
else
count=arg4count -- save count-value
end
if datatype(count,"N") then -- count is numeric, check it out
do
if count<0 then -- change the "count" last occurrences in string!
do
-- search starting position for changes
len=length(arg2haystack) -- remember length of string
pos=len -- start out with last position of string
-- find starting position
do i=1 to -count until pos=0
oldPos=pos
if oldPos<=1 then leave -- already at beginning!
if bCaseDependent then
do
pos= lastPos(arg1needle, arg2haystack, oldPos-(1-(len=oldPos)))
end
else -- ignore case
do
pos=lastPos2(arg1needle, arg2haystack, oldPos-(1-(len=oldPos)), "I")
end
end
-- carry out the changes
if oldPos>1, pos>0 then -- o.k., not all "needle"s to change: split, change and return
do
-- extract part that does not get changed
mb=.MutableBuffer~new~~append( arg2haystack~substr(1,Pos-1) )
-- change needle in remainder, add changed string to MutableBuffer
if bCaseDependent then
mb~append( .message~new(arg2haystack~substr(Pos), methName, "A", newArr)~send)
else
mb~append( .message~new(arg2haystack~substr(Pos), "caseless"methName, "A", newArr)~send)
return mb~string -- return changed string
end
end
else
do
newArr[3]=arg4count -- save "count" argument
end
end
-- now invoke the operation
if bCaseDependent then
return .message~new(arg2haystack, methName, "A", newArr)~send
else
return .message~new(arg2haystack, "caseless"methName, "A", newArr)~send
syntax:
raise propagate
/* ======================================================================= */
-- string1, string2[, [padChar] [,{C|I}]]
::method "compare2"
use strict arg arg1string1, arg2string2, arg3padChar=" ", ...
argNr=arg() -- get maximum number of arguments
BIFpos=3 -- last classic BIF argument position
maxArg=4
signal on syntax
if argNr>maxArg then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArg)
methName="compare" -- base name for the message to send
bCaseDependent =.false -- default to caseless version
if argNr>BIFpos then
do
letter=arg(maxArg)~strip~left(1)~upper
if pos(letter,"CI")=0 then -- illegal argument!
raise syntax 93.914 array (argNr, "C, I", arg(maxArg))
bCaseDependent=(letter="C")
argNr-=1 -- decrease one from total number of arguments
end
newArr=.array~new -- create new array for the arguments
newArr[1]=arg2string2 -- other string
newArr[2]=arg3padChar -- pad character
-- now invoke the operation
if bCaseDependent then
return .message~new(arg1string1, methName, "A", newArr)~send
else
return .message~new(arg1string1, "caseless"methName, "A", newArr)~send
syntax:
raise propagate
/* ======================================================================= */
-- not a BIF ::method "compareTo2"
/* ======================================================================= */
-- needle, haystack[,{C|I}]
::method "countStr2"
use strict arg arg1needle, arg2haystack, ...
argNr=arg() -- get maximum number of arguments
BIFpos=2 -- last classic BIF argument position
maxArg=3
signal on syntax
if argNr>maxArg then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArg)
methName="countStr" -- base name for the message to send
bCaseDependent =.false -- default to caseless version
if argNr>BIFpos then
do
letter=arg(maxArg)~strip~left(1)~upper
if pos(letter,"CI")=0 then -- illegal argument!
raise syntax 93.914 array (maxArg, "C, I", arg(maxArg))
bCaseDependent=(letter="C")
end
-- now invoke the operation
if bCaseDependent then
return .message~new(arg2haystack, methName, "I", arg1needle )~send
else
return .message~new(arg2haystack, "caseless"methName, "I", arg1needle )~send
syntax:
raise propagate
-- 2008-02-21, rgf: delStr2(string ,n-start [, n-length])
/* if length is negative, then */
/* ======================================================================= */
::method "delStr2"
use strict arg arg1, ... -- make sure we have at least one arg
parse arg ., arg2, arg3
argNr=arg() -- get maximum number of arguments
BIFpos=3 -- last classic BIF argument position
maxArgs=3
signal on syntax
if argNr>maxArgs then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArgs)
methName="delStr" -- base name for the message to send
len1=length(arg1) -- get length of string
newArr=.array~new -- create new array for the arguments
if datatype(arg2,"W") then -- start
do
if arg2<0 then -- negative, start from right
do
tmp=len1+arg2+1 -- get starting position
if tmp<2 then -- start at first char
arg2=1
else
arg2=tmp
end
newArr[1]=arg2 -- start position
end
else
do
raise syntax 93.905 array('2 ("start position")', arg2)
end
if arg(3,"Exists") then -- length
do
if datatype(arg2, "W") then
do
if arg3<0 then -- we need to move the starting point to the left!
do
arg2=arg2+arg3+1 -- subtract arg3
if arg2<1 then -- reset start to 1
newArr[1]=1
else -- new start pos
newArr[1]=arg2
arg3=-arg3 -- turn it into a positive number
end
end
else
do
raise syntax 93.905 array('3 ("length")', arg3)
end
newArr[2]=arg3 -- length
end
-- now invoke the operation
return .message~new(arg(1), methName, "A", newArr)~send
syntax:
raise propagate
-- 2008-03-14, rgf:
/* ======================================================================= */
/* delWord2(string, start[, length])
... if no words, returns received string
*/
::method "delWord2" -- allows negative start and length
use strict arg string, arg2, ... -- make sure we have at least one arg
parse arg string, arg2, arg3
argNr=arg() -- get maximum number of arguments
maxArgs=3
signal on syntax
if argNr>maxArgs then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArgs)
methName="delWord" -- base name for the message to send
newArr=.array~new -- create new array for the arguments
nrWords=words(string) -- calc # of words
if \datatype(arg2, "W") then
raise syntax 93.905 array('2 ("starting word position")', arg3)
newArr[1]=arg2 -- save starting pos
if arg2<0 then
do
tmp=nrWords+arg2+1 -- calc starting position from right
if tmp<1 then -- if before first word, start at first word
tmp=1
newArr[1]=tmp -- save new starting position
end
if arg(3,"Exists") then -- if given, process length argument
do
if \datatype(arg3, "W") then
raise syntax 93.905 array('3 ("number of words")', arg3)
if arg3<0 then -- determine new starting position and number of words to delete
do
oldStart=newArr[1] -- save old starting position
tmp=oldStart+arg3+1
if tmp<1 then -- oops, make sure we start at first word
tmp=1
newArr[1]=tmp -- new start position
newArr[2]=oldStart-tmp+1 -- length argument (nr of words to delete)
end
else
do
newArr[2]=arg3 -- length argument
end
end
if nrWords=0 then -- nothing to do, return empty/spacy string
return string
-- now invoke the operation
return .message~new(string, methName, "A", newArr)~send
syntax:
raise propagate
/* ======================================================================= */
-- not a BIF ::method "Equals2"
/* ======================================================================= */
/* lastPos needle, haystack [,[n-start] [,{C|I}]] */
::method "lastPos2"
use strict arg arg1needle, arg2haystack, ...
argNr=arg() -- get maximum number of arguments
BIFpos=3 -- last classic BIF argument position
maxArgs=4
signal on syntax
if argNr>maxArgs then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArgs)
StringPos=1 -- position of string object to work with
methName="lastPos" -- base name for the message to send
bCaseDependent =.false -- default to caseless version
if argNr>=BIFpos, \datatype(arg(argNr),"N") then
do
letter=arg(argNr)~strip~left(1)~upper
if pos(letter,"CI")=0 then -- illegal argument!
raise syntax 93.914 array (argNr, "C, I", arg(argNr))
bCaseDependent=(letter="C")
argNr-=1 -- decrease one from total number of arguments
end
newArr=.array~new -- create new array for the arguments
newArr[1]=arg1needle -- needle
arg3=arg(3)
if arg(3,"Exists"), datatype(arg3,"N") then
do
if arg3<0 then -- negative start column: count from right
do
len2=length(arg2haystack) -- get length of string to scan
if -arg3 >= len2 then -- beyond starting position, scan string normally
return 0 -- beyond start, needle cannot be found!
else
newArr[2]=len2+arg3+1 -- determine starting position
end
else -- positive start column
do
newArr[2]=arg3 -- save starting position
end
end
-- now invoke the operation
if bCaseDependent then
return .message~new(arg2haystack, methName, "A", newArr)~send
else
return .message~new(arg2haystack, "caseless"methName, "A", newArr)~send
syntax:
raise propagate
/* ======================================================================= */
-- not a BIF ::method "match2"
/* ======================================================================= */
/* left2 string, length [,pad] */
::method "left2"
use strict arg arg1string, arg2length, ...
argNr=arg() -- get maximum number of arguments
BIFpos=3 -- last classic BIF argument position
maxArgs=3
--signal on syntax
if argNr>maxArgs then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArgs)
if \datatype(arg2length,"W") then
do
raise syntax 93.905 array('"length"', arg2)
end
bLeftBIF=(arg2length>0) -- use left() or right() BIF ?
newArr=.array~new -- create new array for the arguments
if bLeftBIF then
newArr[1]=arg2length
else
newArr[1]=-arg2length
if arg(3,"Exists") then -- padChar supplied ?
newArr[2]=arg(3)
-- now invoke the operation
if bLeftBIF then
return .message~new(arg1string, "left", "A", newArr)~send
else
return .message~new(arg1string, "right", "A", newArr)~send
syntax:
raise propagate
-- 2008-02-21, rgf: lower2(string [,[n-start] [, n-length]])
/* if length is negative, then */
/* ======================================================================= */
::method "lower2"
use strict arg arg1, ... -- make sure we have at least one arg
parse arg ., arg2, arg3
argNr=arg() -- get maximum number of arguments
BIFpos=3 -- last classic BIF argument position
maxArgs=3
signal on syntax
if argNr>maxArgs then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArgs)
methName="lower" -- base name for the message to send
len1=length(arg1) -- get length of string
newArr=.array~new -- create new array for the arguments
if arg(2,"Exists") then -- start
do
if datatype(arg2, "W") then
do
if arg2<0 then -- negative, start from right
do
tmp=len1+arg2+1 -- get starting position
if tmp<2 then -- start at first char
arg2=1
else
arg2=tmp
end
end
else
do
raise syntax 93.905 array('2 ("start position")', arg2)
end
newArr[1]=arg2 -- start position
end
if arg(3,"Exists") then -- length
do
if datatype(arg3,"W") then
do
if arg3<0 then -- we need to move the starting point to the left!
do
arg2=arg2+arg3+1 -- subtract arg3
if arg2<1 then -- reset start to 1
newArr[1]=1
else -- new start pos
newArr[1]=arg2
arg3=-arg3 -- turn it into a positive number
end
end
else
do
raise syntax 93.905 array('3 ("length")', arg3)
end
newArr[2]=arg3 -- length
end
-- now invoke the operation
return .message~new(arg(1), methName, "A", newArr)~send
syntax:
raise propagate
/* ======================================================================= */
-- not a BIF ::method "match2"
/* ======================================================================= */
-- not a BIF ::method "matchChar2"
-- 2008-02-22, rgf: overlay2(new, target [,[n-target-start] [, n-new-length]] [,pad])
--> ATTENTION: if beyond start, prepend appropriate length pad-filled !
/* ======================================================================= */
::method "overlay2"
use strict arg new1string, arg1string, ... -- make sure we have at least two arg
parse arg ., ., arg2start, arg3NewLength, arg4pad
argNr=arg() -- get maximum number of arguments
BIFpos=5 -- last classic BIF argument position
maxArgs=5
signal on syntax
if argNr>maxArgs then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArgs)
methName="overlay" -- base name for the message to send
len1=length(arg1string) -- get length of string
newArr=.array~new -- create new array for the arguments
newArr[1]=new1string -- "new"-string
prepend="" -- optional prepend string (if positioning before start!)
arg2startori=arg2start -- save passed-in value, if any
if arg4pad=="" then arg4pad=" "-- define blank as the default pad char
if arg(3,"Exists") then -- start in "target"-string
do
if datatype(arg2start,"W") then
do
if arg2start<0 then -- negative, start from right
do
tmp=len1+arg2start+1 -- get starting position
if tmp<2 then -- start at first char
do
if tmp<0 then
prepend=copies(arg4pad, -tmp+1) -- create prepend-string
else if tmp=0 then -- fencepost
prepend=arg4pad
arg2start=1
end
else
arg2start=tmp
end
end
else
do
raise syntax 93.905 array('3 ("start position in ''target'' string")', arg2start)
end
newArr[2]=arg2start -- start position
end
if arg(4,"Exists") then -- "new"-length
do
if datatype(arg3NewLength,"W") then
do
if arg3NewLength<0 then -- we need to move the starting point to the left!
do
arg3NewLength=-arg3NewLength -- turn into a positive number
newArr[1]=right(new1string, arg3NewLength, arg4pad) -- "new"-string
end
else
newArr[1]=left(new1string, arg3NewLength, arg4pad) -- "new"-string
end
else
do
raise syntax 93.905 array('3 ("length of ''new''-string")', arg3NewLength)
end
newArr[3]=arg3NewLength -- length
end
if arg4pad<>"" then -- pad-char
newArr[4]=arg4pad
-- now invoke the operation
return .message~new(prepend||arg1string, methName, "A", newArr)~send
syntax:
raise propagate
/* ======================================================================= */
/* Pos needle, haystack [,[n-start] [,{C|I}]] */
::method "Pos2"
use strict arg arg1needle, arg2haystack, ...
argNr=arg() -- get maximum number of arguments
BIFpos=3 -- last classic BIF argument position
maxArgs=4
signal on syntax
if argNr>maxArgs then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArgs)
methName="Pos" -- base name for the message to send
bCaseDependent =.false -- default to caseless version
if argNr>=BIFpos, \datatype(arg(argNr),"W") then
do
letter=arg(argNr)~strip~left(1)~upper
if pos(letter,"CI")=0 then -- illegal argument!
raise syntax 93.914 array (argNr, "C, I", arg(argNr))
bCaseDependent=(letter="C")
argNr-=1 -- decrease one from total number of arguments
end
newArr=.array~new -- create new array for the arguments
newArr[1]=arg1needle -- needle
arg3=arg(3)
if arg(3,"Exists"), datatype(arg3,"N") then
do
if arg3<0 then -- negative start column: count from right
do
len2=length(arg2haystack) -- get length of string to scan
if -arg3 >= len2 then -- beyond starting position, scan string normally
return 0 -- beyond start, needle cannot be found!
else
newArr[2]=len2+arg3+1 -- determine starting position
end
else -- positive start column
do
newArr[2]=arg3 -- save starting position
end
end
-- now invoke the operation
if bCaseDependent then
return .message~new(arg2haystack, methName, "A", newArr)~send
else
return .message~new(arg2haystack, "caseless"methName, "A", newArr)~send
syntax:
raise propagate
/* ======================================================================= */
/* right2 string, length [,pad] */
::method "right2"
use strict arg arg1string, arg2length, ...
argNr=arg() -- get maximum number of arguments
BIFpos=3 -- last classic BIF argument position
maxArgs=3
--signal on syntax
if argNr>maxArgs then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArgs)
if \datatype(arg2length,"W") then
do
raise syntax 93.905 array('"length"', arg2)
end
bLeftBIF=(arg2length>0) -- use left() or right() BIF ?
newArr=.array~new -- create new array for the arguments
if bLeftBIF then
newArr[1]=arg2length
else
newArr[1]=-arg2length
if arg(3,"Exists") then -- padChar supplied ?
newArr[2]=arg(3)
-- now invoke the operation
if bLeftBIF then
return .message~new(arg1string, "right", "A", newArr)~send
else
return .message~new(arg1string, "left", "A", newArr)~send
syntax:
raise propagate
/* ======================================================================= */
/* "Front end" to .Arrays two sort methods "sort" and "sortWith" to simplify usage.
Sorts array in place, but also returns it.
*/
/*
usage: sort2(array [,A|D][,][C|I][N])
sort2(array, comparator [,A|D])
A|D ... Ascending (default) | Descending
C|I|N ... respect Case | Ignore case (default) | Numeric (Rexx-style numbers, default)
------------
sort2(array) ... sort()
sort2(array, comparator[,"A|D"]) ... sortWith(comparator)
sort2(array, collection) ... sortWith(.StringColumnComparator(...))
sort2(array, n, ...)
sort2(array, ["A|D"][,"C|I|N"]) ... sortWith(.StringComparator)
sort2(array, "[A[scending]|D[escending]][",C[aseDependent]|I[gnoreCase]|N[umeric]"])
sort2(array, "A[C|I|N] | D[C|I|N]"...)
Sort2(array, "M[essages]", message...) ... sortWith(.MessageComparator(...))
Sort2(array, "M[essages]", arrayOfMessages...)
*/
::method "sort2"
use strict arg arg1, arg2="A", arg3="IN", ...
signal on syntax
if \arg1~isA(.array) then
do
if \arg1~hasMethod("makeArray") then
raise syntax 93.948 array (1, "Array (or a class with a method 'MAKEARRAY')")
arg1=arg1~makeArray -- get the array that represents the collection
end
argNr=arg() -- get number of args
if argNr=1 then -- default sort as only array collection is given
do
if arg1[1]~isA(.string) then -- string objects to sort?
return sort2(arg1, "A", "IN") -- sort decimal numerically and caselessly
return arg1~sort -- sort without any assumptions
end
-- two arguments only, if strings to sort default to "IN" (case-independent, numeric)
if argNr=2, arg2~isA(.string) then -- if a descending sort
do
order=arg2~strip~left(1)~upper -- extract first letter in capital
if order="A" then -- sort ascendingly
do
if arg1[1]~isA(.string) then -- string objects to sort?
return sort2(arg1, "A", "IN") -- sort ignoring case, compare numbers as numbers
return arg1~sort
end
else if order="D" then -- sort descendingly
do
if arg1[1]~isA(.string) then -- string objects to sort?
return sort2(arg1, "D", "IN") -- sort ignoring case, compare numbers as numbers
return arg1~sortWith(.DescendingComparator~new)
end
end
if arg2~isA(.Comparator) then -- o.k. a comparator given, use it
do
if argNr>3 then -- in this case a maximum of three args allowed
raise syntax 93.902 array (3)
kind="A" -- default to ascending sort
if argNr=3 then -- a third argument given
do
kind=arg3~strip~left(1)~upper -- get first char in uppercase
if pos(kind, "AD")=0 then -- not a valid argument given!
raise syntax 93.914 array (3, "A, D", arg3)
end
if kind="A" then -- sort ascendingly
return arg1~sortWith(arg2)
else
return arg1~sortWith(.InvertingComparator~new(arg2))
end
if datatype(arg2,"W") | arg2~isA(.OrderedCollection) | arg2~isA(.Supplier) then
do
if arg2~isA(.Collection) | arg2~isA(.Supplier) then -- a collection indicating positions, lengths, type of sort
do
if argNr>2 then -- in this case only two arguments allowed!
raise syntax 93.902 array (2)
end
else -- argument is a number, hence interpreted as a starting column
do
arg2=arg(2,"Array") -- turn all args into an array collection
end
-- use a StringColumnComparator for sorting
return arg1~sortWith(.StringColumnComparator~new(arg2))
end
-- ---rgf, 2008-03-27: allow message(s) as arguments
if arg2~isA(.string) then -- check whether "M"essage argument given
do
if arg2~strip~left(1)~upper="M" then
do
if argNr=3 then -- single argument follows
comparator=.MessageComparator~new(arg3)
else -- turn remaining args into an array object
comparator=.MessageComparator~new(arg(3,"Array"))
return arg1~sortWith(comparator)
end
end
-- o.k. now use ".StringComparator" for sorting ("CIN")
if argNr>3 then -- in this case only three args allowed at most
raise syntax 93.902 array (3)
if argNr=2 then -- let .StringComparator deal with the args
return arg1~sortWith(.StringComparator~new(arg2))
else
return arg1~sortWith(.StringComparator~new(arg2, arg3))
syntax: raise propagate
/*
usage: stableSort2(array [,A|D][,][C|I|N])
stableSort2(array, comparator [,A|D])
A|D ... Ascending (default) | Descending
C|I|N ... respect Case | Ignore case (default) | Numeric (Rexx-style numbers)
------------
stableSort2(array) ... sort()
stableSort2(array, comparator[,"A|D"]) ... sortWith(comparator)
stableSort2(array, collection) ... sortWith(.StringColumnComparator(...))
stableSort2(array, n, ...)
stableSort2(array, ["A|D"][,"C|I|N"]) ... sortWith(.StringComparator)
stableSort2(array, "[A[scending]|D[escending]][",C[aseDependent]|I[gnoreCase]|N[umeric]"])
stableSort2(array, "A[C|I|N] | D[C|I|N]"...)
stableSort2(array, "M[essages]", message...) ... sortWith(.MessageComparator(...))
stableSort2(array, "M[essages]", arrayOfMessages...)
*/
::method "stableSort2"
use strict arg arg1, arg2="A", arg3="I", ...
signal on syntax
if \arg1~isA(.array) then
do
if \arg1~hasMethod("makeArray") then
raise syntax 93.948 array (1, "Array (or a class with a method 'MAKEARRAY')")
arg1=arg1~makeArray -- get the array that represents the collection
end
argNr=arg() -- get number of args
if argNr=1 then -- default sort as only array collection is given
do
if arg1[1]~isA(.string) then -- string objects to sort?
return stableSort2(arg1, "A", "N") -- sort decimal numerically and caselessly
return arg1~stableSort -- sort without any assumptions
end
-- two arguments only, if strings to sort default to "IN" (case-independent, numeric)
if argNr=2, arg2~isA(.string) then -- if a descending sort
do
order=arg2~strip~left(1)~upper -- extract first letter in capital
if order="A" then -- sort ascendingly
do
if arg1[1]~isA(.string) then -- string objects to sort?
return stableSort2(arg1, "A", "IN") -- sort ignoring case, compare numbers as numbers
return arg1~stableSort
end
else if order="D" then -- sort descendingly
do
if arg1[1]~isA(.string) then -- string objects to sort?
return stableSort2(arg1, "D", "IN") -- sort ignoring case, compare numbers as numbers
return arg1~stableSortWith(.DescendingComparator~new)
end
end
if arg2~isA(.Comparator) then -- o.k. a comparator given, use it
do
if argNr>3 then -- in this case a maximum of three args allowed
raise syntax 93.902 array (3)
kind="A" -- default to ascending sort
if argNr=3 then -- a third argument given
do
kind=arg3~strip~left(1)~upper -- get first char in uppercase
if pos(kind, "AD")=0 then -- not a valid argument given!
raise syntax 93.914 array (3, "A, D", arg3)
end
if kind="A" then -- sort ascendingly
return arg1~stableSortWith(arg2)
else
return arg1~stableSortWith(.InvertingComparator~new(arg2))
end
if datatype(arg2,"W") | arg2~isA(.OrderedCollection) | arg2~isA(.Supplier) then
do
if arg2~isA(.Collection) | arg2~isA(.Supplier) then -- a collection indicating positions, lengths, type of sort
do
if argNr>2 then -- in this case only two arguments allowed!
raise syntax 93.902 array (2)
end
else -- argument is a number, hence interpreted as a starting column
do
arg2=arg(2,"Array") -- turn all args into an array collection
end
-- use a StringColumnComparator for sorting
return arg1~stableSortWith(.StringColumnComparator~new(arg2))
end
-- ---rgf, 2008-03-27: allow message(s) as arguments
if arg2~isA(.string) then -- check whether "M"essage argument given
do
if arg2~strip~left(1)~upper="M" then
do
if argNr=3 then -- single argument follows
comparator=.MessageComparator~new(arg3)
else -- turn remaining args into an array object
comparator=.MessageComparator~new(arg(3,"Array"))
return arg1~stableSortWith(comparator)
end
end
-- o.k. now use ".StringComparator" for sorting ("CIN")
if argNr>3 then -- in this case only three args allowed at most
raise syntax 93.902 array (3)
if argNr=2 then -- let .StringComparator deal with the args
return arg1~stableSortWith(.StringComparator~new(arg2))
else
return arg1~stableSortWith(.StringComparator~new(arg2, arg3))
syntax: raise propagate
-- 2008-03-14, rgf: subChar2(string,n-pos)
/* if length is negative, then position from right (end of string) */
--> ATTENTION: if beyond start, prepend appropriate length pad-filled !
/* ======================================================================= */
::method "subchar2"
use strict arg arg1, arg2 -- make sure we have at least one arg
parse arg arg1, arg2
argNr=arg() -- get maximum number of arguments
maxArgs=2
signal on syntax
if argNr<>maxArgs then -- not correct amount of arguments ?
do
if argNr<maxArgs then
raise syntax 93.901 array (2)
else
raise syntax 93.902 array (2)
end
len1=length(arg1) -- get length of string
if datatype(arg2,"W") then
do
if arg2<0 then -- negative, start from right
do
arg2=len1+arg2+1 -- calc starting position
if arg2<1 then -- beyond string, return empty string (i.e. no char)
return ""
end
if arg2=0 then
raise syntax 93.924 array (arg2)
else if arg2>len1 then-- beyond string, return empty string (i.e. no char)
return ""
end
else
do
raise syntax 93.905 array('2 ("start position")', arg2)
end
-- now invoke the operation
return arg1~substr(arg2,1) -- return extracted char
syntax:
raise propagate
-- 2008-02-21, rgf: substr2(string [,[n-start] [, n-length]] [,pad])
/* if length is negative, then */
--> ATTENTION: if beyond start, prepend appropriate length pad-filled !
/* ======================================================================= */
::method "substr2"
use strict arg arg1, ... -- make sure we have at least one arg
parse arg ., arg2, arg3, arg4
argNr=arg() -- get maximum number of arguments
BIFpos=4 -- last classic BIF argument position
maxArgs=4
signal on syntax
if argNr>maxArgs then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArgs)
methName="substr" -- base name for the message to send
len1=length(arg1) -- get length of string
newArr=.array~new -- create new array for the arguments
prepend="" -- optional prepend string (if positioning before start!)
arg2ori=arg2 -- save passed-in value, if any
if arg4=="" then arg4=" "-- define blank as the default pad char
if arg(2,"Exists") then -- start
do
if datatype(arg2,"W") then
do
if arg2<0 then -- negative, start from right
do
tmp=len1+arg2+1 -- get starting position
if tmp<2 then -- start at first char
do
if tmp<0 then
prepend=copies(arg4, -tmp+1) -- create prepend-string
else if tmp=0 then -- fencepost
prepend=arg4
arg2=1
end
else
arg2=tmp
end
end
else
do
raise syntax 93.905 array('2 ("start position")', arg2)
end
newArr[1]=arg2 -- start position
end
if arg(3,"Exists") then -- length
do
if datatype(arg3,"W") then
do
if arg3<0 then -- we need to move the starting point to the left!
do
tmp =arg2+arg3 -- subtract arg3
if tmp <1 then -- reset start to 1
do
newArr[1]=1 -- substring from new pos "1"
if tmp <0 then -- create (new?) prepend string
prepend=prepend||copies(arg4, -tmp) -- create prepend-string
end
else -- new start pos
newArr[1]=tmp+1
arg3=-arg3 -- turn it into a positive number
end
end
else
do
raise syntax 93.905 array('3 ("length")', arg3)
end
newArr[2]=arg3 -- length
end
if arg4<>"" then -- pad-char
newArr[3]=arg4
-- now invoke the operation
return .message~new(prepend||arg1, methName, "A", newArr)~send
syntax:
raise propagate
pp:
if arg(1)=.nil then return ""
else return "," arg(1)
-- 2008-03-14, rgf:
/* ======================================================================= */
/* subWord2(string, start[, length])
... if no words, returns received string
*/
::method "subWord2" -- allows negative start and length
use strict arg string, arg2, ... -- make sure we have at least two args
parse arg string, arg2, arg3
argNr=arg() -- get maximum number of arguments
maxArgs=3
signal on syntax
if argNr>maxArgs then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArgs)
methName="subWord" -- base name for the message to send
newArr=.array~new -- create new array for the arguments
nrWords=words(string) -- calc # of words
if \datatype(arg2, "W") then
raise syntax 93.905 array('2 ("starting word position")', arg3)
newArr[1]=arg2 -- save starting pos
if arg2<0 then
do
tmp=nrWords+arg2+1 -- calc starting position from right
if tmp<1 then -- if before first word, start at first word
tmp=1
newArr[1]=tmp -- save new starting position
end
if arg(3,"Exists") then -- if given, process length argument
do
if \datatype(arg3, "W") then
raise syntax 93.905 array('3 ("number of words")', arg3)
if arg3<0 then -- determine new starting position and number of words to delete
do
oldStart=newArr[1] -- save old starting position
tmp=oldStart+arg3+1
if tmp<1 then -- oops, make sure we start at first word
tmp=1
newArr[1]=tmp -- new start position
newArr[2]=oldStart-tmp+1 -- length argument (nr of words to delete)
end
else
do
newArr[2]=arg3 -- length argument
end
end
if nrWords=0 then -- nothing to do, return empty/spacy string
return string
-- now invoke the operation
return .message~new(string, methName, "A", newArr)~send
syntax:
raise propagate
-- 2008-02-21, rgf: upper2(string [,[n-start] [, n-length]])
/* if length is negative, then */
/* ======================================================================= */
::method "upper2"
use strict arg arg1, ... -- make sure we have at least one arg
parse arg ., arg2, arg3
argNr=arg() -- get maximum number of arguments
BIFpos=3 -- last classic BIF argument position
maxArgs=3
signal on syntax
if argNr>maxArgs then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArgs)
methName="upper" -- base name for the message to send
len1=length(arg1) -- get length of string
newArr=.array~new -- create new array for the arguments
if arg(2,"Exists") then -- start
do
if datatype(arg2,"W") then
do
if arg2<0 then -- negative, start from right
do
tmp=len1+arg2+1 -- get starting position
if tmp<2 then -- start at first char
arg2=1
else
arg2=tmp
end
end
else
do
raise syntax 93.905 array('2 ("start position")', arg2)
end
newArr[1]=arg2 -- start position
end
if arg(3,"Exists") then -- length
do
if datatype(arg3,"W") then
do
if arg3<0 then -- we need to move the starting point to the left!
do
arg2=arg2+arg3+1 -- subtract arg3
if arg2<1 then -- reset start to 1
newArr[1]=1
else -- new start pos
newArr[1]=arg2
arg3=-arg3 -- turn it into a positive number
end
end
else
do
raise syntax 93.905 array('3 ("length")', arg3)
end
newArr[2]=arg3 -- length
end
-- now invoke the operation
return .message~new(arg(1), methName, "A", newArr)~send
syntax:
raise propagate
-- 2008-03-14, rgf:
/* ======================================================================= */
/* WORD2(string, pos)
... if beyond string, then return empty string
*/
::method "word2" -- extract and return word
use strict arg string, arg2 -- make sure we have at least one arg
parse arg string, arg2
argNr=arg() -- get maximum number of arguments
maxArgs=2
methName="word" -- base name for the message to send
newArr=.array~new -- create new array for the arguments
signal on syntax
if argNr>maxArgs then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArgs)
if \datatype(arg2, "W") then
raise syntax 93.905 array("2 (position)", arg2) -- must be a number
nrWords=words(string) -- get total number of words
newArr[1]=arg2 -- save position
if arg2<0 then -- negative, position from right
do
tmp=nrWords+arg2+1 -- calc new position
if tmp<1 then -- beyond string, return empty string
return ""
newArr[1]=tmp -- save new position
end
-- now invoke the operation
return .message~new(string, methName, "A", newArr)~send
syntax:
raise propagate
-- 2008-03-14, rgf:
/* ======================================================================= */
/* WORDINDEX2(string, pos)
... if beyond string, then return 0
*/
::method "wordIndex2"
use strict arg string, arg2 -- make sure we have at least one arg
parse arg string, arg2
argNr=arg() -- get maximum number of arguments
maxArgs=2
methName="wordIndex" -- base name for the message to send
newArr=.array~new -- create new array for the arguments
signal on syntax
if argNr>maxArgs then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArgs)
if \datatype(arg2, "W") then
raise syntax 93.905 array("2 (position)", arg2) -- must be a number
nrWords=words(string) -- get total number of words
newArr[1]=arg2 -- save position
if arg2<0 then -- negative, position from right
do
tmp=nrWords+arg2+1 -- calc new position
if tmp<1 then -- beyond string, return empty string
return 0
newArr[1]=tmp -- save new position
end
-- now invoke the operation
return .message~new(string, methName, "A", newArr)~send
syntax:
raise propagate
-- 2008-03-14, rgf:
/* ======================================================================= */
/* WORDLENGTH2(string, position)
... if beyond string, then return 0
*/
::method "wordLength2"
use strict arg string, arg2 -- make sure we have at least one arg
parse arg string, arg2
argNr=arg() -- get maximum number of arguments
maxArgs=2
methName="wordLength" -- base name for the message to send
newArr=.array~new -- create new array for the arguments
signal on syntax
if argNr>maxArgs then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArgs)
if \datatype(arg2, "W") then
raise syntax 93.905 array("2 (position)", arg2) -- must be a number
nrWords=words(string) -- get total number of words
newArr[1]=arg2 -- save position
if arg2<0 then -- negative, position from right
do
tmp=nrWords+arg2+1 -- calc new position
if tmp<1 then -- beyond string, return empty string
return 0
newArr[1]=tmp -- save new position
end
-- now invoke the operation
return .message~new(string, methName, "A", newArr)~send
syntax:
raise propagate
/* ======================================================================= */
/*
WORDPOS2(phrase,string[,start][,{C|I}])
*/
::method "wordPos2"
use strict arg arg1, arg2, arg3=1, ...
argNr=arg() -- get maximum number of arguments
BIFpos=3 -- last classic BIF argument position
maxArgs=4
newArr=.array~new -- create new array for the arguments
newArr[1]=arg1 -- save phrase (single or multiple word/s) to search
signal on syntax
if argNr>maxArgs then -- too many arguments ?
raise syntax 93.902 array ("at most" maxArgs)
methName="wordPos" -- base name for the message to send
bCaseDependent =.false -- default to caseless version
if argNr>=BIFpos, \datatype(arg(argNr),"W") then
do
letter=arg(argNr)~strip~left(1)~upper
if pos(letter,"CI")=0 then -- illegal argument!
raise syntax 93.914 array (argNr, "C, I", arg(argNr))
bCaseDependent=(letter="C")
argNr-=1 -- decrease one from total number of arguments
end
if arg(3,"Exists"), datatype(arg3,"W") then
do
if arg3<0 then
do
tmp=words(arg2)+arg3+1 -- calc starting position from the right
if tmp<1 then -- beyond string, then start with first word!
tmp=1
newArr[2]=tmp
end
else
do
newArr[2]=arg3
end
end
-- now invoke the operation
if bCaseDependent then
return .message~new(arg2, methName, "A", newArr)~send
else
return .message~new(arg2, "caseless"methName, "A", newArr)~send
syntax:
raise propagate
/* ---rgf, 2008-02-26
parseWords2(string[, reference=char-string[, kind="D"|"W"]] [, returnType="W"ords|"P"os)
... returns a one-dimensional array of words parsed from "string" or
a two-dimensional array of starting position and length of word
string ... string from which words should be parsed
reference ... string of characters that delimit words or constitute words;
default value: " " ||2"09"x
"d"elimiterChars|"W"ordChars ... "char-string" consists of all those characters
that either "D"elimit (default) or constitute "W"ords
"W"ords|"P"os "W"ords (default) returns a single dimensional array of
the parsed words; "P"os returns a two-dimensional array of
positions of start-position (index "1") and length of parsed
word (index "2")
*/
::method "parseWords2"
use strict arg string, reference=(" "||"09"x), kind="D", returnType="W"
signal on syntax
.ArgUtil~validateClass("string", string, .string) -- check for correct type
.ArgUtil~validateClass("reference", reference, .string) -- check for correct type
.ArgUtil~validateClass("kind", kind , .string) -- check for correct type
if kind<>"D" then -- not a default value
do
tmp=kind~strip~left(1)~upper
if pos(tmp, "DW")=0 then
raise syntax 93.914 array("'kind'", "D[elimiter] | W[ord-characters]", kind)
kind=tmp
end
.ArgUtil~validateClass("returnType", returnType, .string) -- check for correct type
if returnType<>"W" then -- not a default value
do
tmp=returnType~strip~left(1)~upper
if pos(tmp, "WP")=0 then
raise syntax 93.914 array("'returnType'", "W[ords] | P[ositions]", returnType)
returnType=tmp
end
if returnType="W" then -- single-dimensioned array of words
res=.array~new
else -- two-dimensional array of position and length
res=.array~new(0,0)
maxLen=length(string)
pos=1
endPos=0
do i=1 while endpos<maxLen -- for 30
if kind="D" then -- words are space-delimited according to "reference"
do
if i=1 then -- first iteration?
pos =verify(string, reference, "Nomatch") -- find beginning of word
else
pos =verify(string, reference, "Nomatch", endPos) -- find beginning of word
if pos=0 then -- no more words to find
leave
endPos=verify(string, reference, "Match", pos) -- find next space (end of word)
if endPos=0 then -- last word, string ends with it
endPos=maxLen+1
end
else -- "reference" defines the characters a word consists of
do
if i=1 then -- first iteration?
pos =verify(string, reference, "Match") -- find beginning of word
else
do
pos =verify(string, reference, "Match", endPos) -- find beginning of word
end
if pos=0 then -- no more words to find
leave
endPos=verify(string, reference, "Nomatch", pos) -- find next space (end of word)
if endPos=0 then -- last word, string ends with it
endPos=maxLen+1
end
if returnType="W" then
do
res[i]=string~substr(pos,endPos-pos) -- extract and save word
end
else
do
res[i,1]=pos -- save starting position
res[i,2]=endPos-pos -- save length of word
res[i,3]=endPos -- save starting position of next word, if any
end
end
return res -- return result array
syntax: -- propagate condition
raise propagate
/* ======================================================================= */
/* Dump collection or supplier. */
/*
dumpArray2(--coll--[,-title-]-)
coll ... collection or supplier object to dump in sorted order
title ... optional, title to be displayed
comparator ... the comparator to use in sorting
*/
::routine dump2 public
use arg coll, title=("type: The" coll~class~id "class"), comparator=.nil
if .nil=comparator, title~isA(.comparator) then
do
comparator=title
title=("type: The" coll~class~id "class")
end
if coll~isA(.supplier) then
do
s=coll
len=5 -- define an arbitrary high width
say title
end
else if \coll~isA(.Collection) then -- make sure we have a Collection else
do
if arg(2,"E") then -- title omitted !
say title
say "DUMP2(): ---> argument to dump is *NOT* a *COLLECTION/SUPPLIER* ! <--- "
say " type:" pp2(coll~class)
say " default string value:" pp2(coll)
-- .ArgUtil~validateClass("collection", coll, .Collection) -- must be of type Collection
return
end
else -- a collection in hand
do
say title": ("coll~items "items)"
len=length(coll~items)
end
say
count=0
if coll~isA(.Collection) then
do
s=makeSortedSupplier(coll, comparator)
end
-- determine maximum length of "pretty printed" index-value
maxWidth=0
s2=s~copy
do while s2~available
maxWidth=max(maxWidth,length(ppIndex2(s2~index)))
s2~next
end
count=0
do while s~available
count=count+1
say " " "#" right(count,len)":" "index="ppIndex2(s~index)~left(maxWidth) "-> item="pp2(s~item)
s~next
end
say "-"~copies(50)
return
/* Sort a collection considering its type and return a sorted supplier object. */
makeSortedSupplier: procedure
use arg coll, comparator=.nil
if coll~isA(.OrderedCollection) then -- don't sort, just return the supplier
return coll~supplier
if coll~isA(.SetCollection) then -- use items part, sort it and return it as a supplier
do
arr=coll~allItems -- get array representation
call sortArray arr, comparator -- sort elements
return .supplier~new(arr, arr) -- return supplier with sorted elements
end
if coll~hasMethod('allAt') then -- handle collections with idx -> coll
do
arr=.set~new~union(coll~allIndexes)~makeArray -- remove duplicate indexes, if any
call sortArray arr, comparator -- sort elements
arr2=.array~new
do i=1 to arr~items -- iterate over all indexes
tmp=coll~allAt(arr[i]) -- get all items associated with index
if tmp~items=1 then
arr2[i]=tmp~at(1) -- save single item to show
else
arr2[i]=coll~allAt(arr[i]) -- save collection of associated items
end
return .supplier~new(arr2, arr)
end
-- o.k. only MapCollection/Collection left, assuming 1:1 mapping between index and item
arr=coll~allIndexes -- remove duplicate indexes, if any
call sortArray arr, comparator -- sort elements
arr2=.array~new
do i=1 to arr~items -- iterate over all indexes
arr2[i]=coll[arr[i]] -- retrieve item part
end
return .supplier~new(arr2, arr)
-- just sort the passed in array, depending on whether a comparator is needed or not
sortArray: procedure
use arg arr, comparator=.nil
if comparator=.nil, \arr[1]~hasMethod('compareTo') then -- no comparator available, use string renderings
comparator=.MessageComparator~new("string", .true)
if comparator<>.nil then
arr~stableSortWith(comparator)
else
arr~stableSort
return
/* ======================================================================= */
/* This comparator expects a message name or a message object to send to
both objects. If a message name is given, the appropriate message object
will get created and used. The result of sending the message will then
be used to carry out the actual comparison.
The second argument is optional (default value: .false), and if supplied
must be a logical value. If .true, then the result values from sending
the message will be cached in a table.
.MessageComparator~new(-message-[,-bCache-])
message ... message name or message object; this will get sent to each
object and its result will be used for comparison
bCache ... optional (default: .false), if .true, then the result of
each message will be stored in a table; if an object is
contained more than once in the collection, then sending
a message to it will return the cached result of the previous
execution; this should help performance in situations where
each execution of the message is very time consuming
20071020 - idea: allow array of message-arrays (each entry is an
array with a msgName/msgObject, and optionally "I|A" and
arguments for that particular msg)
20071230 - better idea: ordered collection of message names or
message objects; if one message only, create own "compare"-
methods for it
20080103 - added in multiple message mode the option to attach "/numeric"
to a message name (=string), if values should be sorted as
numbers; done
20080324 - idea: allow "/[a[scending]|d[escending]][n[umeric]|[c[ase]|i[gnore]],
then apply respective comparators
*/
::class "MessageComparator" mixinclass Comparator
::method init
expose message cacheTable messages messageArray numericComparator caselessComparator asc
use strict arg message, bCache=.false
signal on syntax
if \datatype(bCache,"O") then
raise syntax 34.900 array ("Method argument 2 ('cache') must be a logical value, received:" bCache)
bSingleMessage=\(message~isA(.collection)) -- determine whether we received a collection
if \bSingleMessage, bCache=.true then
raise syntax 88.900 array ("Using multiple messages for comparisons, caching not allowed! Argument 'cache' must be omitted or set to '.false'.")
emptyString="" -- define empty string
if bSingleMessage then
do
-- set var "asc" (A[sc]/D[esc]), "kind" (N|I|C)
asc="A" -- default to A[scending] sort
kind=""
bNumericMessage=.false -- indicates whether message result should be compared as a number
if message~isA(.string) then -- name of a message, create message object
do
parse caseless var message message "/" +1 flags
-- set var "asc" (A[sc]/D[esc]), "kind" (N|I|C)
if flags<>"" then
parse value determineSortingKind(flags) with asc kind
message=.message~new(.nil, message~strip) -- make sure to strip leading & trailing space
end
else if \message~isA(.message) then
do
raise syntax 93.900 array ("Method argument 1 must be either a message name (a string) or a message object, found:" message)
end
if bCache then
cacheTable=.table~new -- create table to use for cache
if kind="N" then -- numeric sort
do
numericComparator=.NumberComparator~new -- create the numeric comparator
if bCache then
self~setMethod("compare", self~instanceMethod("cached_plain_numeric_compare"), "Object")
else
self~setMethod("compare", self~instanceMethod("plain_numeric_compare"), "Object")
end
else if kind="I" then -- case independent sort
do
caselessComparator=.CaselessComparator~new -- create the caseless comparator
if bCache then
self~setMethod("compare", self~instanceMethod("cached_plain_caseless_compare"), "Object")
else
self~setMethod("compare", self~instanceMethod("plain_caseless_compare"), "Object")
end
else -- plain sort
do
if bCache then
self~setMethod("compare", self~instanceMethod("cached_plain_compare"), "Object")
else
self~setMethod("compare", self~instanceMethod("plain_compare"), "Object")
end
end
else -- collection of messages!
do
-- create the comparator objects
numericComparator =.NumberComparator~new
caselessComparator=.CaselessComparator~new
messages=.array~new -- use a list to keep all message objects
-- three dimensions: 1=messageObject, 2=kind (I|N|C|""), 3=ascending (A|D)
messageArray =.array~new(message~items,3)
i=0
do msg over message -- iterate over received collection
i+=1
asc="A" -- default to A[scending] sort
kind="" -- no kind given, regular comparison
if msg~isA(.array) then -- [1]=msg (a string or message), [2]=flagString [, [3]=flagString2]
do
flags=msg[2] -- get flags
if \flags~isA(.string) then
raise syntax 93.900 array ("Message item #" i": array object must have a string value ('flags') at index '2'.")
if msg~hasindex(3), msg[3]~isA(.String) then -- maybe index #3 has flag information also?
flags=flags msg[3] -- in this case [2]="A|D", [3]="C|I|N"
if \(msg[1]~isA(.String) | msg[1]~isA(.Message)) then
raise syntax 93.900 array ("Message item #" i": array object must have a string value ('methodName') or message object ('method') at index '1'.")
parse value determineSortingKind(flags) with asc kind -- process flags
msg=msg[1] -- now assign first element
end
if msg~isA(.string) then -- name of a message, create message object
do
-- check whether message contains a "/" which indicates flags coming up
parse var msg msg "/" +1 flags
-- set var "asc" (A[sc]/D[esc]), "kind" (N|I|C)
if flags<>"" then
parse value determineSortingKind(flags) with asc kind
messageArray[i,1]=.message~new(.nil, msg~strip) -- save message object
messageArray[i,2]=kind -- save "kind" (N|I|C|"")
if asc="D" then messageArray[i,3]="D" -- descending sort
else messageArray[i,3]="A" -- ascending sort
end
else if msg~isA(.message) then -- a message object in hand
do
messageArray[i,1]=msg -- save message object
messageArray[i,2]=kind -- kind: regular comparison
messageArray[i,3]=asc -- ascending sort
end
else -- neither string nor message object !
raise syntax 93.900 array ("Item #" i "of the supplied collection must be either a message name (a string) or a message object!")
end
self~setMethod("compare", self~instanceMethod("multiple_messages_compare"), "Object")
end
return
/* Analyze flags return blank delimited string:
A|D [N|I|C]
*/
determineSortingKind: procedure
parse arg flags
signal on syntax
if words(flags)=1 then -- could be a concatenation of "nd", "ac", "di", etc.
do
flags=flags~strip~left(2)~translate
if pos(flags~subchar(2), "NADCI")=0 then -- second char is not an option, remove it
flags=flags~left(1)
end
else
do
tmpStr=""
do i=1 to words(flags) -- get first character of word
tmpStr=tmpStr || word(flags,i)~left(1)
end
flags=tmpStr~upper -- into uppercase
end
pos=verify(flags,"NADCI", "N") -- any non-matching chars?
if pos>1 then
raise syntax 93.914 array("'/flags'", "[C[aseDependent] | I[gnoreCase] | N[umeric]] [A[asc] | D[esc]]]", msg)
res=""
if pos("D", flags)>0 then
do
res="D" -- descending
if pos("A", flags)>0 then
raise syntax 93.300 array("Contradictionary flags: only one of the flags 'A'[scending] and 'D'[escending] must be given.")
end
else -- default value, if neither "D" nor "A" is given
res="A" -- ascending
if pos("N", flags)>0 then
do
res=res "N" -- numeric/number
if verify(flags, "CI", "M")>0 then
raise syntax 93.300 array("Contradictionary flags: only one of the flags 'C'[aseDependent], 'I'[gnoreCase] and 'N'[umeric] must be given.")
end
else if pos("I", flags)>0 then
do
res=res "I" -- ignore case
if verify(flags, "CN", "M")>0 then
raise syntax 93.300 array("Contradictionary flags: only one of the flags 'C'[aseDependent], 'I'[gnoreCase] and 'N'[umeric] must be given.")
end
else if pos("C", flags)>0 then
do
res=res "C" -- respect case
if verify(flags, "IN", "M")>0 then
raise syntax 93.300 array("Contradictionary flags: only one of the flags 'C'[aseDependent], 'I'[gnoreCase] and 'N'[umeric] must be given.")
end
return res
syntax: -- propagate syntax exception, if any
raise propagate
/* this version caches the result of the messages sent, and therefore
can reuse previous message results directly */
::method cached_plain_compare
expose message cacheTable asc
use strict arg left, right
if \cacheTable~hasindex(left) then -- not cached yet?
cacheTable[left]=message~copy~send(left) -- get value
if \cacheTable~hasindex(right) then -- not cached yet?
cacheTable[right]=message~copy~send(right) -- get value
if asc="A" then -- ascending
return cacheTable[left]~compareTo(cacheTable[right])
else -- descending
return cacheTable[right]~compareTo(cacheTable[left])
::method cached_plain_numeric_compare
expose message cacheTable numericComparator asc
use strict arg left, right
if \cacheTable~hasindex(left) then -- not cached yet?
cacheTable[left]=message~copy~send(left) -- get value
if \cacheTable~hasindex(right) then -- not cached yet?
cacheTable[right]=message~copy~send(right) -- get value
if asc="A" then -- ascending
return numericComparator~compare(cacheTable[left], cacheTable[right])
else -- descending
return numericComparator~compare(cacheTable[right], cacheTable[left])
::method cached_plain_caseless_compare
expose message cacheTable caselessComparator asc
use strict arg left, right
if \cacheTable~hasindex(left) then -- not cached yet?
cacheTable[left]=message~copy~send(left) -- get value
if \cacheTable~hasindex(right) then -- not cached yet?
cacheTable[right]=message~copy~send(right) -- get value
if asc="A" then -- ascending
return caselessComparator~compare(cacheTable[left], cacheTable[right])
else -- descending
return caselessComparator~compare(cacheTable[right], cacheTable[left])
::method plain_compare
expose message asc
use strict arg left, right
if asc="A" then -- ascending
return message~copy~send(left)~compareTo(message~copy~send(right))
else -- descending
return message~copy~send(right)~compareTo(message~copy~send(left))
::method plain_numeric_compare
expose message numericComparator asc
use strict arg left, right
if asc="A" then -- ascending
return numericComparator~compare(message~copy~send(left), message~copy~send(right))
else -- descending
return numericComparator~compare(message~copy~send(right), message~copy~send(left))
::method plain_caseless_compare
expose message caselessComparator asc
use strict arg left, right
if asc="A" then -- ascending
return caselessComparator~compare(message~copy~send(left), message~copy~send(right))
else -- descending
return caselessComparator~compare(message~copy~send(right), message~copy~send(left))
::method multiple_messages_compare
expose messageArray numericComparator caselessComparator
use strict arg left, right
do i=1 to messageArray~dimension(1) -- process all messages until a comparison yields unequal
msg=messageArray[i,1] -- get message object
if messageArray[i,2]="" then -- regular comparison
do
if messageArray[i,3]="A" then -- Ascending sort
res=msg~copy~send(left)~compareTo(msg~copy~send(right))
else
res=msg~copy~send(right)~compareTo(msg~copy~send(left))
end
else if messageArray[i,2]="I" then -- case independent comparison!
do
if messageArray[i,3]="A" then -- Ascending sort
res=caselessComparator~compare(msg~copy~send(left), msg~copy~send(right))
else
res=caselessComparator~compare(msg~copy~send(right), msg~copy~send(left))
end
else if messageArray[i,2]="N" then -- numeric comparison!
do
if messageArray[i,3]="A" then -- Ascending sort
res=numericComparator~compare(msg~copy~send(left), msg~copy~send(right))
else
res=numericComparator~compare(msg~copy~send(right), msg~copy~send(left))
end
else -- standard comparison
do
if messageArray[i,3]="A" then -- Ascending sort
res=msg~copy~send(left)~compareTo(msg~copy~send(right))
else
res=msg~copy~send(right)~compareTo(msg~copy~send(left))
end
if res<>0 then
return res
end
return 0 -- default return value
/* ======================================================================= */
/* Compares Rexx (string) numbers. If the instance is created with an (optional)
argument of .true, then numbers are compared as numbers, but if one or both
arguments are not numbers, then the normal string "compareTo" will be employed.
Comparison of numbers is carried out under NUMERIC DIGITS 40, which allows
comparing numbers in the 2**128 range
Restriction: this class is used by .StringComparator and uses StringComparator objects as well
*/
::class "NumberComparator" mixinclass Comparator
::method init
expose stringComparator order
use arg bIgnoreNonNumbers=.true, order="A", case="I"
if bIgnoreNonNumbers=.false then
return -- just use the plain number comparisons (default: method "compare", already defined)
if \datatype(bIgnoreNonNumbers, "O") then -- not boolean/lOgical !
raise syntax 34.901 array (bIgnoreNumbers)
order=order~strip~left(1)~translate
if pos(order, "AD")=0 then
raise syntax 93.914 array ("# 2 (order)", "A, D", arg(2))
case=case~strip~left(1)~translate
if pos(case, "CI")=0 then
raise syntax 93.914 array ("# 3 (comparison type)", "C, I", arg(3))
-- replace default "compare" method with the desired relaxed one
if order="A" then
self~setMethod("compare", self~instanceMethod("compareWithNonNumbers"), "OBJECT")
else
self~setMethod("compare", self~instanceMethod("compareWithNonNumbersDescending"), "OBJECT")
-- now create and set comparator to use for non-numbers
stringComparator=.stringComparator~new(order, case) -- get string comparator to use
stringComparator=.StringComparator~new(order, case)
-- number only version, if non-number let runtime raise the syntax error
::method compare
expose order
use strict arg left, right
numeric digits 40 -- allow to deal with numbers up to 2**128
if order="A" then return (left-right)~sign -- returns -1 (left<right), +1 (left>right), 0 (left=right)
else return -((left-right)~sign) -- descending: invert comparison results
::method stringComparator -- getter
expose stringComparator
return stringComparator
::method "stringComparator=" -- setter
expose stringComparator
use arg stringComparator
::method compareWithNonNumbers -- used by StringComparator as well !
expose stringComparator -- stringComparator to use, if left and/or right are not numbers
use strict arg left, right
if datatype(left, "n"), datatype(right, "n") then
do
numeric digits 40 -- allow to deal with numbers up to 2**128
return (left-right)~sign -- returns -1 (left<right), +1 (left>right), 0 (left=right)
end
return stringComparator~compare(left,right) -- rgf, 20090520
::method compareWithNonNumbersDescending -- used by StringComparator as well !
expose stringComparator -- stringComparator to use, if left and/or right are not numbers
use strict arg left, right
-- if var("stringComparator")=.false then stringComparator=.StringComparator~new("D", "I") -- 20090520, rgf
if datatype(left, "n"), datatype(right, "n") then
do
numeric digits 40 -- allow to deal with numbers up to 2**128
return -((left-right)~sign) -- returns -1 (left<right), +1 (left>right), 0 (left=right)
end
return stringComparator~compare(left,right) -- rgf, 20090520
/* ======================================================================= */
/* Single class to wrap comparators for string objects:
- ascending, case-dependent (Comparator: "A", "C"), also: "AC", "CA"
- descending, case-dependent (DescendingComparator: "D", "C"), also: "DC", "CD"
- ascending, case-independent (CaselessComparator: "A", "I"), also: "AI", "IA"
- descending, case-independent (CaselessDescendingComparator: "D", "I"), also: "DI", "ID"
.StringComparator~new([A|D] ,[C|I][N])
A|D ... optional: "A"scending (default), "D"escending
C|I|N ... optional: "C"ase dependent (default), "I"gnore case (default), "N"umeric (Rexx-style numbers (default) )
[hint: argument letters and sequence from SysStemSort]
Restriction: this class is used by .NumberComparator and uses NumberComparator objects as well
*/
::class "StringComparator" mixinclass Comparator
::method init
parse upper arg order, case
if order="" then order="A" -- default to ascending
else order=order~strip~translate
if case ="" then case="NI" -- default to ignore case and compare numeric strings as Rexx numbers
else case=case~strip~translate
pos=pos(order, "AD") -- check whether valid argument
if pos=0 then
raise syntax 93.914 array ("# 1 (order)", "AD", order)
pos=verify(case, "ICN") -- check whether built of valid arguments
if pos<>0 then
raise syntax 93.914 array ("# 2 (comparison type)", "CIN", case)
bCompareNumeric=(pos("N", case)>0) -- numeric comparisons ?
if pos("C", case)>0 then -- respect case explicitly given?
case="C" -- respect case
else
case="I" -- ignore case (default)
if bCompareNumeric=.true then -- numeric strings should be compared according to the Rexx rules
do
-- create a number comparator that uses a .StringComparator
numComp=.NumberComparator~new(.true, order, case)
-- now get the comparison method from the numberComparator object
self~setMethod("compare", numComp~instanceMethod("compare"))
-- now get (from numComp) and add (to this instance) the getter and setter methods with the same scope
self~setMethod("stringComparator=", numComp~instanceMethod("stringComparator=")) -- setter
self~setMethod("stringComparator", numComp~instanceMethod("stringComparator")) -- getter
-- now get (from numComp) the stringComparator object and use it (in this instance)
self~stringComparator=numComp~stringComparator
end
else -- plain string comparisons, treat numeric values as string of characteres
do
sign="" -- no sign for return value
if order="D" then
sign="-" -- inverse the result
if case="I" then -- ignore case in comparisons
self~setMethod("compare", "use strict arg left, right; return" sign"left~caselessCompareTo(right)")
else
self~setMethod("compare", "use strict arg left, right; return" sign"left~compareTo(right)")
end
::method compare abstract -- by default abstract to define the protocol
/* ======================================================================= */
/*
usage: NEW({pos [,length] [,A|D} [,C|I|N]} [, ...])
NEW(coll [,defaultAD [,defaultCIN]])
sorts by the given column, there can be as many columns as the user sees fit
where arguments:
pos start position
length optional, indicates comparison length
A|D optional, sort "A"scending/"D"escending
C|I|N optional, use "C"ase-sensitive|case-"I"ndependent|
"N"umberComparator
"O"numberComparator relaxed (treats non-numbers as 0)
coll ordered collection or supplier object containing the arguments
in the above described sequence
defaultAD default value for sorting order, in case it is omitted
defaultCIN default value for comparison type, in case it is omitted
there can be any number of columns, two consecutive numbers are interpreted
as pos and length; 'length' is omitted if 'pos' is followed by a non-numeric
argument (A|D or C|I|N)
*/
::class 'StringColumnComparator' mixinclass Comparator
::method init
expose numberComparator
use strict arg arg1, ...
def=""
if datatype(arg(2),"M") then def=arg(2)
if datatype(arg(3),"M") then def=def||arg(3)
-- check and set sort options
parse value checkSortOptions(def) with def defAD defCIN
/* if argument is an ordered collection or supplier, then use its content
to set up the comparison code */
if arg1~isA(.OrderedCollection) | arg1~isA(.Supplier) then
args=arg1~allItems -- get items/values as an array
else
args=arg(1, "A") -- get arguments as an array
-- comparator for Rexx numbers (defaults to allow comparing numbers with strings as well)
numberComparator=.numberComparator~new
-- analyze columns and type of comparison, store in temp array
resArr=.array~new -- [1]...'pos', [2]...'length' or .nil, [3]...A|D, [4]...C|I
count=0 -- index into resulting array
items=args~items -- number of args
do i=1 to items
-- expecting position
val=args[i] -- get argument
if \datatype(val, "W") | val<1 then
raise syntax 93.907 array (i, val) -- raise an error
count=count+1 -- new column to sort
resArr[count,1]=val -- save starting position
-- set default sorting options
resArr[count,3]=defAD -- use default a/descending order
resArr[count,4]=defCIN -- use case-sensitive, insensitive, number
if i=items then leave -- no more infos
-- length available ?
i+=1 -- position on next arg, if available
val=args[i]
if datatype(val, "W") then
do
resArr[count,2]=val -- save length
if i=items then leave -- already last item processed?
if datatype(args[i+1],"W") then -- a number coming up, i.e. a new starting position!
iterate i -- iterate
i+=1 -- position on next item, i.e. sorting option
val=args[i] -- a sorting options
end
-- a sorting option in hand?
if i<items, \datatype(args[i+1],"W") then
do
i+=1 -- position on next item, i.e. a sorting option (CIN)
val=val||args[i]
end
-- determine and assign sorting options
parse value checkSortOptions(val) with val resArr[count,3] resArr[count,4]
end
/* o.k., we now create the code for the method 'compare' */
methArr=.array~of("expose numberComparator", "", "use strict arg left, right", "")
dim1=resArr~dimension(1) -- get nr. of entries in first dimension
do i=1 to dim1
if i<dim1 then -- not the last comparison ?
do
methArr~append("res="createCodeSnippet(resArr,i))
methArr~append("if res<>0 then return res")
methArr~append("")
end
else
methArr~append('return' createCodeSnippet(resArr,i))
end
/* now use this code for a method 'compare', use object's scope */
self~setMethod("compare", methArr, "Object")
return
/* -------------- check for options ------------------- */
-- check options, make sure all are set
checkSortOptions: procedure
parse upper arg def, arg2
def=(def||arg2)~space(0)
-- check for "A"scdending, "D"escending
pos=verify("AD", def, "M") -- find matching char
if pos=0 then
def=def||"A" -- add ascending as default
-- check for "C"ase-sensitive, case "I"nsensitive, "N"umber comparison
pos=verify("CIN", def, "M") -- find matching char
if pos=0 then
def=def||"I" -- ignore case by default
-- def=def||"C" -- add case-sensitive as default
pos=verify(def, "ADCIN", "N") -- find non-matching char
if pos<>0 then -- error: non-matching char in option string!
raise syntax 93.915 array ("ADCIN", def":" substr(def,pos,1))
return def -
substr(def, verify(def, "AD" , "M") ,1) - -- extract option letter
substr(def, verify(def, "CIN", "M"),1) -- extract option letter
/* -------------- create comparison code -------------- */
createCodeSnippet: procedure
use arg resArr, idx
-- determine starting position and (optional) length
startPosAndLength=resArr[idx,1] -- start column
if .nil<>resArr[idx,2] then -- length given?
startPosAndLength=startPosAndLength","resArr[idx,2]
pos=pos(resArr[idx,4], "CIN") -- determine which kind of comparison
if pos<3 then -- string comparisons using "[caseless]CompareTo"
do
if pos=2 then -- case-Independent comparisons
tmpStr="left~caselessCompareTo(right,"
else -- CASE-dependent comparisons
tmpStr="left~compareTo(right,"
tmpStr=tmpStr startPosAndLength -- supply start position (and optional length)
end
else -- Rexx numbers to compare!
do
tmpStr="numberComparator~compare(left~subStr("startPosAndLength")," -
"right~substr("startPosAndLength")"
end
if resArr[idx,3]="D" then -- sort descending: invert result
return "-"tmpStr")" -- return the comparison statement
return tmpStr")" -- return the comparison statement
/* ======================================================================= */
/* Enclose string in square brackets show non-printable chars as Rexx hex-strings.
If non-string object, then show its string value and hash-value.
*/
::routine pp2 public -- rgf, 20091214
use strict arg a1
if \a1~isA(.string) then
do
if a1~isA(.Collection) then
return "["a1~string "("a1~items "items)" "id#_" || (a1~identityHash)"]"
else
return "["a1~string "id#_" || (a1~identityHash)"]"
end
return "["escape2(a1)"]"
/* ======================================================================= */
/* Enclose string in square brackets show non-printable chars as Rexx hex-strings.
If non-string object, then show its string value and hash-value.
Formats Index-values.
*/
::routine ppIndex2 public -- rgf, 20091214
use strict arg a1
if \a1~isA(.string) then
do
if a1~isA(.array), a1~dimension=1 then
do
-- if a1~dimension=1 then -- create comma-delimited list of index-values?
do
tmpStr=""
bFirst=.true
minWid=1
maxElements=5
do i=1 to a1~items for maxElements
tmpVal=a1[i]
if datatype(tmpVal,"W"), length(tmpVal)<minWid then
tmpVal=tmpVal~right(minWid)
if bFirst then
do
tmpStr=tmpVal
bFirst=.false
end
else
tmpStr=tmpStr","tmpVal
end
if a1~items>maxElements then
do
tmpStr=", ..."
end
return "["tmpStr"]"
end
end
return "["a1~string "id#_" || (a1~identityHash)"]"
end
return pp2(a1) -- rgf, 20091228
/* Escape non-printable chars in Rexx-style. */
::routine escape2 public -- rgf, 20091214
parse arg a1
res=""
do while a1\==""
pos1=verify(a1, .rgf.non.printable, "M")
if pos1>0 then
do
pos2=verify(a1, .rgf.non.printable, "N" , pos1)
if pos2=0 then
pos2=length(a1)+1
if pos1=1 then
do
parse var a1 char +(pos2-pos1) a1
bef=""
end
else
parse var a1 bef +(pos1-1) char +(pos2-pos1) a1
if res=="" then
do
if bef \=="" then res=enquote2(bef) '|| '
end
else
do
res=res '||' enquote2(bef) '|| '
end
res=res || '"'char~c2x'"x'
end
else
do
if res<>"" then
res=res '||' enquote2(a1)
else
res=a1
a1=""
end
end
return res
/* ======================================================================= */
/* Returns a new relation object created from the passed in collection
object. If the second argument is given, it must be a message name or
a message object.
makeRelation2(coll [,message])
coll ... collection or supplier object to turn into a relation object
message ... optional, must be the name of a message or a message
object which gets sent to each object in the collection
and which result object is used as the index object to
which the collection object should be associated with
in the new relation
*/
::routine makeRelation2 public
use strict arg coll, message=.nil
if .nil=message then -- only one argument, assuming collection object
do
return .relation~new~~putAll(coll~supplier)
end
if message~isA(.string) then -- name of a message, create message object
message=.message~new(.nil, message)
else if \message~isA(.message) then
raise syntax 36.900 array ("Argument 2 must be a message name (a string) or a message object!")
rel=.relation~new
do o over coll
rel[message~copy~send(o)]=o -- use message result as index for o
end
return rel
/* ======================================================================= */
/* Enquote string, escape quote/apostrophe. Optionally supply character(s) to serve as
quote/apostrophe.
*/
::routine enquote2 public -- rgf, 20091214
use arg string, quote='"'
return quote || string~changestr(quote, quote~copies(2)) || quote
/* ======================================================================= */
/*
Expects a method object and an optional string for indenting/prefixing.
Returns a string containing the source code or a comment indicating that
no source code is available for the method.
*/
::routine ppMethod2 public -- rgf, 20091214
use strict arg meth, indent=""
src=meth~source /* get source */
tmpStr=""
do s over src
if tmpStr="" then /* first round ? */
tmpStr=indent || s
else
tmpStr=tmpStr || .endOfLine || indent || s
end
if tmpStr="" then /* no source code available */
return "/* no source code available */"
return tmpStr
/* Class that allows to define deliberately which characters constitute
delimiters of words or which characters constitute words. All word-related
BIFs are implemented as methods. Operations that change the string value
(methods delWord, subWord) will change the instance's string value accordingly.
The characters that serve either as word-delimiters or as constituting
words are available via the attribute "reference", the interpretation
of the reference characters is controlled via the attribute "kind" (values
"D"elimiter- or "W"ord-characters). The string value to operate on is available
via the attribute "string".
The following attributes are available:
string ... the string to work upon
reference ... a string of characters that either serve as word delimiters or
define the characters that constitute words (e.g. allows for
defining all letters in German, including umlauts!)
kind ... determines how "reference" is interpreted: "D"elimiter characters
or "W"ord characters (characters that constitute a word)
wordArray ... a read-only attribute that supplies a one-dimensional array of
words extracted from "string" according to "reference" characters
interpreted according to "kind"
positionArray ... a read-only attribute that supplies a two-dimensional array
of positions and lengths of the words contained in "string"
according to "reference" characters interpreted according to "kind"
*/
::class "StringOfWords"
/* Arguments:
string ... mandatory
reference ... optional (default: " "||"09"x), defines a string of characters
kind ... optional (default: "D"), determines whether "reference" contains
characters that "d"elimit words or constitute "w"ords.
*/
::method init
expose string oldKind positionArray wordArray
signal on syntax
-- check arguments
use strict arg string, reference=(" "||"09"x), kind="D"
.ArgUtil~validateClass("string", string, .string) -- check for correct type
.ArgUtil~validateClass("reference", reference, .string) -- check for correct type
if reference=="" then -- empty string, define default: blank/tab
reference=" "||"09"x
self~reference=reference -- assign reference
.ArgUtil~validateClass("kind", kind , .string) -- check for correct type
self~kind=kind -- check & assign "kind"-value
return
syntax: raise propagate
::attribute string get
::attribute string set
expose string posDirty? wordDirty?
parse arg string
-- make sure arrays are regenerated at access time
posDirty?=.true
wordDirty?=.true
::attribute reference get -- character-string used for VERIFY()-reference
::attribute reference set
expose reference oldReference dirty?
parse arg tmp
if reference\==tmp then -- save "D" or "W" to use directly with parseWords2()
do
reference=tmp -- save "D" or "W" to use directly with parseWords2()
posDirty?=.true -- on next access of pos+len-array, re-create array object
wordDirty?=.true -- on next access of pos+len-array, re-create array object
end
-- determines whether "reference" is used for determining space or word characters
-- "D"elimiter-chars, "W"ord-chars
::attribute kind get
::attribute kind set
expose internalKind kind posDirty? wordDirty?
parse arg tmp .
signal on syntax
tmp1=tmp~left(1)~upper
if pos(tmp1, "DW")=0 then
raise syntax 93.914 array("'kind'", "D[elimiter] | W[ord-characters]", tmp)
kind=tmp
if internalKind<>tmp1 then -- save "D" or "W" to use directly with parseWords2()
do
internalKind=tmp1 -- save "D" or "W" to use directly with parseWords2()
posDirty?=.true -- on next access of pos+len-array, re-create array object
wordDirty?=.true -- on next access of pos+len-array, re-create array object
end
return
syntax: raise propagate
::attribute positionArray get -- execute "parseWords2"
expose posDirty? internalKind positionArray reference string
if posDirty? then -- string/reference/kind changed, make sure we (re-)generate the position/length array
do
/* TODO: parseWords2() vs. parseWords() ? */
positionArray=parseWords2(string, reference, internalKind, "P")
posDirty?=.false
end
return positionArray~copy
::attribute wordArray get
expose wordArray wordDirty? internalKind reference string
if wordDirty? then -- string/reference/kind changed, make sure we (re-)generate the position/length array
do
/* TODO: parseWords2() vs. parseWords() ? */
wordArray=parseWords2(string, reference, internalKind, "W")
wordDirty?=.false
end
return wordArray~copy
::method makeArray
forward message (wordArray)
/* The following methods are the counterparts of the word-related BIFs.
It is intentional that the arrays are retrieved via a message, such that
on-demand creating of the array object is possible and therefore reflects
the words in the string according to the currently set delimiters and kinds
of parsing.
*/
/* delWord(position[, length]) */
::method delWord -- in place, i.e. changing this string ?
expose string posDirty? wordDirty?
signal on syntax
use strict arg position, ... -- make sure at least one argument
parse arg position, length
if \datatype(position, "W") then
raise syntax 93.905 array("'position'", position) -- must be a number
if position=0 then -- must not be null!
raise syntax 93.924 array(position) -- must be a number
tmpString=string -- default to string value
arr=self~positionArray -- get positional array
words=arr~dimension(1) -- get number of words
if position<0 then -- negative value? Position from right
do
position=words+position+1 -- get number of words, deduct "position"
if position<1 then -- not enough words in string, impossible to position value
do
position=1 -- position on first word
end
end
if length="" then -- length omitted? delete all remaining words
do
if position<=words then -- only delete, if position is within words-range
do
tmpString=string~left(arr[position,1]-1) -- change string in place
posDirty?=.true
wordDirty?=.true
end
return tmpString
end
else if \datatype(length, "W") then
raise syntax 93.905 array("'length'", arg(2)) -- must be a number
if length=0 then -- don't change anything
do
return tmpString
end
if length<0 then -- move positioning position to left?
do
tmpPos=position+length+1
if tmpPos<1 then -- beyond start, delete all words up to and including position
do
length=position -- set length to position
position=1 -- set start to 1
end
else
do
length=position-tmpPos+1 -- number of words affected
position=tmpPos -- starting position
end
end
if position+length-1 < words then -- after deleting there will be words left over?
do
tmpString=string~left(arr[position,1]-1) || string~substr(arr[position+length,1])
posDirty?=.true
wordDirty?=.true
end
else
do
if position<=words then
do
tmpString=string~left(arr[position,1]-1)
posDirty?=.true
wordDirty?=.true
end
end
return tmpString
syntax: raise propagate
/* subWord() strips leading and trailing spaces of returned string (but keeps
them between words)!
*/
::method subWord -- result has never leading or trailing spaces!
expose string
signal on syntax
use strict arg position, ... -- make sure at least one argument
parse arg position, length
if \datatype(position, "W") then
raise syntax 93.905 array("'position'", position) -- must be a number
if position=0 then -- must not be null!
raise syntax 93.924 array(position) -- must be a number
arr=self~positionArray -- get positional array
words=arr~dimension(1) -- get number of words
if position<0 then -- negative value? Position from right
do
position=words+position+1 -- get number of words, deduct "position"
if position<1 then -- not enough words in string, impossible to position value
position=1 -- position on first word
end
tmpString=""
if length="" then -- length omitted? delete all remaining words
do
if position>words then
tmpString=""
else
do
tmpPos=arr[position,1] -- starting position (no leading spaces)
endPos=arr[words,1]+arr[words,2] -- ending position (no trailing spaces)
tmpLen=endPos-tmpPos -- calculate length
tmpString=string~substr(tmpPos,tmpLen) -- extract subword string
end
return tmpString
end
else if \datatype(length, "W") then
raise syntax 93.905 array("'length'", arg(2)) -- must be a number
if length=0 then -- return empty string
do
tmpString=""
return tmpString
end
if length<0 then -- move positioning position to left?
do
tmpPos=position+length+1
if tmpPos<1 then -- beyond start, delete all words up to and including position
do
length=position -- set length to position
position=1 -- set start to 1
end
else
do
length=position-tmpPos+1 -- number of words affected
position=tmpPos -- starting position
end
end
if position>words then
tmpString=""
else
do
tmpPos=arr[position,1] -- starting position (no leading spaces)
lastWord=min(position+length-1,words) -- calc last word to be included
endPos=arr[lastWord,1]+arr[lastWord,2] -- ending position (no trailing spaces)
tmpLen=endPos-tmpPos -- calculate length
tmpString=string~substr(tmpPos,tmpLen) -- extract subword string
end
return tmpString
syntax: raise propagate
::method word -- extract and return word
expose string
signal on syntax
use strict arg position
if \datatype(position, "W") then
raise syntax 93.905 array("'n' (n-th word in string)", arg(2)) -- must be a number
arr=self~positionArray -- get positional array
maxItems=arr~dimension(1) -- get # of entries
if position<1 then
do
position=maxItems+position+1 -- calc position from right
if position<1 then -- minimum start is 1
return ""
end
if maxItems<position then
return ""
return string~substr(arr[position,1], arr[position,2]) -- return extracted word
syntax: raise propagate
::method words -- return # of words
return self~positionArray~dimension(1)
::method wordIndex
signal on syntax
use strict arg position
if \datatype(position, "W") then
raise syntax 93.905 array("'n' (n-th word in string)", arg(2)) -- must be a number
arr=self~positionArray -- get positional array
maxItems=arr~dimension(1) -- get # of entries
if position<1 then
do
position=maxItems+position+1 -- calc position from right
if position<1 then -- minimum start is 1
return 0
end
if maxItems<position then
return 0
return arr[position,1] -- return length of word
syntax: raise propagate
::method wordLength
signal on syntax
use strict arg position
if \datatype(position, "W") then
raise syntax 93.905 array("'n' (n-th word in string)", arg(2)) -- must be a number
arr=self~positionArray -- get positional array
maxItems=arr~dimension(1) -- get # of entries
if position<1 then
do
position=maxItems+position+1 -- calc position from right
if position<1 then -- minimum start is 1
return 0
end
if maxItems<position then
return 0
return arr[position,2] -- return length of word
syntax: raise propagate
::method wordPos
expose string
signal on syntax
use strict arg phrase, startPos=1, letter="I"
argNr=arg() -- get total number of arguments
if argNr=2 then -- only two args
do
if \ datatype(startPos,"W") then -- the "letter" argument is last one?
do
letter=startPos~strip~left(1)~upper -- assuming C or I argument
startPos=1
end
end
if \datatype(startPos,"W") then
raise syntax 93.905 array ("2 ('startPosition')", startPos)
if arg(3,"Exists") then
letter=letter~strip~left(1)~upper
if pos(letter,"CI")=0 then -- illegal argument!
raise syntax 93.914 array (argNr, "C, I", arg(argNr))
bCaselessCompare=(letter="I")
arr=self~positionArray -- get positional array
phrObj=self~class~new(phrase) -- wrap phrase, could contain more than one word!
arrPhrasePos=phrObj~positionArray -- get position array of words
arrPhraseWord=phrObj~wordArray -- get array of words
firstPhraseWordLength=arrPhrasePos[1,2] -- save length of first word in phrase
maxItems=arr~dimension(1) -- get maximum entries in dimension 1, i.e. max nr of words in string
if startPos<1 then -- negative start position
do
tmpPos=maxItems+startPos+1 -- calc start position from right
if tmpPos<1 then -- minimum start poistion is 1
tmpPos=1
startPos=tmpPos -- set startPos
end
maxPhraseItems=arrPhrasePos~dimension(1) -- get maximum of words in phrase
do i=startPos to maxItems
if arr[i,2]=firstPhraseWordLength then -- o.k. word of same length
do
if (i+maxPhraseItems-1)>maxItems then -- not enough words left, hence cannot match
return 0 -- indicate phrase not found
bFound=.true
do k=1 to maxPhraseItems while bFound
m=i+k-1
if bCaselessCompare then
bFound=bFound & ((string~caselessMatch(arr[m,1], arrPhraseWord[k]))=1)
else
bFound=bFound & ((string~ match(arr[m,1], arrPhraseWord[k]))=1)
end
if bFound then -- found! return position
return i
end
end
return 0 -- not found
syntax: raise propagate
/* rgf, maybe "todo" as of 2010-01-12
- Folding sort2() and stableSort2() ?
- DateTime2: alle "toXXX", updateable, epochable
- Class ConstantGroup
*/
/* create and return a string rendering of the supplied condition object
rgf, 2011-06-08
*/
::routine ppCondition2 public
use strict arg co, bShowPackageInfo=.false, indent1="09"x, lf=.endOfLine
indent2=indent1~copies(2)
indent3=indent1~copies(3)
maxWidth=0 -- determine length of widest index
do idx over co
if idx~isA(.string) then maxWidth=max(maxWidth,idx~length)
end
maxWidth+=2 -- add square brackets
mb=.MutableBuffer~new
do idx over co~allindexes~sort
entry=co[idx]
mb~~append(indent1) ~~append(pp2(idx)~left(maxWidth)) ~~append("=") ~~append(pp2(entry)) ~~append(lf)
if entry~isA(.collection) then
do val over entry
mb ~~append(indent2) ~~append(pp2(val)) ~~append(lf)
end
else if entry~isA(.package), bShowPackageInfo=.true then
do
mb ~~append(ppPackage2(entry, indent2, indent3, lf))
end
end
return mb~string
/* Create and return a string rendering of the package information.
rgf, 2011-06-08
*/
::routine ppPackage2 public
use strict arg package, indent1="", indent2="09"x, lf=.endOfLine
width=20
mb = .MutableBuffer~new
mb ~~append(indent1) ~~append(pp2("name")~left(width, ".")) ~~append(pp2(package~name)) ~~append(lf)
mb ~~append(indent1) ~~append(pp2("size")~left(width, ".")) ~~append(pp2(package~sourceSize)) ~~append( " line(s)")~~append(lf)
mb ~~append(indent1) ~~append("---") ~~append(lf)
mb ~~append(indent1) ~~append(pp2("definedMethods")~left(width, ".")) ~~append(listCollection(package~definedMethods, indent2, lf)) ~~append(lf)
mb ~~append(indent1) ~~append("---") ~~append(lf)
mb ~~append(indent1) ~~append(pp2("defined classes")~left(width, ".")) ~~append(listCollection(package~classes, indent2, lf)) ~~append(lf)
mb ~~append(indent1) ~~append(pp2("publicClasses")~left(width, ".")) ~~append(listCollection(package~publicClasses, indent2, lf)) ~~append(lf)
mb ~~append(indent1) ~~append(pp2("importedClasses")~left(width, ".")) ~~append(listCollection(package~importedClasses, indent2, lf)) ~~append(lf)
mb ~~append(indent1) ~~append("---") ~~append(lf)
mb ~~append(indent1) ~~append(pp2("defined routines")~left(width, ".")) ~~append(listCollection(package~routines, indent2, lf)) ~~append(lf)
mb ~~append(indent1) ~~append(pp2("publicRoutines")~left(width, ".")) ~~append(listCollection(package~publicRoutines, indent2, lf)) ~~append(lf)
mb ~~append(indent1) ~~append(pp2("importedRoutines")~left(width, ".")) ~~append(listCollection(package~importedRoutines, indent2, lf)) ~~append(lf)
mb ~~append(indent1) ~~append("---") ~~append(lf)
mb ~~append(indent1) ~~append(pp2("importedPackages")~left(width, ".")) ~~append(listCollection(package~importedPackages, indent2, lf)) ~~append(lf)
mb ~~append(indent1) ~~append("---") ~~append(lf)
return mb~string
listCollection: procedure
use strict arg coll, indent, lf
mb=.MutableBuffer~new
mb~~append(pp2(coll)) ~~append(lf)
if coll~isA(.MapCollection) then
do
if coll~isA(.Directory) then -- index is a string, comparisons are o.k.
workColl=coll~allIndexes~sort
else -- else make sure we get a string value to sort on
workColl=sort2(coll~allIndexes, "Message", "string")
end
else -- do not sort
workColl=coll~allItems
do idx over workColl -- iterate over
if idx~isA(.package) then
mb~~append(indent) ~~append(pp2(idx~name)) ~~append(lf)
else
mb~~append(indent) ~~append(pp2(idx)) ~~append(lf)
end
return mb~string

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks