|
23 | 23 | #include <strings.h>
|
24 | 24 | #endif
|
25 | 25 |
|
26 |
| -int _stringshims_strncasecmp_l(const char * _Nullable s1, |
27 |
| - const char * _Nullable s2, |
28 |
| - size_t n, |
29 |
| - locale_t _Nullable loc) |
30 |
| -{ |
31 | 26 | #if TARGET_OS_WINDOWS
|
32 |
| - static _locale_t storage; |
33 |
| - static _locale_t *cloc = NULL; |
34 |
| - if (cloc == NULL) { |
35 |
| - storage = _create_locale(LC_ALL, "C"); |
36 |
| - cloc = &storage; |
37 |
| - } |
38 |
| - return _strnicmp_l(s1, s2, n, loc ? loc : *cloc); |
39 |
| -#else |
40 |
| - if (loc != NULL) { |
41 |
| -#if (defined(TARGET_OS_EXCLAVEKIT) && TARGET_OS_EXCLAVEKIT) || \ |
42 |
| - (defined(TARGET_OS_ANDROID) && TARGET_OS_ANDROID && __ANDROID_API__ < 23) |
43 |
| - abort(); |
| 27 | +#define FOUNDATION_C_LOCALE (_clocale()) |
| 28 | +static _locale_t _clocale() { |
| 29 | + static _locale_t storage; |
| 30 | + static bool once = false; |
| 31 | + if (!once) { |
| 32 | + storage = _create_locale(LC_ALL, "C"); |
| 33 | + once = true; |
| 34 | + } |
| 35 | + return storage; |
| 36 | +} |
| 37 | +#elif TARGET_OS_MAC |
| 38 | +#define FOUNDATION_C_LOCALE LC_C_LOCALE |
44 | 39 | #else
|
45 |
| - return strncasecmp_l(s1, s2, n, loc); |
46 |
| -#endif |
| 40 | +#define FOUNDATION_C_LOCALE (_clocale()) |
| 41 | +static locale_t _clocale() { |
| 42 | + static locale_t storage; |
| 43 | + static bool once = false; |
| 44 | + if (!once) { |
| 45 | + storage = newlocale(LC_ALL_MASK, "C", (locale_t)0); |
| 46 | + once = true; |
47 | 47 | }
|
48 |
| - // On Darwin, NULL loc means unlocalized compare. |
49 |
| - // Uses the standard C locale for Linux in this case |
50 |
| -#if (defined(TARGET_OS_EXCLAVEKIT) && TARGET_OS_EXCLAVEKIT) || \ |
| 48 | + return storage; |
| 49 | +} |
| 50 | +#endif |
| 51 | + |
| 52 | + |
| 53 | +int _stringshims_strncasecmp_clocale(const char * _Nullable s1, |
| 54 | + const char * _Nullable s2, |
| 55 | + size_t n) |
| 56 | +{ |
| 57 | +#if TARGET_OS_WINDOWS |
| 58 | + return _strnicmp_l(s1, s2, n, FOUNDATION_C_LOCALE); |
| 59 | +#elif (defined(TARGET_OS_EXCLAVEKIT) && TARGET_OS_EXCLAVEKIT) || \ |
51 | 60 | (defined(TARGET_OS_ANDROID) && TARGET_OS_ANDROID && __ANDROID_API__ < 23)
|
52 | 61 | return strncasecmp(s1, s2, n);
|
53 |
| -#elif TARGET_OS_MAC |
54 |
| - return strncasecmp_l(s1, s2, n, NULL); |
55 | 62 | #else
|
56 |
| - locale_t clocale = newlocale(LC_ALL_MASK, "C", (locale_t)0); |
57 |
| - return strncasecmp_l(s1, s2, n, clocale); |
58 |
| -#endif // TARGET_OS_MAC |
59 |
| -#endif // TARGET_OS_WINDOWS |
| 63 | + return strncasecmp_l(s1, s2, n, FOUNDATION_C_LOCALE); |
| 64 | +#endif |
60 | 65 | }
|
61 | 66 |
|
62 |
| -double _stringshims_strtod_l(const char * _Nullable restrict nptr, |
63 |
| - char * _Nullable * _Nullable restrict endptr, |
64 |
| - locale_t _Nullable loc) |
| 67 | +double _stringshims_strtod_clocale(const char * _Nullable restrict nptr, |
| 68 | + char * _Nullable * _Nullable restrict endptr) |
65 | 69 | {
|
66 |
| -#if defined(TARGET_OS_EXCLAVEKIT) && TARGET_OS_EXCLAVEKIT |
67 |
| - assert(loc == NULL); |
68 |
| - return strtod_l(nptr, endptr, NULL); |
69 |
| -#elif TARGET_OS_MAC |
70 |
| - return strtod_l(nptr, endptr, loc); |
| 70 | +#if TARGET_OS_MAC |
| 71 | + return strtod_l(nptr, endptr, FOUNDATION_C_LOCALE); |
71 | 72 | #elif TARGET_OS_WINDOWS
|
72 |
| - return _strtod_l(nptr, endptr, loc); |
| 73 | + return _strtod_l(nptr, endptr, FOUNDATION_C_LOCALE); |
73 | 74 | #else
|
74 | 75 | // Use the C locale
|
75 |
| - locale_t clocale = newlocale(LC_ALL_MASK, "C", (locale_t)0); |
76 |
| - locale_t oldLocale = uselocale(clocale); |
| 76 | + locale_t oldLocale = uselocale(FOUNDATION_C_LOCALE); |
77 | 77 | double result = strtod(nptr, endptr);
|
78 | 78 | // Restore locale
|
79 | 79 | uselocale(oldLocale);
|
80 | 80 | return result;
|
81 | 81 | #endif
|
82 | 82 | }
|
83 | 83 |
|
84 |
| -float _stringshims_strtof_l(const char * _Nullable restrict nptr, |
85 |
| - char * _Nullable * _Nullable restrict endptr, |
86 |
| - locale_t _Nullable loc) |
| 84 | +float _stringshims_strtof_clocale(const char * _Nullable restrict nptr, |
| 85 | + char * _Nullable * _Nullable restrict endptr) |
87 | 86 | {
|
88 |
| -#if defined(TARGET_OS_EXCLAVEKIT) && TARGET_OS_EXCLAVEKIT |
89 |
| - assert(loc == NULL); |
90 |
| - return strtof_l(nptr, endptr, NULL); |
91 |
| -#elif TARGET_OS_MAC |
92 |
| - return strtof_l(nptr, endptr, loc); |
| 87 | +#if TARGET_OS_MAC |
| 88 | + return strtof_l(nptr, endptr, FOUNDATION_C_LOCALE); |
93 | 89 | #elif TARGET_OS_WINDOWS
|
94 |
| - return _strtof_l(nptr, endptr, loc); |
| 90 | + return _strtof_l(nptr, endptr, FOUNDATION_C_LOCALE); |
95 | 91 | #else
|
96 | 92 | // Use the C locale
|
97 |
| - locale_t clocale = newlocale(LC_ALL_MASK, "C", (locale_t)0); |
98 |
| - locale_t oldLocale = uselocale(clocale); |
99 |
| - float result = strtof(nptr, endptr); |
| 93 | + locale_t oldLocale = uselocale(FOUNDATION_C_LOCALE); |
| 94 | + double result = strtof(nptr, endptr); |
100 | 95 | // Restore locale
|
101 | 96 | uselocale(oldLocale);
|
102 | 97 | return result;
|
|
0 commit comments