summaryrefslogtreecommitdiffstats
path: root/svl/source/numbers/zforfind.cxx
blob: c1898104a905396badc91627b9a84730e47dd6bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <cstdlib>
#include <dtoa.h>
#include <float.h>
#include <comphelper/string.hxx>
#include <o3tl/string_view.hxx>
#include <sal/log.hxx>
#include <tools/date.hxx>
#include <rtl/math.hxx>
#include <rtl/character.hxx>
#include <unotools/charclass.hxx>
#include <unotools/calendarwrapper.hxx>
#include <unotools/localedatawrapper.hxx>
#include <com/sun/star/i18n/CalendarFieldIndex.hpp>
#include <com/sun/star/i18n/LocaleCalendar2.hpp>
#include <unotools/digitgroupingiterator.hxx>
#include <comphelper/sequence.hxx>

#include <svl/zforlist.hxx>
#include "zforscan.hxx"
#include <svl/zformat.hxx>

#include <memory>

#include "zforfind.hxx"

#ifndef DBG_UTIL
#define NF_TEST_CALENDAR 0
#else
#define NF_TEST_CALENDAR 0
#endif
#if NF_TEST_CALENDAR
#include <comphelper/processfactory.hxx>
#include <com/sun/star/i18n/XCalendar4.hpp>
#endif


const sal_uInt8 ImpSvNumberInputScan::nMatchedEndString    = 0x01;
const sal_uInt8 ImpSvNumberInputScan::nMatchedMidString    = 0x02;
const sal_uInt8 ImpSvNumberInputScan::nMatchedStartString  = 0x04;
const sal_uInt8 ImpSvNumberInputScan::nMatchedVirgin       = 0x08;
const sal_uInt8 ImpSvNumberInputScan::nMatchedUsedAsReturn = 0x10;

/* It is not clear how we want timezones to be handled. Convert them to local
 * time isn't wanted, as it isn't done in any other place and timezone
 * information isn't stored anywhere. Ignoring them and pretending local time
 * may be wrong too and might not be what the user expects. Keep the input as
 * string so that no information is lost.
 * Anyway, defining NF_RECOGNIZE_ISO8601_TIMEZONES to 1 would be the way how it
 * would work, together with the nTimezonePos handling in GetTimeRef(). */
#define NF_RECOGNIZE_ISO8601_TIMEZONES 0

const sal_Unicode cNoBreakSpace = 0xA0;
const sal_Unicode cNarrowNoBreakSpace = 0x202F;
const bool kDefaultEra = true;     // Gregorian CE, positive year

ImpSvNumberInputScan::ImpSvNumberInputScan( SvNumberFormatter* pFormatterP )
        :
        bTextInitialized( false ),
        bScanGenitiveMonths( false ),
        bScanPartitiveMonths( false ),
        eScannedType( SvNumFormatType::UNDEFINED ),
        eSetType( SvNumFormatType::UNDEFINED )
{
    pFormatter = pFormatterP;
    moNullDate.emplace( 30,12,1899 );
    nYear2000 = SvNumberFormatter::GetYear2000Default();
    Reset();
    ChangeIntl();
}


ImpSvNumberInputScan::~ImpSvNumberInputScan()
{
}


void ImpSvNumberInputScan::Reset()
{
    mpFormat     = nullptr;
    nMonth       = 0;
    nMonthPos    = 0;
    nDayOfWeek   = 0;
    nTimePos     = 0;
    nSign        = 0;
    nESign       = 0;
    nDecPos      = 0;
    bNegCheck    = false;
    nStringsCnt  = 0;
    nNumericsCnt = 0;
    nThousand    = 0;
    eScannedType = SvNumFormatType::UNDEFINED;
    nAmPm        = 0;
    nPosThousandString = 0;
    nLogical     = 0;
    mbEraCE        = kDefaultEra;
    nStringScanNumFor = 0;
    nStringScanSign = 0;
    nMatchedAllStrings = nMatchedVirgin;
    nMayBeIso8601 = 0;
    bIso8601Tsep = false;
    nMayBeMonthDate = 0;
    nAcceptedDatePattern = -2;
    nDatePatternStart = 0;
    nDatePatternNumbers = 0;

    for (sal_uInt32 i = 0; i < SV_MAX_COUNT_INPUT_STRINGS; i++)
    {
        IsNum[i] = false;
        nNums[i] = 0;
    }
}

// native number transliteration if necessary
static void TransformInput( SvNumberFormatter const * pFormatter, OUString& rStr )
{
    sal_Int32 nPos, nLen;
    for ( nPos = 0, nLen = rStr.getLength(); nPos < nLen; ++nPos )
    {
        if ( 256 <= rStr[ nPos ] &&
             pFormatter->GetCharClass()->isDigit( rStr, nPos ) )
        {
            break;
        }
    }
    if ( nPos < nLen )
    {
        rStr = pFormatter->GetNatNum()->getNativeNumberString( rStr,
                                                               pFormatter->GetLanguageTag().getLocale(), 0 );
    }
}


/**
 * Only simple unsigned floating point values without any error detection,
 * decimal separator has to be '.'
 */
double ImpSvNumberInputScan::StringToDouble( std::u16string_view aStr, bool bForceFraction )
{
    std::unique_ptr<char[]> bufInHeap;
    constexpr int bufOnStackSize = 256;
    char bufOnStack[bufOnStackSize];
    char* buf = bufOnStack;
    const sal_Int32 bufsize = aStr.size() + (bForceFraction ? 2 : 1);
    if (bufsize > bufOnStackSize)
    {
        bufInHeap = std::make_unique<char[]>(bufsize);
        buf = bufInHeap.get();
    }
    char* p = buf;
    if (bForceFraction)
        *p++ = '.';
    for (size_t nPos = 0; nPos < aStr.size(); ++nPos)
    {
        sal_Unicode c = aStr[nPos];
        if (c == '.' || (c >= '0' && c <= '9'))
            *p++ = static_cast<char>(c);
        else
            break;
    }
    *p = '\0';

    return strtod_nolocale(buf, nullptr);
}

namespace {

/**
 * Splits up the input into numbers and strings for further processing
 * (by the Turing machine).
 *
 * Starting state = GetChar
 * ---------------+-------------------+-----------------------------+---------------
 *  Old State     | Character read    | Event                       | New state
 * ---------------+-------------------+-----------------------------+---------------
 *  GetChar       | Number            | Symbol = Character          | GetValue
 *                | Else              | Symbol = Character          | GetString
 * ---------------|-------------------+-----------------------------+---------------
 *  GetValue      | Number            | Symbol = Symbol + Character | GetValue
 *                | Else              | Dec(CharPos)                | Stop
 * ---------------+-------------------+-----------------------------+---------------
 *  GetString     | Number            | Dec(CharPos)                | Stop
 *                | Else              | Symbol = Symbol + Character | GetString
 * ---------------+-------------------+-----------------------------+---------------
 */
enum ScanState  // States of the Turing machine
{
    SsStop      = 0,
    SsStart     = 1,
    SsGetValue  = 2,
    SsGetString = 3
};

}

bool ImpSvNumberInputScan::NextNumberStringSymbol( const sal_Unicode*& pStr,
                                                   OUString& rSymbol )
{
    bool isNumber = false;
    sal_Unicode cToken;
    ScanState eState = SsStart;
    const sal_Unicode* pHere = pStr;
    sal_Int32 nChars = 0;

    for (;;)
    {
        cToken = *pHere;
        if (cToken == 0 || eState == SsStop)
            break;
        pHere++;
        switch (eState)
        {
        case SsStart:
            if ( rtl::isAsciiDigit( cToken ) )
            {
                eState = SsGetValue;
                isNumber = true;
            }
            else
            {
                eState = SsGetString;
            }
            nChars++;
            break;
        case SsGetValue:
            if ( rtl::isAsciiDigit( cToken ) )
            {
                nChars++;
            }
            else
            {
                eState = SsStop;
                pHere--;
            }
            break;
        case SsGetString:
            if ( !rtl::isAsciiDigit( cToken ) )
            {
                nChars++;
            }
            else
            {
                eState = SsStop;
                pHere--;
            }
            break;
        default:
            break;
        } // switch
    } // while

    if ( nChars )
    {
        rSymbol = OUString( pStr, nChars );
    }
    else
    {
        rSymbol.clear();
    }

    pStr = pHere;

    return isNumber;
}


// FIXME: should be grouping; it is only used though in case nStringsCnt is
// near SV_MAX_COUNT_INPUT_STRINGS, in NumberStringDivision().

bool ImpSvNumberInputScan::SkipThousands( const sal_Unicode*& pStr,
                                          OUString& rSymbol ) const
{
    bool res = false;
    OUStringBuffer sBuff(rSymbol);
    sal_Unicode cToken;
    const OUString& rThSep = pFormatter->GetNumThousandSep();
    const sal_Unicode* pHere = pStr;
    ScanState eState = SsStart;
    sal_Int32 nCounter = 0; // counts 3 digits

    for (;;)
    {
        cToken = *pHere;
        if (cToken == 0 || eState == SsStop)
            break;
        pHere++;
        switch (eState)
        {
        case SsStart:
            if ( StringPtrContains( rThSep, pHere-1, 0 ) )
            {
                nCounter = 0;
                eState = SsGetValue;
                pHere += rThSep.getLength() - 1;
            }
            else
            {
                eState = SsStop;
                pHere--;
            }
            break;
        case SsGetValue:
            if ( rtl::isAsciiDigit( cToken ) )
            {
                sBuff.append(cToken);
                nCounter++;
                if (nCounter == 3)
                {
                    eState = SsStart;
                    res = true; // .000 combination found
                }
            }
            else
            {
                eState = SsStop;
                pHere--;
            }
            break;
        default:
            break;
        } // switch
    } // while

    if (eState == SsGetValue) // break with less than 3 digits
    {
        if ( nCounter )
        {
            sBuff.remove( sBuff.getLength() - nCounter, nCounter );
        }
        pHere -= nCounter + rThSep.getLength(); // put back ThSep also
    }
    rSymbol = sBuff.makeStringAndClear();
    pStr = pHere;

    return res;
}


void ImpSvNumberInputScan::NumberStringDivision( const OUString& rString )
{
    const sal_Unicode* pStr = rString.getStr();
    const sal_Unicode* const pEnd = pStr + rString.getLength();
    while ( pStr < pEnd && nStringsCnt < SV_MAX_COUNT_INPUT_STRINGS )
    {
        if ( NextNumberStringSymbol( pStr, sStrArray[nStringsCnt] ) )
        {   // Number
            IsNum[nStringsCnt] = true;
            nNums[nNumericsCnt] = nStringsCnt;
            nNumericsCnt++;
            if (nStringsCnt >= SV_MAX_COUNT_INPUT_STRINGS - 7 &&
                nPosThousandString == 0) // Only once
            {
                if ( SkipThousands( pStr, sStrArray[nStringsCnt] ) )
                {
                    nPosThousandString = nStringsCnt;
                }
            }
        }
        else
        {
            IsNum[nStringsCnt] = false;
        }
        nStringsCnt++;
    }
}


/**
 * Whether rString contains rWhat at nPos
 */
bool ImpSvNumberInputScan::StringContainsImpl( const OUString& rWhat,
                                               const OUString& rString, sal_Int32 nPos )
{
    if ( nPos + rWhat.getLength() <= rString.getLength() )
    {
        return StringPtrContainsImpl( rWhat, rString.getStr(), nPos );
    }
    return false;
}


/**
 * Whether pString contains rWhat at nPos
 */
bool ImpSvNumberInputScan::StringPtrContainsImpl( const OUString& rWhat,
                                                  const sal_Unicode* pString, sal_Int32 nPos )
{
    if ( rWhat.isEmpty() )
    {
        return false;
    }
    const sal_Unicode* pWhat = rWhat.getStr();
    const sal_Unicode* const pEnd = pWhat + rWhat.getLength();
    const sal_Unicode* pStr = pString + nPos;
    while ( pWhat < pEnd )
    {
        if ( *pWhat != *pStr )
        {
            return false;
        }
        pWhat++;
        pStr++;
    }
    return true;
}


/**
 * Whether rString contains word rWhat at nPos
 */
bool ImpSvNumberInputScan::StringContainsWord( const OUString& rWhat,
                                               const OUString& rString, sal_Int32 nPos ) const
{
    if (rWhat.isEmpty() || rString.getLength() < nPos + rWhat.getLength())
        return false;

    if (StringPtrContainsImpl( rWhat, rString.getStr(), nPos))
    {
        nPos += rWhat.getLength();
        if (nPos == rString.getLength())
            return true;    // word at end of string

        /* TODO: we COULD invoke bells and whistles word break iterator to find
         * the next boundary, but really ... this is called for date input, so
         * how many languages do not separate the day and month names in some
         * form? */

        // Check simple ASCII first before invoking i18n or anything else.
        const sal_Unicode c = rString[nPos];

        // Common separating ASCII characters in date context.
        switch (c)
        {
            case ' ':
            case '-':
            case '.':
            case '/':
                return true;
            default:
                ;   // nothing
        }

        if (rtl::isAsciiAlphanumeric( c ))
            return false;   // Alpha or numeric is not word gap.

        sal_Int32 nIndex = nPos;
        rString.iterateCodePoints( &nIndex);
        if (nPos+1 < nIndex)
            return true;    // Surrogate, assume these to be new words.

        const sal_Int32 nType = pFormatter->GetCharClass()->getCharacterType( rString, nPos);
        using namespace ::com::sun::star::i18n;

        if ((nType & (KCharacterType::UPPER | KCharacterType::LOWER | KCharacterType::DIGIT)) != 0)
            return false;   // Alpha or numeric is not word gap.

        if (nType & KCharacterType::LETTER)
            return true;    // Letter other than alpha is new word. (Is it?)

        return true;        // Catch all remaining as gap until we know better.
    }

    return false;
}


/**
 * Skips the supplied char
 */
inline bool ImpSvNumberInputScan::SkipChar( sal_Unicode c, std::u16string_view rString,
                                            sal_Int32& nPos )
{
    if ((nPos < static_cast<sal_Int32>(rString.size())) && (rString[nPos] == c))
    {
        nPos++;
        return true;
    }
    return false;
}


/**
 * Skips blanks
 */
inline bool ImpSvNumberInputScan::SkipBlanks( const OUString& rString,
                                              sal_Int32& nPos )
{
    sal_Int32 nHere = nPos;
    if ( nPos < rString.getLength() )
    {
        const sal_Unicode* p = rString.getStr() + nPos;
        while ( *p == ' ' || *p == cNoBreakSpace || *p == cNarrowNoBreakSpace )
        {
            nPos++;
            p++;
        }
    }
    return nHere < nPos;
}


/**
 * jump over rWhat in rString at nPos
 */
inline bool ImpSvNumberInputScan::SkipString( const OUString& rWhat,
                                              const OUString& rString, sal_Int32& nPos )
{
    if ( StringContains( rWhat, rString, nPos ) )
    {
        nPos = nPos + rWhat.getLength();
        return true;
    }
    return false;
}


/**
 * Recognizes exactly ,111 in {3} and {3,2} or ,11 in {3,2} grouping
 */
inline bool ImpSvNumberInputScan::GetThousandSep( std::u16string_view rString,
                                                  sal_Int32& nPos,
                                                  sal_uInt16 nStringPos ) const
{
    const OUString& rSep = pFormatter->GetNumThousandSep();
    // Is it an ordinary space instead of a no-break space?
    bool bSpaceBreak = (rSep[0] == cNoBreakSpace || rSep[0] == cNarrowNoBreakSpace) &&
        rString[0] == u' ' &&
        rSep.getLength() == 1 && rString.size() == 1;
    if (!((rString == rSep || bSpaceBreak) &&      // nothing else
           nStringPos < nStringsCnt - 1 &&         // safety first!
           IsNum[ nStringPos + 1 ] ))              // number follows
    {
        return false; // no? => out
    }

    utl::DigitGroupingIterator aGrouping( pFormatter->GetLocaleData()->getDigitGrouping());
    // Match ,### in {3} or ,## in {3,2}
    /* FIXME: this could be refined to match ,## in {3,2} only if ,##,## or
     * ,##,### and to match ,### in {3,2} only if it's the last. However,
     * currently there is no track kept where group separators occur. In {3,2}
     * #,###,### and #,##,## would be valid input, which maybe isn't even bad
     * for #,###,###. Other combinations such as #,###,## maybe not. */
    sal_Int32 nLen = sStrArray[ nStringPos + 1 ].getLength();
    if (nLen == aGrouping.get() ||                  // with 3 (or so) digits
        nLen == aGrouping.advance().get() ||        // or with 2 (or 3 or so) digits
        nPosThousandString == nStringPos + 1 )      // or concatenated
    {
        nPos = nPos + rSep.getLength();
        return true;
    }
    return false;
}


/**
 * Conversion of text to logical value
 *  "true" =>  1:
 *  "false"=> -1:
 *  else   =>  0:
 */
short ImpSvNumberInputScan::GetLogical( std::u16string_view rString ) const
{
    short res;

    const ImpSvNumberformatScan* pFS = pFormatter->GetFormatScanner();
    if ( rString == pFS->GetTrueString() )
    {
        res = 1;
    }
    else if ( rString == pFS->GetFalseString() )
    {
        res = -1;
    }
    else
    {
        res = 0;
    }
    return res;
}


/**
 * Converts a string containing a month name (JAN, January) at nPos into the
 * month number (negative if abbreviated), returns 0 if nothing found
 */
