File: CFLocale.c

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (2013 lines) | stat: -rw-r--r-- 92,794 bytes parent folder | download
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
/*      CFLocale.c
	Copyright (c) 2002-2019, Apple Inc. and the Swift project authors
 
	Portions Copyright (c) 2014-2019, Apple Inc. and the Swift project authors
	Licensed under Apache License v2.0 with Runtime Library Exception
	See http://swift.org/LICENSE.txt for license information
	See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
	Responsibility: David Smith
*/

// Note the header file is in the OpenSource set (stripped to almost nothing), but not the .c file

#include "CFLocale.h"
#include "CFLocale_Private.h"
#include "CFString.h"
#include "CFArray.h"
#include "CFDictionary.h"
#include "CFCalendar.h"
#include "CFNumber.h"
#include "CFInternal.h"
#include "CFRuntime_Internal.h"
#include "CFPreferences.h"
#include "CFBundle_Internal.h"
#include "CFLocaleInternal.h"
#include <stdatomic.h>
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
#include <_foundation_unicode/uloc.h>           // ICU locales
#include <_foundation_unicode/ulocdata.h>       // ICU locale data
#include <_foundation_unicode/ucal.h>
#include <_foundation_unicode/ucurr.h>          // ICU currency functions
#include <_foundation_unicode/uset.h>           // ICU Unicode sets
#include <_foundation_unicode/putil.h>          // ICU low-level utilities
#include <_foundation_unicode/umsg.h>           // ICU message formatting
#include <_foundation_unicode/ucol.h>
#include <_foundation_unicode/unumsys.h>        // ICU numbering systems
#include <_foundation_unicode/uvernum.h>
#if U_ICU_VERSION_MAJOR_NUM > 53 && __has_include(<_foundation_unicode/uameasureformat.h>)
#include <_foundation_unicode/uameasureformat.h>

extern int32_t
uameasfmt_getUnitsForUsage( const char*     locale,
                           const char*     category,
                           const char*     usage,
                           UAMeasureUnit*  units,
                           int32_t         unitsCapacity,
                           UErrorCode*     status );

#endif
#endif
#include "CFNumberFormatter.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#if TARGET_OS_IPHONE
#include <mach-o/dyld_priv.h>
#endif


CF_PRIVATE CFCalendarRef _CFCalendarCreateCoWWithIdentifier(CFStringRef identifier);

CONST_STRING_DECL(kCFLocaleCurrentLocaleDidChangeNotification, "kCFLocaleCurrentLocaleDidChangeNotification")

CF_PRIVATE void __CFLocalePrefsChanged(CFNotificationCenterRef, void *, CFStringRef, const void *, CFDictionaryRef);

static const char * const kCalendarKeyword = "calendar";
static const char * const kCollationKeyword = "collation";
#define kMaxICUNameSize 1024

typedef struct __CFLocale *CFMutableLocaleRef;

CONST_STRING_DECL(__kCFLocaleCollatorID, "locale:collator id")


enum {
    __kCFLocaleKeyTableCount = 22
};

struct key_table {
    CFStringRef const * key;
    bool (*get)(CFLocaleRef, bool user, CFTypeRef *, CFStringRef context);  // returns an immutable copy & reference
    bool (*set)(CFMutableLocaleRef, CFTypeRef, CFStringRef context);
    bool (*name)(const char *, const char *, CFStringRef *); 
    CFStringRef const * context;
};


// Must forward decl. these functions:
static bool __CFLocaleCopyLocaleID(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context);
static bool __CFLocaleSetNOP(CFMutableLocaleRef locale, CFTypeRef cf, CFStringRef context);
static bool __CFLocaleFullName(const char *locale, const char *value, CFStringRef *out);
static bool __CFLocaleCopyCodes(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context);
static bool __CFLocaleCountryName(const char *locale, const char *value, CFStringRef *out);
static bool __CFLocaleScriptName(const char *locale, const char *value, CFStringRef *out);
static bool __CFLocaleLanguageName(const char *locale, const char *value, CFStringRef *out);
static bool __CFLocaleCurrencyShortName(const char *locale, const char *value, CFStringRef *out);
static bool __CFLocaleCopyExemplarCharSet(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context);
static bool __CFLocaleVariantName(const char *locale, const char *value, CFStringRef *out);
static bool __CFLocaleNoName(const char *locale, const char *value, CFStringRef *out);
static bool __CFLocaleCopyCalendarID(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context);
static bool __CFLocaleCalendarName(const char *locale, const char *value, CFStringRef *out);
static bool __CFLocaleCollationName(const char *locale, const char *value, CFStringRef *out);
static bool __CFLocaleCopyUsesMetric(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context);
static bool __CFLocaleCopyCalendar(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context);
static bool __CFLocaleCopyCollationID(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context);
static bool __CFLocaleCopyMeasurementSystem(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context);
static bool __CFLocaleCopyTemperatureUnit(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context);
static bool __CFLocaleCopyNumberFormat(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context);
static bool __CFLocaleCopyNumberFormat2(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context);
static bool __CFLocaleCurrencyFullName(const char *locale, const char *value, CFStringRef *out);
static bool __CFLocaleCopyCollatorID(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context);
static bool __CFLocaleCopyDelimiter(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context);

static struct key_table const __CFLocaleKeyTable[__kCFLocaleKeyTableCount] = {
    {&kCFLocaleIdentifierKey, __CFLocaleCopyLocaleID, __CFLocaleSetNOP, __CFLocaleFullName, NULL},
    {&kCFLocaleLanguageCodeKey, __CFLocaleCopyCodes, __CFLocaleSetNOP, __CFLocaleLanguageName, &kCFLocaleLanguageCodeKey},
    {&kCFLocaleCountryCodeKey, __CFLocaleCopyCodes, __CFLocaleSetNOP, __CFLocaleCountryName, &kCFLocaleCountryCodeKey},
    {&kCFLocaleScriptCodeKey, __CFLocaleCopyCodes, __CFLocaleSetNOP, __CFLocaleScriptName, &kCFLocaleScriptCodeKey},
    {&kCFLocaleVariantCodeKey, __CFLocaleCopyCodes, __CFLocaleSetNOP, __CFLocaleVariantName, &kCFLocaleVariantCodeKey},
    {&kCFLocaleExemplarCharacterSetKey, __CFLocaleCopyExemplarCharSet, __CFLocaleSetNOP, __CFLocaleNoName, NULL},
    {&kCFLocaleCalendarIdentifierKey, __CFLocaleCopyCalendarID, __CFLocaleSetNOP, __CFLocaleCalendarName, NULL},
    {&kCFLocaleCalendarKey, __CFLocaleCopyCalendar, __CFLocaleSetNOP, __CFLocaleNoName, NULL},
    {&kCFLocaleCollationIdentifierKey, __CFLocaleCopyCollationID, __CFLocaleSetNOP, __CFLocaleCollationName, NULL},
    {&kCFLocaleUsesMetricSystemKey, __CFLocaleCopyUsesMetric, __CFLocaleSetNOP, __CFLocaleNoName, NULL},
    {&kCFLocaleMeasurementSystemKey, __CFLocaleCopyMeasurementSystem, __CFLocaleSetNOP, __CFLocaleNoName, NULL},
    {&kCFLocaleTemperatureUnitKey, __CFLocaleCopyTemperatureUnit, __CFLocaleSetNOP, __CFLocaleNoName, NULL},
    {&kCFLocaleDecimalSeparatorKey, __CFLocaleCopyNumberFormat, __CFLocaleSetNOP, __CFLocaleNoName, &kCFNumberFormatterDecimalSeparatorKey},
    {&kCFLocaleGroupingSeparatorKey, __CFLocaleCopyNumberFormat, __CFLocaleSetNOP, __CFLocaleNoName, &kCFNumberFormatterGroupingSeparatorKey},
    {&kCFLocaleCurrencySymbolKey, __CFLocaleCopyNumberFormat2, __CFLocaleSetNOP, __CFLocaleCurrencyShortName, &kCFNumberFormatterCurrencySymbolKey},
    {&kCFLocaleCurrencyCodeKey, __CFLocaleCopyNumberFormat2, __CFLocaleSetNOP, __CFLocaleCurrencyFullName, &kCFNumberFormatterCurrencyCodeKey},
    {&kCFLocaleCollatorIdentifierKey, __CFLocaleCopyCollatorID, __CFLocaleSetNOP, __CFLocaleNoName, NULL},
    {&__kCFLocaleCollatorID, __CFLocaleCopyCollatorID, __CFLocaleSetNOP, __CFLocaleNoName, NULL},
    {&kCFLocaleQuotationBeginDelimiterKey, __CFLocaleCopyDelimiter, __CFLocaleSetNOP, __CFLocaleNoName, &kCFLocaleQuotationBeginDelimiterKey},
    {&kCFLocaleQuotationEndDelimiterKey, __CFLocaleCopyDelimiter, __CFLocaleSetNOP, __CFLocaleNoName, &kCFLocaleQuotationEndDelimiterKey},
    {&kCFLocaleAlternateQuotationBeginDelimiterKey, __CFLocaleCopyDelimiter, __CFLocaleSetNOP, __CFLocaleNoName, &kCFLocaleAlternateQuotationBeginDelimiterKey},
    {&kCFLocaleAlternateQuotationEndDelimiterKey, __CFLocaleCopyDelimiter, __CFLocaleSetNOP, __CFLocaleNoName, &kCFLocaleAlternateQuotationEndDelimiterKey},
};


static CFLocaleRef __CFLocaleSystem = NULL;
static CFMutableDictionaryRef __CFLocaleCache = NULL;
static CFLock_t __CFLocaleGlobalLock = CFLockInit;

struct __CFLocale {
    CFRuntimeBase _base;
    CFStringRef _identifier;    // canonical identifier, never NULL
    _Atomic(CFMutableDictionaryRef) _cache;
    CFDictionaryRef _prefs;
    CFLock_t _lock;
    // True if this locale is **NOT** one of the "special" languages that
    // requires special handing during case mapping:
    // - "az": Azerbaijani
    // - "lt": Lithuanian
    // - "tr": Turkish
    // - "nl": Dutch
    // - "el": Greek
    // See `CFUniCharMapCaseTo`
    // See https://www.unicode.org/Public/UNIDATA/SpecialCasing.txt
    Boolean _doesNotRequireSpecialCaseHandling;
};
 
CF_PRIVATE Boolean __CFLocaleGetDoesNotRequireSpecialCaseHandling(struct __CFLocale *locale) {
    CF_OBJC_FUNCDISPATCHV(CFLocaleGetTypeID(), Boolean, (NSLocale *)locale, _doesNotRequireSpecialCaseHandling);
    return locale->_doesNotRequireSpecialCaseHandling;
}

CF_PRIVATE void __CFLocaleSetDoesNotRequireSpecialCaseHandling(struct __CFLocale *locale) {
    CF_OBJC_FUNCDISPATCHV(CFLocaleGetTypeID(), void, (NSLocale *)locale, _setDoesNotRequireSpecialCaseHandling);
    locale->_doesNotRequireSpecialCaseHandling = true;
}

/* Flag bits */
enum {      /* Bits 0-1 */
    __kCFLocaleOrdinary = 0,
    __kCFLocaleSystem = 1,
    __kCFLocaleUser = 2,
    __kCFLocaleCustom = 3
};

CF_INLINE CFIndex __CFLocaleGetType(CFLocaleRef locale) {
    return __CFRuntimeGetValue(locale, 1, 0);
}

CF_INLINE void __CFLocaleSetType(CFLocaleRef locale, CFIndex type) {
    __CFRuntimeSetValue(locale, 1, 0, (uint8_t)type);
}

CF_INLINE void __CFLocaleLockGlobal(void) {
    __CFLock(&__CFLocaleGlobalLock);
}

CF_INLINE void __CFLocaleUnlockGlobal(void) {
    __CFUnlock(&__CFLocaleGlobalLock);
}

CF_INLINE void __CFLocaleLock(CFLocaleRef locale) {
    __CFLock(&((struct __CFLocale *)locale)->_lock);
}

CF_INLINE void __CFLocaleUnlock(CFLocaleRef locale) {
    __CFUnlock(&((struct __CFLocale *)locale)->_lock);
}

CF_INLINE Boolean __CFLocaleCacheGetValueIfPresent(CFLocaleRef locale, CFLocaleKey key, CFTypeRef *value) {
    CFDictionaryRef cache = atomic_load_explicit(&locale->_cache, memory_order_acquire);
    if (!cache) {
        *value = NULL;
        return false;
    }
    
    return CFDictionaryGetValueIfPresent(cache, key, value);
}

