locutil.cpp 8.6 KB

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