locutil.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. if (id.isBogus()) {
  133. result.setToBogus();
  134. } else {
  135. /*
  136. * We need to convert from a UnicodeString to char * in order to
  137. * create a Locale.
  138. *
  139. * Problem: Locale ID strings may contain '@' which is a variant
  140. * character and cannot be handled by invariant-character conversion.
  141. *
  142. * Hack: Since ICU code can handle locale IDs with multiple encodings
  143. * of '@' (at least for EBCDIC; it's not known to be a problem for
  144. * ASCII-based systems),
  145. * we use regular invariant-character conversion for everything else
  146. * and manually convert U+0040 into a compiler-char-constant '@'.
  147. * While this compilation-time constant may not match the runtime
  148. * encoding of '@', it should be one of the encodings which ICU
  149. * recognizes.
  150. *
  151. * There should be only at most one '@' in a locale ID.
  152. */
  153. CharString buffer;
  154. int32_t prev, i;
  155. prev = 0;
  156. UErrorCode status = U_ZERO_ERROR;
  157. do {
  158. i = id.indexOf(static_cast<char16_t>(0x40), prev);
  159. if(i < 0) {
  160. // no @ between prev and the rest of the string
  161. buffer.appendInvariantChars(id.tempSubString(prev), status);
  162. break; // done
  163. } else {
  164. // normal invariant-character conversion for text between @s
  165. buffer.appendInvariantChars(id.tempSubString(prev, i - prev), status);
  166. // manually "convert" U+0040 at id[i] into '@' at buffer[i]
  167. buffer.append('@', status);
  168. prev = i + 1;
  169. }
  170. } while (U_SUCCESS(status));
  171. if (U_FAILURE(status)) {
  172. result.setToBogus();
  173. } else {
  174. result = Locale::createFromName(buffer.data());
  175. }
  176. }
  177. return result;
  178. }
  179. UnicodeString&
  180. LocaleUtility::initNameFromLocale(const Locale& locale, UnicodeString& result)
  181. {
  182. if (locale.isBogus()) {
  183. result.setToBogus();
  184. } else {
  185. result.append(UnicodeString(locale.getName(), -1, US_INV));
  186. }
  187. return result;
  188. }
  189. const Hashtable*
  190. LocaleUtility::getAvailableLocaleNames(const UnicodeString& bundleID)
  191. {
  192. // LocaleUtility_cache is a hash-of-hashes. The top-level keys
  193. // are path strings ('bundleID') passed to
  194. // ures_openAvailableLocales. The top-level values are
  195. // second-level hashes. The second-level keys are result strings
  196. // from ures_openAvailableLocales. The second-level values are
  197. // garbage ((void*)1 or other random pointer).
  198. UErrorCode status = U_ZERO_ERROR;
  199. umtx_initOnce(LocaleUtilityInitOnce, locale_utility_init, status);
  200. Hashtable *cache = LocaleUtility_cache;
  201. if (cache == nullptr) {
  202. // Catastrophic failure.
  203. return nullptr;
  204. }
  205. Hashtable* htp;
  206. umtx_lock(nullptr);
  207. htp = static_cast<Hashtable*>(cache->get(bundleID));
  208. umtx_unlock(nullptr);
  209. if (htp == nullptr) {
  210. htp = new Hashtable(status);
  211. if (htp && U_SUCCESS(status)) {
  212. CharString cbundleID;
  213. cbundleID.appendInvariantChars(bundleID, status);
  214. const char* path = cbundleID.isEmpty() ? nullptr : cbundleID.data();
  215. icu::LocalUEnumerationPointer uenum(ures_openAvailableLocales(path, &status));
  216. for (;;) {
  217. const char16_t* id = uenum_unext(uenum.getAlias(), nullptr, &status);
  218. if (id == nullptr) {
  219. break;
  220. }
  221. htp->put(UnicodeString(id), (void*)htp, status);
  222. }
  223. if (U_FAILURE(status)) {
  224. delete htp;
  225. return nullptr;
  226. }
  227. umtx_lock(nullptr);
  228. Hashtable *t = static_cast<Hashtable *>(cache->get(bundleID));
  229. if (t != nullptr) {
  230. // Another thread raced through this code, creating the cache entry first.
  231. // Discard ours and return theirs.
  232. umtx_unlock(nullptr);
  233. delete htp;
  234. htp = t;
  235. } else {
  236. cache->put(bundleID, (void*)htp, status);
  237. umtx_unlock(nullptr);
  238. }
  239. }
  240. }
  241. return htp;
  242. }
  243. bool
  244. LocaleUtility::isFallbackOf(const UnicodeString& root, const UnicodeString& child)
  245. {
  246. return child.indexOf(root) == 0 &&
  247. (child.length() == root.length() ||
  248. child.charAt(root.length()) == UNDERSCORE_CHAR);
  249. }
  250. U_NAMESPACE_END
  251. /* !UCONFIG_NO_SERVICE */
  252. #endif