CF_INLINE void __CFLocaleCacheSet_alreadyLocked(CFLocaleRef locale, CFLocaleKey key, CFTypeRef value) {
    CFMutableDictionaryRef cache = atomic_load_explicit(&locale->_cache, memory_order_acquire);
    if (!cache) {
        cache = CFDictionaryCreateMutable(CFGetAllocator(locale), 0, NULL, &kCFTypeDictionaryValueCallBacks);
        atomic_store_explicit(&((struct __CFLocale *)locale)->_cache, cache, memory_order_release);
    }
    
    CFDictionarySetValue(cache, key, value);
}

static Boolean __CFLocaleEqual(CFTypeRef cf1, CFTypeRef cf2) {
    CFLocaleRef locale1 = (CFLocaleRef)cf1;
    CFLocaleRef locale2 = (CFLocaleRef)cf2;
    // a user locale and a locale created with an ident are not the same even if their contents are
    if (__CFLocaleGetType(locale1) != __CFLocaleGetType(locale2)) return false;
    if (!CFEqual(locale1->_identifier, locale2->_identifier)) return false;
    if (__kCFLocaleUser == __CFLocaleGetType(locale1)) {
        return CFEqual(locale1->_prefs, locale2->_prefs);
    }
    return true;
}

static CFHashCode __CFLocaleHash(CFTypeRef cf) {
    CFLocaleRef locale = (CFLocaleRef)cf;
    return CFHash(locale->_identifier);
}

static CFStringRef __CFLocaleCopyDescription(CFTypeRef cf) {
    CFLocaleRef locale = (CFLocaleRef)cf;
    const char *type = NULL;
    switch (__CFLocaleGetType(locale)) {
    case __kCFLocaleOrdinary: type = "ordinary"; break;
    case __kCFLocaleSystem: type = "system"; break;
    case __kCFLocaleUser: type = "user"; break;
    case __kCFLocaleCustom: type = "custom"; break;
    }
    return CFStringCreateWithFormat(CFGetAllocator(locale), NULL, CFSTR("<CFLocale %p [%p]>{type = %s, identifier = '%@'}"), cf, CFGetAllocator(locale), type, locale->_identifier);
}

static void __CFLocaleDeallocate(CFTypeRef cf) {
    CFLocaleRef locale = (CFLocaleRef)cf;
    CFRelease(locale->_identifier);
    if (NULL != locale->_cache) CFRelease(locale->_cache);
    if (NULL != locale->_prefs) CFRelease(locale->_prefs);
}

const CFRuntimeClass __CFLocaleClass = {
    0,
    "CFLocale",
    NULL,   // init
    NULL,   // copy
    __CFLocaleDeallocate,
    __CFLocaleEqual,
    __CFLocaleHash,
    NULL,   // 
    __CFLocaleCopyDescription
};

CFTypeID CFLocaleGetTypeID(void) {
    return _kCFRuntimeIDCFLocale;
}

CFLocaleRef CFLocaleGetSystem(void) {
    CFLocaleRef locale;
    CFLocaleRef uselessLocale = NULL; //if we lose the race creating the global locale, we need to release the one we created, but we want to do it outside the lock.
    __CFLocaleLockGlobal();
    if (NULL == __CFLocaleSystem) {
	__CFLocaleUnlockGlobal();
	locale = CFLocaleCreate(kCFAllocatorSystemDefault, CFSTR(""));
	if (!locale) return NULL;
	__CFLocaleSetType(locale, __kCFLocaleSystem);
	__CFLocaleLockGlobal();
	if (NULL == __CFLocaleSystem) {
	    __CFLocaleSystem = locale;
	} else {
            uselessLocale = locale;
	}
    }
#if !DEPLOYMENT_RUNTIME_SWIFT
    // This line relies on the fact that outside of Swift, __CFLocaleSystem is immortal.
    locale = __CFLocaleSystem ? (CFLocaleRef)CFRetain(__CFLocaleSystem) : NULL;
#else
    locale = __CFLocaleSystem;
#endif
    __CFLocaleUnlockGlobal();
    if (uselessLocale) CFRelease(uselessLocale);
    return locale;
}

extern CFDictionaryRef __CFXPreferencesCopyCurrentApplicationStateWithDeadlockAvoidance(Boolean * /* _Nonnull */outWouldDeadlock);

static _Atomic(CFLocaleRef) _CFLocaleCurrent_ = NULL;

CF_INLINE CFLocaleRef _cachedCurrentLocale() {
    return atomic_load(&_CFLocaleCurrent_);
}

// Returns true if `newLocale` is made immortal.
static Boolean _setCachedCurrentLocaleAndMakeImmortal(CFLocaleRef newLocale) {
    Boolean success;
    if (newLocale) {
        CFLocaleRef cachedLocale = NULL;
        success = atomic_compare_exchange_strong(&_CFLocaleCurrent_, &cachedLocale, newLocale);
        if (success) {
#if !DEPLOYMENT_RUNTIME_SWIFT
            __CFRuntimeSetRC((CFTypeRef)newLocale, 0);
#else
            // Swift does not support immortal objects yet; add an unbalanced retain instead.
            CFRetain((CFTypeRef)newLocale);
#endif

        }
    } else {
        success = false;
        atomic_store(&_CFLocaleCurrent_, newLocale);
    }

    return success;
}


#if TARGET_OS_OSX && !DEPLOYMENT_RUNTIME_SWIFT
#define FALLBACK_LOCALE_NAME CFSTR("")
#elif TARGET_OS_IPHONE
#define FALLBACK_LOCALE_NAME CFSTR("en_US")
#elif TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD || DEPLOYMENT_RUNTIME_SWIFT
#define FALLBACK_LOCALE_NAME CFSTR("en_US")
#endif

static CFStringRef _CFLocaleCopyLocaleIdentifierByAddingLikelySubtags(CFStringRef localeID)
{
    if (!localeID) {
        return NULL;
    }
    CFStringRef result = NULL;

    char bufLocaleID[ULOC_FULLNAME_CAPACITY];
    const char *cLocaleID = CFStringGetCStringPtr(localeID, kCFStringEncodingUTF8);
    if (NULL == cLocaleID) {
        if (CFStringGetCString(localeID, bufLocaleID, ULOC_FULLNAME_CAPACITY, kCFStringEncodingUTF8)) {
            cLocaleID = bufLocaleID;
        }
    }
    UErrorCode icuStatus = U_ZERO_ERROR;
    char maximizedLocaleID[ULOC_FULLNAME_CAPACITY];
    int32_t bufSize = uloc_addLikelySubtags(cLocaleID, maximizedLocaleID, ULOC_FULLNAME_CAPACITY, &icuStatus);
    if ((bufSize != -1) && U_SUCCESS(icuStatus)) {
        result = CFStringCreateWithCString(NULL, maximizedLocaleID, kCFStringEncodingUTF8);
    }

    return result ? : CFRetain(localeID);
}

// For a given locale (e.g. `en_US`, `zh_CN`, etc.) copies the language identifier with an explicit script code (e.g. `en-Latn`, zh-Hans`, etc.)
static CFStringRef _CFLocaleCopyLanguageIdentifierWithScriptCodeForLocaleIdentifier(CFStringRef localeID)
{
    CFStringRef languageID = NULL;
    if (localeID) {
        CFStringRef maximizedLocaleID = _CFLocaleCopyLocaleIdentifierByAddingLikelySubtags(localeID);
        CFDictionaryRef components = CFLocaleCreateComponentsFromLocaleIdentifier(NULL, maximizedLocaleID);
        CFRelease(maximizedLocaleID);

        CFStringRef languageCode = CFDictionaryGetValue(components, kCFLocaleLanguageCode);
        CFStringRef scriptCode = CFDictionaryGetValue(components, kCFLocaleScriptCode);
        if (languageCode && scriptCode) {
            languageID = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@-%@"), languageCode, scriptCode);
        }
        CFRelease(components);
    }
    return languageID;
}

CFStringRef _CFLocaleCopyNumberingSystemForLocaleIdentifier(CFStringRef localeID)
{
    CFStringRef numberingSystemID = NULL;
    if (localeID) {
        CFDictionaryRef components = CFLocaleCreateComponentsFromLocaleIdentifier(NULL, localeID);
        if (components) {
            // If the locale has an explicitly defined numbering system, that’s our answer!
            numberingSystemID = CFDictionaryGetValue(components, CFSTR("numbers"));
            if (numberingSystemID) {
                CFRetain(numberingSystemID);
            }
            // Otherwise, query ICU for what the default numbering system is.
            else {
                CFMutableDictionaryRef mutableComponents = CFDictionaryCreateMutableCopy(NULL, 0, components);
                if (mutableComponents) {
                    CFDictionarySetValue(mutableComponents, CFSTR("numbers"), CFSTR("default"));
                    CFStringRef localeIDWithDefaultNumbers = CFLocaleCreateLocaleIdentifierFromComponents(NULL, mutableComponents);
                    if (localeIDWithDefaultNumbers) {
                        char bufLocaleIDWithDefaultNumbers[ULOC_FULLNAME_CAPACITY];
                        const char *cLocaleIDWithDefaultNumbers = CFStringGetCStringPtr(localeIDWithDefaultNumbers, kCFStringEncodingUTF8);
                        if (!cLocaleIDWithDefaultNumbers) {
                            if (CFStringGetCString(localeIDWithDefaultNumbers, bufLocaleIDWithDefaultNumbers, ULOC_FULLNAME_CAPACITY, kCFStringEncodingUTF8)) {
                                cLocaleIDWithDefaultNumbers = bufLocaleIDWithDefaultNumbers;
                            }
                        }
                        if (cLocaleIDWithDefaultNumbers) {
                            UErrorCode icuStatus = U_ZERO_ERROR;
                            UNumberingSystem *numberingSystem = unumsys_open(cLocaleIDWithDefaultNumbers, &icuStatus);
                            if (numberingSystem) {
                                const char *cNumberingSystemID = unumsys_getName(numberingSystem);
                                if (cNumberingSystemID) {
                                    numberingSystemID = CFStringCreateWithCString(NULL, cNumberingSystemID, kCFStringEncodingUTF8);
                                }
                                unumsys_close(numberingSystem);
                            }
                        }
                        CFRelease(localeIDWithDefaultNumbers);
                    }
                    CFRelease(mutableComponents);
                }
            }
            CFRelease(components);
        }
    }
    return numberingSystemID;
}