short ImpSvNumberInputScan::GetMonth( const OUString& rString, sal_Int32& nPos )
{
    short res = 0; // no month found

    if (rString.getLength() > nPos) // only if needed
    {
        if ( !bTextInitialized )
        {
            InitText();
        }
        sal_Int16 nMonths = pFormatter->GetCalendar()->getNumberOfMonthsInYear();
        for ( sal_Int16 i = 0; i < nMonths; i++ )
        {
            if ( bScanGenitiveMonths && StringContainsWord( pUpperGenitiveMonthText[i], rString, nPos ) )
            {   // genitive full names first
                nPos = nPos + pUpperGenitiveMonthText[i].getLength();
                res = i + 1;
                break;  // for
            }
            else if ( bScanGenitiveMonths && StringContainsWord( pUpperGenitiveAbbrevMonthText[i], rString, nPos ) )
            {   // genitive abbreviated
                nPos = nPos + pUpperGenitiveAbbrevMonthText[i].getLength();
                res = sal::static_int_cast< short >(-(i+1)); // negative
                break;  // for
            }
            else if ( bScanPartitiveMonths && StringContainsWord( pUpperPartitiveMonthText[i], rString, nPos ) )
            {   // partitive full names
                nPos = nPos + pUpperPartitiveMonthText[i].getLength();
                res = i+1;
                break;  // for
            }
            else if ( bScanPartitiveMonths && StringContainsWord( pUpperPartitiveAbbrevMonthText[i], rString, nPos ) )
            {   // partitive abbreviated
                nPos = nPos + pUpperPartitiveAbbrevMonthText[i].getLength();
                res = sal::static_int_cast< short >(-(i+1)); // negative
                break;  // for
            }
            else if ( StringContainsWord( pUpperMonthText[i], rString, nPos ) )
            {   // noun full names
                nPos = nPos + pUpperMonthText[i].getLength();
                res = i+1;
                break;  // for
            }
            else if ( StringContainsWord( pUpperAbbrevMonthText[i], rString, nPos ) )
            {   // noun abbreviated
                nPos = nPos + pUpperAbbrevMonthText[i].getLength();
                res = sal::static_int_cast< short >(-(i+1)); // negative
                break;  // for
            }
            else if (i == 2 && pFormatter->GetLanguageTag().getLanguage() == "de")
            {
                if (pUpperAbbrevMonthText[i] == u"M\u00C4R" && StringContainsWord( "MRZ", rString, nPos))
                {   // Accept MRZ for MÄR
                    nPos = nPos + 3;
                    res = sal::static_int_cast< short >(-(i+1)); // negative
                    break;  // for
                }
                else if (pUpperAbbrevMonthText[i] == "MRZ" && StringContainsWord( u"M\u00C4R"_ustr, rString, nPos))
                {   // And vice versa, accept MÄR for MRZ
                    nPos = nPos + 3;
                    res = sal::static_int_cast< short >(-(i+1)); // negative
                    break;  // for
                }
            }
            else if (i == 8)
            {
                // This assumes the weirdness is applicable to all locales.
                // It is the case for at least en-* and de-* locales.
                if (pUpperAbbrevMonthText[i] == "SEPT" && StringContainsWord( "SEP", rString, nPos))
                {   // #102136# The correct English form of month September abbreviated is
                    // SEPT, but almost every data contains SEP instead.
                    nPos = nPos + 3;
                    res = sal::static_int_cast< short >(-(i+1)); // negative
                    break;  // for
                }
                else if (pUpperAbbrevMonthText[i] == "SEP" && StringContainsWord( "SEPT", rString, nPos))
                {   // And vice versa, accept SEPT for SEP
                    nPos = nPos + 4;
                    res = sal::static_int_cast< short >(-(i+1)); // negative
                    break;  // for
                }
            }
        }
        if (!res)
        {
            // Brutal hack for German locales that know "Januar" or "Jänner".
            /* TODO: add alternative month names to locale data? if there are
             * more languages... */
            const LanguageTag& rLanguageTag = pFormatter->GetLanguageTag();
            if (rLanguageTag.getLanguage() == "de")
            {
                if (rLanguageTag.getCountry() == "AT")
                {
                    // Locale data has Jänner/Jän
                    assert(pUpperMonthText[0] == u"J\u00C4NNER");
                    if (StringContainsWord( "JANUAR", rString, nPos))
                    {
                        nPos += 6;
                        res = 1;
                    }
                    else if (StringContainsWord( "JAN", rString, nPos))
                    {
                        nPos += 3;
                        res = -1;
                    }
                }
                else
                {
                    // Locale data has Januar/Jan
                    assert(pUpperMonthText[0] == "JANUAR");
                    if (StringContainsWord( u"J\u00C4NNER"_ustr, rString, nPos))
                    {
                        nPos += 6;
                        res = 1;
                    }
                    else if (StringContainsWord( u"J\u00C4N"_ustr, rString, nPos))
                    {
                        nPos += 3;
                        res = -1;
                    }
                }
            }
        }
    }

    return res;
}


/**
 * Converts a string containing a DayOfWeek name (Mon, Monday) at nPos into the
 * DayOfWeek number + 1 (negative if abbreviated), returns 0 if nothing found
 */
int ImpSvNumberInputScan::GetDayOfWeek( const OUString& rString, sal_Int32& nPos )
{
    int res = 0; // no day found

    if (rString.getLength() > nPos) // only if needed
    {
        if ( !bTextInitialized )
        {
            InitText();
        }
        sal_Int16 nDays = pFormatter->GetCalendar()->getNumberOfDaysInWeek();
        for ( sal_Int16 i = 0; i < nDays; i++ )
        {
            if ( StringContainsWord( pUpperDayText[i], rString, nPos ) )
            {   // full names first
                nPos = nPos + pUpperDayText[i].getLength();
                res = i + 1;
                break;  // for
            }
            if ( StringContainsWord( pUpperAbbrevDayText[i], rString, nPos ) )
            {   // abbreviated
                nPos = nPos + pUpperAbbrevDayText[i].getLength();
                res = -(i + 1); // negative
                break;  // for
            }
        }
    }

    return res;
}


/**
 * Reading a currency symbol
 * '$'   => true
 * else => false
 */
bool ImpSvNumberInputScan::GetCurrency( const OUString& rString, sal_Int32& nPos )
{
    if ( rString.getLength() > nPos )
    {
        if ( !aUpperCurrSymbol.getLength() )
        {   // If no format specified the currency of the currently active locale.
            LanguageType eLang = (mpFormat ? mpFormat->GetLanguage() :
                    pFormatter->GetLocaleData()->getLanguageTag().getLanguageType());
            aUpperCurrSymbol = pFormatter->GetCharClass()->uppercase(
                SvNumberFormatter::GetCurrencyEntry( eLang ).GetSymbol() );
        }
        if ( StringContains( aUpperCurrSymbol, rString, nPos ) )
        {
            nPos = nPos + aUpperCurrSymbol.getLength();
            return true;
        }
        if ( mpFormat )
        {
            OUString aSymbol, aExtension;
            if ( mpFormat->GetNewCurrencySymbol( aSymbol, aExtension ) )
            {
                if ( aSymbol.getLength() <= rString.getLength() - nPos )
                {
                    aSymbol = pFormatter->GetCharClass()->uppercase(aSymbol);
                    if ( StringContains( aSymbol, rString, nPos ) )
                    {
                        nPos = nPos + aSymbol.getLength();
                        return true;
                    }
                }
            }
        }
    }

    return false;
}


/**
 * Reading the time period specifier (AM/PM) for the 12 hour clock
 *
 *  Returns:
 *   "AM" or "PM" => true
 *   else         => false
 *
 *  nAmPos:
 *   "AM"  =>  1
 *   "PM"  => -1
 *   else =>  0
*/
bool ImpSvNumberInputScan::GetTimeAmPm( const OUString& rString, sal_Int32& nPos )
{

    if ( rString.getLength() > nPos )
    {
        const CharClass* pChr = pFormatter->GetCharClass();
        const LocaleDataWrapper* pLoc = pFormatter->GetLocaleData();
        if ( StringContains( pChr->uppercase( pLoc->getTimeAM() ), rString, nPos ) )
        {
            nAmPm = 1;
            nPos = nPos + pLoc->getTimeAM().getLength();
            return true;
        }
        else if ( StringContains( pChr->uppercase( pLoc->getTimePM() ), rString, nPos ) )
        {
            nAmPm = -1;
            nPos = nPos + pLoc->getTimePM().getLength();
            return true;
        }
    }

    return false;
}


/**
 * Read a decimal separator (',')
 * ','   => true
 * else => false
 */
inline bool ImpSvNumberInputScan::GetDecSep( std::u16string_view rString, sal_Int32& nPos ) const
{
    if ( static_cast<sal_Int32>(rString.size()) > nPos )
    {
        const OUString& rSep = pFormatter->GetNumDecimalSep();
        if ( o3tl::starts_with(rString.substr(nPos), rSep) )
        {
            nPos = nPos + rSep.getLength();
            return true;
        }
        const OUString& rSepAlt = pFormatter->GetNumDecimalSepAlt();
        if ( !rSepAlt.isEmpty() && o3tl::starts_with(rString.substr(nPos), rSepAlt) )
        {
            nPos = nPos + rSepAlt.getLength();
            return true;
        }
    }
    return false;
}


/**
 * Reading a hundredth seconds separator
 */
inline bool ImpSvNumberInputScan::GetTime100SecSep( std::u16string_view rString, sal_Int32& nPos ) const
{
    if ( static_cast<sal_Int32>(rString.size()) > nPos )
    {
        if (bIso8601Tsep)
        {
            // ISO 8601 specifies both '.' dot and ',' comma as fractional
            // separator.
            if (rString[nPos] == '.' || rString[nPos] == ',')
            {
                ++nPos;
                return true;
            }
        }
        // Even in an otherwise ISO 8601 string be lenient and accept the
        // locale defined separator.
        const OUString& rSep = pFormatter->GetLocaleData()->getTime100SecSep();
        if ( o3tl::starts_with(rString.substr(nPos), rSep))
        {
            nPos = nPos + rSep.getLength();
            return true;
        }
    }
    return false;
}


/**
 * Read a sign including brackets
 * '+'   =>  1
 * '-'   => -1
 * u'−'   => -1
 *  '('   => -1, bNegCheck = 1
 * else =>  0
 */
int ImpSvNumberInputScan::GetSign( std::u16string_view rString, sal_Int32& nPos )
{
    if (static_cast<sal_Int32>(rString.size()) > nPos)
        switch (rString[ nPos ])
        {
        case '+':
            nPos++;
            return 1;
        case '(': // '(' similar to '-' ?!?
            bNegCheck = true;
            [[fallthrough]];
        case '-':
        // tdf#117037 - unicode minus (0x2212)
        case u'−':
            nPos++;
            return -1;
        default:
            break;
        }

    return 0;
}


/**
 * Read a sign with an exponent
 * '+'   =>  1
 * '-'   => -1
 * else =>  0
 */
short ImpSvNumberInputScan::GetESign( std::u16string_view rString, sal_Int32& nPos )
{
    if (static_cast<sal_Int32>(rString.size()) > nPos)
    {
        switch (rString[nPos])
        {
        case '+':
            nPos++;
            return 1;
        case '-':
            nPos++;
            return -1;
        default:
            break;
        }
    }
    return 0;
}


/**
 * i counts string portions, j counts numbers thereof.
 * It should had been called SkipNumber instead.
 */
inline bool ImpSvNumberInputScan::GetNextNumber( sal_uInt16& i, sal_uInt16& j ) const
{
    if ( i < nStringsCnt && IsNum[i] )
    {
        j++;
        i++;
        return true;
    }
    return false;
}


bool ImpSvNumberInputScan::GetTimeRef( double& fOutNumber,
                                       sal_uInt16 nIndex, // j-value of the first numeric time part of input, default 0
                                       sal_uInt16 nCnt,   // count of numeric time parts
                                       SvNumInputOptions eInputOptions
                                     ) const
{
    bool bRet = true;
    sal_Int32 nHour;
    sal_Int32 nMinute = 0;
    sal_Int32 nSecond = 0;
    double fSecond100 = 0.0;
    sal_uInt16 nStartIndex = nIndex;

    if (nDecPos == 2 && (nCnt == 3 || nCnt == 2)) // 20:45.5 or 45.5
    {
        nHour = 0;
    }
    else if (mpFormat && nDecPos == 0 && nCnt == 2 && mpFormat->IsMinuteSecondFormat())
    {
        // Input on MM:SS format, instead of doing HH:MM:00
        nHour = 0;
    }
    else if (nIndex - nStartIndex < nCnt)
    {
        const OUString& rValStr = sStrArray[nNums[nIndex++]];
        nHour = rValStr.toInt32();
        if (nHour == 0 && rValStr != "0" && rValStr != "00")
            bRet = false;   // overflow -> Text
    }
    else
    {
        nHour = 0;
        bRet = false;
        SAL_WARN( "svl.numbers", "ImpSvNumberInputScan::GetTimeRef: bad number index");
    }

    // 0:123 or 0:0:123 or 0:123:59 is valid
    bool bAllowDuration = (nHour == 0 && !nAmPm);

    if (nAmPm && nHour > 12) // not a valid AM/PM clock time
    {
        bRet = false;
    }
    else if (nAmPm == -1 && nHour != 12) // PM
    {
        nHour += 12;
    }
    else if (nAmPm == 1 && nHour == 12) // 12 AM
    {
        nHour = 0;
    }

    if (nDecPos == 2 && nCnt == 2) // 45.5
    {
        nMinute = 0;
    }
    else if (nIndex - nStartIndex < nCnt)
    {
        const OUString& rValStr = sStrArray[nNums[nIndex++]];
        nMinute = rValStr.toInt32();
        if (nMinute == 0 && rValStr != "0" && rValStr != "00")
            bRet = false;   // overflow -> Text
        if (!(eInputOptions & SvNumInputOptions::LAX_TIME) && !bAllowDuration
                && nIndex > 1 && nMinute > 59)
            bRet = false;   // 1:60 or 1:123 is invalid, 123:1 or 0:123 is valid
        if (bAllowDuration)
            bAllowDuration = (nMinute == 0);
    }
    if (nIndex - nStartIndex < nCnt)
    {
        const OUString& rValStr = sStrArray[nNums[nIndex++]];
        nSecond = rValStr.toInt32();
        if (nSecond == 0 && rValStr != "0" && rValStr != "00")
            bRet = false;   // overflow -> Text
        if (!(eInputOptions & SvNumInputOptions::LAX_TIME) && !bAllowDuration
                && nIndex > 1 && nSecond > 59 && !(nHour == 23 && nMinute == 59 && nSecond == 60))
            bRet = false;   // 1:60 or 1:123 or 1:1:123 is invalid, 123:1 or 123:1:1 or 0:0:123 is valid, or leap second
    }
    if (nIndex - nStartIndex < nCnt)
    {
        fSecond100 = StringToDouble( sStrArray[nNums[nIndex]], true );
    }
    fOutNumber = (static_cast<double>(nHour)*3600 +
                  static_cast<double>(nMinute)*60 +
                  static_cast<double>(nSecond) +
                  fSecond100)/86400.0;
    return bRet;
}


sal_uInt16 ImpSvNumberInputScan::ImplGetDay( sal_uInt16 nIndex ) const
{
    sal_uInt16 nRes = 0;

    if (sStrArray[nNums[nIndex]].getLength() <= 2)
    {
        sal_uInt16 nNum = static_cast<sal_uInt16>(sStrArray[nNums[nIndex]].toInt32());
        if (nNum <= 31)
        {
            nRes = nNum;
        }
    }

    return nRes;
}


sal_uInt16 ImpSvNumberInputScan::ImplGetMonth( sal_uInt16 nIndex ) const
{
    // Preset invalid month number
    sal_uInt16 nRes = pFormatter->GetCalendar()->getNumberOfMonthsInYear();

    if (sStrArray[nNums[nIndex]].getLength() <= 2)
    {
        sal_uInt16 nNum = static_cast<sal_uInt16>(sStrArray[nNums[nIndex]].toInt32());
        if ( 0 < nNum && nNum <= nRes )
        {
            nRes = nNum - 1; // zero based for CalendarFieldIndex::MONTH
        }
    }

    return nRes;
}


/**
 * 30 -> 1930, 29 -> 2029, or 56 -> 1756, 55 -> 1855, ...
 */
sal_uInt16 ImpSvNumberInputScan::ImplGetYear( sal_uInt16 nIndex )
{
    sal_uInt16 nYear = 0;

    sal_Int32 nLen = sStrArray[nNums[nIndex]].getLength();
    // 16-bit integer year width can have 5 digits, allow for one additional
    // leading zero as convention.
    if (nLen <= 6)
    {
        nYear = static_cast<sal_uInt16>(sStrArray[nNums[nIndex]].toInt32());
        // A year in another, not Gregorian CE era is never expanded.
        // A year < 100 entered with at least 3 digits with leading 0 is taken
        // as is without expansion.
        if (mbEraCE == kDefaultEra && nYear < 100 && nLen < 3)
        {
            nYear = SvNumberFormatter::ExpandTwoDigitYear( nYear, nYear2000 );
        }
    }

    return nYear;
}


