1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2002-2014, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
9 #include "unicode/utypes.h"
11 #if !UCONFIG_NO_SERVICE || !UCONFIG_NO_TRANSLITERATION
13 #include "unicode/resbund.h"
22 // see LocaleUtility::getAvailableLocaleNames
23 static icu::UInitOnce LocaleUtilityInitOnce
= U_INITONCE_INITIALIZER
;
24 static icu::Hashtable
* LocaleUtility_cache
= NULL
;
26 #define UNDERSCORE_CHAR ((UChar)0x005f)
27 #define AT_SIGN_CHAR ((UChar)64)
28 #define PERIOD_CHAR ((UChar)46)
31 ******************************************************************
35 * Release all static memory held by Locale Utility.
38 static UBool U_CALLCONV
service_cleanup(void) {
39 if (LocaleUtility_cache
) {
40 delete LocaleUtility_cache
;
41 LocaleUtility_cache
= NULL
;
47 static void U_CALLCONV
locale_utility_init(UErrorCode
&status
) {
49 U_ASSERT(LocaleUtility_cache
== NULL
);
50 ucln_common_registerCleanup(UCLN_COMMON_SERVICE
, service_cleanup
);
51 LocaleUtility_cache
= new Hashtable(status
);
52 if (U_FAILURE(status
)) {
53 delete LocaleUtility_cache
;
54 LocaleUtility_cache
= NULL
;
57 if (LocaleUtility_cache
== NULL
) {
58 status
= U_MEMORY_ALLOCATION_ERROR
;
61 LocaleUtility_cache
->setValueDeleter(uhash_deleteHashtable
);
69 LocaleUtility::canonicalLocaleString(const UnicodeString
* id
, UnicodeString
& result
)
74 // Fix case only (no other changes) up to the first '@' or '.' or
75 // end of string, whichever comes first. In 3.0 I changed this to
76 // stop at first '@' or '.'. It used to run out to the end of
77 // string. My fix makes the tests pass but is probably
78 // structurally incorrect. See below. [alan 3.0]
80 // TODO: Doug, you might want to revise this...
83 int32_t end
= result
.indexOf(AT_SIGN_CHAR
);
84 int32_t n
= result
.indexOf(PERIOD_CHAR
);
85 if (n
>= 0 && n
< end
) {
89 end
= result
.length();
91 n
= result
.indexOf(UNDERSCORE_CHAR
);
96 UChar c
= result
.charAt(i
);
97 if (c
>= 0x0041 && c
<= 0x005a) {
99 result
.setCharAt(i
, c
);
102 for (n
= end
; i
< n
; ++i
) {
103 UChar c
= result
.charAt(i
);
104 if (c
>= 0x0061 && c
<= 0x007a) {
106 result
.setCharAt(i
, c
);
113 // This code does a proper full level 2 canonicalization of id.
114 // It's nasty to go from UChar to char to char to UChar -- but
115 // that's what you have to do to use the uloc_canonicalize
116 // function on UnicodeStrings.
118 // I ended up doing the alternate fix (see above) not for
119 // performance reasons, although performance will certainly be
120 // better, but because doing a full level 2 canonicalization
121 // causes some tests to fail. [alan 3.0]
123 // TODO: Doug, you might want to revisit this...
126 int32_t buflen
= id
->length() + 8; // space for NUL
127 char* buf
= (char*) uprv_malloc(buflen
);
128 char* canon
= (buf
== 0) ? 0 : (char*) uprv_malloc(buflen
);
129 if (buf
!= 0 && canon
!= 0) {
130 U_ASSERT(id
->extract(0, INT32_MAX
, buf
, buflen
) < buflen
);
131 UErrorCode ec
= U_ZERO_ERROR
;
132 uloc_canonicalize(buf
, canon
, buflen
, &ec
);
134 result
= UnicodeString(canon
);
145 LocaleUtility::initLocaleFromName(const UnicodeString
& id
, Locale
& result
)
147 enum { BUFLEN
= 128 }; // larger than ever needed
149 if (id
.isBogus() || id
.length() >= BUFLEN
) {
153 * We need to convert from a UnicodeString to char * in order to
156 * Problem: Locale ID strings may contain '@' which is a variant
157 * character and cannot be handled by invariant-character conversion.
159 * Hack: Since ICU code can handle locale IDs with multiple encodings
160 * of '@' (at least for EBCDIC; it's not known to be a problem for
161 * ASCII-based systems),
162 * we use regular invariant-character conversion for everything else
163 * and manually convert U+0040 into a compiler-char-constant '@'.
164 * While this compilation-time constant may not match the runtime
165 * encoding of '@', it should be one of the encodings which ICU
168 * There should be only at most one '@' in a locale ID.
174 i
= id
.indexOf((UChar
)0x40, prev
);
176 // no @ between prev and the rest of the string
177 id
.extract(prev
, INT32_MAX
, buffer
+ prev
, BUFLEN
- prev
, US_INV
);
180 // normal invariant-character conversion for text between @s
181 id
.extract(prev
, i
- prev
, buffer
+ prev
, BUFLEN
- prev
, US_INV
);
182 // manually "convert" U+0040 at id[i] into '@' at buffer[i]
187 result
= Locale::createFromName(buffer
);
193 LocaleUtility::initNameFromLocale(const Locale
& locale
, UnicodeString
& result
)
195 if (locale
.isBogus()) {
198 result
.append(UnicodeString(locale
.getName(), -1, US_INV
));
204 LocaleUtility::getAvailableLocaleNames(const UnicodeString
& bundleID
)
206 // LocaleUtility_cache is a hash-of-hashes. The top-level keys
207 // are path strings ('bundleID') passed to
208 // ures_openAvailableLocales. The top-level values are
209 // second-level hashes. The second-level keys are result strings
210 // from ures_openAvailableLocales. The second-level values are
211 // garbage ((void*)1 or other random pointer).
213 UErrorCode status
= U_ZERO_ERROR
;
214 umtx_initOnce(LocaleUtilityInitOnce
, locale_utility_init
, status
);
215 Hashtable
*cache
= LocaleUtility_cache
;
217 // Catastrophic failure.
223 htp
= (Hashtable
*) cache
->get(bundleID
);
227 htp
= new Hashtable(status
);
228 if (htp
&& U_SUCCESS(status
)) {
229 CharString cbundleID
;
230 cbundleID
.appendInvariantChars(bundleID
, status
);
231 const char* path
= cbundleID
.isEmpty() ? NULL
: cbundleID
.data();
232 UEnumeration
*uenum
= ures_openAvailableLocales(path
, &status
);
234 const UChar
* id
= uenum_unext(uenum
, NULL
, &status
);
238 htp
->put(UnicodeString(id
), (void*)htp
, status
);
241 if (U_FAILURE(status
)) {
246 Hashtable
*t
= static_cast<Hashtable
*>(cache
->get(bundleID
));
248 // Another thread raced through this code, creating the cache entry first.
249 // Discard ours and return theirs.
254 cache
->put(bundleID
, (void*)htp
, status
);
263 LocaleUtility::isFallbackOf(const UnicodeString
& root
, const UnicodeString
& child
)
265 return child
.indexOf(root
) == 0 &&
266 (child
.length() == root
.length() ||
267 child
.charAt(root
.length()) == UNDERSCORE_CHAR
);
272 /* !UCONFIG_NO_SERVICE */