CFArrayRef _CFLocaleCopyValidNumberingSystemsForLocaleIdentifier(CFStringRef localeID)
{
    CFMutableArrayRef numberingSystemIDs = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
    if (localeID) {
        CFDictionaryRef components = CFLocaleCreateComponentsFromLocaleIdentifier(NULL, localeID);
        if (components) {
            // 1. If there is an explicitly defined override numbering system, add it first to the list.
            CFStringRef overrideNumberingSystemID = CFDictionaryGetValue(components, CFSTR("numbers"));
            if (overrideNumberingSystemID) {
                CFArrayAppendValue(numberingSystemIDs, overrideNumberingSystemID);
            }
            
            // 2. Query ICU for additional supported numbering systems
            CFStringRef queryList[4] = { CFSTR("default"), NULL, NULL, NULL };
            CFStringRef languageCode = CFDictionaryGetValue(components, kCFLocaleLanguageCode);
            // For Chinese & Thai, although there is a traditional numbering system, it is not one that users will expect to use as a numbering system in the system. (cf. <rdar://problem/19742123&20068835>)
            if (!(CFEqual(languageCode, CFSTR("th")) ||
                  CFEqual(languageCode, CFSTR("zh")) ||
                  CFEqual(languageCode, CFSTR("wuu")) ||
                  CFEqual(languageCode, CFSTR("yue")))) {
                queryList[1] = CFSTR("native");
                queryList[2] = CFSTR("traditional");
                queryList[3] = CFSTR("finance");
            }
            CFMutableDictionaryRef mutableComponents = CFDictionaryCreateMutableCopy(NULL, 0, components);
            if (mutableComponents) {
                for (CFIndex i = 0, count = sizeof(queryList)/sizeof(CFStringRef); i < count; i++) {
                    CFStringRef query = queryList[i];
                    if (query) {
                        CFDictionarySetValue(mutableComponents, CFSTR("numbers"), query);
                        CFStringRef localeIDWithNumbersQuery = CFLocaleCreateLocaleIdentifierFromComponents(NULL, mutableComponents);
                        if (localeIDWithNumbersQuery) {
                            char bufLocaleIDWithNumbersQuery[ULOC_FULLNAME_CAPACITY];
                            const char *cLocaleIDWithNumbersQuery = CFStringGetCStringPtr(localeIDWithNumbersQuery, kCFStringEncodingUTF8);
                            if (!cLocaleIDWithNumbersQuery) {
                                if (CFStringGetCString(localeIDWithNumbersQuery, bufLocaleIDWithNumbersQuery, ULOC_FULLNAME_CAPACITY, kCFStringEncodingUTF8)) {
                                    cLocaleIDWithNumbersQuery = bufLocaleIDWithNumbersQuery;
                                }
                            }
                            if (cLocaleIDWithNumbersQuery) {
                                UNumberingSystem *numberingSystem = NULL;
                                UErrorCode icuStatus = U_ZERO_ERROR;
                                if ((numberingSystem = unumsys_open(cLocaleIDWithNumbersQuery, &icuStatus)) != NULL) {
                                    // There are some really funky numbering systems out there, and we do not support ones that are algorithmic (like the traditional ones for Hebrew, etc.) and ones that are not base 10.
                                    if (!unumsys_isAlgorithmic(numberingSystem) && unumsys_getRadix(numberingSystem) == 10) {
                                        const char *cNumberingSystemID = unumsys_getName(numberingSystem);
                                        if (cNumberingSystemID) {
                                            CFStringRef numberingSystemID = CFStringCreateWithCString(NULL, cNumberingSystemID, kCFStringEncodingUTF8);
                                            if (numberingSystemID) {
                                                if (!CFArrayContainsValue(numberingSystemIDs, CFRangeMake(0, CFArrayGetCount(numberingSystemIDs)), numberingSystemID)) {
                                                    CFArrayAppendValue(numberingSystemIDs, numberingSystemID);
                                                }
                                                CFRelease(numberingSystemID);
                                            }
                                        }
                                    }
                                    unumsys_close(numberingSystem);
                                }
                            }
                            CFRelease(localeIDWithNumbersQuery);
                        }
                    }
                }
                CFRelease(mutableComponents);
            }
            
            // 3. Add `latn`, which we support that for all languages.
            if (!CFArrayContainsValue(numberingSystemIDs, CFRangeMake(0, CFArrayGetCount(numberingSystemIDs)), CFSTR("latn"))) {
                CFArrayAppendValue(numberingSystemIDs, CFSTR("latn"));
            }
            
            CFRelease(components);
        }
    }
    return numberingSystemIDs;
}

CFStringRef _CFLocaleCreateLocaleIdentiferByReplacingLanguageCodeAndScriptCode(CFStringRef localeIDWithDesiredLangCode, CFStringRef localeIDWithDesiredComponents) {
    CFStringRef localeID = NULL;
    if (localeIDWithDesiredLangCode && localeIDWithDesiredComponents) {
        CFStringRef langIDToUse = _CFLocaleCopyLanguageIdentifierWithScriptCodeForLocaleIdentifier(localeIDWithDesiredLangCode);
        if (langIDToUse) {
            CFStringRef maximizedLocaleID = _CFLocaleCopyLocaleIdentifierByAddingLikelySubtags(localeIDWithDesiredComponents);
            if (maximizedLocaleID) {
                CFDictionaryRef localeIDComponents = CFLocaleCreateComponentsFromLocaleIdentifier(NULL, maximizedLocaleID);
                CFRelease(maximizedLocaleID);
                if (localeIDComponents) {
                    CFMutableDictionaryRef mutableComps = CFDictionaryCreateMutableCopy(NULL, CFDictionaryGetCount(localeIDComponents), localeIDComponents);
                    CFRelease(localeIDComponents);
                    if (mutableComps) {
                        CFDictionaryRef languageIDComponents = CFLocaleCreateComponentsFromLocaleIdentifier(NULL, langIDToUse);
                        if (languageIDComponents) {
                            CFStringRef languageCode = CFDictionaryGetValue(languageIDComponents, kCFLocaleLanguageCode);
                            CFStringRef scriptCode = CFDictionaryGetValue(languageIDComponents, kCFLocaleScriptCode);
                            if (languageCode && scriptCode) {
                                // 1. Language & Script
                                // Note that both `languageCode` and `scriptCode` should be overridden in `mutableComps`, even for combinations like `en` + `latn`, because the previous language’s script may not be compatible with the new language. This will produce a “maximized” locale identifier, which we will canonicalize (below) to remove superfluous tags.
                                CFDictionarySetValue(mutableComps, kCFLocaleLanguageCode, languageCode);
                                CFDictionarySetValue(mutableComps, kCFLocaleScriptCode, scriptCode);
                                
                                // 2. Numbering System
                                CFStringRef numberingSystem = _CFLocaleCopyNumberingSystemForLocaleIdentifier(localeIDWithDesiredComponents);
                                if (numberingSystem) {
                                    CFArrayRef validNumberingSystems = _CFLocaleCopyValidNumberingSystemsForLocaleIdentifier(localeIDWithDesiredLangCode);
                                    if (validNumberingSystems) {
                                        CFIndex indexOfNumberingSystem = CFArrayGetFirstIndexOfValue(validNumberingSystems, CFRangeMake(0, CFArrayGetCount(validNumberingSystems)), numberingSystem);
                                        // If the numbering system for `localeIDWithDesiredComponents` is not compatible with the constructed locale’s language, then we should discard it, e.g. `ar_AE@numbers=arab` + `en` should get `en_AE`, not `en_AE@numbers=arab`, since `arab` is not valid for `en`.
                                        if (indexOfNumberingSystem == kCFNotFound || indexOfNumberingSystem == 0) {
                                            CFDictionaryRemoveValue(mutableComps, CFSTR("numbers"));
                                        }
                                        // If the numbering system for `localeIDWithDesiredComponents` is compatible with the constructed locale’s language and is not already the default numbering system (index 0), then set it on the new locale, e.g. `hi_IN@numbers=latn` + `ar` shoudl get `ar_IN@numbers=latn`, since `latn` is valid for `ar`.
                                        else if (indexOfNumberingSystem > 0) {
                                            CFDictionarySetValue(mutableComps, CFSTR("numbers"), numberingSystem);
                                        }
                                        CFRelease(validNumberingSystems);
                                    }
                                    CFRelease(numberingSystem);
                                }
                                
                                // 3. Construct & Canonicalize
                                // The locale constructed from the components will be over-specified for many cases, such as `en_Latn_US`. Before returning it, we should canonicalize it, which will remove any script code that is already implicit in the definition of the locale, yielding `en_US` instead.
                                CFStringRef maximizedLocaleID = CFLocaleCreateLocaleIdentifierFromComponents(NULL, mutableComps);
                                if (maximizedLocaleID) {
                                    localeID = CFLocaleCreateCanonicalLocaleIdentifierFromString(NULL, maximizedLocaleID);
                                    CFRelease(maximizedLocaleID);
                                }
                            }
                            CFRelease(languageIDComponents);
                        }
                        CFRelease(mutableComps);
                    }
                }
            }
            CFRelease(langIDToUse);
        }
    }
    return localeID;
}

static CFArrayRef _CFLocaleCopyPreferredLanguagesFromPrefs(CFArrayRef languagesArray);

/// Creates a new locale identifier by identifying the most preferred localization (using `availableLocalizations` and `preferredLanguages`) and then creating a locale based on the most preferred localization, while retaining any relevant attributes from `preferredLocaleID`, e.g. if `availableLocalizations` is `[ "en", "fr", "de" ]`, `preferredLanguages` is `[ "ar-AE", "en-AE" ]`, `preferredLocaleID` is `ar_AE@numbers=arab;calendar=islamic-civil`, it will return `en_AE@calendar=islamic-civil`, i.e. the language will be matched to `en` since that’s the only available localization that matches, `calendar` will be retained since it’s language-agnostic, but `numbers` will be discarded because the `arab` numbering system is not valid for `en`.
static CFStringRef _CFLocaleCreateLocaleIdentifierForAvailableLocalizations(CFArrayRef availableLocalizations, CFArrayRef preferredLanguages, CFStringRef preferredLocaleID, CFArrayRef *outCanonicalizedPreferredLanguages) {
    CFStringRef result = NULL;
    if (availableLocalizations && CFArrayGetCount(availableLocalizations) > 0 &&
        preferredLanguages && CFArrayGetCount(preferredLanguages) > 0 &&
        preferredLocaleID && CFStringGetLength(preferredLocaleID) > 0)
    {
        CFArrayRef canonicalizedPreferredLanguages = _CFLocaleCopyPreferredLanguagesFromPrefs(preferredLanguages);

        // Combine `availableLocalizations` with `preferredLanguages` to get `preferredLocalizations`, whose #0 object indicates the localization that the app is current launched in.
        CFArrayRef preferredLocalizations = NULL; {
            // Since `availableLocalizations` can contains legacy lproj names such as `English`, `French`, etc. we need to canonicalize these into language identifiers such as `en`, `fr`, etc. Otherwise the logic that later compares these to language identifiers will fail. (<rdar://problem/37141123>)
            CFArrayRef canonicalizedAvailableLocalizations = _CFLocaleCopyPreferredLanguagesFromPrefs(availableLocalizations);
            if (canonicalizedAvailableLocalizations) {
                preferredLocalizations = CFBundleCopyLocalizationsForPreferences(canonicalizedAvailableLocalizations, canonicalizedPreferredLanguages);
                CFRelease(canonicalizedAvailableLocalizations);
            }
        }
        
        if (preferredLocalizations && CFArrayGetCount(preferredLocalizations) > 0) {
            // If we didn't find an overlap, we go with the preferred locale of the bundle.
            CFStringRef preferredLocalization = CFArrayGetValueAtIndex(preferredLocalizations, 0);
            if (preferredLocalization) {
                // The goal here is to preserve all of the overrides present in the value stored in AppleLocale (e.g. "@calendar=buddhist")
                CFStringRef preferredLocaleLanguageID = _CFLocaleCopyLanguageIdentifierWithScriptCodeForLocaleIdentifier(preferredLocaleID);
                CFStringRef preferredLocalizationLanguageID = _CFLocaleCopyLanguageIdentifierWithScriptCodeForLocaleIdentifier(preferredLocalization);
                if (preferredLocaleLanguageID && preferredLocalizationLanguageID) {
                    if (CFEqual(preferredLocaleLanguageID, preferredLocalizationLanguageID)) {
                        result = CFRetain(preferredLocaleID);
                    } else {
                        result = _CFLocaleCreateLocaleIdentiferByReplacingLanguageCodeAndScriptCode(preferredLocalization, preferredLocaleID);
                    }
                }
                if (preferredLocaleLanguageID) { CFRelease(preferredLocaleLanguageID); }
                if (preferredLocalizationLanguageID) { CFRelease(preferredLocalizationLanguageID); }
            }
            
        }
        if (preferredLocalizations) { CFRelease(preferredLocalizations); }
        if (outCanonicalizedPreferredLanguages) {
            *outCanonicalizedPreferredLanguages = canonicalizedPreferredLanguages;
        } else if (canonicalizedPreferredLanguages) {
            CFRelease(canonicalizedPreferredLanguages);
        }
    }
    return result;
}

CFLocaleRef _CFLocaleCreateLikeCurrentWithBundleLocalizations(CFArrayRef availableLocalizations, Boolean allowsMixedLocalizations) {
    CFLocaleRef locale = NULL;
    
    if (allowsMixedLocalizations) {
        locale = _CFLocaleCopyPreferred();
    } else {
        CFArrayRef preferredLanguages = CFLocaleCopyPreferredLanguages();
        CFStringRef preferredLocaleID = CFPreferencesCopyAppValue(CFSTR("AppleLocale"), kCFPreferencesCurrentApplication);
        
        CFStringRef identifier = _CFLocaleCreateLocaleIdentifierForAvailableLocalizations(availableLocalizations, preferredLanguages, preferredLocaleID, NULL);
        if (identifier) {
            locale = CFLocaleCreate(kCFAllocatorSystemDefault, identifier);
        }
        
        if (identifier) {
            CFRelease(identifier);
        }
        if (preferredLocaleID) {
            CFRelease(preferredLocaleID);
        }
        if (preferredLanguages) {
            CFRelease(preferredLanguages);
        }
    }
    
    return locale;
}

