Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[r8286]: main / trunk / interpreter / classes / NumberStringClass.cpp Maximize Restore History

Download this file

NumberStringClass.cpp    3801 lines (3502 with data), 167.4 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
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
/*----------------------------------------------------------------------------*/
/* */
/* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
/* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
/* */
/* This program and the accompanying materials are made available under */
/* the terms of the Common Public License v1.0 which accompanies this */
/* distribution. A copy is also available at the following address: */
/* http://www.oorexx.org/license.html */
/* */
/* Redistribution and use in source and binary forms, with or */
/* without modification, are permitted provided that the following */
/* conditions are met: */
/* */
/* Redistributions of source code must retain the above copyright */
/* notice, this list of conditions and the following disclaimer. */
/* Redistributions in binary form must reproduce the above copyright */
/* notice, this list of conditions and the following disclaimer in */
/* the documentation and/or other materials provided with the distribution. */
/* */
/* Neither the name of Rexx Language Association nor the names */
/* of its contributors may be used to endorse or promote products */
/* derived from this software without specific prior written permission. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
/* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
/* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
/* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
/* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
/* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* */
/*----------------------------------------------------------------------------*/
/******************************************************************************/
/* REXX Kernel */
/* */
/* Primitive NumberString Class */
/* */
/******************************************************************************/
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include "RexxCore.h"
#include "StringClass.hpp"
#include "BufferClass.hpp"
#include "RexxActivation.hpp"
#include "NumberStringMath.hpp"
#include "Numerics.hpp"
#include "StringUtil.hpp"
// singleton class instance
RexxClass *RexxNumberString::classInstance = OREF_NULL;
/**
* Create initial class object at bootstrap time.
*/
void RexxNumberString::createInstance()
{
CLASS_CREATE(NumberString, "String", RexxClass);
}
/* MHES 20050108 deprecated */
#define string_forwarder(method)\
RexxObject *RexxNumberString::##method(RexxObject *operand)\
{\
return (RexxObject *)this->stringValue()->##method(operand);\
}
/**
* Constructor for a new number string value.
*
* @param len The length we require for the value.
*/
RexxNumberString::RexxNumberString(size_t len)
{
this->NumDigits = number_digits();
this->sign = 1;
this->length = len;
if (number_form() == Numerics::FORM_SCIENTIFIC)
{
this->NumFlags |= NumFormScientific;
}
}
/**
* Create a number string for a given digits precision
*
* @param len The length of value we need to accomodate
* @param precision The precision to be used for formatting.
*/
RexxNumberString::RexxNumberString(size_t len, size_t precision)
{
this->NumDigits = precision;
this->sign = 1;
this->length = len;
if (number_form() == Numerics::FORM_SCIENTIFIC)
{
this->NumFlags |= NumFormScientific;
}
}
RexxNumberString *RexxNumberString::clone()
/******************************************************************************/
/* Function: low level copy of a number string object */
/******************************************************************************/
{
/* first clone ourselves */
RexxNumberString *newObj = (RexxNumberString *)this->RexxInternalObject::clone();
/* don't keep the original string */
OrefSet(newObj, newObj->stringObject, OREF_NULL);
/* or the OVD fields */
OrefSet(newObj, newObj->objectVariables, OREF_NULL);
return newObj; /* return this */
}
/**
* Get the primitive hash value of this String object.
*
* @return The calculated string hash for the string.
*/
HashCode RexxNumberString::getHashValue()
{
return stringValue()->getHashValue();
}
void RexxNumberString::live(size_t liveMark)
/******************************************************************************/
/* Function: Normal garbage collection live marking */
/******************************************************************************/
{
memory_mark(this->objectVariables);
memory_mark(this->stringObject);
}
void RexxNumberString::liveGeneral(int reason)
/******************************************************************************/
/* Function: Generalized object marking */
/******************************************************************************/
{
memory_mark_general(this->objectVariables);
memory_mark_general(this->stringObject);
}
void RexxNumberString::flatten(RexxEnvelope *envelope)
/******************************************************************************/
/* Function: Flatten an object */
/******************************************************************************/
{
setUpFlatten(RexxNumberString)
flatten_reference(newThis->objectVariables, envelope);
flatten_reference(newThis->stringObject, envelope);
cleanUpFlatten
}
void RexxNumberString::setString(
RexxString *stringObj ) /* new string value */
/******************************************************************************/
/* Function: Set the number string's string value */
/******************************************************************************/
{
/* set the new string value */
OrefSet(this, this->stringObject, stringObj);
this->setHasReferences(); /* we now have to garbage collect */
}
RexxString *RexxNumberString::makeString()
/******************************************************************************/
/* Function: Handle a REQUEST('STRING') request for a REXX numberstring */
/******************************************************************************/
{
return this->stringValue(); /* return the string value */
}
RexxInteger *RexxNumberString::hasMethod(RexxString *methodName)
/******************************************************************************/
/* Function: Handle a HASMETHOD request for an integer */
/******************************************************************************/
{
/* return the string value's answer */
return this->stringValue()->hasMethod(methodName);
}
RexxString *RexxNumberString::primitiveMakeString()
/******************************************************************************/
/* Function: Handle a REQUEST('STRING') request for a REXX numberstring */
/******************************************************************************/
{
if (this->stringObject != OREF_NULL) /* already converted? */
{
return this->stringObject; /* all finished */
}
return this->stringValue(); /* return the string value */
}
RexxString *RexxNumberString::stringValue()
/******************************************************************************/
/* Function: Convert a number string to a string object */
/******************************************************************************/
{
char expstring[17], num;
int carry;
size_t createdDigits;
size_t MaxNumSize, LenValue;
wholenumber_t numindex;
wholenumber_t temp, ExpValue, ExpFactor;
size_t charpos;
RexxString *StringObj;
if (this->stringObject != OREF_NULL) /* already converted? */
{
return this->stringObject; /* all finished */
}
/* Start converting the number. */
if (this->sign == 0 )
{ /* is the number zero? */
/* Yes, return a 0 string. */
OrefSet(this, this->stringObject, OREF_ZERO_STRING);
this->setHasReferences(); /* we now have to garbage collect */
return this->stringObject; /* and return now */
}
else
{ /* No, convert the number. */
/* convert the exponent numeber into */
/* string. */
createdDigits = this->NumDigits; /* Get Digits settings at creation */
ExpValue = this->exp; /* Working copy of exponent */
LenValue = this->length; /* Working copy of the length. */
/* If no exponent */
if (ExpValue == 0)
{
/* Yes, we can fast path conversion */
MaxNumSize = LenValue; /* Size of result is length of number*/
if (this->sign <0) /* if number is negative. */
{
MaxNumSize++; /* result one bigger for - sign. */
}
/* Get new string, of exact length, */
StringObj = (RexxString *)raw_string(MaxNumSize);
charpos = 0; /* data in string to start. */
if (this->sign < 0)
{ /* If number is neagative */
/* add negative sign and bump index */
StringObj->putChar(charpos++, ch_MINUS);
}
/* For each number digits in number */
for (numindex=0; (size_t)numindex < LenValue; numindex++)
{
/* place char rep in NumString */
num = this->number[numindex] + ch_ZERO;
StringObj->putChar(charpos++, num);
} /* Done with Fast Path.... */
}
else
{ /* We need to do this the long way */
carry = 0; /* assume rounding-up NOT necessary */
/*is number just flat out too big? */
if ((( ExpValue + (wholenumber_t)LenValue - 1) > Numerics::MAX_EXPONENT) ||
(ExpValue < (Numerics::MIN_EXPONENT)) ) /* Yes, report Overflow error. */
{
reportException(Error_Conversion_operator, this);
}
ExpFactor = 0; /* number not eponential yet.. */
temp = ExpValue + (wholenumber_t)LenValue - 1; /* get size of this number */
expstring[0] = '\0'; /* string vlaue of exp factor, Null */
/* is left of decimal > NumDigits or */
if ((temp >= (wholenumber_t)createdDigits) || /* exponent twice numDigits */
((size_t)Numerics::abs(ExpValue) > (createdDigits*2)) )
{
/* Yes, we need to go exponential. */
/* we need Engineering format? */
if (!(this->NumFlags & NumFormScientific))
{
if (temp < 0) /* Yes, is number a whole number? */
{
temp -= 2; /* force 2 char adjustment to left */
}
temp = (temp/3) * 3; /* get count to the right of Decimal */
}
if (Numerics::abs(temp) > Numerics::MAX_EXPONENT)
{ /* is adjusted number too big? */
if (temp > Numerics::MAX_EXPONENT) /* did it overflow? */
{
/* Yes, report overflow error. */
reportException(Error_Overflow_expoverflow, temp, Numerics::DEFAULT_DIGITS);
}
else
{
/* Actually an underflow error. */
reportException(Error_Overflow_expunderflow, temp, Numerics::DEFAULT_DIGITS);
}
}
ExpValue -= temp; /* adjust the exponent */
if ( temp != 0 )
{ /* do we still have exponent ? */
ExpFactor = true; /* Save the factor */
}
else
{
ExpFactor = false; /* no need to save the factor */
}
if (temp < 0)
{
*expstring = 'E';
/* convert exponent value into string*/
Numerics::formatWholeNumber(temp, expstring + 1);
}
else if (temp > 0)
{
strcpy(expstring, "E+");
/* convert exponent value into string*/
Numerics::formatWholeNumber(temp, expstring + 2);
}
temp = Numerics::abs(temp); /* get positive exponent factor */
}
/* Now compute size of result string */
if (ExpValue >= 0 ) /* if the Exponent is positive */
{
/* Size is length of number plus exp.*/
MaxNumSize = (size_t)ExpValue + LenValue;
}
/*is exponent larger than length, */
else if ((size_t)Numerics::abs(ExpValue) >= LenValue)
{
/* Yes, we will need to add zeros to */
/* the front plus a 0. */
MaxNumSize = Numerics::abs(ExpValue) + 2;
}
else /*Won't be adding any digits, just */
{
MaxNumSize = LenValue + 1; /* length of number + 1 for decimal */
}
if (ExpFactor) /* Are we using Exponential notation?*/
{
/* Yes, need to add in size of the */
MaxNumSize += strlen(expstring); /* exponent stuff. */
}
if (this->sign <0) /* is number negative? */
{
MaxNumSize++; /* yes, add one to size for - sign */
}
/* get new string of appropriate size*/
StringObj = (RexxString *)raw_string(MaxNumSize);
charpos = 0; /* set starting position */
if (this->sign < 0)
{ /* Is the number negative? */
/* Yes, add in the negative sign. */
StringObj->putChar(charpos, ch_MINUS);
}
temp = ExpValue + (wholenumber_t)LenValue; /* get the adjusted length. */
/* Since we may have carry from round*/
/* we'll fill in the number from the */
/* back and make our way forward. */
/* Strindex points to exponent start */
/* part of string. */
charpos = MaxNumSize - strlen(expstring);
if (ExpFactor) /* will result have Exponent? */
{
/* Yes, put the data into the string.*/
StringObj->put(charpos, expstring, strlen(expstring));
}
/* Next series of If's will determine*/
/* if we need to add zeros to end */
/* of the number and where to place */
/* decimal, fill in string as we */
/* go, also need to check for a carry*/
/* if we rounded the number early on.*/
if (temp <= 0)
{ /* Is ther an Integer portion? */
/* 0.000000xxxx result. */
/* ^^^^^^^^ filler */
/* Start filling in digits */
/* from the back.... */
for (numindex = (wholenumber_t)(LenValue-1);numindex >= 0 ;numindex-- )
{
/* are we carry from round? */
num = this->number[numindex]; /* working copy of this Digit. */
num = num + ch_ZERO; /* now put the number as a character */
StringObj->putChar(--charpos, num);
}
temp = -temp; /* make the number positive... */
if (temp)
{
charpos -= temp; /* yes, point to starting pos to fill*/
/* now fill in the leading Zeros. */
StringObj->set(charpos, ch_ZERO, temp);
}
StringObj->putChar(--charpos, ch_PERIOD);
if (carry) /* now put in the leading 1. is carry*/
{
StringObj->putChar(--charpos, ch_ONE);
}
else /* or 0. if no carry. */
{
StringObj->putChar(--charpos, ch_ZERO);
}
}
/* do we need to add zeros at end? */
else if ((size_t)temp >= LenValue)
{
/* xxxxxx000000 result */
/* ^^^^^^ filler */
/* Yes, add zeros first. */
temp -= LenValue; /* see how many zeros we need to add.*/
charpos -= temp; /* point to starting pos to fill */
/* now fill in the leading Zeros. */
StringObj->set(charpos, ch_ZERO, temp);
/* done w/ trailing zeros start */
/* adding digits (from back) */
/* start filling in digits */
for (numindex = (wholenumber_t)LenValue-1;numindex >= 0 ;numindex-- )
{
num = this->number[numindex]; /* working copy of this Digit. */
num = num + ch_ZERO; /* now put the number as a character */
StringObj->putChar(--charpos, num);
}
} /* done with this case.... */
else
{ /* we have a partial Decimal number */
/* add in the first digit a 1. */
/* Start filling in digits */
for (numindex = (wholenumber_t)LenValue - 1; numindex > temp - 1 ;numindex-- )
{
num = this->number[numindex]; /* working copy of this Digit. */
/* now put the number as a character */
num += ch_ZERO;
StringObj->putChar(--charpos, num);
}
/* add in the decimal point. */
StringObj->putChar(--charpos, ch_PERIOD);
/* Start filling in digits */
/* add numbers before decimal point */
for (numindex = temp - 1; numindex >= 0; numindex-- )
{
num = this->number[numindex]; /* working copy of this Digit. */
num += ch_ZERO;
StringObj->putChar(--charpos, num);
}
/* end of final case, conversion done*/
}
} /* end of non-fast path conversion. */
} /* End of conversion of number */
/* since string is created from */
/* number string, we can set the */
StringObj->setNumberString(this); /* lookaside right away */
/* and also save this string */
OrefSet(this, this->stringObject, StringObj);
this->setHasReferences(); /* we now have to garbage collect */
return StringObj; /* all done, return new string */
}
bool RexxNumberString::numberValue(wholenumber_t &result)
/******************************************************************************/
/* Function: Convert a number string to a wholenumber value */
/******************************************************************************/
{
// convert using the default digits version
return this->numberValue(result, Numerics::DEFAULT_DIGITS);
}
bool RexxNumberString::unsignedNumberValue(stringsize_t &result)
/******************************************************************************/
/* Function: Convert a number string to a unsigned whole number value */
/******************************************************************************/
{
// convert using the default digits version
return this->unsignedNumberValue(result, Numerics::DEFAULT_DIGITS);
}
bool RexxNumberString::numberValue(wholenumber_t &result, size_t numDigits)
/******************************************************************************/
/* Function: Convert a number string to a number value */
/******************************************************************************/
{
// set up the default values
bool carry = false;
wholenumber_t numberExp = this->exp;
stringsize_t numberLength = this->length;
size_t intnum;
// if the number is exactly zero, then this is easy
if (this->sign == 0)
{
result = 0;
return true;
}
// is this easily within limits (very common)?
if (length <= numDigits && numberExp >= 0)
{
if (!createUnsignedValue(number, length, false, numberExp, Numerics::maxValueForDigits(numDigits), intnum))
{
return false; // too big to handle
}
// adjust for the sign
result = ((wholenumber_t)intnum) * sign;
return true;
}
// this number either has decimals, or needs to be truncated/rounded because of
// the conversion digits value. We need to make adjustments.
if (!checkIntegerDigits(numDigits, numberLength, numberExp, carry))
{
return false;
}
// if because of this adjustment, the decimal point lies to the left
// of our first digit, then this value truncates to 0 (or 1, if a carry condition
// resulted).
if (-numberExp>= (wholenumber_t)numberLength)
{
// since we know a) this number is all decimals, and b) the
// remaining decimals are either all 0 or all 9s with a carry,
// this result is either 0 or 1.
result = carry ? 1 : 0;
return true;
}
// we process different bits depending on whether this is a negative or positive
// exponent
if (numberExp < 0)
{
// now convert this into an unsigned value
if (!createUnsignedValue(number, numberLength + numberExp, carry, 0, Numerics::maxValueForDigits(numDigits), intnum))
{
return false; // to big to handle
}
}
else
{ /* straight out number. just compute.*/
if (!createUnsignedValue(number, numberLength, carry, numberExp, Numerics::maxValueForDigits(numDigits), intnum))
{
return false; // to big to handle
}
}
// adjust for the sign
result = ((wholenumber_t)intnum) * sign;
return true;
}
bool RexxNumberString::unsignedNumberValue(stringsize_t &result, size_t numDigits)
/******************************************************************************/
/* Function: Convert a number string to an unsigned number value */
/******************************************************************************/
{
// set up the default values
bool carry = false;
wholenumber_t numberExp = this->exp;
stringsize_t numberLength = this->length;
size_t intnum;
// if the number is exactly zero, then this is easy
if (this->sign == 0)
{
result = 0;
return true;
}
// we can't convert negative values into an unsigned one
if (sign < 0)
{
return false;
}
// is this easily within limits (very common)?
if (length <= numDigits && numberExp >= 0)
{
if (!createUnsignedValue(number, length, false, numberExp, Numerics::maxValueForDigits(numDigits), intnum))
{
return false; // too big to handle
}
// we can just return this directly.
result = intnum;
return true;
}
// this number either has decimals, or needs to be truncated/rounded because of
// the conversion digits value. We need to make adjustments.
if (!checkIntegerDigits(numDigits, numberLength, numberExp, carry))
{
return false;
}
// if because of this adjustment, the decimal point lies to the left
// of our first digit, then this value truncates to 0 (or 1, if a carry condition
// resulted).
if (-numberExp>= (wholenumber_t)numberLength)
{
// since we know a) this number is all decimals, and b) the
// remaining decimals are either all 0 or all 9s with a carry,
// this result is either 0 or 1.
result = carry ? 1 : 0;
return true;
}
// we process different bits depending on whether this is a negative or positive
// exponent
if (numberExp < 0)
{
// now convert this into an unsigned value
if (!createUnsignedValue(number, numberLength + numberExp, carry, 0, Numerics::maxValueForDigits(numDigits), intnum))
{
return false; // to big to handle
}
}
else
{ /* straight out number. just compute.*/
if (!createUnsignedValue(number, numberLength, carry, numberExp, Numerics::maxValueForDigits(numDigits), intnum))
{
return false; // to big to handle
}
}
// adjust for the sign
result = intnum;
return true;
}
bool RexxNumberString::doubleValue(double &result)
/******************************************************************************/
/* Function: Convert a number string to a double */
/******************************************************************************/
{
RexxString *string; /* string version of the number */
string = this->stringValue(); /* get the string value */
/* convert the number */
result = strtod(string->getStringData(), NULL);
// and let pass all of the special cases
return true;
}
RexxInteger *RexxNumberString::integerValue(
size_t digits ) /* required precision */
/******************************************************************************/
/* Function: convert a number string to an integer object */
/******************************************************************************/
{
wholenumber_t integerNumber; /* converted value */
if (!numberValue(integerNumber, number_digits()))
{
return (RexxInteger *)TheNilObject;/* just return .nil */
}
return new_integer(integerNumber);
}
/*********************************************************************/
/* Function: Convert the numberstring to unsigned value */
/*********************************************************************/
bool RexxNumberString::createUnsignedValue(const char *thisnum, stringsize_t intlength, int carry, wholenumber_t exponent, size_t maxValue, size_t &result)
{
// if the exponent multiplier would cause an overflow, there's no point in doing
// anything here
if (exponent > (wholenumber_t)Numerics::ARGUMENT_DIGITS)
{
return false;
}
// our converted value
size_t intNumber = 0;
for (stringsize_t numpos = 1; numpos <= intlength; numpos++ )
{
// add in the next digit value
size_t newNumber = (intNumber * 10) + (size_t)*thisnum++;
// if an overflow occurs, then the new number will wrap around and be
// smaller that the starting value.
if (newNumber < intNumber)
{
return false;
}
// make this the current value and continue
intNumber = newNumber;
}
// do we need to add in a carry value because of a rounding situation?
if (carry)
{
// add in the carry bit and check for an overflow, again
size_t newNumber = intNumber + 1;
if (newNumber < intNumber)
{
return false;
}
intNumber = newNumber;
}
// have an exponent to process?
if (exponent > 0)
{
// get this as a multipler value
size_t exponentMultiplier = 1;
while (exponent > 0)
{
exponentMultiplier *= 10;
exponent--;
}
// get this as a multipler value
size_t newNumber = intNumber * exponentMultiplier;
// did this wrap? This is a safe test, since we capped
// the maximum exponent size we can multiply by.
if (newNumber < intNumber)
{
return false;
}
intNumber = newNumber;
}
// was ths out of range for this conversion?
if (intNumber >= maxValue)
{
return false;
}
result = intNumber; /* Assign return value. */
return true; /* Indicate sucessfule converison. */
}
/*********************************************************************/
/* Function: Convert the numberstring to unsigned value */
/*********************************************************************/
bool RexxNumberString::createUnsignedInt64Value(const char *thisnum, stringsize_t intlength, int carry, wholenumber_t exponent, uint64_t maxValue, uint64_t &result)
{
// if the exponent multiplier would cause an overflow, there's no point in doing
// anything here
if (exponent > (wholenumber_t)Numerics::DIGITS64)
{
return false;
}
// our converted value
uint64_t intNumber = 0;
for (stringsize_t numpos = 1; numpos <= intlength; numpos++ )
{
// add in the next digit value
uint64_t newNumber = (intNumber * 10) + (uint64_t)*thisnum++;
// if an overflow occurs, then the new number will wrap around and be
// smaller that the starting value.
if (newNumber < intNumber)
{
return false;
}
// make this the current value and continue
intNumber = newNumber;
}
// do we need to add in a carry value because of a rounding situation?
if (carry)
{
// add in the carry bit and check for an overflow, again
uint64_t newNumber = intNumber + 1;
if (newNumber < intNumber)
{
return false;
}
intNumber = newNumber;
}
// have an exponent to process?
if (exponent > 0)
{
// get this as a multipler value
uint64_t exponentMultiplier = 1;
while (exponent > 0)
{
exponentMultiplier *= 10;
exponent--;
}
uint64_t newNumber = intNumber * exponentMultiplier;
// did this wrap? This is a safe test, since we capped
// the maximum exponent size we can multiply by.
if (newNumber < intNumber)
{
return false;
}
intNumber = newNumber;
}
// was ths out of range for this conversion?
if (intNumber > maxValue)
{
return false;
}
result = intNumber; /* Assign return value. */
return true; /* Indicate sucessfule converison. */
}
bool RexxNumberString::checkIntegerDigits(stringsize_t numDigits, stringsize_t &numberLength,
wholenumber_t &numberExponent, bool &carry)
/******************************************************************************/
/* Function: Check that a numberstring is convertable into an integer value */
/******************************************************************************/
{
carry = false;
numberExponent = this->exp;
numberLength = this->length;
// is this number longer than the digits value?
// this is going to be truncated or rounded, so we
// need to see if a carry is required, and also
// adjust the exponent value.
if (this->length > numDigits)
{
// adjust the effective exponent up by the difference
numberExponent += (this->length - numDigits);
// and override the converted length to be just the digits length
numberLength = numDigits;
// now check to see if the first excluded digit will cause rounding
// if it does, we need to worry about a carry value when converting
if (*(this->number + numberLength) >= 5)
{
carry = true;
}
}
// if we have a negative exponent, then we need to look at
// the values below the decimal point
if (numberExponent < 0)
{
// the position of the decimal is the negation of the exponent
stringsize_t decimalPos = (stringsize_t)(-numberExponent);
// everything to the right of the decimal must be a zero.
char compareChar = 0;
// if we had a previous carry condition, this changes things a
// bit. Since the carry will add 1 to the right-most digit,
// in order for all of the decimals to end up zero, then all of
// the digits there must actually be '9's.
if (carry)
{
// if the decimal position will result in at least one padding
// zero, then the carry makes it impossible for all of the decimals
// to reduce to zero. This cannot be a whole number.
if (decimalPos > numberLength)
{
return false;
}
// switch the checking value
compareChar = 9;
}
const char *numberData;
if (decimalPos >= numberLength )
{
// decimal is somewhere to the left of everything...
// all of these digits must be checked
decimalPos = numberLength;
numberData = this->number;
}
else
{
// get the exponent adjusted position
numberData = this->number + numberLength + numberExponent;
}
for ( ; decimalPos > 0 ; decimalPos--)
{
// a bad digits data means a conversion failure
if ( *numberData++ != compareChar)
{
return false;
}
}
}
return true;
}
bool RexxNumberString::int64Value(int64_t *result, stringsize_t numDigits)
/******************************************************************************/
/* Function: Convert a number string to a int64 value */
/******************************************************************************/
{
// set up the default values
bool carry = false;
wholenumber_t numberExp = this->exp;
stringsize_t numberLength = this->length;
uint64_t intnum;
// if the number is exactly zero, then this is easy
if (this->sign == 0)
{
*result = 0;
return true;
}
// is this easily within limits (very common)?
if (length <= numDigits && numberExp >= 0)
{
// the minimum negative value requires one more than the max positive
if (!createUnsignedInt64Value(number, length, false, numberExp, ((uint64_t)INT64_MAX) + 1, intnum))
{
return false; // too big to handle
}
// this edge case can be a problem, so check for it specifically
if (intnum == ((uint64_t)INT64_MAX) + 1)
{
// if at the limit, this must be a negative number
if (sign != -1)
{
return false;
}
*result = INT64_MIN;
}
else
{
// adjust for the sign
*result = ((int64_t)intnum) * sign;
}
return true;
}
// this number either has decimals, or needs to be truncated/rounded because of
// the conversion digits value. We need to make adjustments.
if (!checkIntegerDigits(numDigits, numberLength, numberExp, carry))
{
return false;
}
// if because of this adjustment, the decimal point lies to the left
// of our first digit, then this value truncates to 0 (or 1, if a carry condition
// resulted).
if (-numberExp>= (wholenumber_t)numberLength)
{
// since we know a) this number is all decimals, and b) the
// remaining decimals are either all 0 or all 9s with a carry,
// this result is either 0 or 1.
*result = carry ? 1 : 0;
return true;
}
// we process different bits depending on whether this is a negative or positive
// exponent
if (numberExp < 0)
{
// now convert this into an unsigned value
if (!createUnsignedInt64Value(number, numberLength + numberExp, carry, 0, ((uint64_t)INT64_MAX) + 1, intnum))
{
return false; // to big to handle
}
}
else
{ /* straight out number. just compute.*/
if (!createUnsignedInt64Value(number, numberLength, carry, numberExp, ((uint64_t)INT64_MAX) + 1, intnum))
{
return false; // to big to handle
}
}
// the edge case is a problem, so handle it directly
if (intnum == ((uint64_t)INT64_MAX) + 1)
{
// if at the limit, this must be a negative number
if (sign != -1)
{
return false;
}
*result = INT64_MAX;
}
else
{
// adjust for the sign
*result = ((int64_t)intnum) * sign;
}
return true;
}
bool RexxNumberString::unsignedInt64Value(uint64_t *result, stringsize_t numDigits)
/******************************************************************************/
/* Function: Convert a number string to a int64 value */
/******************************************************************************/
{
// set up the default values
bool carry = false;
wholenumber_t numberExp = this->exp;
stringsize_t numberLength = this->length;
// if the number is exactly zero, then this is easy
if (this->sign == 0)
{
*result = 0;
return true;
}
// no signed values allowed
if (sign == -1)
{
return false;
}
// is this easily within limits (very common)?
if (length <= numDigits && numberExp >= 0)
{
if (!createUnsignedInt64Value(number, length, false, numberExp, UINT64_MAX, *result))
{
return false; // too big to handle
}
return true;
}
// this number either has decimals, or needs to be truncated/rounded because of
// the conversion digits value. We need to make adjustments.
if (!checkIntegerDigits(numDigits, numberLength, numberExp, carry))
{
return false;
}
// if because of this adjustment, the decimal point lies to the left
// of our first digit, then this value truncates to 0 (or 1, if a carry condition
// resulted).
if (-numberExp>= (wholenumber_t)numberLength)
{
// since we know a) this number is all decimals, and b) the
// remaining decimals are either all 0 or all 9s with a carry,
// this result is either 0 or 1.
*result = carry ? 1 : 0;
return true;
}
// we process different bits depending on whether this is a negative or positive
// exponent
if (numberExp < 0)
{
// now convert this into an unsigned value
return createUnsignedInt64Value(number, numberLength + numberExp, carry, 0, UINT64_MAX, *result);
}
else
{ /* straight out number. just compute.*/
return createUnsignedInt64Value(number, numberLength, carry, numberExp, UINT64_MAX, *result);
}
}
bool RexxNumberString::truthValue(
int errorcode ) /* error to raise if not good */
/******************************************************************************/
/* Function: Return a truth value boolean for a number string */
/******************************************************************************/
{
if (this->sign == 0 ) /* exactly zero? */
{
return false; /* done quickly */
}
/* not exactly 1? */
else if (!(this->sign == 1 && this->exp == 0 && this->length == 1L && *(this->number) == 1))
{
reportException(errorcode, this);/* report the error */
}
return true; /* this is true */
}
/**
* Convert an object to a logical value without raising an
* error.
*
* @param result The converted value.
*
* @return true if this converted ok, false for an invalid logical.
*/
bool RexxNumberString::logicalValue(logical_t &result)
{
if (this->sign == 0 ) /* exactly zero? */
{
result = false; // this is false and the conversion worked
return true;
}
/* exactly 1? */
else if (this->sign == 1 && this->exp == 0 && this->length == 1 && *(this->number) == 1)
{
result = true; // this is true and the conversion worked
return true;
}
else
{
return false; // bad conversion
}
}
bool numberStringScan(const char *number, size_t length)
/******************************************************************************/
/* Arguments: Number data, number length */
/* Function :Scan the string to determine if its a valid number */
/* Returned : 0 if input was valid number */
/* 1 if input was invalid number */
/******************************************************************************/
{
char ch; /* current character */
const char *InPtr; /* Input Data Pointer */
const char *EndData; /* Scan end position */
bool hadPeriod; /* had a decimal point already */
/* for efficiency, this code takes */
/* advantage of the fact that REXX */
/* string object's all have a guard */
/* null character on the end */
if (!length)
{ /* Length zero not a number? */
return true; /* a null string is not a number */
}
hadPeriod = false; /* period yet */
InPtr = number; /*Point to start of input string. */
EndData = InPtr + length; /*Point to end of Data + 1. */
while (*InPtr == ch_BLANK || *InPtr == ch_TAB) /* Skip all leading blanks. */
{
InPtr++; /* Skip it, and go on to next char */
}
/* Is this a sign Character? */
if ((ch = *InPtr) == ch_MINUS || ch == ch_PLUS)
{
InPtr++; /* Yes, skip it. */
while (*InPtr == ch_BLANK || *InPtr == ch_TAB) /* Ship all leading blanks. */
{
InPtr++; /* Skip it, and go on to next char */
}
}
if (*InPtr == ch_PERIOD)
{ /* got a leading period? */
InPtr++; /* step over it */
hadPeriod = true; /* got the decimal point already */
}
ch = *InPtr; /* Get 1st Digit. */
if (ch < ch_ZERO || ch > ch_NINE) /* Is this a valid digit? */
{
return true; /* Nope, bad number */
}
else
{
/*Skip all leading Zero's */
while (*InPtr == ch_ZERO) /* While 1st Digit is a 0 */
{
InPtr++; /* Go to next character. */
}
/* Have we reach end of number,num */
/*zero? */
if (InPtr >= EndData)
{
return false; /* valid number... all Zeros */
}
}
/* While the character is a Digit. */
while (*InPtr >= ch_ZERO && *InPtr <= ch_NINE)
{
InPtr++; /* Go to next digit */
}
if (InPtr >= EndData) /* Did we reach end of data? */
{
return false; /* all done, just return valid number*/
}
if (*InPtr == ch_PERIOD)
{ /*Decimal point??? */
if (hadPeriod) /* already had one? */
{
return true; /* yep, this is a bad number */
}
InPtr++; /* yes, skip it. */
/* While the character is a Digit. */
while (*InPtr >= ch_ZERO && *InPtr <= ch_NINE)
{
InPtr++; /* Go to next digit */
}
if (InPtr >= EndData) /* Did we reach end of data */
{
return false; /* this was fine */
}
}
if (toupper(*InPtr) == 'E')
{ /* See if this char is an exponent? */
if (++InPtr >= EndData) /* Yes, but did we reach end of input*/
{
/* Yes, invalid number. */
return true;
}
/* If this a plus/minus sign? */
if ((*InPtr == ch_MINUS) || (*InPtr == ch_PLUS))
{
InPtr++; /* go on to next char. */
}
if (InPtr >= EndData) /* reach end of Input ? */
{
return true; /* Yes, invalid number. */
}
/* If this char a valid digit? */
if (*InPtr < ch_ZERO || *InPtr > ch_NINE)
{
return true; /* No, invalid number. */
}
/* Do while we have a valid digit */
while (*InPtr >= ch_ZERO && *InPtr <= ch_NINE)
{
InPtr++; /* Yup, go to next one and check */
}
}
/* At this point all that should be */
/* left Are trailing blanks. */
while (*InPtr == ch_BLANK || *InPtr == ch_TAB) /* Skip all trailing blanks */
{
InPtr++; /* Skip it, and go on to next char */
}
if (InPtr >= EndData) /* Did we reach end of data */
{
return false; /* this was fine */
}
return true; /* wasn't a valid number */
}
void fill_digits( /* create a string of digits */
char *outPtr, /* output location */
const char *number, /* start of string of digits */
size_t count ) /* size of resulting string */
/******************************************************************************/
/* Function : Copy "count" digits of a number to the desired location, */
/* converting them back to character digits */
/******************************************************************************/
{
while (count--) /* while still have characters */
{
*outPtr++ = *number++ + '0'; /* convert back to character */
}
}
RexxObject *RexxNumberString::trunc(
RexxObject *decimal_digits) /* number of decimal digits */
/******************************************************************************/
/* Function: Truncate a number to given decimal digit count */
/******************************************************************************/
{
/* get the decimal count */
size_t needed_digits = optionalNonNegative(decimal_digits, 0, ARG_ONE);
/* round to current digits setting */
return this->prepareNumber(number_digits(), ROUND)->truncInternal(needed_digits);
}
RexxObject *RexxNumberString::truncInternal(
size_t needed_digits) /* number of decimal digits */
/******************************************************************************/
/* Function: Truncate a number to given decimal digit count */
/******************************************************************************/
{
RexxString *result; /* returned result */
wholenumber_t temp; /* temporary string value */
wholenumber_t integer_digits; /* leading integer digits */
size_t size; /* total size of the result */
int signValue; /* current sign indicator */
char *resultPtr; /* result pointer */
if (this->sign == 0)
{ /* is the number zero? */
if (needed_digits == 0) /* no decimals requested? */
{
/* just return a zero */
return IntegerZero;
}
else
{ /* need to pad */
/* get an empty string */
result = (RexxString *)raw_string(needed_digits + 2);
/* get a data pointer */
resultPtr = result->getWritableData();
strcpy(resultPtr, "0."); /* copy the leading part */
/* fill in the trailing zeros */
memset(resultPtr + 2, '0', needed_digits);
return result; /* return the result */
}
}
else
{ /* have to do real formatting */
size = 0; /* start with nothing */
signValue = this->sign; /* copy the sign */
/* calculate the leading part */
/* number have a decimal part? */
if (this->exp > 0)
{
/* add in both length and exponent */
size += this->length + this->exp;
if (needed_digits != 0) /* have digits required? */
{
size += needed_digits + 1; /* add in the digits and the decimal */
}
}
else
{ /* number has a decimal part. */
/* get the leading part */
integer_digits = (wholenumber_t)this->length + this->exp;
if (integer_digits > 0)
{ /* something on the left hand side? */
size += integer_digits; /* add in these digits */
if (needed_digits != 0) /* decimals requested? */
{
size += needed_digits + 1; /* add in the digits and the decimal */
}
}
else
{ /* no leading part */
if (needed_digits == 0) /* nothing wanted after decimal? */
{
return IntegerZero; /* this is just zero then */
}
/* do we need to pad more zeros than */
/* number we want after the decimal?*/
if ((wholenumber_t)needed_digits <= -integer_digits)
{
size = needed_digits + 2; /* result is formatted zero...no sign*/
signValue = 0; /* force the sign out */
}
else
{
size += needed_digits + 2; /* calculate the decimal size */
}
}
}
if (signValue < 0) /* negative number? */
{
size++; /* start with a sign */
}
/* get an empty pointer */
result = (RexxString *)raw_string(size);
/* point to the data part */
resultPtr = result->getWritableData();
if (signValue < 0) /* negative number? */
{
*resultPtr++ = '-'; /* start with a sign */
}
/* calculate the leading part */
/* number have a decimal part? */
if (this->exp > 0)
{
/* fill in the digits */
fill_digits(resultPtr, this->number, this->length);
resultPtr += this->length; /* step over the length */
/* now fill in the extra zeros */
memset(resultPtr, '0', this->exp);
resultPtr += this->exp; /* and the exponent */
if (needed_digits != 0)
{ /* decimals requested? */
*resultPtr++ = '.'; /* add a trailing decimal point */
/* copy on the trailers */
memset(resultPtr, '0', needed_digits);
}
}
else
{ /* number has a decimal part. */
integer_digits = this->length + this->exp;
if (integer_digits > 0)
{ /* something on the left hand side? */
/* add the integer digits */
fill_digits(resultPtr, this->number, integer_digits);
resultPtr += integer_digits; /* step over the digits */
if (needed_digits != 0)
{ /* decimals requested? */
*resultPtr++ = '.'; /* add a trailing decimal point */
/* get count to add */
temp = Numerics::minVal(needed_digits, this->length - integer_digits);
/* fill in the digits */
fill_digits(resultPtr, this->number + integer_digits, temp);
resultPtr += temp; /* step over the digits */
needed_digits -= temp; /* adjust the length */
if (needed_digits != 0) /* still need more? */
{
/* copy on the trailers */
memset(resultPtr, '0', needed_digits);
}
}
}
else
{ /* no leading part */
/* do we need to pad more zeros than */
/* number we want after the decimal?*/
if ((wholenumber_t)needed_digits <= -integer_digits)
{
strcpy(resultPtr, "0."); /* copy a leading zero part */
resultPtr += 2; /* step over */
/* copy on the trailers */
memset(resultPtr, '0', needed_digits);
}
else
{
strcpy(resultPtr, "0."); /* copy a leading zero part */
resultPtr += 2; /* step over */
/* copy on the trailers */
memset(resultPtr, '0', -integer_digits);
resultPtr += -integer_digits;/* step over the digits */
needed_digits += integer_digits; /* reduce needed_digits */
/* get count to add */
temp = Numerics::minVal(needed_digits, this->length);
/* fill in the digits */
fill_digits(resultPtr, this->number, temp);
resultPtr += temp; /* step over the digits */
needed_digits -= temp; /* adjust the length */
if (needed_digits != 0) /* still need more? */
{
/* copy on the trailers */
memset(resultPtr, '0', needed_digits);
}
}
}
}
}
return result; /* return the formatted number */
}
/**
* Return the floor value for a decimal numeric value. The floor
* is the first full number value less that the current
* value. If the number has no significant decimal positions,
* this returns the same value (minus any trailing decimal zeros).
* NOTE: If the number is negative, the value goes toward the
* lower number (e.g., the floor of -3.5 is -4, not -3).
*
* @return The numeric floor of this value.
*/
RexxObject *RexxNumberString::floor()
{
/* round to current digits setting */
return this->prepareNumber(number_digits(), ROUND)->floorInternal();
}
/**
* Calculate the floor value for a number.
*
* @return A string value of the floor, with appropriate
* formatting for the function.
*/
RexxObject *RexxNumberString::floorInternal()
{
// if this is exactly zero, then the floor is always zero
if (this->sign == 0)
{
return IntegerZero;
}
// if this is a positive number, then this is the same as trunc
else if (this->sign > 0)
{
return this->truncInternal(0);
}
else
{
// since we have to go lower, first check to see if there are
// any non-zero decimals. If not, then we can call trunc
// directly to format. If there are non-zero decimals, we add one
// to the value and then let trunc do the heavy lifting.
// not have a decimal part? If no decimal part, just pass to trunc
if (this->exp >= 0)
{
return this->truncInternal(0);
}
else
{
// number has a decimal part, so we need to see if there are
// any non-zero decimals
// get the number of decimals we need to scan
stringsize_t decimals = Numerics::minVal(this->length, (stringsize_t)(-this->exp));
// get the position to start the scan
stringsize_t lastDecimal = this->length - 1;
bool foundNonZero = false;
for (stringsize_t i = decimals; i > 0; i--)
{
// if we found a non-zero, we have to do this the hard way
if (this->number[lastDecimal--] != 0)
{
foundNonZero = true;
break;
}
}
// no-nonzero values, we can allow trunc to handle it from here
if (!foundNonZero)
{
return this->truncInternal(0);
}
// ok, we need to add 1 to this value, and then allow trunc to finish the
// job. Unfortunately, this might require us to round out of the top of
// the number, so we need to account for this
// get the leading digits of the value
wholenumber_t integer_digits = (wholenumber_t)this->length + this->exp;
// if there are no integer digits, then this number is between 0 and -1.
// we can just punt here and return -1.
if (integer_digits <= 0)
{
return IntegerMinusOne;
}
else
{
// ok, we have integer digits. Let's make like easy at this
// point by chopping this down to just the integer digits and
// throwing away the exponent
this->length = integer_digits;
this->exp = 0;
// point to the first digit, and start adding until
// we no longer round
char *current = this->number + integer_digits - 1;
while (current >= this->number)
{
int ch = *current + 1;
// did this round?
if (ch > 9)
{
// overwrite with a zero and keep looping
*current-- = 0;
}
else
{
// overwrite this value and have the trunc function
// format the value
*current = ch;
return this->truncInternal(0);
}
}
// ok, we rounded all the way out. At this point, every digit in
// the buffer is a zero. Doing the rounding is easy here, just
// stuff a 1 in the first digit and bump the exponent by 1
*this->number = 1;
this->exp += 1;
// and again, the trunc code can handle all of the formatting
return this->truncInternal(0);
}
}
}
}
/**
* Return the ceiling value for a decimal numeric value. The
* ceiling is the first full number value greater that the
* current value. If the number has no significant decimal
* positions, this returns the same value (minus any trailing
* decimal zeros). NOTE: If the number is negative, the value
* goes toward the higher number (e.g., the ceiling of -3.5 is
* -3, not
* -4).
*
* @return The numeric ceiling of this value.
*/
RexxObject *RexxNumberString::ceiling()
{
/* round to current digits setting */
return this->prepareNumber(number_digits(), ROUND)->ceilingInternal();
}
/**
* Calculate the ceiling value for a number.
*
* @return A string value of the ceiling, with appropriate
* formatting for the function.
*/
RexxObject *RexxNumberString::ceilingInternal()
{
// if this is exactly zero, then the ceiling is always zero
if (this->sign == 0)
{
return IntegerZero;
}
// if this is a negative number, then this is the same as trunc
else if (this->sign < 0)
{
return this->truncInternal(0);
}
else
{
// since we have to go higher, first check to see if there are
// any non-zero decimals. If not, then we can call trunc
// directly to format. If there are non-zero decimals, we add one
// to the value and then let trunc do the heavy lifting.
// not have a decimal part? If no decimal part, just pass to trunc
if (this->exp >= 0)
{
return this->truncInternal(0);
}
else
{
// number has a decimal part, so we need to see if there are
// any non-zero decimals
// get the number of decimals we need to scan
stringsize_t decimals = Numerics::minVal((stringsize_t)this->length, (stringsize_t)(-this->exp));
// get the position to start the scan
stringsize_t lastDecimal = this->length - 1;
bool foundNonZero = false;
for (stringsize_t i = decimals; i > 0; i--)
{
// if we found a non-zero, we have to do this the hard way
if (this->number[lastDecimal--] != 0)
{
foundNonZero = true;
break;
}
}
// no-nonzero values, we can allow trunc to handle it from here
if (!foundNonZero)
{
return this->truncInternal(0);
}
// ok, we need to add 1 to this value, and then allow trunc to finish the
// job. Unfortunately, this might require us to round out of the top of
// the number, so we need to account for this
// get the leading digits of the value
wholenumber_t integer_digits = (wholenumber_t)this->length + this->exp;
// if there are no integer digits, then this number is between 0 and 1.
// we can just punt here and return 1.
if (integer_digits <= 0)
{
return IntegerOne;
}
else
{
// ok, we have integer digits. Let's make like easy at this
// point by chopping this down to just the integer digits and
// throwing away the exponent
this->length = integer_digits;
this->exp = 0;
// point to the first digit, and start adding until
// we no longer round
char *current = this->number + integer_digits - 1;
while (current >= this->number)
{
int ch = *current + 1;
// did this round?
if (ch > 9)
{
// overwrite with a zero and keep looping
*current-- = 0;
}
else
{
// overwrite this value and have the trunc function
// format the value
*current = ch;
return this->truncInternal(0);
}
}
// ok, we rounded all the way out. At this point, every digit in
// the buffer is a zero. Doing the rounding is easy here, just
// stuff a 1 in the first digit and bump the exponent by 1
*this->number = 1;
this->exp += 1;
// and again, the trunc code can handle all of the formatting
return this->truncInternal(0);
}
}
}
}
/**
* Round the number value depending on the value of the first
* decimal position using standard rounding rules.
* NOTE: this uses the same rules for floor and ceiling when
* determining how things are rounded. This is really defined
* as floor(number + .5). Thus 3.4 and -3.4 round to 3 and -3,
* since they end up calculating floor(3.9) and floor(-2.9),
* repectively.
*
* @return The rounded value
*/
RexxObject *RexxNumberString::round()
{
/* round to current digits setting */
return this->prepareNumber(number_digits(), ROUND)->roundInternal();
}
/**
* Calculate the rounded number value.
*
* @return A string value of the rounded number.
*/
RexxObject *RexxNumberString::roundInternal()
{
// if this is exactly zero, then the rounded value is always zero
if (this->sign == 0)
{
return IntegerZero;
}
// ok, regardless of the sign, we first decide based on the first
// decimal which way we are rounding.
else
{
// since we might have to go higher, first check to see if there are
// any non-zero decimals. If not, then we can call trunc
// directly to format. Otherwise, we need to look at the first
// decimal position and see which way we go.
// not have a decimal part? If no decimal part, just pass to trunc
if (this->exp >= 0)
{
return this->truncInternal(0);
}
else
{
// number has a decimal part, so we need to look at the first
// decimal position
// get the leading digits of the value
wholenumber_t integer_digits = (wholenumber_t)this->length + this->exp;
// if the exponent is larger than the number of significant digits, then
// the first digit to the right of the decimal is zero, so this will always
// round to zero
if (integer_digits < 0)
{
return IntegerZero;
}
else
{
// ok, we have integer digits and decimals. The returned value will
// be an integer, so we can just set the exponent to zero now and
// throw away the decimal part
this->length = integer_digits;
this->exp = 0;
// Now we need to look at the first decimal and see if
// we're rounding up
char *current = this->number + integer_digits;
// no rounding needed, go do the formatting
if (*current < 5)
{
return this->truncInternal(0);
}
// we need to add one to the integer part...which of course
// might round all the way out.
current--;
while (current >= this->number)
{
int ch = *current + 1;
// did this round?
if (ch > 9)
{
// overwrite with a zero and keep looping
*current-- = 0;
}
else
{
// overwrite this value and have the trunc function
// format the value
*current = ch;
return this->truncInternal(0);
}
}
// ok, we rounded all the way out. At this point, every digit in
// the buffer is a zero. Doing the rounding is easy here, just
// stuff a 1 in the first digit and bump the exponent by 1
*this->number = 1;
this->exp += 1;
// and again, the trunc code can handle all of the formatting
return this->truncInternal(0);
}
}
}
}
RexxString *RexxNumberString::formatRexx(
RexxObject *Integers, /* space for integer part */
RexxObject *Decimals, /* number of decimals required */
RexxObject *MathExp, /* the exponent size */
RexxObject *ExpTrigger ) /* the exponent trigger */
/******************************************************************************/
/* Function : Format the numberstring data according to the format */
/* function controls. */
/******************************************************************************/
{
size_t integers; /* integer space requested */
size_t decimals; /* decimal space requested */
size_t mathexp; /* exponent space requested */
size_t exptrigger; /* exponential notation trigger */
size_t digits; /* current numeric digits */
bool form; /* current numeric form */
digits = number_digits(); /* get the current digits value */
form = number_form(); /* and the exponential form */
/* get the space for integer part */
integers = optionalNonNegative(Integers, -1, ARG_ONE);
/* and the decimal part */
decimals = optionalNonNegative(Decimals, -1, ARG_TWO);
/* also the exponent size */
mathexp = optionalNonNegative(MathExp, -1, ARG_THREE);
/* and the trigger */
exptrigger = optionalNonNegative(ExpTrigger, digits, ARG_FOUR);
/* round to current digits setting */
return this->prepareNumber(digits, ROUND)->formatInternal(integers, decimals, mathexp, exptrigger, this, digits, form);
}
RexxString *RexxNumberString::formatInternal(
size_t integers, /* space for integer part */
size_t decimals, /* number of decimals required */
size_t mathexp, /* the exponent size */
size_t exptrigger, /* the exponent trigger */
RexxNumberString *original, /* oringial NumStr */
size_t digits, /* digits to format to */
bool form) /* form to format to */
/******************************************************************************/
/* Function : Format the numberstring data according to the format */
/* function controls. */
/******************************************************************************/
{
wholenumber_t expfactor; /* actual used exponent */
wholenumber_t temp; /* temporary calculation holder */
size_t exponentsize = 0; /* size of the exponent */
char exponent[15]; /* character exponent value */
wholenumber_t adjust; /* exponent adjustment factor */
size_t size; /* total size of the result */
size_t leadingSpaces; /* number of leading spaces */
size_t leadingZeros = 0; /* number of leading zeros */
size_t leadingExpZeros = 0; /* number of leading zeros in exp */
size_t trailingZeros; /* number of trailing zeros */
size_t reqIntegers; /* requested integers */
RexxString *result; /* final formatted number */
char *resultPtr; /* pointer within the result */
bool defaultexpsize = false; /* default exponent size */
expfactor = 0; /* not exponential yet */
if (mathexp != 0)
{ /* Is exponential allowed? */
/* calculate the exponent factor */
temp = this->exp + this->length - 1;
/* is left of dec>digits */
/* or twice digits on right */
if (temp >= (wholenumber_t)exptrigger || Numerics::abs(this->exp) > (wholenumber_t)(exptrigger * 2))
{
if (form == Numerics::FORM_ENGINEERING)
{ /* request for Engineering notation? */
if (temp < 0) /* yes, is it a whole number? */
{
temp = temp - 2; /* no, force two char left adjustment -2 instead of -1 */
}
temp = (temp / 3) * 3; /* get count right of decimal point */
}
this->exp = this->exp - temp; /* adjust the exponent */
expfactor = temp; /* save the factor */
temp = Numerics::abs(temp); /* get positive exponent value */
/* format exponent to a string */
Numerics::formatWholeNumber(temp, exponent);
/* get the number of digits needed */
exponentsize = strlen(exponent);
if (mathexp == (size_t)-1)
{ /* default exponent size? */
mathexp = exponentsize; /* use actual length */
defaultexpsize = true; /* default exponent size on */
}
if (exponentsize > mathexp) /* not enough room? */
{
reportException(Error_Incorrect_method_exponent_oversize, (RexxObject *)original, mathexp);
}
}
}
if (decimals == (size_t)-1)
{ /* default decimal processing? */
if (this->exp < 0) /* negative exponent? */
{
decimals = -this->exp; /* get number of decimals */
}
}
else
{
if (this->exp < 0)
{ /* have actual decimals? */
adjust = -this->exp; /* get absolute value */
if ((size_t)adjust > decimals)
{ /* need to round or truncate? */
adjust = adjust - decimals; /* get the difference */
/* adjust exponent */
this->exp = this->exp + adjust;
if (adjust >= (wholenumber_t)this->length)
{ /* Losing all digits? need rounding */
/* is rounding needed? */
if (adjust == (wholenumber_t)this->length && this->number[0] >= 5)
{
this->number[0] = 1; /* round up */
}
else
{
this->number[0] = 0; /* round down */
this->exp = 0; /* nothing left at all */
this->sign = 1; /* suppress a negative sign */
}
this->length = 1; /* just one digit left */
}
/* Need to round? */
else
{ /* truncating, need to check rounding*/
temp = decimals - adjust; /* get the difference */
/* adjust the length */
this->length = this->length - adjust;
/* go round this number */
this->mathRound(this->number);
/* calculate new adjusted value */
/* undo the previous exponent calculation */
/* needed for format(.999999,,4,2,2) */
if (mathexp != 0 && expfactor != 0)
{
this->exp += expfactor;
expfactor = 0;
strcpy(exponent, "0");
exponentsize = strlen(exponent);
}
temp = this->exp + this->length - 1;
/* did rounding trigger the */
/* exponential form? */
if (mathexp != 0 && (temp >= (wholenumber_t)exptrigger || (size_t)Numerics::abs(this->exp) > exptrigger * 2))
{
/* yes, request for */
if (form == Numerics::FORM_ENGINEERING)
{
/* Engineering notation fmt? */
if (temp < 0) /* yes, is it a whole number? */
temp = temp - 2; /* no, force two char adjust to left */
temp = (temp / 3) * 3; /* get count right of decimal point */
}
/* adjust the exponent */
this->exp = this->exp - temp;
/* adjust the exponent factor */
expfactor = expfactor + temp;
/* format exponent to a string */
Numerics::formatWholeNumber(Numerics::abs(expfactor), exponent);
/* get the number of digits needed */
exponentsize = strlen(exponent);
if (mathexp == (size_t)-1) /* default exponent size? */
{
mathexp = exponentsize; /* use actual length */
}
if (exponentsize > mathexp)/* not enough room? */
{
reportException(Error_Incorrect_method_exponent_oversize, original, mathexp);
}
}
}
}
}
}
if (integers == (size_t)-1)
{ /* default integers requested */
if (this->exp >= 0) /* non-negative exponent? */
{
/* use all of number */
integers = this->length + this->exp;
}
else
{
/* no integer part? */
if ((size_t)Numerics::abs(this->exp) > this->length)
{
integers = 1; /* just the leading zero */
}
else /* get the integer part */
{
integers = this->length + this->exp;
}
}
}
else
{ /* user requested size */
reqIntegers = integers; /* save integers */
if (this->sign == -1) /* negative number? */
{
integers = integers - 1; /* the sign takes up one spot */
}
if (this->exp >= 0) /* non-negative exponent? */
{
temp = this->length + this->exp; /* use all of number */
}
else
{
/* no integer part? */
if ((size_t)Numerics::abs(this->exp) > this->length)
{
temp = 1; /* just the leading zero */
}
else
{
/* get the integer part */
temp = this->length + this->exp;
}
}
if ((wholenumber_t)integers < temp) /* not enough room? */
{
/* this is an error */
reportException(Error_Incorrect_method_before_oversize, original, reqIntegers);
}
}
size = 0; /* start with a null string */
leadingSpaces = 0; /* no leading spaces yet */
temp = this->exp + this->length; /* get adjusted length */
if (temp != (wholenumber_t)integers)
{ /* need leading spaces? */
if (temp > 0) /* have leading part? */
{
leadingSpaces = integers - temp; /* get leading length */
}
else
{
leadingSpaces = integers - 1; /* leave space for leading 0 */
}
size += leadingSpaces; /* fill in the spaces */
}
if (this->sign == -1) /* negative number? */
{
size++; /* space for the sign */
}
if (temp <= 0)
{ /* no integer portion? */
size += 2; /* add room for zero and decimal */
leadingZeros = -temp; /* get positive of zeros */
size += leadingZeros; /* add in the zeros size */
if (this->length > 0) /* have a length? */
{
size += this->length; /* add on the actual data */
}
/* need more zeros? */
if (leadingZeros + this->length < decimals)
{
/* get the trailing count */
trailingZeros = decimals - (leadingZeros + this->length);
size += trailingZeros; /* add them on */
}
else
{
trailingZeros = 0; /* no trailing zeros */
}
}
else if (temp >= (wholenumber_t)this->length)
{ /* all integer data? */
size += this->length; /* add on the digits */
/* reduce total length */
trailingZeros = temp - this->length;
size += trailingZeros; /* add this to the total size */
if (decimals > 0) /* decimals needed? */
{
size += decimals + 1; /* add decimal point and trailers */
}
}
else
{ /* partial decimal number */
size += this->length + 1; /* need the length plus a decimal */
/* get needed extra zeroes */
trailingZeros = decimals - (this->length - temp);
size += trailingZeros; /* add that to the size */
if ((wholenumber_t)trailingZeros < 0)
{
this->length += trailingZeros;
this->exp -= trailingZeros;
trailingZeros = 0;
}
}
if (expfactor != 0)
{ /* exponential value? */
size += 2; /* add on the E and the sign */
/* get extra size needed */
leadingExpZeros = mathexp - exponentsize;
size += mathexp; /* add on the total exponent size */
}
/* spaces needed for exp.? */
else if (mathexp > 0 && !defaultexpsize && temp > (wholenumber_t)exptrigger)
{
size += mathexp + 2; /* add on the spaces needed */
}
result = raw_string(size); /* get an empty string to start */
resultPtr = result->getWritableData();
temp = this->exp + this->length; /* get adjusted length */
if (leadingSpaces != 0)
{ /* need leading spaces? */
/* fill them in */
memset(resultPtr, ' ', leadingSpaces);
resultPtr += leadingSpaces; /* and step past them */
}
if (this->sign == -1) /* negative number? */
{
*resultPtr++ = '-'; /* add the sign */
}
if (temp <= 0)
{ /* no integer portion? */
strcpy(resultPtr, "0."); /* add the leading zero and decimal */
resultPtr += 2; /* and step past them */
if (leadingZeros != 0)
{ /* zeroes needed? */
/* fill them in */
memset(resultPtr, '0', leadingZeros);
resultPtr += leadingZeros; /* and step past them */
}
if (this->length > 0)
{ /* have a length? */
/* fill in the remaining part */
fill_digits(resultPtr, this->number, this->length);
resultPtr += this->length; /* step over the digits */
}
if (trailingZeros != 0)
{ /* need more zeros? */
/* fill them in */
memset(resultPtr, '0', trailingZeros);
resultPtr += trailingZeros; /* and step past them */
}
}
else if (temp >= (wholenumber_t)this->length)
{/* all integer data? */
/* fill in the remaining part */
fill_digits(resultPtr, this->number, this->length);
resultPtr += this->length; /* step over the digits */
if (trailingZeros != 0)
{ /* need more zeros? */
/* fill them in */
memset(resultPtr, '0', trailingZeros);
resultPtr += trailingZeros; /* and step past them */
}
if ((wholenumber_t)decimals > 0)
{ /* decimals needed? */
*resultPtr++ = '.'; /* add the period */
memset(resultPtr, '0', decimals);/* fill them in */
resultPtr += decimals; /* and step past them */
}
}
else
{ /* partial decimal number */
/* fill in the leading part */
fill_digits(resultPtr, this->number, temp);
resultPtr += temp; /* step over the digits */
*resultPtr++ = '.'; /* add the period */
/* fill in the trailing part */
fill_digits(resultPtr, this->number + temp, this->length - temp);
resultPtr += this->length - temp; /* step over the extra part */
if ((wholenumber_t)trailingZeros > 0)
{ /* extra decimals needed? */
/* fill them in */
memset(resultPtr, '0', trailingZeros);
resultPtr += trailingZeros; /* and step past them */
}
}
if (expfactor != 0)
{ /* exponential value? */
*resultPtr++ = 'E'; /* fill in the notation character */
if (expfactor > 0) /* positive exponent? */
{
*resultPtr++ = '+'; /* add the plus sign */
}
else
{
*resultPtr++ = '-'; /* a minus sign is required */
}
if (leadingExpZeros > 0)
{ /* need extras? */
/* fill them in */
memset(resultPtr, '0', leadingExpZeros);
resultPtr += leadingExpZeros; /* and step past them */
}
/* now add on the exponent */
memcpy(resultPtr, exponent, exponentsize);
}
/* blanks needed instead? */
else if (mathexp > 0 && !defaultexpsize && temp > (wholenumber_t)exptrigger)
{
/* fill them in */
memset(resultPtr, ' ', mathexp + 2);
resultPtr += mathexp; /* and step past them */
/* add on the spaces */
}
return result; /* return the result */
}
int RexxNumberString::format(const char *_number, size_t _length)
/******************************************************************************/
/* Function : Format the string data into a numberstring. */
/* NOTE: now that a scan is done first the is some cleanup that can*/
/* be done, since we can make some assumptions about certain */
/* data/chars being valid. */
/* */
/* Returned: 0 if input was valid number */
/* 1 if input was invalid number */
/******************************************************************************/
{
int ExpSign; /* Exponent Sign */
wholenumber_t ExpValue; /* Exponent Value */
size_t MaxDigits; /* Maximum number size */
char ch; /* current character */
char MSDigit = 0; /* Most Significant digit truncated */
const char *InPtr; /* Input Data Pointer */
char *OutPtr; /* Output Data Pointer */
const char *EndData; /* Scan end position */
bool isZero; /* Number is zero if true */
size_t resultDigits; /* Number of digits in result */
ExpValue = 0; /* Initial Exponent. */
ExpSign = 0; /* set exponent sign */
isZero = true; /* Assume number will be zero. */
InPtr = _number; /*Point to start of input string. */
EndData = InPtr + _length; /*Point to end of Data + 1. */
while (*InPtr == ch_BLANK || *InPtr == ch_TAB) /* Ship all leading blanks. */
{
InPtr++; /* Skip it, and go on to next char */
}
/* Is this a sign Character? */
if ((ch = *InPtr) == ch_MINUS || ch == ch_PLUS)
{
InPtr++; /* Yes, skip it. */
if (ch == ch_MINUS) /* is it a Minus sign? */
{
this->sign = -1; /* Yup, indicate a negative number. */
}
}
while (*InPtr == ch_BLANK || *InPtr == ch_TAB) /* Ship all leading blanks. */
{
InPtr++; /* Skip it, and go on to next char */
}
ch = *InPtr; /* Get 1st Digit. */
MaxDigits = resultDigits = _length; /* Set our max digits counter. */
OutPtr = this->number; /* Point to Output area. */
/*Skip all leading Zero's */
while (*InPtr == ch_ZERO) /* While 1st Digit is a 0 */
{
InPtr++; /* Go to next character. */
}
/* Have we reach end of number,num */
/*zero? */
if (InPtr >= EndData)
{
SetNumberStringZero(); /* Make value a zero. */
return 0;
}
/* Now process real digits. */
ExpValue = 0; /* Start accumulating exponent */
if (*InPtr > ch_ZERO && *InPtr <= ch_NINE)
{
isZero = false; /* found the first non-zero digit */
}
/* While the character is a Digit. */
while (*InPtr >= ch_ZERO && *InPtr <= ch_NINE)
{
if (MaxDigits)
{ /* Still room to add digits */
/* Move digit into output. */
*OutPtr++ = (char)(*InPtr++ - '0');
MaxDigits--; /* Now room for one less. */
}
else
{
/* Have we found our most Sig Digit */
/* and have we not run out of data? */
if ((!MSDigit) && (InPtr < EndData))
{
MSDigit = *InPtr; /* Nope, now we have MSD. */
}
InPtr++; /* Point to next char */
ExpValue++; /* Increment the exponent value. */
}
}
if (InPtr >= EndData)
{ /* Did we reach end of data? */
/* compute length. */
this->length = (size_t) (resultDigits - MaxDigits);
this->exp = ExpValue; /* set exponent value */
this->roundUp(MSDigit);
this->roundUp(MSDigit); /* Round up the number if necessary */
return 0; /* all done, just return */
}
/* compute length. */
this->length = (resultDigits - MaxDigits);
this->exp = ExpValue; /* set exponent value */
if (*InPtr == ch_PERIOD)
{ /*Decimal point??? */
InPtr++; /* yes, skip it. */
if (InPtr >= EndData)
{ /* Did we reach end of data */
/* Yes, valid digits continue. */
/*is it "0.", or number Zero */
if (MaxDigits == resultDigits || isZero)
{
this->setZero(); /* make number just zero. */
}
else
{
/* Round up the number if necessary */
this->roundUp(MSDigit);
}
return 0; /* All done, exit. */
}
if (MaxDigits == resultDigits)
{ /*Any significant digits? */
/* No, Ship leading Zeros */
while (*InPtr == ch_ZERO)
{ /* While 1st Digit is a 0 */
ExpValue--; /* decrement exponent. */
InPtr++; /* Go to next character. */
/* Have we reach end of number,num */
/*zero? */
if (InPtr >= EndData)
{
SetNumberStringZero(); /* Make value a zero. */
return 0;
}
}
}
/* in the range 1-9? */
if (*InPtr > ch_ZERO && *InPtr <= ch_NINE)
{
isZero = false; /* found the first non-zero digit */
}
/*While there are still digits */
while (*InPtr >= ch_ZERO && *InPtr <= ch_NINE)
{
if (MaxDigits)
{ /*if still room for digits */
ExpValue--; /* Decrement Exponent */
/* Move char to output */
*OutPtr++ = (char)(*InPtr++ - '0');
MaxDigits--; /* Room for one less digit. */
}
else
{
if (!MSDigit) /* not gotten a most sig digit yet? */
{
MSDigit = *InPtr; /* record this one */
}
InPtr++; /* No more room, go to next digit */
}
}
if (InPtr >= EndData)
{ /*Are we at end of data? */
/* Compute length of number. */
this->length = (resultDigits - MaxDigits);
this->exp = ExpValue; /* get exponent. */
/* Round up the number if necessary */
this->roundUp(MSDigit);
return 0; /* All done, return */
}
} /* End is it a Decimal point. */
/* At this point we are don copying */
/* digits. We are just looking for */
/* exponent value if any and any */
/*trailing blanks */
/* Get final length of number. */
this->length = resultDigits - MaxDigits;
if (!this->length)
{ /* No digits, number is Zero. */
/* Have we reached the end of the */
/*string */
if (InPtr >= EndData)
{
/* Yes, all done. */
this->setZero(); /* make number just zero. */
return 0; /* All done, exit. */
}
}
this->exp = ExpValue; /* get current exponent value. */
if (toupper(*InPtr) == 'E')
{ /* See if this char is an exponent? */
ExpSign = 1; /* Assume sign of exponent to '+' */
InPtr++; /* step over the 'E' */
if (*InPtr == ch_MINUS)
{ /* If this a minus sign? */
ExpSign = -1; /* Yes, make sign of exp '-' */
InPtr++; /* go on to next char. */
}
else if (*InPtr == ch_PLUS) /* If this a plus sign? */
{
InPtr++; /* Yes, skip it and go to next char. */
}
ExpValue = 0; /* Start of exponent clear work area.*/
MaxDigits = 0; /* claer digit counter. */
/* Do while we have a valid digit */
while (*InPtr >= ch_ZERO && *InPtr <= ch_NINE)
{
/* Add this digit to Exponent value. */
ExpValue = ExpValue * 10 + ((*InPtr++) - '0');
if (ExpValue > Numerics::MAX_EXPONENT) /* Exponent can only be 9 digits long*/
{
return 1; /* if more than that, indicate error.*/
}
if (ExpValue) /* Any significance in the Exponent? */
{
MaxDigits++; /* Yes, bump up the digits counter. */
}
}
this->exp += (ExpValue * ExpSign);/* Compute real exponent. */
/* Is it bigger than allowed max */
if (Numerics::abs(this->exp) > Numerics::MAX_EXPONENT)
{
return 1; /* yes, indicate error. */
}
}
if (this->sign == 0 || isZero)
{ /* Was this really a zero number? */
this->setZero(); /* make number just zero. */
}
this->roundUp(MSDigit); /* Round up the number if necessary */
/*is number just flat out too big? */
if ((this->exp + (wholenumber_t)this->length - 1) > Numerics::MAX_EXPONENT)
{
return 1; /* also bad */
}
return 0; /* All done !! */
}
void RexxNumberString::formatNumber(wholenumber_t integer)
/******************************************************************************/
/* Function : Format the integer num into a numberstring. */
/******************************************************************************/
{
if (integer == 0)
{ /* is integer 0? */
/* indicate this. */
this->setZero();
}
else
{ /* number is non-zero */
/* Format the number */
if (integer < 0 )
{ /* Negative integer number? */
this->sign = -1;
}
/* convert value into string */
this->length = Numerics::normalizeWholeNumber(integer, (char *)this->number);
}
}
void RexxNumberString::formatUnsignedNumber(size_t integer)
/******************************************************************************/
/* Function : Format the integer num into a numberstring. */
/******************************************************************************/
{
char *current; /* current position */
if (integer == 0)
{ /* is integer 0? */
/* indicate this. */
this->setZero();
}
else
{ /* number is non-zero */
/* Format the number */
/* convert value into string */
Numerics::formatStringSize(integer, (char *)this->number);
current = this->number; /* point to the data start */
while (*current != '\0')
{ /* while still have digits */
*current -= '0'; /* make zero based */
current++; /* step to the next one */
}
/* set the proper length */
this->length = current - this->number;
}
}
void RexxNumberString::formatInt64(int64_t integer)
/******************************************************************************/
/* Function : Format the integer num into a numberstring. */
/******************************************************************************/
{
if (integer == 0)
{ /* is integer 0? */
/* indicate this. */
this->setZero();
}
else
{ /* number is non-zero */
// we convert this directly because portable numeric-to-ascii routines
// don't really exist for the various 32/64 bit values.
char buffer[32];
size_t index = sizeof(buffer);
// negative number? copy a negative sign, and take the abs value
if (integer < 0)
{
// work from an unsigned version that can hold all of the digits
// we need to use a version we can negate first, then add the
// digit back in
uint64_t working = (uint64_t)(-(integer + 1));
working++; // undoes the +1 above
sign = -1; // negative number
while (working > 0)
{
// get the digit and reduce the size of the integer
int digit = (int)(working % 10);
working = working / 10;
// store the digit
buffer[--index] = digit;
}
}
else
{
sign = 1; // positive number
while (integer > 0)
{
// get the digit and reduce the size of the integer
int digit = (int)(integer % 10);
integer = integer / 10;
// store the digit
buffer[--index] = digit;
}
}
// copy into the buffer and set the length
this->length = sizeof(buffer) - index;
memcpy(this->number, &buffer[index], this->length);
}
}
void RexxNumberString::formatUnsignedInt64(uint64_t integer)
/******************************************************************************/
/* Function : Format the integer num into a numberstring. */
/******************************************************************************/
{
if (integer == 0)
{ /* is integer 0? */
/* indicate this. */
this->setZero();
}
else
{ /* number is non-zero */
// we convert this directly because A) we need to post-process the numbers
// to make them zero based, and B) portable numeric-to-ascii routines
// don't really exist for the various 32/64 bit values.
char buffer[32];
size_t index = sizeof(buffer);
while (integer > 0)
{
// get the digit and reduce the size of the integer
int digit = (int)(integer % 10);
integer = integer / 10;
// store the digit
buffer[--index] = digit;
}
// copy into the buffer and set the length
this->length = sizeof(buffer) - index;
memcpy(this->number, &buffer[index], this->length);
}
}
RexxObject *RexxNumberString::unknown(RexxString *msgname, RexxArray *arguments)
/******************************************************************************/
/* Function: Forward all unknown messages to the numberstring's string */
/* representation */
/******************************************************************************/
{
return this->stringValue()->sendMessage(msgname, arguments);
}
/**
* Override for the normal isinstanceof method. This version
* allows the NumberStringClass to "lie" about being a string.
*
* @param other The comparison class
*
* @return True if the string value is an instance of the target class.
*/
bool RexxNumberString::isInstanceOf(RexxClass *other)
{
return stringValue()->isInstanceOf(other);
}
/**
* Retrieve the method instance for an object's defined method.
*
* @param method_name
* The method name.
*
* @return The method object that implements the object method.
*/
RexxMethod *RexxNumberString::instanceMethod(RexxString *method_name)
{
return stringValue()->instanceMethod(method_name);
}
/**
* Return a supplier containing the methods implemented by an
* object. Depending on the argument, this is either A) all of
* the methods, B) just the explicitly set instance methods, or
* C) the methods provided by a given class.
*
* @param class_object
* The target class object (optional).
*
* @return A supplier with the appropriate method set.
*/
RexxSupplier *RexxNumberString::instanceMethods(RexxClass *class_object)
{
return stringValue()->instanceMethods(class_object);
}
RexxString *RexxNumberString::concatBlank(RexxObject *other)
/******************************************************************************/
/* Function: Blank concatenation operator */
/******************************************************************************/
{
return this->stringValue()->concatBlank(other);
}
RexxString *RexxNumberString::concat(RexxObject *other)
/******************************************************************************/
/* Function: Normal concatentation operator */
/******************************************************************************/
{
return this->stringValue()->concatRexx(other);
}
/* numberstring operator forwarders */
/* to process string operators */
RexxObject *RexxNumberString::orOp(RexxObject *operand)
{
return (RexxObject *)this->stringValue()->orOp(operand);
}
RexxObject *RexxNumberString::andOp(RexxObject *operand)
{
return (RexxObject *)this->stringValue()->andOp(operand);
}
RexxObject *RexxNumberString::xorOp(RexxObject *operand)
{
return (RexxObject *)this->stringValue()->xorOp(operand);
}
bool RexxNumberString::isEqual(
RexxObject *other) /* other comparison object */
/******************************************************************************/
/* Function: Primitive strict equal\not equal method. This determines */
/* only strict equality, not greater or less than values. */
/******************************************************************************/
{
if (this->isSubClassOrEnhanced()) /* not a primitive? */
{
/* do the full lookup compare */
return this->sendMessage(OREF_STRICT_EQUAL, other)->truthValue(Error_Logical_value_method);
}
/* go do a string compare */
return this->stringValue()->isEqual(other);
}
wholenumber_t RexxNumberString::strictComp(RexxObject *other)
/******************************************************************************/
/* Function: Compare the two values. */
/* */
/* Returned: return <0 if other is greater than this */
/* return 0 if this equals other */
/* return >0 if this is greater than other */
/******************************************************************************/
{
/* the strict compare is done against*/
/* strings only, so convert to string*/
/* and let string do this compare. */
return this->stringValue()->strictComp(other);
}
wholenumber_t RexxNumberString::comp(
RexxObject *right) /* right hand side of compare */
/******************************************************************************/
/* Function: Do a value comparison of two number strings for the non-strict */
/* comparisons. This returns for the compares: */
/* */
/* a value < 0 when this is smaller than other */
/* a value 0 when this is equal to other */
/* a value > 0 when this is larger than other */
/******************************************************************************/
{
RexxNumberString *rightNumber; /* converted right hand number */
wholenumber_t aLexp; /* adjusted left exponent */
wholenumber_t aRexp; /* adjusted right exponent */
size_t aLlen; /* adjusted left length */
size_t aRlen; /* adjusted right length */
wholenumber_t MinExp; /* minimum exponent */
size_t NumberDigits; /* current digits setting */
char *scan; /* scan pointer */
wholenumber_t rc; /* compare result */
/* the compare is acually done by */
/* subtracting the two numbers, the */
/* sign of the result obj will be our*/
/* return value. */
requiredArgument(right, ARG_ONE); /* make sure we have a real value */
/* get a numberstring object from */
/*right */
rightNumber = right->numberString();
if (rightNumber == OREF_NULL) /* didn't convert? */
{
/* numbers couldn't be compared */
/* numerically, do a string compare. */
return this->stringValue()->comp(right);
}
if (this->sign != rightNumber->sign) /* are numbers the same sign? */
{
/* no, this is easy */
return(this->sign < rightNumber->sign) ? -1 : 1;
}
if (rightNumber->sign == 0) /* right one is zero? */
{
return this->sign; /* use this sign */
}
if (this->sign == 0) /* am I zero? */
{
return rightNumber->sign; /* return the right sign */
}
/* set smaller exponent */
MinExp = (rightNumber->exp < this->exp)? rightNumber->exp : this->exp;
aLexp = this->exp - MinExp; /* get adjusted left size */
aRexp = rightNumber->exp - MinExp; /* get adjusted right size */
aLlen = aLexp + this->length; /* get adjusted left size */
aRlen = aRexp + rightNumber->length; /* get adjusted right size */
NumberDigits = number_fuzzydigits(); /* get precision for comparisons. */
/* can we do a fast exit? */
if (aLlen <= NumberDigits && aRlen <= NumberDigits)
{
/* longer number is the winner */
if (aLlen > aRlen) /* left longer? */
{
return this->sign; /* use left sign */
}
else if (aRlen > aLlen) /* right longer? */
{
return -this->sign; /* use inverse of the sign */
}
else
{
/* actual lengths the same? */
if (this->length == rightNumber->length)
{
/* return the comparison result */
/* adjusted by the sign value */
return memcmp(this->number, rightNumber->number, this->length) * this->sign;
}
/* right one shorter? */
else if (this->length > rightNumber->length)
{
/* compare for shorter length */
rc = memcmp(this->number, rightNumber->number, rightNumber->length) * this->sign;
if (rc == 0)
{ /* equal for that length? */
/* point to the remainder part */
scan = this->number + rightNumber->length;
/* get the remainder length */
aRlen = this->length - rightNumber->length;
while (aRlen--)
{ /* scan the remainder */
if (*scan++ != 0) /* found a non-zero one? */
{
return this->sign; /* left side is greater */
}
}
return 0; /* these are equal */
}
return rc; /* return compare result */
}
else
{ /* left one is shorter */
/* compare for shorter length */
rc = memcmp(this->number, rightNumber->number, this->length) * this->sign;
if (rc == 0)
{ /* equal for that length? */
/* point to the remainder part */
scan = rightNumber->number + this->length;
/* get the remainder length */
aRlen = rightNumber->length - this->length;
while (aRlen--)
{ /* scan the remainder */
if (*scan++ != 0) /* found a non-zero one? */
{
return -this->sign; /* right side is greater */
}
}
return 0; /* these are equal */
}
return rc; /* return compare result */
}
}
}
else
{ /* need to subtract these */
/* call addsub to do computation */
rightNumber = this->addSub(rightNumber, OT_MINUS, number_fuzzydigits());
return rightNumber->sign; /* compare result is subtract sign */
}
}
RexxInteger *RexxNumberString::equal(RexxObject *other)
/******************************************************************************/
/* Function: non-strict "=" operator */
/******************************************************************************/
{
if (other == TheNilObject) // all conditionals return .false when compared to .nil
{
return TheFalseObject;
}
return (this->comp(other) == 0) ? TheTrueObject : TheFalseObject;
}
RexxInteger *RexxNumberString::notEqual(RexxObject *other)
/******************************************************************************/
/* Function: non-strict "\=" operator */
/******************************************************************************/
{
if (other == TheNilObject) // all conditionals return .false when compared to .nil
{
return TheTrueObject;
}
return (this->comp(other) != 0) ? TheTrueObject : TheFalseObject;
}
RexxInteger *RexxNumberString::isGreaterThan(RexxObject *other)
/******************************************************************************/
/* Function: non-strict ">" operator */
/******************************************************************************/
{
if (other == TheNilObject) // all conditionals return .false when compared to .nil
{
return TheFalseObject;
}
return (this->comp(other) > 0) ? TheTrueObject : TheFalseObject;
}
RexxInteger *RexxNumberString::isLessThan(RexxObject *other)
/******************************************************************************/
/* Function: non-strict "<" operator */
/******************************************************************************/
{
if (other == TheNilObject) // all conditionals return .false when compared to .nil
{
return TheFalseObject;
}
return (this->comp(other) < 0) ? TheTrueObject : TheFalseObject;
}
RexxInteger *RexxNumberString::isGreaterOrEqual(RexxObject *other)
/******************************************************************************/
/* Function: non-strict ">=" operator */
/******************************************************************************/
{
if (other == TheNilObject) // all conditionals return .false when compared to .nil
{
return TheFalseObject;
}
return (this->comp(other) >= 0) ? TheTrueObject : TheFalseObject;
}
RexxInteger *RexxNumberString::isLessOrEqual(RexxObject *other)
/******************************************************************************/
/* Function: non-strict "<=" operator */
/******************************************************************************/
{
if (other == TheNilObject) // all conditionals return .false when compared to .nil
{
return TheFalseObject;
}
return (this->comp(other) <= 0) ? TheTrueObject : TheFalseObject;
}
/**
* Exported version of the HASHCODE method for retrieving the
* object's hash.
*
* @return A string version of the hash (generally holds binary characters).
*/
RexxObject *RexxNumberString::hashCode()
{
// get the hash value, which is actually derived from the integer string value
HashCode h = this->hash();
return new_string((const char *)&h, sizeof(HashCode));
}
RexxInteger *RexxNumberString::strictEqual(RexxObject *other)
/******************************************************************************/
/* Function: Perform the primitive level "==" compare, including the hash */
/* value processing. */
/******************************************************************************/
{
if (other == TheNilObject) // all conditionals return .false when compared to .nil
{
return TheFalseObject;
}
return (this->strictComp(other) == 0) ? TheTrueObject : TheFalseObject;
}
RexxInteger *RexxNumberString::strictNotEqual(RexxObject *other)
/******************************************************************************/
/* Function: Strict inequality operation */
/******************************************************************************/
{
if (other == TheNilObject) // all conditionals return .false when compared to .nil
{
return TheTrueObject;
}
return (this->strictComp(other) != 0) ? TheTrueObject : TheFalseObject;
}
RexxInteger *RexxNumberString::strictGreaterThan(RexxObject *other)
/******************************************************************************/
/* Function: strict ">>" operator */
/******************************************************************************/
{
if (other == TheNilObject) // all conditionals return .false when compared to .nil
{
return TheFalseObject;
}
return (this->strictComp(other) > 0) ? TheTrueObject : TheFalseObject;
}
RexxInteger *RexxNumberString::strictLessThan(RexxObject *other)
/******************************************************************************/
/* Function: strict "<<" operator */
/******************************************************************************/
{
if (other == TheNilObject) // all conditionals return .false when compared to .nil
{
return TheFalseObject;
}
return (this->strictComp(other) < 0) ? TheTrueObject : TheFalseObject;
}
RexxInteger *RexxNumberString::strictGreaterOrEqual(RexxObject *other)
/******************************************************************************/
/* Function: strict ">>=" operator */
/******************************************************************************/
{
if (other == TheNilObject) // all conditionals return .false when compared to .nil
{
return TheFalseObject;
}
return (this->strictComp(other) >= 0) ? TheTrueObject : TheFalseObject;
}
RexxInteger *RexxNumberString::strictLessOrEqual(RexxObject *other)
/******************************************************************************/
/* Function: strict "<<=" operator */
/******************************************************************************/
{
if (other == TheNilObject) // all conditionals return .false when compared to .nil
{
return TheFalseObject;
}
return (this->strictComp(other) <= 0) ? TheTrueObject : TheFalseObject;
}
RexxNumberString *RexxNumberString::plus(RexxObject *right)
/********************************************************************/
/* Function: Add two number strings */
/********************************************************************/
{
if (right != OREF_NULL)
{ /* Is this a dyadic operation? */
/* get a numberstring object from */
/*right */
RexxNumberString *rightNumber = right->numberString();
if (rightNumber == OREF_NULL) /* is the operand numeric? */
{
/* nope, this is an error */
reportException(Error_Conversion_operator, right);
}
/* call addsub to do computation */
return this->addSub(rightNumber, OT_PLUS, number_digits());
}
else
{
/* need to format under different */
/* precision? */
if (this->stringObject != OREF_NULL || this->NumDigits != number_digits() ||
(number_form() == Numerics::FORM_SCIENTIFIC && !(this->NumFlags&NumFormScientific)) ||
(number_form() == Numerics::FORM_ENGINEERING && this->NumFlags&NumFormScientific))
{
/* need to copy and reformat */
return this->prepareNumber(number_digits(), ROUND);
}
else
{
return this; /* just return the same value */
}
}
}
RexxNumberString *RexxNumberString::minus(RexxObject *right)
/********************************************************************/
/* Function: Subtraction between two numbers */
/********************************************************************/
{
if (right != OREF_NULL)
{ /* Is this a dyadic operation? */
/* get a numberstring object from */
/*right */
RexxNumberString *rightNumber = right->numberString();
if (rightNumber == OREF_NULL) /* is the operand numeric? */
{
/* nope, this is an error */
reportException(Error_Conversion_operator, right);
}
/* call addsub to do computation */
return this->addSub(rightNumber, OT_MINUS, number_digits());
}
else
{
/* need to copy and reformat */
RexxNumberString *result = this->prepareNumber(number_digits(), ROUND);
/* invert the sign of our copy. */
result->sign = -(result->sign);
return result; /* return addition result */
}
}
RexxNumberString *RexxNumberString::multiply(RexxObject *right)
/********************************************************************/
/* Function: Multiply two numbers */
/********************************************************************/
{
requiredArgument(right, ARG_ONE); /* must have an argument */
/* get a numberstring object from */
/*right */
RexxNumberString *rightNumber = right->numberString();
if (rightNumber == OREF_NULL) /* is the operand numeric? */
{
/* nope, this is an error */
reportException(Error_Conversion_operator, right);
}
return this->Multiply(rightNumber); /* go do the multiply */
}
RexxNumberString *RexxNumberString::divide(RexxObject *right)
/********************************************************************/
/* Function: Divide two numbers */
/********************************************************************/
{
requiredArgument(right, ARG_ONE); /* must have an argument */
/* get a numberstring object from */
/*right */
RexxNumberString *rightNumber = right->numberString();
if (rightNumber == OREF_NULL) /* is the operand numeric? */
{
/* nope, this is an error */
reportException(Error_Conversion_operator, right);
}
/* go do the division */
return this->Division(rightNumber, OT_DIVIDE);
}
RexxNumberString *RexxNumberString::integerDivide(RexxObject *right)
/********************************************************************/
/* Function: Integer division between two numbers */
/********************************************************************/
{
requiredArgument(right, ARG_ONE); /* must have an argument */
/* get a numberstring object from */
/*right */
RexxNumberString *rightNumber = right->numberString();
if (rightNumber == OREF_NULL) /* is the operand numeric? */
{
/* nope, this is an error */
reportException(Error_Conversion_operator, right);
}
/* go do the division */
return this->Division(rightNumber, OT_INT_DIVIDE);
}
RexxNumberString *RexxNumberString::remainder(RexxObject *right)
/********************************************************************/
/* Function: Remainder division between two numbers */
/********************************************************************/
{
requiredArgument(right, ARG_ONE); /* must have an argument */
/* get a numberstring object from */
/*right */
RexxNumberString *rightNumber = right->numberString();
if (rightNumber == OREF_NULL) /* is the operand numeric? */
{
/* nope, this is an error */
reportException(Error_Conversion_operator, right);
}
/* go do the division */
return this->Division(rightNumber, OT_REMAINDER);
}
RexxNumberString *RexxNumberString::abs()
/********************************************************************/
/* Function: Return the absolute value of a number */
/********************************************************************/
{
RexxNumberString *NewNumber = this->clone(); /* copy the number */
/* inherit the current numeric settings and perform rounding, if */
/* necessary */
NewNumber->setupNumber();
/* switch the sign */
NewNumber->sign = (short)::abs(NewNumber->sign);
return NewNumber; /* and return */
}
RexxInteger *RexxNumberString::Sign()
/********************************************************************/
/* Function: Return the sign of a number */
/********************************************************************/
{
RexxNumberString *NewNumber = this->clone(); /* copy the number */
/* inherit the current numeric settings and perform rounding, if */
/* necessary */
NewNumber->setupNumber();
return new_integer(NewNumber->sign); /* just return the sign value */
}
RexxObject *RexxNumberString::notOp()
/********************************************************************/
/* Function: Logical not of a number string value */
/********************************************************************/
{
return this->stringValue()->notOp();
}
RexxObject *RexxNumberString::operatorNot(RexxObject *right)
/********************************************************************/
/* Function: Polymorphic NOT operator method */
/********************************************************************/
{
return this->stringValue()->notOp();
}
RexxNumberString *RexxNumberString::Max(
RexxObject **args, /* array of comparison values */
size_t argCount) /* count of arguments */
/********************************************************************/
/* Function: Process MAX function */
/********************************************************************/
{
return this->maxMin(args, argCount, OT_MAX);
}
RexxNumberString *RexxNumberString::Min(
RexxObject **args, /* array of comparison values */
size_t argCount) /* count of arguments */
/********************************************************************/
/* Function: Process the MIN function */
/********************************************************************/
{
return this->maxMin(args, argCount, OT_MIN);
}
RexxObject *RexxNumberString::isInteger()
/******************************************************************************/
/* This method determines if the formatted numberstring is s true integer */
/* string. That is, its not of the form 1.00E3 but 10000 */
/******************************************************************************/
{
/* first convert number to formatted */
/* objetc, "make it a string" */
/* now call string to check for */
return this->stringValue()->isInteger();
}
/*********************************************************************/
/* Function: Round up a number as a result of the chopping*/
/* digits off of the number during init. */
/*********************************************************************/
void RexxNumberString::roundUp(int MSDigit)
{
int carry;
char *InPtr;
char ch;
/* Did we chop off digits and is it */
/* greater/equal to 5, rounding? */
if (MSDigit >= ch_FIVE && MSDigit <= ch_NINE)
{
/* yes, we have to round up digits */
carry = 1; /* indicate we have a carry. */
/* point to last digit. */
InPtr = this->number + this->length - 1;
/* all digits and still have a carry */
while ((InPtr >= this->number) && carry)
{
if (*InPtr == 9) /* Is this digit a 9? */
{
ch = 0; /* make digit 0, still have carry */
}
else
{
ch = *InPtr + 1; /* Not nine, just add one to digit. */
carry = 0; /* No more carry. */
}
*InPtr-- = ch; /* replace digit with new value. */
} /* All done rounding. */
if (carry)
{ /* Do we still have a carry? */
/* yes, carry rippled all the way */
*this->number = 1; /* set 1st digit to a 1. */
this->exp += 1; /* increment exponent by one. */
}
}
}
RexxString *RexxNumberString::d2x(
RexxObject *_length) /* result length */
/******************************************************************************/
/* Function: Convert a valid numberstring to a hex string. */
/******************************************************************************/
{
/* forward to the formatting routine */
return this->d2xD2c(_length, false);
}
RexxString *RexxNumberString::d2c(
RexxObject *_length) /* result length */
/******************************************************************************/
/* Function: Convert a valid numberstring to a character string. */
/******************************************************************************/
{
/* forward to the formatting routine */
return this->d2xD2c(_length, true);
}
RexxObject *RexxNumberString::evaluate(
RexxActivation *context, /* current activation context */
RexxExpressionStack *stack ) /* evaluation stack */
/******************************************************************************/
/* Function: Polymorphic method that makes numberstring a polymorphic */
/* expression term for literals */
/******************************************************************************/
{
stack->push(this); /* place on the evaluation stack */
/* trace if necessary */
context->traceIntermediate(this, TRACE_PREFIX_LITERAL);
return this; /* also return the result */
}
RexxString *RexxNumberString::d2xD2c(
RexxObject *_length, /* result length */
bool type ) /* D2C or D2X flag */
/******************************************************************************/
/* Function: Convert a valid numberstring to a hex or character string. */
/******************************************************************************/
{
char PadChar; /* needed padding character */
stringsize_t ResultSize; /* size of result string */
size_t HexLength; /* length of hex characters */
size_t BufferLength; /* length of the buffer */
char * Scan; /* scan pointer */
char * HighDigit; /* highest digit location */
char * Accumulator; /* accumulator pointer */
char * TempPtr; /* temporary pointer value */
size_t PadSize; /* needed padding */
size_t CurrentDigits; /* current digits setting */
size_t TargetLength; /* length of current number */
RexxBuffer *Target; /* formatted number */
RexxString *Retval; /* returned result */
/* get the target length */
ResultSize = optionalLengthArgument(_length, SIZE_MAX, ARG_ONE);
CurrentDigits = number_digits(); /* get the current digits setting */
TargetLength = this->length; /* copy the length */
/* too big to process? */
if (this->exp + this->length > CurrentDigits)
{
if (type == true) /* d2c form? */
{
/* use that message */
reportException(Error_Incorrect_method_d2c, this);
}
else /* use d2x form */
{
reportException(Error_Incorrect_method_d2x, this);
}
}
else if (this->exp < 0)
{ /* may have trailing zeros */
/* point to the decimal part */
TempPtr = this->number + this->length + this->exp;
HexLength = -this->exp; /* get the length to check */
/* point to the rounding digit */
HighDigit = this->number + CurrentDigits;
/* while more decimals */
while (HexLength -- && TempPtr <= HighDigit)
{
if (*TempPtr != 0)
{ /* non-zero decimal? */
/* this may be non-significant */
if (TargetLength > CurrentDigits)
{
/* this the "rounding" digit? */
if (TempPtr == HighDigit && *TempPtr < 5)
{
break; /* insignificant digit found */
}
}
if (type == true) /* d2c form? */
{
/* use that message */
reportException(Error_Incorrect_method_d2c, this);
}
else /* use d2x form */
{
reportException(Error_Incorrect_method_d2x, this);
}
}
TempPtr++; /* step the pointer */
}
/* adjust the length */
TargetLength = this->length + this->exp;
}
/* negative without a size */
if (this->sign < 0 && ResultSize == SIZE_MAX)
{
/* this is an error */
reportException(Error_Incorrect_method_d2xd2c);
}
if (ResultSize == SIZE_MAX) /* using default size? */
{
/* allocate buffer based on digits */
BufferLength = CurrentDigits + OVERFLOWSPACE;
}
else if (type == true)
{ /* X2C function? */
if (ResultSize * 2 < CurrentDigits)/* smaller than digits setting? */
{
/* allocate buffer based on digits */
BufferLength = CurrentDigits + OVERFLOWSPACE;
}
else /* allocate a large buffer */
{
BufferLength = (ResultSize * 2) + OVERFLOWSPACE;
}
}
else
{ /* D2X function */
if (ResultSize < CurrentDigits) /* smaller than digits setting? */
{
/* allocate buffer based on digits */
BufferLength = CurrentDigits + OVERFLOWSPACE;
}
else /* allocate a large buffer */
{
BufferLength = ResultSize + OVERFLOWSPACE;
}
}
Target = new_buffer(BufferLength); /* set up format buffer */
Scan = this->number; /* point to first digit */
/* set accumulator pointer */
Accumulator = Target->getData() + BufferLength - 2;
HighDigit = Accumulator - 1; /* set initial high position */
/* clear the accumulator */
memset(Target->getData(), '\0', BufferLength);
while (TargetLength--)
{ /* while more digits */
/* add next digit */
HighDigit = addToBaseSixteen(*Scan++, Accumulator, HighDigit);
if (TargetLength != 0) /* not last digit? */
{
/* do another multiply */
HighDigit = multiplyBaseSixteen(Accumulator, HighDigit);
}
}
if (this->exp > 0)
{ /* have extra digits to worry about? */
/* do another multiply */
HighDigit = multiplyBaseSixteen(Accumulator, HighDigit);
TargetLength = this->exp; /* copy the exponent */
while (TargetLength--)
{ /* while more digits */
/* add next zero digit */
HighDigit = addToBaseSixteen('\0', Accumulator, HighDigit);
if (TargetLength != 0) /* not last digit? */
{
/* do the multiply */
HighDigit = multiplyBaseSixteen(Accumulator, HighDigit);
}
}
}
HexLength = Accumulator - HighDigit; /* get accumulator length */
if (this->sign < 0)
{ /* have a negative number? */
/* take twos complement */
PadChar = 'F'; /* pad negatives with foxes */
Scan = Accumulator; /* point to last digit */
while (!*Scan) /* handle any borrows */
{
*Scan-- = 0xf; /* make digit a 15 */
}
*Scan = *Scan - 1; /* subtract the 1 */
Scan = Accumulator; /* start at first digit again */
while (Scan > HighDigit)
{ /* invert all the bits */
/* one digit at a time */
*Scan = (char)(*Scan ^ (unsigned)0x0f);
Scan--; /* step to next digit */
}
}
else
{
PadChar = '0'; /* pad positives with zero */
}
/* now make number printable */
Scan = Accumulator; /* start at first digit again */
while (Scan > HighDigit)
{ /* convert all the nibbles */
*Scan = IntToHexDigit(*Scan); /* one digit at a time */
Scan--; /* step to next digit */
}
Scan = HighDigit + 1; /* point to first digit */
if (type == false)
{ /* d2x function ? */
if (ResultSize == SIZE_MAX) /* using default length? */
{
ResultSize = HexLength; /* use actual data length */
}
}
else
{ /* d2c function */
if (ResultSize == SIZE_MAX) /* using default length? */
{
ResultSize = HexLength; /* use actual data length */
}
else
{
ResultSize += ResultSize; /* double the size */
}
}
if (ResultSize < HexLength)
{ /* need to truncate? */
PadSize = 0; /* no Padding */
Scan += HexLength - ResultSize; /* step the pointer */
HexLength = ResultSize; /* adjust number of digits */
}
else /* possible padding */
{
PadSize = ResultSize - HexLength; /* calculate needed padding */
}
if (PadSize)
{ /* padding needed? */
Scan -= PadSize; /* step back the pointer */
memset(Scan, PadChar, PadSize); /* pad in front */
}
if (type == true) /* need to pack? */
{
Retval = StringUtil::packHex(Scan, ResultSize);/* yes, pack to character */
}
else
{
/* allocate result string */
Retval = new_string(Scan, ResultSize);
}
return Retval; /* return proper result */
}
RexxObject *RexxNumberString::getValue(
RexxActivation *context) /* current activation context */
/******************************************************************************/
/* Function: Polymorphic get_value function used with expression terms */
/******************************************************************************/
{
return (RexxObject *)this; /* just return this value */
}
RexxObject *RexxNumberString::getValue(
RexxVariableDictionary *context) /* current activation context */
/******************************************************************************/
/* Function: Polymorphic get_value function used with expression terms */
/******************************************************************************/
{
return (RexxObject *)this; /* just return this value */
}
RexxObject *RexxNumberString::getRealValue(
RexxActivation *context) /* current activation context */
/******************************************************************************/
/* Function: Polymorphic get_value function used with expression terms */
/******************************************************************************/
{
return (RexxObject *)this; /* just return this value */
}
RexxObject *RexxNumberString::getRealValue(
RexxVariableDictionary *context) /* current activation context */
/******************************************************************************/
/* Function: Polymorphic get_value function used with expression terms */
/******************************************************************************/
{
return (RexxObject *)this; /* just return this value */
}
RexxClass *RexxNumberString::classObject()
/******************************************************************************/
/* Function: Return the String class object for numberstring instances */
/******************************************************************************/
{
/* just return class from behaviour */
return TheStringClass;
}
void *RexxNumberString::operator new(size_t size, size_t length)
/******************************************************************************/
/* Function: Create a new NumberString object */
/******************************************************************************/
{
RexxNumberString *newNumber = (RexxNumberString *)new_object(size + length, T_NumberString);
/* initialize the new object */
newNumber->setHasNoReferences(); /* Let GC know no to bother with LIVE*/
return newNumber; /* return the new numberstring */
}
RexxNumberString *RexxNumberString::newInstance(const char *number, stringsize_t len)
/******************************************************************************/
/* Function: Create a new number string object */
/******************************************************************************/
{
RexxNumberString *newNumber;
if (number == NULL)
{ /* asking for a dummy string? */
/* allocate a new string */
newNumber = new (len) RexxNumberString (len);
/* make it a zero value */
newNumber->setZero();
return newNumber; /* return this now */
}
/* scan the string 1st to see if its */
/*valid */
if (numberStringScan(number, len))
{
newNumber = OREF_NULL; /* not a valid number, get out now. */
}
else
{
/* looks to be valid. get a new */
/* format it */
newNumber = new (len) RexxNumberString (len);
/* now see if the data actually is */
/* a number and fill in actual data */
/* NOTE: even though a scan has been */
/* we still may not have a thorough*/
/* enough check. */
if (newNumber->format(number, len))
{
/* number didn't convert, */
newNumber = OREF_NULL;
}
}
return newNumber;
}
RexxNumberString *RexxNumberString::newInstanceFromFloat(float num)
/******************************************************************************/
/* Function: Create a numberstring object from a floating point number */
/******************************************************************************/
{
return newInstanceFromDouble((double)num, number_digits());
}
RexxNumberString *RexxNumberString::newInstanceFromDouble(double number)
/******************************************************************************/
/* Function: Create a NumberString from a double value */
/******************************************************************************/
{
return newInstanceFromDouble(number, number_digits());
}
/**
* Create a numberstring from a double value using a given
* formatting precision.
*
* @param number The double number to convert
* @param precision The precision to apply to the result.
*
* @return The formatted number, as a numberstring value.
*/
RexxNumberString *RexxNumberString::newInstanceFromDouble(double number, size_t precision)
{
// make a nan value a string value
if (isnan(number))
{
return (RexxNumberString *)new_string("nan");
}
else if (number == +HUGE_VAL)
{
return (RexxNumberString *)new_string("+infinity");
}
else if (number == -HUGE_VAL)
{
return (RexxNumberString *)new_string("-infinity");
}
RexxNumberString *result;
size_t resultLen;
/* Max length of double str is */
/* 22, make 30 just to be safe */
char doubleStr[30];
/* get double as a string value. */
/* Use digits as precision. */
sprintf(doubleStr, "%.*g", (int)(precision + 2), number);
resultLen = strlen(doubleStr); /* Compute length of floatString */
/* Create new NumberString */
result = new (resultLen) RexxNumberString (resultLen, precision);
/* now format as a numberstring */
result->format(doubleStr, resultLen);
return result->prepareNumber(precision, ROUND);
}
RexxNumberString *RexxNumberString::newInstanceFromWholenumber(wholenumber_t integer)
/******************************************************************************/
/* Function: Create a NumberString object from a wholenumber_t value */
/******************************************************************************/
{
// the size of the integer depends on the platform, 32-bit or 64-bit.
// ARGUMENT_DIGITS ensures the correct value
RexxNumberString *newNumber = new (Numerics::ARGUMENT_DIGITS) RexxNumberString (Numerics::ARGUMENT_DIGITS);
newNumber->formatNumber(integer); /* format the number */
return newNumber;
}
RexxNumberString *RexxNumberString::newInstanceFromStringsize(stringsize_t integer)
/******************************************************************************/
/* Function: Create a NumberString object from a size_t value */
/******************************************************************************/
{
// the size of the integer depends on the platform, 32-bit or 64-bit.
// ARGUMENT_DIGITS ensures the correct value
RexxNumberString *newNumber = new (Numerics::ARGUMENT_DIGITS) RexxNumberString (Numerics::ARGUMENT_DIGITS);
newNumber->formatUnsignedNumber(integer); /* format the number */
return newNumber;
}
RexxNumberString *RexxNumberString::newInstanceFromInt64(int64_t integer)
/******************************************************************************/
/* Function: Create a NumberString object from a signed 64 bit number */
/******************************************************************************/
{
// this give us space for entire binary range of the int64_t number.
RexxNumberString *newNumber = new (Numerics::DIGITS64) RexxNumberString (Numerics::DIGITS64);
newNumber->formatInt64(integer); /* format the number */
return newNumber;
}
RexxNumberString *RexxNumberString::newInstanceFromUint64(uint64_t integer)
/******************************************************************************/
/* Function: Create a NumberString object from an unsigned 64 bit number */
/******************************************************************************/
{
// this give us space for entire binary range of the uint64_t number.
RexxNumberString *newNumber = new (Numerics::DIGITS64) RexxNumberString (Numerics::DIGITS64);
newNumber->formatUnsignedInt64(integer); /* format the number */
return newNumber;
}
/* numberstring operator methods */
PCPPM RexxNumberString::operatorMethods[] =
{
NULL, /* first entry not used */
(PCPPM)&RexxNumberString::plus,
(PCPPM)&RexxNumberString::minus,
(PCPPM)&RexxNumberString::multiply,
(PCPPM)&RexxNumberString::divide,
(PCPPM)&RexxNumberString::integerDivide,
(PCPPM)&RexxNumberString::remainder,
(PCPPM)&RexxNumberString::power,
(PCPPM)&RexxNumberString::concat,
(PCPPM)&RexxNumberString::concat, /* Duplicate entry neccessary */
(PCPPM)&RexxNumberString::concatBlank,
(PCPPM)&RexxNumberString::equal,
(PCPPM)&RexxNumberString::notEqual,
(PCPPM)&RexxNumberString::isGreaterThan,
(PCPPM)&RexxNumberString::isLessOrEqual,
(PCPPM)&RexxNumberString::isLessThan,
(PCPPM)&RexxNumberString::isGreaterOrEqual,
/* Duplicate entry neccessary */
(PCPPM)&RexxNumberString::isGreaterOrEqual,
(PCPPM)&RexxNumberString::isLessOrEqual,
(PCPPM)&RexxNumberString::strictEqual,
(PCPPM)&RexxNumberString::strictNotEqual,
(PCPPM)&RexxNumberString::strictGreaterThan,
(PCPPM)&RexxNumberString::strictLessOrEqual,
(PCPPM)&RexxNumberString::strictLessThan,
(PCPPM)&RexxNumberString::strictGreaterOrEqual,
/* Duplicate entry neccessary */
(PCPPM)&RexxNumberString::strictGreaterOrEqual,
(PCPPM)&RexxNumberString::strictLessOrEqual,
(PCPPM)&RexxNumberString::notEqual,
/* Duplicate entry neccessary */
(PCPPM)&RexxNumberString::notEqual,
(PCPPM)&RexxNumberString::andOp,
(PCPPM)&RexxNumberString::orOp,
(PCPPM)&RexxNumberString::xorOp,
(PCPPM)&RexxNumberString::operatorNot,
};