uprops.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. *
  6. * Copyright (C) 2002-2016, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. *******************************************************************************
  10. * file name: uprops.cpp
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2002feb24
  16. * created by: Markus W. Scherer
  17. *
  18. * Implementations for mostly non-core Unicode character properties
  19. * stored in uprops.icu.
  20. *
  21. * With the APIs implemented here, almost all properties files and
  22. * their associated implementation files are used from this file,
  23. * including those for normalization and case mappings.
  24. */
  25. #include "unicode/utypes.h"
  26. #include "unicode/uchar.h"
  27. #include "unicode/ucptrie.h"
  28. #include "unicode/udata.h"
  29. #include "unicode/unorm2.h"
  30. #include "unicode/uscript.h"
  31. #include "unicode/ustring.h"
  32. #include "unicode/utf16.h"
  33. #include "cstring.h"
  34. #include "emojiprops.h"
  35. #include "mutex.h"
  36. #include "normalizer2impl.h"
  37. #include "umutex.h"
  38. #include "ubidi_props.h"
  39. #include "uprops.h"
  40. #include "ucase.h"
  41. #include "ucln_cmn.h"
  42. #include "ulayout_props.h"
  43. #include "ustr_imp.h"
  44. U_NAMESPACE_USE
  45. // Unicode text layout properties data -----------------------------------------
  46. namespace {
  47. icu::UInitOnce gLayoutInitOnce {};
  48. UDataMemory *gLayoutMemory = nullptr;
  49. UCPTrie *gInpcTrie = nullptr; // Indic_Positional_Category
  50. UCPTrie *gInscTrie = nullptr; // Indic_Syllabic_Category
  51. UCPTrie *gVoTrie = nullptr; // Vertical_Orientation
  52. int32_t gMaxInpcValue = 0;
  53. int32_t gMaxInscValue = 0;
  54. int32_t gMaxVoValue = 0;
  55. UBool U_CALLCONV uprops_cleanup() {
  56. udata_close(gLayoutMemory);
  57. gLayoutMemory = nullptr;
  58. ucptrie_close(gInpcTrie);
  59. gInpcTrie = nullptr;
  60. ucptrie_close(gInscTrie);
  61. gInscTrie = nullptr;
  62. ucptrie_close(gVoTrie);
  63. gVoTrie = nullptr;
  64. gMaxInpcValue = 0;
  65. gMaxInscValue = 0;
  66. gMaxVoValue = 0;
  67. gLayoutInitOnce.reset();
  68. return true;
  69. }
  70. UBool U_CALLCONV
  71. ulayout_isAcceptable(void * /*context*/,
  72. const char * /* type */, const char * /*name*/,
  73. const UDataInfo *pInfo) {
  74. return pInfo->size >= 20 &&
  75. pInfo->isBigEndian == U_IS_BIG_ENDIAN &&
  76. pInfo->charsetFamily == U_CHARSET_FAMILY &&
  77. pInfo->dataFormat[0] == ULAYOUT_FMT_0 &&
  78. pInfo->dataFormat[1] == ULAYOUT_FMT_1 &&
  79. pInfo->dataFormat[2] == ULAYOUT_FMT_2 &&
  80. pInfo->dataFormat[3] == ULAYOUT_FMT_3 &&
  81. pInfo->formatVersion[0] == 1;
  82. }
  83. // UInitOnce singleton initialization function
  84. void U_CALLCONV ulayout_load(UErrorCode &errorCode) {
  85. gLayoutMemory = udata_openChoice(
  86. nullptr, ULAYOUT_DATA_TYPE, ULAYOUT_DATA_NAME,
  87. ulayout_isAcceptable, nullptr, &errorCode);
  88. if (U_FAILURE(errorCode)) { return; }
  89. const uint8_t *inBytes = (const uint8_t *)udata_getMemory(gLayoutMemory);
  90. const int32_t *inIndexes = (const int32_t *)inBytes;
  91. int32_t indexesLength = inIndexes[ULAYOUT_IX_INDEXES_LENGTH];
  92. if (indexesLength < 12) {
  93. errorCode = U_INVALID_FORMAT_ERROR; // Not enough indexes.
  94. return;
  95. }
  96. int32_t offset = indexesLength * 4;
  97. int32_t top = inIndexes[ULAYOUT_IX_INPC_TRIE_TOP];
  98. int32_t trieSize = top - offset;
  99. if (trieSize >= 16) {
  100. gInpcTrie = ucptrie_openFromBinary(
  101. UCPTRIE_TYPE_ANY, UCPTRIE_VALUE_BITS_ANY,
  102. inBytes + offset, trieSize, nullptr, &errorCode);
  103. }
  104. offset = top;
  105. top = inIndexes[ULAYOUT_IX_INSC_TRIE_TOP];
  106. trieSize = top - offset;
  107. if (trieSize >= 16) {
  108. gInscTrie = ucptrie_openFromBinary(
  109. UCPTRIE_TYPE_ANY, UCPTRIE_VALUE_BITS_ANY,
  110. inBytes + offset, trieSize, nullptr, &errorCode);
  111. }
  112. offset = top;
  113. top = inIndexes[ULAYOUT_IX_VO_TRIE_TOP];
  114. trieSize = top - offset;
  115. if (trieSize >= 16) {
  116. gVoTrie = ucptrie_openFromBinary(
  117. UCPTRIE_TYPE_ANY, UCPTRIE_VALUE_BITS_ANY,
  118. inBytes + offset, trieSize, nullptr, &errorCode);
  119. }
  120. uint32_t maxValues = inIndexes[ULAYOUT_IX_MAX_VALUES];
  121. gMaxInpcValue = maxValues >> ULAYOUT_MAX_INPC_SHIFT;
  122. gMaxInscValue = (maxValues >> ULAYOUT_MAX_INSC_SHIFT) & 0xff;
  123. gMaxVoValue = (maxValues >> ULAYOUT_MAX_VO_SHIFT) & 0xff;
  124. ucln_common_registerCleanup(UCLN_COMMON_UPROPS, uprops_cleanup);
  125. }
  126. UBool ulayout_ensureData(UErrorCode &errorCode) {
  127. if (U_FAILURE(errorCode)) { return false; }
  128. umtx_initOnce(gLayoutInitOnce, &ulayout_load, errorCode);
  129. return U_SUCCESS(errorCode);
  130. }
  131. UBool ulayout_ensureData() {
  132. UErrorCode errorCode = U_ZERO_ERROR;
  133. return ulayout_ensureData(errorCode);
  134. }
  135. } // namespace
  136. /* general properties API functions ----------------------------------------- */
  137. struct BinaryProperty;
  138. typedef UBool BinaryPropertyContains(const BinaryProperty &prop, UChar32 c, UProperty which);
  139. struct BinaryProperty {
  140. int32_t column; // SRC_PROPSVEC column, or "source" if mask==0
  141. uint32_t mask;
  142. BinaryPropertyContains *contains;
  143. };
  144. static UBool defaultContains(const BinaryProperty &prop, UChar32 c, UProperty /*which*/) {
  145. /* systematic, directly stored properties */
  146. return (u_getUnicodeProperties(c, prop.column)&prop.mask)!=0;
  147. }
  148. static UBool caseBinaryPropertyContains(const BinaryProperty &/*prop*/, UChar32 c, UProperty which) {
  149. return static_cast<UBool>(ucase_hasBinaryProperty(c, which));
  150. }
  151. static UBool isBidiControl(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  152. return ubidi_isBidiControl(c);
  153. }
  154. static UBool isMirrored(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  155. return ubidi_isMirrored(c);
  156. }
  157. static UBool isJoinControl(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  158. return ubidi_isJoinControl(c);
  159. }
  160. #if UCONFIG_NO_NORMALIZATION
  161. static UBool hasFullCompositionExclusion(const BinaryProperty &, UChar32, UProperty) {
  162. return false;
  163. }
  164. #else
  165. static UBool hasFullCompositionExclusion(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  166. // By definition, Full_Composition_Exclusion is the same as NFC_QC=No.
  167. UErrorCode errorCode=U_ZERO_ERROR;
  168. const Normalizer2Impl *impl=Normalizer2Factory::getNFCImpl(errorCode);
  169. return U_SUCCESS(errorCode) && impl->isCompNo(impl->getNorm16(c));
  170. }
  171. #endif
  172. // UCHAR_NF*_INERT properties
  173. #if UCONFIG_NO_NORMALIZATION
  174. static UBool isNormInert(const BinaryProperty &, UChar32, UProperty) {
  175. return false;
  176. }
  177. #else
  178. static UBool isNormInert(const BinaryProperty &/*prop*/, UChar32 c, UProperty which) {
  179. UErrorCode errorCode=U_ZERO_ERROR;
  180. const Normalizer2 *norm2=Normalizer2Factory::getInstance(
  181. (UNormalizationMode)(which-UCHAR_NFD_INERT+UNORM_NFD), errorCode);
  182. return U_SUCCESS(errorCode) && norm2->isInert(c);
  183. }
  184. #endif
  185. #if UCONFIG_NO_NORMALIZATION
  186. static UBool changesWhenCasefolded(const BinaryProperty &, UChar32, UProperty) {
  187. return false;
  188. }
  189. #else
  190. static UBool changesWhenCasefolded(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  191. UnicodeString nfd;
  192. UErrorCode errorCode=U_ZERO_ERROR;
  193. const Normalizer2 *nfcNorm2=Normalizer2::getNFCInstance(errorCode);
  194. if(U_FAILURE(errorCode)) {
  195. return false;
  196. }
  197. if(nfcNorm2->getDecomposition(c, nfd)) {
  198. /* c has a decomposition */
  199. if(nfd.length()==1) {
  200. c=nfd[0]; /* single BMP code point */
  201. } else if(nfd.length()<=U16_MAX_LENGTH &&
  202. nfd.length()==U16_LENGTH(c=nfd.char32At(0))
  203. ) {
  204. /* single supplementary code point */
  205. } else {
  206. c=U_SENTINEL;
  207. }
  208. } else if(c<0) {
  209. return false; /* protect against bad input */
  210. }
  211. if(c>=0) {
  212. /* single code point */
  213. const char16_t *resultString;
  214. return (UBool)(ucase_toFullFolding(c, &resultString, U_FOLD_CASE_DEFAULT)>=0);
  215. } else {
  216. /* guess some large but stack-friendly capacity */
  217. char16_t dest[2*UCASE_MAX_STRING_LENGTH];
  218. int32_t destLength;
  219. destLength=u_strFoldCase(dest, UPRV_LENGTHOF(dest),
  220. nfd.getBuffer(), nfd.length(),
  221. U_FOLD_CASE_DEFAULT, &errorCode);
  222. return (UBool)(U_SUCCESS(errorCode) &&
  223. 0!=u_strCompare(nfd.getBuffer(), nfd.length(),
  224. dest, destLength, false));
  225. }
  226. }
  227. #endif
  228. #if UCONFIG_NO_NORMALIZATION
  229. static UBool changesWhenNFKC_Casefolded(const BinaryProperty &, UChar32, UProperty) {
  230. return false;
  231. }
  232. #else
  233. static UBool changesWhenNFKC_Casefolded(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  234. UErrorCode errorCode=U_ZERO_ERROR;
  235. const Normalizer2Impl *kcf=Normalizer2Factory::getNFKC_CFImpl(errorCode);
  236. if(U_FAILURE(errorCode)) {
  237. return false;
  238. }
  239. UnicodeString src(c);
  240. UnicodeString dest;
  241. {
  242. // The ReorderingBuffer must be in a block because its destructor
  243. // needs to release dest's buffer before we look at its contents.
  244. ReorderingBuffer buffer(*kcf, dest);
  245. // Small destCapacity for NFKC_CF(c).
  246. if(buffer.init(5, errorCode)) {
  247. const char16_t *srcArray=src.getBuffer();
  248. kcf->compose(srcArray, srcArray+src.length(), false,
  249. true, buffer, errorCode);
  250. }
  251. }
  252. return U_SUCCESS(errorCode) && dest!=src;
  253. }
  254. #endif
  255. #if UCONFIG_NO_NORMALIZATION
  256. static UBool isCanonSegmentStarter(const BinaryProperty &, UChar32, UProperty) {
  257. return false;
  258. }
  259. #else
  260. static UBool isCanonSegmentStarter(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  261. UErrorCode errorCode=U_ZERO_ERROR;
  262. const Normalizer2Impl *impl=Normalizer2Factory::getNFCImpl(errorCode);
  263. return
  264. U_SUCCESS(errorCode) && impl->ensureCanonIterData(errorCode) &&
  265. impl->isCanonSegmentStarter(c);
  266. }
  267. #endif
  268. static UBool isPOSIX_alnum(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  269. return u_isalnumPOSIX(c);
  270. }
  271. static UBool isPOSIX_blank(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  272. return u_isblank(c);
  273. }
  274. static UBool isPOSIX_graph(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  275. return u_isgraphPOSIX(c);
  276. }
  277. static UBool isPOSIX_print(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  278. return u_isprintPOSIX(c);
  279. }
  280. static UBool isPOSIX_xdigit(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  281. return u_isxdigit(c);
  282. }
  283. static UBool isRegionalIndicator(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  284. // Property starts are a subset of lb=RI etc.
  285. return 0x1F1E6<=c && c<=0x1F1FF;
  286. }
  287. static UBool hasEmojiProperty(const BinaryProperty &/*prop*/, UChar32 c, UProperty which) {
  288. return EmojiProps::hasBinaryProperty(c, which);
  289. }
  290. static const BinaryProperty binProps[UCHAR_BINARY_LIMIT]={
  291. /*
  292. * column and mask values for binary properties from u_getUnicodeProperties().
  293. * Must be in order of corresponding UProperty,
  294. * and there must be exactly one entry per binary UProperty.
  295. *
  296. * Properties with mask==0 are handled in code.
  297. * For them, column is the UPropertySource value.
  298. */
  299. { 1, U_MASK(UPROPS_ALPHABETIC), defaultContains },
  300. { 1, U_MASK(UPROPS_ASCII_HEX_DIGIT), defaultContains },
  301. { UPROPS_SRC_BIDI, 0, isBidiControl },
  302. { UPROPS_SRC_BIDI, 0, isMirrored },
  303. { 1, U_MASK(UPROPS_DASH), defaultContains },
  304. { 1, U_MASK(UPROPS_DEFAULT_IGNORABLE_CODE_POINT), defaultContains },
  305. { 1, U_MASK(UPROPS_DEPRECATED), defaultContains },
  306. { 1, U_MASK(UPROPS_DIACRITIC), defaultContains },
  307. { 1, U_MASK(UPROPS_EXTENDER), defaultContains },
  308. { UPROPS_SRC_NFC, 0, hasFullCompositionExclusion },
  309. { 1, U_MASK(UPROPS_GRAPHEME_BASE), defaultContains },
  310. { 1, U_MASK(UPROPS_GRAPHEME_EXTEND), defaultContains },
  311. { 1, U_MASK(UPROPS_GRAPHEME_LINK), defaultContains },
  312. { 1, U_MASK(UPROPS_HEX_DIGIT), defaultContains },
  313. { 1, U_MASK(UPROPS_HYPHEN), defaultContains },
  314. { 1, U_MASK(UPROPS_ID_CONTINUE), defaultContains },
  315. { 1, U_MASK(UPROPS_ID_START), defaultContains },
  316. { 1, U_MASK(UPROPS_IDEOGRAPHIC), defaultContains },
  317. { 1, U_MASK(UPROPS_IDS_BINARY_OPERATOR), defaultContains },
  318. { 1, U_MASK(UPROPS_IDS_TRINARY_OPERATOR), defaultContains },
  319. { UPROPS_SRC_BIDI, 0, isJoinControl },
  320. { 1, U_MASK(UPROPS_LOGICAL_ORDER_EXCEPTION), defaultContains },
  321. { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_LOWERCASE
  322. { 1, U_MASK(UPROPS_MATH), defaultContains },
  323. { 1, U_MASK(UPROPS_NONCHARACTER_CODE_POINT), defaultContains },
  324. { 1, U_MASK(UPROPS_QUOTATION_MARK), defaultContains },
  325. { 1, U_MASK(UPROPS_RADICAL), defaultContains },
  326. { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_SOFT_DOTTED
  327. { 1, U_MASK(UPROPS_TERMINAL_PUNCTUATION), defaultContains },
  328. { 1, U_MASK(UPROPS_UNIFIED_IDEOGRAPH), defaultContains },
  329. { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_UPPERCASE
  330. { 1, U_MASK(UPROPS_WHITE_SPACE), defaultContains },
  331. { 1, U_MASK(UPROPS_XID_CONTINUE), defaultContains },
  332. { 1, U_MASK(UPROPS_XID_START), defaultContains },
  333. { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_CASE_SENSITIVE
  334. { 1, U_MASK(UPROPS_S_TERM), defaultContains },
  335. { 1, U_MASK(UPROPS_VARIATION_SELECTOR), defaultContains },
  336. { UPROPS_SRC_NFC, 0, isNormInert }, // UCHAR_NFD_INERT
  337. { UPROPS_SRC_NFKC, 0, isNormInert }, // UCHAR_NFKD_INERT
  338. { UPROPS_SRC_NFC, 0, isNormInert }, // UCHAR_NFC_INERT
  339. { UPROPS_SRC_NFKC, 0, isNormInert }, // UCHAR_NFKC_INERT
  340. { UPROPS_SRC_NFC_CANON_ITER, 0, isCanonSegmentStarter },
  341. { 1, U_MASK(UPROPS_PATTERN_SYNTAX), defaultContains },
  342. { 1, U_MASK(UPROPS_PATTERN_WHITE_SPACE), defaultContains },
  343. { UPROPS_SRC_CHAR_AND_PROPSVEC, 0, isPOSIX_alnum },
  344. { UPROPS_SRC_CHAR, 0, isPOSIX_blank },
  345. { UPROPS_SRC_CHAR, 0, isPOSIX_graph },
  346. { UPROPS_SRC_CHAR, 0, isPOSIX_print },
  347. { UPROPS_SRC_CHAR, 0, isPOSIX_xdigit },
  348. { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_CASED
  349. { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_CASE_IGNORABLE
  350. { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_CHANGES_WHEN_LOWERCASED
  351. { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_CHANGES_WHEN_UPPERCASED
  352. { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_CHANGES_WHEN_TITLECASED
  353. { UPROPS_SRC_CASE_AND_NORM, 0, changesWhenCasefolded },
  354. { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_CHANGES_WHEN_CASEMAPPED
  355. { UPROPS_SRC_NFKC_CF, 0, changesWhenNFKC_Casefolded },
  356. { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_EMOJI
  357. { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_EMOJI_PRESENTATION
  358. { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_EMOJI_MODIFIER
  359. { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_EMOJI_MODIFIER_BASE
  360. { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_EMOJI_COMPONENT
  361. { 2, 0, isRegionalIndicator },
  362. { 1, U_MASK(UPROPS_PREPENDED_CONCATENATION_MARK), defaultContains },
  363. { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_EXTENDED_PICTOGRAPHIC
  364. { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_BASIC_EMOJI
  365. { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_EMOJI_KEYCAP_SEQUENCE
  366. { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_RGI_EMOJI_MODIFIER_SEQUENCE
  367. { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_RGI_EMOJI_FLAG_SEQUENCE
  368. { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_RGI_EMOJI_TAG_SEQUENCE
  369. { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_RGI_EMOJI_ZWJ_SEQUENCE
  370. { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_RGI_EMOJI
  371. };
  372. U_CAPI UBool U_EXPORT2
  373. u_hasBinaryProperty(UChar32 c, UProperty which) {
  374. /* c is range-checked in the functions that are called from here */
  375. if(which<UCHAR_BINARY_START || UCHAR_BINARY_LIMIT<=which) {
  376. /* not a known binary property */
  377. return false;
  378. } else {
  379. const BinaryProperty &prop=binProps[which];
  380. return prop.contains(prop, c, which);
  381. }
  382. }
  383. /* Checks if the Unicode character can start a Unicode identifier.*/
  384. U_CAPI UBool U_EXPORT2
  385. u_isIDStart(UChar32 c) {
  386. return u_hasBinaryProperty(c, UCHAR_ID_START);
  387. }
  388. /* Checks if the Unicode character can be a Unicode identifier part other than starting the
  389. identifier.*/
  390. U_CAPI UBool U_EXPORT2
  391. u_isIDPart(UChar32 c) {
  392. return u_hasBinaryProperty(c, UCHAR_ID_CONTINUE);
  393. }
  394. U_CAPI UBool U_EXPORT2
  395. u_stringHasBinaryProperty(const char16_t *s, int32_t length, UProperty which) {
  396. if (s == nullptr && length != 0) { return false; }
  397. if (length == 1) {
  398. return u_hasBinaryProperty(s[0], which); // single code point
  399. } else if (length == 2 || (length < 0 && *s != 0)) { // not empty string
  400. // first code point
  401. int32_t i = 0;
  402. UChar32 c;
  403. U16_NEXT(s, i, length, c);
  404. if (length > 0 ? i == length : s[i] == 0) {
  405. return u_hasBinaryProperty(c, which); // single code point
  406. }
  407. }
  408. // Only call into EmojiProps for a relevant property,
  409. // so that we not unnecessarily try to load its data file.
  410. return UCHAR_BASIC_EMOJI <= which && which <= UCHAR_RGI_EMOJI &&
  411. EmojiProps::hasBinaryProperty(s, length, which);
  412. }
  413. struct IntProperty;
  414. typedef int32_t IntPropertyGetValue(const IntProperty &prop, UChar32 c, UProperty which);
  415. typedef int32_t IntPropertyGetMaxValue(const IntProperty &prop, UProperty which);
  416. struct IntProperty {
  417. int32_t column; // SRC_PROPSVEC column, or "source" if mask==0
  418. uint32_t mask;
  419. int32_t shift; // =maxValue if getMaxValueFromShift() is used
  420. IntPropertyGetValue *getValue;
  421. IntPropertyGetMaxValue *getMaxValue;
  422. };
  423. static int32_t defaultGetValue(const IntProperty &prop, UChar32 c, UProperty /*which*/) {
  424. /* systematic, directly stored properties */
  425. return (int32_t)(u_getUnicodeProperties(c, prop.column)&prop.mask)>>prop.shift;
  426. }
  427. static int32_t defaultGetMaxValue(const IntProperty &prop, UProperty /*which*/) {
  428. return (uprv_getMaxValues(prop.column)&prop.mask)>>prop.shift;
  429. }
  430. static int32_t getMaxValueFromShift(const IntProperty &prop, UProperty /*which*/) {
  431. return prop.shift;
  432. }
  433. static int32_t getBiDiClass(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  434. return (int32_t)u_charDirection(c);
  435. }
  436. static int32_t getBiDiPairedBracketType(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  437. return (int32_t)ubidi_getPairedBracketType(c);
  438. }
  439. static int32_t biDiGetMaxValue(const IntProperty &/*prop*/, UProperty which) {
  440. return ubidi_getMaxValue(which);
  441. }
  442. #if UCONFIG_NO_NORMALIZATION
  443. static int32_t getCombiningClass(const IntProperty &, UChar32, UProperty) {
  444. return 0;
  445. }
  446. #else
  447. static int32_t getCombiningClass(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  448. return u_getCombiningClass(c);
  449. }
  450. #endif
  451. static int32_t getGeneralCategory(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  452. return (int32_t)u_charType(c);
  453. }
  454. static int32_t getJoiningGroup(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  455. return ubidi_getJoiningGroup(c);
  456. }
  457. static int32_t getJoiningType(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  458. return ubidi_getJoiningType(c);
  459. }
  460. static int32_t getNumericType(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  461. int32_t ntv=(int32_t)GET_NUMERIC_TYPE_VALUE(u_getMainProperties(c));
  462. return UPROPS_NTV_GET_TYPE(ntv);
  463. }
  464. static int32_t getScript(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  465. UErrorCode errorCode=U_ZERO_ERROR;
  466. return (int32_t)uscript_getScript(c, &errorCode);
  467. }
  468. static int32_t scriptGetMaxValue(const IntProperty &/*prop*/, UProperty /*which*/) {
  469. uint32_t scriptX=uprv_getMaxValues(0)&UPROPS_SCRIPT_X_MASK;
  470. return uprops_mergeScriptCodeOrIndex(scriptX);
  471. }
  472. /*
  473. * Map some of the Grapheme Cluster Break values to Hangul Syllable Types.
  474. * Hangul_Syllable_Type is fully redundant with a subset of Grapheme_Cluster_Break.
  475. */
  476. static const UHangulSyllableType gcbToHst[]={
  477. U_HST_NOT_APPLICABLE, /* U_GCB_OTHER */
  478. U_HST_NOT_APPLICABLE, /* U_GCB_CONTROL */
  479. U_HST_NOT_APPLICABLE, /* U_GCB_CR */
  480. U_HST_NOT_APPLICABLE, /* U_GCB_EXTEND */
  481. U_HST_LEADING_JAMO, /* U_GCB_L */
  482. U_HST_NOT_APPLICABLE, /* U_GCB_LF */
  483. U_HST_LV_SYLLABLE, /* U_GCB_LV */
  484. U_HST_LVT_SYLLABLE, /* U_GCB_LVT */
  485. U_HST_TRAILING_JAMO, /* U_GCB_T */
  486. U_HST_VOWEL_JAMO /* U_GCB_V */
  487. /*
  488. * Omit GCB values beyond what we need for hst.
  489. * The code below checks for the array length.
  490. */
  491. };
  492. static int32_t getHangulSyllableType(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  493. /* see comments on gcbToHst[] above */
  494. int32_t gcb=(int32_t)(u_getUnicodeProperties(c, 2)&UPROPS_GCB_MASK)>>UPROPS_GCB_SHIFT;
  495. if(gcb<UPRV_LENGTHOF(gcbToHst)) {
  496. return gcbToHst[gcb];
  497. } else {
  498. return U_HST_NOT_APPLICABLE;
  499. }
  500. }
  501. #if UCONFIG_NO_NORMALIZATION
  502. static int32_t getNormQuickCheck(const IntProperty &, UChar32, UProperty) {
  503. return 0;
  504. }
  505. #else
  506. static int32_t getNormQuickCheck(const IntProperty &/*prop*/, UChar32 c, UProperty which) {
  507. return (int32_t)unorm_getQuickCheck(c, (UNormalizationMode)(which-UCHAR_NFD_QUICK_CHECK+UNORM_NFD));
  508. }
  509. #endif
  510. #if UCONFIG_NO_NORMALIZATION
  511. static int32_t getLeadCombiningClass(const IntProperty &, UChar32, UProperty) {
  512. return 0;
  513. }
  514. #else
  515. static int32_t getLeadCombiningClass(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  516. return unorm_getFCD16(c)>>8;
  517. }
  518. #endif
  519. #if UCONFIG_NO_NORMALIZATION
  520. static int32_t getTrailCombiningClass(const IntProperty &, UChar32, UProperty) {
  521. return 0;
  522. }
  523. #else
  524. static int32_t getTrailCombiningClass(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
  525. return unorm_getFCD16(c)&0xff;
  526. }
  527. #endif
  528. static int32_t getInPC(const IntProperty &, UChar32 c, UProperty) {
  529. return ulayout_ensureData() && gInpcTrie != nullptr ? ucptrie_get(gInpcTrie, c) : 0;
  530. }
  531. static int32_t getInSC(const IntProperty &, UChar32 c, UProperty) {
  532. return ulayout_ensureData() && gInscTrie != nullptr ? ucptrie_get(gInscTrie, c) : 0;
  533. }
  534. static int32_t getVo(const IntProperty &, UChar32 c, UProperty) {
  535. return ulayout_ensureData() && gVoTrie != nullptr ? ucptrie_get(gVoTrie, c) : 0;
  536. }
  537. static int32_t layoutGetMaxValue(const IntProperty &/*prop*/, UProperty which) {
  538. if (!ulayout_ensureData()) { return 0; }
  539. switch (which) {
  540. case UCHAR_INDIC_POSITIONAL_CATEGORY:
  541. return gMaxInpcValue;
  542. case UCHAR_INDIC_SYLLABIC_CATEGORY:
  543. return gMaxInscValue;
  544. case UCHAR_VERTICAL_ORIENTATION:
  545. return gMaxVoValue;
  546. default:
  547. return 0;
  548. }
  549. }
  550. static const IntProperty intProps[UCHAR_INT_LIMIT-UCHAR_INT_START]={
  551. /*
  552. * column, mask and shift values for int-value properties from u_getUnicodeProperties().
  553. * Must be in order of corresponding UProperty,
  554. * and there must be exactly one entry per int UProperty.
  555. *
  556. * Properties with mask==0 are handled in code.
  557. * For them, column is the UPropertySource value.
  558. */
  559. { UPROPS_SRC_BIDI, 0, 0, getBiDiClass, biDiGetMaxValue },
  560. { 0, UPROPS_BLOCK_MASK, UPROPS_BLOCK_SHIFT, defaultGetValue, defaultGetMaxValue },
  561. { UPROPS_SRC_NFC, 0, 0xff, getCombiningClass, getMaxValueFromShift },
  562. { 2, UPROPS_DT_MASK, 0, defaultGetValue, defaultGetMaxValue },
  563. { 0, UPROPS_EA_MASK, UPROPS_EA_SHIFT, defaultGetValue, defaultGetMaxValue },
  564. { UPROPS_SRC_CHAR, 0, (int32_t)U_CHAR_CATEGORY_COUNT-1,getGeneralCategory, getMaxValueFromShift },
  565. { UPROPS_SRC_BIDI, 0, 0, getJoiningGroup, biDiGetMaxValue },
  566. { UPROPS_SRC_BIDI, 0, 0, getJoiningType, biDiGetMaxValue },
  567. { 2, UPROPS_LB_MASK, UPROPS_LB_SHIFT, defaultGetValue, defaultGetMaxValue },
  568. { UPROPS_SRC_CHAR, 0, (int32_t)U_NT_COUNT-1, getNumericType, getMaxValueFromShift },
  569. { UPROPS_SRC_PROPSVEC, 0, 0, getScript, scriptGetMaxValue },
  570. { UPROPS_SRC_PROPSVEC, 0, (int32_t)U_HST_COUNT-1, getHangulSyllableType, getMaxValueFromShift },
  571. // UCHAR_NFD_QUICK_CHECK: max=1=YES -- never "maybe", only "no" or "yes"
  572. { UPROPS_SRC_NFC, 0, (int32_t)UNORM_YES, getNormQuickCheck, getMaxValueFromShift },
  573. // UCHAR_NFKD_QUICK_CHECK: max=1=YES -- never "maybe", only "no" or "yes"
  574. { UPROPS_SRC_NFKC, 0, (int32_t)UNORM_YES, getNormQuickCheck, getMaxValueFromShift },
  575. // UCHAR_NFC_QUICK_CHECK: max=2=MAYBE
  576. { UPROPS_SRC_NFC, 0, (int32_t)UNORM_MAYBE, getNormQuickCheck, getMaxValueFromShift },
  577. // UCHAR_NFKC_QUICK_CHECK: max=2=MAYBE
  578. { UPROPS_SRC_NFKC, 0, (int32_t)UNORM_MAYBE, getNormQuickCheck, getMaxValueFromShift },
  579. { UPROPS_SRC_NFC, 0, 0xff, getLeadCombiningClass, getMaxValueFromShift },
  580. { UPROPS_SRC_NFC, 0, 0xff, getTrailCombiningClass, getMaxValueFromShift },
  581. { 2, UPROPS_GCB_MASK, UPROPS_GCB_SHIFT, defaultGetValue, defaultGetMaxValue },
  582. { 2, UPROPS_SB_MASK, UPROPS_SB_SHIFT, defaultGetValue, defaultGetMaxValue },
  583. { 2, UPROPS_WB_MASK, UPROPS_WB_SHIFT, defaultGetValue, defaultGetMaxValue },
  584. { UPROPS_SRC_BIDI, 0, 0, getBiDiPairedBracketType, biDiGetMaxValue },
  585. { UPROPS_SRC_INPC, 0, 0, getInPC, layoutGetMaxValue },
  586. { UPROPS_SRC_INSC, 0, 0, getInSC, layoutGetMaxValue },
  587. { UPROPS_SRC_VO, 0, 0, getVo, layoutGetMaxValue },
  588. };
  589. U_CAPI int32_t U_EXPORT2
  590. u_getIntPropertyValue(UChar32 c, UProperty which) {
  591. if(which<UCHAR_INT_START) {
  592. if(UCHAR_BINARY_START<=which && which<UCHAR_BINARY_LIMIT) {
  593. const BinaryProperty &prop=binProps[which];
  594. return prop.contains(prop, c, which);
  595. }
  596. } else if(which<UCHAR_INT_LIMIT) {
  597. const IntProperty &prop=intProps[which-UCHAR_INT_START];
  598. return prop.getValue(prop, c, which);
  599. } else if(which==UCHAR_GENERAL_CATEGORY_MASK) {
  600. return U_MASK(u_charType(c));
  601. }
  602. return 0; // undefined
  603. }
  604. U_CAPI int32_t U_EXPORT2
  605. u_getIntPropertyMinValue(UProperty /*which*/) {
  606. return 0; /* all binary/enum/int properties have a minimum value of 0 */
  607. }
  608. U_CAPI int32_t U_EXPORT2
  609. u_getIntPropertyMaxValue(UProperty which) {
  610. if(which<UCHAR_INT_START) {
  611. if(UCHAR_BINARY_START<=which && which<UCHAR_BINARY_LIMIT) {
  612. return 1; // maximum true for all binary properties
  613. }
  614. } else if(which<UCHAR_INT_LIMIT) {
  615. const IntProperty &prop=intProps[which-UCHAR_INT_START];
  616. return prop.getMaxValue(prop, which);
  617. }
  618. return -1; // undefined
  619. }
  620. U_CFUNC UPropertySource U_EXPORT2
  621. uprops_getSource(UProperty which) {
  622. if(which<UCHAR_BINARY_START) {
  623. return UPROPS_SRC_NONE; /* undefined */
  624. } else if(which<UCHAR_BINARY_LIMIT) {
  625. const BinaryProperty &prop=binProps[which];
  626. if(prop.mask!=0) {
  627. return UPROPS_SRC_PROPSVEC;
  628. } else {
  629. return (UPropertySource)prop.column;
  630. }
  631. } else if(which<UCHAR_INT_START) {
  632. return UPROPS_SRC_NONE; /* undefined */
  633. } else if(which<UCHAR_INT_LIMIT) {
  634. const IntProperty &prop=intProps[which-UCHAR_INT_START];
  635. if(prop.mask!=0) {
  636. return UPROPS_SRC_PROPSVEC;
  637. } else {
  638. return (UPropertySource)prop.column;
  639. }
  640. } else if(which<UCHAR_STRING_START) {
  641. switch(which) {
  642. case UCHAR_GENERAL_CATEGORY_MASK:
  643. case UCHAR_NUMERIC_VALUE:
  644. return UPROPS_SRC_CHAR;
  645. default:
  646. return UPROPS_SRC_NONE;
  647. }
  648. } else if(which<UCHAR_STRING_LIMIT) {
  649. switch(which) {
  650. case UCHAR_AGE:
  651. return UPROPS_SRC_PROPSVEC;
  652. case UCHAR_BIDI_MIRRORING_GLYPH:
  653. return UPROPS_SRC_BIDI;
  654. case UCHAR_CASE_FOLDING:
  655. case UCHAR_LOWERCASE_MAPPING:
  656. case UCHAR_SIMPLE_CASE_FOLDING:
  657. case UCHAR_SIMPLE_LOWERCASE_MAPPING:
  658. case UCHAR_SIMPLE_TITLECASE_MAPPING:
  659. case UCHAR_SIMPLE_UPPERCASE_MAPPING:
  660. case UCHAR_TITLECASE_MAPPING:
  661. case UCHAR_UPPERCASE_MAPPING:
  662. return UPROPS_SRC_CASE;
  663. case UCHAR_ISO_COMMENT:
  664. case UCHAR_NAME:
  665. case UCHAR_UNICODE_1_NAME:
  666. return UPROPS_SRC_NAMES;
  667. default:
  668. return UPROPS_SRC_NONE;
  669. }
  670. } else {
  671. switch(which) {
  672. case UCHAR_SCRIPT_EXTENSIONS:
  673. return UPROPS_SRC_PROPSVEC;
  674. default:
  675. return UPROPS_SRC_NONE; /* undefined */
  676. }
  677. }
  678. }
  679. U_CFUNC void U_EXPORT2
  680. uprops_addPropertyStarts(UPropertySource src, const USetAdder *sa, UErrorCode *pErrorCode) {
  681. if (!ulayout_ensureData(*pErrorCode)) { return; }
  682. const UCPTrie *trie;
  683. switch (src) {
  684. case UPROPS_SRC_INPC:
  685. trie = gInpcTrie;
  686. break;
  687. case UPROPS_SRC_INSC:
  688. trie = gInscTrie;
  689. break;
  690. case UPROPS_SRC_VO:
  691. trie = gVoTrie;
  692. break;
  693. default:
  694. *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
  695. return;
  696. }
  697. if (trie == nullptr) {
  698. *pErrorCode = U_MISSING_RESOURCE_ERROR;
  699. return;
  700. }
  701. // Add the start code point of each same-value range of the trie.
  702. UChar32 start = 0, end;
  703. while ((end = ucptrie_getRange(trie, start, UCPMAP_RANGE_NORMAL, 0,
  704. nullptr, nullptr, nullptr)) >= 0) {
  705. sa->add(sa->set, start);
  706. start = end + 1;
  707. }
  708. }
  709. #if !UCONFIG_NO_NORMALIZATION
  710. U_CAPI int32_t U_EXPORT2
  711. u_getFC_NFKC_Closure(UChar32 c, char16_t *dest, int32_t destCapacity, UErrorCode *pErrorCode) {
  712. if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
  713. return 0;
  714. }
  715. if(destCapacity<0 || (dest==nullptr && destCapacity>0)) {
  716. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  717. return 0;
  718. }
  719. // Compute the FC_NFKC_Closure on the fly:
  720. // We have the API for complete coverage of Unicode properties, although
  721. // this value by itself is not useful via API.
  722. // (What could be useful is a custom normalization table that combines
  723. // case folding and NFKC.)
  724. // For the derivation, see Unicode's DerivedNormalizationProps.txt.
  725. const Normalizer2 *nfkc=Normalizer2::getNFKCInstance(*pErrorCode);
  726. if(U_FAILURE(*pErrorCode)) {
  727. return 0;
  728. }
  729. // first: b = NFKC(Fold(a))
  730. UnicodeString folded1String;
  731. const char16_t *folded1;
  732. int32_t folded1Length=ucase_toFullFolding(c, &folded1, U_FOLD_CASE_DEFAULT);
  733. if(folded1Length<0) {
  734. const Normalizer2Impl *nfkcImpl=Normalizer2Factory::getImpl(nfkc);
  735. if(nfkcImpl->getCompQuickCheck(nfkcImpl->getNorm16(c))!=UNORM_NO) {
  736. return u_terminateUChars(dest, destCapacity, 0, pErrorCode); // c does not change at all under CaseFolding+NFKC
  737. }
  738. folded1String.setTo(c);
  739. } else {
  740. if(folded1Length>UCASE_MAX_STRING_LENGTH) {
  741. folded1String.setTo(folded1Length);
  742. } else {
  743. folded1String.setTo(false, folded1, folded1Length);
  744. }
  745. }
  746. UnicodeString kc1=nfkc->normalize(folded1String, *pErrorCode);
  747. // second: c = NFKC(Fold(b))
  748. UnicodeString folded2String(kc1);
  749. UnicodeString kc2=nfkc->normalize(folded2String.foldCase(), *pErrorCode);
  750. // if (c != b) add the mapping from a to c
  751. if(U_FAILURE(*pErrorCode) || kc1==kc2) {
  752. return u_terminateUChars(dest, destCapacity, 0, pErrorCode);
  753. } else {
  754. return kc2.extract(dest, destCapacity, *pErrorCode);
  755. }
  756. }
  757. #endif