static CFLocaleRef _CFLocaleCopyCurrentGuts(CFStringRef name, Boolean useCache, CFDictionaryRef overridePrefs, Boolean disableBundleMatching) {
    /*
     NOTE: calling any CFPreferences function, or any function which calls into a CFPreferences function, *except* for __CFXPreferencesCopyCurrentApplicationStateWithDeadlockAvoidance (and accepting backstop values if its outparam is false), will deadlock. This is because CFPreferences calls os_log_*, which calls -descriptionWithLocale:, which calls CFLocaleCopyCurrent.
     */
    
    CFStringRef ident = NULL;
    // We cannot be helpful here, because it causes performance problems,
    // even though the preference lookup is relatively quick, as there are
    // things which call this function thousands or millions of times in
    // a short period.
    if (!name) {
#if 0 // TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
        name = (CFStringRef)CFPreferencesCopyAppValue(CFSTR("AppleLocale"), kCFPreferencesCurrentApplication);
#endif
    } else {
        CFRetain(name);
    }
    if (name && (CFStringGetTypeID() == CFGetTypeID(name))) {
        ident = CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorSystemDefault, name);
        if (os_log_debug_enabled(_CFOSLog())) {
            CFDictionaryRef const components = CFLocaleCreateComponentsFromLocaleIdentifier(NULL, ident);
            if (components) {
                if (!CFDictionaryGetValue(components, kCFLocaleCountryCode)) {
                    os_log_debug(_CFOSLog(), "CFLocaleCopyCurrent() called with overriding locale identifier '%{public}@' which does not have a country code", ident);
                }
                
                CFRelease(components);
            }
        }
    }
    if (name) CFRelease(name);
    
    // If `disableBundleMatching` is true, caching needs to be turned off, only a single value is cached for the most common case of calling `CFLocaleCopyCurrent`.
    if (disableBundleMatching) {
        useCache = false;
    }
    
    if (useCache) {
        CFLocaleRef cached = _cachedCurrentLocale();
        if (cached && ident) {
            if (CFEqual(cached->_identifier, ident)) {
                // We can just return what's in the cache.
                CFRelease(ident);
                ident = NULL;
            } else {
                // We'll replace what's in the cache with ident below.
                _setCachedCurrentLocaleAndMakeImmortal(NULL);
                cached = NULL;
            }
        }

        if (cached) {
#if DEPLOYMENT_RUNTIME_SWIFT
            CFRetain(cached); // In Swift, this object isn't immortal and needs to be correctly memory-managed.
#endif
            
            return cached;
        }
    }
    
    CFDictionaryRef prefs = NULL;
    
    struct __CFLocale *locale;
    uint32_t size = sizeof(struct __CFLocale) - sizeof(CFRuntimeBase);
    locale = (struct __CFLocale *)_CFRuntimeCreateInstance(kCFAllocatorSystemDefault, CFLocaleGetTypeID(), size, NULL);
    if (NULL == locale) {
	if (prefs) CFRelease(prefs);
	if (ident) CFRelease(ident);
	return NULL;
    }

    __CFLocaleSetType(locale, __kCFLocaleUser);
    
    if (!ident) {
        ident = (CFStringRef)CFRetain(FALLBACK_LOCALE_NAME);

        // <rdar://problem/51409572> CFLocaleCopyCurrent() failed to look up current locale -- gpsd dameon is not localized, does not interact directly with users
        // This log was added to try to catch scenarios in which apps fail to look up the current locale thanks to sandboxing issues or CFPreferences issues. It turns out that in its current formulation, this log has a high false positive rate and is very confusing.
        // Disabled for now.
        /*
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            os_log_error(_CFOSLog(), "CFLocaleCopyCurrent() failed to look up current locale via 'AppleLocale' and 'AppleLanguages' in user preferences; falling back to locale identifier '%{public}@' as the default. Consider checking Console for sandbox violations from this process for reading from preferences, or enabling CoreFoundation debug logging for more information. This will only be logged once.", ident);
        });
        */
    }
    
    locale->_identifier = ident;
    locale->_prefs = prefs;
    locale->_lock = CFLockInit;
    locale->_doesNotRequireSpecialCaseHandling = false;
    
    if (useCache) {
        Boolean success = _setCachedCurrentLocaleAndMakeImmortal(locale);
        if (success) {
            // useCache is enabled, the locale is made immortal.
            // The clang analyzer doesn't know about __CFRuntimeSetRC, though, so it sees overwriting locale below as a leak.
            _CLANG_ANALYZER_IGNORE_RETAIN(locale);
        } else {
            // We already have a cached locale. Release the newly created one before overwriting it below.
            CFRelease(locale);
        }
        locale = (struct __CFLocale *)_cachedCurrentLocale();
    }
    return locale;
}

/*
 <rdar://problem/13834276> NSDateFormatter: Cannot specify force12HourTime/force24HourTime
 This returns an instance of CFLocale that's set up exactly like it would be if the user changed the current locale to that identifier, then called CFLocaleCopyCurrent()
 */
CFLocaleRef _CFLocaleCopyAsIfCurrent(CFStringRef name) {
    return _CFLocaleCopyCurrentGuts(name, false, NULL, false);
}

/*
 <rdar://problem/14032388> Need the ability to initialize a CFLocaleRef from a preferences dictionary
 This returns an instance of CFLocale that's set up exactly like it would be if the user changed the current locale to that identifier, set the preferences keys in the overrides dictionary, then called CFLocaleCopyCurrent()
 */
CFLocaleRef _CFLocaleCopyAsIfCurrentWithOverrides(CFStringRef name, CFDictionaryRef overrides) {
    return _CFLocaleCopyCurrentGuts(name, false, overrides, false);
}

CFLocaleRef _CFLocaleCopyPreferred(void) {
    return _CFLocaleCopyCurrentGuts(NULL, true, NULL, true);
}

CFLocaleRef CFLocaleCopyCurrent(void) {
    return _CFLocaleCopyCurrentGuts(NULL, true, NULL, false);
}

CF_PRIVATE CFDictionaryRef __CFLocaleGetPrefs(CFLocaleRef locale) {
    CF_OBJC_FUNCDISPATCHV(CFLocaleGetTypeID(), CFDictionaryRef, (NSLocale *)locale, _prefs);
    return locale->_prefs;
}

#if DEPLOYMENT_RUNTIME_SWIFT
Boolean _CFLocaleInit(CFLocaleRef locale, CFStringRef identifier) {
    CFStringRef localeIdentifier = NULL;
    if (identifier) {
        localeIdentifier = CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorSystemDefault, identifier);
    }
    if (NULL == localeIdentifier) return false;
    CFStringRef old = localeIdentifier;
    localeIdentifier = (CFStringRef)CFStringCreateCopy(kCFAllocatorSystemDefault, localeIdentifier);
    CFRelease(old);
    
    __CFLocaleSetType(locale, __kCFLocaleOrdinary);
    ((struct __CFLocale *)locale)->_identifier = localeIdentifier;
    ((struct __CFLocale *)locale)->_cache = NULL;
    ((struct __CFLocale *)locale)->_prefs = NULL;
    ((struct __CFLocale *)locale)->_lock = CFLockInit;
    
    return true;
}
#endif

CFLocaleRef CFLocaleCreate(CFAllocatorRef allocator, CFStringRef identifier) {
    if (allocator == NULL) allocator = __CFGetDefaultAllocator();
    __CFGenericValidateType(allocator, CFAllocatorGetTypeID());
    __CFGenericValidateType(identifier, CFStringGetTypeID());
    CFStringRef localeIdentifier = NULL;
    if (identifier) {
	localeIdentifier = CFLocaleCreateCanonicalLocaleIdentifierFromString(allocator, identifier);
    }
    if (NULL == localeIdentifier) return NULL;
    CFStringRef old = localeIdentifier;
    localeIdentifier = (CFStringRef)CFStringCreateCopy(allocator, localeIdentifier);
    CFRelease(old);
    // Look for cases where we can return a cached instance.
    // We only use cached objects if the allocator is the system
    // default allocator.
    if (!allocator) allocator = __CFGetDefaultAllocator();
    Boolean canCache = _CFAllocatorIsSystemDefault(allocator);
    static os_unfair_lock __CFLocaleCacheLock = OS_UNFAIR_LOCK_INIT;
    os_unfair_lock_lock_with_options(&__CFLocaleCacheLock, OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION);
    if (canCache && __CFLocaleCache) {
	CFLocaleRef locale = (CFLocaleRef)CFDictionaryGetValue(__CFLocaleCache, localeIdentifier);
	if (locale) {
	    CFRetain(locale);
            os_unfair_lock_unlock(&__CFLocaleCacheLock);
	    CFRelease(localeIdentifier);
	    return locale;
	}
    }
    struct __CFLocale *locale = NULL;
    uint32_t size = sizeof(struct __CFLocale) - sizeof(CFRuntimeBase);
    locale = (struct __CFLocale *)_CFRuntimeCreateInstance(allocator, CFLocaleGetTypeID(), size, NULL);
    if (NULL == locale) {
        if (localeIdentifier) { CFRelease(localeIdentifier); }
	return NULL;
    }
    __CFLocaleSetType(locale, __kCFLocaleOrdinary);
    locale->_identifier = localeIdentifier;
    locale->_prefs = NULL;
    locale->_lock = CFLockInit;
    if (canCache) {
	if (NULL == __CFLocaleCache) {
	    __CFLocaleCache = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
	}
        CFDictionarySetValue(__CFLocaleCache, localeIdentifier, locale);
    }
    os_unfair_lock_unlock(&__CFLocaleCacheLock);
    return (CFLocaleRef)locale;
}

//CFLocaleCreateCopy() always just retained. This caused problems because CFLocaleGetValue(locale, kCFLocaleCalendarKey) would create a calendar, then set its locale to self, leading to a retain cycle
static CFLocaleRef _CFLocaleCreateCopyGuts(CFAllocatorRef allocator, CFLocaleRef locale, CFStringRef calendarIdentifier) {
    CF_OBJC_FUNCDISPATCHV(CFLocaleGetTypeID(), CFLocaleRef, (NSLocale *)locale, copy);
    if (allocator == NULL) allocator = __CFGetDefaultAllocator();
    __CFGenericValidateType(allocator, CFAllocatorGetTypeID());
    CFStringRef localeIdentifier = CFLocaleGetIdentifier(locale);
    
    if (calendarIdentifier) {
        CFDictionaryRef components = CFLocaleCreateComponentsFromLocaleIdentifier(kCFAllocatorSystemDefault, localeIdentifier);
        CFMutableDictionaryRef mcomponents = CFDictionaryCreateMutableCopy(kCFAllocatorSystemDefault, 0, components);
        CFDictionarySetValue(mcomponents, kCFLocaleCalendarIdentifierKey, calendarIdentifier);
        localeIdentifier = CFLocaleCreateLocaleIdentifierFromComponents(kCFAllocatorSystemDefault, mcomponents);
        CFRelease(mcomponents);
        CFRelease(components);
    } else {
        localeIdentifier = CFStringCreateCopy(allocator, localeIdentifier);
    }

    struct __CFLocale *loc = NULL;
    uint32_t size = sizeof(struct __CFLocale) - sizeof(CFRuntimeBase);
    loc = (struct __CFLocale *)_CFRuntimeCreateInstance(allocator, CFLocaleGetTypeID(), size, NULL);
    if (NULL == loc) {
        if (localeIdentifier) { CFRelease(localeIdentifier); }
        return NULL;
    }
    __CFLocaleSetType(loc, __CFLocaleGetType(locale));
    loc->_identifier = localeIdentifier;
    CFDictionaryRef prefs = __CFLocaleGetPrefs(locale);
    loc->_prefs = prefs ? CFRetain(prefs) : NULL;
    loc->_lock = CFLockInit;
    loc->_doesNotRequireSpecialCaseHandling = locale->_doesNotRequireSpecialCaseHandling;
    return (CFLocaleRef)loc;
}

//CFLocaleCreateCopy() always just retained. This caused problems because CFLocaleGetValue(locale, kCFLocaleCalendarKey) would create a calendar, then set its locale to self, leading to a retain cycle
CFLocaleRef CFLocaleCreateCopy(CFAllocatorRef allocator, CFLocaleRef locale) {
    return _CFLocaleCreateCopyGuts(allocator, locale, NULL);
}

//For CFDateFormatter
CF_PRIVATE CFLocaleRef _CFLocaleCreateCopyWithNewCalendarIdentifier(CFAllocatorRef allocator, CFLocaleRef locale, CFStringRef calendarIdentifier) {
    return _CFLocaleCreateCopyGuts(allocator, locale, calendarIdentifier);
}

CFStringRef CFLocaleGetIdentifier(CFLocaleRef locale) {
    CF_OBJC_FUNCDISPATCHV(CFLocaleGetTypeID(), CFStringRef, (NSLocale *)locale, localeIdentifier);
    return locale->_identifier;
}

