1 // Copyright (C) 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:   US-ASCII
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/unorm2.h"
29 #include "unicode/uscript.h"
30 #include "unicode/ustring.h"
31 #include "cstring.h"
32 #include "normalizer2impl.h"
33 #include "umutex.h"
34 #include "ubidi_props.h"
35 #include "uprops.h"
36 #include "ucase.h"
37 #include "ustr_imp.h"
38 
39 U_NAMESPACE_USE
40 
41 #define GET_BIDI_PROPS() ubidi_getSingleton()
42 
43 /* general properties API functions ----------------------------------------- */
44 
45 struct BinaryProperty;
46 
47 typedef UBool BinaryPropertyContains(const BinaryProperty &prop, UChar32 c, UProperty which);
48 
49 struct BinaryProperty {
50     int32_t column;  // SRC_PROPSVEC column, or "source" if mask==0
51     uint32_t mask;
52     BinaryPropertyContains *contains;
53 };
54 
defaultContains(const BinaryProperty & prop,UChar32 c,UProperty)55 static UBool defaultContains(const BinaryProperty &prop, UChar32 c, UProperty /*which*/) {
56     /* systematic, directly stored properties */
57     return (u_getUnicodeProperties(c, prop.column)&prop.mask)!=0;
58 }
59 
caseBinaryPropertyContains(const BinaryProperty &,UChar32 c,UProperty which)60 static UBool caseBinaryPropertyContains(const BinaryProperty &/*prop*/, UChar32 c, UProperty which) {
61     return ucase_hasBinaryProperty(c, which);
62 }
63 
isBidiControl(const BinaryProperty &,UChar32 c,UProperty)64 static UBool isBidiControl(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
65     return ubidi_isBidiControl(GET_BIDI_PROPS(), c);
66 }
67 
isMirrored(const BinaryProperty &,UChar32 c,UProperty)68 static UBool isMirrored(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
69     return ubidi_isMirrored(GET_BIDI_PROPS(), c);
70 }
71 
isJoinControl(const BinaryProperty &,UChar32 c,UProperty)72 static UBool isJoinControl(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
73     return ubidi_isJoinControl(GET_BIDI_PROPS(), c);
74 }
75 
76 #if UCONFIG_NO_NORMALIZATION
hasFullCompositionExclusion(const BinaryProperty &,UChar32,UProperty)77 static UBool hasFullCompositionExclusion(const BinaryProperty &, UChar32, UProperty) {
78     return FALSE;
79 }
80 #else
hasFullCompositionExclusion(const BinaryProperty &,UChar32 c,UProperty)81 static UBool hasFullCompositionExclusion(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
82     // By definition, Full_Composition_Exclusion is the same as NFC_QC=No.
83     UErrorCode errorCode=U_ZERO_ERROR;
84     const Normalizer2Impl *impl=Normalizer2Factory::getNFCImpl(errorCode);
85     return U_SUCCESS(errorCode) && impl->isCompNo(impl->getNorm16(c));
86 }
87 #endif
88 
89 // UCHAR_NF*_INERT properties
90 #if UCONFIG_NO_NORMALIZATION
isNormInert(const BinaryProperty &,UChar32,UProperty)91 static UBool isNormInert(const BinaryProperty &, UChar32, UProperty) {
92     return FALSE;
93 }
94 #else
isNormInert(const BinaryProperty &,UChar32 c,UProperty which)95 static UBool isNormInert(const BinaryProperty &/*prop*/, UChar32 c, UProperty which) {
96     UErrorCode errorCode=U_ZERO_ERROR;
97     const Normalizer2 *norm2=Normalizer2Factory::getInstance(
98         (UNormalizationMode)(which-UCHAR_NFD_INERT+UNORM_NFD), errorCode);
99     return U_SUCCESS(errorCode) && norm2->isInert(c);
100 }
101 #endif
102 
103 #if UCONFIG_NO_NORMALIZATION
changesWhenCasefolded(const BinaryProperty &,UChar32,UProperty)104 static UBool changesWhenCasefolded(const BinaryProperty &, UChar32, UProperty) {
105     return FALSE;
106 }
107 #else
changesWhenCasefolded(const BinaryProperty &,UChar32 c,UProperty)108 static UBool changesWhenCasefolded(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
109     UnicodeString nfd;
110     UErrorCode errorCode=U_ZERO_ERROR;
111     const Normalizer2 *nfcNorm2=Normalizer2::getNFCInstance(errorCode);
112     if(U_FAILURE(errorCode)) {
113         return FALSE;
114     }
115     if(nfcNorm2->getDecomposition(c, nfd)) {
116         /* c has a decomposition */
117         if(nfd.length()==1) {
118             c=nfd[0];  /* single BMP code point */
119         } else if(nfd.length()<=U16_MAX_LENGTH &&
120                   nfd.length()==U16_LENGTH(c=nfd.char32At(0))
121         ) {
122             /* single supplementary code point */
123         } else {
124             c=U_SENTINEL;
125         }
126     } else if(c<0) {
127         return FALSE;  /* protect against bad input */
128     }
129     if(c>=0) {
130         /* single code point */
131         const UCaseProps *csp=ucase_getSingleton();
132         const UChar *resultString;
133         return (UBool)(ucase_toFullFolding(csp, c, &resultString, U_FOLD_CASE_DEFAULT)>=0);
134     } else {
135         /* guess some large but stack-friendly capacity */
136         UChar dest[2*UCASE_MAX_STRING_LENGTH];
137         int32_t destLength;
138         destLength=u_strFoldCase(dest, UPRV_LENGTHOF(dest),
139                                   nfd.getBuffer(), nfd.length(),
140                                   U_FOLD_CASE_DEFAULT, &errorCode);
141         return (UBool)(U_SUCCESS(errorCode) &&
142                        0!=u_strCompare(nfd.getBuffer(), nfd.length(),
143                                        dest, destLength, FALSE));
144     }
145 }
146 #endif
147 
148 #if UCONFIG_NO_NORMALIZATION
changesWhenNFKC_Casefolded(const BinaryProperty &,UChar32,UProperty)149 static UBool changesWhenNFKC_Casefolded(const BinaryProperty &, UChar32, UProperty) {
150     return FALSE;
151 }
152 #else
changesWhenNFKC_Casefolded(const BinaryProperty &,UChar32 c,UProperty)153 static UBool changesWhenNFKC_Casefolded(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
154     UErrorCode errorCode=U_ZERO_ERROR;
155     const Normalizer2Impl *kcf=Normalizer2Factory::getNFKC_CFImpl(errorCode);
156     if(U_FAILURE(errorCode)) {
157         return FALSE;
158     }
159     UnicodeString src(c);
160     UnicodeString dest;
161     {
162         // The ReorderingBuffer must be in a block because its destructor
163         // needs to release dest's buffer before we look at its contents.
164         ReorderingBuffer buffer(*kcf, dest);
165         // Small destCapacity for NFKC_CF(c).
166         if(buffer.init(5, errorCode)) {
167             const UChar *srcArray=src.getBuffer();
168             kcf->compose(srcArray, srcArray+src.length(), FALSE,
169                           TRUE, buffer, errorCode);
170         }
171     }
172     return U_SUCCESS(errorCode) && dest!=src;
173 }
174 #endif
175 
176 #if UCONFIG_NO_NORMALIZATION
isCanonSegmentStarter(const BinaryProperty &,UChar32,UProperty)177 static UBool isCanonSegmentStarter(const BinaryProperty &, UChar32, UProperty) {
178     return FALSE;
179 }
180 #else
isCanonSegmentStarter(const BinaryProperty &,UChar32 c,UProperty)181 static UBool isCanonSegmentStarter(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
182     UErrorCode errorCode=U_ZERO_ERROR;
183     const Normalizer2Impl *impl=Normalizer2Factory::getNFCImpl(errorCode);
184     return
185         U_SUCCESS(errorCode) && impl->ensureCanonIterData(errorCode) &&
186         impl->isCanonSegmentStarter(c);
187 }
188 #endif
189 
isPOSIX_alnum(const BinaryProperty &,UChar32 c,UProperty)190 static UBool isPOSIX_alnum(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
191     return u_isalnumPOSIX(c);
192 }
193 
isPOSIX_blank(const BinaryProperty &,UChar32 c,UProperty)194 static UBool isPOSIX_blank(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
195     return u_isblank(c);
196 }
197 
isPOSIX_graph(const BinaryProperty &,UChar32 c,UProperty)198 static UBool isPOSIX_graph(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
199     return u_isgraphPOSIX(c);
200 }
201 
isPOSIX_print(const BinaryProperty &,UChar32 c,UProperty)202 static UBool isPOSIX_print(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
203     return u_isprintPOSIX(c);
204 }
205 
isPOSIX_xdigit(const BinaryProperty &,UChar32 c,UProperty)206 static UBool isPOSIX_xdigit(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
207     return u_isxdigit(c);
208 }
209 
210 static const BinaryProperty binProps[UCHAR_BINARY_LIMIT]={
211     /*
212      * column and mask values for binary properties from u_getUnicodeProperties().
213      * Must be in order of corresponding UProperty,
214      * and there must be exactly one entry per binary UProperty.
215      *
216      * Properties with mask==0 are handled in code.
217      * For them, column is the UPropertySource value.
218      */
219     { 1,                U_MASK(UPROPS_ALPHABETIC), defaultContains },
220     { 1,                U_MASK(UPROPS_ASCII_HEX_DIGIT), defaultContains },
221     { UPROPS_SRC_BIDI,  0, isBidiControl },
222     { UPROPS_SRC_BIDI,  0, isMirrored },
223     { 1,                U_MASK(UPROPS_DASH), defaultContains },
224     { 1,                U_MASK(UPROPS_DEFAULT_IGNORABLE_CODE_POINT), defaultContains },
225     { 1,                U_MASK(UPROPS_DEPRECATED), defaultContains },
226     { 1,                U_MASK(UPROPS_DIACRITIC), defaultContains },
227     { 1,                U_MASK(UPROPS_EXTENDER), defaultContains },
228     { UPROPS_SRC_NFC,   0, hasFullCompositionExclusion },
229     { 1,                U_MASK(UPROPS_GRAPHEME_BASE), defaultContains },
230     { 1,                U_MASK(UPROPS_GRAPHEME_EXTEND), defaultContains },
231     { 1,                U_MASK(UPROPS_GRAPHEME_LINK), defaultContains },
232     { 1,                U_MASK(UPROPS_HEX_DIGIT), defaultContains },
233     { 1,                U_MASK(UPROPS_HYPHEN), defaultContains },
234     { 1,                U_MASK(UPROPS_ID_CONTINUE), defaultContains },
235     { 1,                U_MASK(UPROPS_ID_START), defaultContains },
236     { 1,                U_MASK(UPROPS_IDEOGRAPHIC), defaultContains },
237     { 1,                U_MASK(UPROPS_IDS_BINARY_OPERATOR), defaultContains },
238     { 1,                U_MASK(UPROPS_IDS_TRINARY_OPERATOR), defaultContains },
239     { UPROPS_SRC_BIDI,  0, isJoinControl },
240     { 1,                U_MASK(UPROPS_LOGICAL_ORDER_EXCEPTION), defaultContains },
241     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_LOWERCASE
242     { 1,                U_MASK(UPROPS_MATH), defaultContains },
243     { 1,                U_MASK(UPROPS_NONCHARACTER_CODE_POINT), defaultContains },
244     { 1,                U_MASK(UPROPS_QUOTATION_MARK), defaultContains },
245     { 1,                U_MASK(UPROPS_RADICAL), defaultContains },
246     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_SOFT_DOTTED
247     { 1,                U_MASK(UPROPS_TERMINAL_PUNCTUATION), defaultContains },
248     { 1,                U_MASK(UPROPS_UNIFIED_IDEOGRAPH), defaultContains },
249     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_UPPERCASE
250     { 1,                U_MASK(UPROPS_WHITE_SPACE), defaultContains },
251     { 1,                U_MASK(UPROPS_XID_CONTINUE), defaultContains },
252     { 1,                U_MASK(UPROPS_XID_START), defaultContains },
253     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_CASE_SENSITIVE
254     { 1,                U_MASK(UPROPS_S_TERM), defaultContains },
255     { 1,                U_MASK(UPROPS_VARIATION_SELECTOR), defaultContains },
256     { UPROPS_SRC_NFC,   0, isNormInert },  // UCHAR_NFD_INERT
257     { UPROPS_SRC_NFKC,  0, isNormInert },  // UCHAR_NFKD_INERT
258     { UPROPS_SRC_NFC,   0, isNormInert },  // UCHAR_NFC_INERT
259     { UPROPS_SRC_NFKC,  0, isNormInert },  // UCHAR_NFKC_INERT
260     { UPROPS_SRC_NFC_CANON_ITER, 0, isCanonSegmentStarter },
261     { 1,                U_MASK(UPROPS_PATTERN_SYNTAX), defaultContains },
262     { 1,                U_MASK(UPROPS_PATTERN_WHITE_SPACE), defaultContains },
263     { UPROPS_SRC_CHAR_AND_PROPSVEC,  0, isPOSIX_alnum },
264     { UPROPS_SRC_CHAR,  0, isPOSIX_blank },
265     { UPROPS_SRC_CHAR,  0, isPOSIX_graph },
266     { UPROPS_SRC_CHAR,  0, isPOSIX_print },
267     { UPROPS_SRC_CHAR,  0, isPOSIX_xdigit },
268     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_CASED
269     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_CASE_IGNORABLE
270     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_CHANGES_WHEN_LOWERCASED
271     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_CHANGES_WHEN_UPPERCASED
272     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_CHANGES_WHEN_TITLECASED
273     { UPROPS_SRC_CASE_AND_NORM,  0, changesWhenCasefolded },
274     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_CHANGES_WHEN_CASEMAPPED
275     { UPROPS_SRC_NFKC_CF, 0, changesWhenNFKC_Casefolded },
276     { 2,                U_MASK(UPROPS_2_EMOJI), defaultContains },
277     { 2,                U_MASK(UPROPS_2_EMOJI_PRESENTATION), defaultContains },
278     { 2,                U_MASK(UPROPS_2_EMOJI_MODIFIER), defaultContains },
279     { 2,                U_MASK(UPROPS_2_EMOJI_MODIFIER_BASE), defaultContains },
280 };
281 
282 U_CAPI UBool U_EXPORT2
u_hasBinaryProperty(UChar32 c,UProperty which)283 u_hasBinaryProperty(UChar32 c, UProperty which) {
284     /* c is range-checked in the functions that are called from here */
285     if(which<UCHAR_BINARY_START || UCHAR_BINARY_LIMIT<=which) {
286         /* not a known binary property */
287         return FALSE;
288     } else {
289         const BinaryProperty &prop=binProps[which];
290         return prop.contains(prop, c, which);
291     }
292 }
293 
294 struct IntProperty;
295 
296 typedef int32_t IntPropertyGetValue(const IntProperty &prop, UChar32 c, UProperty which);
297 typedef int32_t IntPropertyGetMaxValue(const IntProperty &prop, UProperty which);
298 
299 struct IntProperty {
300     int32_t column;  // SRC_PROPSVEC column, or "source" if mask==0
301     uint32_t mask;
302     int32_t shift;  // =maxValue if getMaxValueFromShift() is used
303     IntPropertyGetValue *getValue;
304     IntPropertyGetMaxValue *getMaxValue;
305 };
306 
defaultGetValue(const IntProperty & prop,UChar32 c,UProperty)307 static int32_t defaultGetValue(const IntProperty &prop, UChar32 c, UProperty /*which*/) {
308     /* systematic, directly stored properties */
309     return (int32_t)(u_getUnicodeProperties(c, prop.column)&prop.mask)>>prop.shift;
310 }
311 
defaultGetMaxValue(const IntProperty & prop,UProperty)312 static int32_t defaultGetMaxValue(const IntProperty &prop, UProperty /*which*/) {
313     return (uprv_getMaxValues(prop.column)&prop.mask)>>prop.shift;
314 }
315 
getMaxValueFromShift(const IntProperty & prop,UProperty)316 static int32_t getMaxValueFromShift(const IntProperty &prop, UProperty /*which*/) {
317     return prop.shift;
318 }
319 
getBiDiClass(const IntProperty &,UChar32 c,UProperty)320 static int32_t getBiDiClass(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
321     return (int32_t)u_charDirection(c);
322 }
323 
getBiDiPairedBracketType(const IntProperty &,UChar32 c,UProperty)324 static int32_t getBiDiPairedBracketType(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
325     return (int32_t)ubidi_getPairedBracketType(GET_BIDI_PROPS(), c);
326 }
327 
biDiGetMaxValue(const IntProperty &,UProperty which)328 static int32_t biDiGetMaxValue(const IntProperty &/*prop*/, UProperty which) {
329     return ubidi_getMaxValue(GET_BIDI_PROPS(), which);
330 }
331 
332 #if UCONFIG_NO_NORMALIZATION
getCombiningClass(const IntProperty &,UChar32,UProperty)333 static int32_t getCombiningClass(const IntProperty &, UChar32, UProperty) {
334     return 0;
335 }
336 #else
getCombiningClass(const IntProperty &,UChar32 c,UProperty)337 static int32_t getCombiningClass(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
338     return u_getCombiningClass(c);
339 }
340 #endif
341 
getGeneralCategory(const IntProperty &,UChar32 c,UProperty)342 static int32_t getGeneralCategory(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
343     return (int32_t)u_charType(c);
344 }
345 
getJoiningGroup(const IntProperty &,UChar32 c,UProperty)346 static int32_t getJoiningGroup(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
347     return ubidi_getJoiningGroup(GET_BIDI_PROPS(), c);
348 }
349 
getJoiningType(const IntProperty &,UChar32 c,UProperty)350 static int32_t getJoiningType(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
351     return ubidi_getJoiningType(GET_BIDI_PROPS(), c);
352 }
353 
getNumericType(const IntProperty &,UChar32 c,UProperty)354 static int32_t getNumericType(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
355     int32_t ntv=(int32_t)GET_NUMERIC_TYPE_VALUE(u_getMainProperties(c));
356     return UPROPS_NTV_GET_TYPE(ntv);
357 }
358 
getScript(const IntProperty &,UChar32 c,UProperty)359 static int32_t getScript(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
360     UErrorCode errorCode=U_ZERO_ERROR;
361     return (int32_t)uscript_getScript(c, &errorCode);
362 }
363 
364 /*
365  * Map some of the Grapheme Cluster Break values to Hangul Syllable Types.
366  * Hangul_Syllable_Type is fully redundant with a subset of Grapheme_Cluster_Break.
367  */
368 static const UHangulSyllableType gcbToHst[]={
369     U_HST_NOT_APPLICABLE,   /* U_GCB_OTHER */
370     U_HST_NOT_APPLICABLE,   /* U_GCB_CONTROL */
371     U_HST_NOT_APPLICABLE,   /* U_GCB_CR */
372     U_HST_NOT_APPLICABLE,   /* U_GCB_EXTEND */
373     U_HST_LEADING_JAMO,     /* U_GCB_L */
374     U_HST_NOT_APPLICABLE,   /* U_GCB_LF */
375     U_HST_LV_SYLLABLE,      /* U_GCB_LV */
376     U_HST_LVT_SYLLABLE,     /* U_GCB_LVT */
377     U_HST_TRAILING_JAMO,    /* U_GCB_T */
378     U_HST_VOWEL_JAMO        /* U_GCB_V */
379     /*
380      * Omit GCB values beyond what we need for hst.
381      * The code below checks for the array length.
382      */
383 };
384 
getHangulSyllableType(const IntProperty &,UChar32 c,UProperty)385 static int32_t getHangulSyllableType(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
386     /* see comments on gcbToHst[] above */
387     int32_t gcb=(int32_t)(u_getUnicodeProperties(c, 2)&UPROPS_GCB_MASK)>>UPROPS_GCB_SHIFT;
388     if(gcb<UPRV_LENGTHOF(gcbToHst)) {
389         return gcbToHst[gcb];
390     } else {
391         return U_HST_NOT_APPLICABLE;
392     }
393 }
394 
395 #if UCONFIG_NO_NORMALIZATION
getNormQuickCheck(const IntProperty &,UChar32,UProperty)396 static int32_t getNormQuickCheck(const IntProperty &, UChar32, UProperty) {
397     return 0;
398 }
399 #else
getNormQuickCheck(const IntProperty &,UChar32 c,UProperty which)400 static int32_t getNormQuickCheck(const IntProperty &/*prop*/, UChar32 c, UProperty which) {
401     return (int32_t)unorm_getQuickCheck(c, (UNormalizationMode)(which-UCHAR_NFD_QUICK_CHECK+UNORM_NFD));
402 }
403 #endif
404 
405 #if UCONFIG_NO_NORMALIZATION
getLeadCombiningClass(const IntProperty &,UChar32,UProperty)406 static int32_t getLeadCombiningClass(const IntProperty &, UChar32, UProperty) {
407     return 0;
408 }
409 #else
getLeadCombiningClass(const IntProperty &,UChar32 c,UProperty)410 static int32_t getLeadCombiningClass(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
411     return unorm_getFCD16(c)>>8;
412 }
413 #endif
414 
415 #if UCONFIG_NO_NORMALIZATION
getTrailCombiningClass(const IntProperty &,UChar32,UProperty)416 static int32_t getTrailCombiningClass(const IntProperty &, UChar32, UProperty) {
417     return 0;
418 }
419 #else
getTrailCombiningClass(const IntProperty &,UChar32 c,UProperty)420 static int32_t getTrailCombiningClass(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
421     return unorm_getFCD16(c)&0xff;
422 }
423 #endif
424 
425 static const IntProperty intProps[UCHAR_INT_LIMIT-UCHAR_INT_START]={
426     /*
427      * column, mask and shift values for int-value properties from u_getUnicodeProperties().
428      * Must be in order of corresponding UProperty,
429      * and there must be exactly one entry per int UProperty.
430      *
431      * Properties with mask==0 are handled in code.
432      * For them, column is the UPropertySource value.
433      */
434     { UPROPS_SRC_BIDI,  0, 0,                               getBiDiClass, biDiGetMaxValue },
435     { 0,                UPROPS_BLOCK_MASK, UPROPS_BLOCK_SHIFT, defaultGetValue, defaultGetMaxValue },
436     { UPROPS_SRC_NFC,   0, 0xff,                            getCombiningClass, getMaxValueFromShift },
437     { 2,                UPROPS_DT_MASK, 0,                  defaultGetValue, defaultGetMaxValue },
438     { 0,                UPROPS_EA_MASK, UPROPS_EA_SHIFT,    defaultGetValue, defaultGetMaxValue },
439     { UPROPS_SRC_CHAR,  0, (int32_t)U_CHAR_CATEGORY_COUNT-1,getGeneralCategory, getMaxValueFromShift },
440     { UPROPS_SRC_BIDI,  0, 0,                               getJoiningGroup, biDiGetMaxValue },
441     { UPROPS_SRC_BIDI,  0, 0,                               getJoiningType, biDiGetMaxValue },
442     { 2,                UPROPS_LB_MASK, UPROPS_LB_SHIFT,    defaultGetValue, defaultGetMaxValue },
443     { UPROPS_SRC_CHAR,  0, (int32_t)U_NT_COUNT-1,           getNumericType, getMaxValueFromShift },
444     { 0,                UPROPS_SCRIPT_MASK, 0,              getScript, defaultGetMaxValue },
445     { UPROPS_SRC_PROPSVEC, 0, (int32_t)U_HST_COUNT-1,       getHangulSyllableType, getMaxValueFromShift },
446     // UCHAR_NFD_QUICK_CHECK: max=1=YES -- never "maybe", only "no" or "yes"
447     { UPROPS_SRC_NFC,   0, (int32_t)UNORM_YES,              getNormQuickCheck, getMaxValueFromShift },
448     // UCHAR_NFKD_QUICK_CHECK: max=1=YES -- never "maybe", only "no" or "yes"
449     { UPROPS_SRC_NFKC,  0, (int32_t)UNORM_YES,              getNormQuickCheck, getMaxValueFromShift },
450     // UCHAR_NFC_QUICK_CHECK: max=2=MAYBE
451     { UPROPS_SRC_NFC,   0, (int32_t)UNORM_MAYBE,            getNormQuickCheck, getMaxValueFromShift },
452     // UCHAR_NFKC_QUICK_CHECK: max=2=MAYBE
453     { UPROPS_SRC_NFKC,  0, (int32_t)UNORM_MAYBE,            getNormQuickCheck, getMaxValueFromShift },
454     { UPROPS_SRC_NFC,   0, 0xff,                            getLeadCombiningClass, getMaxValueFromShift },
455     { UPROPS_SRC_NFC,   0, 0xff,                            getTrailCombiningClass, getMaxValueFromShift },
456     { 2,                UPROPS_GCB_MASK, UPROPS_GCB_SHIFT,  defaultGetValue, defaultGetMaxValue },
457     { 2,                UPROPS_SB_MASK, UPROPS_SB_SHIFT,    defaultGetValue, defaultGetMaxValue },
458     { 2,                UPROPS_WB_MASK, UPROPS_WB_SHIFT,    defaultGetValue, defaultGetMaxValue },
459     { UPROPS_SRC_BIDI,  0, 0,                               getBiDiPairedBracketType, biDiGetMaxValue },
460 };
461 
462 U_CAPI int32_t U_EXPORT2
u_getIntPropertyValue(UChar32 c,UProperty which)463 u_getIntPropertyValue(UChar32 c, UProperty which) {
464     if(which<UCHAR_INT_START) {
465         if(UCHAR_BINARY_START<=which && which<UCHAR_BINARY_LIMIT) {
466             const BinaryProperty &prop=binProps[which];
467             return prop.contains(prop, c, which);
468         }
469     } else if(which<UCHAR_INT_LIMIT) {
470         const IntProperty &prop=intProps[which-UCHAR_INT_START];
471         return prop.getValue(prop, c, which);
472     } else if(which==UCHAR_GENERAL_CATEGORY_MASK) {
473         return U_MASK(u_charType(c));
474     }
475     return 0;  // undefined
476 }
477 
478 U_CAPI int32_t U_EXPORT2
u_getIntPropertyMinValue(UProperty)479 u_getIntPropertyMinValue(UProperty /*which*/) {
480     return 0; /* all binary/enum/int properties have a minimum value of 0 */
481 }
482 
483 U_CAPI int32_t U_EXPORT2
u_getIntPropertyMaxValue(UProperty which)484 u_getIntPropertyMaxValue(UProperty which) {
485     if(which<UCHAR_INT_START) {
486         if(UCHAR_BINARY_START<=which && which<UCHAR_BINARY_LIMIT) {
487             return 1;  // maximum TRUE for all binary properties
488         }
489     } else if(which<UCHAR_INT_LIMIT) {
490         const IntProperty &prop=intProps[which-UCHAR_INT_START];
491         return prop.getMaxValue(prop, which);
492     }
493     return -1;  // undefined
494 }
495 
496 U_CFUNC UPropertySource U_EXPORT2
uprops_getSource(UProperty which)497 uprops_getSource(UProperty which) {
498     if(which<UCHAR_BINARY_START) {
499         return UPROPS_SRC_NONE; /* undefined */
500     } else if(which<UCHAR_BINARY_LIMIT) {
501         const BinaryProperty &prop=binProps[which];
502         if(prop.mask!=0) {
503             return UPROPS_SRC_PROPSVEC;
504         } else {
505             return (UPropertySource)prop.column;
506         }
507     } else if(which<UCHAR_INT_START) {
508         return UPROPS_SRC_NONE; /* undefined */
509     } else if(which<UCHAR_INT_LIMIT) {
510         const IntProperty &prop=intProps[which-UCHAR_INT_START];
511         if(prop.mask!=0) {
512             return UPROPS_SRC_PROPSVEC;
513         } else {
514             return (UPropertySource)prop.column;
515         }
516     } else if(which<UCHAR_STRING_START) {
517         switch(which) {
518         case UCHAR_GENERAL_CATEGORY_MASK:
519         case UCHAR_NUMERIC_VALUE:
520             return UPROPS_SRC_CHAR;
521 
522         default:
523             return UPROPS_SRC_NONE;
524         }
525     } else if(which<UCHAR_STRING_LIMIT) {
526         switch(which) {
527         case UCHAR_AGE:
528             return UPROPS_SRC_PROPSVEC;
529 
530         case UCHAR_BIDI_MIRRORING_GLYPH:
531             return UPROPS_SRC_BIDI;
532 
533         case UCHAR_CASE_FOLDING:
534         case UCHAR_LOWERCASE_MAPPING:
535         case UCHAR_SIMPLE_CASE_FOLDING:
536         case UCHAR_SIMPLE_LOWERCASE_MAPPING:
537         case UCHAR_SIMPLE_TITLECASE_MAPPING:
538         case UCHAR_SIMPLE_UPPERCASE_MAPPING:
539         case UCHAR_TITLECASE_MAPPING:
540         case UCHAR_UPPERCASE_MAPPING:
541             return UPROPS_SRC_CASE;
542 
543         case UCHAR_ISO_COMMENT:
544         case UCHAR_NAME:
545         case UCHAR_UNICODE_1_NAME:
546             return UPROPS_SRC_NAMES;
547 
548         default:
549             return UPROPS_SRC_NONE;
550         }
551     } else {
552         switch(which) {
553         case UCHAR_SCRIPT_EXTENSIONS:
554             return UPROPS_SRC_PROPSVEC;
555         default:
556             return UPROPS_SRC_NONE; /* undefined */
557         }
558     }
559 }
560 
561 #if !UCONFIG_NO_NORMALIZATION
562 
563 U_CAPI int32_t U_EXPORT2
u_getFC_NFKC_Closure(UChar32 c,UChar * dest,int32_t destCapacity,UErrorCode * pErrorCode)564 u_getFC_NFKC_Closure(UChar32 c, UChar *dest, int32_t destCapacity, UErrorCode *pErrorCode) {
565     if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
566         return 0;
567     }
568     if(destCapacity<0 || (dest==NULL && destCapacity>0)) {
569         *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
570         return 0;
571     }
572     // Compute the FC_NFKC_Closure on the fly:
573     // We have the API for complete coverage of Unicode properties, although
574     // this value by itself is not useful via API.
575     // (What could be useful is a custom normalization table that combines
576     // case folding and NFKC.)
577     // For the derivation, see Unicode's DerivedNormalizationProps.txt.
578     const Normalizer2 *nfkc=Normalizer2::getNFKCInstance(*pErrorCode);
579     const UCaseProps *csp=ucase_getSingleton();
580     if(U_FAILURE(*pErrorCode)) {
581         return 0;
582     }
583     // first: b = NFKC(Fold(a))
584     UnicodeString folded1String;
585     const UChar *folded1;
586     int32_t folded1Length=ucase_toFullFolding(csp, c, &folded1, U_FOLD_CASE_DEFAULT);
587     if(folded1Length<0) {
588         const Normalizer2Impl *nfkcImpl=Normalizer2Factory::getImpl(nfkc);
589         if(nfkcImpl->getCompQuickCheck(nfkcImpl->getNorm16(c))!=UNORM_NO) {
590             return u_terminateUChars(dest, destCapacity, 0, pErrorCode);  // c does not change at all under CaseFolding+NFKC
591         }
592         folded1String.setTo(c);
593     } else {
594         if(folded1Length>UCASE_MAX_STRING_LENGTH) {
595             folded1String.setTo(folded1Length);
596         } else {
597             folded1String.setTo(FALSE, folded1, folded1Length);
598         }
599     }
600     UnicodeString kc1=nfkc->normalize(folded1String, *pErrorCode);
601     // second: c = NFKC(Fold(b))
602     UnicodeString folded2String(kc1);
603     UnicodeString kc2=nfkc->normalize(folded2String.foldCase(), *pErrorCode);
604     // if (c != b) add the mapping from a to c
605     if(U_FAILURE(*pErrorCode) || kc1==kc2) {
606         return u_terminateUChars(dest, destCapacity, 0, pErrorCode);
607     } else {
608         return kc2.extract(dest, destCapacity, *pErrorCode);
609     }
610 }
611 
612 #endif
613