bool ImpSvNumberInputScan::MayBeIso8601()
{
    if (nMayBeIso8601 == 0)
    {
        nMayBeIso8601 = 1;
        sal_Int32 nLen = ((nNumericsCnt >= 1 && nNums[0] < nStringsCnt) ? sStrArray[nNums[0]].getLength() : 0);
        if (nLen)
        {
            sal_Int32 n;
            if (nNumericsCnt >= 3 && nNums[2] < nStringsCnt &&
                sStrArray[nNums[0]+1] == "-" && // separator year-month
                (n = sStrArray[nNums[1]].toInt32()) >= 1 && n <= 12 &&  // month
                sStrArray[nNums[1]+1] == "-" && // separator month-day
                (n = sStrArray[nNums[2]].toInt32()) >= 1 && n <= 31)    // day
            {
                // Year (nNums[0]) value not checked, may be anything, but
                // length (number of digits) is checked.
                nMayBeIso8601 = (nLen >= 4 ? 4 : (nLen == 3 ? 3 : (nLen > 0 ? 2 : 1)));
            }
        }
    }
    return nMayBeIso8601 > 1;
}


bool ImpSvNumberInputScan::CanForceToIso8601( DateOrder eDateOrder )
{
    int nCanForceToIso8601 = 0;
    if (!MayBeIso8601())
    {
        return false;
    }
    else if (nMayBeIso8601 >= 3)
    {
        return true;    // at least 3 digits in year
    }
    else
    {
        if (eDateOrder == DateOrder::Invalid)
        {
            // As if any of the cases below can be applied, but only if a
            // locale dependent date pattern was not matched.
            if ((GetDatePatternNumbers() == nNumericsCnt) && IsDatePatternNumberOfType(0,'Y'))
                return false;
            eDateOrder = GetDateOrder();
        }

        nCanForceToIso8601 = 1;
    }

    sal_Int32 n;
    switch (eDateOrder)
    {
        case DateOrder::DMY:               // "day" value out of range => ISO 8601 year
            n = sStrArray[nNums[0]].toInt32();
            if (n < 1 || n > 31)
            {
                nCanForceToIso8601 = 2;
            }
        break;
        case DateOrder::MDY:               // "month" value out of range => ISO 8601 year
            n = sStrArray[nNums[0]].toInt32();
            if (n < 1 || n > 12)
            {
                nCanForceToIso8601 = 2;
            }
        break;
        case DateOrder::YMD:               // always possible
            nCanForceToIso8601 = 2;
        break;
        default: break;
    }
    return nCanForceToIso8601 > 1;
}


bool ImpSvNumberInputScan::IsAcceptableIso8601()
{
    if (mpFormat && (mpFormat->GetType() & SvNumFormatType::DATE))
    {
        switch (pFormatter->GetEvalDateFormat())
        {
            case NF_EVALDATEFORMAT_INTL:
                return CanForceToIso8601( GetDateOrder());
            case NF_EVALDATEFORMAT_FORMAT:
                return CanForceToIso8601( mpFormat->GetDateOrder());
            default:
                return CanForceToIso8601( GetDateOrder()) || CanForceToIso8601( mpFormat->GetDateOrder());
        }
    }
    return CanForceToIso8601( GetDateOrder());
}


bool ImpSvNumberInputScan::MayBeMonthDate()
{
    if (nMayBeMonthDate == 0)
    {
        nMayBeMonthDate = 1;
        if (nNumericsCnt >= 2 && nNums[1] < nStringsCnt)
        {
            // "-Jan-"
            const OUString& rM = sStrArray[ nNums[ 0 ] + 1 ];
            if (rM.getLength() >= 3 && rM[0] == '-' && rM[ rM.getLength() - 1] == '-')
            {
                // Check year length assuming at least 3 digits (including
                // leading zero). Two digit years 1..31 are out of luck here
                // and may be taken as day of month.
                bool bYear1 = (sStrArray[nNums[0]].getLength() >= 3);
                bool bYear2 = (sStrArray[nNums[1]].getLength() >= 3);
                sal_Int32 n;
                bool bDay1 = !bYear1;
                if (bDay1)
                {
                    n = sStrArray[nNums[0]].toInt32();
                    bDay1 = n >= 1 && n <= 31;
                }
                bool bDay2 = !bYear2;
                if (bDay2)
                {
                    n = sStrArray[nNums[1]].toInt32();
                    bDay2 = n >= 1 && n <= 31;
                }

                if (bDay1 && !bDay2)
                {
                    nMayBeMonthDate = 2;        // dd-month-yy
                }
                else if (!bDay1 && bDay2)
                {
                    nMayBeMonthDate = 3;        // yy-month-dd
                }
                else if (bDay1 && bDay2)
                {
                    // Ambiguous ##-MMM-## date, but some big vendor's database
                    // reports write this crap, assume this always to be
                    nMayBeMonthDate = 2;        // dd-month-yy
                }
            }
        }
    }
    return nMayBeMonthDate > 1;
}


/** If a string is a separator plus '-' minus sign preceding a 'Y' year in
    a date pattern at position nPat.
 */
static bool lcl_IsSignedYearSep( std::u16string_view rStr, std::u16string_view rPat, sal_Int32 nPat )
{
    bool bOk = false;
    sal_Int32 nLen = rStr.size();
    if (nLen > 1 && rStr[nLen-1] == '-')
    {
        --nLen;
        if (nPat + nLen < static_cast<sal_Int32>(rPat.size()) && rPat[nPat+nLen] == 'Y')
        {
            // Signed year is possible.
            bOk = (rPat.find( rStr.substr( 0, nLen), nPat) == static_cast<size_t>(nPat));
        }
    }
    return bOk;
}


/** Length of separator usually is 1 but theoretically could be anything. */
static sal_Int32 lcl_getPatternSeparatorLength( std::u16string_view rPat, sal_Int32 nPat )
{
    sal_Int32 nSep = nPat;
    sal_Unicode c;
    while (nSep < static_cast<sal_Int32>(rPat.size()) && (c = rPat[nSep]) != 'D' && c != 'M' && c != 'Y')
        ++nSep;
    return nSep - nPat;
}


bool ImpSvNumberInputScan::IsAcceptedDatePattern( sal_uInt16 nStartPatternAt )
{
    if (nAcceptedDatePattern >= -1)
    {
        return (nAcceptedDatePattern >= 0);
    }
    if (!nNumericsCnt)
    {
        nAcceptedDatePattern = -1;
    }
    else if (!sDateAcceptancePatterns.hasElements())
    {
        // The current locale is the format's locale, if a format is present.
        const NfEvalDateFormat eEDF = pFormatter->GetEvalDateFormat();
        if (!mpFormat || eEDF == NF_EVALDATEFORMAT_FORMAT || mpFormat->GetLanguage() == pFormatter->GetLanguage())
        {
            sDateAcceptancePatterns = pFormatter->GetLocaleData()->getDateAcceptancePatterns();
        }
        else
        {
            OnDemandLocaleDataWrapper& xLocaleData = pFormatter->GetOnDemandLocaleDataWrapper(
                    SvNumberFormatter::InputScannerPrivateAccess());
            const LanguageTag aSaveLocale( xLocaleData->getLanguageTag() );
            assert(mpFormat->GetLanguage() == aSaveLocale.getLanguageType());   // prerequisite
            // Obtain formatter's locale's (e.g. system) patterns.
            xLocaleData.changeLocale( LanguageTag( pFormatter->GetLanguage()));
            const css::uno::Sequence<OUString> aLocalePatterns( xLocaleData->getDateAcceptancePatterns());
            // Reset to format's locale.
            xLocaleData.changeLocale( aSaveLocale);
            // When concatenating don't care about duplicates, combining
            // weeding those out reallocs yet another time and probably doesn't
            // take less time than looping over two additional patterns below...
            switch (eEDF)
            {
                case NF_EVALDATEFORMAT_FORMAT:
                    assert(!"shouldn't reach here");
                break;
                case NF_EVALDATEFORMAT_INTL:
                    sDateAcceptancePatterns = aLocalePatterns;
                break;
                case NF_EVALDATEFORMAT_INTL_FORMAT:
                    sDateAcceptancePatterns = comphelper::concatSequences(
                            aLocalePatterns,
                            xLocaleData->getDateAcceptancePatterns());
                break;
                case NF_EVALDATEFORMAT_FORMAT_INTL:
                    sDateAcceptancePatterns = comphelper::concatSequences(
                            xLocaleData->getDateAcceptancePatterns(),
                            aLocalePatterns);
                break;
            }
        }
        SAL_WARN_IF( !sDateAcceptancePatterns.hasElements(), "svl.numbers", "ImpSvNumberInputScan::IsAcceptedDatePattern: no date acceptance patterns");
        nAcceptedDatePattern = (sDateAcceptancePatterns.hasElements() ? -2 : -1);
    }

    if (nAcceptedDatePattern == -1)
    {
        return false;
    }
    nDatePatternStart = nStartPatternAt; // remember start particle

    const sal_Int32 nMonthsInYear = pFormatter->GetCalendar()->getNumberOfMonthsInYear();

    for (sal_Int32 nPattern=0; nPattern < sDateAcceptancePatterns.getLength(); ++nPattern)
    {
        const OUString& rPat = sDateAcceptancePatterns[nPattern];
        if (rPat.getLength() == 3)
        {
            // Ignore a pattern that would match numeric input with decimal
            // separator. It may had been read from configuration or resulted
            // from the locales' patterns concatenation above.
            if (    rPat[1] == pFormatter->GetLocaleData()->getNumDecimalSep().toChar()
                 || rPat[1] == pFormatter->GetLocaleData()->getNumDecimalSepAlt().toChar())
            {
                SAL_WARN("svl.numbers", "ignoring date acceptance pattern with decimal separator ambiguity: " << rPat);
                continue;   // for, next pattern
            }
        }
        sal_uInt16 nNext = nDatePatternStart;
        nDatePatternNumbers = 0;
        bool bOk = true;
        sal_Int32 nPat = 0;
        for ( ; nPat < rPat.getLength() && bOk && nNext < nStringsCnt; ++nPat, ++nNext)
        {
            const sal_Unicode c = rPat[nPat];
            switch (c)
            {
            case 'Y':
            case 'M':
            case 'D':
                bOk = IsNum[nNext];
                if (bOk && (c == 'M' || c == 'D'))
                {
                    // Check the D and M cases for plausibility. This also
                    // prevents recognition of date instead of number with a
                    // numeric group input if date separator is identical to
                    // group separator, for example with D.M as a pattern and
                    // #.### as a group.
                    sal_Int32 nMaxLen, nMaxVal;
                    switch (c)
                    {
                        case 'M':
                            nMaxLen = 2;
                            nMaxVal = nMonthsInYear;
                            break;
                        case 'D':
                            nMaxLen = 2;
                            nMaxVal = 31;
                            break;
                        default:
                            // This merely exists against
                            // -Werror=maybe-uninitialized, which is nonsense
                            // after the (c == 'M' || c == 'D') check above,
                            // but ...
                            nMaxLen = 2;
                            nMaxVal = 31;
                    }
                    bOk = (sStrArray[nNext].getLength() <= nMaxLen);
                    if (bOk)
                    {
                        sal_Int32 nNum = sStrArray[nNext].toInt32();
                        bOk = (1 <= nNum && nNum <= nMaxVal);
                    }
                }
                if (bOk)
                    ++nDatePatternNumbers;
                break;
            default:
                bOk = !IsNum[nNext];
                if (bOk)
                {
                    const sal_Int32 nSepLen = lcl_getPatternSeparatorLength( rPat, nPat);
                    // Non-numeric input must match separator exactly to be
                    // accepted as such.
                    const sal_Int32 nLen = sStrArray[nNext].getLength();
                    bOk = (nLen == nSepLen && rPat.indexOf( sStrArray[nNext], nPat) == nPat);
                    if (bOk)
                    {
                        nPat += nLen - 1;
                    }
                    else if ((bOk = lcl_IsSignedYearSep( sStrArray[nNext], rPat, nPat)))
                    {
                        nPat += nLen - 2;
                    }
                    else if (nPat + nLen > rPat.getLength() && sStrArray[nNext][ nLen - 1 ] == ' ')
                    {
                        using namespace comphelper::string;
                        // Trailing blanks in input.
                        OUStringBuffer aBuf(sStrArray[nNext]);
                        aBuf.stripEnd();
                        // Expand again in case of pattern "M. D. " and
                        // input "M. D.  ", maybe fetched far, but...
                        padToLength(aBuf, rPat.getLength() - nPat, ' ');
                        bOk = (rPat.indexOf( aBuf, nPat) == nPat);
                        if (bOk)
                        {
                            nPat += aBuf.getLength() - 1;
                        }
                    }
                }
                break;
            }
        }
        if (bOk)
        {
            // Check for trailing characters mismatch.
            if (nNext < nStringsCnt)
            {
                // Pattern end but not input end.
                // A trailing blank may be part of the current pattern input,
                // if pattern is "D.M." and input is "D.M. hh:mm" last was
                // ". ", or may be following the current pattern input, if
                // pattern is "D.M" and input is "D.M hh:mm" last was "M".
                sal_Int32 nPos = 0;
                sal_uInt16 nCheck;
                if (nPat > 0 && nNext > 0)
                {
                    // nPat is one behind after the for loop.
                    sal_Int32 nPatCheck = nPat - 1;
                    switch (rPat[nPatCheck])
                    {
                        case 'Y':
                        case 'M':
                        case 'D':
                            nCheck = nNext;
                            break;
                        default:
                            {
                                nCheck = nNext - 1;
                                // Advance position in input to match length of
                                // non-YMD (separator) characters in pattern.
                                sal_Unicode c;
                                do
                                {
                                    ++nPos;
                                    c = rPat[--nPatCheck];
                                } while (c != 'Y' && c != 'M' && c != 'D' && nPatCheck > 0);
                            }
                    }
                }
                else
                {
                    nCheck = nNext;
                }
                if (!IsNum[nCheck])
                {
                    // Trailing (or separating if time follows) blanks are ok.
                    // No blank and a following number is not.
                    const bool bBlanks = SkipBlanks( sStrArray[nCheck], nPos);
                    if (nPos == sStrArray[nCheck].getLength() && (bBlanks || !IsNum[nNext]))
                    {
                        nAcceptedDatePattern = nPattern;
                        return true;
                    }
                }
            }
            else if (nPat == rPat.getLength())
            {
                // Input end and pattern end => match.
                nAcceptedDatePattern = nPattern;
                return true;
            }
            // else Input end but not pattern end, no match.
        }
    }
    nAcceptedDatePattern = -1;
    return false;
}


bool ImpSvNumberInputScan::SkipDatePatternSeparator( sal_uInt16 nParticle, sal_Int32 & rPos, bool & rSignedYear )
{
    // If not initialized yet start with first number, if any.
    if (!IsAcceptedDatePattern( nNumericsCnt ? nNums[0] : 0 ))
    {
        return false;
    }
    if (nParticle < nDatePatternStart || nParticle >= nStringsCnt || IsNum[nParticle])
    {
        return false;
    }
    sal_uInt16 nNext = nDatePatternStart;
    const OUString& rPat = sDateAcceptancePatterns[nAcceptedDatePattern];
    for (sal_Int32 nPat = 0; nPat < rPat.getLength() && nNext < nStringsCnt; ++nPat, ++nNext)
    {
        switch (rPat[nPat])
        {
        case 'Y':
        case 'M':
        case 'D':
            break;
        default:
            if (nNext == nParticle)
            {
                const sal_Int32 nSepLen = lcl_getPatternSeparatorLength( rPat, nPat);
                const sal_Int32 nLen = sStrArray[nNext].getLength();
                bool bOk = (nLen == nSepLen && rPat.indexOf( sStrArray[nNext], nPat) == nPat);
                if (!bOk)
                {
                    bOk = lcl_IsSignedYearSep( sStrArray[nNext], rPat, nPat);
                    if (bOk)
                        rSignedYear = true;
                }
                if (!bOk && (nPat + nLen > rPat.getLength() && sStrArray[nNext][ nLen - 1 ] == ' '))
                {
                    // The same ugly trailing blanks check as in
                    // IsAcceptedDatePattern().
                    using namespace comphelper::string;
                    OUStringBuffer aBuf(sStrArray[nNext]);
                    aBuf.stripEnd();
                    padToLength(aBuf, rPat.getLength() - nPat, ' ');
                    bOk = (rPat.indexOf(aBuf, nPat) == nPat);
                }
                if (bOk)
                {
                    rPos = nLen; // yes, set, not add!
                    return true;
                }
                else
                    return false;
            }
            nPat += sStrArray[nNext].getLength() - 1;
            break;
        }
    }
    return false;
}


sal_uInt16 ImpSvNumberInputScan::GetDatePatternNumbers()
{
    // If not initialized yet start with first number, if any.
    if (!IsAcceptedDatePattern( nNumericsCnt ? nNums[0] : 0 ))
    {
        return 0;
    }
    return nDatePatternNumbers;
}


bool ImpSvNumberInputScan::IsDatePatternNumberOfType( sal_uInt16 nNumber, sal_Unicode cType )
{
    if (GetDatePatternNumbers() <= nNumber)
        return false;

    sal_uInt16 nNum = 0;
    const OUString& rPat = sDateAcceptancePatterns[nAcceptedDatePattern];
    for (sal_Int32 nPat = 0; nPat < rPat.getLength(); ++nPat)
    {
        switch (rPat[nPat])
        {
            case 'Y':
            case 'M':
            case 'D':
                if (nNum == nNumber)
                    return rPat[nPat] == cType;
                ++nNum;
            break;
        }
    }
    return false;
}


