1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 2002-2014, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
8 */
9 #include "unicode/utypes.h"
10
11 #if !UCONFIG_NO_SERVICE || !UCONFIG_NO_TRANSLITERATION
12
13 #include "unicode/resbund.h"
14 #include "unicode/uenum.h"
15 #include "cmemory.h"
16 #include "ustrfmt.h"
17 #include "locutil.h"
18 #include "charstr.h"
19 #include "ucln_cmn.h"
20 #include "uassert.h"
21 #include "umutex.h"
22
23 // see LocaleUtility::getAvailableLocaleNames
24 static icu::UInitOnce LocaleUtilityInitOnce {};
25 static icu::Hashtable * LocaleUtility_cache = nullptr;
26
27 #define UNDERSCORE_CHAR ((char16_t)0x005f)
28 #define AT_SIGN_CHAR ((char16_t)64)
29 #define PERIOD_CHAR ((char16_t)46)
30
31 /*
32 ******************************************************************
33 */
34
35 /**
36 * Release all static memory held by Locale Utility.
37 */
38 U_CDECL_BEGIN
service_cleanup()39 static UBool U_CALLCONV service_cleanup() {
40 if (LocaleUtility_cache) {
41 delete LocaleUtility_cache;
42 LocaleUtility_cache = nullptr;
43 }
44 return true;
45 }
46
47
locale_utility_init(UErrorCode & status)48 static void U_CALLCONV locale_utility_init(UErrorCode &status) {
49 using namespace icu;
50 U_ASSERT(LocaleUtility_cache == nullptr);
51 ucln_common_registerCleanup(UCLN_COMMON_SERVICE, service_cleanup);
52 LocaleUtility_cache = new Hashtable(status);
53 if (U_FAILURE(status)) {
54 delete LocaleUtility_cache;
55 LocaleUtility_cache = nullptr;
56 return;
57 }
58 if (LocaleUtility_cache == nullptr) {
59 status = U_MEMORY_ALLOCATION_ERROR;
60 return;
61 }
62 LocaleUtility_cache->setValueDeleter(uhash_deleteHashtable);
63 }
64
65 U_CDECL_END
66
67 U_NAMESPACE_BEGIN
68
69 UnicodeString&
canonicalLocaleString(const UnicodeString * id,UnicodeString & result)70 LocaleUtility::canonicalLocaleString(const UnicodeString* id, UnicodeString& result)
71 {
72 if (id == nullptr) {
73 result.setToBogus();
74 } else {
75 // Fix case only (no other changes) up to the first '@' or '.' or
76 // end of string, whichever comes first. In 3.0 I changed this to
77 // stop at first '@' or '.'. It used to run out to the end of
78 // string. My fix makes the tests pass but is probably
79 // structurally incorrect. See below. [alan 3.0]
80
81 // TODO: Doug, you might want to revise this...
82 result = *id;
83 int32_t i = 0;
84 int32_t end = result.indexOf(AT_SIGN_CHAR);
85 int32_t n = result.indexOf(PERIOD_CHAR);
86 if (n >= 0 && n < end) {
87 end = n;
88 }
89 if (end < 0) {
90 end = result.length();
91 }
92 n = result.indexOf(UNDERSCORE_CHAR);
93 if (n < 0) {
94 n = end;
95 }
96 for (; i < n; ++i) {
97 char16_t c = result.charAt(i);
98 if (c >= 0x0041 && c <= 0x005a) {
99 c += 0x20;
100 result.setCharAt(i, c);
101 }
102 }
103 for (n = end; i < n; ++i) {
104 char16_t c = result.charAt(i);
105 if (c >= 0x0061 && c <= 0x007a) {
106 c -= 0x20;
107 result.setCharAt(i, c);
108 }
109 }
110 }
111 return result;
112
113 #if 0
114 // This code does a proper full level 2 canonicalization of id.
115 // It's nasty to go from char16_t to char to char to char16_t -- but
116 // that's what you have to do to use the uloc_canonicalize
117 // function on UnicodeStrings.
118
119 // I ended up doing the alternate fix (see above) not for
120 // performance reasons, although performance will certainly be
121 // better, but because doing a full level 2 canonicalization
122 // causes some tests to fail. [alan 3.0]
123
124 // TODO: Doug, you might want to revisit this...
125 result.setToBogus();
126 if (id != 0) {
127 int32_t buflen = id->length() + 8; // space for NUL
128 char* buf = (char*) uprv_malloc(buflen);
129 char* canon = (buf == 0) ? 0 : (char*) uprv_malloc(buflen);
130 if (buf != 0 && canon != 0) {
131 U_ASSERT(id->extract(0, INT32_MAX, buf, buflen) < buflen);
132 UErrorCode ec = U_ZERO_ERROR;
133 uloc_canonicalize(buf, canon, buflen, &ec);
134 if (U_SUCCESS(ec)) {
135 result = UnicodeString(canon);
136 }
137 }
138 uprv_free(buf);
139 uprv_free(canon);
140 }
141 return result;
142 #endif
143 }
144
145 Locale&
initLocaleFromName(const UnicodeString & id,Locale & result)146 LocaleUtility::initLocaleFromName(const UnicodeString& id, Locale& result)
147 {
148 if (id.isBogus()) {
149 result.setToBogus();
150 } else {
151 /*
152 * We need to convert from a UnicodeString to char * in order to
153 * create a Locale.
154 *
155 * Problem: Locale ID strings may contain '@' which is a variant
156 * character and cannot be handled by invariant-character conversion.
157 *
158 * Hack: Since ICU code can handle locale IDs with multiple encodings
159 * of '@' (at least for EBCDIC; it's not known to be a problem for
160 * ASCII-based systems),
161 * we use regular invariant-character conversion for everything else
162 * and manually convert U+0040 into a compiler-char-constant '@'.
163 * While this compilation-time constant may not match the runtime
164 * encoding of '@', it should be one of the encodings which ICU
165 * recognizes.
166 *
167 * There should be only at most one '@' in a locale ID.
168 */
169 CharString buffer;
170 int32_t prev, i;
171 prev = 0;
172 UErrorCode status = U_ZERO_ERROR;
173 do {
174 i = id.indexOf(static_cast<char16_t>(0x40), prev);
175 if(i < 0) {
176 // no @ between prev and the rest of the string
177 buffer.appendInvariantChars(id.tempSubString(prev), status);
178 break; // done
179 } else {
180 // normal invariant-character conversion for text between @s
181 buffer.appendInvariantChars(id.tempSubString(prev, i - prev), status);
182 // manually "convert" U+0040 at id[i] into '@' at buffer[i]
183 buffer.append('@', status);
184 prev = i + 1;
185 }
186 } while (U_SUCCESS(status));
187 if (U_FAILURE(status)) {
188 result.setToBogus();
189 } else {
190 result = Locale::createFromName(buffer.data());
191 }
192 }
193 return result;
194 }
195
196 UnicodeString&
initNameFromLocale(const Locale & locale,UnicodeString & result)197 LocaleUtility::initNameFromLocale(const Locale& locale, UnicodeString& result)
198 {
199 if (locale.isBogus()) {
200 result.setToBogus();
201 } else {
202 result.append(UnicodeString(locale.getName(), -1, US_INV));
203 }
204 return result;
205 }
206
207 const Hashtable*
getAvailableLocaleNames(const UnicodeString & bundleID)208 LocaleUtility::getAvailableLocaleNames(const UnicodeString& bundleID)
209 {
210 // LocaleUtility_cache is a hash-of-hashes. The top-level keys
211 // are path strings ('bundleID') passed to
212 // ures_openAvailableLocales. The top-level values are
213 // second-level hashes. The second-level keys are result strings
214 // from ures_openAvailableLocales. The second-level values are
215 // garbage ((void*)1 or other random pointer).
216
217 UErrorCode status = U_ZERO_ERROR;
218 umtx_initOnce(LocaleUtilityInitOnce, locale_utility_init, status);
219 Hashtable *cache = LocaleUtility_cache;
220 if (cache == nullptr) {
221 // Catastrophic failure.
222 return nullptr;
223 }
224
225 Hashtable* htp;
226 umtx_lock(nullptr);
227 htp = static_cast<Hashtable*>(cache->get(bundleID));
228 umtx_unlock(nullptr);
229
230 if (htp == nullptr) {
231 htp = new Hashtable(status);
232 if (htp && U_SUCCESS(status)) {
233 CharString cbundleID;
234 cbundleID.appendInvariantChars(bundleID, status);
235 const char* path = cbundleID.isEmpty() ? nullptr : cbundleID.data();
236 icu::LocalUEnumerationPointer uenum(ures_openAvailableLocales(path, &status));
237 for (;;) {
238 const char16_t* id = uenum_unext(uenum.getAlias(), nullptr, &status);
239 if (id == nullptr) {
240 break;
241 }
242 htp->put(UnicodeString(id), (void*)htp, status);
243 }
244 if (U_FAILURE(status)) {
245 delete htp;
246 return nullptr;
247 }
248 umtx_lock(nullptr);
249 Hashtable *t = static_cast<Hashtable *>(cache->get(bundleID));
250 if (t != nullptr) {
251 // Another thread raced through this code, creating the cache entry first.
252 // Discard ours and return theirs.
253 umtx_unlock(nullptr);
254 delete htp;
255 htp = t;
256 } else {
257 cache->put(bundleID, (void*)htp, status);
258 umtx_unlock(nullptr);
259 }
260 }
261 }
262 return htp;
263 }
264
265 bool
isFallbackOf(const UnicodeString & root,const UnicodeString & child)266 LocaleUtility::isFallbackOf(const UnicodeString& root, const UnicodeString& child)
267 {
268 return child.indexOf(root) == 0 &&
269 (child.length() == root.length() ||
270 child.charAt(root.length()) == UNDERSCORE_CHAR);
271 }
272
273 U_NAMESPACE_END
274
275 /* !UCONFIG_NO_SERVICE */
276 #endif
277