CFTypeRef CFLocaleGetValue(CFLocaleRef locale, CFStringRef key) {
#if TARGET_OS_OSX
    if (!_CFExecutableLinkedOnOrAfter(CFSystemVersionSnowLeopard)) {
	// Hack for Opera, which is using the hard-coded string value below instead of
        // the perfectly good public kCFLocaleCountryCode constant, for whatever reason.
	if (key && CFEqual(key, CFSTR("locale:country code"))) {
	    key = kCFLocaleCountryCodeKey;
	}
    }
#endif
    CF_OBJC_FUNCDISPATCHV(CFLocaleGetTypeID(), CFTypeRef, (NSLocale *)locale, objectForKey:(id)key);
    CFIndex idx, slot = -1;
    for (idx = 0; idx < __kCFLocaleKeyTableCount; idx++) {
	if (*__CFLocaleKeyTable[idx].key == key) {
	    slot = idx;
	    break;
	}
    }
    if (-1 == slot && NULL != key) {
	for (idx = 0; idx < __kCFLocaleKeyTableCount; idx++) {
	    if (CFEqual(*__CFLocaleKeyTable[idx].key, key)) {
		slot = idx;
		break;
	    }
	}
    }
    if (-1 == slot) {
	return NULL;
    }
    CFTypeRef value;
    __CFLocaleLock(locale);
    struct key_table const entry = __CFLocaleKeyTable[slot];
    if (__CFLocaleCacheGetValueIfPresent(locale, *entry.key, &value)) {
	__CFLocaleUnlock(locale);
	return value;
    }
    CFStringRef const context = entry.context ? *entry.context : NULL;
    if (__kCFLocaleUser == __CFLocaleGetType(locale) && entry.get(locale, true, &value, context)) {
	if (value) __CFLocaleCacheSet_alreadyLocked(locale, *entry.key, value);
	if (value) CFRelease(value);
	__CFLocaleUnlock(locale);
	return value;
    }
    if (entry.get(locale, false, &value, context)) {
	if (value) __CFLocaleCacheSet_alreadyLocked(locale, *entry.key, value);
	if (value) CFRelease(value);
	__CFLocaleUnlock(locale);
	return value;
    }
    __CFLocaleUnlock(locale);
    return NULL;
}

CFStringRef CFLocaleCopyDisplayNameForPropertyValue(CFLocaleRef displayLocale, CFStringRef key, CFStringRef value) {
    CF_OBJC_FUNCDISPATCHV(CFLocaleGetTypeID(), CFStringRef, (NSLocale *)displayLocale, _copyDisplayNameForKey:(id)key value:(id)value);
    CFIndex idx, slot = -1;
    for (idx = 0; idx < __kCFLocaleKeyTableCount; idx++) {
	if (*__CFLocaleKeyTable[idx].key == key) {
	    slot = idx;
	    break;
	}
    }
    if (-1 == slot && NULL != key) {
	for (idx = 0; idx < __kCFLocaleKeyTableCount; idx++) {
	    if (CFEqual(*__CFLocaleKeyTable[idx].key, key)) {
		slot = idx;
		break;
	    }
	}
    }
    if (-1 == slot || !value) {
	return NULL;
    }
    // Get the locale ID as a C string
    char localeID[ULOC_FULLNAME_CAPACITY+ULOC_KEYWORD_AND_VALUES_CAPACITY];
    char cValue[ULOC_FULLNAME_CAPACITY+ULOC_KEYWORD_AND_VALUES_CAPACITY];
    if (CFStringGetCString(displayLocale->_identifier, localeID, sizeof(localeID)/sizeof(localeID[0]), kCFStringEncodingASCII) && CFStringGetCString(value, cValue, sizeof(cValue)/sizeof(char), kCFStringEncodingASCII)) {
        CFStringRef result;
        if (__CFLocaleKeyTable[slot].name(localeID, cValue, &result)) {
            return result;
        }

        // We could not find a result using the requested language. Fall back through all preferred languages.
        CFArrayRef langPref = NULL;
	if (displayLocale->_prefs) {
	    langPref = (CFArrayRef)CFDictionaryGetValue(displayLocale->_prefs, CFSTR("AppleLanguages"));
	    if (langPref) CFRetain(langPref);
	} else {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
	    langPref = (CFArrayRef)CFPreferencesCopyAppValue(CFSTR("AppleLanguages"), kCFPreferencesCurrentApplication);
#endif
	}
        if (langPref != NULL) {
            CFIndex count = CFArrayGetCount(langPref);
            CFIndex i;
            bool success = false;
            for (i = 0; i < count && !success; ++i) {
                CFStringRef language = (CFStringRef)CFArrayGetValueAtIndex(langPref, i);
                CFStringRef cleanLanguage = CFLocaleCreateCanonicalLanguageIdentifierFromString(kCFAllocatorSystemDefault, language);
                if (CFStringGetCString(cleanLanguage, localeID, sizeof(localeID)/sizeof(localeID[0]), kCFStringEncodingASCII)) {
                    success = __CFLocaleKeyTable[slot].name(localeID, cValue, &result);
		}
                CFRelease(cleanLanguage);
            }
	    CFRelease(langPref);
            if (success)
                return result;
        }
    }
    return NULL;
}

CFArrayRef CFLocaleCopyAvailableLocaleIdentifiers(void) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    int32_t locale, localeCount = uloc_countAvailable();
    CFMutableSetRef working = CFSetCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeSetCallBacks);
    for (locale = 0; locale < localeCount; ++locale) {
        const char *localeID = uloc_getAvailable(locale);
        CFStringRef string1 = CFStringCreateWithCString(kCFAllocatorSystemDefault, localeID, kCFStringEncodingASCII);
	// do not include canonicalized version as IntlFormats cannot cope with that in its popup
	CFSetAddValue(working, string1);
        CFRelease(string1);
    }
    CFIndex cnt = CFSetGetCount(working);
    STACK_BUFFER_DECL(const void *, buffer, cnt);
    CFSetGetValues(working, buffer);
    CFArrayRef result = CFArrayCreate(kCFAllocatorSystemDefault, buffer, cnt, &kCFTypeArrayCallBacks);
    CFRelease(working);
    return result;
#else
    return CFArrayCreate(kCFAllocatorSystemDefault, NULL, 0, &kCFTypeArrayCallBacks);
#endif
}

#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
static CFArrayRef __CFLocaleCopyCStringsAsArray(const char* const* p) {
    CFMutableArrayRef working = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks);
    for (; *p; ++p) {
        CFStringRef string = CFStringCreateWithCString(kCFAllocatorSystemDefault, *p, kCFStringEncodingASCII);
        CFArrayAppendValue(working, string);
        CFRelease(string);
    }
    CFArrayRef result = CFArrayCreateCopy(kCFAllocatorSystemDefault, working);
    CFRelease(working);
    return result;
}

static CFArrayRef __CFLocaleCopyUEnumerationAsArray(UEnumeration *enumer, UErrorCode *icuErr) {
    const UChar *next = NULL;
    int32_t len = 0;
    CFMutableArrayRef working = NULL;
    if (U_SUCCESS(*icuErr)) {
        working = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks);
    }
    while ((next = uenum_unext(enumer, &len, icuErr)) && U_SUCCESS(*icuErr)) {
        CFStringRef string = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (const UniChar *)next, (CFIndex) len);
        CFArrayAppendValue(working, string);
        CFRelease(string);
    }
    if (*icuErr == U_INDEX_OUTOFBOUNDS_ERROR) {
        *icuErr = U_ZERO_ERROR;      // Temp: Work around bug (ICU 5220) in ucurr enumerator
    }
    CFArrayRef result = NULL;
    if (U_SUCCESS(*icuErr)) {
        result = CFArrayCreateCopy(kCFAllocatorSystemDefault, working);
    }
    if (working != NULL) {
        CFRelease(working);
    }
    return result;
}
#endif

CFArrayRef CFLocaleCopyISOLanguageCodes(void) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    const char* const* p = uloc_getISOLanguages();
    return __CFLocaleCopyCStringsAsArray(p);
#else
    return CFArrayCreate(kCFAllocatorSystemDefault, NULL, 0, &kCFTypeArrayCallBacks);
#endif
}

CFArrayRef CFLocaleCopyISOCountryCodes(void) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    const char* const* p = uloc_getISOCountries();
    return __CFLocaleCopyCStringsAsArray(p);
#else
    return CFArrayCreate(kCFAllocatorSystemDefault, NULL, 0, &kCFTypeArrayCallBacks);
#endif
}

CFArrayRef CFLocaleCopyISOCurrencyCodes(void) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    UErrorCode icuStatus = U_ZERO_ERROR;
    UEnumeration *enumer = ucurr_openISOCurrencies(UCURR_ALL, &icuStatus);
    CFArrayRef result = __CFLocaleCopyUEnumerationAsArray(enumer, &icuStatus);
    uenum_close(enumer);
#else
    CFArrayRef result = CFArrayCreate(kCFAllocatorSystemDefault, NULL, 0, &kCFTypeArrayCallBacks);
#endif
    return result;
}

CFArrayRef CFLocaleCopyCommonISOCurrencyCodes(void) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    UErrorCode icuStatus = U_ZERO_ERROR;
    UEnumeration *enumer = ucurr_openISOCurrencies(UCURR_COMMON|UCURR_NON_DEPRECATED, &icuStatus);
    CFArrayRef result = __CFLocaleCopyUEnumerationAsArray(enumer, &icuStatus);
    uenum_close(enumer);
#else
    CFArrayRef result = CFArrayCreate(kCFAllocatorSystemDefault, NULL, 0, &kCFTypeArrayCallBacks);
#endif
    return result;
}

CFStringRef CFLocaleCreateLocaleIdentifierFromWindowsLocaleCode(CFAllocatorRef allocator, uint32_t lcid) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    char buffer[kMaxICUNameSize];
    UErrorCode status = U_ZERO_ERROR;
    int32_t ret = uloc_getLocaleForLCID(lcid, buffer, kMaxICUNameSize, &status);
    if (U_FAILURE(status) || kMaxICUNameSize <= ret) return NULL;
    CFStringRef str = CFStringCreateWithCString(kCFAllocatorSystemDefault, buffer, kCFStringEncodingASCII);
    CFStringRef ident = CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorSystemDefault, str);
    CFRelease(str);
    return ident;
#else
    return CFSTR("");
#endif
}

uint32_t CFLocaleGetWindowsLocaleCodeFromLocaleIdentifier(CFStringRef localeIdentifier) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    CFStringRef ident = CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorSystemDefault, localeIdentifier);
    char localeID[ULOC_FULLNAME_CAPACITY+ULOC_KEYWORD_AND_VALUES_CAPACITY];
    Boolean b = ident ? CFStringGetCString(ident, localeID, sizeof(localeID)/sizeof(char), kCFStringEncodingASCII) : false;
    if (ident) CFRelease(ident);
    return b ? uloc_getLCID(localeID) : 0;
#else
    return 0;
#endif
}

CFLocaleLanguageDirection CFLocaleGetLanguageCharacterDirection(CFStringRef isoLangCode) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    char localeID[ULOC_FULLNAME_CAPACITY+ULOC_KEYWORD_AND_VALUES_CAPACITY];
    Boolean b = isoLangCode ? CFStringGetCString(isoLangCode, localeID, sizeof(localeID)/sizeof(char), kCFStringEncodingASCII) : false;
    CFLocaleLanguageDirection dir;
    UErrorCode status = U_ZERO_ERROR;
    ULayoutType idir = b ? uloc_getCharacterOrientation(localeID, &status) : ULOC_LAYOUT_UNKNOWN;
    switch (idir) {
    case ULOC_LAYOUT_LTR: dir = kCFLocaleLanguageDirectionLeftToRight; break;
    case ULOC_LAYOUT_RTL: dir = kCFLocaleLanguageDirectionRightToLeft; break;
    case ULOC_LAYOUT_TTB: dir = kCFLocaleLanguageDirectionTopToBottom; break;
    case ULOC_LAYOUT_BTT: dir = kCFLocaleLanguageDirectionBottomToTop; break;
    default: dir = kCFLocaleLanguageDirectionUnknown; break;
    }
    return dir;
#else
    return kCFLocaleLanguageDirectionLeftToRight;
#endif
}