sal_uInt32 ImpSvNumberInputScan::GetDatePatternOrder()
{
    // If not initialized yet start with first number, if any.
    if (!IsAcceptedDatePattern( nNumericsCnt ? nNums[0] : 0 ))
    {
        return 0;
    }
    sal_uInt32 nOrder = 0;
    const OUString& rPat = sDateAcceptancePatterns[nAcceptedDatePattern];
    for (sal_Int32 nPat = 0; nPat < rPat.getLength() && !(nOrder & 0xff0000); ++nPat)
    {
        switch (rPat[nPat])
        {
        case 'Y':
        case 'M':
        case 'D':
            nOrder = (nOrder << 8) | rPat[nPat];
            break;
        }
    }
    return nOrder;
}


DateOrder ImpSvNumberInputScan::GetDateOrder( bool bFromFormatIfNoPattern )
{
    sal_uInt32 nOrder = GetDatePatternOrder();
    if (!nOrder)
    {
        if (bFromFormatIfNoPattern && mpFormat)
            return mpFormat->GetDateOrder();
        else
            return pFormatter->GetLocaleData()->getDateOrder();
    }
    switch ((nOrder & 0xff0000) >> 16)
    {
    case 'Y':
        if ((((nOrder & 0xff00) >> 8) == 'M') && ((nOrder & 0xff) == 'D'))
        {
            return DateOrder::YMD;
        }
        break;
    case 'M':
        if ((((nOrder & 0xff00) >> 8) == 'D') && ((nOrder & 0xff) == 'Y'))
        {
            return DateOrder::MDY;
        }
        break;
    case 'D':
        if ((((nOrder & 0xff00) >> 8) == 'M') && ((nOrder & 0xff) == 'Y'))
        {
            return DateOrder::DMY;
        }
        break;
    default:
    case 0:
        switch ((nOrder & 0xff00) >> 8)
        {
        case 'Y':
            switch (nOrder & 0xff)
            {
            case 'M':
                return DateOrder::YMD;
            }
            break;
        case 'M':
            switch (nOrder & 0xff)
            {
            case 'Y':
                return DateOrder::DMY;
            case 'D':
                return DateOrder::MDY;
            }
            break;
        case 'D':
            switch (nOrder & 0xff)
            {
            case 'Y':
                return DateOrder::MDY;
            case 'M':
                return DateOrder::DMY;
            }
            break;
        default:
        case 0:
            switch (nOrder & 0xff)
            {
            case 'Y':
                return DateOrder::YMD;
            case 'M':
                return DateOrder::MDY;
            case 'D':
                return DateOrder::DMY;
            }
            break;
        }
    }
    SAL_WARN( "svl.numbers", "ImpSvNumberInputScan::GetDateOrder: undefined, falling back to locale's default");
    return pFormatter->GetLocaleData()->getDateOrder();
}

LongDateOrder ImpSvNumberInputScan::GetMiddleMonthLongDateOrder( bool bFormatTurn,
                                                                 const LocaleDataWrapper* pLoc,
                                                                 DateOrder eDateOrder )
{
    if (MayBeMonthDate())
        return (nMayBeMonthDate == 2) ? LongDateOrder::DMY : LongDateOrder::YMD;

    LongDateOrder eLDO;
    const sal_uInt32 nExactDateOrder = (bFormatTurn ? mpFormat->GetExactDateOrder() : 0);
    if (!nExactDateOrder)
        eLDO = pLoc->getLongDateOrder();
    else if ((((nExactDateOrder >> 16) & 0xff) == 'Y') && ((nExactDateOrder & 0xff) == 'D'))
        eLDO = LongDateOrder::YMD;
    else if ((((nExactDateOrder >> 16) & 0xff) == 'D') && ((nExactDateOrder & 0xff) == 'Y'))
        eLDO = LongDateOrder::DMY;
    else
        eLDO = pLoc->getLongDateOrder();
    if (eLDO != LongDateOrder::YMD && eLDO != LongDateOrder::DMY)
    {
        switch (eDateOrder)
        {
            case DateOrder::YMD:
                eLDO = LongDateOrder::YMD;
            break;
            case DateOrder::DMY:
                eLDO = LongDateOrder::DMY;
            break;
            default:
                ;   // nothing, not a date
        }
    }
    else if (eLDO == LongDateOrder::DMY && eDateOrder == DateOrder::YMD)
    {
        // Check possible order and maybe switch.
        if (!ImplGetDay(0) && ImplGetDay(1))
            eLDO = LongDateOrder::YMD;
    }
    else if (eLDO == LongDateOrder::YMD && eDateOrder == DateOrder::DMY)
    {
        // Check possible order and maybe switch.
        if (!ImplGetDay(1) && ImplGetDay(0))
            eLDO = LongDateOrder::DMY;
    }
    return eLDO;
}

bool ImpSvNumberInputScan::GetDateRef( double& fDays, sal_uInt16& nCounter )
{
    using namespace ::com::sun::star::i18n;
    NfEvalDateFormat eEDF;
    int nFormatOrder;
    if ( mpFormat && (mpFormat->GetType() & SvNumFormatType::DATE) )
    {
        eEDF = pFormatter->GetEvalDateFormat();
        switch ( eEDF )
        {
        case NF_EVALDATEFORMAT_INTL :
        case NF_EVALDATEFORMAT_FORMAT :
            nFormatOrder = 1; // only one loop
            break;
        default:
            nFormatOrder = 2;
            if ( nMatchedAllStrings )
            {
                eEDF = NF_EVALDATEFORMAT_FORMAT_INTL;
                // we have a complete match, use it
            }
        }
    }
    else
    {
        eEDF = NF_EVALDATEFORMAT_INTL;
        nFormatOrder = 1;
    }
    bool res = true;

    const LocaleDataWrapper* pLoc = pFormatter->GetLocaleData();
    CalendarWrapper* pCal = pFormatter->GetCalendar();
    for ( int nTryOrder = 1; nTryOrder <= nFormatOrder; nTryOrder++ )
    {
        pCal->setGregorianDateTime( Date( Date::SYSTEM ) ); // today
        OUString aOrgCalendar; // empty => not changed yet
        DateOrder DateFmt;
        bool bFormatTurn;
        switch ( eEDF )
        {
        case NF_EVALDATEFORMAT_INTL :
            bFormatTurn = false;
            DateFmt = GetDateOrder();
            break;
        case NF_EVALDATEFORMAT_FORMAT :
            bFormatTurn = true;
            DateFmt = mpFormat->GetDateOrder();
            break;
        case NF_EVALDATEFORMAT_INTL_FORMAT :
            if ( nTryOrder == 1 )
            {
                bFormatTurn = false;
                DateFmt = GetDateOrder();
            }
            else
            {
                bFormatTurn = true;
                DateFmt = mpFormat->GetDateOrder();
            }
            break;
        case NF_EVALDATEFORMAT_FORMAT_INTL :
            if ( nTryOrder == 2 )
            {
                bFormatTurn = false;
                DateFmt = GetDateOrder();
            }
            else
            {
                bFormatTurn = true;
                // Even if the format pattern is to be preferred, the input may
                // have matched a pattern of the current locale, which then
                // again is to be preferred. Both date orders can be different
                // so we need to obtain the actual match. For example ISO
                // YYYY-MM-DD format vs locale's DD.MM.YY input.
                // If no pattern was matched, obtain from format.
                // Note that patterns may have been constructed from the
                // format's locale and prepended to the current locale's
                // patterns, it doesn't necessarily mean a current locale's
                // pattern was matched, but may if the format's locale's
                // patterns didn't match, which were tried first.
                DateFmt = GetDateOrder(true);
            }
            break;
        default:
            SAL_WARN( "svl.numbers", "ImpSvNumberInputScan::GetDateRef: unknown NfEvalDateFormat" );
            DateFmt = DateOrder::YMD;
            bFormatTurn = false;
        }
        if ( bFormatTurn )
        {
/* TODO:
We are currently not able to fully support a switch to another calendar during
input for the following reasons:
1. We do have a problem if both (locale's default and format's) calendars
   define the same YMD order and use the same date separator, there is no way
   to distinguish between them if the input results in valid calendar input for
   both calendars. How to solve? Would NfEvalDateFormat be sufficient? Should
   it always be set to NF_EVALDATEFORMAT_FORMAT_INTL and thus the format's
   calendar be preferred? This could be confusing if a Calc cell was formatted
   different to the locale's default and has no content yet, then the user has
   no clue about the format or calendar being set.
2. In Calc cell edit mode a date is always displayed and edited using the
   default edit format of the default calendar (normally being Gregorian). If
   input was ambiguous due to issue #1 we'd need a mechanism to tell that a
   date was edited and not newly entered. Not feasible. Otherwise we'd need a
   mechanism to use a specific edit format with a specific calendar according
   to the format set.
3. For some calendars like Japanese Gengou we'd need era input, which isn't
   implemented at all. Though this is a rare and special case, forcing a
   calendar dependent edit format as suggested in item #2 might require era
   input, if it shouldn't result in a fallback to Gregorian calendar.
4. Last and least: the GetMonth() method currently only matches month names of
   the default calendar. Alternating month names of the actual format's
   calendar would have to be implemented. No problem.

*/
#ifdef THE_FUTURE
            if ( mpFormat->IsOtherCalendar( nStringScanNumFor ) )
            {
                mpFormat->SwitchToOtherCalendar( aOrgCalendar, fOrgDateTime );
            }
            else
            {
                mpFormat->SwitchToSpecifiedCalendar( aOrgCalendar, fOrgDateTime,
                                                    nStringScanNumFor );
            }
#endif
        }

        res = true;
        nCounter = 0;
        // For incomplete dates, always assume first day of month if not specified.
        pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, 1 );

        switch (nNumericsCnt) // count of numbers in string
        {
        case 0:                 // none
            if (nMonthPos)      // only month (Jan)
            {
                pCal->setValue( CalendarFieldIndex::MONTH, std::abs(nMonth)-1 );
            }
            else
            {
                res = false;
            }
            break;

        case 1:                 // only one number
            nCounter = 1;
            switch (nMonthPos)  // where is the month
            {
            case 0:             // not found
            {
                // If input matched a date pattern, use the pattern
                // to determine if it is a day, month or year. The
                // pattern should have only one single value then,
                // 'D-', 'M-' or 'Y-'. If input did not match a
                // pattern assume the usual day of current month.
                sal_uInt32 nDateOrder = (bFormatTurn ?
                                         mpFormat->GetExactDateOrder() :
                                         GetDatePatternOrder());
                switch (nDateOrder)
                {
                case 'Y':
                    pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(0) );
                    break;
                case 'M':
                    pCal->setValue( CalendarFieldIndex::MONTH, ImplGetMonth(0) );
                    break;
                case 'D':
                default:
                    pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(0) );
                    break;
                }
                break;
            }
            case 1:             // month at the beginning (Jan 01)
                pCal->setValue( CalendarFieldIndex::MONTH, std::abs(nMonth)-1 );
                switch (DateFmt)
                {
                case DateOrder::MDY:
                case DateOrder::YMD:
                {
                    sal_uInt16 nDay = ImplGetDay(0);
                    sal_uInt16 nYear = ImplGetYear(0);
                    if (nDay == 0 || nDay > 32)
                    {
                        pCal->setValue( CalendarFieldIndex::YEAR, nYear);
                    }
                    else
                    {
                        pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(0) );
                    }
                    break;
                }
                case DateOrder::DMY:
                    pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(0) );
                    break;
                default:
                    res = false;
                    break;
                }
                break;
            case 3:             // month at the end (10 Jan)
                pCal->setValue( CalendarFieldIndex::MONTH, std::abs(nMonth)-1 );
                switch (DateFmt)
                {
                case DateOrder::DMY:
                    pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(0) );
                    break;
                case DateOrder::YMD:
                    pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(0) );
                    break;
                default:
                    res = false;
                    break;
                }
                break;
            default:
                res = false;
                break;
            }   // switch (nMonthPos)
            break;

        case 2:                 // 2 numbers
            nCounter = 2;
            switch (nMonthPos)  // where is the month
            {
            case 0:             // not found
            {
                sal_uInt32 nExactDateOrder = (bFormatTurn ?
                                              mpFormat->GetExactDateOrder() :
                                              GetDatePatternOrder());
                bool bIsExact = (0xff < nExactDateOrder && nExactDateOrder <= 0xffff);
                if (!bIsExact && bFormatTurn && IsAcceptedDatePattern( nNums[0]))
                {
                    // If input does not match format but pattern, use pattern
                    // instead, even if eEDF==NF_EVALDATEFORMAT_FORMAT_INTL.
                    // For example, format has "Y-M-D" and pattern is "D.M.",
                    // input with 2 numbers can't match format and 31.12. would
                    // lead to 1931-12-01 (fdo#54344)
                    nExactDateOrder = GetDatePatternOrder();
                    bIsExact = (0xff < nExactDateOrder && nExactDateOrder <= 0xffff);
                }
                bool bHadExact;
                if (bIsExact)
                {
                    // formatted as date and exactly 2 parts
                    bHadExact = true;
                    switch ( (nExactDateOrder >> 8) & 0xff )
                    {
                    case 'Y':
                        pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(0) );
                        break;
                    case 'M':
                        pCal->setValue( CalendarFieldIndex::MONTH, ImplGetMonth(0) );
                        break;
                    case 'D':
                        pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(0) );
                        break;
                    default:
                        bHadExact = false;
                    }
                    switch ( nExactDateOrder & 0xff )
                    {
                    case 'Y':
                        pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(1) );
                        break;
                    case 'M':
                        pCal->setValue( CalendarFieldIndex::MONTH, ImplGetMonth(1) );
                        break;
                    case 'D':
                        pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(1) );
                        break;
                    default:
                        bHadExact = false;
                    }
                    SAL_WARN_IF( !bHadExact, "svl.numbers", "ImpSvNumberInputScan::GetDateRef: error in exact date order");
                }
                else
                {
                    bHadExact = false;
                }
                // If input matched against a date acceptance pattern
                // do not attempt to mess around with guessing the
                // order, either it matches or it doesn't.
                if ((bFormatTurn || !bIsExact) && (!bHadExact || !pCal->isValid()))
                {
                    if ( !bHadExact && nExactDateOrder )
                    {
                        pCal->setGregorianDateTime( Date( Date::SYSTEM ) ); // reset today
                    }
                    switch (DateFmt)
                    {
                    case DateOrder::MDY:
                        // M D
                        pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(1) );
                        pCal->setValue( CalendarFieldIndex::MONTH, ImplGetMonth(0) );
                        if ( !pCal->isValid() )             // 2nd try
                        {                                   // M Y
                            pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, 1 );
                            pCal->setValue( CalendarFieldIndex::MONTH, ImplGetMonth(0) );
                            pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(1) );
                        }
                        break;
                    case DateOrder::DMY:
                        // D M
                        pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(0) );
                        pCal->setValue( CalendarFieldIndex::MONTH, ImplGetMonth(1) );
                        if ( !pCal->isValid() )             // 2nd try
                        {                                   // M Y
                            pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, 1 );
                            pCal->setValue( CalendarFieldIndex::MONTH, ImplGetMonth(0) );
                            pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(1) );
                        }
                        break;
                    case DateOrder::YMD:
                        // M D
                        pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(1) );
                        pCal->setValue( CalendarFieldIndex::MONTH, ImplGetMonth(0) );
                        if ( !pCal->isValid() )             // 2nd try
                        {                                   // Y M
                            pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, 1 );
                            pCal->setValue( CalendarFieldIndex::MONTH, ImplGetMonth(1) );
                            pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(0) );
                        }
                        break;
                    default:
                        res = false;
                        break;
                    }
                }
            }
            break;
            case 1:             // month at the beginning (Jan 01 01)
            {
                // The input is valid as MDY in almost any
                // constellation, there is no date order (M)YD except if
                // set in a format applied.
                pCal->setValue( CalendarFieldIndex::MONTH, std::abs(nMonth)-1 );
                sal_uInt32 nExactDateOrder = (bFormatTurn ? mpFormat->GetExactDateOrder() : 0);
                if ((((nExactDateOrder >> 8) & 0xff) == 'Y') && ((nExactDateOrder & 0xff) == 'D'))
                {
                    pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(1) );
                    pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(0) );
                }
                else
                {
                    pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(0) );
                    pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(1) );
                }
                break;
            }
            case 2:             // month in the middle (10 Jan 94)
            {
                pCal->setValue( CalendarFieldIndex::MONTH, std::abs(nMonth)-1 );
                const LongDateOrder eLDO = GetMiddleMonthLongDateOrder( bFormatTurn, pLoc, DateFmt);
                switch (eLDO)
                {
                case LongDateOrder::DMY:
                    pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(0) );
                    pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(1) );
                    break;
                case LongDateOrder::YMD:
                    pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(1) );
                    pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(0) );
                    break;
                default:
                    res = false;
                    break;
                }
                break;
            }
            case 3:             // month at the end (94 10 Jan)
                if (pLoc->getLongDateOrder() != LongDateOrder::YDM)
                    res = false;
                else
                {
                    pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(1) );
                    pCal->setValue( CalendarFieldIndex::MONTH, std::abs(nMonth)-1 );
                    pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(0) );
                }
                break;
            default:
                res = false;
                break;
            }   // switch (nMonthPos)
            break;

        default:                // more than two numbers (31.12.94 8:23) (31.12. 8:23)
            switch (nMonthPos)  // where is the month
            {
            case 0:             // not found
            {
                nCounter = 3;
                if ( nTimePos > 1 )
                {   // find first time number index (should only be 3 or 2 anyway)
                    for ( sal_uInt16 j = 0; j < nNumericsCnt; j++ )
                    {
                        if ( nNums[j] == nTimePos - 2 )
                        {
                            nCounter = j;
                            break; // for
                        }
                    }
                }
                // ISO 8601 yyyy-mm-dd forced recognition
                DateOrder eDF = (CanForceToIso8601( DateFmt) ? DateOrder::YMD : DateFmt);
                switch (eDF)
                {
                case DateOrder::MDY:
                    pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(1) );
                    pCal->setValue( CalendarFieldIndex::MONTH, ImplGetMonth(0) );
                    if ( nCounter > 2 )
                        pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(2) );
                    break;
                case DateOrder::DMY:
                    pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(0) );
                    pCal->setValue( CalendarFieldIndex::MONTH, ImplGetMonth(1) );
                    if ( nCounter > 2 )
                        pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(2) );
                    break;
                case DateOrder::YMD:
                    if ( nCounter > 2 )
                        pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(2) );
                    pCal->setValue( CalendarFieldIndex::MONTH, ImplGetMonth(1) );
                    pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(0) );
                    break;
                default:
                    res = false;
                    break;
                }
                break;
            }
            case 1:             // month at the beginning (Jan 01 01 8:23)
            {
                nCounter = 2;
                // The input is valid as MDY in almost any
                // constellation, there is no date order (M)YD except if
                // set in a format applied.
                pCal->setValue( CalendarFieldIndex::MONTH, std::abs(nMonth)-1 );
                sal_uInt32 nExactDateOrder = (bFormatTurn ? mpFormat->GetExactDateOrder() : 0);
                if ((((nExactDateOrder >> 8) & 0xff) == 'Y') && ((nExactDateOrder & 0xff) == 'D'))
                {
                    pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(1) );
                    pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(0) );
                }
                else
                {
                    pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(0) );
                    pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(1) );
                }
                break;
            }
            case 2:             // month in the middle (10 Jan 94 8:23)
            {
                nCounter = 2;
                pCal->setValue( CalendarFieldIndex::MONTH, std::abs(nMonth)-1 );
                const LongDateOrder eLDO = GetMiddleMonthLongDateOrder( bFormatTurn, pLoc, DateFmt);
                switch (eLDO)
                {
                case LongDateOrder::DMY:
                    pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(0) );
                    pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(1) );
                    break;
                case LongDateOrder::YMD:
                    pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(1) );
                    pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(0) );
                    break;
                default:
                    res = false;
                    break;
                }
                break;
            }
            case 3:            // month at the end (94 10 Jan 8:23)
                nCounter = 2;
                if (pLoc->getLongDateOrder() != LongDateOrder::YDM)
                    res = false;
                else
                {
                    pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(1) );
                    pCal->setValue( CalendarFieldIndex::MONTH, std::abs(nMonth)-1 );
                    pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(0) );
                }
                break;
            default:
                nCounter = 2;
                res = false;
                break;
            }   // switch (nMonthPos)
            break;
        }   // switch (nNumericsCnt)

        if (mbEraCE != kDefaultEra)
            pCal->setValue( CalendarFieldIndex::ERA, mbEraCE ? 1 : 0);

        if ( res && pCal->isValid() )
        {
            double fDiff = DateTime::Sub( DateTime(*moNullDate), pCal->getEpochStart());
            fDays = ::rtl::math::approxFloor( pCal->getLocalDateTime() );
            fDays -= fDiff;
            nTryOrder = nFormatOrder; // break for
        }
        else
        {
            res = false;
        }
        if ( aOrgCalendar.getLength() )
        {
            pCal->loadCalendar( aOrgCalendar, pLoc->getLanguageTag().getLocale() ); // restore calendar
        }
