characterproperties.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // © 2018 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. // characterproperties.cpp
  4. // created: 2018sep03 Markus W. Scherer
  5. #include "unicode/utypes.h"
  6. #include "unicode/localpointer.h"
  7. #include "unicode/uchar.h"
  8. #include "unicode/ucpmap.h"
  9. #include "unicode/ucptrie.h"
  10. #include "unicode/umutablecptrie.h"
  11. #include "unicode/uniset.h"
  12. #include "unicode/uscript.h"
  13. #include "unicode/uset.h"
  14. #include "cmemory.h"
  15. #include "emojiprops.h"
  16. #include "mutex.h"
  17. #include "normalizer2impl.h"
  18. #include "uassert.h"
  19. #include "ubidi_props.h"
  20. #include "ucase.h"
  21. #include "ucln_cmn.h"
  22. #include "umutex.h"
  23. #include "uprops.h"
  24. using icu::LocalPointer;
  25. #if !UCONFIG_NO_NORMALIZATION
  26. using icu::Normalizer2Factory;
  27. using icu::Normalizer2Impl;
  28. #endif
  29. using icu::UInitOnce;
  30. using icu::UnicodeSet;
  31. namespace {
  32. UBool U_CALLCONV characterproperties_cleanup();
  33. constexpr int32_t NUM_INCLUSIONS = UPROPS_SRC_COUNT + (UCHAR_INT_LIMIT - UCHAR_INT_START);
  34. struct Inclusion {
  35. UnicodeSet *fSet = nullptr;
  36. UInitOnce fInitOnce {};
  37. };
  38. Inclusion gInclusions[NUM_INCLUSIONS]; // cached getInclusions()
  39. UnicodeSet *sets[UCHAR_BINARY_LIMIT] = {};
  40. UCPMap *maps[UCHAR_INT_LIMIT - UCHAR_INT_START] = {};
  41. icu::UMutex cpMutex;
  42. //----------------------------------------------------------------
  43. // Inclusions list
  44. //----------------------------------------------------------------
  45. // USetAdder implementation
  46. // Does not use uset.h to reduce code dependencies
  47. void U_CALLCONV
  48. _set_add(USet *set, UChar32 c) {
  49. ((UnicodeSet *)set)->add(c);
  50. }
  51. void U_CALLCONV
  52. _set_addRange(USet *set, UChar32 start, UChar32 end) {
  53. ((UnicodeSet *)set)->add(start, end);
  54. }
  55. void U_CALLCONV
  56. _set_addString(USet *set, const char16_t *str, int32_t length) {
  57. ((UnicodeSet *)set)->add(icu::UnicodeString((UBool)(length<0), str, length));
  58. }
  59. UBool U_CALLCONV characterproperties_cleanup() {
  60. for (Inclusion &in: gInclusions) {
  61. delete in.fSet;
  62. in.fSet = nullptr;
  63. in.fInitOnce.reset();
  64. }
  65. for (int32_t i = 0; i < UPRV_LENGTHOF(sets); ++i) {
  66. delete sets[i];
  67. sets[i] = nullptr;
  68. }
  69. for (int32_t i = 0; i < UPRV_LENGTHOF(maps); ++i) {
  70. ucptrie_close(reinterpret_cast<UCPTrie *>(maps[i]));
  71. maps[i] = nullptr;
  72. }
  73. return true;
  74. }
  75. void U_CALLCONV initInclusion(UPropertySource src, UErrorCode &errorCode) {
  76. // This function is invoked only via umtx_initOnce().
  77. U_ASSERT(0 <= src && src < UPROPS_SRC_COUNT);
  78. if (src == UPROPS_SRC_NONE) {
  79. errorCode = U_INTERNAL_PROGRAM_ERROR;
  80. return;
  81. }
  82. U_ASSERT(gInclusions[src].fSet == nullptr);
  83. LocalPointer<UnicodeSet> incl(new UnicodeSet());
  84. if (incl.isNull()) {
  85. errorCode = U_MEMORY_ALLOCATION_ERROR;
  86. return;
  87. }
  88. USetAdder sa = {
  89. (USet *)incl.getAlias(),
  90. _set_add,
  91. _set_addRange,
  92. _set_addString,
  93. nullptr, // don't need remove()
  94. nullptr // don't need removeRange()
  95. };
  96. switch(src) {
  97. case UPROPS_SRC_CHAR:
  98. uchar_addPropertyStarts(&sa, &errorCode);
  99. break;
  100. case UPROPS_SRC_PROPSVEC:
  101. upropsvec_addPropertyStarts(&sa, &errorCode);
  102. break;
  103. case UPROPS_SRC_CHAR_AND_PROPSVEC:
  104. uchar_addPropertyStarts(&sa, &errorCode);
  105. upropsvec_addPropertyStarts(&sa, &errorCode);
  106. break;
  107. #if !UCONFIG_NO_NORMALIZATION
  108. case UPROPS_SRC_CASE_AND_NORM: {
  109. const Normalizer2Impl *impl=Normalizer2Factory::getNFCImpl(errorCode);
  110. if(U_SUCCESS(errorCode)) {
  111. impl->addPropertyStarts(&sa, errorCode);
  112. }
  113. ucase_addPropertyStarts(&sa, &errorCode);
  114. break;
  115. }
  116. case UPROPS_SRC_NFC: {
  117. const Normalizer2Impl *impl=Normalizer2Factory::getNFCImpl(errorCode);
  118. if(U_SUCCESS(errorCode)) {
  119. impl->addPropertyStarts(&sa, errorCode);
  120. }
  121. break;
  122. }
  123. case UPROPS_SRC_NFKC: {
  124. const Normalizer2Impl *impl=Normalizer2Factory::getNFKCImpl(errorCode);
  125. if(U_SUCCESS(errorCode)) {
  126. impl->addPropertyStarts(&sa, errorCode);
  127. }
  128. break;
  129. }
  130. case UPROPS_SRC_NFKC_CF: {
  131. const Normalizer2Impl *impl=Normalizer2Factory::getNFKC_CFImpl(errorCode);
  132. if(U_SUCCESS(errorCode)) {
  133. impl->addPropertyStarts(&sa, errorCode);
  134. }
  135. break;
  136. }
  137. case UPROPS_SRC_NFC_CANON_ITER: {
  138. const Normalizer2Impl *impl=Normalizer2Factory::getNFCImpl(errorCode);
  139. if(U_SUCCESS(errorCode)) {
  140. impl->addCanonIterPropertyStarts(&sa, errorCode);
  141. }
  142. break;
  143. }
  144. #endif
  145. case UPROPS_SRC_CASE:
  146. ucase_addPropertyStarts(&sa, &errorCode);
  147. break;
  148. case UPROPS_SRC_BIDI:
  149. ubidi_addPropertyStarts(&sa, &errorCode);
  150. break;
  151. case UPROPS_SRC_INPC:
  152. case UPROPS_SRC_INSC:
  153. case UPROPS_SRC_VO:
  154. uprops_addPropertyStarts((UPropertySource)src, &sa, &errorCode);
  155. break;
  156. case UPROPS_SRC_EMOJI: {
  157. const icu::EmojiProps *ep = icu::EmojiProps::getSingleton(errorCode);
  158. if (U_SUCCESS(errorCode)) {
  159. ep->addPropertyStarts(&sa, errorCode);
  160. }
  161. break;
  162. }
  163. default:
  164. errorCode = U_INTERNAL_PROGRAM_ERROR;
  165. break;
  166. }
  167. if (U_FAILURE(errorCode)) {
  168. return;
  169. }
  170. if (incl->isBogus()) {
  171. errorCode = U_MEMORY_ALLOCATION_ERROR;
  172. return;
  173. }
  174. // Compact for caching.
  175. incl->compact();
  176. gInclusions[src].fSet = incl.orphan();
  177. ucln_common_registerCleanup(UCLN_COMMON_CHARACTERPROPERTIES, characterproperties_cleanup);
  178. }
  179. const UnicodeSet *getInclusionsForSource(UPropertySource src, UErrorCode &errorCode) {
  180. if (U_FAILURE(errorCode)) { return nullptr; }
  181. if (src < 0 || UPROPS_SRC_COUNT <= src) {
  182. errorCode = U_ILLEGAL_ARGUMENT_ERROR;
  183. return nullptr;
  184. }
  185. Inclusion &i = gInclusions[src];
  186. umtx_initOnce(i.fInitOnce, &initInclusion, src, errorCode);
  187. return i.fSet;
  188. }
  189. void U_CALLCONV initIntPropInclusion(UProperty prop, UErrorCode &errorCode) {
  190. // This function is invoked only via umtx_initOnce().
  191. U_ASSERT(UCHAR_INT_START <= prop && prop < UCHAR_INT_LIMIT);
  192. int32_t inclIndex = UPROPS_SRC_COUNT + (prop - UCHAR_INT_START);
  193. U_ASSERT(gInclusions[inclIndex].fSet == nullptr);
  194. UPropertySource src = uprops_getSource(prop);
  195. const UnicodeSet *incl = getInclusionsForSource(src, errorCode);
  196. if (U_FAILURE(errorCode)) {
  197. return;
  198. }
  199. LocalPointer<UnicodeSet> intPropIncl(new UnicodeSet(0, 0));
  200. if (intPropIncl.isNull()) {
  201. errorCode = U_MEMORY_ALLOCATION_ERROR;
  202. return;
  203. }
  204. int32_t numRanges = incl->getRangeCount();
  205. int32_t prevValue = 0;
  206. for (int32_t i = 0; i < numRanges; ++i) {
  207. UChar32 rangeEnd = incl->getRangeEnd(i);
  208. for (UChar32 c = incl->getRangeStart(i); c <= rangeEnd; ++c) {
  209. // TODO: Get a UCharacterProperty.IntProperty to avoid the property dispatch.
  210. int32_t value = u_getIntPropertyValue(c, prop);
  211. if (value != prevValue) {
  212. intPropIncl->add(c);
  213. prevValue = value;
  214. }
  215. }
  216. }
  217. if (intPropIncl->isBogus()) {
  218. errorCode = U_MEMORY_ALLOCATION_ERROR;
  219. return;
  220. }
  221. // Compact for caching.
  222. intPropIncl->compact();
  223. gInclusions[inclIndex].fSet = intPropIncl.orphan();
  224. ucln_common_registerCleanup(UCLN_COMMON_CHARACTERPROPERTIES, characterproperties_cleanup);
  225. }
  226. } // namespace
  227. U_NAMESPACE_BEGIN
  228. const UnicodeSet *CharacterProperties::getInclusionsForProperty(
  229. UProperty prop, UErrorCode &errorCode) {
  230. if (U_FAILURE(errorCode)) { return nullptr; }
  231. if (UCHAR_INT_START <= prop && prop < UCHAR_INT_LIMIT) {
  232. int32_t inclIndex = UPROPS_SRC_COUNT + (prop - UCHAR_INT_START);
  233. Inclusion &i = gInclusions[inclIndex];
  234. umtx_initOnce(i.fInitOnce, &initIntPropInclusion, prop, errorCode);
  235. return i.fSet;
  236. } else {
  237. UPropertySource src = uprops_getSource(prop);
  238. return getInclusionsForSource(src, errorCode);
  239. }
  240. }
  241. U_NAMESPACE_END
  242. namespace {
  243. UnicodeSet *makeSet(UProperty property, UErrorCode &errorCode) {
  244. if (U_FAILURE(errorCode)) { return nullptr; }
  245. LocalPointer<UnicodeSet> set(new UnicodeSet());
  246. if (set.isNull()) {
  247. errorCode = U_MEMORY_ALLOCATION_ERROR;
  248. return nullptr;
  249. }
  250. if (UCHAR_BASIC_EMOJI <= property && property <= UCHAR_RGI_EMOJI) {
  251. // property of strings
  252. const icu::EmojiProps *ep = icu::EmojiProps::getSingleton(errorCode);
  253. if (U_FAILURE(errorCode)) { return nullptr; }
  254. USetAdder sa = {
  255. (USet *)set.getAlias(),
  256. _set_add,
  257. _set_addRange,
  258. _set_addString,
  259. nullptr, // don't need remove()
  260. nullptr // don't need removeRange()
  261. };
  262. ep->addStrings(&sa, property, errorCode);
  263. if (property != UCHAR_BASIC_EMOJI && property != UCHAR_RGI_EMOJI) {
  264. // property of _only_ strings
  265. set->freeze();
  266. return set.orphan();
  267. }
  268. }
  269. const UnicodeSet *inclusions =
  270. icu::CharacterProperties::getInclusionsForProperty(property, errorCode);
  271. if (U_FAILURE(errorCode)) { return nullptr; }
  272. int32_t numRanges = inclusions->getRangeCount();
  273. UChar32 startHasProperty = -1;
  274. for (int32_t i = 0; i < numRanges; ++i) {
  275. UChar32 rangeEnd = inclusions->getRangeEnd(i);
  276. for (UChar32 c = inclusions->getRangeStart(i); c <= rangeEnd; ++c) {
  277. // TODO: Get a UCharacterProperty.BinaryProperty to avoid the property dispatch.
  278. if (u_hasBinaryProperty(c, property)) {
  279. if (startHasProperty < 0) {
  280. // Transition from false to true.
  281. startHasProperty = c;
  282. }
  283. } else if (startHasProperty >= 0) {
  284. // Transition from true to false.
  285. set->add(startHasProperty, c - 1);
  286. startHasProperty = -1;
  287. }
  288. }
  289. }
  290. if (startHasProperty >= 0) {
  291. set->add(startHasProperty, 0x10FFFF);
  292. }
  293. set->freeze();
  294. return set.orphan();
  295. }
  296. UCPMap *makeMap(UProperty property, UErrorCode &errorCode) {
  297. if (U_FAILURE(errorCode)) { return nullptr; }
  298. uint32_t nullValue = property == UCHAR_SCRIPT ? USCRIPT_UNKNOWN : 0;
  299. icu::LocalUMutableCPTriePointer mutableTrie(
  300. umutablecptrie_open(nullValue, nullValue, &errorCode));
  301. const UnicodeSet *inclusions =
  302. icu::CharacterProperties::getInclusionsForProperty(property, errorCode);
  303. if (U_FAILURE(errorCode)) { return nullptr; }
  304. int32_t numRanges = inclusions->getRangeCount();
  305. UChar32 start = 0;
  306. uint32_t value = nullValue;
  307. for (int32_t i = 0; i < numRanges; ++i) {
  308. UChar32 rangeEnd = inclusions->getRangeEnd(i);
  309. for (UChar32 c = inclusions->getRangeStart(i); c <= rangeEnd; ++c) {
  310. // TODO: Get a UCharacterProperty.IntProperty to avoid the property dispatch.
  311. uint32_t nextValue = u_getIntPropertyValue(c, property);
  312. if (value != nextValue) {
  313. if (value != nullValue) {
  314. umutablecptrie_setRange(mutableTrie.getAlias(), start, c - 1, value, &errorCode);
  315. }
  316. start = c;
  317. value = nextValue;
  318. }
  319. }
  320. }
  321. if (value != 0) {
  322. umutablecptrie_setRange(mutableTrie.getAlias(), start, 0x10FFFF, value, &errorCode);
  323. }
  324. UCPTrieType type;
  325. if (property == UCHAR_BIDI_CLASS || property == UCHAR_GENERAL_CATEGORY) {
  326. type = UCPTRIE_TYPE_FAST;
  327. } else {
  328. type = UCPTRIE_TYPE_SMALL;
  329. }
  330. UCPTrieValueWidth valueWidth;
  331. // TODO: UCharacterProperty.IntProperty
  332. int32_t max = u_getIntPropertyMaxValue(property);
  333. if (max <= 0xff) {
  334. valueWidth = UCPTRIE_VALUE_BITS_8;
  335. } else if (max <= 0xffff) {
  336. valueWidth = UCPTRIE_VALUE_BITS_16;
  337. } else {
  338. valueWidth = UCPTRIE_VALUE_BITS_32;
  339. }
  340. return reinterpret_cast<UCPMap *>(
  341. umutablecptrie_buildImmutable(mutableTrie.getAlias(), type, valueWidth, &errorCode));
  342. }
  343. } // namespace
  344. U_NAMESPACE_BEGIN
  345. const UnicodeSet *CharacterProperties::getBinaryPropertySet(UProperty property, UErrorCode &errorCode) {
  346. if (U_FAILURE(errorCode)) { return nullptr; }
  347. if (property < 0 || UCHAR_BINARY_LIMIT <= property) {
  348. errorCode = U_ILLEGAL_ARGUMENT_ERROR;
  349. return nullptr;
  350. }
  351. Mutex m(&cpMutex);
  352. UnicodeSet *set = sets[property];
  353. if (set == nullptr) {
  354. sets[property] = set = makeSet(property, errorCode);
  355. }
  356. return set;
  357. }
  358. U_NAMESPACE_END
  359. U_NAMESPACE_USE
  360. U_CAPI const USet * U_EXPORT2
  361. u_getBinaryPropertySet(UProperty property, UErrorCode *pErrorCode) {
  362. const UnicodeSet *set = CharacterProperties::getBinaryPropertySet(property, *pErrorCode);
  363. return U_SUCCESS(*pErrorCode) ? set->toUSet() : nullptr;
  364. }
  365. U_CAPI const UCPMap * U_EXPORT2
  366. u_getIntPropertyMap(UProperty property, UErrorCode *pErrorCode) {
  367. if (U_FAILURE(*pErrorCode)) { return nullptr; }
  368. if (property < UCHAR_INT_START || UCHAR_INT_LIMIT <= property) {
  369. *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
  370. return nullptr;
  371. }
  372. Mutex m(&cpMutex);
  373. UCPMap *map = maps[property - UCHAR_INT_START];
  374. if (map == nullptr) {
  375. maps[property - UCHAR_INT_START] = map = makeMap(property, *pErrorCode);
  376. }
  377. return map;
  378. }