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
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 2007-2016, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*
* File DTPTNGEN.CPP
*
*******************************************************************************
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/datefmt.h"
#include "unicode/decimfmt.h"
#include "unicode/dtfmtsym.h"
#include "unicode/dtptngen.h"
#include "unicode/localpointer.h"
#include "unicode/simpleformatter.h"
#include "unicode/smpdtfmt.h"
#include "unicode/udat.h"
#include "unicode/udatpg.h"
#include "unicode/uniset.h"
#include "unicode/uloc.h"
#include "unicode/ures.h"
#include "unicode/ustring.h"
#include "unicode/rep.h"
#include "unicode/region.h"
#include "cpputils.h"
#include "mutex.h"
#include "umutex.h"
#include "cmemory.h"
#include "cstring.h"
#include "locbased.h"
#include "hash.h"
#include "uhash.h"
#include "uresimp.h"
#include "dtptngen_impl.h"
#include "ucln_in.h"
#include "charstr.h"
#include "uassert.h"
#if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
/**
* If we are on EBCDIC, use an iterator which will
* traverse the bundles in ASCII order.
*/
#define U_USE_ASCII_BUNDLE_ITERATOR
#define U_SORT_ASCII_BUNDLE_ITERATOR
#endif
#if defined(U_USE_ASCII_BUNDLE_ITERATOR)
#include "unicode/ustring.h"
#include "uarrsort.h"
struct UResAEntry {
UChar *key;
UResourceBundle *item;
};
struct UResourceBundleAIterator {
UResourceBundle *bund;
UResAEntry *entries;
int32_t num;
int32_t cursor;
};
/* Must be C linkage to pass function pointer to the sort function */
U_CDECL_BEGIN
static int32_t U_CALLCONV
ures_a_codepointSort(const void *context, const void *left, const void *right) {
//CompareContext *cmp=(CompareContext *)context;
return u_strcmp(((const UResAEntry *)left)->key,
((const UResAEntry *)right)->key);
}
U_CDECL_END
static void ures_a_open(UResourceBundleAIterator *aiter, UResourceBundle *bund, UErrorCode *status) {
if(U_FAILURE(*status)) {
return;
}
aiter->bund = bund;
aiter->num = ures_getSize(aiter->bund);
aiter->cursor = 0;
#if !defined(U_SORT_ASCII_BUNDLE_ITERATOR)
aiter->entries = nullptr;
#else
aiter->entries = (UResAEntry*)uprv_malloc(sizeof(UResAEntry)*aiter->num);
for(int i=0;i<aiter->num;i++) {
aiter->entries[i].item = ures_getByIndex(aiter->bund, i, nullptr, status);
const char *akey = ures_getKey(aiter->entries[i].item);
int32_t len = uprv_strlen(akey)+1;
aiter->entries[i].key = (UChar*)uprv_malloc(len*sizeof(UChar));
u_charsToUChars(akey, aiter->entries[i].key, len);
}
uprv_sortArray(aiter->entries, aiter->num, sizeof(UResAEntry), ures_a_codepointSort, nullptr, TRUE, status);
#endif
}
static void ures_a_close(UResourceBundleAIterator *aiter) {
#if defined(U_SORT_ASCII_BUNDLE_ITERATOR)
for(int i=0;i<aiter->num;i++) {
uprv_free(aiter->entries[i].key);
ures_close(aiter->entries[i].item);
}
#endif
}
static const UChar *ures_a_getNextString(UResourceBundleAIterator *aiter, int32_t *len, const char **key, UErrorCode *err) {
#if !defined(U_SORT_ASCII_BUNDLE_ITERATOR)
return ures_getNextString(aiter->bund, len, key, err);
#else
if(U_FAILURE(*err)) return nullptr;
UResourceBundle *item = aiter->entries[aiter->cursor].item;
const UChar* ret = ures_getString(item, len, err);
*key = ures_getKey(item);
aiter->cursor++;
return ret;
#endif
}
#endif
U_NAMESPACE_BEGIN
// *****************************************************************************
// class DateTimePatternGenerator
// *****************************************************************************
static const UChar Canonical_Items[] = {
// GyQMwWEDFdaHmsSv
CAP_G, LOW_Y, CAP_Q, CAP_M, LOW_W, CAP_W, CAP_E,
CAP_D, CAP_F, LOW_D, LOW_A, // The UDATPG_x_FIELD constants and these fields have a different order than in ICU4J
CAP_H, LOW_M, LOW_S, CAP_S, LOW_V, 0
};
static const dtTypeElem dtTypes[] = {
// patternChar, field, type, minLen, weight
{CAP_G, UDATPG_ERA_FIELD, DT_SHORT, 1, 3,},
{CAP_G, UDATPG_ERA_FIELD, DT_LONG, 4, 0},
{CAP_G, UDATPG_ERA_FIELD, DT_NARROW, 5, 0},
{LOW_Y, UDATPG_YEAR_FIELD, DT_NUMERIC, 1, 20},
{CAP_Y, UDATPG_YEAR_FIELD, DT_NUMERIC + DT_DELTA, 1, 20},
{LOW_U, UDATPG_YEAR_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 20},
{LOW_R, UDATPG_YEAR_FIELD, DT_NUMERIC + 3*DT_DELTA, 1, 20},
{CAP_U, UDATPG_YEAR_FIELD, DT_SHORT, 1, 3},
{CAP_U, UDATPG_YEAR_FIELD, DT_LONG, 4, 0},
{CAP_U, UDATPG_YEAR_FIELD, DT_NARROW, 5, 0},
{CAP_Q, UDATPG_QUARTER_FIELD, DT_NUMERIC, 1, 2},
{CAP_Q, UDATPG_QUARTER_FIELD, DT_SHORT, 3, 0},
{CAP_Q, UDATPG_QUARTER_FIELD, DT_LONG, 4, 0},
{CAP_Q, UDATPG_QUARTER_FIELD, DT_NARROW, 5, 0},
{LOW_Q, UDATPG_QUARTER_FIELD, DT_NUMERIC + DT_DELTA, 1, 2},
{LOW_Q, UDATPG_QUARTER_FIELD, DT_SHORT - DT_DELTA, 3, 0},
{LOW_Q, UDATPG_QUARTER_FIELD, DT_LONG - DT_DELTA, 4, 0},
{LOW_Q, UDATPG_QUARTER_FIELD, DT_NARROW - DT_DELTA, 5, 0},
{CAP_M, UDATPG_MONTH_FIELD, DT_NUMERIC, 1, 2},
{CAP_M, UDATPG_MONTH_FIELD, DT_SHORT, 3, 0},
{CAP_M, UDATPG_MONTH_FIELD, DT_LONG, 4, 0},
{CAP_M, UDATPG_MONTH_FIELD, DT_NARROW, 5, 0},
{CAP_L, UDATPG_MONTH_FIELD, DT_NUMERIC + DT_DELTA, 1, 2},
{CAP_L, UDATPG_MONTH_FIELD, DT_SHORT - DT_DELTA, 3, 0},
{CAP_L, UDATPG_MONTH_FIELD, DT_LONG - DT_DELTA, 4, 0},
{CAP_L, UDATPG_MONTH_FIELD, DT_NARROW - DT_DELTA, 5, 0},
{LOW_L, UDATPG_MONTH_FIELD, DT_NUMERIC + DT_DELTA, 1, 1},
{LOW_W, UDATPG_WEEK_OF_YEAR_FIELD, DT_NUMERIC, 1, 2},
{CAP_W, UDATPG_WEEK_OF_MONTH_FIELD, DT_NUMERIC, 1, 0},
{CAP_E, UDATPG_WEEKDAY_FIELD, DT_SHORT, 1, 3},
{CAP_E, UDATPG_WEEKDAY_FIELD, DT_LONG, 4, 0},
{CAP_E, UDATPG_WEEKDAY_FIELD, DT_NARROW, 5, 0},
{CAP_E, UDATPG_WEEKDAY_FIELD, DT_SHORTER, 6, 0},
{LOW_C, UDATPG_WEEKDAY_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 2},
{LOW_C, UDATPG_WEEKDAY_FIELD, DT_SHORT - 2*DT_DELTA, 3, 0},
{LOW_C, UDATPG_WEEKDAY_FIELD, DT_LONG - 2*DT_DELTA, 4, 0},
{LOW_C, UDATPG_WEEKDAY_FIELD, DT_NARROW - 2*DT_DELTA, 5, 0},
{LOW_C, UDATPG_WEEKDAY_FIELD, DT_SHORTER - 2*DT_DELTA, 6, 0},
{LOW_E, UDATPG_WEEKDAY_FIELD, DT_NUMERIC + DT_DELTA, 1, 2}, // LOW_E is currently not used in CLDR data, should not be canonical
{LOW_E, UDATPG_WEEKDAY_FIELD, DT_SHORT - DT_DELTA, 3, 0},
{LOW_E, UDATPG_WEEKDAY_FIELD, DT_LONG - DT_DELTA, 4, 0},
{LOW_E, UDATPG_WEEKDAY_FIELD, DT_NARROW - DT_DELTA, 5, 0},
{LOW_E, UDATPG_WEEKDAY_FIELD, DT_SHORTER - DT_DELTA, 6, 0},
{LOW_D, UDATPG_DAY_FIELD, DT_NUMERIC, 1, 2},
{LOW_G, UDATPG_DAY_FIELD, DT_NUMERIC + DT_DELTA, 1, 20}, // really internal use, so we don't care
{CAP_D, UDATPG_DAY_OF_YEAR_FIELD, DT_NUMERIC, 1, 3},
{CAP_F, UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, DT_NUMERIC, 1, 0},
{LOW_A, UDATPG_DAYPERIOD_FIELD, DT_SHORT, 1, 3},
{LOW_A, UDATPG_DAYPERIOD_FIELD, DT_LONG, 4, 0},
{LOW_A, UDATPG_DAYPERIOD_FIELD, DT_NARROW, 5, 0},
{LOW_B, UDATPG_DAYPERIOD_FIELD, DT_SHORT - DT_DELTA, 1, 3},
{LOW_B, UDATPG_DAYPERIOD_FIELD, DT_LONG - DT_DELTA, 4, 0},
{LOW_B, UDATPG_DAYPERIOD_FIELD, DT_NARROW - DT_DELTA, 5, 0},
// b needs to be closer to a than to B, so we make this 3*DT_DELTA
{CAP_B, UDATPG_DAYPERIOD_FIELD, DT_SHORT - 3*DT_DELTA, 1, 3},
{CAP_B, UDATPG_DAYPERIOD_FIELD, DT_LONG - 3*DT_DELTA, 4, 0},
{CAP_B, UDATPG_DAYPERIOD_FIELD, DT_NARROW - 3*DT_DELTA, 5, 0},
{CAP_H, UDATPG_HOUR_FIELD, DT_NUMERIC + 10*DT_DELTA, 1, 2}, // 24 hour
{LOW_K, UDATPG_HOUR_FIELD, DT_NUMERIC + 11*DT_DELTA, 1, 2}, // 24 hour
{LOW_H, UDATPG_HOUR_FIELD, DT_NUMERIC, 1, 2}, // 12 hour
{CAP_K, UDATPG_HOUR_FIELD, DT_NUMERIC + DT_DELTA, 1, 2}, // 12 hour
// The C code has had versions of the following 3, keep & update. Should not need these, but...
// Without these, certain tests using e.g. staticGetSkeleton fail because j/J in patterns
// get skipped instead of mapped to the right hour chars, for example in
// DateFormatTest::TestPatternFromSkeleton
// IntlTestDateTimePatternGeneratorAPI:: testStaticGetSkeleton
// DateIntervalFormatTest::testTicket11985
// Need to investigate better handling of jJC replacement e.g. in staticGetSkeleton.
{CAP_J, UDATPG_HOUR_FIELD, DT_NUMERIC + 5*DT_DELTA, 1, 2}, // 12/24 hour no AM/PM
{LOW_J, UDATPG_HOUR_FIELD, DT_NUMERIC + 6*DT_DELTA, 1, 6}, // 12/24 hour
{CAP_C, UDATPG_HOUR_FIELD, DT_NUMERIC + 7*DT_DELTA, 1, 6}, // 12/24 hour with preferred dayPeriods for 12
{LOW_M, UDATPG_MINUTE_FIELD, DT_NUMERIC, 1, 2},
{LOW_S, UDATPG_SECOND_FIELD, DT_NUMERIC, 1, 2},
{CAP_A, UDATPG_SECOND_FIELD, DT_NUMERIC + DT_DELTA, 1, 1000},
{CAP_S, UDATPG_FRACTIONAL_SECOND_FIELD, DT_NUMERIC, 1, 1000},
{LOW_V, UDATPG_ZONE_FIELD, DT_SHORT - 2*DT_DELTA, 1, 0},
{LOW_V, UDATPG_ZONE_FIELD, DT_LONG - 2*DT_DELTA, 4, 0},
{LOW_Z, UDATPG_ZONE_FIELD, DT_SHORT, 1, 3},
{LOW_Z, UDATPG_ZONE_FIELD, DT_LONG, 4, 0},
{CAP_Z, UDATPG_ZONE_FIELD, DT_NARROW - DT_DELTA, 1, 3},
{CAP_Z, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
{CAP_Z, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 5, 0},
{CAP_O, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 1, 0},
{CAP_O, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
{CAP_V, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 1, 0},
{CAP_V, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 2, 0},
{CAP_V, UDATPG_ZONE_FIELD, DT_LONG-1 - DT_DELTA, 3, 0},
{CAP_V, UDATPG_ZONE_FIELD, DT_LONG-2 - DT_DELTA, 4, 0},
{CAP_X, UDATPG_ZONE_FIELD, DT_NARROW - DT_DELTA, 1, 0},
{CAP_X, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 2, 0},
{CAP_X, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
{LOW_X, UDATPG_ZONE_FIELD, DT_NARROW - DT_DELTA, 1, 0},
{LOW_X, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 2, 0},
{LOW_X, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
{0, UDATPG_FIELD_COUNT, 0, 0, 0} , // last row of dtTypes[]
};
static const char* const CLDR_FIELD_APPEND[] = {
"Era", "Year", "Quarter", "Month", "Week", "*", "Day-Of-Week",
"*", "*", "Day", "*", // The UDATPG_x_FIELD constants and these fields have a different order than in ICU4J
"Hour", "Minute", "Second", "*", "Timezone"
};
static const char* const CLDR_FIELD_NAME[UDATPG_FIELD_COUNT] = {
"era", "year", "quarter", "month", "week", "weekOfMonth", "weekday",
"dayOfYear", "weekdayOfMonth", "day", "dayperiod", // The UDATPG_x_FIELD constants and these fields have a different order than in ICU4J
"hour", "minute", "second", "*", "zone"
};
static const char* const CLDR_FIELD_WIDTH[] = { // [UDATPG_WIDTH_COUNT]
"", "-short", "-narrow"
};
// TODO(ticket:13619): remove when definition uncommented in dtptngen.h.
static const int32_t UDATPG_WIDTH_COUNT = UDATPG_NARROW + 1;
static constexpr UDateTimePGDisplayWidth UDATPG_WIDTH_APPENDITEM = UDATPG_WIDE;
static constexpr int32_t UDATPG_FIELD_KEY_MAX = 24; // max length of CLDR field tag (type + width)
// For appendItems
static const UChar UDATPG_ItemFormat[]= {0x7B, 0x30, 0x7D, 0x20, 0x251C, 0x7B, 0x32, 0x7D, 0x3A,
0x20, 0x7B, 0x31, 0x7D, 0x2524, 0}; // {0} \u251C{2}: {1}\u2524
//static const UChar repeatedPatterns[6]={CAP_G, CAP_E, LOW_Z, LOW_V, CAP_Q, 0}; // "GEzvQ"
static const char DT_DateTimePatternsTag[]="DateTimePatterns";
static const char DT_DateTimeCalendarTag[]="calendar";
static const char DT_DateTimeGregorianTag[]="gregorian";
static const char DT_DateTimeAppendItemsTag[]="appendItems";
static const char DT_DateTimeFieldsTag[]="fields";
static const char DT_DateTimeAvailableFormatsTag[]="availableFormats";
//static const UnicodeString repeatedPattern=UnicodeString(repeatedPatterns);
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DateTimePatternGenerator)
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DTSkeletonEnumeration)
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DTRedundantEnumeration)
DateTimePatternGenerator* U_EXPORT2
DateTimePatternGenerator::createInstance(UErrorCode& status) {
return createInstance(Locale::getDefault(), status);
}
DateTimePatternGenerator* U_EXPORT2
DateTimePatternGenerator::createInstance(const Locale& locale, UErrorCode& status) {
if (U_FAILURE(status)) {
return nullptr;
}
LocalPointer<DateTimePatternGenerator> result(
new DateTimePatternGenerator(locale, status), status);
return U_SUCCESS(status) ? result.orphan() : nullptr;
}
DateTimePatternGenerator* U_EXPORT2
DateTimePatternGenerator::createInstanceNoStdPat(const Locale& locale, UErrorCode& status) {
if (U_FAILURE(status)) {
return nullptr;
}
LocalPointer<DateTimePatternGenerator> result(
new DateTimePatternGenerator(locale, status, true), status);
return U_SUCCESS(status) ? result.orphan() : nullptr;
}
DateTimePatternGenerator* U_EXPORT2
DateTimePatternGenerator::createEmptyInstance(UErrorCode& status) {
if (U_FAILURE(status)) {
return nullptr;
}
LocalPointer<DateTimePatternGenerator> result(
new DateTimePatternGenerator(status), status);
return U_SUCCESS(status) ? result.orphan() : nullptr;
}
DateTimePatternGenerator::DateTimePatternGenerator(UErrorCode &status) :
skipMatcher(nullptr),
fAvailableFormatKeyHash(nullptr),
fDefaultHourFormatChar(0),
internalErrorCode(U_ZERO_ERROR)
{
fp = new FormatParser();
dtMatcher = new DateTimeMatcher();
distanceInfo = new DistanceInfo();
patternMap = new PatternMap();
if (fp == nullptr || dtMatcher == nullptr || distanceInfo == nullptr || patternMap == nullptr) {
internalErrorCode = status = U_MEMORY_ALLOCATION_ERROR;
}
}
DateTimePatternGenerator::DateTimePatternGenerator(const Locale& locale, UErrorCode &status, UBool skipStdPatterns) :
skipMatcher(nullptr),
fAvailableFormatKeyHash(nullptr),
fDefaultHourFormatChar(0),
internalErrorCode(U_ZERO_ERROR)
{
fp = new FormatParser();
dtMatcher = new DateTimeMatcher();
distanceInfo = new DistanceInfo();
patternMap = new PatternMap();
if (fp == nullptr || dtMatcher == nullptr || distanceInfo == nullptr || patternMap == nullptr) {
internalErrorCode = status = U_MEMORY_ALLOCATION_ERROR;
}
else {
initData(locale, status, skipStdPatterns);
}
}
DateTimePatternGenerator::DateTimePatternGenerator(const DateTimePatternGenerator& other) :
UObject(),
skipMatcher(nullptr),
fAvailableFormatKeyHash(nullptr),
fDefaultHourFormatChar(0),
internalErrorCode(U_ZERO_ERROR)
{
fp = new FormatParser();
dtMatcher = new DateTimeMatcher();
distanceInfo = new DistanceInfo();
patternMap = new PatternMap();
if (fp == nullptr || dtMatcher == nullptr || distanceInfo == nullptr || patternMap == nullptr) {
internalErrorCode = U_MEMORY_ALLOCATION_ERROR;
}
*this=other;
}
DateTimePatternGenerator&
DateTimePatternGenerator::operator=(const DateTimePatternGenerator& other) {
// reflexive case
if (&other == this) {
return *this;
}
internalErrorCode = other.internalErrorCode;
pLocale = other.pLocale;
fDefaultHourFormatChar = other.fDefaultHourFormatChar;
*fp = *(other.fp);
dtMatcher->copyFrom(other.dtMatcher->skeleton);
*distanceInfo = *(other.distanceInfo);
dateTimeFormat = other.dateTimeFormat;
decimal = other.decimal;
// NUL-terminate for the C API.
dateTimeFormat.getTerminatedBuffer();
decimal.getTerminatedBuffer();
delete skipMatcher;
if ( other.skipMatcher == nullptr ) {
skipMatcher = nullptr;
}
else {
skipMatcher = new DateTimeMatcher(*other.skipMatcher);
if (skipMatcher == nullptr)
{
internalErrorCode = U_MEMORY_ALLOCATION_ERROR;
return *this;
}
}
for (int32_t i=0; i< UDATPG_FIELD_COUNT; ++i ) {
appendItemFormats[i] = other.appendItemFormats[i];
appendItemFormats[i].getTerminatedBuffer(); // NUL-terminate for the C API.
for (int32_t j=0; j< UDATPG_WIDTH_COUNT; ++j ) {
fieldDisplayNames[i][j] = other.fieldDisplayNames[i][j];
fieldDisplayNames[i][j].getTerminatedBuffer(); // NUL-terminate for the C API.
}
}
patternMap->copyFrom(*other.patternMap, internalErrorCode);
copyHashtable(other.fAvailableFormatKeyHash, internalErrorCode);
return *this;
}
bool
DateTimePatternGenerator::operator==(const DateTimePatternGenerator& other) const {
if (this == &other) {
return true;
}
if ((pLocale==other.pLocale) && (patternMap->equals(*other.patternMap)) &&
(dateTimeFormat==other.dateTimeFormat) && (decimal==other.decimal)) {
for ( int32_t i=0 ; i<UDATPG_FIELD_COUNT; ++i ) {
if (appendItemFormats[i] != other.appendItemFormats[i]) {
return false;
}
for (int32_t j=0; j< UDATPG_WIDTH_COUNT; ++j ) {
if (fieldDisplayNames[i][j] != other.fieldDisplayNames[i][j]) {
return false;
}
}
}
return true;
}
else {
return false;
}
}
bool
DateTimePatternGenerator::operator!=(const DateTimePatternGenerator& other) const {
return !operator==(other);
}
DateTimePatternGenerator::~DateTimePatternGenerator() {
if (fAvailableFormatKeyHash!=nullptr) {
delete fAvailableFormatKeyHash;
}
if (fp != nullptr) delete fp;
if (dtMatcher != nullptr) delete dtMatcher;
if (distanceInfo != nullptr) delete distanceInfo;
if (patternMap != nullptr) delete patternMap;
if (skipMatcher != nullptr) delete skipMatcher;
}
namespace {
UInitOnce initOnce = U_INITONCE_INITIALIZER;
UHashtable *localeToAllowedHourFormatsMap = nullptr;
// Value deleter for hashmap.
U_CFUNC void U_CALLCONV deleteAllowedHourFormats(void *ptr) {
uprv_free(ptr);
}
// Close hashmap at cleanup.
U_CFUNC UBool U_CALLCONV allowedHourFormatsCleanup() {
uhash_close(localeToAllowedHourFormatsMap);
return TRUE;
}
enum AllowedHourFormat{
ALLOWED_HOUR_FORMAT_UNKNOWN = -1,
ALLOWED_HOUR_FORMAT_h,
ALLOWED_HOUR_FORMAT_H,
ALLOWED_HOUR_FORMAT_K, // Added ICU-20383, used by JP
ALLOWED_HOUR_FORMAT_k, // Added ICU-20383, not currently used
ALLOWED_HOUR_FORMAT_hb,
ALLOWED_HOUR_FORMAT_hB,
ALLOWED_HOUR_FORMAT_Kb, // Added ICU-20383, not currently used
ALLOWED_HOUR_FORMAT_KB, // Added ICU-20383, not currently used
// ICU-20383 The following are unlikely and not currently used
ALLOWED_HOUR_FORMAT_Hb,
ALLOWED_HOUR_FORMAT_HB
};
} // namespace
void
DateTimePatternGenerator::initData(const Locale& locale, UErrorCode &status, UBool skipStdPatterns) {
//const char *baseLangName = locale.getBaseName(); // unused
skipMatcher = nullptr;
fAvailableFormatKeyHash=nullptr;
addCanonicalItems(status);
if (!skipStdPatterns) { // skip to prevent circular dependency when called from SimpleDateFormat::construct
addICUPatterns(locale, status);
}
addCLDRData(locale, status);
setDateTimeFromCalendar(locale, status);
setDecimalSymbols(locale, status);
umtx_initOnce(initOnce, loadAllowedHourFormatsData, status);
getAllowedHourFormats(locale, status);
// If any of the above methods failed then the object is in an invalid state.
internalErrorCode = status;
} // DateTimePatternGenerator::initData
namespace {
struct AllowedHourFormatsSink : public ResourceSink {
// Initialize sub-sinks.
AllowedHourFormatsSink() {}
virtual ~AllowedHourFormatsSink();
virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
UErrorCode &errorCode) override {
ResourceTable timeData = value.getTable(errorCode);
if (U_FAILURE(errorCode)) { return; }
for (int32_t i = 0; timeData.getKeyAndValue(i, key, value); ++i) {
const char *regionOrLocale = key;
ResourceTable formatList = value.getTable(errorCode);
if (U_FAILURE(errorCode)) { return; }
// below we construct a list[] that has an entry for the "preferred" value at [0],
// followed by 1 or more entries for the "allowed" values, terminated with an
// entry for ALLOWED_HOUR_FORMAT_UNKNOWN (not included in length below)
LocalMemory<int32_t> list;
int32_t length = 0;
int32_t preferredFormat = ALLOWED_HOUR_FORMAT_UNKNOWN;
for (int32_t j = 0; formatList.getKeyAndValue(j, key, value); ++j) {
if (uprv_strcmp(key, "allowed") == 0) {
if (value.getType() == URES_STRING) {
length = 2; // 1 preferred to add later, 1 allowed to add now
if (list.allocateInsteadAndReset(length + 1) == nullptr) {
errorCode = U_MEMORY_ALLOCATION_ERROR;
return;
}
list[1] = getHourFormatFromUnicodeString(value.getUnicodeString(errorCode));
}
else {
ResourceArray allowedFormats = value.getArray(errorCode);
length = allowedFormats.getSize() + 1; // 1 preferred, getSize allowed
if (list.allocateInsteadAndReset(length + 1) == nullptr) {
errorCode = U_MEMORY_ALLOCATION_ERROR;
return;
}
for (int32_t k = 1; k < length; ++k) {
allowedFormats.getValue(k-1, value);
list[k] = getHourFormatFromUnicodeString(value.getUnicodeString(errorCode));
}
}
} else if (uprv_strcmp(key, "preferred") == 0) {
preferredFormat = getHourFormatFromUnicodeString(value.getUnicodeString(errorCode));
}
}
if (length > 1) {
list[0] = (preferredFormat!=ALLOWED_HOUR_FORMAT_UNKNOWN)? preferredFormat: list[1];
} else {
// fallback handling for missing data
length = 2; // 1 preferred, 1 allowed
if (list.allocateInsteadAndReset(length + 1) == nullptr) {
errorCode = U_MEMORY_ALLOCATION_ERROR;
return;
}
list[0] = (preferredFormat!=ALLOWED_HOUR_FORMAT_UNKNOWN)? preferredFormat: ALLOWED_HOUR_FORMAT_H;
list[1] = list[0];
}
list[length] = ALLOWED_HOUR_FORMAT_UNKNOWN;
// At this point list[] will have at least two non-ALLOWED_HOUR_FORMAT_UNKNOWN entries,
// followed by ALLOWED_HOUR_FORMAT_UNKNOWN.
uhash_put(localeToAllowedHourFormatsMap, const_cast<char *>(regionOrLocale), list.orphan(), &errorCode);
if (U_FAILURE(errorCode)) { return; }
}
}
AllowedHourFormat getHourFormatFromUnicodeString(const UnicodeString &s) {
if (s.length() == 1) {
if (s[0] == LOW_H) { return ALLOWED_HOUR_FORMAT_h; }
if (s[0] == CAP_H) { return ALLOWED_HOUR_FORMAT_H; }
if (s[0] == CAP_K) { return ALLOWED_HOUR_FORMAT_K; }
if (s[0] == LOW_K) { return ALLOWED_HOUR_FORMAT_k; }
} else if (s.length() == 2) {
if (s[0] == LOW_H && s[1] == LOW_B) { return ALLOWED_HOUR_FORMAT_hb; }
if (s[0] == LOW_H && s[1] == CAP_B) { return ALLOWED_HOUR_FORMAT_hB; }
if (s[0] == CAP_K && s[1] == LOW_B) { return ALLOWED_HOUR_FORMAT_Kb; }
if (s[0] == CAP_K && s[1] == CAP_B) { return ALLOWED_HOUR_FORMAT_KB; }
if (s[0] == CAP_H && s[1] == LOW_B) { return ALLOWED_HOUR_FORMAT_Hb; }
if (s[0] == CAP_H && s[1] == CAP_B) { return ALLOWED_HOUR_FORMAT_HB; }
}
return ALLOWED_HOUR_FORMAT_UNKNOWN;
}
};
} // namespace
AllowedHourFormatsSink::~AllowedHourFormatsSink() {}
U_CFUNC void U_CALLCONV DateTimePatternGenerator::loadAllowedHourFormatsData(UErrorCode &status) {
if (U_FAILURE(status)) { return; }
localeToAllowedHourFormatsMap = uhash_open(
uhash_hashChars, uhash_compareChars, nullptr, &status);
if (U_FAILURE(status)) { return; }
uhash_setValueDeleter(localeToAllowedHourFormatsMap, deleteAllowedHourFormats);
ucln_i18n_registerCleanup(UCLN_I18N_ALLOWED_HOUR_FORMATS, allowedHourFormatsCleanup);
LocalUResourceBundlePointer rb(ures_openDirect(nullptr, "supplementalData", &status));
if (U_FAILURE(status)) { return; }
AllowedHourFormatsSink sink;
// TODO: Currently in the enumeration each table allocates a new array.
// Try to reduce the number of memory allocations. Consider storing a
// UVector32 with the concatenation of all of the sub-arrays, put the start index
// into the hashmap, store 6 single-value sub-arrays right at the beginning of the
// vector (at index enum*2) for easy data sharing, copy sub-arrays into runtime
// object. Remember to clean up the vector, too.
ures_getAllItemsWithFallback(rb.getAlias(), "timeData", sink, status);
}
static int32_t* getAllowedHourFormatsLangCountry(const char* language, const char* country, UErrorCode& status) {
CharString langCountry;
langCountry.append(language, status);
langCountry.append('_', status);
langCountry.append(country, status);
int32_t* allowedFormats;
allowedFormats = (int32_t *)uhash_get(localeToAllowedHourFormatsMap, langCountry.data());
if (allowedFormats == nullptr) {
allowedFormats = (int32_t *)uhash_get(localeToAllowedHourFormatsMap, const_cast<char *>(country));
}
return allowedFormats;
}
void DateTimePatternGenerator::getAllowedHourFormats(const Locale &locale, UErrorCode &status) {
if (U_FAILURE(status)) { return; }
const char *language = locale.getLanguage();
const char *country = locale.getCountry();
Locale maxLocale; // must be here for correct lifetime
if (*language == '\0' || *country == '\0') {
maxLocale = locale;
UErrorCode localStatus = U_ZERO_ERROR;
maxLocale.addLikelySubtags(localStatus);
if (U_SUCCESS(localStatus)) {
language = maxLocale.getLanguage();
country = maxLocale.getCountry();
}
}
if (*language == '\0') {
// Unexpected, but fail gracefully
language = "und";
}
if (*country == '\0') {
country = "001";
}
int32_t* allowedFormats = getAllowedHourFormatsLangCountry(language, country, status);
// We need to check if there is an hour cycle on locale
char buffer[8];
int32_t count = locale.getKeywordValue("hours", buffer, sizeof(buffer), status);
fDefaultHourFormatChar = 0;
if (U_SUCCESS(status) && count > 0) {
if(uprv_strcmp(buffer, "h24") == 0) {
fDefaultHourFormatChar = LOW_K;
} else if(uprv_strcmp(buffer, "h23") == 0) {
fDefaultHourFormatChar = CAP_H;
} else if(uprv_strcmp(buffer, "h12") == 0) {
fDefaultHourFormatChar = LOW_H;
} else if(uprv_strcmp(buffer, "h11") == 0) {
fDefaultHourFormatChar = CAP_K;
}
}
// Check if the region has an alias
if (allowedFormats == nullptr) {
UErrorCode localStatus = U_ZERO_ERROR;
const Region* region = Region::getInstance(country, localStatus);
if (U_SUCCESS(localStatus)) {
country = region->getRegionCode(); // the real region code
allowedFormats = getAllowedHourFormatsLangCountry(language, country, status);
}
}
if (allowedFormats != nullptr) { // Lookup is successful
// Here allowedFormats points to a list consisting of key for preferredFormat,
// followed by one or more keys for allowedFormats, then followed by ALLOWED_HOUR_FORMAT_UNKNOWN.
if (!fDefaultHourFormatChar) {
switch (allowedFormats[0]) {
case ALLOWED_HOUR_FORMAT_h: fDefaultHourFormatChar = LOW_H; break;
case ALLOWED_HOUR_FORMAT_H: fDefaultHourFormatChar = CAP_H; break;
case ALLOWED_HOUR_FORMAT_K: fDefaultHourFormatChar = CAP_K; break;
case ALLOWED_HOUR_FORMAT_k: fDefaultHourFormatChar = LOW_K; break;
default: fDefaultHourFormatChar = CAP_H; break;
}
}
for (int32_t i = 0; i < UPRV_LENGTHOF(fAllowedHourFormats); ++i) {
fAllowedHourFormats[i] = allowedFormats[i + 1];
if (fAllowedHourFormats[i] == ALLOWED_HOUR_FORMAT_UNKNOWN) {
break;
}
}
} else { // Lookup failed, twice
if (!fDefaultHourFormatChar) {
fDefaultHourFormatChar = CAP_H;
}
fAllowedHourFormats[0] = ALLOWED_HOUR_FORMAT_H;
fAllowedHourFormats[1] = ALLOWED_HOUR_FORMAT_UNKNOWN;
}
}
UDateFormatHourCycle
DateTimePatternGenerator::getDefaultHourCycle(UErrorCode& status) const {
if (U_FAILURE(status)) {
return UDAT_HOUR_CYCLE_23;
}
if (fDefaultHourFormatChar == 0) {
// We need to return something, but the caller should ignore it
// anyways since the returned status is a failure.
status = U_UNSUPPORTED_ERROR;
return UDAT_HOUR_CYCLE_23;
}
switch (fDefaultHourFormatChar) {
case CAP_K:
return UDAT_HOUR_CYCLE_11;
case LOW_H:
return UDAT_HOUR_CYCLE_12;
case CAP_H:
return UDAT_HOUR_CYCLE_23;
case LOW_K:
return UDAT_HOUR_CYCLE_24;
default:
UPRV_UNREACHABLE_EXIT;
}
}
UnicodeString
DateTimePatternGenerator::getSkeleton(const UnicodeString& pattern, UErrorCode&
/*status*/) {
FormatParser fp2;
DateTimeMatcher matcher;
PtnSkeleton localSkeleton;
matcher.set(pattern, &fp2, localSkeleton);
return localSkeleton.getSkeleton();
}
UnicodeString
DateTimePatternGenerator::staticGetSkeleton(
const UnicodeString& pattern, UErrorCode& /*status*/) {
FormatParser fp;
DateTimeMatcher matcher;
PtnSkeleton localSkeleton;
matcher.set(pattern, &fp, localSkeleton);
return localSkeleton.getSkeleton();
}
UnicodeString
DateTimePatternGenerator::getBaseSkeleton(const UnicodeString& pattern, UErrorCode& /*status*/) {
FormatParser fp2;
DateTimeMatcher matcher;
PtnSkeleton localSkeleton;
matcher.set(pattern, &fp2, localSkeleton);
return localSkeleton.getBaseSkeleton();
}
UnicodeString
DateTimePatternGenerator::staticGetBaseSkeleton(
const UnicodeString& pattern, UErrorCode& /*status*/) {
FormatParser fp;
DateTimeMatcher matcher;
PtnSkeleton localSkeleton;
matcher.set(pattern, &fp, localSkeleton);
return localSkeleton.getBaseSkeleton();
}
void
DateTimePatternGenerator::addICUPatterns(const Locale& locale, UErrorCode& status) {
if (U_FAILURE(status)) { return; }
UnicodeString dfPattern;
UnicodeString conflictingString;
DateFormat* df;
// Load with ICU patterns
for (int32_t i=DateFormat::kFull; i<=DateFormat::kShort; i++) {
DateFormat::EStyle style = (DateFormat::EStyle)i;
df = DateFormat::createDateInstance(style, locale);
SimpleDateFormat* sdf;
if (df != nullptr && (sdf = dynamic_cast<SimpleDateFormat*>(df)) != nullptr) {
sdf->toPattern(dfPattern);
addPattern(dfPattern, FALSE, conflictingString, status);
}
// TODO Maybe we should return an error when the date format isn't simple.
delete df;
if (U_FAILURE(status)) { return; }
df = DateFormat::createTimeInstance(style, locale);
if (df != nullptr && (sdf = dynamic_cast<SimpleDateFormat*>(df)) != nullptr) {
sdf->toPattern(dfPattern);
addPattern(dfPattern, FALSE, conflictingString, status);
// TODO: C++ and Java are inconsistent (see #12568).
// C++ uses MEDIUM, but Java uses SHORT.
if ( i==DateFormat::kShort && !dfPattern.isEmpty() ) {
consumeShortTimePattern(dfPattern, status);
}
}
// TODO Maybe we should return an error when the date format isn't simple.
delete df;
if (U_FAILURE(status)) { return; }
}
}
void
DateTimePatternGenerator::hackTimes(const UnicodeString& hackPattern, UErrorCode& status) {
UnicodeString conflictingString;
fp->set(hackPattern);
UnicodeString mmss;
UBool gotMm=FALSE;
for (int32_t i=0; i<fp->itemNumber; ++i) {
UnicodeString field = fp->items[i];
if ( fp->isQuoteLiteral(field) ) {
if ( gotMm ) {
UnicodeString quoteLiteral;
fp->getQuoteLiteral(quoteLiteral, &i);
mmss += quoteLiteral;
}
}
else {
if (fp->isPatternSeparator(field) && gotMm) {
mmss+=field;
}
else {
UChar ch=field.charAt(0);
if (ch==LOW_M) {
gotMm=TRUE;
mmss+=field;
}
else {
if (ch==LOW_S) {
if (!gotMm) {
break;
}
mmss+= field;
addPattern(mmss, FALSE, conflictingString, status);
break;
}
else {
if (gotMm || ch==LOW_Z || ch==CAP_Z || ch==LOW_V || ch==CAP_V) {
break;
}
}
}
}
}
}
}
#define ULOC_LOCALE_IDENTIFIER_CAPACITY (ULOC_FULLNAME_CAPACITY + 1 + ULOC_KEYWORD_AND_VALUES_CAPACITY)
void
DateTimePatternGenerator::getCalendarTypeToUse(const Locale& locale, CharString& destination, UErrorCode& err) {
destination.clear().append(DT_DateTimeGregorianTag, -1, err); // initial default
if ( U_SUCCESS(err) ) {
UErrorCode localStatus = U_ZERO_ERROR;
char localeWithCalendarKey[ULOC_LOCALE_IDENTIFIER_CAPACITY];
// obtain a locale that always has the calendar key value that should be used
ures_getFunctionalEquivalent(
localeWithCalendarKey,
ULOC_LOCALE_IDENTIFIER_CAPACITY,
nullptr,
"calendar",
"calendar",
locale.getName(),
nullptr,
FALSE,
&localStatus);
localeWithCalendarKey[ULOC_LOCALE_IDENTIFIER_CAPACITY-1] = 0; // ensure null termination
// now get the calendar key value from that locale
char calendarType[ULOC_KEYWORDS_CAPACITY];
int32_t calendarTypeLen = uloc_getKeywordValue(
localeWithCalendarKey,
"calendar",
calendarType,
ULOC_KEYWORDS_CAPACITY,
&localStatus);
// If the input locale was invalid, don't fail with missing resource error, instead
// continue with default of Gregorian.
if (U_FAILURE(localStatus) && localStatus != U_MISSING_RESOURCE_ERROR) {
err = localStatus;
return;
}
if (calendarTypeLen > 0 && calendarTypeLen < ULOC_KEYWORDS_CAPACITY) {
destination.clear().append(calendarType, -1, err);
if (U_FAILURE(err)) { return; }
}
}
}
void
DateTimePatternGenerator::consumeShortTimePattern(const UnicodeString& shortTimePattern,
UErrorCode& status) {
if (U_FAILURE(status)) { return; }
// ICU-20383 No longer set fDefaultHourFormatChar to the hour format character from
// this pattern; instead it is set from localeToAllowedHourFormatsMap which now
// includes entries for both preferred and allowed formats.
// HACK for hh:ss
hackTimes(shortTimePattern, status);
}
struct DateTimePatternGenerator::AppendItemFormatsSink : public ResourceSink {
// Destination for data, modified via setters.
DateTimePatternGenerator& dtpg;
AppendItemFormatsSink(DateTimePatternGenerator& _dtpg) : dtpg(_dtpg) {}
virtual ~AppendItemFormatsSink();
virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
UErrorCode &errorCode) override {
UDateTimePatternField field = dtpg.getAppendFormatNumber(key);
if (field == UDATPG_FIELD_COUNT) { return; }
const UnicodeString& valueStr = value.getUnicodeString(errorCode);
if (dtpg.getAppendItemFormat(field).isEmpty() && !valueStr.isEmpty()) {
dtpg.setAppendItemFormat(field, valueStr);
}
}
void fillInMissing() {
UnicodeString defaultItemFormat(TRUE, UDATPG_ItemFormat, UPRV_LENGTHOF(UDATPG_ItemFormat)-1); // Read-only alias.
for (int32_t i = 0; i < UDATPG_FIELD_COUNT; i++) {
UDateTimePatternField field = (UDateTimePatternField)i;
if (dtpg.getAppendItemFormat(field).isEmpty()) {
dtpg.setAppendItemFormat(field, defaultItemFormat);
}
}
}
};
struct DateTimePatternGenerator::AppendItemNamesSink : public ResourceSink {
// Destination for data, modified via setters.
DateTimePatternGenerator& dtpg;
AppendItemNamesSink(DateTimePatternGenerator& _dtpg) : dtpg(_dtpg) {}
virtual ~AppendItemNamesSink();
virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
UErrorCode &errorCode) override {
UDateTimePGDisplayWidth width;
UDateTimePatternField field = dtpg.getFieldAndWidthIndices(key, &width);
if (field == UDATPG_FIELD_COUNT) { return; }
ResourceTable detailsTable = value.getTable(errorCode);
if (U_FAILURE(errorCode)) { return; }
if (!detailsTable.findValue("dn", value)) { return; }
const UnicodeString& valueStr = value.getUnicodeString(errorCode);
if (U_SUCCESS(errorCode) && dtpg.getFieldDisplayName(field,width).isEmpty() && !valueStr.isEmpty()) {
dtpg.setFieldDisplayName(field,width,valueStr);
}
}
void fillInMissing() {
for (int32_t i = 0; i < UDATPG_FIELD_COUNT; i++) {
UnicodeString& valueStr = dtpg.getMutableFieldDisplayName((UDateTimePatternField)i, UDATPG_WIDE);
if (valueStr.isEmpty()) {
valueStr = CAP_F;
U_ASSERT(i < 20);
if (i < 10) {
// F0, F1, ..., F9
valueStr += (UChar)(i+0x30);
} else {
// F10, F11, ...
valueStr += (UChar)0x31;
valueStr += (UChar)(i-10 + 0x30);
}
// NUL-terminate for the C API.
valueStr.getTerminatedBuffer();
}
for (int32_t j = 1; j < UDATPG_WIDTH_COUNT; j++) {
UnicodeString& valueStr2 = dtpg.getMutableFieldDisplayName((UDateTimePatternField)i, (UDateTimePGDisplayWidth)j);
if (valueStr2.isEmpty()) {
valueStr2 = dtpg.getFieldDisplayName((UDateTimePatternField)i, (UDateTimePGDisplayWidth)(j-1));
}
}
}
}
};
struct DateTimePatternGenerator::AvailableFormatsSink : public ResourceSink {
// Destination for data, modified via setters.
DateTimePatternGenerator& dtpg;
// Temporary variable, required for calling addPatternWithSkeleton.
UnicodeString conflictingPattern;
AvailableFormatsSink(DateTimePatternGenerator& _dtpg) : dtpg(_dtpg) {}
virtual ~AvailableFormatsSink();
virtual void put(const char *key, ResourceValue &value, UBool isRoot,
UErrorCode &errorCode) override {
const UnicodeString formatKey(key, -1, US_INV);
if (!dtpg.isAvailableFormatSet(formatKey) ) {
dtpg.setAvailableFormat(formatKey, errorCode);
// Add pattern with its associated skeleton. Override any duplicate
// derived from std patterns, but not a previous availableFormats entry:
const UnicodeString& formatValue = value.getUnicodeString(errorCode);
conflictingPattern.remove();
dtpg.addPatternWithSkeleton(formatValue, &formatKey, !isRoot, conflictingPattern, errorCode);
}
}
};
// Virtual destructors must be defined out of line.
DateTimePatternGenerator::AppendItemFormatsSink::~AppendItemFormatsSink() {}
DateTimePatternGenerator::AppendItemNamesSink::~AppendItemNamesSink() {}
DateTimePatternGenerator::AvailableFormatsSink::~AvailableFormatsSink() {}
void
DateTimePatternGenerator::addCLDRData(const Locale& locale, UErrorCode& errorCode) {
if (U_FAILURE(errorCode)) { return; }
UnicodeString rbPattern, value, field;
CharString path;
LocalUResourceBundlePointer rb(ures_open(nullptr, locale.getName(), &errorCode));
if (U_FAILURE(errorCode)) { return; }
CharString calendarTypeToUse; // to be filled in with the type to use, if all goes well
getCalendarTypeToUse(locale, calendarTypeToUse, errorCode);
if (U_FAILURE(errorCode)) { return; }
// Local err to ignore resource not found exceptions
UErrorCode err = U_ZERO_ERROR;
// Load append item formats.
AppendItemFormatsSink appendItemFormatsSink(*this);
path.clear()
.append(DT_DateTimeCalendarTag, errorCode)
.append('/', errorCode)
.append(calendarTypeToUse, errorCode)
.append('/', errorCode)
.append(DT_DateTimeAppendItemsTag, errorCode); // i.e., calendar/xxx/appendItems
if (U_FAILURE(errorCode)) { return; }
ures_getAllChildrenWithFallback(rb.getAlias(), path.data(), appendItemFormatsSink, err);
appendItemFormatsSink.fillInMissing();
// Load CLDR item names.
err = U_ZERO_ERROR;
AppendItemNamesSink appendItemNamesSink(*this);
ures_getAllChildrenWithFallback(rb.getAlias(), DT_DateTimeFieldsTag, appendItemNamesSink, err);
appendItemNamesSink.fillInMissing();
// Load the available formats from CLDR.
err = U_ZERO_ERROR;
initHashtable(errorCode);
if (U_FAILURE(errorCode)) { return; }
AvailableFormatsSink availableFormatsSink(*this);
path.clear()
.append(DT_DateTimeCalendarTag, errorCode)
.append('/', errorCode)
.append(calendarTypeToUse, errorCode)
.append('/', errorCode)
.append(DT_DateTimeAvailableFormatsTag, errorCode); // i.e., calendar/xxx/availableFormats
if (U_FAILURE(errorCode)) { return; }
ures_getAllChildrenWithFallback(rb.getAlias(), path.data(), availableFormatsSink, err);
}
void
DateTimePatternGenerator::initHashtable(UErrorCode& err) {
if (U_FAILURE(err)) { return; }
if (fAvailableFormatKeyHash!=nullptr) {
return;
}
LocalPointer<Hashtable> hash(new Hashtable(FALSE, err), err);
if (U_SUCCESS(err)) {
fAvailableFormatKeyHash = hash.orphan();
}
}
void
DateTimePatternGenerator::setAppendItemFormat(UDateTimePatternField field, const UnicodeString& value) {
appendItemFormats[field] = value;
// NUL-terminate for the C API.
appendItemFormats[field].getTerminatedBuffer();
}
const UnicodeString&
DateTimePatternGenerator::getAppendItemFormat(UDateTimePatternField field) const {
return appendItemFormats[field];
}
void
DateTimePatternGenerator::setAppendItemName(UDateTimePatternField field, const UnicodeString& value) {
setFieldDisplayName(field, UDATPG_WIDTH_APPENDITEM, value);
}
const UnicodeString&
DateTimePatternGenerator::getAppendItemName(UDateTimePatternField field) const {
return fieldDisplayNames[field][UDATPG_WIDTH_APPENDITEM];
}
void
DateTimePatternGenerator::setFieldDisplayName(UDateTimePatternField field, UDateTimePGDisplayWidth width, const UnicodeString& value) {
fieldDisplayNames[field][width] = value;
// NUL-terminate for the C API.
fieldDisplayNames[field][width].getTerminatedBuffer();
}
UnicodeString
DateTimePatternGenerator::getFieldDisplayName(UDateTimePatternField field, UDateTimePGDisplayWidth width) const {
return fieldDisplayNames[field][width];
}
UnicodeString&
DateTimePatternGenerator::getMutableFieldDisplayName(UDateTimePatternField field, UDateTimePGDisplayWidth width) {
return fieldDisplayNames[field][width];
}
void
DateTimePatternGenerator::getAppendName(UDateTimePatternField field, UnicodeString& value) {
value = SINGLE_QUOTE;
value += fieldDisplayNames[field][UDATPG_WIDTH_APPENDITEM];
value += SINGLE_QUOTE;
}
UnicodeString
DateTimePatternGenerator::getBestPattern(const UnicodeString& patternForm, UErrorCode& status) {
return getBestPattern(patternForm, UDATPG_MATCH_NO_OPTIONS, status);
}
UnicodeString
DateTimePatternGenerator::getBestPattern(const UnicodeString& patternForm, UDateTimePatternMatchOptions options, UErrorCode& status) {
if (U_FAILURE(status)) {
return UnicodeString();
}
if (U_FAILURE(internalErrorCode)) {
status = internalErrorCode;
return UnicodeString();
}
const UnicodeString *bestPattern = nullptr;
UnicodeString dtFormat;
UnicodeString resultPattern;
int32_t flags = kDTPGNoFlags;
int32_t dateMask=(1<<UDATPG_DAYPERIOD_FIELD) - 1;
int32_t timeMask=(1<<UDATPG_FIELD_COUNT) - 1 - dateMask;
// Replace hour metacharacters 'j', 'C' and 'J', set flags as necessary
UnicodeString patternFormMapped = mapSkeletonMetacharacters(patternForm, &flags, status);
if (U_FAILURE(status)) {
return UnicodeString();
}
resultPattern.remove();
dtMatcher->set(patternFormMapped, fp);
const PtnSkeleton* specifiedSkeleton = nullptr;
bestPattern=getBestRaw(*dtMatcher, -1, distanceInfo, status, &specifiedSkeleton);
if (U_FAILURE(status)) {
return UnicodeString();
}
if ( distanceInfo->missingFieldMask==0 && distanceInfo->extraFieldMask==0 ) {
resultPattern = adjustFieldTypes(*bestPattern, specifiedSkeleton, flags, options);
return resultPattern;
}
int32_t neededFields = dtMatcher->getFieldMask();
UnicodeString datePattern=getBestAppending(neededFields & dateMask, flags, status, options);
UnicodeString timePattern=getBestAppending(neededFields & timeMask, flags, status, options);
if (U_FAILURE(status)) {
return UnicodeString();
}
if (datePattern.length()==0) {
if (timePattern.length()==0) {
resultPattern.remove();
}
else {
return timePattern;
}
}
if (timePattern.length()==0) {
return datePattern;
}
resultPattern.remove();
status = U_ZERO_ERROR;
dtFormat=getDateTimeFormat();
SimpleFormatter(dtFormat, 2, 2, status).format(timePattern, datePattern, resultPattern, status);
return resultPattern;
}
/*
* Map a skeleton that may have metacharacters jJC to one without, by replacing
* the metacharacters with locale-appropriate fields of h/H/k/K and of a/b/B
* (depends on fDefaultHourFormatChar and fAllowedHourFormats being set, which in
* turn depends on initData having been run). This method also updates the flags
* as necessary. Returns the updated skeleton.
*/
UnicodeString
DateTimePatternGenerator::mapSkeletonMetacharacters(const UnicodeString& patternForm, int32_t* flags, UErrorCode& status) {
UnicodeString patternFormMapped;
patternFormMapped.remove();
UBool inQuoted = FALSE;
int32_t patPos, patLen = patternForm.length();
for (patPos = 0; patPos < patLen; patPos++) {
UChar patChr = patternForm.charAt(patPos);
if (patChr == SINGLE_QUOTE) {
inQuoted = !inQuoted;
} else if (!inQuoted) {
// Handle special mappings for 'j' and 'C' in which fields lengths
// 1,3,5 => hour field length 1
// 2,4,6 => hour field length 2
// 1,2 => abbreviated dayPeriod (field length 1..3)
// 3,4 => long dayPeriod (field length 4)
// 5,6 => narrow dayPeriod (field length 5)
if (patChr == LOW_J || patChr == CAP_C) {
int32_t extraLen = 0; // 1 less than total field length
while (patPos+1 < patLen && patternForm.charAt(patPos+1)==patChr) {
extraLen++;
patPos++;
}
int32_t hourLen = 1 + (extraLen & 1);
int32_t dayPeriodLen = (extraLen < 2)? 1: 3 + (extraLen >> 1);
UChar hourChar = LOW_H;
UChar dayPeriodChar = LOW_A;
if (patChr == LOW_J) {
hourChar = fDefaultHourFormatChar;
} else {
AllowedHourFormat bestAllowed;
if (fAllowedHourFormats[0] != ALLOWED_HOUR_FORMAT_UNKNOWN) {
bestAllowed = (AllowedHourFormat)fAllowedHourFormats[0];
} else {
status = U_INVALID_FORMAT_ERROR;
return UnicodeString();
}
if (bestAllowed == ALLOWED_HOUR_FORMAT_H || bestAllowed == ALLOWED_HOUR_FORMAT_HB || bestAllowed == ALLOWED_HOUR_FORMAT_Hb) {
hourChar = CAP_H;
} else if (bestAllowed == ALLOWED_HOUR_FORMAT_K || bestAllowed == ALLOWED_HOUR_FORMAT_KB || bestAllowed == ALLOWED_HOUR_FORMAT_Kb) {
hourChar = CAP_K;
} else if (bestAllowed == ALLOWED_HOUR_FORMAT_k) {
hourChar = LOW_K;
}
// in #13183 just add b/B to skeleton, no longer need to set special flags
if (bestAllowed == ALLOWED_HOUR_FORMAT_HB || bestAllowed == ALLOWED_HOUR_FORMAT_hB || bestAllowed == ALLOWED_HOUR_FORMAT_KB) {
dayPeriodChar = CAP_B;
} else if (bestAllowed == ALLOWED_HOUR_FORMAT_Hb || bestAllowed == ALLOWED_HOUR_FORMAT_hb || bestAllowed == ALLOWED_HOUR_FORMAT_Kb) {
dayPeriodChar = LOW_B;
}
}
if (hourChar==CAP_H || hourChar==LOW_K) {
dayPeriodLen = 0;
}
while (dayPeriodLen-- > 0) {
patternFormMapped.append(dayPeriodChar);
}
while (hourLen-- > 0) {
patternFormMapped.append(hourChar);
}
} else if (patChr == CAP_J) {
// Get pattern for skeleton with H, then replace H or k
// with fDefaultHourFormatChar (if different)
patternFormMapped.append(CAP_H);
*flags |= kDTPGSkeletonUsesCapJ;
} else {
patternFormMapped.append(patChr);
}
}
}
return patternFormMapped;
}
UnicodeString
DateTimePatternGenerator::replaceFieldTypes(const UnicodeString& pattern,
const UnicodeString& skeleton,
UErrorCode& status) {
return replaceFieldTypes(pattern, skeleton, UDATPG_MATCH_NO_OPTIONS, status);
}
UnicodeString
DateTimePatternGenerator::replaceFieldTypes(const UnicodeString& pattern,
const UnicodeString& skeleton,
UDateTimePatternMatchOptions options,
UErrorCode& status) {
if (U_FAILURE(status)) {
return UnicodeString();
}
if (U_FAILURE(internalErrorCode)) {
status = internalErrorCode;
return UnicodeString();
}
dtMatcher->set(skeleton, fp);
UnicodeString result = adjustFieldTypes(pattern, nullptr, kDTPGNoFlags, options);
return result;
}
void
DateTimePatternGenerator::setDecimal(const UnicodeString& newDecimal) {
this->decimal = newDecimal;
// NUL-terminate for the C API.
this->decimal.getTerminatedBuffer();
}
const UnicodeString&
DateTimePatternGenerator::getDecimal() const {
return decimal;
}
void
DateTimePatternGenerator::addCanonicalItems(UErrorCode& status) {
if (U_FAILURE(status)) { return; }
UnicodeString conflictingPattern;
for (int32_t i=0; i<UDATPG_FIELD_COUNT; i++) {
if (Canonical_Items[i] > 0) {
addPattern(UnicodeString(Canonical_Items[i]), FALSE, conflictingPattern, status);
}
if (U_FAILURE(status)) { return; }
}
}
void
DateTimePatternGenerator::setDateTimeFormat(const UnicodeString& dtFormat) {
dateTimeFormat = dtFormat;
// NUL-terminate for the C API.
dateTimeFormat.getTerminatedBuffer();
}
const UnicodeString&
DateTimePatternGenerator::getDateTimeFormat() const {
return dateTimeFormat;
}
void
DateTimePatternGenerator::setDateTimeFromCalendar(const Locale& locale, UErrorCode& status) {
if (U_FAILURE(status)) { return; }
const UChar *resStr;
int32_t resStrLen = 0;
LocalPointer<Calendar> fCalendar(Calendar::createInstance(locale, status), status);
if (U_FAILURE(status)) { return; }
LocalUResourceBundlePointer calData(ures_open(nullptr, locale.getBaseName(), &status));
if (U_FAILURE(status)) { return; }
ures_getByKey(calData.getAlias(), DT_DateTimeCalendarTag, calData.getAlias(), &status);
if (U_FAILURE(status)) { return; }
LocalUResourceBundlePointer dateTimePatterns;
if (fCalendar->getType() != nullptr && *fCalendar->getType() != '\0'
&& uprv_strcmp(fCalendar->getType(), DT_DateTimeGregorianTag) != 0) {
dateTimePatterns.adoptInstead(ures_getByKeyWithFallback(calData.getAlias(), fCalendar->getType(),
nullptr, &status));
ures_getByKeyWithFallback(dateTimePatterns.getAlias(), DT_DateTimePatternsTag,
dateTimePatterns.getAlias(), &status);
}
if (dateTimePatterns.isNull() || status == U_MISSING_RESOURCE_ERROR) {
status = U_ZERO_ERROR;
dateTimePatterns.adoptInstead(ures_getByKeyWithFallback(calData.getAlias(), DT_DateTimeGregorianTag,
dateTimePatterns.orphan(), &status));
ures_getByKeyWithFallback(dateTimePatterns.getAlias(), DT_DateTimePatternsTag,
dateTimePatterns.getAlias(), &status);
}
if (U_FAILURE(status)) { return; }
if (ures_getSize(dateTimePatterns.getAlias()) <= DateFormat::kDateTime)
{
status = U_INVALID_FORMAT_ERROR;
return;
}
resStr = ures_getStringByIndex(dateTimePatterns.getAlias(), (int32_t)DateFormat::kDateTime, &resStrLen, &status);
setDateTimeFormat(UnicodeString(TRUE, resStr, resStrLen));
}
void
DateTimePatternGenerator::setDecimalSymbols(const Locale& locale, UErrorCode& status) {
DecimalFormatSymbols dfs = DecimalFormatSymbols(locale, status);
if(U_SUCCESS(status)) {
decimal = dfs.getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol);
// NUL-terminate for the C API.
decimal.getTerminatedBuffer();
}
}
UDateTimePatternConflict
DateTimePatternGenerator::addPattern(
const UnicodeString& pattern,
UBool override,
UnicodeString &conflictingPattern,
UErrorCode& status)
{
if (U_FAILURE(internalErrorCode)) {
status = internalErrorCode;
return UDATPG_NO_CONFLICT;
}
return addPatternWithSkeleton(pattern, nullptr, override, conflictingPattern, status);
}
// For DateTimePatternGenerator::addPatternWithSkeleton -
// If skeletonToUse is specified, then an availableFormats entry is being added. In this case:
// 1. We pass that skeleton to matcher.set instead of having it derive a skeleton from the pattern.
// 2. If the new entry's skeleton or basePattern does match an existing entry but that entry also had a skeleton specified
// (i.e. it was also from availableFormats), then the new entry does not override it regardless of the value of the override
// parameter. This prevents later availableFormats entries from a parent locale overriding earlier ones from the actual
// specified locale. However, availableFormats entries *should* override entries with matching skeleton whose skeleton was
// derived (i.e. entries derived from the standard date/time patters for the specified locale).
// 3. When adding the pattern (patternMap->add), we set a new boolean to indicate that the added entry had a
// specified skeleton (which sets a new field in the PtnElem in the PatternMap).
UDateTimePatternConflict
DateTimePatternGenerator::addPatternWithSkeleton(
const UnicodeString& pattern,
const UnicodeString* skeletonToUse,
UBool override,
UnicodeString& conflictingPattern,
UErrorCode& status)
{
if (U_FAILURE(internalErrorCode)) {
status = internalErrorCode;
return UDATPG_NO_CONFLICT;
}
UnicodeString basePattern;
PtnSkeleton skeleton;
UDateTimePatternConflict conflictingStatus = UDATPG_NO_CONFLICT;
DateTimeMatcher matcher;
if ( skeletonToUse == nullptr ) {
matcher.set(pattern, fp, skeleton);
matcher.getBasePattern(basePattern);
} else {
matcher.set(*skeletonToUse, fp, skeleton); // no longer trims skeleton fields to max len 3, per #7930
matcher.getBasePattern(basePattern); // or perhaps instead: basePattern = *skeletonToUse;
}
// We only care about base conflicts - and replacing the pattern associated with a base - if:
// 1. the conflicting previous base pattern did *not* have an explicit skeleton; in that case the previous
// base + pattern combination was derived from either (a) a canonical item, (b) a standard format, or
// (c) a pattern specified programmatically with a previous call to addPattern (which would only happen
// if we are getting here from a subsequent call to addPattern).
// 2. a skeleton is specified for the current pattern, but override=false; in that case we are checking
// availableFormats items from root, which should not override any previous entry with the same base.
UBool entryHadSpecifiedSkeleton;
const UnicodeString *duplicatePattern = patternMap->getPatternFromBasePattern(basePattern, entryHadSpecifiedSkeleton);
if (duplicatePattern != nullptr && (!entryHadSpecifiedSkeleton || (skeletonToUse != nullptr && !override))) {
conflictingStatus = UDATPG_BASE_CONFLICT;
conflictingPattern = *duplicatePattern;
if (!override) {
return conflictingStatus;
}
}
// The only time we get here with override=true and skeletonToUse!=null is when adding availableFormats
// items from CLDR data. In that case, we don't want an item from a parent locale to replace an item with
// same skeleton from the specified locale, so skip the current item if skeletonWasSpecified is true for
// the previously-specified conflicting item.
const PtnSkeleton* entrySpecifiedSkeleton = nullptr;
duplicatePattern = patternMap->getPatternFromSkeleton(skeleton, &entrySpecifiedSkeleton);
if (duplicatePattern != nullptr ) {
conflictingStatus = UDATPG_CONFLICT;
conflictingPattern = *duplicatePattern;
if (!override || (skeletonToUse != nullptr && entrySpecifiedSkeleton != nullptr)) {
return conflictingStatus;
}
}
patternMap->add(basePattern, skeleton, pattern, skeletonToUse != nullptr, status);
if(U_FAILURE(status)) {
return conflictingStatus;
}
return UDATPG_NO_CONFLICT;
}
UDateTimePatternField
DateTimePatternGenerator::getAppendFormatNumber(const char* field) const {
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
if (uprv_strcmp(CLDR_FIELD_APPEND[i], field)==0) {
return (UDateTimePatternField)i;
}
}
return UDATPG_FIELD_COUNT;
}
UDateTimePatternField
DateTimePatternGenerator::getFieldAndWidthIndices(const char* key, UDateTimePGDisplayWidth* widthP) const {
char cldrFieldKey[UDATPG_FIELD_KEY_MAX + 1];
uprv_strncpy(cldrFieldKey, key, UDATPG_FIELD_KEY_MAX);
cldrFieldKey[UDATPG_FIELD_KEY_MAX]=0; // ensure termination
*widthP = UDATPG_WIDE;
char* hyphenPtr = uprv_strchr(cldrFieldKey, '-');
if (hyphenPtr) {
for (int32_t i=UDATPG_WIDTH_COUNT-1; i>0; --i) {
if (uprv_strcmp(CLDR_FIELD_WIDTH[i], hyphenPtr)==0) {
*widthP=(UDateTimePGDisplayWidth)i;
break;
}
}
*hyphenPtr = 0; // now delete width portion of key
}
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
if (uprv_strcmp(CLDR_FIELD_NAME[i],cldrFieldKey)==0) {
return (UDateTimePatternField)i;
}
}
return UDATPG_FIELD_COUNT;
}
const UnicodeString*
DateTimePatternGenerator::getBestRaw(DateTimeMatcher& source,
int32_t includeMask,
DistanceInfo* missingFields,
UErrorCode &status,
const PtnSkeleton** specifiedSkeletonPtr) {
int32_t bestDistance = 0x7fffffff;
int32_t bestMissingFieldMask = -1;
DistanceInfo tempInfo;
const UnicodeString *bestPattern=nullptr;
const PtnSkeleton* specifiedSkeleton=nullptr;
PatternMapIterator it(status);
if (U_FAILURE(status)) { return nullptr; }
for (it.set(*patternMap); it.hasNext(); ) {
DateTimeMatcher trial = it.next();
if (trial.equals(skipMatcher)) {
continue;
}
int32_t distance=source.getDistance(trial, includeMask, tempInfo);
// Because we iterate over a map the order is undefined. Can change between implementations,
// versions, and will very likely be different between Java and C/C++.
// So if we have patterns with the same distance we also look at the missingFieldMask,
// and we favour the smallest one. Because the field is a bitmask this technically means we
// favour differences in the "least significant fields". For example we prefer the one with differences
// in seconds field vs one with difference in the hours field.
if (distance<bestDistance || (distance==bestDistance && bestMissingFieldMask<tempInfo.missingFieldMask)) {
bestDistance=distance;
bestMissingFieldMask=tempInfo.missingFieldMask;
bestPattern=patternMap->getPatternFromSkeleton(*trial.getSkeletonPtr(), &specifiedSkeleton);
missingFields->setTo(tempInfo);
if (distance==0) {
break;
}
}
}
// If the best raw match had a specified skeleton and that skeleton was requested by the caller,
// then return it too. This generally happens when the caller needs to pass that skeleton
// through to adjustFieldTypes so the latter can do a better job.
if (bestPattern && specifiedSkeletonPtr) {
*specifiedSkeletonPtr = specifiedSkeleton;
}
return bestPattern;
}
UnicodeString
DateTimePatternGenerator::adjustFieldTypes(const UnicodeString& pattern,
const PtnSkeleton* specifiedSkeleton,
int32_t flags,
UDateTimePatternMatchOptions options) {
UnicodeString newPattern;
fp->set(pattern);
for (int32_t i=0; i < fp->itemNumber; i++) {
UnicodeString field = fp->items[i];
if ( fp->isQuoteLiteral(field) ) {
UnicodeString quoteLiteral;
fp->getQuoteLiteral(quoteLiteral, &i);
newPattern += quoteLiteral;
}
else {
if (fp->isPatternSeparator(field)) {
newPattern+=field;
continue;
}
int32_t canonicalIndex = fp->getCanonicalIndex(field);
if (canonicalIndex < 0) {
newPattern+=field;
continue; // don't adjust
}
const dtTypeElem *row = &dtTypes[canonicalIndex];
int32_t typeValue = row->field;
// handle day periods - with #13183, no longer need special handling here, integrated with normal types
if ((flags & kDTPGFixFractionalSeconds) != 0 && typeValue == UDATPG_SECOND_FIELD) {
field += decimal;
dtMatcher->skeleton.original.appendFieldTo(UDATPG_FRACTIONAL_SECOND_FIELD, field);
} else if (dtMatcher->skeleton.type[typeValue]!=0) {
// Here:
// - "reqField" is the field from the originally requested skeleton after replacement
// of metacharacters 'j', 'C' and 'J', with length "reqFieldLen".
// - "field" is the field from the found pattern.
//
// The adjusted field should consist of characters from the originally requested
// skeleton, except in the case of UDATPG_MONTH_FIELD or
// UDATPG_WEEKDAY_FIELD or UDATPG_YEAR_FIELD, in which case it should consist
// of characters from the found pattern. In some cases of UDATPG_HOUR_FIELD,
// there is adjustment following the "defaultHourFormatChar". There is explanation
// how it is done below.
//
// The length of the adjusted field (adjFieldLen) should match that in the originally
// requested skeleton, except that in the following cases the length of the adjusted field
// should match that in the found pattern (i.e. the length of this pattern field should
// not be adjusted):
// 1. typeValue is UDATPG_HOUR_FIELD/MINUTE/SECOND and the corresponding bit in options is
// not set (ticket #7180). Note, we may want to implement a similar change for other
// numeric fields (MM, dd, etc.) so the default behavior is to get locale preference for
// field length, but options bits can be used to override this.
// 2. There is a specified skeleton for the found pattern and one of the following is true:
// a) The length of the field in the skeleton (skelFieldLen) is equal to reqFieldLen.
// b) The pattern field is numeric and the skeleton field is not, or vice versa.
UChar reqFieldChar = dtMatcher->skeleton.original.getFieldChar(typeValue);
int32_t reqFieldLen = dtMatcher->skeleton.original.getFieldLength(typeValue);
if (reqFieldChar == CAP_E && reqFieldLen < 3)
reqFieldLen = 3; // 1-3 for E are equivalent to 3 for c,e
int32_t adjFieldLen = reqFieldLen;
if ( (typeValue==UDATPG_HOUR_FIELD && (options & UDATPG_MATCH_HOUR_FIELD_LENGTH)==0) ||
(typeValue==UDATPG_MINUTE_FIELD && (options & UDATPG_MATCH_MINUTE_FIELD_LENGTH)==0) ||
(typeValue==UDATPG_SECOND_FIELD && (options & UDATPG_MATCH_SECOND_FIELD_LENGTH)==0) ) {
adjFieldLen = field.length();
} else if (specifiedSkeleton && reqFieldChar != LOW_C && reqFieldChar != LOW_E) {
// (we skip this section for 'c' and 'e' because unlike the other characters considered in this function,
// they have no minimum field length-- 'E' and 'EE' are equivalent to 'EEE', but 'e' and 'ee' are not
// equivalent to 'eee' -- see the entries for "week day" in
// https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table for more info)
int32_t skelFieldLen = specifiedSkeleton->original.getFieldLength(typeValue);
UBool patFieldIsNumeric = (row->type > 0);
UBool skelFieldIsNumeric = (specifiedSkeleton->type[typeValue] > 0);
if (skelFieldLen == reqFieldLen || (patFieldIsNumeric && !skelFieldIsNumeric) || (skelFieldIsNumeric && !patFieldIsNumeric)) {
// don't adjust the field length in the found pattern
adjFieldLen = field.length();
}
}
UChar c = (typeValue!= UDATPG_HOUR_FIELD
&& typeValue!= UDATPG_MONTH_FIELD
&& typeValue!= UDATPG_WEEKDAY_FIELD
&& (typeValue!= UDATPG_YEAR_FIELD || reqFieldChar==CAP_Y))
? reqFieldChar
: field.charAt(0);
if (c == CAP_E && adjFieldLen < 3) {
c = LOW_E;
}
if (typeValue == UDATPG_HOUR_FIELD && fDefaultHourFormatChar != 0) {
// The adjustment here is required to match spec (https://www.unicode.org/reports/tr35/tr35-dates.html#dfst-hour).
// It is necessary to match the hour-cycle preferred by the Locale.
// Given that, we need to do the following adjustments:
// 1. When hour-cycle is h11 it should replace 'h' by 'K'.
// 2. When hour-cycle is h23 it should replace 'H' by 'k'.
// 3. When hour-cycle is h24 it should replace 'k' by 'H'.
// 4. When hour-cycle is h12 it should replace 'K' by 'h'.
if ((flags & kDTPGSkeletonUsesCapJ) != 0 || reqFieldChar == fDefaultHourFormatChar) {
c = fDefaultHourFormatChar;
} else if (reqFieldChar == LOW_H && fDefaultHourFormatChar == CAP_K) {
c = CAP_K;
} else if (reqFieldChar == CAP_H && fDefaultHourFormatChar == LOW_K) {
c = LOW_K;
} else if (reqFieldChar == LOW_K && fDefaultHourFormatChar == CAP_H) {
c = CAP_H;
} else if (reqFieldChar == CAP_K && fDefaultHourFormatChar == LOW_H) {
c = LOW_H;
}
}
field.remove();
for (int32_t j=adjFieldLen; j>0; --j) {
field += c;
}
}
newPattern+=field;
}
}
return newPattern;
}
UnicodeString
DateTimePatternGenerator::getBestAppending(int32_t missingFields, int32_t flags, UErrorCode &status, UDateTimePatternMatchOptions options) {
if (U_FAILURE(status)) {
return UnicodeString();
}
UnicodeString resultPattern, tempPattern;
const UnicodeString* tempPatternPtr;
int32_t lastMissingFieldMask=0;
if (missingFields!=0) {
resultPattern=UnicodeString();
const PtnSkeleton* specifiedSkeleton=nullptr;
tempPatternPtr = getBestRaw(*dtMatcher, missingFields, distanceInfo, status, &specifiedSkeleton);
if (U_FAILURE(status)) {
return UnicodeString();
}
tempPattern = *tempPatternPtr;
resultPattern = adjustFieldTypes(tempPattern, specifiedSkeleton, flags, options);
if ( distanceInfo->missingFieldMask==0 ) {
return resultPattern;
}
while (distanceInfo->missingFieldMask!=0) { // precondition: EVERY single field must work!
if ( lastMissingFieldMask == distanceInfo->missingFieldMask ) {
break; // cannot find the proper missing field
}
if (((distanceInfo->missingFieldMask & UDATPG_SECOND_AND_FRACTIONAL_MASK)==UDATPG_FRACTIONAL_MASK) &&
((missingFields & UDATPG_SECOND_AND_FRACTIONAL_MASK) == UDATPG_SECOND_AND_FRACTIONAL_MASK)) {
resultPattern = adjustFieldTypes(resultPattern, specifiedSkeleton, flags | kDTPGFixFractionalSeconds, options);
distanceInfo->missingFieldMask &= ~UDATPG_FRACTIONAL_MASK;
continue;
}
int32_t startingMask = distanceInfo->missingFieldMask;
tempPatternPtr = getBestRaw(*dtMatcher, distanceInfo->missingFieldMask, distanceInfo, status, &specifiedSkeleton);
if (U_FAILURE(status)) {
return UnicodeString();
}
tempPattern = *tempPatternPtr;
tempPattern = adjustFieldTypes(tempPattern, specifiedSkeleton, flags, options);
int32_t foundMask=startingMask& ~distanceInfo->missingFieldMask;
int32_t topField=getTopBitNumber(foundMask);
if (appendItemFormats[topField].length() != 0) {
UnicodeString appendName;
getAppendName((UDateTimePatternField)topField, appendName);
const UnicodeString *values[3] = {
&resultPattern,
&tempPattern,
&appendName
};
SimpleFormatter(appendItemFormats[topField], 2, 3, status).
formatAndReplace(values, 3, resultPattern, nullptr, 0, status);
}
lastMissingFieldMask = distanceInfo->missingFieldMask;
}
}
return resultPattern;
}
int32_t
DateTimePatternGenerator::getTopBitNumber(int32_t foundMask) const {
if ( foundMask==0 ) {
return 0;
}
int32_t i=0;
while (foundMask!=0) {
foundMask >>=1;
++i;
}
if (i-1 >UDATPG_ZONE_FIELD) {
return UDATPG_ZONE_FIELD;
}
else
return i-1;
}
void
DateTimePatternGenerator::setAvailableFormat(const UnicodeString &key, UErrorCode& err)
{
fAvailableFormatKeyHash->puti(key, 1, err);
}
UBool
DateTimePatternGenerator::isAvailableFormatSet(const UnicodeString &key) const {
return (UBool)(fAvailableFormatKeyHash->geti(key) == 1);
}
void
DateTimePatternGenerator::copyHashtable(Hashtable *other, UErrorCode &status) {
if (other == nullptr || U_FAILURE(status)) {
return;
}
if (fAvailableFormatKeyHash != nullptr) {
delete fAvailableFormatKeyHash;
fAvailableFormatKeyHash = nullptr;
}
initHashtable(status);
if(U_FAILURE(status)){
return;
}
int32_t pos = UHASH_FIRST;
const UHashElement* elem = nullptr;
// walk through the hash table and create a deep clone
while((elem = other->nextElement(pos))!= nullptr){
const UHashTok otherKeyTok = elem->key;
UnicodeString* otherKey = (UnicodeString*)otherKeyTok.pointer;
fAvailableFormatKeyHash->puti(*otherKey, 1, status);
if(U_FAILURE(status)){
return;
}
}
}
StringEnumeration*
DateTimePatternGenerator::getSkeletons(UErrorCode& status) const {
if (U_FAILURE(status)) {
return nullptr;
}
if (U_FAILURE(internalErrorCode)) {
status = internalErrorCode;
return nullptr;
}
LocalPointer<StringEnumeration> skeletonEnumerator(
new DTSkeletonEnumeration(*patternMap, DT_SKELETON, status), status);
return U_SUCCESS(status) ? skeletonEnumerator.orphan() : nullptr;
}
const UnicodeString&
DateTimePatternGenerator::getPatternForSkeleton(const UnicodeString& skeleton) const {
PtnElem *curElem;
if (skeleton.length() ==0) {
return emptyString;
}
curElem = patternMap->getHeader(skeleton.charAt(0));
while ( curElem != nullptr ) {
if ( curElem->skeleton->getSkeleton()==skeleton ) {
return curElem->pattern;
}
curElem = curElem->next.getAlias();
}
return emptyString;
}
StringEnumeration*
DateTimePatternGenerator::getBaseSkeletons(UErrorCode& status) const {
if (U_FAILURE(status)) {
return nullptr;
}
if (U_FAILURE(internalErrorCode)) {
status = internalErrorCode;
return nullptr;
}
LocalPointer<StringEnumeration> baseSkeletonEnumerator(
new DTSkeletonEnumeration(*patternMap, DT_BASESKELETON, status), status);
return U_SUCCESS(status) ? baseSkeletonEnumerator.orphan() : nullptr;
}
StringEnumeration*
DateTimePatternGenerator::getRedundants(UErrorCode& status) {
if (U_FAILURE(status)) { return nullptr; }
if (U_FAILURE(internalErrorCode)) {
status = internalErrorCode;
return nullptr;
}
LocalPointer<StringEnumeration> output(new DTRedundantEnumeration(), status);
if (U_FAILURE(status)) { return nullptr; }
const UnicodeString *pattern;
PatternMapIterator it(status);
if (U_FAILURE(status)) { return nullptr; }
for (it.set(*patternMap); it.hasNext(); ) {
DateTimeMatcher current = it.next();
pattern = patternMap->getPatternFromSkeleton(*(it.getSkeleton()));
if ( isCanonicalItem(*pattern) ) {
continue;
}
if ( skipMatcher == nullptr ) {
skipMatcher = new DateTimeMatcher(current);
if (skipMatcher == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR;
return nullptr;
}
}
else {
*skipMatcher = current;
}
UnicodeString trial = getBestPattern(current.getPattern(), status);
if (U_FAILURE(status)) { return nullptr; }
if (trial == *pattern) {
((DTRedundantEnumeration *)output.getAlias())->add(*pattern, status);
if (U_FAILURE(status)) { return nullptr; }
}
if (current.equals(skipMatcher)) {
continue;
}
}
return output.orphan();
}
UBool
DateTimePatternGenerator::isCanonicalItem(const UnicodeString& item) const {
if ( item.length() != 1 ) {
return FALSE;
}
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
if (item.charAt(0)==Canonical_Items[i]) {
return TRUE;
}
}
return FALSE;
}
DateTimePatternGenerator*
DateTimePatternGenerator::clone() const {
return new DateTimePatternGenerator(*this);
}
PatternMap::PatternMap() {
for (int32_t i=0; i < MAX_PATTERN_ENTRIES; ++i ) {
boot[i] = nullptr;
}
isDupAllowed = TRUE;
}
void
PatternMap::copyFrom(const PatternMap& other, UErrorCode& status) {
if (U_FAILURE(status)) {
return;
}
this->isDupAllowed = other.isDupAllowed;
for (int32_t bootIndex = 0; bootIndex < MAX_PATTERN_ENTRIES; ++bootIndex) {
PtnElem *curElem, *otherElem, *prevElem=nullptr;
otherElem = other.boot[bootIndex];
while (otherElem != nullptr) {
LocalPointer<PtnElem> newElem(new PtnElem(otherElem->basePattern, otherElem->pattern), status);
if (U_FAILURE(status)) {
return; // out of memory
}
newElem->skeleton.adoptInsteadAndCheckErrorCode(new PtnSkeleton(*(otherElem->skeleton)), status);
if (U_FAILURE(status)) {
return; // out of memory
}
newElem->skeletonWasSpecified = otherElem->skeletonWasSpecified;
// Release ownership from the LocalPointer of the PtnElem object.
// The PtnElem will now be owned by either the boot (for the first entry in the linked-list)
// or owned by the previous PtnElem object in the linked-list.
curElem = newElem.orphan();
if (this->boot[bootIndex] == nullptr) {
this->boot[bootIndex] = curElem;
} else {
if (prevElem != nullptr) {
prevElem->next.adoptInstead(curElem);
} else {
UPRV_UNREACHABLE_EXIT;
}
}
prevElem = curElem;
otherElem = otherElem->next.getAlias();
}
}
}
PtnElem*
PatternMap::getHeader(UChar baseChar) const {
PtnElem* curElem;
if ( (baseChar >= CAP_A) && (baseChar <= CAP_Z) ) {
curElem = boot[baseChar-CAP_A];
}
else {
if ( (baseChar >=LOW_A) && (baseChar <= LOW_Z) ) {
curElem = boot[26+baseChar-LOW_A];
}
else {
return nullptr;
}
}
return curElem;
}
PatternMap::~PatternMap() {
for (int32_t i=0; i < MAX_PATTERN_ENTRIES; ++i ) {
if (boot[i] != nullptr ) {
delete boot[i];
boot[i] = nullptr;
}
}
} // PatternMap destructor
void
PatternMap::add(const UnicodeString& basePattern,
const PtnSkeleton& skeleton,
const UnicodeString& value,// mapped pattern value
UBool skeletonWasSpecified,
UErrorCode &status) {
UChar baseChar = basePattern.charAt(0);
PtnElem *curElem, *baseElem;
status = U_ZERO_ERROR;
// the baseChar must be A-Z or a-z
if ((baseChar >= CAP_A) && (baseChar <= CAP_Z)) {
baseElem = boot[baseChar-CAP_A];
}
else {
if ((baseChar >=LOW_A) && (baseChar <= LOW_Z)) {
baseElem = boot[26+baseChar-LOW_A];
}
else {
status = U_ILLEGAL_CHARACTER;
return;
}
}
if (baseElem == nullptr) {
LocalPointer<PtnElem> newElem(new PtnElem(basePattern, value), status);
if (U_FAILURE(status)) {
return; // out of memory
}
newElem->skeleton.adoptInsteadAndCheckErrorCode(new PtnSkeleton(skeleton), status);
if (U_FAILURE(status)) {
return; // out of memory
}
newElem->skeletonWasSpecified = skeletonWasSpecified;
if (baseChar >= LOW_A) {
boot[26 + (baseChar - LOW_A)] = newElem.orphan(); // the boot array now owns the PtnElem.
}
else {
boot[baseChar - CAP_A] = newElem.orphan(); // the boot array now owns the PtnElem.
}
}
if ( baseElem != nullptr ) {
curElem = getDuplicateElem(basePattern, skeleton, baseElem);
if (curElem == nullptr) {
// add new element to the list.
curElem = baseElem;
while( curElem -> next != nullptr )
{
curElem = curElem->next.getAlias();
}
LocalPointer<PtnElem> newElem(new PtnElem(basePattern, value), status);
if (U_FAILURE(status)) {
return; // out of memory
}
newElem->skeleton.adoptInsteadAndCheckErrorCode(new PtnSkeleton(skeleton), status);
if (U_FAILURE(status)) {
return; // out of memory
}
newElem->skeletonWasSpecified = skeletonWasSpecified;
curElem->next.adoptInstead(newElem.orphan());
curElem = curElem->next.getAlias();
}
else {
// Pattern exists in the list already.
if ( !isDupAllowed ) {
return;
}
// Overwrite the value.
curElem->pattern = value;
// It was a bug that we were not doing the following previously,
// though that bug hid other problems by making things partly work.
curElem->skeletonWasSpecified = skeletonWasSpecified;
}
}
} // PatternMap::add
// Find the pattern from the given basePattern string.
const UnicodeString *
PatternMap::getPatternFromBasePattern(const UnicodeString& basePattern, UBool& skeletonWasSpecified) const { // key to search for
PtnElem *curElem;
if ((curElem=getHeader(basePattern.charAt(0)))==nullptr) {
return nullptr; // no match
}
do {
if ( basePattern.compare(curElem->basePattern)==0 ) {
skeletonWasSpecified = curElem->skeletonWasSpecified;
return &(curElem->pattern);
}
curElem = curElem->next.getAlias();
} while (curElem != nullptr);
return nullptr;
} // PatternMap::getFromBasePattern
// Find the pattern from the given skeleton.
// At least when this is called from getBestRaw & addPattern (in which case specifiedSkeletonPtr is non-NULL),
// the comparison should be based on skeleton.original (which is unique and tied to the distance measurement in bestRaw)
// and not skeleton.baseOriginal (which is not unique); otherwise we may pick a different skeleton than the one with the
// optimum distance value in getBestRaw. When this is called from public getRedundants (specifiedSkeletonPtr is NULL),
// for now it will continue to compare based on baseOriginal so as not to change the behavior unnecessarily.
const UnicodeString *
PatternMap::getPatternFromSkeleton(const PtnSkeleton& skeleton, const PtnSkeleton** specifiedSkeletonPtr) const { // key to search for
PtnElem *curElem;
if (specifiedSkeletonPtr) {
*specifiedSkeletonPtr = nullptr;
}
// find boot entry
UChar baseChar = skeleton.getFirstChar();
if ((curElem=getHeader(baseChar))==nullptr) {
return nullptr; // no match
}
do {
UBool equal;
if (specifiedSkeletonPtr != nullptr) { // called from DateTimePatternGenerator::getBestRaw or addPattern, use original
equal = curElem->skeleton->original == skeleton.original;
} else { // called from DateTimePatternGenerator::getRedundants, use baseOriginal
equal = curElem->skeleton->baseOriginal == skeleton.baseOriginal;
}
if (equal) {
if (specifiedSkeletonPtr && curElem->skeletonWasSpecified) {
*specifiedSkeletonPtr = curElem->skeleton.getAlias();
}
return &(curElem->pattern);
}
curElem = curElem->next.getAlias();
} while (curElem != nullptr);
return nullptr;
}
UBool
PatternMap::equals(const PatternMap& other) const {
if ( this==&other ) {
return TRUE;
}
for (int32_t bootIndex = 0; bootIndex < MAX_PATTERN_ENTRIES; ++bootIndex) {
if (boot[bootIndex] == other.boot[bootIndex]) {
continue;
}
if ((boot[bootIndex] == nullptr) || (other.boot[bootIndex] == nullptr)) {
return FALSE;
}
PtnElem *otherElem = other.boot[bootIndex];
PtnElem *myElem = boot[bootIndex];
while ((otherElem != nullptr) || (myElem != nullptr)) {
if ( myElem == otherElem ) {
break;
}
if ((otherElem == nullptr) || (myElem == nullptr)) {
return FALSE;
}
if ( (myElem->basePattern != otherElem->basePattern) ||
(myElem->pattern != otherElem->pattern) ) {
return FALSE;
}
if ((myElem->skeleton.getAlias() != otherElem->skeleton.getAlias()) &&
!myElem->skeleton->equals(*(otherElem->skeleton))) {
return FALSE;
}
myElem = myElem->next.getAlias();
otherElem = otherElem->next.getAlias();
}
}
return TRUE;
}
// find any key existing in the mapping table already.
// return TRUE if there is an existing key, otherwise return FALSE.
PtnElem*
PatternMap::getDuplicateElem(
const UnicodeString &basePattern,
const PtnSkeleton &skeleton,
PtnElem *baseElem) {
PtnElem *curElem;
if ( baseElem == nullptr ) {
return nullptr;
}
else {
curElem = baseElem;
}
do {
if ( basePattern.compare(curElem->basePattern)==0 ) {
UBool isEqual = TRUE;
for (int32_t i = 0; i < UDATPG_FIELD_COUNT; ++i) {
if (curElem->skeleton->type[i] != skeleton.type[i] ) {
isEqual = FALSE;
break;
}
}
if (isEqual) {
return curElem;
}
}
curElem = curElem->next.getAlias();
} while( curElem != nullptr );
// end of the list
return nullptr;
} // PatternMap::getDuplicateElem
DateTimeMatcher::DateTimeMatcher(void) {
}
DateTimeMatcher::~DateTimeMatcher() {}
DateTimeMatcher::DateTimeMatcher(const DateTimeMatcher& other) {
copyFrom(other.skeleton);
}
DateTimeMatcher& DateTimeMatcher::operator=(const DateTimeMatcher& other) {
copyFrom(other.skeleton);
return *this;
}
void
DateTimeMatcher::set(const UnicodeString& pattern, FormatParser* fp) {
PtnSkeleton localSkeleton;
return set(pattern, fp, localSkeleton);
}
void
DateTimeMatcher::set(const UnicodeString& pattern, FormatParser* fp, PtnSkeleton& skeletonResult) {
int32_t i;
for (i=0; i<UDATPG_FIELD_COUNT; ++i) {
skeletonResult.type[i] = NONE;
}
skeletonResult.original.clear();
skeletonResult.baseOriginal.clear();
skeletonResult.addedDefaultDayPeriod = FALSE;
fp->set(pattern);
for (i=0; i < fp->itemNumber; i++) {
const UnicodeString& value = fp->items[i];
// don't skip 'a' anymore, dayPeriod handled specially below
if ( fp->isQuoteLiteral(value) ) {
UnicodeString quoteLiteral;
fp->getQuoteLiteral(quoteLiteral, &i);
continue;
}
int32_t canonicalIndex = fp->getCanonicalIndex(value);
if (canonicalIndex < 0) {
continue;
}
const dtTypeElem *row = &dtTypes[canonicalIndex];
int32_t field = row->field;
skeletonResult.original.populate(field, value);
UChar repeatChar = row->patternChar;
int32_t repeatCount = row->minLen;
skeletonResult.baseOriginal.populate(field, repeatChar, repeatCount);
int16_t subField = row->type;
if (row->type > 0) {
U_ASSERT(value.length() < INT16_MAX);
subField += static_cast<int16_t>(value.length());
}
skeletonResult.type[field] = subField;
}
// #20739, we have a skeleton with minutes and milliseconds, but no seconds
//
// Theoretically we would need to check and fix all fields with "gaps":
// for example year-day (no month), month-hour (no day), and so on, All the possible field combinations.
// Plus some smartness: year + hour => should we add month, or add day-of-year?
// What about month + day-of-week, or month + am/pm indicator.
// I think beyond a certain point we should not try to fix bad developer input and try guessing what they mean.
// Garbage in, garbage out.
if (!skeletonResult.original.isFieldEmpty(UDATPG_MINUTE_FIELD)
&& !skeletonResult.original.isFieldEmpty(UDATPG_FRACTIONAL_SECOND_FIELD)
&& skeletonResult.original.isFieldEmpty(UDATPG_SECOND_FIELD)) {
// Force the use of seconds
for (i = 0; dtTypes[i].patternChar != 0; i++) {
if (dtTypes[i].field == UDATPG_SECOND_FIELD) {
// first entry for UDATPG_SECOND_FIELD
skeletonResult.original.populate(UDATPG_SECOND_FIELD, dtTypes[i].patternChar, dtTypes[i].minLen);
skeletonResult.baseOriginal.populate(UDATPG_SECOND_FIELD, dtTypes[i].patternChar, dtTypes[i].minLen);
// We add value.length, same as above, when type is first initialized.
// The value we want to "fake" here is "s", and 1 means "s".length()
int16_t subField = dtTypes[i].type;
skeletonResult.type[UDATPG_SECOND_FIELD] = (subField > 0) ? subField + 1 : subField;
break;
}
}
}
// #13183, handle special behavior for day period characters (a, b, B)
if (!skeletonResult.original.isFieldEmpty(UDATPG_HOUR_FIELD)) {
if (skeletonResult.original.getFieldChar(UDATPG_HOUR_FIELD)==LOW_H || skeletonResult.original.getFieldChar(UDATPG_HOUR_FIELD)==CAP_K) {
// We have a skeleton with 12-hour-cycle format
if (skeletonResult.original.isFieldEmpty(UDATPG_DAYPERIOD_FIELD)) {
// But we do not have a day period in the skeleton; add the default DAYPERIOD (currently "a")
for (i = 0; dtTypes[i].patternChar != 0; i++) {
if ( dtTypes[i].field == UDATPG_DAYPERIOD_FIELD ) {
// first entry for UDATPG_DAYPERIOD_FIELD
skeletonResult.original.populate(UDATPG_DAYPERIOD_FIELD, dtTypes[i].patternChar, dtTypes[i].minLen);
skeletonResult.baseOriginal.populate(UDATPG_DAYPERIOD_FIELD, dtTypes[i].patternChar, dtTypes[i].minLen);
skeletonResult.type[UDATPG_DAYPERIOD_FIELD] = dtTypes[i].type;
skeletonResult.addedDefaultDayPeriod = TRUE;
break;
}
}
}
} else {
// Skeleton has 24-hour-cycle hour format and has dayPeriod, delete dayPeriod (i.e. ignore it)
skeletonResult.original.clearField(UDATPG_DAYPERIOD_FIELD);
skeletonResult.baseOriginal.clearField(UDATPG_DAYPERIOD_FIELD);
skeletonResult.type[UDATPG_DAYPERIOD_FIELD] = NONE;
}
}
copyFrom(skeletonResult);
}
void
DateTimeMatcher::getBasePattern(UnicodeString &result ) {
result.remove(); // Reset the result first.
skeleton.baseOriginal.appendTo(result);
}
UnicodeString
DateTimeMatcher::getPattern() {
UnicodeString result;
return skeleton.original.appendTo(result);
}
int32_t
DateTimeMatcher::getDistance(const DateTimeMatcher& other, int32_t includeMask, DistanceInfo& distanceInfo) const {
int32_t result = 0;
distanceInfo.clear();
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
int32_t myType = (includeMask&(1<<i))==0 ? 0 : skeleton.type[i];
int32_t otherType = other.skeleton.type[i];
if (myType==otherType) {
continue;
}
if (myType==0) {// and other is not
result += EXTRA_FIELD;
distanceInfo.addExtra(i);
}
else {
if (otherType==0) {
result += MISSING_FIELD;
distanceInfo.addMissing(i);
}
else {
result += abs(myType - otherType);
}
}
}
return result;
}
void
DateTimeMatcher::copyFrom(const PtnSkeleton& newSkeleton) {
skeleton.copyFrom(newSkeleton);
}
void
DateTimeMatcher::copyFrom() {
// same as clear
skeleton.clear();
}
UBool
DateTimeMatcher::equals(const DateTimeMatcher* other) const {
if (other==nullptr) { return FALSE; }
return skeleton.original == other->skeleton.original;
}
int32_t
DateTimeMatcher::getFieldMask() const {
int32_t result = 0;
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
if (skeleton.type[i]!=0) {
result |= (1<<i);
}
}
return result;
}
PtnSkeleton*
DateTimeMatcher::getSkeletonPtr() {
return &skeleton;
}
FormatParser::FormatParser () {
status = START;
itemNumber = 0;
}
FormatParser::~FormatParser () {
}
// Find the next token with the starting position and length
// Note: the startPos may
FormatParser::TokenStatus
FormatParser::setTokens(const UnicodeString& pattern, int32_t startPos, int32_t *len) {
int32_t curLoc = startPos;
if ( curLoc >= pattern.length()) {
return DONE;
}
// check the current char is between A-Z or a-z
do {
UChar c=pattern.charAt(curLoc);
if ( (c>=CAP_A && c<=CAP_Z) || (c>=LOW_A && c<=LOW_Z) ) {
curLoc++;
}
else {
startPos = curLoc;
*len=1;
return ADD_TOKEN;
}
if ( pattern.charAt(curLoc)!= pattern.charAt(startPos) ) {
break; // not the same token
}
} while(curLoc <= pattern.length());
*len = curLoc-startPos;
return ADD_TOKEN;
}
void
FormatParser::set(const UnicodeString& pattern) {
int32_t startPos = 0;
TokenStatus result = START;
int32_t len = 0;
itemNumber = 0;
do {
result = setTokens( pattern, startPos, &len );
if ( result == ADD_TOKEN )
{
items[itemNumber++] = UnicodeString(pattern, startPos, len );
startPos += len;
}
else {
break;
}
} while (result==ADD_TOKEN && itemNumber < MAX_DT_TOKEN);
}
int32_t
FormatParser::getCanonicalIndex(const UnicodeString& s, UBool strict) {
int32_t len = s.length();
if (len == 0) {
return -1;
}
UChar ch = s.charAt(0);
// Verify that all are the same character.
for (int32_t l = 1; l < len; l++) {
if (ch != s.charAt(l)) {
return -1;
}
}
int32_t i = 0;
int32_t bestRow = -1;
while (dtTypes[i].patternChar != 0x0000) {
if ( dtTypes[i].patternChar != ch ) {
++i;
continue;
}
bestRow = i;
if (dtTypes[i].patternChar != dtTypes[i+1].patternChar) {
return i;
}
if (dtTypes[i+1].minLen <= len) {
++i;
continue;
}
return i;
}
return strict ? -1 : bestRow;
}
UBool
FormatParser::isQuoteLiteral(const UnicodeString& s) {
return (UBool)(s.charAt(0) == SINGLE_QUOTE);
}
// This function assumes the current itemIndex points to the quote literal.
// Please call isQuoteLiteral prior to this function.
void
FormatParser::getQuoteLiteral(UnicodeString& quote, int32_t *itemIndex) {
int32_t i = *itemIndex;
quote.remove();
if (items[i].charAt(0)==SINGLE_QUOTE) {
quote += items[i];
++i;
}
while ( i < itemNumber ) {
if ( items[i].charAt(0)==SINGLE_QUOTE ) {
if ( (i+1<itemNumber) && (items[i+1].charAt(0)==SINGLE_QUOTE)) {
// two single quotes e.g. 'o''clock'
quote += items[i++];
quote += items[i++];
continue;
}
else {
quote += items[i];
break;
}
}
else {
quote += items[i];
}
++i;
}
*itemIndex=i;
}
UBool
FormatParser::isPatternSeparator(const UnicodeString& field) const {
for (int32_t i=0; i<field.length(); ++i ) {
UChar c= field.charAt(i);
if ( (c==SINGLE_QUOTE) || (c==BACKSLASH) || (c==SPACE) || (c==COLON) ||
(c==QUOTATION_MARK) || (c==COMMA) || (c==HYPHEN) ||(items[i].charAt(0)==DOT) ) {
continue;
}
else {
return FALSE;
}
}
return TRUE;
}
DistanceInfo::~DistanceInfo() {}
void
DistanceInfo::setTo(const DistanceInfo& other) {
missingFieldMask = other.missingFieldMask;
extraFieldMask= other.extraFieldMask;
}
PatternMapIterator::PatternMapIterator(UErrorCode& status) :
bootIndex(0), nodePtr(nullptr), matcher(nullptr), patternMap(nullptr)
{
if (U_FAILURE(status)) { return; }
matcher.adoptInsteadAndCheckErrorCode(new DateTimeMatcher(), status);
}
PatternMapIterator::~PatternMapIterator() {
}
void
PatternMapIterator::set(PatternMap& newPatternMap) {
this->patternMap=&newPatternMap;
}
PtnSkeleton*
PatternMapIterator::getSkeleton() const {
if ( nodePtr == nullptr ) {
return nullptr;
}
else {
return nodePtr->skeleton.getAlias();
}
}
UBool
PatternMapIterator::hasNext() const {
int32_t headIndex = bootIndex;
PtnElem *curPtr = nodePtr;
if (patternMap==nullptr) {
return FALSE;
}
while ( headIndex < MAX_PATTERN_ENTRIES ) {
if ( curPtr != nullptr ) {
if ( curPtr->next != nullptr ) {
return TRUE;
}
else {
headIndex++;
curPtr=nullptr;
continue;
}
}
else {
if ( patternMap->boot[headIndex] != nullptr ) {
return TRUE;
}
else {
headIndex++;
continue;
}
}
}
return FALSE;
}
DateTimeMatcher&
PatternMapIterator::next() {
while ( bootIndex < MAX_PATTERN_ENTRIES ) {
if ( nodePtr != nullptr ) {
if ( nodePtr->next != nullptr ) {
nodePtr = nodePtr->next.getAlias();
break;
}
else {
bootIndex++;
nodePtr=nullptr;
continue;
}
}
else {
if ( patternMap->boot[bootIndex] != nullptr ) {
nodePtr = patternMap->boot[bootIndex];
break;
}
else {
bootIndex++;
continue;
}
}
}
if (nodePtr!=nullptr) {
matcher->copyFrom(*nodePtr->skeleton);
}
else {
matcher->copyFrom();
}
return *matcher;
}
SkeletonFields::SkeletonFields() {
// Set initial values to zero
clear();
}
void SkeletonFields::clear() {
uprv_memset(chars, 0, sizeof(chars));
uprv_memset(lengths, 0, sizeof(lengths));
}
void SkeletonFields::copyFrom(const SkeletonFields& other) {
uprv_memcpy(chars, other.chars, sizeof(chars));
uprv_memcpy(lengths, other.lengths, sizeof(lengths));
}
void SkeletonFields::clearField(int32_t field) {
chars[field] = 0;
lengths[field] = 0;
}
UChar SkeletonFields::getFieldChar(int32_t field) const {
return chars[field];
}
int32_t SkeletonFields::getFieldLength(int32_t field) const {
return lengths[field];
}
void SkeletonFields::populate(int32_t field, const UnicodeString& value) {
populate(field, value.charAt(0), value.length());
}
void SkeletonFields::populate(int32_t field, UChar ch, int32_t length) {
chars[field] = (int8_t) ch;
lengths[field] = (int8_t) length;
}
UBool SkeletonFields::isFieldEmpty(int32_t field) const {
return lengths[field] == 0;
}
UnicodeString& SkeletonFields::appendTo(UnicodeString& string) const {
for (int32_t i = 0; i < UDATPG_FIELD_COUNT; ++i) {
appendFieldTo(i, string);
}
return string;
}
UnicodeString& SkeletonFields::appendFieldTo(int32_t field, UnicodeString& string) const {
UChar ch(chars[field]);
int32_t length = (int32_t) lengths[field];
for (int32_t i=0; i<length; i++) {
string += ch;
}
return string;
}
UChar SkeletonFields::getFirstChar() const {
for (int32_t i = 0; i < UDATPG_FIELD_COUNT; ++i) {
if (lengths[i] != 0) {
return chars[i];
}
}
return '\0';
}
PtnSkeleton::PtnSkeleton()
: addedDefaultDayPeriod(FALSE) {
}
PtnSkeleton::PtnSkeleton(const PtnSkeleton& other) {
copyFrom(other);
}
void PtnSkeleton::copyFrom(const PtnSkeleton& other) {
uprv_memcpy(type, other.type, sizeof(type));
original.copyFrom(other.original);
baseOriginal.copyFrom(other.baseOriginal);
addedDefaultDayPeriod = other.addedDefaultDayPeriod;
}
void PtnSkeleton::clear() {
uprv_memset(type, 0, sizeof(type));
original.clear();
baseOriginal.clear();
}
UBool
PtnSkeleton::equals(const PtnSkeleton& other) const {
return (original == other.original)
&& (baseOriginal == other.baseOriginal)
&& (uprv_memcmp(type, other.type, sizeof(type)) == 0);
}
UnicodeString
PtnSkeleton::getSkeleton() const {
UnicodeString result;
result = original.appendTo(result);
int32_t pos;
if (addedDefaultDayPeriod && (pos = result.indexOf(LOW_A)) >= 0) {
// for backward compatibility: if DateTimeMatcher.set added a single 'a' that
// was not in the provided skeleton, remove it here before returning skeleton.
result.remove(pos, 1);
}
return result;
}
UnicodeString
PtnSkeleton::getBaseSkeleton() const {
UnicodeString result;
result = baseOriginal.appendTo(result);
int32_t pos;
if (addedDefaultDayPeriod && (pos = result.indexOf(LOW_A)) >= 0) {
// for backward compatibility: if DateTimeMatcher.set added a single 'a' that
// was not in the provided skeleton, remove it here before returning skeleton.
result.remove(pos, 1);
}
return result;
}
UChar
PtnSkeleton::getFirstChar() const {
return baseOriginal.getFirstChar();
}
PtnSkeleton::~PtnSkeleton() {
}
PtnElem::PtnElem(const UnicodeString &basePat, const UnicodeString &pat) :
basePattern(basePat), skeleton(nullptr), pattern(pat), next(nullptr)
{
}
PtnElem::~PtnElem() {
}
DTSkeletonEnumeration::DTSkeletonEnumeration(PatternMap& patternMap, dtStrEnum type, UErrorCode& status) : fSkeletons(nullptr) {
PtnElem *curElem;
PtnSkeleton *curSkeleton;
UnicodeString s;
int32_t bootIndex;
pos=0;
fSkeletons.adoptInsteadAndCheckErrorCode(new UVector(status), status);
if (U_FAILURE(status)) {
return;
}
for (bootIndex=0; bootIndex<MAX_PATTERN_ENTRIES; ++bootIndex ) {
curElem = patternMap.boot[bootIndex];
while (curElem!=nullptr) {
switch(type) {
case DT_BASESKELETON:
s=curElem->basePattern;
break;
case DT_PATTERN:
s=curElem->pattern;
break;
case DT_SKELETON:
curSkeleton=curElem->skeleton.getAlias();
s=curSkeleton->getSkeleton();
break;
}
if ( !isCanonicalItem(s) ) {
LocalPointer<UnicodeString> newElem(new UnicodeString(s), status);
if (U_FAILURE(status)) {
return;
}
fSkeletons->addElementX(newElem.getAlias(), status);
if (U_FAILURE(status)) {
fSkeletons.adoptInstead(nullptr);
return;
}
newElem.orphan(); // fSkeletons vector now owns the UnicodeString.
}
curElem = curElem->next.getAlias();
}
}
if ((bootIndex==MAX_PATTERN_ENTRIES) && (curElem!=nullptr) ) {
status = U_BUFFER_OVERFLOW_ERROR;
}
}
const UnicodeString*
DTSkeletonEnumeration::snext(UErrorCode& status) {
if (U_SUCCESS(status) && fSkeletons.isValid() && pos < fSkeletons->size()) {
return (const UnicodeString*)fSkeletons->elementAt(pos++);
}
return nullptr;
}
void
DTSkeletonEnumeration::reset(UErrorCode& /*status*/) {
pos=0;
}
int32_t
DTSkeletonEnumeration::count(UErrorCode& /*status*/) const {
return (fSkeletons.isNull()) ? 0 : fSkeletons->size();
}
UBool
DTSkeletonEnumeration::isCanonicalItem(const UnicodeString& item) {
if ( item.length() != 1 ) {
return FALSE;
}
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
if (item.charAt(0)==Canonical_Items[i]) {
return TRUE;
}
}
return FALSE;
}
DTSkeletonEnumeration::~DTSkeletonEnumeration() {
UnicodeString *s;
if (fSkeletons.isValid()) {
for (int32_t i = 0; i < fSkeletons->size(); ++i) {
if ((s = (UnicodeString *)fSkeletons->elementAt(i)) != nullptr) {
delete s;
}
}
}
}
DTRedundantEnumeration::DTRedundantEnumeration() : pos(0), fPatterns(nullptr) {
}
void
DTRedundantEnumeration::add(const UnicodeString& pattern, UErrorCode& status) {
if (U_FAILURE(status)) { return; }
if (fPatterns.isNull()) {
fPatterns.adoptInsteadAndCheckErrorCode(new UVector(status), status);
if (U_FAILURE(status)) {
return;
}
}
LocalPointer<UnicodeString> newElem(new UnicodeString(pattern), status);
if (U_FAILURE(status)) {
return;
}
fPatterns->addElementX(newElem.getAlias(), status);
if (U_FAILURE(status)) {
fPatterns.adoptInstead(nullptr);
return;
}
newElem.orphan(); // fPatterns now owns the string.
}
const UnicodeString*
DTRedundantEnumeration::snext(UErrorCode& status) {
if (U_SUCCESS(status) && fPatterns.isValid() && pos < fPatterns->size()) {
return (const UnicodeString*)fPatterns->elementAt(pos++);
}
return nullptr;
}
void
DTRedundantEnumeration::reset(UErrorCode& /*status*/) {
pos=0;
}
int32_t
DTRedundantEnumeration::count(UErrorCode& /*status*/) const {
return (fPatterns.isNull()) ? 0 : fPatterns->size();
}
UBool
DTRedundantEnumeration::isCanonicalItem(const UnicodeString& item) const {
if ( item.length() != 1 ) {
return FALSE;
}
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
if (item.charAt(0)==Canonical_Items[i]) {
return TRUE;
}
}
return FALSE;
}
DTRedundantEnumeration::~DTRedundantEnumeration() {
UnicodeString *s;
if (fPatterns.isValid()) {
for (int32_t i = 0; i < fPatterns->size(); ++i) {
if ((s = (UnicodeString *)fPatterns->elementAt(i)) != nullptr) {
delete s;
}
}
}
}
U_NAMESPACE_END
#endif /* #if !UCONFIG_NO_FORMATTING */
//eof
|