CFLocaleLanguageDirection CFLocaleGetLanguageLineDirection(CFStringRef isoLangCode) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    char localeID[ULOC_FULLNAME_CAPACITY+ULOC_KEYWORD_AND_VALUES_CAPACITY];
    Boolean b = isoLangCode ? CFStringGetCString(isoLangCode, localeID, sizeof(localeID)/sizeof(char), kCFStringEncodingASCII) : false;
    CFLocaleLanguageDirection dir;
    UErrorCode status = U_ZERO_ERROR;
    ULayoutType idir = b ? uloc_getLineOrientation(localeID, &status) : ULOC_LAYOUT_UNKNOWN;
    switch (idir) {
    case ULOC_LAYOUT_LTR: dir = kCFLocaleLanguageDirectionLeftToRight; break;
    case ULOC_LAYOUT_RTL: dir = kCFLocaleLanguageDirectionRightToLeft; break;
    case ULOC_LAYOUT_TTB: dir = kCFLocaleLanguageDirectionTopToBottom; break;
    case ULOC_LAYOUT_BTT: dir = kCFLocaleLanguageDirectionBottomToTop; break;
    default: dir = kCFLocaleLanguageDirectionUnknown; break;
    }
    return dir;
#else
    return kCFLocaleLanguageDirectionLeftToRight;
#endif
}

_CFLocaleCalendarDirection _CFLocaleGetCalendarDirection(void) {
#if TARGET_OS_MAC
    _CFLocaleCalendarDirection calendarDirection = _kCFLocaleCalendarDirectionLeftToRight;
    Boolean keyExistsAndHasValidFormat = false;
    Boolean calendarIsRightToLeft = CFPreferencesGetAppBooleanValue(CFSTR("NSLocaleCalendarDirectionIsRightToLeft"), kCFPreferencesAnyApplication, &keyExistsAndHasValidFormat);
    if (keyExistsAndHasValidFormat) {
        calendarDirection = calendarIsRightToLeft ? _kCFLocaleCalendarDirectionRightToLeft : _kCFLocaleCalendarDirectionLeftToRight;
    } else {
        // If there was no default set, return the directionality of the effective language,
        // except for Hebrew, where the default should be LTR
        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFArrayRef bundleLocalizations = CFBundleCopyBundleLocalizations(mainBundle);

        if (NULL != bundleLocalizations) {
            CFArrayRef effectiveLocalizations = CFBundleCopyPreferredLocalizationsFromArray(bundleLocalizations);
            CFStringRef effectiveLocale = CFArrayGetValueAtIndex(effectiveLocalizations, 0);
            CFDictionaryRef effectiveLocaleComponents = CFLocaleCreateComponentsFromLocaleIdentifier(kCFAllocatorDefault, effectiveLocale);
            CFStringRef effectiveLanguage = CFDictionaryGetValue(effectiveLocaleComponents, kCFLocaleLanguageCodeKey);
            if (NULL != effectiveLanguage) {
                CFLocaleLanguageDirection effectiveLanguageDirection = CFLocaleGetLanguageCharacterDirection(effectiveLanguage);
                calendarDirection = (effectiveLanguageDirection == kCFLocaleLanguageDirectionRightToLeft) ? _kCFLocaleCalendarDirectionRightToLeft : _kCFLocaleCalendarDirectionLeftToRight;
            }
            CFRelease(effectiveLocaleComponents);
            CFRelease(effectiveLocalizations);
            CFRelease(bundleLocalizations);
        }
    }
    return calendarDirection;
#else
    return _kCFLocaleCalendarDirectionLeftToRight;
#endif
}

static CFArrayRef _CFLocaleCopyPreferredLanguagesFromPrefs(CFArrayRef languagesArray) {
    CFMutableArrayRef newArray = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks);
    if (languagesArray && (CFArrayGetTypeID() == CFGetTypeID(languagesArray))) {
        for (CFIndex idx = 0, cnt = CFArrayGetCount(languagesArray); idx < cnt; idx++) {
            CFStringRef str = (CFStringRef)CFArrayGetValueAtIndex(languagesArray, idx);
            if (str && (CFStringGetTypeID() == CFGetTypeID(str))) {
                CFStringRef ident = CFLocaleCreateCanonicalLanguageIdentifierFromString(kCFAllocatorSystemDefault, str);
                if (ident) {
                    CFArrayAppendValue(newArray, ident);
                    CFRelease(ident);
                }
            }
        }
    }
    return newArray;
}

static CFArrayRef __CFLocaleCopyPreferredLanguagesForCurrentUser(Boolean forCurrentUser) {
    CFArrayRef languagesArray = NULL;
    if (forCurrentUser) {
        languagesArray = (CFArrayRef)CFPreferencesCopyValue(CFSTR("AppleLanguages"), kCFPreferencesAnyApplication, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
    } else {
        languagesArray = (CFArrayRef)CFPreferencesCopyAppValue(CFSTR("AppleLanguages"), kCFPreferencesCurrentApplication);
    }
    CFArrayRef result = _CFLocaleCopyPreferredLanguagesFromPrefs(languagesArray);
    if (languagesArray) CFRelease(languagesArray);
    return result;
}

CFArrayRef CFLocaleCopyPreferredLanguages(void) {
    return __CFLocaleCopyPreferredLanguagesForCurrentUser(false);
}

CFArrayRef _CFLocaleCopyPreferredLanguagesForCurrentUser(void) {
    return __CFLocaleCopyPreferredLanguagesForCurrentUser(true);
}

// -------- -------- -------- -------- -------- --------

// These functions return true or false depending on the success or failure of the function.
// In the Copy case, this is failure to fill the *cf out parameter, and that out parameter is
// returned by reference WITH a retain on it.
static bool __CFLocaleSetNOP(CFMutableLocaleRef locale, CFTypeRef cf, CFStringRef context) {
    return false;
}

static bool __CFLocaleCopyLocaleID(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context) {
    *cf = CFRetain(locale->_identifier);
    return true;
}


static bool __CFLocaleCopyCodes(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context) {
    CFStringRef const kCFLocaleCodesKey = CFSTR("__kCFLocaleCodes");
    
    bool codesWasAllocated = false;
    CFDictionaryRef codes = NULL;
    if (!__CFLocaleCacheGetValueIfPresent(locale, kCFLocaleCodesKey, (const void **)&codes)) {
        codes = CFLocaleCreateComponentsFromLocaleIdentifier(kCFAllocatorSystemDefault, locale->_identifier);
        codesWasAllocated = (codes != NULL);
        
        // This function is only called from a __CFLocaleKeyTable[i].get(...) access, which only happens under a lock.
        // We explicitly assign `NULL` into the cache here to prevent trying to call `CFLocaleCreateComponentsFromLocaleIdentifier` on every access of this — `locale->_identifier` is immutable, so the results would never change.
        __CFLocaleCacheSet_alreadyLocked(locale, kCFLocaleCodesKey, codes);
    }
    
    CFStringRef value = codes ? (CFStringRef)CFDictionaryGetValue(codes, context) : NULL; // context is one of kCFLocale*Code constants
    if (codesWasAllocated) {
        CFRelease(codes);
    }
    
    if (value) {
        *cf = CFRetain(value);
        return true;
    } else {
        return false;
    }
}

#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
CFCharacterSetRef _CFCreateCharacterSetFromUSet(USet *set) {
    UErrorCode icuErr = U_ZERO_ERROR;
    CFMutableCharacterSetRef working = CFCharacterSetCreateMutable(NULL);
    UChar   buffer[2048];   // Suitable for most small sets
    int32_t stringLen;

    if (working == NULL)
        return NULL;

    int32_t itemCount = uset_getItemCount(set);
    int32_t i;
    for (i = 0; i < itemCount; ++i)
    {
        UChar32   start, end;
        UChar * string;

        string = buffer;
        stringLen = uset_getItem(set, i, &start, &end, buffer, sizeof(buffer)/sizeof(UChar), &icuErr);
        if (icuErr == U_BUFFER_OVERFLOW_ERROR)
        {
            string = (UChar *) malloc(sizeof(UChar)*(stringLen+1));
            if (!string)
            {
                CFRelease(working);
                return NULL;
            }
            icuErr = U_ZERO_ERROR;
            (void) uset_getItem(set, i, &start, &end, string, stringLen+1, &icuErr);
        }
        if (U_FAILURE(icuErr))
        {
            if (string != buffer)
                free(string);
            CFRelease(working);
            return NULL;
        }
        if (stringLen <= 0)
            CFCharacterSetAddCharactersInRange(working, CFRangeMake(start, end-start+1));
        else
        {
            CFStringRef cfString = CFStringCreateWithCharactersNoCopy(kCFAllocatorSystemDefault, (UniChar *)string, stringLen, kCFAllocatorNull);
            CFCharacterSetAddCharactersInString(working, cfString);
            CFRelease(cfString);
        }
        if (string != buffer)
            free(string);
    }
    
    CFCharacterSetRef   result = CFCharacterSetCreateCopy(kCFAllocatorSystemDefault, working);
    CFRelease(working);
    return result;
}
#endif

static bool __CFLocaleCopyExemplarCharSet(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    char localeID[ULOC_FULLNAME_CAPACITY+ULOC_KEYWORD_AND_VALUES_CAPACITY];
    if (CFStringGetCString(locale->_identifier, localeID, sizeof(localeID)/sizeof(char), kCFStringEncodingASCII)) {
        UErrorCode icuStatus = U_ZERO_ERROR;
	ULocaleData* uld = ulocdata_open(localeID, &icuStatus);
        USet *set = ulocdata_getExemplarSet(uld, NULL, USET_ADD_CASE_MAPPINGS, ULOCDATA_ES_STANDARD, &icuStatus);
	ulocdata_close(uld);
        if (U_FAILURE(icuStatus))
            return false;
        if (icuStatus == U_USING_DEFAULT_WARNING)   // If default locale used, force to empty set
            uset_clear(set);
        *cf = (CFTypeRef) _CFCreateCharacterSetFromUSet(set);
        uset_close(set);
        return (*cf != NULL);
    }
#endif
    return false;
}

static bool __CFLocaleCopyICUKeyword(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context, const char *keyword)
{
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    char localeID[ULOC_FULLNAME_CAPACITY+ULOC_KEYWORD_AND_VALUES_CAPACITY];
    if (CFStringGetCString(locale->_identifier, localeID, sizeof(localeID)/sizeof(char), kCFStringEncodingASCII))
    {
        char value[ULOC_KEYWORD_AND_VALUES_CAPACITY];
        UErrorCode icuStatus = U_ZERO_ERROR;
        if (uloc_getKeywordValue(localeID, keyword, value, sizeof(value)/sizeof(char), &icuStatus) > 0 && U_SUCCESS(icuStatus))
        {
            *cf = (CFTypeRef) CFStringCreateWithCString(kCFAllocatorSystemDefault, value, kCFStringEncodingASCII);
            return true;
        }
    }
#endif
    *cf = NULL;
    return false;
}

static bool __CFLocaleCopyICUCalendarID(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context, const char *keyword) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    char localeID[ULOC_FULLNAME_CAPACITY+ULOC_KEYWORD_AND_VALUES_CAPACITY];
    if (CFStringGetCString(locale->_identifier, localeID, sizeof(localeID)/sizeof(char), kCFStringEncodingASCII)) {
        UErrorCode icuStatus = U_ZERO_ERROR;
	UEnumeration *en = ucal_getKeywordValuesForLocale(keyword, localeID, TRUE, &icuStatus);
	int32_t len;
	const char *value = uenum_next(en, &len, &icuStatus);
	if (U_SUCCESS(icuStatus)) {
            *cf = (CFTypeRef) CFStringCreateWithCString(kCFAllocatorSystemDefault, value, kCFStringEncodingASCII);
	    uenum_close(en);
            return true;
        }
	uenum_close(en);
    }
#endif
    *cf = NULL;
    return false;
}