#if NF_TEST_CALENDAR
        {
            using namespace ::com::sun::star;
            struct entry { const char* lan; const char* cou; const char* cal; };
            const entry cals[] = {
                { "en", "US",  "gregorian" },
                { "ar", "TN",      "hijri" },
                { "he", "IL",     "jewish" },
                { "ja", "JP",     "gengou" },
                { "ko", "KR", "hanja_yoil" },
                { "th", "TH",   "buddhist" },
                { "zh", "TW",        "ROC" },
                {0,0,0}
            };
            lang::Locale aLocale;
            bool bValid;
            sal_Int16 nDay, nMyMonth, nYear, nHour, nMinute, nSecond;
            sal_Int16 nDaySet, nMonthSet, nYearSet, nHourSet, nMinuteSet, nSecondSet;
            sal_Int16 nZO, nDST1, nDST2, nDST, nZOmillis, nDST1millis, nDST2millis, nDSTmillis;
            sal_Int32 nZoneInMillis, nDST1InMillis, nDST2InMillis;
            uno::Reference< uno::XComponentContext > xContext =
                ::comphelper::getProcessComponentContext();
            uno::Reference< i18n::XCalendar4 > xCal = i18n::LocaleCalendar2::create(xContext);
            for ( const entry* p = cals; p->lan; ++p )
            {
                aLocale.Language = OUString::createFromAscii( p->lan );
                aLocale.Country  = OUString::createFromAscii( p->cou );
                xCal->loadCalendar( OUString::createFromAscii( p->cal ),
                                    aLocale );
                double nDateTime = 0.0; // 1-Jan-1970 00:00:00
                nZO           = xCal->getValue( i18n::CalendarFieldIndex::ZONE_OFFSET );
                nZOmillis     = xCal->getValue( i18n::CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS );
                nZoneInMillis = static_cast<sal_Int32>(nZO) * 60000 +
                    (nZO < 0 ? -1 : 1) * static_cast<sal_uInt16>(nZOmillis);
                nDST1         = xCal->getValue( i18n::CalendarFieldIndex::DST_OFFSET );
                nDST1millis   = xCal->getValue( i18n::CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS );
                nDST1InMillis = static_cast<sal_Int32>(nDST1) * 60000 +
                    (nDST1 < 0 ? -1 : 1) * static_cast<sal_uInt16>(nDST1millis);
                nDateTime    -= (double)(nZoneInMillis + nDST1InMillis) / 1000.0 / 60.0 / 60.0 / 24.0;
                xCal->setDateTime( nDateTime );
                nDST2         = xCal->getValue( i18n::CalendarFieldIndex::DST_OFFSET );
                nDST2millis   = xCal->getValue( i18n::CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS );
                nDST2InMillis = static_cast<sal_Int32>(nDST2) * 60000 +
                    (nDST2 < 0 ? -1 : 1) * static_cast<sal_uInt16>(nDST2millis);
                if ( nDST1InMillis != nDST2InMillis )
                {
                    nDateTime = 0.0 - (double)(nZoneInMillis + nDST2InMillis) / 1000.0 / 60.0 / 60.0 / 24.0;
                    xCal->setDateTime( nDateTime );
                }
                nDaySet    = xCal->getValue( i18n::CalendarFieldIndex::DAY_OF_MONTH );
                nMonthSet  = xCal->getValue( i18n::CalendarFieldIndex::MONTH );
                nYearSet   = xCal->getValue( i18n::CalendarFieldIndex::YEAR );
                nHourSet   = xCal->getValue( i18n::CalendarFieldIndex::HOUR );
                nMinuteSet = xCal->getValue( i18n::CalendarFieldIndex::MINUTE );
                nSecondSet = xCal->getValue( i18n::CalendarFieldIndex::SECOND );
                nZO        = xCal->getValue( i18n::CalendarFieldIndex::ZONE_OFFSET );
                nZOmillis  = xCal->getValue( i18n::CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS );
                nDST       = xCal->getValue( i18n::CalendarFieldIndex::DST_OFFSET );
                nDSTmillis = xCal->getValue( i18n::CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS );
                xCal->setValue( i18n::CalendarFieldIndex::DAY_OF_MONTH, nDaySet );
                xCal->setValue( i18n::CalendarFieldIndex::MONTH, nMonthSet );
                xCal->setValue( i18n::CalendarFieldIndex::YEAR, nYearSet );
                xCal->setValue( i18n::CalendarFieldIndex::HOUR, nHourSet );
                xCal->setValue( i18n::CalendarFieldIndex::MINUTE, nMinuteSet );
                xCal->setValue( i18n::CalendarFieldIndex::SECOND, nSecondSet );
                bValid  = xCal->isValid();
                nDay    = xCal->getValue( i18n::CalendarFieldIndex::DAY_OF_MONTH );
                nMyMonth= xCal->getValue( i18n::CalendarFieldIndex::MONTH );
                nYear   = xCal->getValue( i18n::CalendarFieldIndex::YEAR );
                nHour   = xCal->getValue( i18n::CalendarFieldIndex::HOUR );
                nMinute = xCal->getValue( i18n::CalendarFieldIndex::MINUTE );
                nSecond = xCal->getValue( i18n::CalendarFieldIndex::SECOND );
                bValid = bValid && nDay == nDaySet && nMyMonth == nMonthSet && nYear ==
                    nYearSet && nHour == nHourSet && nMinute == nMinuteSet && nSecond
                    == nSecondSet;
            }
        }
#endif  // NF_TEST_CALENDAR

    }

    return res;
}


/**
 * Analyze first string
 * All gone => true
 * else     => false
 */
bool ImpSvNumberInputScan::ScanStartString( const OUString& rString )
{
    sal_Int32 nPos = 0;

    // First of all, eat leading blanks
    SkipBlanks(rString, nPos);

    // Yes, nMatchedAllStrings should know about the sign position
    nSign = GetSign(rString, nPos);
    if ( nSign )           // sign?
    {
        SkipBlanks(rString, nPos);
    }
    // #102371# match against format string only if start string is not a sign character
    if ( nMatchedAllStrings && !(nSign && rString.getLength() == 1) )
    {
        // Match against format in any case, so later on for a "x1-2-3" input
        // we may distinguish between a xy-m-d (or similar) date and a x0-0-0
        // format. No sign detection here!
        if ( ScanStringNumFor( rString, nPos, 0, true ) )
        {
            nMatchedAllStrings |= nMatchedStartString;
        }
        else
        {
            nMatchedAllStrings = 0;
        }
    }

    // Bail out early for just a sign.
    if (nSign && nPos == rString.getLength())
        return true;

    const sal_Int32 nStartBlanks = nPos;
    if ( GetDecSep(rString, nPos) )                 // decimal separator in start string
    {
        if (SkipBlanks(rString, nPos))
            nPos = nStartBlanks;                    // `. 2` not a decimal separator
        else
            nDecPos = 1;                            // leading decimal separator
    }
    else if ( GetCurrency(rString, nPos) )          // currency (DM 1)?
    {
        eScannedType = SvNumFormatType::CURRENCY;       // !!! it IS currency !!!
        SkipBlanks(rString, nPos);
        if (nSign == 0)                             // no sign yet
        {
            nSign = GetSign(rString, nPos);
            if ( nSign )   // DM -1
            {
                SkipBlanks(rString, nPos);
            }
        }
        if ( GetDecSep(rString, nPos) )             // decimal separator follows currency
        {
            if (SkipBlanks(rString, nPos))
            {
                nPos = nStartBlanks;                // `DM . 2` not a decimal separator
                eScannedType = SvNumFormatType::UNDEFINED;  // !!! it is NOT currency !!!
            }
            else
                nDecPos = 1;                        // leading decimal separator
        }
    }
    else
    {
        const sal_Int32 nMonthStart = nPos;
        short nTempMonth = GetMonth(rString, nPos);
        if (nTempMonth < 0)
        {
            // Short month and day names may be identical in some locales, e.g.
            // "mar" for "martes" or "marzo" in Spanish.
            // Do not let a month name immediately take precedence if a day
            // name was meant instead. Assume that both could be valid, until
            // encountered differently or the final evaluation in
            // IsNumberFormat() checks, but continue with weighing the month
            // name higher unless we have both day of week and month name here.
            sal_Int32 nTempPos = nMonthStart;
            nDayOfWeek = GetDayOfWeek( rString, nTempPos);
            if (nDayOfWeek < 0)
            {
                SkipChar( '.', rString, nTempPos ); // abbreviated
                SkipString( pFormatter->GetLocaleData()->getLongDateDayOfWeekSep(), rString, nTempPos );
                SkipBlanks( rString, nTempPos);
                short nTempTempMonth = GetMonth( rString, nTempPos);
                if (nTempTempMonth)
                {
                    // Fall into the else branch below that handles both.
                    nTempMonth = 0;
                    nPos = nMonthStart;
                    nDayOfWeek = 0;
                    // Do not set nDayOfWeek hereafter, anywhere.
                }
            }
        }
        if ( nTempMonth )    // month (Jan 1)?
        {
            // Jan1 without separator is not a date, unless it is followed by a
            // separator and a (year) number.
            if (nPos < rString.getLength() || (nStringsCnt >= 4 && nNumericsCnt >= 2))
            {
                eScannedType = SvNumFormatType::DATE;   // !!! it IS a date !!!
                nMonth = nTempMonth;
                nMonthPos = 1;                      // month at the beginning
                if ( nMonth < 0 )
                {
                    SkipChar( '.', rString, nPos ); // abbreviated
                }
                SkipBlanks(rString, nPos);
            }
            else
            {
                nPos = nMonthStart;                 // rewind month
            }
        }
        else
        {
            int nTempDayOfWeek = GetDayOfWeek( rString, nPos );
            if ( nTempDayOfWeek )
            {
                // day of week is just parsed away
                eScannedType = SvNumFormatType::DATE;       // !!! it IS a date !!!
                if ( nPos < rString.getLength() )
                {
                    if ( nTempDayOfWeek < 0 )
                    {
                        // abbreviated
                        if ( rString[ nPos ] == '.' )
                        {
                            ++nPos;
                        }
                    }
                    else
                    {
                        // full long name
                        SkipBlanks(rString, nPos);
                        SkipString( pFormatter->GetLocaleData()->getLongDateDayOfWeekSep(), rString, nPos );
                    }
                    SkipBlanks(rString, nPos);
                    nTempMonth = GetMonth(rString, nPos);
                    if ( nTempMonth ) // month (Jan 1)?
                    {
                        // Jan1 without separator is not a date, unless it is followed by a
                        // separator and a (year) number.
                        if (nPos < rString.getLength() || (nStringsCnt >= 4 && nNumericsCnt >= 2))
                        {
                            nMonth = nTempMonth;
                            nMonthPos = 1; // month at the beginning
                            if ( nMonth < 0 )
                            {
                                SkipChar( '.', rString, nPos ); // abbreviated
                            }
                            SkipBlanks(rString, nPos);
                        }
                        else
                        {
                            nPos = nMonthStart;                 // rewind month
                        }
                    }
                }
                if (!nMonth)
                {
                    // Determine and remember following date pattern, if any.
                    IsAcceptedDatePattern( 1);
                }
            }
        }
        // Skip one trailing '-' or '/' character to recognize June-2007
        if (nMonth && nPos + 1 == rString.getLength())
        {
            SkipChar('-', rString, nPos) || SkipChar('/', rString, nPos);
        }
    }

    if (nPos < rString.getLength()) // not everything consumed
    {
        // Does input StartString equal StartString of format?
        // This time with sign detection!
        if ( !ScanStringNumFor( rString, nPos, 0 ) )
        {
            return MatchedReturn();
        }
    }

    return true;
}


/**
 * Analyze string in the middle
 * All gone => true
 * else     => false
 */
