characterproperties.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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(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. case UPROPS_SRC_IDSU:
  164. // New in Unicode 15.1 for just two characters.
  165. sa.add(sa.set, 0x2FFE);
  166. sa.add(sa.set, 0x2FFF + 1);
  167. break;
  168. case UPROPS_SRC_ID_COMPAT_MATH:
  169. uprops_addPropertyStarts(src, &sa, &errorCode);
  170. break;
  171. default:
  172. errorCode = U_INTERNAL_PROGRAM_ERROR;
  173. break;
  174. }
  175. if (U_FAILURE(errorCode)) {
  176. return;
  177. }
  178. if (incl->isBogus()) {
  179. errorCode = U_MEMORY_ALLOCATION_ERROR;
  180. return;
  181. }
  182. // Compact for caching.
  183. incl->compact();
  184. gInclusions[src].fSet = incl.orphan();
  185. ucln_common_registerCleanup(UCLN_COMMON_CHARACTERPROPERTIES, characterproperties_cleanup);
  186. }
  187. const UnicodeSet *getInclusionsForSource(UPropertySource src, UErrorCode &errorCode) {
  188. if (U_FAILURE(errorCode)) { return nullptr; }
  189. if (src < 0 || UPROPS_SRC_COUNT <= src) {
  190. errorCode = U_ILLEGAL_ARGUMENT_ERROR;
  191. return nullptr;
  192. }
  193. Inclusion &i = gInclusions[src];
  194. umtx_initOnce(i.fInitOnce, &initInclusion, src, errorCode);
  195. return i.fSet;
  196. }
  197. void U_CALLCONV initIntPropInclusion(UProperty prop, UErrorCode &errorCode) {
  198. // This function is invoked only via umtx_initOnce().
  199. U_ASSERT(UCHAR_INT_START <= prop && prop < UCHAR_INT_LIMIT);
  200. int32_t inclIndex = UPROPS_SRC_COUNT + (prop - UCHAR_INT_START);
  201. U_ASSERT(gInclusions[inclIndex].fSet == nullptr);
  202. UPropertySource src = uprops_getSource(prop);
  203. const UnicodeSet *incl = getInclusionsForSource(src, errorCode);
  204. if (U_FAILURE(errorCode)) {
  205. return;
  206. }
  207. LocalPointer<UnicodeSet> intPropIncl(new UnicodeSet(0, 0));
  208. if (intPropIncl.isNull()) {
  209. errorCode = U_MEMORY_ALLOCATION_ERROR;
  210. return;
  211. }
  212. int32_t numRanges = incl->getRangeCount();
  213. int32_t prevValue = 0;
  214. for (int32_t i = 0; i < numRanges; ++i) {
  215. UChar32 rangeEnd = incl->getRangeEnd(i);
  216. for (UChar32 c = incl->getRangeStart(i); c <= rangeEnd; ++c) {
  217. // TODO: Get a UCharacterProperty.IntProperty to avoid the property dispatch.
  218. int32_t value = u_getIntPropertyValue(c, prop);
  219. if (value != prevValue) {
  220. intPropIncl->add(c);
  221. prevValue = value;
  222. }
  223. }
  224. }
  225. if (intPropIncl->isBogus()) {
  226. errorCode = U_MEMORY_ALLOCATION_ERROR;
  227. return;
  228. }
  229. // Compact for caching.
  230. intPropIncl->compact();
  231. gInclusions[inclIndex].fSet = intPropIncl.orphan();
  232. ucln_common_registerCleanup(UCLN_COMMON_CHARACTERPROPERTIES, characterproperties_cleanup);
  233. }
  234. } // namespace
  235. U_NAMESPACE_BEGIN
  236. const UnicodeSet *CharacterProperties::getInclusionsForProperty(
  237. UProperty prop, UErrorCode &errorCode) {
  238. if (U_FAILURE(errorCode)) { return nullptr; }
  239. if (UCHAR_INT_START <= prop && prop < UCHAR_INT_LIMIT) {
  240. int32_t inclIndex = UPROPS_SRC_COUNT + (prop - UCHAR_INT_START);
  241. Inclusion &i = gInclusions[inclIndex];
  242. umtx_initOnce(i.fInitOnce, &initIntPropInclusion, prop, errorCode);
  243. return i.fSet;
  244. } else {
  245. UPropertySource src = uprops_getSource(prop);
  246. return getInclusionsForSource(src, errorCode);
  247. }
  248. }
  249. U_NAMESPACE_END
  250. namespace {
  251. UnicodeSet *makeSet(UProperty property, UErrorCode &errorCode) {
  252. if (U_FAILURE(errorCode)) { return nullptr; }
  253. LocalPointer<UnicodeSet> set(new UnicodeSet());
  254. if (set.isNull()) {
  255. errorCode = U_MEMORY_ALLOCATION_ERROR;
  256. return nullptr;
  257. }
  258. if (UCHAR_BASIC_EMOJI <= property && property <= UCHAR_RGI_EMOJI) {
  259. // property of strings
  260. const icu::EmojiProps *ep = icu::EmojiProps::getSingleton(errorCode);
  261. if (U_FAILURE(errorCode)) { return nullptr; }
  262. USetAdder sa = {
  263. (USet *)set.getAlias(),
  264. _set_add,
  265. _set_addRange,
  266. _set_addString,
  267. nullptr, // don't need remove()
  268. nullptr // don't need removeRange()
  269. };
  270. ep->addStrings(&sa, property, errorCode);
  271. if (property != UCHAR_BASIC_EMOJI && property != UCHAR_RGI_EMOJI) {
  272. // property of _only_ strings
  273. set->freeze();
  274. return set.orphan();
  275. }
  276. }
  277. const UnicodeSet *inclusions =
  278. icu::CharacterProperties::getInclusionsForProperty(property, errorCode);
  279. if (U_FAILURE(errorCode)) { return nullptr; }
  280. int32_t numRanges = inclusions->getRangeCount();
  281. UChar32 startHasProperty = -1;
  282. for (int32_t i = 0; i < numRanges; ++i) {
  283. UChar32 rangeEnd = inclusions->getRangeEnd(i);
  284. for (UChar32 c = inclusions->getRangeStart(i); c <= rangeEnd; ++c) {
  285. // TODO: Get a UCharacterProperty.BinaryProperty to avoid the property dispatch.
  286. if (u_hasBinaryProperty(c, property)) {
  287. if (startHasProperty < 0) {
  288. // Transition from false to true.
  289. startHasProperty = c;
  290. }
  291. } else if (startHasProperty >= 0) {
  292. // Transition from true to false.
  293. set->add(startHasProperty, c - 1);
  294. startHasProperty = -1;
  295. }
  296. }
  297. }
  298. if (startHasProperty >= 0) {
  299. set->add(startHasProperty, 0x10FFFF);
  300. }
  301. set->freeze();
  302. return set.orphan();
  303. }
  304. UCPMap *makeMap(UProperty property, UErrorCode &errorCode) {
  305. if (U_FAILURE(errorCode)) { return nullptr; }
  306. uint32_t nullValue = property == UCHAR_SCRIPT ? USCRIPT_UNKNOWN : 0;
  307. icu::LocalUMutableCPTriePointer mutableTrie(
  308. umutablecptrie_open(nullValue, nullValue, &errorCode));
  309. const UnicodeSet *inclusions =
  310. icu::CharacterProperties::getInclusionsForProperty(property, errorCode);
  311. if (U_FAILURE(errorCode)) { return nullptr; }
  312. int32_t numRanges = inclusions->getRangeCount();
  313. UChar32 start = 0;
  314. uint32_t value = nullValue;
  315. for (int32_t i = 0; i < numRanges; ++i) {
  316. UChar32 rangeEnd = inclusions->getRangeEnd(i);
  317. for (UChar32 c = inclusions->getRangeStart(i); c <= rangeEnd; ++c) {
  318. // TODO: Get a UCharacterProperty.IntProperty to avoid the property dispatch.
  319. uint32_t nextValue = u_getIntPropertyValue(c, property);
  320. if (value != nextValue) {
  321. if (value != nullValue) {
  322. umutablecptrie_setRange(mutableTrie.getAlias(), start, c - 1, value, &errorCode);
  323. }
  324. start = c;
  325. value = nextValue;
  326. }
  327. }
  328. }
  329. if (value != 0) {
  330. umutablecptrie_setRange(mutableTrie.getAlias(), start, 0x10FFFF, value, &errorCode);
  331. }
  332. UCPTrieType type;
  333. if (property == UCHAR_BIDI_CLASS || property == UCHAR_GENERAL_CATEGORY) {
  334. type = UCPTRIE_TYPE_FAST;
  335. } else {
  336. type = UCPTRIE_TYPE_SMALL;
  337. }
  338. UCPTrieValueWidth valueWidth;
  339. // TODO: UCharacterProperty.IntProperty
  340. int32_t max = u_getIntPropertyMaxValue(property);
  341. if (max <= 0xff) {
  342. valueWidth = UCPTRIE_VALUE_BITS_8;
  343. } else if (max <= 0xffff) {
  344. valueWidth = UCPTRIE_VALUE_BITS_16;
  345. } else {
  346. valueWidth = UCPTRIE_VALUE_BITS_32;
  347. }
  348. return reinterpret_cast<UCPMap *>(
  349. umutablecptrie_buildImmutable(mutableTrie.getAlias(), type, valueWidth, &errorCode));
  350. }
  351. } // namespace
  352. U_NAMESPACE_BEGIN
  353. const UnicodeSet *CharacterProperties::getBinaryPropertySet(UProperty property, UErrorCode &errorCode) {
  354. if (U_FAILURE(errorCode)) { return nullptr; }
  355. if (property < 0 || UCHAR_BINARY_LIMIT <= property) {
  356. errorCode = U_ILLEGAL_ARGUMENT_ERROR;
  357. return nullptr;
  358. }
  359. Mutex m(&cpMutex);
  360. UnicodeSet *set = sets[property];
  361. if (set == nullptr) {
  362. sets[property] = set = makeSet(property, errorCode);
  363. }
  364. return set;
  365. }
  366. U_NAMESPACE_END
  367. U_NAMESPACE_USE
  368. U_CAPI const USet * U_EXPORT2
  369. u_getBinaryPropertySet(UProperty property, UErrorCode *pErrorCode) {
  370. const UnicodeSet *set = CharacterProperties::getBinaryPropertySet(property, *pErrorCode);
  371. return U_SUCCESS(*pErrorCode) ? set->toUSet() : nullptr;
  372. }
  373. U_CAPI const UCPMap * U_EXPORT2
  374. u_getIntPropertyMap(UProperty property, UErrorCode *pErrorCode) {
  375. if (U_FAILURE(*pErrorCode)) { return nullptr; }
  376. if (property < UCHAR_INT_START || UCHAR_INT_LIMIT <= property) {
  377. *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
  378. return nullptr;
  379. }
  380. Mutex m(&cpMutex);
  381. UCPMap *map = maps[property - UCHAR_INT_START];
  382. if (map == nullptr) {
  383. maps[property - UCHAR_INT_START] = map = makeMap(property, *pErrorCode);
  384. }
  385. return map;
  386. }