static bool __CFLocaleCopyCalendarID(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context) {
    bool succeeded = __CFLocaleCopyICUKeyword(locale, user, cf, context, kCalendarKeyword);
    if (!succeeded) {
	succeeded = __CFLocaleCopyICUCalendarID(locale, user, cf, context, kCalendarKeyword);
    }
    if (succeeded) {
	if (CFEqual(*cf, kCFCalendarIdentifierGregorian)) {
	    CFRelease(*cf);
	    *cf = CFRetain(kCFCalendarIdentifierGregorian);
	} else if (CFEqual(*cf, kCFCalendarIdentifierBuddhist)) {
	    CFRelease(*cf);
	    *cf = CFRetain(kCFCalendarIdentifierBuddhist);
	} else if (CFEqual(*cf, kCFCalendarIdentifierJapanese)) {
	    CFRelease(*cf);
	    *cf = CFRetain(kCFCalendarIdentifierJapanese);
	} else if (CFEqual(*cf, kCFCalendarIdentifierIslamic)) {
	    CFRelease(*cf);
	    *cf = CFRetain(kCFCalendarIdentifierIslamic);
	} else if (CFEqual(*cf, kCFCalendarIdentifierIslamicCivil)) {
	    CFRelease(*cf);
	    *cf = CFRetain(kCFCalendarIdentifierIslamicCivil);
	} else if (CFEqual(*cf, kCFCalendarIdentifierHebrew)) {
	    CFRelease(*cf);
	    *cf = CFRetain(kCFCalendarIdentifierHebrew);
	} else if (CFEqual(*cf, kCFCalendarIdentifierChinese)) {
	    CFRelease(*cf);
	    *cf = CFRetain(kCFCalendarIdentifierChinese);
	} else if (CFEqual(*cf, kCFCalendarIdentifierRepublicOfChina)) {
	    CFRelease(*cf);
	    *cf = CFRetain(kCFCalendarIdentifierRepublicOfChina);
	} else if (CFEqual(*cf, kCFCalendarIdentifierPersian)) {
	    CFRelease(*cf);
	    *cf = CFRetain(kCFCalendarIdentifierPersian);
	} else if (CFEqual(*cf, kCFCalendarIdentifierIndian)) {
	    CFRelease(*cf);
	    *cf = CFRetain(kCFCalendarIdentifierIndian);
	} else if (CFEqual(*cf, kCFCalendarIdentifierISO8601)) {
	    CFRelease(*cf);
	    *cf = CFRetain(kCFCalendarIdentifierISO8601);
	} else if (CFEqual(*cf, kCFCalendarIdentifierCoptic)) {
	    CFRelease(*cf);
	    *cf = CFRetain(kCFCalendarIdentifierCoptic);
	} else if (CFEqual(*cf, kCFCalendarIdentifierEthiopicAmeteMihret)) {
	    CFRelease(*cf);
	    *cf = CFRetain(kCFCalendarIdentifierEthiopicAmeteMihret);
	} else if (CFEqual(*cf, kCFCalendarIdentifierEthiopicAmeteAlem)) {
	    CFRelease(*cf);
	    *cf = CFRetain(kCFCalendarIdentifierEthiopicAmeteAlem);
        } else if (CFEqual(*cf, kCFCalendarIdentifierIslamicTabular)) {
            CFRelease(*cf);
            *cf = CFRetain(kCFCalendarIdentifierIslamicTabular);
        } else if (CFEqual(*cf, kCFCalendarIdentifierIslamicUmmAlQura)) {
            CFRelease(*cf);
            *cf = CFRetain(kCFCalendarIdentifierIslamicUmmAlQura);
        } else {
	    CFRelease(*cf);
	    *cf = NULL;
	    return false;
	}
    } else {
	*cf = CFRetain(kCFCalendarIdentifierGregorian);
    }
    return true;
}

static bool __CFLocaleCopyCalendar(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    if (__CFLocaleCopyCalendarID(locale, user, cf, context)) {
        CFCalendarRef calendar = _CFCalendarCreateCoWWithIdentifier((CFStringRef)*cf);
	CFCalendarSetLocale(calendar, locale);
        CFDictionaryRef prefs = __CFLocaleGetPrefs(locale);
        CFPropertyListRef metapref = prefs ? CFDictionaryGetValue(prefs, CFSTR("AppleFirstWeekday")) : NULL;
        if (NULL != metapref && CFGetTypeID(metapref) == CFDictionaryGetTypeID()) {
            metapref = (CFNumberRef)CFDictionaryGetValue((CFDictionaryRef)metapref, *cf);
        }
        if (NULL != metapref && CFGetTypeID(metapref) == CFNumberGetTypeID()) {
            CFIndex wkdy;
            if (CFNumberGetValue((CFNumberRef)metapref, kCFNumberCFIndexType, &wkdy)) {
                CFCalendarSetFirstWeekday(calendar, wkdy);
            }
        }
        metapref = prefs ? CFDictionaryGetValue(prefs, CFSTR("AppleMinDaysInFirstWeek")) : NULL;
        if (NULL != metapref && CFGetTypeID(metapref) == CFDictionaryGetTypeID()) {
            metapref = (CFNumberRef)CFDictionaryGetValue((CFDictionaryRef)metapref, *cf);
        }
        if (NULL != metapref && CFGetTypeID(metapref) == CFNumberGetTypeID()) {
            CFIndex mwd;
            if (CFNumberGetValue((CFNumberRef)metapref, kCFNumberCFIndexType, &mwd)) {
                CFCalendarSetMinimumDaysInFirstWeek(calendar, mwd);
            }
        }
	CFRelease(*cf);
	*cf = calendar;
	return true;
    }
#endif
    return false;
}

static bool __CFLocaleCopyDelimiter(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX  || TARGET_OS_BSD
    ULocaleDataDelimiterType type = (ULocaleDataDelimiterType)0;
    if (context == kCFLocaleQuotationBeginDelimiterKey) {
	type = ULOCDATA_QUOTATION_START;
    } else if (context == kCFLocaleQuotationEndDelimiterKey) {
	type = ULOCDATA_QUOTATION_END;
    } else if (context == kCFLocaleAlternateQuotationBeginDelimiterKey) {
	type = ULOCDATA_ALT_QUOTATION_START;
    } else if (context == kCFLocaleAlternateQuotationEndDelimiterKey) {
	type = ULOCDATA_ALT_QUOTATION_END;
    } else {
	return false;
    }

    char localeID[ULOC_FULLNAME_CAPACITY+ULOC_KEYWORD_AND_VALUES_CAPACITY];
    if (!CFStringGetCString(locale->_identifier, localeID, sizeof(localeID)/sizeof(char), kCFStringEncodingASCII)) {
	return false;
    }

    UChar buffer[130];
    UErrorCode status = U_ZERO_ERROR;
    ULocaleData *uld = ulocdata_open(localeID, &status);
    int32_t len = ulocdata_getDelimiter(uld, type, buffer, sizeof(buffer) / sizeof(buffer[0]), &status);
    ulocdata_close(uld);
    if (U_FAILURE(status) || sizeof(buffer) / sizeof(buffer[0]) < len) {
        return false;
    }

    *cf = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (UniChar *)buffer, len);
    return (*cf != NULL);
#else
    if (context == kCFLocaleQuotationBeginDelimiterKey || context == kCFLocaleQuotationEndDelimiterKey || context == kCFLocaleAlternateQuotationBeginDelimiterKey || context == kCFLocaleAlternateQuotationEndDelimiterKey) {
	*cf = CFRetain(CFSTR("\""));
        return true;
    } else {
        return false;
    }
#endif
}

static bool __CFLocaleCopyCollationID(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context) {
    return __CFLocaleCopyICUKeyword(locale, user, cf, context, kCollationKeyword);
}

static bool __CFLocaleCopyCollatorID(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context) {
    CFStringRef canonLocaleCFStr = NULL;
    if (user && locale->_prefs) {
	CFStringRef pref = (CFStringRef)CFDictionaryGetValue(locale->_prefs, CFSTR("AppleCollationOrder"));
	if (pref) {
	    // Canonicalize pref string in case it's not in the canonical format.
	    canonLocaleCFStr = CFLocaleCreateCanonicalLanguageIdentifierFromString(kCFAllocatorSystemDefault, pref);
	} else {
	    CFArrayRef languagesArray = (CFArrayRef)CFDictionaryGetValue(locale->_prefs, CFSTR("AppleLanguages"));
	    if (languagesArray && (CFArrayGetTypeID() == CFGetTypeID(languagesArray))) {
		if (0 < CFArrayGetCount(languagesArray)) {
		    CFStringRef str = (CFStringRef)CFArrayGetValueAtIndex(languagesArray, 0);
		    if (str && (CFStringGetTypeID() == CFGetTypeID(str))) {
			canonLocaleCFStr = CFLocaleCreateCanonicalLanguageIdentifierFromString(kCFAllocatorSystemDefault, str);
		    }
		}
	    }
	}
    }
    if (!canonLocaleCFStr) {
	canonLocaleCFStr = CFLocaleGetIdentifier(locale);
	CFRetain(canonLocaleCFStr);
    }
    *cf = canonLocaleCFStr;
    return canonLocaleCFStr ? true : false;
}

#if TARGET_OS_MAC
STATIC_CONST_STRING_DECL(_metricUnitsKey, "AppleMetricUnits");
STATIC_CONST_STRING_DECL(_measurementUnitsKey, "AppleMeasurementUnits");
STATIC_CONST_STRING_DECL(_measurementUnitsCentimeters, "Centimeters");
STATIC_CONST_STRING_DECL(_measurementUnitsInches, "Inches");
STATIC_CONST_STRING_DECL(_temperatureUnitKey, "AppleTemperatureUnit");

static bool __CFLocaleGetMeasurementSystemForPreferences(CFTypeRef metricPref, CFTypeRef measurementPref, UMeasurementSystem *outMeasurementSystem) {
    if (metricPref || measurementPref) {
#if U_ICU_VERSION_MAJOR_NUM >= 55
        if (metricPref == kCFBooleanTrue && measurementPref && CFEqual(measurementPref, _measurementUnitsInches)) {
            *outMeasurementSystem = UMS_UK;
#else
            return false;
#endif
        } else if (metricPref == kCFBooleanFalse) {
            *outMeasurementSystem = UMS_US;
        } else {
            *outMeasurementSystem = UMS_SI;
        }
        return true;
    }
    return false;
}

static void __CFLocaleGetPreferencesForMeasurementSystem(UMeasurementSystem measurementSystem, CFTypeRef *outMetricPref, CFTypeRef *outMeasurementPref) {
    *outMetricPref = measurementSystem != UMS_US? kCFBooleanTrue: kCFBooleanFalse;
    *outMeasurementPref = measurementSystem == UMS_SI? _measurementUnitsCentimeters: _measurementUnitsInches;
}
#endif

#if U_ICU_VERSION_MAJOR_NUM > 54 || (DEPLOYMENT_RUNTIME_OBJC && TARGET_OS_MAC)
static bool _CFLocaleGetTemperatureUnitForPreferences(CFTypeRef temperaturePref, bool *outCelsius) {
    if (temperaturePref) {
        if (CFEqual(temperaturePref, kCFLocaleTemperatureUnitCelsius)) {
            *outCelsius = true;
            return true;
        } else if (CFEqual(temperaturePref, kCFLocaleTemperatureUnitFahrenheit)) {
            *outCelsius = false;
            return true;
        }
    }
    return false;
}
#endif

#if U_ICU_VERSION_MAJOR_NUM > 54 || (DEPLOYMENT_RUNTIME_OBJC && TARGET_OS_MAC)
static CFStringRef _CFLocaleGetTemperatureUnitName(bool celsius) {
    return celsius? kCFLocaleTemperatureUnitCelsius: kCFLocaleTemperatureUnitFahrenheit;
}
#endif

#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"

static CFStringRef __CFLocaleGetMeasurementSystemName(UMeasurementSystem measurementSystem) {
    switch (measurementSystem) {
        case UMS_US:
            return kCFLocaleMeasurementSystemUS;
#if U_ICU_VERSION_MAJOR_NUM >= 55
        case UMS_UK:
            return kCFLocaleMeasurementSystemUK;
#endif
        default:
            break;
    }
    return kCFLocaleMeasurementSystemMetric;
}

static  bool __CFLocaleGetMeasurementSystemForName(CFStringRef name, UMeasurementSystem *outMeasurementSystem) {
    if (name) {
        if (CFEqual(name, kCFLocaleMeasurementSystemMetric)) {
            *outMeasurementSystem = UMS_SI;
            return true;
        }
        if (CFEqual(name, kCFLocaleMeasurementSystemUS)) {
            *outMeasurementSystem = UMS_US;
            return true;
        }
#if U_ICU_VERSION_MAJOR_NUM >= 55
        if (CFEqual(name, kCFLocaleMeasurementSystemUK)) {
            *outMeasurementSystem = UMS_UK;
            return true;
        }
#endif
    }
    return false;
}

#pragma clang diagnostic pop

#endif