bool ImpSvNumberInputScan::ScanMidString( const OUString& rString, sal_uInt16 nStringPos, sal_uInt16 nCurNumCount )
{
    sal_Int32 nPos = 0;
    SvNumFormatType eOldScannedType = eScannedType;

    if ( nMatchedAllStrings )
    {   // Match against format in any case, so later on for a "1-2-3-4" input
        // we may distinguish between a y-m-d (or similar) date and a 0-0-0-0
        // format.
        if ( ScanStringNumFor( rString, 0, nStringPos ) )
        {
            nMatchedAllStrings |= nMatchedMidString;
        }
        else
        {
            nMatchedAllStrings = 0;
        }
    }

    const sal_Int32 nStartBlanks = nPos;
    const bool bBlanks = SkipBlanks(rString, nPos);
    if (GetDecSep(rString, nPos))                   // decimal separator?
    {
        if (nDecPos == 1 || nDecPos == 3)           // .12.4 or 1.E2.1
        {
            return MatchedReturn();
        }
        else if (nDecPos == 2)                      // . dup: 12.4.
        {
            bool bSignedYear = false;
            if (bDecSepInDateSeps ||                // . also date separator
                SkipDatePatternSeparator( nStringPos, nPos, bSignedYear))
            {
                if ( eScannedType != SvNumFormatType::UNDEFINED &&
                     eScannedType != SvNumFormatType::DATE &&
                     eScannedType != SvNumFormatType::DATETIME)  // already another type
                {
                    return MatchedReturn();
                }
                if (eScannedType == SvNumFormatType::UNDEFINED)
                {
                    eScannedType = SvNumFormatType::DATE; // !!! it IS a date
                }
                SkipBlanks(rString, nPos);
            }
            else
            {
                return MatchedReturn();
            }
        }
        else if (bBlanks)
        {
            // `1 .2` or `1 . 2` not a decimal separator, reset
            nPos = nStartBlanks;
        }
        else if (SkipBlanks(rString, nPos))
        {
            // `1. 2` not a decimal separator, reset
            nPos = nStartBlanks;
        }
        else
        {
            nDecPos = 2;                            // . in mid string
        }
    }
    else if ( (eScannedType & SvNumFormatType::TIME) &&
              GetTime100SecSep( rString, nPos ) )
    {                                               // hundredth seconds separator
        if ( nDecPos )
        {
            return MatchedReturn();
        }
        nDecPos = 2;                                // . in mid string

        // If this is exactly an ISO 8601 fractional seconds separator, bail
        // out early to not get confused by later checks for group separator or
        // other.
        if (bIso8601Tsep && nPos == rString.getLength() &&
                eScannedType == SvNumFormatType::DATETIME && (rString == "." || rString == ","))
            return true;

        SkipBlanks(rString, nPos);
    }

    if (SkipChar('/', rString, nPos))               // fraction?
    {
        if ( eScannedType != SvNumFormatType::UNDEFINED &&  // already another type
             eScannedType != SvNumFormatType::DATE)         // except date
        {
            return MatchedReturn();                         // => jan/31/1994
        }
        else if (eScannedType != SvNumFormatType::DATE &&   // analyzed no date until now
                 (eSetType == SvNumFormatType::FRACTION ||  // and preset was fraction
                  (nNumericsCnt == 3 &&                     // or 3 numbers
                   (nStringPos == 3 ||                      // and 4th string particle
                    (nStringPos == 4 && nSign)) &&          // or 5th if signed
                   sStrArray[nStringPos-2].indexOf('/') == -1)))  // and not 23/11/1999
                                                                  // that was not accepted as date yet
        {
            SkipBlanks(rString, nPos);
            if (nPos == rString.getLength())
            {
                eScannedType = SvNumFormatType::FRACTION;   // !!! it IS a fraction (so far)
                if (eSetType == SvNumFormatType::FRACTION &&
                    nNumericsCnt == 2 &&
                    (nStringPos == 1 ||                     // for 4/5
                     (nStringPos == 2 && nSign)))           // or signed -4/5
                {
                    return true;                            // don't fall into date trap
                }
            }
        }
        else
        {
            nPos--;                                 // put '/' back
        }
    }

    if (GetThousandSep(rString, nPos, nStringPos))  // 1,000
    {
        if ( eScannedType != SvNumFormatType::UNDEFINED &&   // already another type
             eScannedType != SvNumFormatType::CURRENCY)      // except currency
        {
            return MatchedReturn();
        }
        nThousand++;
    }

    const LocaleDataWrapper* pLoc = pFormatter->GetLocaleData();
    bool bSignedYear = false;
    bool bDate = SkipDatePatternSeparator( nStringPos, nPos, bSignedYear);   // 12/31  31.12.  12/31/1999  31.12.1999
    if (!bDate)
    {
        const OUString& rDate = pFormatter->GetDateSep();
        SkipBlanks(rString, nPos);
        bDate = SkipString( rDate, rString, nPos);      // 10.  10-  10/
    }
    if (!bDate && nStringPos == 1 && mpFormat && (mpFormat->GetType() & SvNumFormatType::DATE))
    {
        // If a DMY format was given and a mid string starts with a literal
        // ". " dot+space and could contain a following month name and ends
        // with a space or LongDateMonthSeparator, like it's scanned in
        // `14". AUG "18`, then it may be a date as well. Regardless whether
        // defined such by the locale or not.
        // This *could* check for presence of ". "MMM or ". "MMMM in the actual
        // format code for further restriction to match only if present, but..

        const sal_uInt32 nExactDateOrder = mpFormat->GetExactDateOrder();
        // Exactly DMY.
        if (((nExactDateOrder & 0xff) == 'Y') && (((nExactDateOrder >> 8) & 0xff) == 'M')
                && (((nExactDateOrder >> 16) & 0xff) == 'D'))
        {
            const sal_Int32 nTmpPos = nPos;
            if (SkipChar('.', rString, nPos) && SkipBlanks(rString, nPos) && nPos + 2 < rString.getLength()
                    && (rString.endsWith(" ") || rString.endsWith( pLoc->getLongDateMonthSep())))
                bDate = true;
            else
                nPos = nTmpPos;
        }
    }
    if (bDate || ((MayBeIso8601() || MayBeMonthDate()) &&    // 1999-12-31  31-Dec-1999
                  SkipChar( '-', rString, nPos)))
    {
        if ( eScannedType != SvNumFormatType::UNDEFINED &&  // already another type
             eScannedType != SvNumFormatType::DATE)       // except date
        {
            return MatchedReturn();
        }
        SkipBlanks(rString, nPos);
        eScannedType = SvNumFormatType::DATE;           // !!! it IS a date
        short nTmpMonth = GetMonth(rString, nPos);  // 10. Jan 94
        if (nMonth && nTmpMonth)                    // month dup
        {
            return MatchedReturn();
        }
        if (nTmpMonth)
        {
            nMonth = nTmpMonth;
            nMonthPos = 2;                          // month in the middle
            if ( nMonth < 0 && SkipChar( '.', rString, nPos ) )
                ;   // short month may be abbreviated Jan.
            else if ( SkipChar( '-', rString, nPos ) )
                ;   // #79632# recognize 17-Jan-2001 to be a date
                    // #99065# short and long month name
            else
            {
                SkipString( pLoc->getLongDateMonthSep(), rString, nPos );
            }
            SkipBlanks(rString, nPos);
        }
        if (bSignedYear)
        {
            if (mbEraCE != kDefaultEra)               // signed year twice?
                return MatchedReturn();

            mbEraCE = false;  // BCE
        }
    }

    const sal_Int32 nMonthStart = nPos;
    short nTempMonth = GetMonth(rString, nPos);     // month in the middle (10 Jan 94) or at the end (94 10 Jan)
    if (nTempMonth)
    {
        if (nMonth != 0)                            // month dup
        {
            return MatchedReturn();
        }
        if ( eScannedType != SvNumFormatType::UNDEFINED &&  // already another type
             eScannedType != SvNumFormatType::DATE)         // except date
        {
            return MatchedReturn();
        }
        if (nMonthStart > 0 && nPos < rString.getLength())  // 10Jan or Jan94 without separator are not dates
        {
            eScannedType = SvNumFormatType::DATE;       // !!! it IS a date
            nMonth = nTempMonth;
            if (nCurNumCount <= 1)
                nMonthPos = 2;                      // month in the middle
            else
                nMonthPos = 3;                      // month at the end
            if ( nMonth < 0 )
            {
                SkipChar( '.', rString, nPos );     // abbreviated
            }
            SkipString( pLoc->getLongDateMonthSep(), rString, nPos );
            SkipBlanks(rString, nPos);
        }
        else
        {
            nPos = nMonthStart;                     // rewind month
        }
    }

    if ( SkipChar('E', rString, nPos) ||            // 10E, 10e, 10,Ee
         SkipChar('e', rString, nPos) )
    {
        if (eScannedType != SvNumFormatType::UNDEFINED) // already another type
        {
            return MatchedReturn();
        }
        else
        {
            SkipBlanks(rString, nPos);
            eScannedType = SvNumFormatType::SCIENTIFIC; // !!! it IS scientific
            if ( nThousand+2 == nNumericsCnt && nDecPos == 2 ) // special case 1.E2
            {
                nDecPos = 3;                        // 1,100.E2 1,100,100.E3
            }
        }
        nESign = GetESign(rString, nPos);           // signed exponent?
        SkipBlanks(rString, nPos);
    }

    const OUString& rTime = pLoc->getTimeSep();
    if ( SkipString(rTime, rString, nPos) )         // time separator?
    {
        if (nDecPos)                                // already . => maybe error
        {
            if (bDecSepInDateSeps)                  // . also date sep
            {
                if ( eScannedType != SvNumFormatType::DATE &&    // already another type than date
                     eScannedType != SvNumFormatType::DATETIME)  // or date time
                {
                    return MatchedReturn();
                }
                if (eScannedType == SvNumFormatType::DATE)
                {
                    nDecPos = 0;                    // reset for time transition
                }
            }
            else
            {
                return MatchedReturn();
            }
        }
        if ((eScannedType == SvNumFormatType::DATE ||        // already date type
             eScannedType == SvNumFormatType::DATETIME) &&   // or date time
            nNumericsCnt > 3)                                // and more than 3 numbers? (31.Dez.94 8:23)
        {
            SkipBlanks(rString, nPos);
            eScannedType = SvNumFormatType::DATETIME;   // !!! it IS date with time
        }
        else if ( eScannedType != SvNumFormatType::UNDEFINED &&  // already another type
                  eScannedType != SvNumFormatType::TIME)         // except time
        {
            return MatchedReturn();
        }
        else
        {
            SkipBlanks(rString, nPos);
            eScannedType = SvNumFormatType::TIME;       // !!! it IS a time
        }
        if ( !nTimePos )
        {
            nTimePos = nStringPos + 1;
        }
    }

    if (nPos < rString.getLength())
    {
        switch (eScannedType)
        {
        case SvNumFormatType::DATE:
            if (nMonthPos == 1 && pLoc->getLongDateOrder() == LongDateOrder::MDY)
            {
                // #68232# recognize long date separators like ", " in "September 5, 1999"
                if (SkipString( pLoc->getLongDateDaySep(), rString, nPos ))
                {
                    SkipBlanks( rString, nPos );
                }
            }
            else if (nPos == 0 && rString.getLength() == 1 && MayBeIso8601())
            {
                if (    (nStringPos == 5 && rString[0] == 'T') ||
                        (nStringPos == 6 && rString[0] == 'T' && sStrArray[0] == "-"))
                {
                    // ISO 8601 combined date and time, yyyy-mm-ddThh:mm or -yyyy-mm-ddThh:mm
                    ++nPos;
                    bIso8601Tsep = true;
                }
                else if (nStringPos == 7 && rString[0] == ':')
                {
                    // ISO 8601 combined date and time, the time part; we reach
                    // here if the locale's separator is not ':' so it couldn't
                    // be detected above in the time block.
                    if (nNumericsCnt >= 5)
                        eScannedType = SvNumFormatType::DATETIME;
                    ++nPos;
                }
            }
            break;
        case SvNumFormatType::DATETIME:
            if (nPos == 0 && rString.getLength() == 1 && MayBeIso8601())
            {
                if (nStringPos == 9 && rString[0] == ':')
                {
                    // ISO 8601 combined date and time, the time part continued.
                    ++nPos;
                }
            }
#if NF_RECOGNIZE_ISO8601_TIMEZONES
            else if (nPos == 0 && rString.getLength() == 1 && nStringPos >= 9 && MayBeIso8601())
            {
                // ISO 8601 timezone offset
                switch (rString[ 0 ])
                {
                case '+':
                case '-':
                    if (nStringPos == nStringsCnt - 2 ||
                        nStringPos == nStringsCnt - 4)
                    {
                        ++nPos;     // yyyy-mm-ddThh:mm[:ss]+xx[[:]yy]
                        // nTimezonePos needed for GetTimeRef()
                        if (!nTimezonePos)
                        {
                            nTimezonePos = nStringPos + 1;
                        }
                    }
                    break;
                case ':':
                    if (nTimezonePos && nStringPos >= 11 &&
                        nStringPos == nStringsCnt - 2)
                    {
                        ++nPos;     // yyyy-mm-ddThh:mm[:ss]+xx:yy
                    }
                    break;
                }
            }
#endif
            break;
        default: break;
        }
    }

    if (nPos < rString.getLength()) // not everything consumed?
    {
        if ( nMatchedAllStrings & ~nMatchedVirgin )
        {
            eScannedType = eOldScannedType;
        }
        else
        {
            return false;
        }
    }

    return true;
}


/**
 * Analyze the end
 * All gone => true
 * else     => false
 */
bool ImpSvNumberInputScan::ScanEndString( const OUString& rString )
{
    sal_Int32 nPos = 0;

    if ( nMatchedAllStrings )
    {   // Match against format in any case, so later on for a "1-2-3-4" input
        // we may distinguish between a y-m-d (or similar) date and a 0-0-0-0
        // format.
        if ( ScanStringNumFor( rString, 0, 0xFFFF ) )
        {
            nMatchedAllStrings |= nMatchedEndString;
        }
        else
        {
            nMatchedAllStrings = 0;
        }
    }

    const sal_Int32 nStartBlanks = nPos;
    const bool bBlanks = SkipBlanks(rString, nPos);
    if (GetDecSep(rString, nPos))                   // decimal separator?
    {
        if (nDecPos == 1 || nDecPos == 3)           // .12.4 or 12.E4.
        {
            return MatchedReturn();
        }
        else if (nDecPos == 2)                      // . dup: 12.4.
        {
            bool bSignedYear = false;
            if (bDecSepInDateSeps ||                // . also date separator
                SkipDatePatternSeparator( nStringsCnt-1, nPos, bSignedYear))
            {
                if ( eScannedType != SvNumFormatType::UNDEFINED &&
                     eScannedType != SvNumFormatType::DATE &&
                     eScannedType != SvNumFormatType::DATETIME)  // already another type
                {
                    return MatchedReturn();
                }
                if (eScannedType == SvNumFormatType::UNDEFINED)
                {
                    eScannedType = SvNumFormatType::DATE;   // !!! it IS a date
                }
                SkipBlanks(rString, nPos);
            }
            else
            {
                return MatchedReturn();
            }
        }
        else if (bBlanks)
        {
            // not a decimal separator, reset
            nPos = nStartBlanks;
        }
        else
        {
            nDecPos = 3;                            // . in end string
            SkipBlanks(rString, nPos);
        }
    }

    bool bSignDetectedHere = false;
    if ( nSign == 0  &&                             // conflict - not signed
         eScannedType != SvNumFormatType::DATE)         // and not date
                                                    //!? catch time too?
    {                                               // not signed yet
        nSign = GetSign(rString, nPos);             // 1- DM
        if (bNegCheck)                              // '(' as sign
        {
            return MatchedReturn();
        }
        if (nSign)
        {
            bSignDetectedHere = true;
        }
    }

    SkipBlanks(rString, nPos);
    if (bNegCheck && SkipChar(')', rString, nPos))  // skip ')' if appropriate
    {
        bNegCheck = false;
        SkipBlanks(rString, nPos);
    }

    if ( GetCurrency(rString, nPos) )               // currency symbol?
    {
        if (eScannedType != SvNumFormatType::UNDEFINED) // currency dup
        {
            return MatchedReturn();
        }
        else
        {
            SkipBlanks(rString, nPos);
            eScannedType = SvNumFormatType::CURRENCY;
        }                                           // behind currency a '-' is allowed
        if (nSign == 0)                             // not signed yet
        {
            nSign = GetSign(rString, nPos);         // DM -
            SkipBlanks(rString, nPos);
            if (bNegCheck)                          // 3 DM (
            {
                return MatchedReturn();
            }
        }
        if ( bNegCheck && eScannedType == SvNumFormatType::CURRENCY &&
             SkipChar(')', rString, nPos) )
        {
            bNegCheck = false;                          // ')' skipped
            SkipBlanks(rString, nPos);              // only if currency
        }
    }

    if ( SkipChar('%', rString, nPos) )             // 1%
    {
        if (eScannedType != SvNumFormatType::UNDEFINED) // already another type
        {
            return MatchedReturn();
        }
        SkipBlanks(rString, nPos);
        eScannedType = SvNumFormatType::PERCENT;
    }

    const LocaleDataWrapper* pLoc = pFormatter->GetLocaleData();
    const OUString& rTime = pLoc->getTimeSep();
    if ( SkipString(rTime, rString, nPos) )         // 10:
    {
        if (nDecPos)                                // already , => error
        {
            return MatchedReturn();
        }
        if (eScannedType == SvNumFormatType::DATE && nNumericsCnt > 2) // 31.Dez.94 8:
        {
            SkipBlanks(rString, nPos);
            eScannedType = SvNumFormatType::DATETIME;
        }
        else if (eScannedType != SvNumFormatType::UNDEFINED &&
                 eScannedType != SvNumFormatType::TIME) // already another type
        {
            return MatchedReturn();
        }
        else
        {
            SkipBlanks(rString, nPos);
            eScannedType = SvNumFormatType::TIME;
        }
        if ( !nTimePos )
        {
            nTimePos = nStringsCnt;
        }
    }

    bool bSignedYear = false;
    bool bDate = SkipDatePatternSeparator( nStringsCnt-1, nPos, bSignedYear);   // 12/31  31.12.  12/31/1999  31.12.1999
    if (!bDate)
    {
        const OUString& rDate = pFormatter->GetDateSep();
        bDate = SkipString( rDate, rString, nPos);      // 10.  10-  10/
    }
    if (bDate && bSignDetectedHere)
    {
        nSign = 0;                                  // 'D-' takes precedence over signed date
    }
    if (bDate || ((MayBeIso8601() || MayBeMonthDate())
                  && SkipChar( '-', rString, nPos)))
    {
        if (eScannedType != SvNumFormatType::UNDEFINED &&
            eScannedType != SvNumFormatType::DATE)          // already another type
        {
            return MatchedReturn();
        }
        else
        {
            SkipBlanks(rString, nPos);
            eScannedType = SvNumFormatType::DATE;
        }
        short nTmpMonth = GetMonth(rString, nPos);  // 10. Jan
        if (nMonth && nTmpMonth)                    // month dup
        {
            return MatchedReturn();
        }
        if (nTmpMonth)
        {
            nMonth = nTmpMonth;
            nMonthPos = 3;                          // month at end
            if ( nMonth < 0 )
            {
                SkipChar( '.', rString, nPos );     // abbreviated
            }
            SkipBlanks(rString, nPos);
        }
    }

    const sal_Int32 nMonthStart = nPos;
    short nTempMonth = GetMonth(rString, nPos);     // 10 Jan
    if (nTempMonth)
    {
        if (nMonth)                                 // month dup
        {
            return MatchedReturn();
        }
        if (eScannedType != SvNumFormatType::UNDEFINED &&
            eScannedType != SvNumFormatType::DATE)      // already another type
        {
            return MatchedReturn();
        }
        if (nMonthStart > 0)                        // 10Jan without separator is not a date
        {
            eScannedType = SvNumFormatType::DATE;
            nMonth = nTempMonth;
            nMonthPos = 3;                          // month at end
            if ( nMonth < 0 )
            {
                SkipChar( '.', rString, nPos );     // abbreviated
            }
            SkipBlanks(rString, nPos);
        }
        else
        {
            nPos = nMonthStart;                     // rewind month
        }
    }

    sal_Int32 nOrigPos = nPos;
    if (GetTimeAmPm(rString, nPos))
    {
        if (eScannedType != SvNumFormatType::UNDEFINED &&
            eScannedType != SvNumFormatType::TIME &&
            eScannedType != SvNumFormatType::DATETIME)  // already another type
        {
            return MatchedReturn();
        }
        else
        {
            // If not already scanned as time, 6.78am does not result in 6
            // seconds and 78 hundredths in the morning. Keep as suffix.
            if (eScannedType != SvNumFormatType::TIME && nDecPos == 2 && nNumericsCnt == 2)
            {
                nPos = nOrigPos; // rewind am/pm
            }
            else
            {
                SkipBlanks(rString, nPos);
                if ( eScannedType != SvNumFormatType::DATETIME )
                {
                    eScannedType = SvNumFormatType::TIME;
                }
            }
        }
    }

    if ( bNegCheck && SkipChar(')', rString, nPos) )
    {
        if (eScannedType == SvNumFormatType::CURRENCY)  // only if currency
        {
            bNegCheck = false;                          // skip ')'
            SkipBlanks(rString, nPos);
        }
        else
        {
            return MatchedReturn();
        }
    }

    if ( nPos < rString.getLength() &&
         (eScannedType == SvNumFormatType::DATE ||
          eScannedType == SvNumFormatType::DATETIME) )
    {
        // day of week is just parsed away
        sal_Int32 nOldPos = nPos;
        const OUString& rSep = pFormatter->GetLocaleData()->getLongDateDayOfWeekSep();
        if ( StringContains( rSep, rString, nPos ) )
        {
            nPos = nPos + rSep.getLength();
            SkipBlanks(rString, nPos);
        }
        int nTempDayOfWeek = GetDayOfWeek( rString, nPos );
        if ( nTempDayOfWeek )
        {
            if ( nPos < rString.getLength() )
            {
                if ( nTempDayOfWeek < 0 )
                {   // short
                    if ( rString[ nPos ] == '.' )
                    {
                        ++nPos;
                    }
                }
                SkipBlanks(rString, nPos);
            }
        }
        else
        {
            nPos = nOldPos;
        }
    }

#if NF_RECOGNIZE_ISO8601_TIMEZONES
    if (nPos == 0 && eScannedType == SvNumFormatType::DATETIME &&
        rString.getLength() == 1 && rString[ 0 ] == 'Z' && MayBeIso8601())
    {
        // ISO 8601 timezone UTC yyyy-mm-ddThh:mmZ
        ++nPos;
    }
#endif

    if (nPos < rString.getLength()) // everything consumed?
    {
        // does input EndString equal EndString in Format?
        if ( !ScanStringNumFor( rString, nPos, 0xFFFF ) )
        {
            return false;
        }
    }

    return true;
}


