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