#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
static void __CFLocaleGetMeasurementSystemGuts(CFLocaleRef locale, bool user, UMeasurementSystem *outMeasurementSystem) {
    UMeasurementSystem output = UMS_SI;    // Default is Metric
    bool done = false;
#if TARGET_OS_MAC
    if (user) {
        CFTypeRef metricPref = CFDictionaryGetValue(locale->_prefs, _metricUnitsKey);
        CFTypeRef measurementPref = CFDictionaryGetValue(locale->_prefs, _measurementUnitsKey);
        done = __CFLocaleGetMeasurementSystemForPreferences(metricPref, measurementPref, &output);
    }
#endif
    if (!done) {
        char localeID[ULOC_FULLNAME_CAPACITY+ULOC_KEYWORD_AND_VALUES_CAPACITY];
        if (CFStringGetCString(locale->_identifier, localeID, sizeof(localeID)/sizeof(char), kCFStringEncodingASCII)) {
            UErrorCode  icuStatus = U_ZERO_ERROR;
            output = ulocdata_getMeasurementSystem(localeID, &icuStatus);
            if (U_SUCCESS(icuStatus)) {
                done = true;
            }
        }
    }
    if (!done) {
        output = UMS_SI;
    }
    *outMeasurementSystem = output;
}
#endif


static bool __CFLocaleCopyUsesMetric(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    UMeasurementSystem system = UMS_SI;
    __CFLocaleGetMeasurementSystemGuts(locale, user, &system);
    *cf = system != UMS_US ? kCFBooleanTrue : kCFBooleanFalse;
    return true;
#else
    *cf = kCFBooleanFalse;  //historical behavior, probably irrelevant in CF Mini
    return true;
#endif
}

static bool __CFLocaleCopyMeasurementSystem(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    UMeasurementSystem system = UMS_SI;
    __CFLocaleGetMeasurementSystemGuts(locale, user, &system);
    *cf = CFRetain(__CFLocaleGetMeasurementSystemName(system));
    return true;
#else
    *cf = CFRetain(kCFLocaleMeasurementSystemUS); //historical behavior, probably irrelevant in CF Mini
    return true;
#endif
}



static bool __CFLocaleCopyTemperatureUnit(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context) {
#if U_ICU_VERSION_MAJOR_NUM > 54
    bool celsius = true;    // Default is Celsius
    bool done = false;
#if TARGET_OS_MAC
    if (user) {
        CFTypeRef temperatureUnitPref = CFDictionaryGetValue(locale->_prefs, _temperatureUnitKey);
        done = _CFLocaleGetTemperatureUnitForPreferences(temperatureUnitPref, &celsius);
    }
#endif
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    if (!done) {
        char localeID[ULOC_FULLNAME_CAPACITY+ULOC_KEYWORD_AND_VALUES_CAPACITY];
        if (CFStringGetCString(locale->_identifier, localeID, sizeof(localeID)/sizeof(char), kCFStringEncodingASCII)) {
#if U_ICU_VERSION_MAJOR_NUM > 53 && __has_include(<_foundation_unicode/uameasureformat.h>)
            UErrorCode icuStatus = U_ZERO_ERROR;
            UAMeasureUnit unit;
            int32_t unitCount = uameasfmt_getUnitsForUsage(localeID, "temperature", "weather", &unit, 1, &icuStatus);
            if (U_SUCCESS(icuStatus) && unitCount > 0) {
                if (unit == UAMEASUNIT_TEMPERATURE_FAHRENHEIT) {
                    celsius = false;
                }
                done = true;
            }
#endif
        }
    }
    if (!done) {
        UMeasurementSystem system = UMS_SI;
        __CFLocaleGetMeasurementSystemGuts(locale, user, &system);
        if (system == UMS_US) {
            celsius = false;
        }
        done = true;
    }
#endif
    if (!done) {
        celsius = true;
    }
    *cf = CFRetain(_CFLocaleGetTemperatureUnitName(celsius));
    return true;
#else
    return false;
#endif
}

static bool __CFLocaleCopyNumberFormat(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context) {
    CFStringRef str = NULL;
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    CFNumberFormatterRef nf = CFNumberFormatterCreate(kCFAllocatorSystemDefault, locale, kCFNumberFormatterDecimalStyle);
    str = nf ? (CFStringRef)CFNumberFormatterCopyProperty(nf, context) : NULL;
    if (nf) CFRelease(nf);
#endif
    if (str) {
	*cf = str;
	return true;
    }
    return false;
}

// ICU does not reliably set up currency info for other than Currency-type formatters,
// so we have to have another routine here which creates a Currency number formatter.
static bool __CFLocaleCopyNumberFormat2(CFLocaleRef locale, bool user, CFTypeRef *cf, CFStringRef context) {
    CFStringRef str = NULL;
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    CFNumberFormatterRef nf = CFNumberFormatterCreate(kCFAllocatorSystemDefault, locale, kCFNumberFormatterCurrencyStyle);
    str = nf ? (CFStringRef)CFNumberFormatterCopyProperty(nf, context) : NULL;
    if (nf) CFRelease(nf);
#endif
    if (str) {
	*cf = str;
	return true;
    }
    return false;
}

#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
typedef int32_t (*__CFICUFunction)(const char *, const char *, UChar *, int32_t, UErrorCode *);

static bool __CFLocaleICUName(const char *locale, const char *valLocale, CFStringRef *out, __CFICUFunction icu) {
    UErrorCode icuStatus = U_ZERO_ERROR;
    int32_t size;
    UChar name[kMaxICUNameSize];

    size = (*icu)(valLocale, locale, name, kMaxICUNameSize, &icuStatus);
    if (U_SUCCESS(icuStatus) && size > 0 && icuStatus != U_USING_DEFAULT_WARNING) {
        *out = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (UniChar *)name, size);
        return (*out != NULL);
    }
    return false;
}

static bool __CFLocaleICUKeywordValueName(const char *locale, const char *value, const char *keyword, CFStringRef *out) {
    UErrorCode icuStatus = U_ZERO_ERROR;
    int32_t size = 0;
    UChar name[kMaxICUNameSize];
    // Need to make a fake locale ID
    char lid[ULOC_FULLNAME_CAPACITY+ULOC_KEYWORD_AND_VALUES_CAPACITY];
    if (strlen(value) < ULOC_KEYWORD_AND_VALUES_CAPACITY) {
	strlcpy(lid, "en_US@", sizeof(lid));
	strlcat(lid, keyword, sizeof(lid));
	strlcat(lid, "=", sizeof(lid));
	strlcat(lid, value, sizeof(lid));
        size = uloc_getDisplayKeywordValue(lid, keyword, locale, name, kMaxICUNameSize, &icuStatus);
        if (U_SUCCESS(icuStatus) && size > 0 && icuStatus != U_USING_DEFAULT_WARNING) {
            *out = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (UniChar *)name, size);
            return (*out != NULL);
        }
    }
    return false;
}

static bool __CFLocaleICUCurrencyName(const char *locale, const char *value, UCurrNameStyle style, CFStringRef *out) {
    int valLen = strlen(value);
    if (valLen != 3) // not a valid ISO code
        return false;
    UChar curr[4];
    UBool isChoice = FALSE;
    int32_t size = 0;
    UErrorCode icuStatus = U_ZERO_ERROR;
    u_charsToUChars(value, curr, valLen);
    curr[valLen] = '\0';
    const UChar *name;
    name = ucurr_getName(curr, locale, style, &isChoice, &size, &icuStatus);
    if (U_FAILURE(icuStatus) || icuStatus == U_USING_DEFAULT_WARNING)
        return false;
    UChar result[kMaxICUNameSize];
    if (isChoice)
    {
        UChar pattern[kMaxICUNameSize];
        CFStringRef patternRef = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("{0,choice,%S}"), name);
        CFIndex pattlen = CFStringGetLength(patternRef);
        CFStringGetCharacters(patternRef, CFRangeMake(0, pattlen), (UniChar *)pattern);
        CFRelease(patternRef);
        pattern[pattlen] = '\0';        // null terminate the pattern
        // Format the message assuming a large amount of the currency
        size = u_formatMessage("en_US", pattern, pattlen, result, kMaxICUNameSize, &icuStatus, 10.0);
        if (U_FAILURE(icuStatus))
            return false;
        name = result;
        
    }
    *out = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (UniChar *)name, size);
    return (*out != NULL);
}
#endif

static bool __CFLocaleFullName(const char *locale, const char *value, CFStringRef *out) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    UErrorCode icuStatus = U_ZERO_ERROR;
    int32_t size;
    UChar name[kMaxICUNameSize];
    
    // First, try to get the full locale.
    size = uloc_getDisplayName(value, locale, name, kMaxICUNameSize, &icuStatus);
    if (U_FAILURE(icuStatus) || size <= 0)
        return false;

    // Did we wind up using a default somewhere?
    if (icuStatus == U_USING_DEFAULT_WARNING) {
        // For some locale IDs, there may be no language which has a translation for every
        // piece. Rather than return nothing, see if we can at least handle
        // the language part of the locale.
        UErrorCode localStatus = U_ZERO_ERROR;
        int32_t localSize;
        UChar localName[kMaxICUNameSize];
        localSize = uloc_getDisplayLanguage(value, locale, localName, kMaxICUNameSize, &localStatus);
        if (U_FAILURE(localStatus) || size <= 0 || localStatus == U_USING_DEFAULT_WARNING)
            return false;
    }

    // This locale is OK, so use the result.
    *out = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (UniChar *)name, size);
    return (*out != NULL);
#else
    *out = CFRetain(CFSTR("(none)"));
    return true;
#endif
}

static bool __CFLocaleLanguageName(const char *locale, const char *value, CFStringRef *out) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    return __CFLocaleICUName(locale, value, out, uloc_getDisplayLanguage);
#else
    *out = CFRetain(CFSTR("(none)"));
    return true;
#endif
}

static bool __CFLocaleCountryName(const char *locale, const char *value, CFStringRef *out) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    // Need to make a fake locale ID
    char lid[ULOC_FULLNAME_CAPACITY];
    if (strlen(value) < sizeof(lid) - 3) {
	strlcpy(lid, "en_", sizeof(lid));
	strlcat(lid, value, sizeof(lid));
        return __CFLocaleICUName(locale, lid, out, uloc_getDisplayCountry);
    }
    return false;
#else
    *out = CFRetain(CFSTR("(none)"));
    return true;
#endif
}

static bool __CFLocaleScriptName(const char *locale, const char *value, CFStringRef *out) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    // Need to make a fake locale ID
    char lid[ULOC_FULLNAME_CAPACITY];
    if (strlen(value) == 4) {
	strlcpy(lid, "en_", sizeof(lid));
	strlcat(lid, value, sizeof(lid));
	strlcat(lid, "_US", sizeof(lid));
        return __CFLocaleICUName(locale, lid, out, uloc_getDisplayScript);
    }
    return false;
#else
    *out = CFRetain(CFSTR("(none)"));
    return true;
#endif
}

static bool __CFLocaleVariantName(const char *locale, const char *value, CFStringRef *out) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    // Need to make a fake locale ID
    char lid[ULOC_FULLNAME_CAPACITY+ULOC_KEYWORD_AND_VALUES_CAPACITY];
    if (strlen(value) < sizeof(lid) - 6) {
	strlcpy(lid, "en_US_", sizeof(lid));
	strlcat(lid, value, sizeof(lid));
        return __CFLocaleICUName(locale, lid, out, uloc_getDisplayVariant);
    }
    return false;
#else
    *out = CFRetain(CFSTR("(none)"));
    return true;
#endif
}

static bool __CFLocaleCalendarName(const char *locale, const char *value, CFStringRef *out) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    return __CFLocaleICUKeywordValueName(locale, value, kCalendarKeyword, out);
#else
    *out = CFRetain(CFSTR("(none)"));
    return true;
#endif
}

static bool __CFLocaleCollationName(const char *locale, const char *value, CFStringRef *out) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    return __CFLocaleICUKeywordValueName(locale, value, kCollationKeyword, out);
#else
    *out = CFRetain(CFSTR("(none)"));
    return true;
#endif
}

static bool __CFLocaleCurrencyShortName(const char *locale, const char *value, CFStringRef *out) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    return __CFLocaleICUCurrencyName(locale, value, UCURR_SYMBOL_NAME, out);
#else
    *out = CFRetain(CFSTR("(none)"));
    return true;
#endif
}

static bool __CFLocaleCurrencyFullName(const char *locale, const char *value, CFStringRef *out) {
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
    return __CFLocaleICUCurrencyName(locale, value, UCURR_LONG_NAME, out);
#else
    *out = CFRetain(CFSTR("(none)"));
    return true;
#endif
}

static bool __CFLocaleNoName(const char *locale, const char *value, CFStringRef *out) {
    return false;
}

#undef kMaxICUNameSize