bool ImpSvNumberInputScan::ScanStringNumFor( const OUString& rString,       // String to scan
                                             sal_Int32 nPos,                // Position until which was consumed
                                             sal_uInt16 nString,            // Substring of format, 0xFFFF => last
                                             bool bDontDetectNegation)      // Suppress sign detection
{
    if ( !mpFormat )
    {
        return false;
    }
    const ::utl::TransliterationWrapper* pTransliteration = pFormatter->GetTransliteration();
    const OUString* pStr;
    OUString aString( rString );
    bool bFound = false;
    bool bFirst = true;
    bool bContinue = true;
    sal_uInt16 nSub;
    do
    {
        // Don't try "lower" subformats ff the very first match was the second
        // or third subformat.
        nSub = nStringScanNumFor;
        do
        {   // Step through subformats, first positive, then negative, then
            // other, but not the last (text) subformat.
            pStr = mpFormat->GetNumForString( nSub, nString, true );
            if ( pStr && pTransliteration->isEqual( aString, *pStr ) )
            {
                bFound = true;
                bContinue = false;
            }
            else if ( nSub < 2 )
            {
                ++nSub;
            }
            else
            {
                bContinue = false;
            }
        }
        while ( bContinue );
        if ( !bFound && bFirst && nPos )
        {
            // try remaining substring
            bFirst = false;
            aString = aString.copy(nPos);
            bContinue = true;
        }
    }
    while ( bContinue );

    if ( !bFound )
    {
        if ( !bDontDetectNegation && (nString == 0) &&
             !bFirst && (nSign < 0) && mpFormat->IsSecondSubformatRealNegative() )
        {
            // simply negated twice? --1
            aString = aString.replaceAll(" ", "");
            if ( (aString.getLength() == 1) && (aString[0] == '-') )
            {
                bFound = true;
                nStringScanSign = -1;
                nSub = 0; //! not 1
            }
        }
        if ( !bFound )
        {
            return false;
        }
    }
    else if ( !bDontDetectNegation && (nSub == 1) &&
              mpFormat->IsSecondSubformatRealNegative() )
    {
        // negative
        if ( nStringScanSign < 0 )
        {
            if ( (nSign < 0) && (nStringScanNumFor != 1) )
            {
                nStringScanSign = 1; // triple negated --1 yyy
            }
        }
        else if ( nStringScanSign == 0 )
        {
            if ( nSign < 0 )
            {   // nSign and nStringScanSign will be combined later,
                // flip sign if doubly negated
                if ( (nString == 0) && !bFirst &&
                     SvNumberformat::HasStringNegativeSign( aString ) )
                {
                    nStringScanSign = -1; // direct double negation
                }
                else if ( mpFormat->IsNegativeWithoutSign() )
                {
                    nStringScanSign = -1; // indirect double negation
                }
            }
            else
            {
                nStringScanSign = -1;
            }
        }
        else    // > 0
        {
            nStringScanSign = -1;
        }
    }
    nStringScanNumFor = nSub;
    return true;
}


/**
 * Recognizes types of number, exponential, fraction, percent, currency, date, time.
 * Else text => return false
 */
bool ImpSvNumberInputScan::IsNumberFormatMain( const OUString& rString,        // string to be analyzed
                                               const SvNumberformat* pFormat ) // maybe number format set to match against
{
    Reset();
    mpFormat = pFormat;
    NumberStringDivision( rString );             // breakdown into strings and numbers
    if (nStringsCnt >= SV_MAX_COUNT_INPUT_STRINGS) // too many elements
    {
        return false;                            // Njet, Nope, ...
    }
    if (nNumericsCnt == 0)                           // no number in input
    {
        if ( nStringsCnt > 0 )
        {
            // Here we may change the original, we don't need it anymore.
            // This saves copies and ToUpper() in GetLogical() and is faster.
            sStrArray[0] = comphelper::string::strip(sStrArray[0], ' ');
            OUString& rStrArray = sStrArray[0];
            nLogical = GetLogical( rStrArray );
            if ( nLogical )
            {
                eScannedType = SvNumFormatType::LOGICAL; // !!! it's a BOOLEAN
                nMatchedAllStrings &= ~nMatchedVirgin;
                return true;
            }
            else
            {
                return false;                   // simple text
            }
        }
        else
        {
            return false;                       // simple text
        }
    }

    sal_uInt16 i = 0;                           // mark any symbol
    sal_uInt16 j = 0;                           // mark only numbers

    switch ( nNumericsCnt )
    {
    case 1 :                                // Exactly 1 number in input
        // nStringsCnt >= 1
        if (GetNextNumber(i,j)) // i=1,0
        {   // Number at start
            if (eSetType == SvNumFormatType::FRACTION)  // Fraction 1 = 1/1
            {
                if (i >= nStringsCnt || // no end string nor decimal separator
                    pFormatter->IsDecimalSep( sStrArray[i]))
                {
                    eScannedType = SvNumFormatType::FRACTION;
                    nMatchedAllStrings &= ~nMatchedVirgin;
                    return true;
                }
            }
        }
        else
        {                                   // Analyze start string
            if (!ScanStartString( sStrArray[i] ))  // i=0
            {
                return false;               // already an error
            }
            i++;                            // next symbol, i=1
        }
        GetNextNumber(i,j);                 // i=1,2
        if (eSetType == SvNumFormatType::FRACTION)  // Fraction -1 = -1/1
        {
            if (nSign && !bNegCheck &&      // Sign +, -
                eScannedType == SvNumFormatType::UNDEFINED &&   // not date or currency
                nDecPos == 0 &&             // no previous decimal separator
                (i >= nStringsCnt ||        // no end string nor decimal separator
                 pFormatter->IsDecimalSep( sStrArray[i]))
                )
            {
                eScannedType = SvNumFormatType::FRACTION;
                nMatchedAllStrings &= ~nMatchedVirgin;
                return true;
            }
        }
        if (i < nStringsCnt && !ScanEndString( sStrArray[i] ))
        {
            return false;
        }
        break;
    case 2 :                                // Exactly 2 numbers in input
                                            // nStringsCnt >= 3
        if (!GetNextNumber(i,j))            // i=1,0
        {                                   // Analyze start string
            if (!ScanStartString( sStrArray[i] ))
                return false;               // already an error
            i++;                            // i=1
        }
        GetNextNumber(i,j);                 // i=1,2
        if ( !ScanMidString( sStrArray[i], i, j ) )
        {
            return false;
        }
        i++;                                // next symbol, i=2,3
        GetNextNumber(i,j);                 // i=3,4
        if (i < nStringsCnt && !ScanEndString( sStrArray[i] ))
        {
            return false;
        }
        if (eSetType == SvNumFormatType::FRACTION)  // -1,200. as fraction
        {
            if (!bNegCheck  &&                  // no sign '('
                eScannedType == SvNumFormatType::UNDEFINED &&
                (nDecPos == 0 || nDecPos == 3)  // no decimal separator or at end
                )
            {
                eScannedType = SvNumFormatType::FRACTION;
                nMatchedAllStrings &= ~nMatchedVirgin;
                return true;
            }
        }
        break;
    case 3 :                                // Exactly 3 numbers in input
                                            // nStringsCnt >= 5
        if (!GetNextNumber(i,j))            // i=1,0
        {                                   // Analyze start string
            if (!ScanStartString( sStrArray[i] ))
            {
                return false;               // already an error
            }
            i++;                            // i=1
            if (nDecPos == 1)               // decimal separator at start => error
            {
                return false;
            }
        }
        GetNextNumber(i,j);                 // i=1,2
        if ( !ScanMidString( sStrArray[i], i, j ) )
        {
            return false;
        }
        i++;                                // i=2,3
        if (eScannedType == SvNumFormatType::SCIENTIFIC)    // E only at end
        {
            return false;
        }
        GetNextNumber(i,j);                 // i=3,4
        if ( !ScanMidString( sStrArray[i], i, j ) )
        {
            return false;
        }
        i++;                                // i=4,5
        GetNextNumber(i,j);                 // i=5,6
        if (i < nStringsCnt && !ScanEndString( sStrArray[i] ))
        {
            return false;
        }
        if (eSetType == SvNumFormatType::FRACTION)  // -1,200,100. as fraction
        {
            if (!bNegCheck  &&                  // no sign '('
                eScannedType == SvNumFormatType::UNDEFINED &&
                (nDecPos == 0 || nDecPos == 3)  // no decimal separator or at end
                )
            {
                eScannedType = SvNumFormatType::FRACTION;
                nMatchedAllStrings &= ~nMatchedVirgin;
                return true;
            }
        }
        if ( eScannedType == SvNumFormatType::FRACTION && nDecPos )
        {
            return false;                   // #36857# not a real fraction
        }
        break;
    default:                                // More than 3 numbers in input
                                            // nStringsCnt >= 7
        if (!GetNextNumber(i,j))            // i=1,0
        {                                   // Analyze startstring
            if (!ScanStartString( sStrArray[i] ))
                return false;               // already an error
            i++;                            // i=1
            if (nDecPos == 1)               // decimal separator at start => error
                return false;
        }
        GetNextNumber(i,j);                 // i=1,2
        if ( !ScanMidString( sStrArray[i], i, j ) )
        {
            return false;
        }
        i++;                                // i=2,3
        {
            sal_uInt16 nThOld = 10;                 // just not 0 or 1
            while (nThOld != nThousand && j < nNumericsCnt-1) // Execute at least one time
                                                          // but leave one number.
            {                                             // Loop over group separators
                nThOld = nThousand;
                if (eScannedType == SvNumFormatType::SCIENTIFIC)    // E only at end
                {
                    return false;
                }
                GetNextNumber(i,j);
                if ( i < nStringsCnt && !ScanMidString( sStrArray[i], i, j ) )
                {
                    return false;
                }
                i++;
            }
        }
        if (eScannedType == SvNumFormatType::DATE ||    // long date or
            eScannedType == SvNumFormatType::TIME ||    // long time or
            eScannedType == SvNumFormatType::UNDEFINED) // long number
        {
            for (sal_uInt16 k = j; k < nNumericsCnt-1; k++)
            {
                if (eScannedType == SvNumFormatType::SCIENTIFIC)    // E only at endd
                {
                    return false;
                }
                GetNextNumber(i,j);
                if ( i < nStringsCnt && !ScanMidString( sStrArray[i], i, j ) )
                {
                    return false;
                }
                i++;
            }
        }
        GetNextNumber(i,j);
        if (i < nStringsCnt && !ScanEndString( sStrArray[i] ))
        {
            return false;
        }
        if (eSetType == SvNumFormatType::FRACTION)  // -1,200,100. as fraction
        {
            if (!bNegCheck  &&                  // no sign '('
                eScannedType == SvNumFormatType::UNDEFINED &&
                (nDecPos == 0 || nDecPos == 3)  // no decimal separator or at end
                )
            {
                eScannedType = SvNumFormatType::FRACTION;
                nMatchedAllStrings &= ~nMatchedVirgin;
                return true;
            }
        }
        if ( eScannedType == SvNumFormatType::FRACTION && nDecPos )
        {
            return false;                       // #36857# not a real fraction
        }
        break;
    }

    if (eScannedType == SvNumFormatType::UNDEFINED)
    {
        nMatchedAllStrings &= ~nMatchedVirgin;
        // did match including nMatchedUsedAsReturn
        bool bDidMatch = (nMatchedAllStrings != 0);
        if ( nMatchedAllStrings )
        {
            bool bMatch = mpFormat && mpFormat->IsNumForStringElementCountEqual(
                               nStringScanNumFor, nStringsCnt, nNumericsCnt );
            if ( !bMatch )
            {
                nMatchedAllStrings = 0;
            }
        }
        if ( nMatchedAllStrings )
        {
            // A type DEFINED means that no category could be assigned to the
            // overall format because of mixed type subformats. Use the scan
            // matched subformat's type if any.
            SvNumFormatType eForType = eSetType;
            if ((eForType == SvNumFormatType::UNDEFINED || eForType == SvNumFormatType::DEFINED) && mpFormat)
                eForType = mpFormat->GetNumForInfoScannedType( nStringScanNumFor);
            if (eForType != SvNumFormatType::UNDEFINED && eForType != SvNumFormatType::DEFINED)
                eScannedType = eForType;
            else
                eScannedType = SvNumFormatType::NUMBER;
        }
        else if ( bDidMatch )
        {
            // Accept a plain fractional number like 123.45 as there may be a
            // decimal separator also present as literal like in a 0"."0 weirdo
            // format.
            if (nDecPos != 2 || nNumericsCnt != 2)
                return false;
            eScannedType = SvNumFormatType::NUMBER;
        }
        else
        {
            eScannedType = SvNumFormatType::NUMBER;
            // everything else should have been recognized by now
        }
    }
    else if ( eScannedType == SvNumFormatType::DATE )
    {
        // the very relaxed date input checks may interfere with a preset format
        nMatchedAllStrings &= ~nMatchedVirgin;
        bool bWasReturn = ((nMatchedAllStrings & nMatchedUsedAsReturn) != 0);
        if ( nMatchedAllStrings )
        {
            bool bMatch = mpFormat && mpFormat->IsNumForStringElementCountEqual(
                               nStringScanNumFor, nStringsCnt, nNumericsCnt );
            if ( !bMatch )
            {
                nMatchedAllStrings = 0;
            }
        }
        if ( nMatchedAllStrings )
        {
            // A type DEFINED means that no category could be assigned to the
            // overall format because of mixed type subformats. Do not override
            // the scanned type in this case. Otherwise in IsNumberFormat() the
            // first numeric particle would be accepted as number.
            SvNumFormatType eForType = eSetType;
            if ((eForType == SvNumFormatType::UNDEFINED || eForType == SvNumFormatType::DEFINED) && mpFormat)
                eForType = mpFormat->GetNumForInfoScannedType( nStringScanNumFor);
            if (eForType != SvNumFormatType::UNDEFINED && eForType != SvNumFormatType::DEFINED)
                eScannedType = eForType;
        }
        else if ( bWasReturn )
        {
            return false;
        }
    }
    else
    {
        nMatchedAllStrings = 0; // reset flag to no substrings matched
    }
    return true;
}


