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
26 #include "unicode/utypes.h"
27 #include "unicode/uchar.h"
28 #include "unicode/ucptrie.h"
29 #include "unicode/udata.h"
30 #include "unicode/unorm2.h"
31 #include "unicode/uscript.h"
32 #include "unicode/ustring.h"
33 #include "unicode/utf16.h"
34 #include "cstring.h"
35 #include "emojiprops.h"
36 #include "mutex.h"
37 #include "normalizer2impl.h"
38 #include "umutex.h"
39 #include "ubidi_props.h"
40 #include "uprops.h"
41 #include "ucase.h"
42 #include "ucln_cmn.h"
43 #include "ulayout_props.h"
44 #include "ustr_imp.h"
45
46 U_NAMESPACE_USE
47
48 // Unicode text layout properties data -----------------------------------------
49
50 namespace {
51
52 icu::UInitOnce gLayoutInitOnce {};
53 UDataMemory *gLayoutMemory = nullptr;
54
55 UCPTrie *gInpcTrie = nullptr; // Indic_Positional_Category
56 UCPTrie *gInscTrie = nullptr; // Indic_Syllabic_Category
57 UCPTrie *gVoTrie = nullptr; // Vertical_Orientation
58
59 int32_t gMaxInpcValue = 0;
60 int32_t gMaxInscValue = 0;
61 int32_t gMaxVoValue = 0;
62
uprops_cleanup()63 UBool U_CALLCONV uprops_cleanup() {
64 udata_close(gLayoutMemory);
65 gLayoutMemory = nullptr;
66
67 ucptrie_close(gInpcTrie);
68 gInpcTrie = nullptr;
69 ucptrie_close(gInscTrie);
70 gInscTrie = nullptr;
71 ucptrie_close(gVoTrie);
72 gVoTrie = nullptr;
73
74 gMaxInpcValue = 0;
75 gMaxInscValue = 0;
76 gMaxVoValue = 0;
77
78 gLayoutInitOnce.reset();
79 return true;
80 }
81
82 UBool U_CALLCONV
ulayout_isAcceptable(void *,const char *,const char *,const UDataInfo * pInfo)83 ulayout_isAcceptable(void * /*context*/,
84 const char * /* type */, const char * /*name*/,
85 const UDataInfo *pInfo) {
86 return pInfo->size >= 20 &&
87 pInfo->isBigEndian == U_IS_BIG_ENDIAN &&
88 pInfo->charsetFamily == U_CHARSET_FAMILY &&
89 pInfo->dataFormat[0] == ULAYOUT_FMT_0 &&
90 pInfo->dataFormat[1] == ULAYOUT_FMT_1 &&
91 pInfo->dataFormat[2] == ULAYOUT_FMT_2 &&
92 pInfo->dataFormat[3] == ULAYOUT_FMT_3 &&
93 pInfo->formatVersion[0] == 1;
94 }
95
96 // UInitOnce singleton initialization function
ulayout_load(UErrorCode & errorCode)97 void U_CALLCONV ulayout_load(UErrorCode &errorCode) {
98 gLayoutMemory = udata_openChoice(
99 nullptr, ULAYOUT_DATA_TYPE, ULAYOUT_DATA_NAME,
100 ulayout_isAcceptable, nullptr, &errorCode);
101 if (U_FAILURE(errorCode)) { return; }
102
103 const uint8_t *inBytes = (const uint8_t *)udata_getMemory(gLayoutMemory);
104 const int32_t *inIndexes = (const int32_t *)inBytes;
105 int32_t indexesLength = inIndexes[ULAYOUT_IX_INDEXES_LENGTH];
106 if (indexesLength < 12) {
107 errorCode = U_INVALID_FORMAT_ERROR; // Not enough indexes.
108 return;
109 }
110 int32_t offset = indexesLength * 4;
111 int32_t top = inIndexes[ULAYOUT_IX_INPC_TRIE_TOP];
112 int32_t trieSize = top - offset;
113 if (trieSize >= 16) {
114 gInpcTrie = ucptrie_openFromBinary(
115 UCPTRIE_TYPE_ANY, UCPTRIE_VALUE_BITS_ANY,
116 inBytes + offset, trieSize, nullptr, &errorCode);
117 }
118 offset = top;
119 top = inIndexes[ULAYOUT_IX_INSC_TRIE_TOP];
120 trieSize = top - offset;
121 if (trieSize >= 16) {
122 gInscTrie = ucptrie_openFromBinary(
123 UCPTRIE_TYPE_ANY, UCPTRIE_VALUE_BITS_ANY,
124 inBytes + offset, trieSize, nullptr, &errorCode);
125 }
126 offset = top;
127 top = inIndexes[ULAYOUT_IX_VO_TRIE_TOP];
128 trieSize = top - offset;
129 if (trieSize >= 16) {
130 gVoTrie = ucptrie_openFromBinary(
131 UCPTRIE_TYPE_ANY, UCPTRIE_VALUE_BITS_ANY,
132 inBytes + offset, trieSize, nullptr, &errorCode);
133 }
134
135 uint32_t maxValues = inIndexes[ULAYOUT_IX_MAX_VALUES];
136 gMaxInpcValue = maxValues >> ULAYOUT_MAX_INPC_SHIFT;
137 gMaxInscValue = (maxValues >> ULAYOUT_MAX_INSC_SHIFT) & 0xff;
138 gMaxVoValue = (maxValues >> ULAYOUT_MAX_VO_SHIFT) & 0xff;
139
140 ucln_common_registerCleanup(UCLN_COMMON_UPROPS, uprops_cleanup);
141 }
142
ulayout_ensureData(UErrorCode & errorCode)143 UBool ulayout_ensureData(UErrorCode &errorCode) {
144 if (U_FAILURE(errorCode)) { return false; }
145 umtx_initOnce(gLayoutInitOnce, &ulayout_load, errorCode);
146 return U_SUCCESS(errorCode);
147 }
148
ulayout_ensureData()149 UBool ulayout_ensureData() {
150 UErrorCode errorCode = U_ZERO_ERROR;
151 return ulayout_ensureData(errorCode);
152 }
153
154 } // namespace
155
156 /* general properties API functions ----------------------------------------- */
157
158 struct BinaryProperty;
159
160 typedef UBool BinaryPropertyContains(const BinaryProperty &prop, UChar32 c, UProperty which);
161
162 struct BinaryProperty {
163 int32_t column; // SRC_PROPSVEC column, or "source" if mask==0
164 uint32_t mask;
165 BinaryPropertyContains *contains;
166 };
167
defaultContains(const BinaryProperty & prop,UChar32 c,UProperty)168 static UBool defaultContains(const BinaryProperty &prop, UChar32 c, UProperty /*which*/) {
169 /* systematic, directly stored properties */
170 return (u_getUnicodeProperties(c, prop.column)&prop.mask)!=0;
171 }
172
caseBinaryPropertyContains(const BinaryProperty &,UChar32 c,UProperty which)173 static UBool caseBinaryPropertyContains(const BinaryProperty &/*prop*/, UChar32 c, UProperty which) {
174 return static_cast<UBool>(ucase_hasBinaryProperty(c, which));
175 }
176
isBidiControl(const BinaryProperty &,UChar32 c,UProperty)177 static UBool isBidiControl(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
178 return ubidi_isBidiControl(c);
179 }
180
isMirrored(const BinaryProperty &,UChar32 c,UProperty)181 static UBool isMirrored(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
182 return ubidi_isMirrored(c);
183 }
184
isJoinControl(const BinaryProperty &,UChar32 c,UProperty)185 static UBool isJoinControl(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
186 return ubidi_isJoinControl(c);
187 }
188
189 #if UCONFIG_NO_NORMALIZATION
hasFullCompositionExclusion(const BinaryProperty &,UChar32,UProperty)190 static UBool hasFullCompositionExclusion(const BinaryProperty &, UChar32, UProperty) {
191 return false;
192 }
193 #else
hasFullCompositionExclusion(const BinaryProperty &,UChar32 c,UProperty)194 static UBool hasFullCompositionExclusion(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
195 // By definition, Full_Composition_Exclusion is the same as NFC_QC=No.
196 UErrorCode errorCode=U_ZERO_ERROR;
197 const Normalizer2Impl *impl=Normalizer2Factory::getNFCImpl(errorCode);
198 return U_SUCCESS(errorCode) && impl->isCompNo(impl->getNorm16(c));
199 }
200 #endif
201
202 // UCHAR_NF*_INERT properties
203 #if UCONFIG_NO_NORMALIZATION
isNormInert(const BinaryProperty &,UChar32,UProperty)204 static UBool isNormInert(const BinaryProperty &, UChar32, UProperty) {
205 return false;
206 }
207 #else
isNormInert(const BinaryProperty &,UChar32 c,UProperty which)208 static UBool isNormInert(const BinaryProperty &/*prop*/, UChar32 c, UProperty which) {
209 UErrorCode errorCode=U_ZERO_ERROR;
210 const Normalizer2 *norm2=Normalizer2Factory::getInstance(
211 (UNormalizationMode)(which-UCHAR_NFD_INERT+UNORM_NFD), errorCode);
212 return U_SUCCESS(errorCode) && norm2->isInert(c);
213 }
214 #endif
215
216 #if UCONFIG_NO_NORMALIZATION
changesWhenCasefolded(const BinaryProperty &,UChar32,UProperty)217 static UBool changesWhenCasefolded(const BinaryProperty &, UChar32, UProperty) {
218 return false;
219 }
220 #else
changesWhenCasefolded(const BinaryProperty &,UChar32 c,UProperty)221 static UBool changesWhenCasefolded(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
222 UnicodeString nfd;
223 UErrorCode errorCode=U_ZERO_ERROR;
224 const Normalizer2 *nfcNorm2=Normalizer2::getNFCInstance(errorCode);
225 if(U_FAILURE(errorCode)) {
226 return false;
227 }
228 if(nfcNorm2->getDecomposition(c, nfd)) {
229 /* c has a decomposition */
230 if(nfd.length()==1) {
231 c=nfd[0]; /* single BMP code point */
232 } else if(nfd.length()<=U16_MAX_LENGTH &&
233 nfd.length()==U16_LENGTH(c=nfd.char32At(0))
234 ) {
235 /* single supplementary code point */
236 } else {
237 c=U_SENTINEL;
238 }
239 } else if(c<0) {
240 return false; /* protect against bad input */
241 }
242 if(c>=0) {
243 /* single code point */
244 const char16_t *resultString;
245 return (UBool)(ucase_toFullFolding(c, &resultString, U_FOLD_CASE_DEFAULT)>=0);
246 } else {
247 /* guess some large but stack-friendly capacity */
248 char16_t dest[2*UCASE_MAX_STRING_LENGTH];
249 int32_t destLength;
250 destLength=u_strFoldCase(dest, UPRV_LENGTHOF(dest),
251 nfd.getBuffer(), nfd.length(),
252 U_FOLD_CASE_DEFAULT, &errorCode);
253 return (UBool)(U_SUCCESS(errorCode) &&
254 0!=u_strCompare(nfd.getBuffer(), nfd.length(),
255 dest, destLength, false));
256 }
257 }
258 #endif
259
260 #if UCONFIG_NO_NORMALIZATION
changesWhenNFKC_Casefolded(const BinaryProperty &,UChar32,UProperty)261 static UBool changesWhenNFKC_Casefolded(const BinaryProperty &, UChar32, UProperty) {
262 return false;
263 }
264 #else
changesWhenNFKC_Casefolded(const BinaryProperty &,UChar32 c,UProperty)265 static UBool changesWhenNFKC_Casefolded(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
266 UErrorCode errorCode=U_ZERO_ERROR;
267 const Normalizer2Impl *kcf=Normalizer2Factory::getNFKC_CFImpl(errorCode);
268 if(U_FAILURE(errorCode)) {
269 return false;
270 }
271 UnicodeString src(c);
272 UnicodeString dest;
273 {
274 // The ReorderingBuffer must be in a block because its destructor
275 // needs to release dest's buffer before we look at its contents.
276 ReorderingBuffer buffer(*kcf, dest);
277 // Small destCapacity for NFKC_CF(c).
278 if(buffer.init(5, errorCode)) {
279 const char16_t *srcArray=src.getBuffer();
280 kcf->compose(srcArray, srcArray+src.length(), false,
281 true, buffer, errorCode);
282 }
283 }
284 return U_SUCCESS(errorCode) && dest!=src;
285 }
286 #endif
287
288 #if UCONFIG_NO_NORMALIZATION
isCanonSegmentStarter(const BinaryProperty &,UChar32,UProperty)289 static UBool isCanonSegmentStarter(const BinaryProperty &, UChar32, UProperty) {
290 return false;
291 }
292 #else
isCanonSegmentStarter(const BinaryProperty &,UChar32 c,UProperty)293 static UBool isCanonSegmentStarter(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
294 UErrorCode errorCode=U_ZERO_ERROR;
295 const Normalizer2Impl *impl=Normalizer2Factory::getNFCImpl(errorCode);
296 return
297 U_SUCCESS(errorCode) && impl->ensureCanonIterData(errorCode) &&
298 impl->isCanonSegmentStarter(c);
299 }
300 #endif
301
isPOSIX_alnum(const BinaryProperty &,UChar32 c,UProperty)302 static UBool isPOSIX_alnum(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
303 return u_isalnumPOSIX(c);
304 }
305
isPOSIX_blank(const BinaryProperty &,UChar32 c,UProperty)306 static UBool isPOSIX_blank(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
307 return u_isblank(c);
308 }
309
isPOSIX_graph(const BinaryProperty &,UChar32 c,UProperty)310 static UBool isPOSIX_graph(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
311 return u_isgraphPOSIX(c);
312 }
313
isPOSIX_print(const BinaryProperty &,UChar32 c,UProperty)314 static UBool isPOSIX_print(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
315 return u_isprintPOSIX(c);
316 }
317
isPOSIX_xdigit(const BinaryProperty &,UChar32 c,UProperty)318 static UBool isPOSIX_xdigit(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
319 return u_isxdigit(c);
320 }
321
isRegionalIndicator(const BinaryProperty &,UChar32 c,UProperty)322 static UBool isRegionalIndicator(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
323 // Property starts are a subset of lb=RI etc.
324 return 0x1F1E6<=c && c<=0x1F1FF;
325 }
326
hasEmojiProperty(const BinaryProperty &,UChar32 c,UProperty which)327 static UBool hasEmojiProperty(const BinaryProperty &/*prop*/, UChar32 c, UProperty which) {
328 return EmojiProps::hasBinaryProperty(c, which);
329 }
330
isIDSUnaryOperator(const BinaryProperty &,UChar32 c,UProperty)331 static UBool isIDSUnaryOperator(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
332 // New in Unicode 15.1 for just two characters.
333 return 0x2FFE<=c && c<=0x2FFF;
334 }
335
336 /** Ranges (start/limit pairs) of ID_Compat_Math_Continue (only), from UCD PropList.txt. */
337 static constexpr UChar32 ID_COMPAT_MATH_CONTINUE[] = {
338 0x00B2, 0x00B3 + 1,
339 0x00B9, 0x00B9 + 1,
340 0x2070, 0x2070 + 1,
341 0x2074, 0x207E + 1,
342 0x2080, 0x208E + 1
343 };
344
345 /** ID_Compat_Math_Start characters, from UCD PropList.txt. */
346 static constexpr UChar32 ID_COMPAT_MATH_START[] = {
347 0x2202,
348 0x2207,
349 0x221E,
350 0x1D6C1,
351 0x1D6DB,
352 0x1D6FB,
353 0x1D715,
354 0x1D735,
355 0x1D74F,
356 0x1D76F,
357 0x1D789,
358 0x1D7A9,
359 0x1D7C3
360 };
361
isIDCompatMathStart(const BinaryProperty &,UChar32 c,UProperty)362 static UBool isIDCompatMathStart(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
363 if (c < ID_COMPAT_MATH_START[0]) { return false; } // fastpath for common scripts
364 for (UChar32 startChar : ID_COMPAT_MATH_START) {
365 if (c == startChar) { return true; }
366 }
367 return false;
368 }
369
isIDCompatMathContinue(const BinaryProperty & prop,UChar32 c,UProperty)370 static UBool isIDCompatMathContinue(const BinaryProperty &prop, UChar32 c, UProperty /*which*/) {
371 for (int32_t i = 0; i < UPRV_LENGTHOF(ID_COMPAT_MATH_CONTINUE); i += 2) {
372 if (c < ID_COMPAT_MATH_CONTINUE[i]) { return false; } // below range start
373 if (c < ID_COMPAT_MATH_CONTINUE[i + 1]) { return true; } // below range limit
374 }
375 return isIDCompatMathStart(prop, c, UCHAR_ID_COMPAT_MATH_START);
376 }
377
378 static const BinaryProperty binProps[UCHAR_BINARY_LIMIT]={
379 /*
380 * column and mask values for binary properties from u_getUnicodeProperties().
381 * Must be in order of corresponding UProperty,
382 * and there must be exactly one entry per binary UProperty.
383 *
384 * Properties with mask==0 are handled in code.
385 * For them, column is the UPropertySource value.
386 */
387 { 1, U_MASK(UPROPS_ALPHABETIC), defaultContains },
388 { 1, U_MASK(UPROPS_ASCII_HEX_DIGIT), defaultContains },
389 { UPROPS_SRC_BIDI, 0, isBidiControl },
390 { UPROPS_SRC_BIDI, 0, isMirrored },
391 { 1, U_MASK(UPROPS_DASH), defaultContains },
392 { 1, U_MASK(UPROPS_DEFAULT_IGNORABLE_CODE_POINT), defaultContains },
393 { 1, U_MASK(UPROPS_DEPRECATED), defaultContains },
394 { 1, U_MASK(UPROPS_DIACRITIC), defaultContains },
395 { 1, U_MASK(UPROPS_EXTENDER), defaultContains },
396 { UPROPS_SRC_NFC, 0, hasFullCompositionExclusion },
397 { 1, U_MASK(UPROPS_GRAPHEME_BASE), defaultContains },
398 { 1, U_MASK(UPROPS_GRAPHEME_EXTEND), defaultContains },
399 { 1, U_MASK(UPROPS_GRAPHEME_LINK), defaultContains },
400 { 1, U_MASK(UPROPS_HEX_DIGIT), defaultContains },
401 { 1, U_MASK(UPROPS_HYPHEN), defaultContains },
402 { 1, U_MASK(UPROPS_ID_CONTINUE), defaultContains },
403 { 1, U_MASK(UPROPS_ID_START), defaultContains },
404 { 1, U_MASK(UPROPS_IDEOGRAPHIC), defaultContains },
405 { 1, U_MASK(UPROPS_IDS_BINARY_OPERATOR), defaultContains },
406 { 1, U_MASK(UPROPS_IDS_TRINARY_OPERATOR), defaultContains },
407 { UPROPS_SRC_BIDI, 0, isJoinControl },
408 { 1, U_MASK(UPROPS_LOGICAL_ORDER_EXCEPTION), defaultContains },
409 { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_LOWERCASE
410 { 1, U_MASK(UPROPS_MATH), defaultContains },
411 { 1, U_MASK(UPROPS_NONCHARACTER_CODE_POINT), defaultContains },
412 { 1, U_MASK(UPROPS_QUOTATION_MARK), defaultContains },
413 { 1, U_MASK(UPROPS_RADICAL), defaultContains },
414 { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_SOFT_DOTTED
415 { 1, U_MASK(UPROPS_TERMINAL_PUNCTUATION), defaultContains },
416 { 1, U_MASK(UPROPS_UNIFIED_IDEOGRAPH), defaultContains },
417 { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_UPPERCASE
418 { 1, U_MASK(UPROPS_WHITE_SPACE), defaultContains },
419 { 1, U_MASK(UPROPS_XID_CONTINUE), defaultContains },
420 { 1, U_MASK(UPROPS_XID_START), defaultContains },
421 { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_CASE_SENSITIVE
422 { 1, U_MASK(UPROPS_S_TERM), defaultContains },
423 { 1, U_MASK(UPROPS_VARIATION_SELECTOR), defaultContains },
424 { UPROPS_SRC_NFC, 0, isNormInert }, // UCHAR_NFD_INERT
425 { UPROPS_SRC_NFKC, 0, isNormInert }, // UCHAR_NFKD_INERT
426 { UPROPS_SRC_NFC, 0, isNormInert }, // UCHAR_NFC_INERT
427 { UPROPS_SRC_NFKC, 0, isNormInert }, // UCHAR_NFKC_INERT
428 { UPROPS_SRC_NFC_CANON_ITER, 0, isCanonSegmentStarter },
429 { 1, U_MASK(UPROPS_PATTERN_SYNTAX), defaultContains },
430 { 1, U_MASK(UPROPS_PATTERN_WHITE_SPACE), defaultContains },
431 { UPROPS_SRC_CHAR_AND_PROPSVEC, 0, isPOSIX_alnum },
432 { UPROPS_SRC_CHAR, 0, isPOSIX_blank },
433 { UPROPS_SRC_CHAR, 0, isPOSIX_graph },
434 { UPROPS_SRC_CHAR, 0, isPOSIX_print },
435 { UPROPS_SRC_CHAR, 0, isPOSIX_xdigit },
436 { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_CASED
437 { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_CASE_IGNORABLE
438 { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_CHANGES_WHEN_LOWERCASED
439 { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_CHANGES_WHEN_UPPERCASED
440 { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_CHANGES_WHEN_TITLECASED
441 { UPROPS_SRC_CASE_AND_NORM, 0, changesWhenCasefolded },
442 { UPROPS_SRC_CASE, 0, caseBinaryPropertyContains }, // UCHAR_CHANGES_WHEN_CASEMAPPED
443 { UPROPS_SRC_NFKC_CF, 0, changesWhenNFKC_Casefolded },
444 { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_EMOJI
445 { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_EMOJI_PRESENTATION
446 { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_EMOJI_MODIFIER
447 { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_EMOJI_MODIFIER_BASE
448 { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_EMOJI_COMPONENT
449 { 2, 0, isRegionalIndicator },
450 { 1, U_MASK(UPROPS_PREPENDED_CONCATENATION_MARK), defaultContains },
451 { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_EXTENDED_PICTOGRAPHIC
452 { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_BASIC_EMOJI
453 { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_EMOJI_KEYCAP_SEQUENCE
454 { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_RGI_EMOJI_MODIFIER_SEQUENCE
455 { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_RGI_EMOJI_FLAG_SEQUENCE
456 { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_RGI_EMOJI_TAG_SEQUENCE
457 { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_RGI_EMOJI_ZWJ_SEQUENCE
458 { UPROPS_SRC_EMOJI, 0, hasEmojiProperty }, // UCHAR_RGI_EMOJI
459 { UPROPS_SRC_IDSU, 0, isIDSUnaryOperator }, // UCHAR_IDS_UNARY_OPERATOR
460 { UPROPS_SRC_ID_COMPAT_MATH, 0, isIDCompatMathStart }, // UCHAR_ID_COMPAT_MATH_START
461 { UPROPS_SRC_ID_COMPAT_MATH, 0, isIDCompatMathContinue }, // UCHAR_ID_COMPAT_MATH_CONTINUE
462 };
463
464 U_CAPI UBool U_EXPORT2
u_hasBinaryProperty(UChar32 c,UProperty which)465 u_hasBinaryProperty(UChar32 c, UProperty which) {
466 /* c is range-checked in the functions that are called from here */
467 if(which<UCHAR_BINARY_START || UCHAR_BINARY_LIMIT<=which) {
468 /* not a known binary property */
469 return false;
470 } else {
471 const BinaryProperty &prop=binProps[which];
472 return prop.contains(prop, c, which);
473 }
474 }
475
476 /* Checks if the Unicode character can start a Unicode identifier.*/
477 U_CAPI UBool U_EXPORT2
u_isIDStart(UChar32 c)478 u_isIDStart(UChar32 c) {
479 return u_hasBinaryProperty(c, UCHAR_ID_START);
480 }
481
482 /* Checks if the Unicode character can be a Unicode identifier part other than starting the
483 identifier.*/
484 U_CAPI UBool U_EXPORT2
u_isIDPart(UChar32 c)485 u_isIDPart(UChar32 c) {
486 return u_hasBinaryProperty(c, UCHAR_ID_CONTINUE);
487 }
488
489 U_CAPI UBool U_EXPORT2
u_stringHasBinaryProperty(const char16_t * s,int32_t length,UProperty which)490 u_stringHasBinaryProperty(const char16_t *s, int32_t length, UProperty which) {
491 if (s == nullptr && length != 0) { return false; }
492 if (length == 1) {
493 return u_hasBinaryProperty(s[0], which); // single code point
494 } else if (length == 2 || (length < 0 && *s != 0)) { // not empty string
495 // first code point
496 int32_t i = 0;
497 UChar32 c;
498 U16_NEXT(s, i, length, c);
499 if (length > 0 ? i == length : s[i] == 0) {
500 return u_hasBinaryProperty(c, which); // single code point
501 }
502 }
503 // Only call into EmojiProps for a relevant property,
504 // so that we not unnecessarily try to load its data file.
505 return UCHAR_BASIC_EMOJI <= which && which <= UCHAR_RGI_EMOJI &&
506 EmojiProps::hasBinaryProperty(s, length, which);
507 }
508
509 struct IntProperty;
510
511 typedef int32_t IntPropertyGetValue(const IntProperty &prop, UChar32 c, UProperty which);
512 typedef int32_t IntPropertyGetMaxValue(const IntProperty &prop, UProperty which);
513
514 struct IntProperty {
515 int32_t column; // SRC_PROPSVEC column, or "source" if mask==0
516 uint32_t mask;
517 int32_t shift; // =maxValue if getMaxValueFromShift() is used
518 IntPropertyGetValue *getValue;
519 IntPropertyGetMaxValue *getMaxValue;
520 };
521
defaultGetValue(const IntProperty & prop,UChar32 c,UProperty)522 static int32_t defaultGetValue(const IntProperty &prop, UChar32 c, UProperty /*which*/) {
523 /* systematic, directly stored properties */
524 return (int32_t)(u_getUnicodeProperties(c, prop.column)&prop.mask)>>prop.shift;
525 }
526
defaultGetMaxValue(const IntProperty & prop,UProperty)527 static int32_t defaultGetMaxValue(const IntProperty &prop, UProperty /*which*/) {
528 return (uprv_getMaxValues(prop.column)&prop.mask)>>prop.shift;
529 }
530
getMaxValueFromShift(const IntProperty & prop,UProperty)531 static int32_t getMaxValueFromShift(const IntProperty &prop, UProperty /*which*/) {
532 return prop.shift;
533 }
534
getBiDiClass(const IntProperty &,UChar32 c,UProperty)535 static int32_t getBiDiClass(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
536 return (int32_t)u_charDirection(c);
537 }
538
getBiDiPairedBracketType(const IntProperty &,UChar32 c,UProperty)539 static int32_t getBiDiPairedBracketType(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
540 return (int32_t)ubidi_getPairedBracketType(c);
541 }
542
biDiGetMaxValue(const IntProperty &,UProperty which)543 static int32_t biDiGetMaxValue(const IntProperty &/*prop*/, UProperty which) {
544 return ubidi_getMaxValue(which);
545 }
546
547 #if UCONFIG_NO_NORMALIZATION
getCombiningClass(const IntProperty &,UChar32,UProperty)548 static int32_t getCombiningClass(const IntProperty &, UChar32, UProperty) {
549 return 0;
550 }
551 #else
getCombiningClass(const IntProperty &,UChar32 c,UProperty)552 static int32_t getCombiningClass(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
553 return u_getCombiningClass(c);
554 }
555 #endif
556
getGeneralCategory(const IntProperty &,UChar32 c,UProperty)557 static int32_t getGeneralCategory(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
558 return (int32_t)u_charType(c);
559 }
560
getJoiningGroup(const IntProperty &,UChar32 c,UProperty)561 static int32_t getJoiningGroup(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
562 return ubidi_getJoiningGroup(c);
563 }
564
getJoiningType(const IntProperty &,UChar32 c,UProperty)565 static int32_t getJoiningType(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
566 return ubidi_getJoiningType(c);
567 }
568
getNumericType(const IntProperty &,UChar32 c,UProperty)569 static int32_t getNumericType(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
570 int32_t ntv=(int32_t)GET_NUMERIC_TYPE_VALUE(u_getMainProperties(c));
571 return UPROPS_NTV_GET_TYPE(ntv);
572 }
573
getScript(const IntProperty &,UChar32 c,UProperty)574 static int32_t getScript(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
575 UErrorCode errorCode=U_ZERO_ERROR;
576 return (int32_t)uscript_getScript(c, &errorCode);
577 }
578
scriptGetMaxValue(const IntProperty &,UProperty)579 static int32_t scriptGetMaxValue(const IntProperty &/*prop*/, UProperty /*which*/) {
580 uint32_t scriptX=uprv_getMaxValues(0)&UPROPS_SCRIPT_X_MASK;
581 return uprops_mergeScriptCodeOrIndex(scriptX);
582 }
583
584 /*
585 * Map some of the Grapheme Cluster Break values to Hangul Syllable Types.
586 * Hangul_Syllable_Type is fully redundant with a subset of Grapheme_Cluster_Break.
587 */
588 static const UHangulSyllableType gcbToHst[]={
589 U_HST_NOT_APPLICABLE, /* U_GCB_OTHER */
590 U_HST_NOT_APPLICABLE, /* U_GCB_CONTROL */
591 U_HST_NOT_APPLICABLE, /* U_GCB_CR */
592 U_HST_NOT_APPLICABLE, /* U_GCB_EXTEND */
593 U_HST_LEADING_JAMO, /* U_GCB_L */
594 U_HST_NOT_APPLICABLE, /* U_GCB_LF */
595 U_HST_LV_SYLLABLE, /* U_GCB_LV */
596 U_HST_LVT_SYLLABLE, /* U_GCB_LVT */
597 U_HST_TRAILING_JAMO, /* U_GCB_T */
598 U_HST_VOWEL_JAMO /* U_GCB_V */
599 /*
600 * Omit GCB values beyond what we need for hst.
601 * The code below checks for the array length.
602 */
603 };
604
getHangulSyllableType(const IntProperty &,UChar32 c,UProperty)605 static int32_t getHangulSyllableType(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
606 /* see comments on gcbToHst[] above */
607 int32_t gcb=(int32_t)(u_getUnicodeProperties(c, 2)&UPROPS_GCB_MASK)>>UPROPS_GCB_SHIFT;
608 if(gcb<UPRV_LENGTHOF(gcbToHst)) {
609 return gcbToHst[gcb];
610 } else {
611 return U_HST_NOT_APPLICABLE;
612 }
613 }
614
615 #if UCONFIG_NO_NORMALIZATION
getNormQuickCheck(const IntProperty &,UChar32,UProperty)616 static int32_t getNormQuickCheck(const IntProperty &, UChar32, UProperty) {
617 return 0;
618 }
619 #else
getNormQuickCheck(const IntProperty &,UChar32 c,UProperty which)620 static int32_t getNormQuickCheck(const IntProperty &/*prop*/, UChar32 c, UProperty which) {
621 return (int32_t)unorm_getQuickCheck(c, (UNormalizationMode)(which-UCHAR_NFD_QUICK_CHECK+UNORM_NFD));
622 }
623 #endif
624
625 #if UCONFIG_NO_NORMALIZATION
getLeadCombiningClass(const IntProperty &,UChar32,UProperty)626 static int32_t getLeadCombiningClass(const IntProperty &, UChar32, UProperty) {
627 return 0;
628 }
629 #else
getLeadCombiningClass(const IntProperty &,UChar32 c,UProperty)630 static int32_t getLeadCombiningClass(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
631 return unorm_getFCD16(c)>>8;
632 }
633 #endif
634
635 #if UCONFIG_NO_NORMALIZATION
getTrailCombiningClass(const IntProperty &,UChar32,UProperty)636 static int32_t getTrailCombiningClass(const IntProperty &, UChar32, UProperty) {
637 return 0;
638 }
639 #else
getTrailCombiningClass(const IntProperty &,UChar32 c,UProperty)640 static int32_t getTrailCombiningClass(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
641 return unorm_getFCD16(c)&0xff;
642 }
643 #endif
644
getInPC(const IntProperty &,UChar32 c,UProperty)645 static int32_t getInPC(const IntProperty &, UChar32 c, UProperty) {
646 return ulayout_ensureData() && gInpcTrie != nullptr ? ucptrie_get(gInpcTrie, c) : 0;
647 }
648
getInSC(const IntProperty &,UChar32 c,UProperty)649 static int32_t getInSC(const IntProperty &, UChar32 c, UProperty) {
650 return ulayout_ensureData() && gInscTrie != nullptr ? ucptrie_get(gInscTrie, c) : 0;
651 }
652
getVo(const IntProperty &,UChar32 c,UProperty)653 static int32_t getVo(const IntProperty &, UChar32 c, UProperty) {
654 return ulayout_ensureData() && gVoTrie != nullptr ? ucptrie_get(gVoTrie, c) : 0;
655 }
656
layoutGetMaxValue(const IntProperty &,UProperty which)657 static int32_t layoutGetMaxValue(const IntProperty &/*prop*/, UProperty which) {
658 if (!ulayout_ensureData()) { return 0; }
659 switch (which) {
660 case UCHAR_INDIC_POSITIONAL_CATEGORY:
661 return gMaxInpcValue;
662 case UCHAR_INDIC_SYLLABIC_CATEGORY:
663 return gMaxInscValue;
664 case UCHAR_VERTICAL_ORIENTATION:
665 return gMaxVoValue;
666 default:
667 return 0;
668 }
669 }
670
getIDStatusValue(const IntProperty &,UChar32 c,UProperty)671 static int32_t getIDStatusValue(const IntProperty & /*prop*/, UChar32 c, UProperty /*which*/) {
672 uint32_t value = u_getUnicodeProperties(c, 2) >> UPROPS_2_ID_TYPE_SHIFT;
673 return value >= UPROPS_ID_TYPE_ALLOWED_MIN ? U_ID_STATUS_ALLOWED : U_ID_STATUS_RESTRICTED;
674 }
675
676 static const IntProperty intProps[UCHAR_INT_LIMIT-UCHAR_INT_START]={
677 /*
678 * column, mask and shift values for int-value properties from u_getUnicodeProperties().
679 * Must be in order of corresponding UProperty,
680 * and there must be exactly one entry per int UProperty.
681 *
682 * Properties with mask==0 are handled in code.
683 * For them, column is the UPropertySource value.
684 */
685 { UPROPS_SRC_BIDI, 0, 0, getBiDiClass, biDiGetMaxValue },
686 { 0, UPROPS_BLOCK_MASK, UPROPS_BLOCK_SHIFT, defaultGetValue, defaultGetMaxValue },
687 { UPROPS_SRC_NFC, 0, 0xff, getCombiningClass, getMaxValueFromShift },
688 { 2, UPROPS_DT_MASK, 0, defaultGetValue, defaultGetMaxValue },
689 { 0, UPROPS_EA_MASK, UPROPS_EA_SHIFT, defaultGetValue, defaultGetMaxValue },
690 { UPROPS_SRC_CHAR, 0, (int32_t)U_CHAR_CATEGORY_COUNT-1,getGeneralCategory, getMaxValueFromShift },
691 { UPROPS_SRC_BIDI, 0, 0, getJoiningGroup, biDiGetMaxValue },
692 { UPROPS_SRC_BIDI, 0, 0, getJoiningType, biDiGetMaxValue },
693 { 2, UPROPS_LB_MASK, UPROPS_LB_SHIFT, defaultGetValue, defaultGetMaxValue },
694 { UPROPS_SRC_CHAR, 0, (int32_t)U_NT_COUNT-1, getNumericType, getMaxValueFromShift },
695 { UPROPS_SRC_PROPSVEC, 0, 0, getScript, scriptGetMaxValue },
696 { UPROPS_SRC_PROPSVEC, 0, (int32_t)U_HST_COUNT-1, getHangulSyllableType, getMaxValueFromShift },
697 // UCHAR_NFD_QUICK_CHECK: max=1=YES -- never "maybe", only "no" or "yes"
698 { UPROPS_SRC_NFC, 0, (int32_t)UNORM_YES, getNormQuickCheck, getMaxValueFromShift },
699 // UCHAR_NFKD_QUICK_CHECK: max=1=YES -- never "maybe", only "no" or "yes"
700 { UPROPS_SRC_NFKC, 0, (int32_t)UNORM_YES, getNormQuickCheck, getMaxValueFromShift },
701 // UCHAR_NFC_QUICK_CHECK: max=2=MAYBE
702 { UPROPS_SRC_NFC, 0, (int32_t)UNORM_MAYBE, getNormQuickCheck, getMaxValueFromShift },
703 // UCHAR_NFKC_QUICK_CHECK: max=2=MAYBE
704 { UPROPS_SRC_NFKC, 0, (int32_t)UNORM_MAYBE, getNormQuickCheck, getMaxValueFromShift },
705 { UPROPS_SRC_NFC, 0, 0xff, getLeadCombiningClass, getMaxValueFromShift },
706 { UPROPS_SRC_NFC, 0, 0xff, getTrailCombiningClass, getMaxValueFromShift },
707 { 2, UPROPS_GCB_MASK, UPROPS_GCB_SHIFT, defaultGetValue, defaultGetMaxValue },
708 { 2, UPROPS_SB_MASK, UPROPS_SB_SHIFT, defaultGetValue, defaultGetMaxValue },
709 { 2, UPROPS_WB_MASK, UPROPS_WB_SHIFT, defaultGetValue, defaultGetMaxValue },
710 { UPROPS_SRC_BIDI, 0, 0, getBiDiPairedBracketType, biDiGetMaxValue },
711 { UPROPS_SRC_INPC, 0, 0, getInPC, layoutGetMaxValue },
712 { UPROPS_SRC_INSC, 0, 0, getInSC, layoutGetMaxValue },
713 { UPROPS_SRC_VO, 0, 0, getVo, layoutGetMaxValue },
714 { UPROPS_SRC_PROPSVEC, 0, (int32_t)U_ID_STATUS_ALLOWED, getIDStatusValue, getMaxValueFromShift },
715 };
716
717 U_CAPI int32_t U_EXPORT2
u_getIntPropertyValue(UChar32 c,UProperty which)718 u_getIntPropertyValue(UChar32 c, UProperty which) {
719 if(which<UCHAR_INT_START) {
720 if(UCHAR_BINARY_START<=which && which<UCHAR_BINARY_LIMIT) {
721 const BinaryProperty &prop=binProps[which];
722 return prop.contains(prop, c, which);
723 }
724 } else if(which<UCHAR_INT_LIMIT) {
725 const IntProperty &prop=intProps[which-UCHAR_INT_START];
726 return prop.getValue(prop, c, which);
727 } else if(which==UCHAR_GENERAL_CATEGORY_MASK) {
728 return U_MASK(u_charType(c));
729 }
730 return 0; // undefined
731 }
732
733 U_CAPI int32_t U_EXPORT2
u_getIntPropertyMinValue(UProperty)734 u_getIntPropertyMinValue(UProperty /*which*/) {
735 return 0; /* all binary/enum/int properties have a minimum value of 0 */
736 }
737
738 U_CAPI int32_t U_EXPORT2
u_getIntPropertyMaxValue(UProperty which)739 u_getIntPropertyMaxValue(UProperty which) {
740 if(which<UCHAR_INT_START) {
741 if(UCHAR_BINARY_START<=which && which<UCHAR_BINARY_LIMIT) {
742 return 1; // maximum true for all binary properties
743 }
744 } else if(which<UCHAR_INT_LIMIT) {
745 const IntProperty &prop=intProps[which-UCHAR_INT_START];
746 return prop.getMaxValue(prop, which);
747 }
748 return -1; // undefined
749 }
750
751 U_CFUNC UPropertySource U_EXPORT2
uprops_getSource(UProperty which)752 uprops_getSource(UProperty which) {
753 if(which<UCHAR_BINARY_START) {
754 return UPROPS_SRC_NONE; /* undefined */
755 } else if(which<UCHAR_BINARY_LIMIT) {
756 const BinaryProperty &prop=binProps[which];
757 if(prop.mask!=0) {
758 return UPROPS_SRC_PROPSVEC;
759 } else {
760 return (UPropertySource)prop.column;
761 }
762 } else if(which<UCHAR_INT_START) {
763 return UPROPS_SRC_NONE; /* undefined */
764 } else if(which<UCHAR_INT_LIMIT) {
765 const IntProperty &prop=intProps[which-UCHAR_INT_START];
766 if(prop.mask!=0) {
767 return UPROPS_SRC_PROPSVEC;
768 } else {
769 return (UPropertySource)prop.column;
770 }
771 } else if(which<UCHAR_STRING_START) {
772 switch(which) {
773 case UCHAR_GENERAL_CATEGORY_MASK:
774 case UCHAR_NUMERIC_VALUE:
775 return UPROPS_SRC_CHAR;
776
777 default:
778 return UPROPS_SRC_NONE;
779 }
780 } else if(which<UCHAR_STRING_LIMIT) {
781 switch(which) {
782 case UCHAR_AGE:
783 return UPROPS_SRC_PROPSVEC;
784
785 case UCHAR_BIDI_MIRRORING_GLYPH:
786 return UPROPS_SRC_BIDI;
787
788 case UCHAR_CASE_FOLDING:
789 case UCHAR_LOWERCASE_MAPPING:
790 case UCHAR_SIMPLE_CASE_FOLDING:
791 case UCHAR_SIMPLE_LOWERCASE_MAPPING:
792 case UCHAR_SIMPLE_TITLECASE_MAPPING:
793 case UCHAR_SIMPLE_UPPERCASE_MAPPING:
794 case UCHAR_TITLECASE_MAPPING:
795 case UCHAR_UPPERCASE_MAPPING:
796 return UPROPS_SRC_CASE;
797
798 case UCHAR_ISO_COMMENT:
799 case UCHAR_NAME:
800 case UCHAR_UNICODE_1_NAME:
801 return UPROPS_SRC_NAMES;
802
803 default:
804 return UPROPS_SRC_NONE;
805 }
806 } else {
807 switch(which) {
808 case UCHAR_SCRIPT_EXTENSIONS:
809 case UCHAR_IDENTIFIER_TYPE:
810 return UPROPS_SRC_PROPSVEC;
811 default:
812 return UPROPS_SRC_NONE; /* undefined */
813 }
814 }
815 }
816
817 U_CFUNC void U_EXPORT2
uprops_addPropertyStarts(UPropertySource src,const USetAdder * sa,UErrorCode * pErrorCode)818 uprops_addPropertyStarts(UPropertySource src, const USetAdder *sa, UErrorCode *pErrorCode) {
819 if (U_FAILURE(*pErrorCode)) { return; }
820 if (src == UPROPS_SRC_ID_COMPAT_MATH) {
821 // range limits
822 for (UChar32 c : ID_COMPAT_MATH_CONTINUE) {
823 sa->add(sa->set, c);
824 }
825 // single characters
826 for (UChar32 c : ID_COMPAT_MATH_START) {
827 sa->add(sa->set, c);
828 sa->add(sa->set, c + 1);
829 }
830 return;
831 }
832 if (!ulayout_ensureData(*pErrorCode)) { return; }
833 const UCPTrie *trie;
834 switch (src) {
835 case UPROPS_SRC_INPC:
836 trie = gInpcTrie;
837 break;
838 case UPROPS_SRC_INSC:
839 trie = gInscTrie;
840 break;
841 case UPROPS_SRC_VO:
842 trie = gVoTrie;
843 break;
844 default:
845 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
846 return;
847 }
848
849 if (trie == nullptr) {
850 *pErrorCode = U_MISSING_RESOURCE_ERROR;
851 return;
852 }
853
854 // Add the start code point of each same-value range of the trie.
855 UChar32 start = 0, end;
856 while ((end = ucptrie_getRange(trie, start, UCPMAP_RANGE_NORMAL, 0,
857 nullptr, nullptr, nullptr)) >= 0) {
858 sa->add(sa->set, start);
859 start = end + 1;
860 }
861 }
862
863 U_CAPI bool U_EXPORT2
u_hasIDType(UChar32 c,UIdentifierType type)864 u_hasIDType(UChar32 c, UIdentifierType type) {
865 uint32_t typeIndex = type; // also guards against negative type integers
866 if (typeIndex >= UPRV_LENGTHOF(uprops_idTypeToEncoded)) {
867 return false;
868 }
869 uint32_t encodedType = uprops_idTypeToEncoded[typeIndex];
870 uint32_t value = u_getUnicodeProperties(c, 2) >> UPROPS_2_ID_TYPE_SHIFT;
871 if ((encodedType & UPROPS_ID_TYPE_BIT) != 0) {
872 return value < UPROPS_ID_TYPE_FORBIDDEN && (value & encodedType) != 0;
873 } else {
874 return value == encodedType;
875 }
876 }
877
878 namespace {
879
maybeAppendType(uint32_t value,uint32_t bit,UIdentifierType t,UIdentifierType * types,int32_t & length,int32_t capacity)880 void maybeAppendType(uint32_t value, uint32_t bit, UIdentifierType t,
881 UIdentifierType *types, int32_t &length, int32_t capacity) {
882 if ((value & bit) != 0) {
883 if (length < capacity) {
884 types[length] = t;
885 }
886 ++length;
887 }
888 }
889
890 } // namespace
891
892 U_CAPI int32_t U_EXPORT2
u_getIDTypes(UChar32 c,UIdentifierType * types,int32_t capacity,UErrorCode * pErrorCode)893 u_getIDTypes(UChar32 c, UIdentifierType *types, int32_t capacity, UErrorCode *pErrorCode) {
894 if (U_FAILURE(*pErrorCode)) { return 0; }
895 if (capacity < 0 || (capacity > 0 && types == nullptr)) {
896 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
897 return 0;
898 }
899 uint32_t value = u_getUnicodeProperties(c, 2) >> UPROPS_2_ID_TYPE_SHIFT;
900 if ((value & UPROPS_ID_TYPE_FORBIDDEN) == UPROPS_ID_TYPE_FORBIDDEN ||
901 value == UPROPS_ID_TYPE_NOT_CHARACTER) {
902 // single value
903 if (capacity > 0) {
904 UIdentifierType t;
905 switch (value) {
906 case UPROPS_ID_TYPE_NOT_CHARACTER: t = U_ID_TYPE_NOT_CHARACTER; break;
907 case UPROPS_ID_TYPE_DEPRECATED: t = U_ID_TYPE_DEPRECATED; break;
908 case UPROPS_ID_TYPE_DEFAULT_IGNORABLE: t = U_ID_TYPE_DEFAULT_IGNORABLE; break;
909 case UPROPS_ID_TYPE_NOT_NFKC: t = U_ID_TYPE_NOT_NFKC; break;
910 case UPROPS_ID_TYPE_INCLUSION: t = U_ID_TYPE_INCLUSION; break;
911 case UPROPS_ID_TYPE_RECOMMENDED: t = U_ID_TYPE_RECOMMENDED; break;
912 default:
913 *pErrorCode = U_INVALID_FORMAT_ERROR;
914 return 0;
915 }
916 types[0] = t;
917 } else {
918 *pErrorCode = U_BUFFER_OVERFLOW_ERROR;
919 }
920 return 1;
921 } else {
922 // one or more combinable bits
923 int32_t length = 0;
924 maybeAppendType(value, UPROPS_ID_TYPE_NOT_XID, U_ID_TYPE_NOT_XID,
925 types, length, capacity);
926 maybeAppendType(value, UPROPS_ID_TYPE_EXCLUSION, U_ID_TYPE_EXCLUSION,
927 types, length, capacity);
928 maybeAppendType(value, UPROPS_ID_TYPE_OBSOLETE, U_ID_TYPE_OBSOLETE,
929 types, length, capacity);
930 maybeAppendType(value, UPROPS_ID_TYPE_TECHNICAL, U_ID_TYPE_TECHNICAL,
931 types, length, capacity);
932 maybeAppendType(value, UPROPS_ID_TYPE_UNCOMMON_USE, U_ID_TYPE_UNCOMMON_USE,
933 types, length, capacity);
934 maybeAppendType(value, UPROPS_ID_TYPE_LIMITED_USE, U_ID_TYPE_LIMITED_USE,
935 types, length, capacity);
936 if (length >= capacity) {
937 *pErrorCode = U_BUFFER_OVERFLOW_ERROR;
938 }
939 return length;
940 }
941 }
942
943 #if !UCONFIG_NO_NORMALIZATION
944
945 U_CAPI int32_t U_EXPORT2
u_getFC_NFKC_Closure(UChar32 c,char16_t * dest,int32_t destCapacity,UErrorCode * pErrorCode)946 u_getFC_NFKC_Closure(UChar32 c, char16_t *dest, int32_t destCapacity, UErrorCode *pErrorCode) {
947 if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
948 return 0;
949 }
950 if(destCapacity<0 || (dest==nullptr && destCapacity>0)) {
951 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
952 return 0;
953 }
954 // Compute the FC_NFKC_Closure on the fly:
955 // We have the API for complete coverage of Unicode properties, although
956 // this value by itself is not useful via API.
957 // (What could be useful is a custom normalization table that combines
958 // case folding and NFKC.)
959 // For the derivation, see Unicode's DerivedNormalizationProps.txt.
960 const Normalizer2 *nfkc=Normalizer2::getNFKCInstance(*pErrorCode);
961 if(U_FAILURE(*pErrorCode)) {
962 return 0;
963 }
964 // first: b = NFKC(Fold(a))
965 UnicodeString folded1String;
966 const char16_t *folded1;
967 int32_t folded1Length=ucase_toFullFolding(c, &folded1, U_FOLD_CASE_DEFAULT);
968 if(folded1Length<0) {
969 const Normalizer2Impl *nfkcImpl=Normalizer2Factory::getImpl(nfkc);
970 if(nfkcImpl->getCompQuickCheck(nfkcImpl->getNorm16(c))!=UNORM_NO) {
971 return u_terminateUChars(dest, destCapacity, 0, pErrorCode); // c does not change at all under CaseFolding+NFKC
972 }
973 folded1String.setTo(c);
974 } else {
975 if(folded1Length>UCASE_MAX_STRING_LENGTH) {
976 folded1String.setTo(folded1Length);
977 } else {
978 folded1String.setTo(false, folded1, folded1Length);
979 }
980 }
981 UnicodeString kc1=nfkc->normalize(folded1String, *pErrorCode);
982 // second: c = NFKC(Fold(b))
983 UnicodeString folded2String(kc1);
984 UnicodeString kc2=nfkc->normalize(folded2String.foldCase(), *pErrorCode);
985 // if (c != b) add the mapping from a to c
986 if(U_FAILURE(*pErrorCode) || kc1==kc2) {
987 return u_terminateUChars(dest, destCapacity, 0, pErrorCode);
988 } else {
989 return kc2.extract(dest, destCapacity, *pErrorCode);
990 }
991 }
992
993 #endif
994