/**
 * Return true or false depending on the nMatched... state and remember usage
 */
bool ImpSvNumberInputScan::MatchedReturn()
{
    if ( nMatchedAllStrings & ~nMatchedVirgin )
    {
        nMatchedAllStrings |= nMatchedUsedAsReturn;
        return true;
    }
    return false;
}


/**
 * Initialize uppercase months and weekdays
 */
void ImpSvNumberInputScan::InitText()
{
    sal_Int32 j, nElems;
    const CharClass* pChrCls = pFormatter->GetCharClass();
    const CalendarWrapper* pCal = pFormatter->GetCalendar();

    pUpperMonthText.reset();
    pUpperAbbrevMonthText.reset();
    css::uno::Sequence< css::i18n::CalendarItem2 > xElems = pCal->getMonths();
    nElems = xElems.getLength();
    pUpperMonthText.reset( new OUString[nElems] );
    pUpperAbbrevMonthText.reset( new OUString[nElems] );
    for ( j = 0; j < nElems; j++ )
    {
        pUpperMonthText[j] = pChrCls->uppercase( xElems[j].FullName );
        pUpperAbbrevMonthText[j] = pChrCls->uppercase( xElems[j].AbbrevName );
    }

    pUpperGenitiveMonthText.reset();
    pUpperGenitiveAbbrevMonthText.reset();
    xElems = pCal->getGenitiveMonths();
    bScanGenitiveMonths = (nElems != xElems.getLength());
    nElems = xElems.getLength();
    pUpperGenitiveMonthText.reset( new OUString[nElems] );
    pUpperGenitiveAbbrevMonthText.reset( new OUString[nElems] );
    for ( j = 0; j < nElems; j++ )
    {
        pUpperGenitiveMonthText[j] = pChrCls->uppercase( xElems[j].FullName );
        pUpperGenitiveAbbrevMonthText[j] = pChrCls->uppercase( xElems[j].AbbrevName );
        if (!bScanGenitiveMonths &&
            (pUpperGenitiveMonthText[j] != pUpperMonthText[j] ||
             pUpperGenitiveAbbrevMonthText[j] != pUpperAbbrevMonthText[j]))
        {
            bScanGenitiveMonths = true;
        }
    }

    pUpperPartitiveMonthText.reset();
    pUpperPartitiveAbbrevMonthText.reset();
    xElems = pCal->getPartitiveMonths();
    bScanPartitiveMonths = (nElems != xElems.getLength());
    nElems = xElems.getLength();
    pUpperPartitiveMonthText.reset( new OUString[nElems] );
    pUpperPartitiveAbbrevMonthText.reset( new OUString[nElems] );
    for ( j = 0; j < nElems; j++ )
    {
        pUpperPartitiveMonthText[j] = pChrCls->uppercase( xElems[j].FullName );
        pUpperPartitiveAbbrevMonthText[j] = pChrCls->uppercase( xElems[j].AbbrevName );
        if (!bScanPartitiveMonths &&
            (pUpperPartitiveMonthText[j] != pUpperGenitiveMonthText[j] ||
             pUpperPartitiveAbbrevMonthText[j] != pUpperGenitiveAbbrevMonthText[j]))
        {
            bScanPartitiveMonths = true;
        }
    }

    pUpperDayText.reset();
    pUpperAbbrevDayText.reset();
    xElems = pCal->getDays();
    nElems = xElems.getLength();
    pUpperDayText.reset( new OUString[nElems] );
    pUpperAbbrevDayText.reset( new OUString[nElems] );
    for ( j = 0; j < nElems; j++ )
    {
        pUpperDayText[j] = pChrCls->uppercase( xElems[j].FullName );
        pUpperAbbrevDayText[j] = pChrCls->uppercase( xElems[j].AbbrevName );
    }

    bTextInitialized = true;
}


/**
 * MUST be called if International/Locale is changed
 */
void ImpSvNumberInputScan::ChangeIntl()
{
    sal_Unicode cDecSep = pFormatter->GetNumDecimalSep()[0];
    bDecSepInDateSeps = ( cDecSep == '-' ||
                          cDecSep == pFormatter->GetDateSep()[0] );
    if (!bDecSepInDateSeps)
    {
        sal_Unicode cDecSepAlt = pFormatter->GetNumDecimalSepAlt().toChar();
        bDecSepInDateSeps = cDecSepAlt && (cDecSepAlt == '-' || cDecSepAlt == pFormatter->GetDateSep()[0]);
    }
    bTextInitialized = false;
    aUpperCurrSymbol.clear();
    InvalidateDateAcceptancePatterns();
}


void ImpSvNumberInputScan::InvalidateDateAcceptancePatterns()
{
    if (sDateAcceptancePatterns.hasElements())
    {
        sDateAcceptancePatterns = css::uno::Sequence< OUString >();
    }
}


void ImpSvNumberInputScan::ChangeNullDate( const sal_uInt16 Day,
                                           const sal_uInt16 Month,
                                           const sal_Int16 Year )
{
    moNullDate = Date(Day, Month, Year);
}


/**
 * Does rString represent a number (also date, time et al)
 */
bool ImpSvNumberInputScan::IsNumberFormat( const OUString& rString,         // string to be analyzed
                                           SvNumFormatType& F_Type,         // IN: old type, OUT: new type
                                           double& fOutNumber,              // OUT: number if convertible
                                           const SvNumberformat* pFormat,   // maybe a number format to match against
                                           SvNumInputOptions eInputOptions )
{
    bool res; // return value
    sal_uInt16 k;
    eSetType = F_Type; // old type set

    if ( !rString.getLength() )
    {
        res = false;
    }
    else if (rString.getLength() > 308) // arbitrary
    {
        res = false;
    }
    else
    {
        // NoMoreUpperNeeded, all comparisons on UpperCase
        OUString aString = pFormatter->GetCharClass()->uppercase( rString );
        // convert native number to ASCII if necessary
        TransformInput(pFormatter, aString);
        res = IsNumberFormatMain( aString, pFormat );
    }

    if (res)
    {
        // Accept signed date only for ISO date with at least four digits in
        // year to not have an input of -M-D-Y arbitrarily recognized. The
        // final order is only determined in GetDateRef().
        // Also accept for Y/M/D date pattern match, i.e. if the first number
        // is year.
        // Accept only if the year immediately follows the sign character with
        // no space in between.
        if (nSign && (eScannedType == SvNumFormatType::DATE ||
                      eScannedType == SvNumFormatType::DATETIME) && mbEraCE == kDefaultEra &&
                (IsDatePatternNumberOfType(0,'Y') || (MayBeIso8601() && sStrArray[nNums[0]].getLength() >= 4)))
        {
            const sal_Unicode c = sStrArray[0][sStrArray[0].getLength()-1];
            if (c == '-' || c == '+')
            {
                // A '+' sign doesn't change the era.
                if (nSign < 0)
                    mbEraCE = false;  // BCE
                nSign = 0;
            }
        }
        if ( bNegCheck ||                             // ')' not found for '('
             (nSign && (eScannedType == SvNumFormatType::DATE ||
                        eScannedType == SvNumFormatType::DATETIME))) // signed date/datetime
        {
            res = false;
        }
        else
        {                                           // check count of partial number strings
            switch (eScannedType)
            {
            case SvNumFormatType::PERCENT:
            case SvNumFormatType::CURRENCY:
            case SvNumFormatType::NUMBER:
                if (nDecPos == 1)               // .05
                {
                    // Matched MidStrings function like group separators, but
                    // there can't be an integer part numeric input, so
                    // effectively 0 thousands groups.
                    if ( nMatchedAllStrings )
                    {
                        nThousand = 0;
                    }
                    else if ( nNumericsCnt != 1 )
                    {
                        res = false;
                    }
                }
                else if (nDecPos == 2)          // 1.05
                {
                    // Matched MidStrings function like group separators, but
                    // let a decimal separator override a literal separator
                    // string; like 0"." with input 123.45
                    if ( nMatchedAllStrings )
                    {
                        if (nNumericsCnt == 2)
                            nThousand = 0;
                        else
                        {
                            // Assume that if there was a decimal separator
                            // matching also a literal string then it was the
                            // last. We could find the last possible match to
                            // support literals in fractions, but really..
                            nThousand = nNumericsCnt - 1;
                        }
                    }
                    else if ( nNumericsCnt != nThousand+2 )
                    {
                        res = false;
                    }
                }
                else                            // 1,100 or 1,100.
                {
                    // matched MidStrings function like group separators
                    if ( nMatchedAllStrings )
                    {
                        nThousand = nNumericsCnt - 1;
                    }
                    else if ( nNumericsCnt != nThousand+1 )
                    {
                        res = false;
                    }
                }
                break;

            case SvNumFormatType::SCIENTIFIC:       // 1.0e-2
                if (nDecPos == 1)               // .05
                {
                    if (nNumericsCnt != 2)
                    {
                        res = false;
                    }
                }
                else if (nDecPos == 2)          // 1.05
                {
                    if (nNumericsCnt != nThousand+3)
                    {
                        res = false;
                    }
                }
                else                            // 1,100 or 1,100.
                {
                    if (nNumericsCnt != nThousand+2)
                    {
                        res = false;
                    }
                }
                break;

            case SvNumFormatType::DATE:
                if (nMonth < 0 && nDayOfWeek < 0 && nNumericsCnt == 3)
                {
                    // If both, short month name and day of week name were
                    // detected, and also numbers for full date, assume that we
                    // have a day of week instead of month name.
                    nMonth = 0;
                    nMonthPos = 0;
                }
                if (nMonth)
                {                               // month name and numbers
                    if (nNumericsCnt > 2)
                    {
                        res = false;
                    }
                }
                else
                {
                    if (nNumericsCnt > 3)
                    {
                        res = false;
                    }
                    else
                    {
                        // Even if a date pattern was matched, for abbreviated
                        // pattern like "D.M." an input of "D.M. #" was
                        // accepted because # could had been a time. Here we do
                        // not have a combined date/time input though and #
                        // would be taken as Year in this example, which it is
                        // not. The count of numbers in pattern must match the
                        // count of numbers in input.
                        res = (GetDatePatternNumbers() == nNumericsCnt)
                            || IsAcceptableIso8601() || nMatchedAllStrings;
                    }
                }
                break;

            case SvNumFormatType::TIME:
                if (nDecPos)
                {                               // hundredth seconds included
                    if (nNumericsCnt > 4)
                    {
                        res = false;
                    }
                }
                else
                {
                    if (nNumericsCnt > 3)
                    {
                        res = false;
                    }
                }
                break;

            case SvNumFormatType::DATETIME:
                if (nMonth < 0 && nDayOfWeek < 0 && nNumericsCnt >= 5)
                {
                    // If both, abbreviated month name and day of week name
                    // were detected, and also at least numbers for full date
                    // plus time including minutes, assume that we have a day
                    // of week instead of month name.
                    nMonth = 0;
                    nMonthPos = 0;
                }
                if (nMonth)
                {                               // month name and numbers
                    if (nDecPos)
                    {                           // hundredth seconds included
                        if (nNumericsCnt > 6)
                        {
                            res = false;
                        }
                    }
                    else
                    {
                        if (nNumericsCnt > 5)
                        {
                            res = false;
                        }
                    }
                }
                else
                {
                    if (nDecPos)
                    {                           // hundredth seconds included
                        if (nNumericsCnt > 7)
                        {
                            res = false;
                        }
                    }
                    else
                    {
                        if (nNumericsCnt > 6)
                        {
                            res = false;
                        }
                    }
                    if (res)
                    {
                        res = IsAcceptedDatePattern( nNums[0]) || MayBeIso8601() || nMatchedAllStrings;
                    }
                }
                break;

            default:
                break;
            }   // switch
        }   // else
    }   // if (res)

    OUStringBuffer sResString;

    if (res)
    {                                       // we finally have a number
        switch (eScannedType)
        {
        case SvNumFormatType::LOGICAL:
            if (nLogical ==  1)
            {
                fOutNumber = 1.0;           // True
            }
            else if (nLogical == -1)
            {
                fOutNumber = 0.0;           // False
            }
            else
            {
                res = false;                // Oops
            }
            break;

        case SvNumFormatType::PERCENT:
        case SvNumFormatType::CURRENCY:
        case SvNumFormatType::NUMBER:
        case SvNumFormatType::SCIENTIFIC:
        case SvNumFormatType::DEFINED:          // if no category detected handle as number
            if ( nDecPos == 1 )             // . at start
            {
                sResString.append("0.");
            }

            for ( k = 0; k <= nThousand; k++)
            {
                sResString.append(sStrArray[nNums[k]]);  // integer part
            }
            if ( nDecPos == 2 && k < nNumericsCnt )     // . somewhere
            {
                sResString.append('.');
                sal_uInt16 nStop = (eScannedType == SvNumFormatType::SCIENTIFIC ?
                                    nNumericsCnt-1 : nNumericsCnt);
                for ( ; k < nStop; k++)
                {
                    sResString.append(sStrArray[nNums[k]]);  // fractional part
                }
            }

            if (eScannedType != SvNumFormatType::SCIENTIFIC)
            {
                fOutNumber = StringToDouble(sResString);
            }
            else
            {                                           // append exponent
                sResString.append('E');
                if ( nESign == -1 )
                {
                    sResString.append('-');
                }
                sResString.append(sStrArray[nNums[nNumericsCnt-1]]);
                rtl_math_ConversionStatus eStatus;
                fOutNumber = ::rtl::math::stringToDouble( sResString, '.', ',', &eStatus );
                if ( eStatus == rtl_math_ConversionStatus_OutOfRange )
                {
                    F_Type = SvNumFormatType::TEXT;         // overflow/underflow -> Text
                    if (nESign == -1)
                    {
                        fOutNumber = 0.0;
                    }
                    else
                    {
                        fOutNumber = DBL_MAX;
                    }
                    return true;
                }
            }

            if ( nStringScanSign )
            {
                if ( nSign )
                {
                    nSign *= nStringScanSign;
                }
                else
                {
                    nSign = nStringScanSign;
                }
            }
            if ( nSign < 0 )
            {
                fOutNumber = -fOutNumber;
            }

            if (eScannedType == SvNumFormatType::PERCENT)
            {
                fOutNumber/= 100.0;
            }
            break;

        case SvNumFormatType::FRACTION:
            if (nNumericsCnt == 1)
            {
                fOutNumber = StringToDouble(sStrArray[nNums[0]]);
            }
            else if (nNumericsCnt == 2)
            {
                if (nThousand == 1)
                {
                    sResString = sStrArray[nNums[0]];
                    sResString.append(sStrArray[nNums[1]]); // integer part
                    fOutNumber = StringToDouble(sResString);
                }
                else
                {
                    double fNumerator = StringToDouble(sStrArray[nNums[0]]);
                    double fDenominator = StringToDouble(sStrArray[nNums[1]]);
                    if (fDenominator != 0.0)
                    {
                        fOutNumber = fNumerator/fDenominator;
                    }
                    else
                    {
                        res = false;
                    }
                }
            }
            else // nNumericsCnt > 2
            {
                k = 1;
                sResString = sStrArray[nNums[0]];
                if (nThousand > 0)
                {
                    for (; k <= nThousand; k++)
                    {
                        sResString.append(sStrArray[nNums[k]]);
                    }
                }
                fOutNumber = StringToDouble(sResString);

                if (k == nNumericsCnt-2)
                {
                    double fNumerator = StringToDouble(sStrArray[nNums[k]]);
                    double fDenominator = StringToDouble(sStrArray[nNums[k + 1]]);
                    if (fDenominator != 0.0)
                    {
                        fOutNumber += fNumerator/fDenominator;
                    }
                    else
                    {
                        res = false;
                    }
                }
            }

            if ( nStringScanSign )
            {
                if ( nSign )
                {
                    nSign *= nStringScanSign;
                }
                else
                {
                    nSign = nStringScanSign;
                }
            }
            if ( nSign < 0 )
            {
                fOutNumber = -fOutNumber;
            }
            break;

        case SvNumFormatType::TIME:
            res = GetTimeRef(fOutNumber, 0, nNumericsCnt, eInputOptions);
            if ( nSign < 0 )
            {
                fOutNumber = -fOutNumber;
            }
            break;

        case SvNumFormatType::DATE:
            res = GetDateRef( fOutNumber, k );
            break;

        case SvNumFormatType::DATETIME:
            res = GetDateRef( fOutNumber, k );
            if ( res )
            {
                double fTime;
                res = GetTimeRef( fTime, k, nNumericsCnt - k, eInputOptions);
                fOutNumber += fTime;
            }
            break;

        default:
            SAL_WARN( "svl.numbers", "Some number recognized but what's it?" );
            fOutNumber = 0.0;
            break;
        }
    }

    if (res) // overflow/underflow -> Text
    {
        if (fOutNumber < -DBL_MAX) // -1.7E308
        {
            F_Type = SvNumFormatType::TEXT;
            fOutNumber = -DBL_MAX;
            return true;
        }
        else if (fOutNumber >  DBL_MAX) // 1.7E308
        {
            F_Type = SvNumFormatType::TEXT;
            fOutNumber = DBL_MAX;
            return true;
        }
    }

    if (!res)
    {
        eScannedType = SvNumFormatType::TEXT;
        fOutNumber = 0.0;
    }

    F_Type = eScannedType;
    return res;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */