2 *******************************************************************************
4 * Copyright (C) 2002-2006, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 * tab size: 8 (not used)
13 * created on: 2002feb24
14 * created by: Markus W. Scherer
16 * Implementations for mostly non-core Unicode character properties
17 * stored in uprops.icu.
19 * With the APIs implemented here, almost all properties files and
20 * their associated implementation files are used from this file,
21 * including those for normalization and case mappings.
24 #include "unicode/utypes.h"
25 #include "unicode/uchar.h"
26 #include "unicode/uscript.h"
31 #include "ubidi_props.h"
35 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
37 /* cleanup ------------------------------------------------------------------ */
39 static const UBiDiProps
*gBdp
=NULL
;
41 static UBool U_CALLCONV
uprops_cleanup(void) {
46 /* bidi/shaping properties API ---------------------------------------------- */
48 /* get the UBiDiProps singleton, or else its dummy, once and for all */
49 static const UBiDiProps
*
52 * This lazy intialization with double-checked locking (without mutex protection for
53 * the initial check) is transiently unsafe under certain circumstances.
54 * Check the readme and use u_init() if necessary.
57 /* the initial check is performed by the GET_BIDI_PROPS() macro */
58 const UBiDiProps
*bdp
;
59 UErrorCode errorCode
=U_ZERO_ERROR
;
61 bdp
=ubidi_getSingleton(&errorCode
);
62 if(U_FAILURE(errorCode
)) {
63 errorCode
=U_ZERO_ERROR
;
64 bdp
=ubidi_getDummy(&errorCode
);
65 if(U_FAILURE(errorCode
)) {
74 ucln_common_registerCleanup(UCLN_COMMON_UPROPS
, uprops_cleanup
);
81 /* see comment for GET_CASE_PROPS() */
82 #define GET_BIDI_PROPS() (gBdp!=NULL ? gBdp : getBiDiProps())
84 /* general properties API functions ----------------------------------------- */
89 } binProps
[UCHAR_BINARY_LIMIT
]={
91 * column and mask values for binary properties from u_getUnicodeProperties().
92 * Must be in order of corresponding UProperty,
93 * and there must be exacly one entry per binary UProperty.
95 * Properties with mask 0 are handled in code.
96 * For them, column is the UPropertySource value.
98 { 1, U_MASK(UPROPS_ALPHABETIC
) },
99 { 1, U_MASK(UPROPS_ASCII_HEX_DIGIT
) },
100 { UPROPS_SRC_BIDI
, 0 }, /* UCHAR_BIDI_CONTROL */
101 { UPROPS_SRC_BIDI
, 0 }, /* UCHAR_BIDI_MIRRORED */
102 { 1, U_MASK(UPROPS_DASH
) },
103 { 1, U_MASK(UPROPS_DEFAULT_IGNORABLE_CODE_POINT
) },
104 { 1, U_MASK(UPROPS_DEPRECATED
) },
105 { 1, U_MASK(UPROPS_DIACRITIC
) },
106 { 1, U_MASK(UPROPS_EXTENDER
) },
107 { UPROPS_SRC_NORM
, 0 }, /* UCHAR_FULL_COMPOSITION_EXCLUSION */
108 { 1, U_MASK(UPROPS_GRAPHEME_BASE
) },
109 { 1, U_MASK(UPROPS_GRAPHEME_EXTEND
) },
110 { 1, U_MASK(UPROPS_GRAPHEME_LINK
) },
111 { 1, U_MASK(UPROPS_HEX_DIGIT
) },
112 { 1, U_MASK(UPROPS_HYPHEN
) },
113 { 1, U_MASK(UPROPS_ID_CONTINUE
) },
114 { 1, U_MASK(UPROPS_ID_START
) },
115 { 1, U_MASK(UPROPS_IDEOGRAPHIC
) },
116 { 1, U_MASK(UPROPS_IDS_BINARY_OPERATOR
) },
117 { 1, U_MASK(UPROPS_IDS_TRINARY_OPERATOR
) },
118 { UPROPS_SRC_BIDI
, 0 }, /* UCHAR_JOIN_CONTROL */
119 { 1, U_MASK(UPROPS_LOGICAL_ORDER_EXCEPTION
) },
120 { UPROPS_SRC_CASE
, 0 }, /* UCHAR_LOWERCASE */
121 { 1, U_MASK(UPROPS_MATH
) },
122 { 1, U_MASK(UPROPS_NONCHARACTER_CODE_POINT
) },
123 { 1, U_MASK(UPROPS_QUOTATION_MARK
) },
124 { 1, U_MASK(UPROPS_RADICAL
) },
125 { UPROPS_SRC_CASE
, 0 }, /* UCHAR_SOFT_DOTTED */
126 { 1, U_MASK(UPROPS_TERMINAL_PUNCTUATION
) },
127 { 1, U_MASK(UPROPS_UNIFIED_IDEOGRAPH
) },
128 { UPROPS_SRC_CASE
, 0 }, /* UCHAR_UPPERCASE */
129 { 1, U_MASK(UPROPS_WHITE_SPACE
) },
130 { 1, U_MASK(UPROPS_XID_CONTINUE
) },
131 { 1, U_MASK(UPROPS_XID_START
) },
132 { UPROPS_SRC_CASE
, 0 }, /* UCHAR_CASE_SENSITIVE */
133 { 2, U_MASK(UPROPS_V2_S_TERM
) },
134 { 2, U_MASK(UPROPS_V2_VARIATION_SELECTOR
) },
135 { UPROPS_SRC_NORM
, 0 }, /* UCHAR_NFD_INERT */
136 { UPROPS_SRC_NORM
, 0 }, /* UCHAR_NFKD_INERT */
137 { UPROPS_SRC_NORM
, 0 }, /* UCHAR_NFC_INERT */
138 { UPROPS_SRC_NORM
, 0 }, /* UCHAR_NFKC_INERT */
139 { UPROPS_SRC_NORM
, 0 }, /* UCHAR_SEGMENT_STARTER */
140 { 2, U_MASK(UPROPS_V2_PATTERN_SYNTAX
) },
141 { 2, U_MASK(UPROPS_V2_PATTERN_WHITE_SPACE
) },
142 { UPROPS_SRC_CHAR_AND_PROPSVEC
, 0 }, /* UCHAR_POSIX_ALNUM */
143 { UPROPS_SRC_CHAR
, 0 }, /* UCHAR_POSIX_BLANK */
144 { UPROPS_SRC_CHAR
, 0 }, /* UCHAR_POSIX_GRAPH */
145 { UPROPS_SRC_CHAR
, 0 }, /* UCHAR_POSIX_PRINT */
146 { UPROPS_SRC_CHAR
, 0 } /* UCHAR_POSIX_XDIGIT */
149 U_CAPI UBool U_EXPORT2
150 u_hasBinaryProperty(UChar32 c
, UProperty which
) {
151 /* c is range-checked in the functions that are called from here */
152 if(which
<UCHAR_BINARY_START
|| UCHAR_BINARY_LIMIT
<=which
) {
153 /* not a known binary property */
155 uint32_t mask
=binProps
[which
].mask
;
156 int32_t column
=binProps
[which
].column
;
158 /* systematic, directly stored properties */
159 return (u_getUnicodeProperties(c
, column
)&mask
)!=0;
161 if(column
==UPROPS_SRC_CASE
) {
162 return ucase_hasBinaryProperty(c
, which
);
163 } else if(column
==UPROPS_SRC_NORM
) {
164 #if !UCONFIG_NO_NORMALIZATION
165 /* normalization properties from unorm.icu */
167 case UCHAR_FULL_COMPOSITION_EXCLUSION
:
168 return unorm_internalIsFullCompositionExclusion(c
);
169 case UCHAR_NFD_INERT
:
170 case UCHAR_NFKD_INERT
:
171 case UCHAR_NFC_INERT
:
172 case UCHAR_NFKC_INERT
:
173 return unorm_isNFSkippable(c
, (UNormalizationMode
)(which
-UCHAR_NFD_INERT
+UNORM_NFD
));
174 case UCHAR_SEGMENT_STARTER
:
175 return unorm_isCanonSafeStart(c
);
180 } else if(column
==UPROPS_SRC_BIDI
) {
181 /* bidi/shaping properties */
182 const UBiDiProps
*bdp
=GET_BIDI_PROPS();
185 case UCHAR_BIDI_MIRRORED
:
186 return ubidi_isMirrored(bdp
, c
);
187 case UCHAR_BIDI_CONTROL
:
188 return ubidi_isBidiControl(bdp
, c
);
189 case UCHAR_JOIN_CONTROL
:
190 return ubidi_isJoinControl(bdp
, c
);
195 /* else return FALSE below */
196 } else if(column
==UPROPS_SRC_CHAR
) {
198 case UCHAR_POSIX_BLANK
:
200 case UCHAR_POSIX_GRAPH
:
201 return u_isgraphPOSIX(c
);
202 case UCHAR_POSIX_PRINT
:
203 return u_isprintPOSIX(c
);
204 case UCHAR_POSIX_XDIGIT
:
205 return u_isxdigit(c
);
209 } else if(column
==UPROPS_SRC_CHAR_AND_PROPSVEC
) {
211 case UCHAR_POSIX_ALNUM
:
212 return u_isalnumPOSIX(c
);
222 U_CAPI
int32_t U_EXPORT2
223 u_getIntPropertyValue(UChar32 c
, UProperty which
) {
224 UErrorCode errorCode
;
227 if(which
<UCHAR_BINARY_START
) {
228 return 0; /* undefined */
229 } else if(which
<UCHAR_BINARY_LIMIT
) {
230 return (int32_t)u_hasBinaryProperty(c
, which
);
231 } else if(which
<UCHAR_INT_START
) {
232 return 0; /* undefined */
233 } else if(which
<UCHAR_INT_LIMIT
) {
235 case UCHAR_BIDI_CLASS
:
236 return (int32_t)u_charDirection(c
);
238 return (int32_t)ublock_getCode(c
);
239 case UCHAR_CANONICAL_COMBINING_CLASS
:
240 #if !UCONFIG_NO_NORMALIZATION
241 return u_getCombiningClass(c
);
245 case UCHAR_DECOMPOSITION_TYPE
:
246 return (int32_t)(u_getUnicodeProperties(c
, 2)&UPROPS_DT_MASK
);
247 case UCHAR_EAST_ASIAN_WIDTH
:
248 return (int32_t)(u_getUnicodeProperties(c
, 0)&UPROPS_EA_MASK
)>>UPROPS_EA_SHIFT
;
249 case UCHAR_GENERAL_CATEGORY
:
250 return (int32_t)u_charType(c
);
251 case UCHAR_JOINING_GROUP
:
252 return ubidi_getJoiningGroup(GET_BIDI_PROPS(), c
);
253 case UCHAR_JOINING_TYPE
:
254 return ubidi_getJoiningType(GET_BIDI_PROPS(), c
);
255 case UCHAR_LINE_BREAK
:
256 return (int32_t)(u_getUnicodeProperties(c
, 0)&UPROPS_LB_MASK
)>>UPROPS_LB_SHIFT
;
257 case UCHAR_NUMERIC_TYPE
:
258 type
=(int32_t)GET_NUMERIC_TYPE(u_getUnicodeProperties(c
, -1));
259 if(type
>U_NT_NUMERIC
) {
260 /* keep internal variants of U_NT_NUMERIC from becoming visible */
265 errorCode
=U_ZERO_ERROR
;
266 return (int32_t)uscript_getScript(c
, &errorCode
);
267 case UCHAR_HANGUL_SYLLABLE_TYPE
:
268 return uchar_getHST(c
);
269 #if !UCONFIG_NO_NORMALIZATION
270 case UCHAR_NFD_QUICK_CHECK
:
271 case UCHAR_NFKD_QUICK_CHECK
:
272 case UCHAR_NFC_QUICK_CHECK
:
273 case UCHAR_NFKC_QUICK_CHECK
:
274 return (int32_t)unorm_getQuickCheck(c
, (UNormalizationMode
)(which
-UCHAR_NFD_QUICK_CHECK
+UNORM_NFD
));
275 case UCHAR_LEAD_CANONICAL_COMBINING_CLASS
:
276 return unorm_getFCD16FromCodePoint(c
)>>8;
277 case UCHAR_TRAIL_CANONICAL_COMBINING_CLASS
:
278 return unorm_getFCD16FromCodePoint(c
)&0xff;
280 case UCHAR_GRAPHEME_CLUSTER_BREAK
:
281 return (int32_t)(u_getUnicodeProperties(c
, 2)&UPROPS_GCB_MASK
)>>UPROPS_GCB_SHIFT
;
282 case UCHAR_SENTENCE_BREAK
:
283 return (int32_t)(u_getUnicodeProperties(c
, 2)&UPROPS_SB_MASK
)>>UPROPS_SB_SHIFT
;
284 case UCHAR_WORD_BREAK
:
285 return (int32_t)(u_getUnicodeProperties(c
, 2)&UPROPS_WB_MASK
)>>UPROPS_WB_SHIFT
;
287 return 0; /* undefined */
289 } else if(which
==UCHAR_GENERAL_CATEGORY_MASK
) {
290 return U_MASK(u_charType(c
));
292 return 0; /* undefined */
296 U_CAPI
int32_t U_EXPORT2
297 u_getIntPropertyMinValue(UProperty which
) {
298 return 0; /* all binary/enum/int properties have a minimum value of 0 */
301 U_CAPI
int32_t U_EXPORT2
302 u_getIntPropertyMaxValue(UProperty which
) {
303 if(which
<UCHAR_BINARY_START
) {
304 return -1; /* undefined */
305 } else if(which
<UCHAR_BINARY_LIMIT
) {
306 return 1; /* maximum TRUE for all binary properties */
307 } else if(which
<UCHAR_INT_START
) {
308 return -1; /* undefined */
309 } else if(which
<UCHAR_INT_LIMIT
) {
311 case UCHAR_BIDI_CLASS
:
312 case UCHAR_JOINING_GROUP
:
313 case UCHAR_JOINING_TYPE
:
314 return ubidi_getMaxValue(GET_BIDI_PROPS(), which
);
316 return (uprv_getMaxValues(0)&UPROPS_BLOCK_MASK
)>>UPROPS_BLOCK_SHIFT
;
317 case UCHAR_CANONICAL_COMBINING_CLASS
:
318 case UCHAR_LEAD_CANONICAL_COMBINING_CLASS
:
319 case UCHAR_TRAIL_CANONICAL_COMBINING_CLASS
:
320 return 0xff; /* TODO do we need to be more precise, getting the actual maximum? */
321 case UCHAR_DECOMPOSITION_TYPE
:
322 return uprv_getMaxValues(2)&UPROPS_DT_MASK
;
323 case UCHAR_EAST_ASIAN_WIDTH
:
324 return (uprv_getMaxValues(0)&UPROPS_EA_MASK
)>>UPROPS_EA_SHIFT
;
325 case UCHAR_GENERAL_CATEGORY
:
326 return (int32_t)U_CHAR_CATEGORY_COUNT
-1;
327 case UCHAR_LINE_BREAK
:
328 return (uprv_getMaxValues(0)&UPROPS_LB_MASK
)>>UPROPS_LB_SHIFT
;
329 case UCHAR_NUMERIC_TYPE
:
330 return (int32_t)U_NT_COUNT
-1;
332 return uprv_getMaxValues(0)&UPROPS_SCRIPT_MASK
;
333 case UCHAR_HANGUL_SYLLABLE_TYPE
:
334 return (int32_t)U_HST_COUNT
-1;
335 #if !UCONFIG_NO_NORMALIZATION
336 case UCHAR_NFD_QUICK_CHECK
:
337 case UCHAR_NFKD_QUICK_CHECK
:
338 return (int32_t)UNORM_YES
; /* these are never "maybe", only "no" or "yes" */
339 case UCHAR_NFC_QUICK_CHECK
:
340 case UCHAR_NFKC_QUICK_CHECK
:
341 return (int32_t)UNORM_MAYBE
;
343 case UCHAR_GRAPHEME_CLUSTER_BREAK
:
344 return (uprv_getMaxValues(2)&UPROPS_GCB_MASK
)>>UPROPS_GCB_SHIFT
;
345 case UCHAR_SENTENCE_BREAK
:
346 return (uprv_getMaxValues(2)&UPROPS_SB_MASK
)>>UPROPS_SB_SHIFT
;
347 case UCHAR_WORD_BREAK
:
348 return (uprv_getMaxValues(2)&UPROPS_WB_MASK
)>>UPROPS_WB_SHIFT
;
350 return -1; /* undefined */
353 return -1; /* undefined */
357 U_CAPI UPropertySource U_EXPORT2
358 uprops_getSource(UProperty which
) {
359 if(which
<UCHAR_BINARY_START
) {
360 return UPROPS_SRC_NONE
; /* undefined */
361 } else if(which
<UCHAR_BINARY_LIMIT
) {
362 if(binProps
[which
].mask
!=0) {
363 return UPROPS_SRC_PROPSVEC
;
365 return (UPropertySource
)binProps
[which
].column
;
367 } else if(which
<UCHAR_INT_START
) {
368 return UPROPS_SRC_NONE
; /* undefined */
369 } else if(which
<UCHAR_INT_LIMIT
) {
371 case UCHAR_GENERAL_CATEGORY
:
372 case UCHAR_NUMERIC_TYPE
:
373 return UPROPS_SRC_CHAR
;
375 case UCHAR_HANGUL_SYLLABLE_TYPE
:
376 return UPROPS_SRC_HST
;
378 case UCHAR_CANONICAL_COMBINING_CLASS
:
379 case UCHAR_NFD_QUICK_CHECK
:
380 case UCHAR_NFKD_QUICK_CHECK
:
381 case UCHAR_NFC_QUICK_CHECK
:
382 case UCHAR_NFKC_QUICK_CHECK
:
383 case UCHAR_LEAD_CANONICAL_COMBINING_CLASS
:
384 case UCHAR_TRAIL_CANONICAL_COMBINING_CLASS
:
385 return UPROPS_SRC_NORM
;
387 case UCHAR_BIDI_CLASS
:
388 case UCHAR_JOINING_GROUP
:
389 case UCHAR_JOINING_TYPE
:
390 return UPROPS_SRC_BIDI
;
393 return UPROPS_SRC_PROPSVEC
;
395 } else if(which
<UCHAR_STRING_START
) {
397 case UCHAR_GENERAL_CATEGORY_MASK
:
398 case UCHAR_NUMERIC_VALUE
:
399 return UPROPS_SRC_CHAR
;
402 return UPROPS_SRC_NONE
;
404 } else if(which
<UCHAR_STRING_LIMIT
) {
407 return UPROPS_SRC_PROPSVEC
;
409 case UCHAR_BIDI_MIRRORING_GLYPH
:
410 return UPROPS_SRC_BIDI
;
412 case UCHAR_CASE_FOLDING
:
413 case UCHAR_LOWERCASE_MAPPING
:
414 case UCHAR_SIMPLE_CASE_FOLDING
:
415 case UCHAR_SIMPLE_LOWERCASE_MAPPING
:
416 case UCHAR_SIMPLE_TITLECASE_MAPPING
:
417 case UCHAR_SIMPLE_UPPERCASE_MAPPING
:
418 case UCHAR_TITLECASE_MAPPING
:
419 case UCHAR_UPPERCASE_MAPPING
:
420 return UPROPS_SRC_CASE
;
422 case UCHAR_ISO_COMMENT
:
424 case UCHAR_UNICODE_1_NAME
:
425 return UPROPS_SRC_NAMES
;
428 return UPROPS_SRC_NONE
;
431 return UPROPS_SRC_NONE
; /* undefined */
435 /*----------------------------------------------------------------
437 *----------------------------------------------------------------*/
440 * Return a set of characters for property enumeration.
441 * The set implicitly contains 0x110000 as well, which is one more than the highest
442 * Unicode code point.
444 * This set is used as an ordered list - its code points are ordered, and
445 * consecutive code points (in Unicode code point order) in the set define a range.
446 * For each two consecutive characters (start, limit) in the set,
447 * all of the UCD/normalization and related properties for
448 * all code points start..limit-1 are all the same,
449 * except for character names and ISO comments.
451 * All Unicode code points U+0000..U+10ffff are covered by these ranges.
452 * The ranges define a partition of the Unicode code space.
453 * ICU uses the inclusions set to enumerate properties for generating
454 * UnicodeSets containing all code points that have a certain property value.
456 * The Inclusion List is generated from the UCD. It is generated
457 * by enumerating the data tries, and code points for hardcoded properties
460 * --------------------------------------------------------------------------
462 * The following are ideas for getting properties-unique code point ranges,
463 * with possible optimizations beyond the current implementation.
464 * These optimizations would require more code and be more fragile.
465 * The current implementation generates one single list (set) for all properties.
467 * To enumerate properties efficiently, one needs to know ranges of
468 * repetitive values, so that the value of only each start code point
469 * can be applied to the whole range.
470 * This information is in principle available in the uprops.icu/unorm.icu data.
472 * There are two obstacles:
474 * 1. Some properties are computed from multiple data structures,
475 * making it necessary to get repetitive ranges by intersecting
476 * ranges from multiple tries.
478 * 2. It is not economical to write code for getting repetitive ranges
479 * that are precise for each of some 50 properties.
483 * - Get ranges per trie, not per individual property.
484 * Each range contains the same values for a whole group of properties.
485 * This would generate currently five range sets, two for uprops.icu tries
486 * and three for unorm.icu tries.
488 * - Combine sets of ranges for multiple tries to get sufficient sets
489 * for properties, e.g., the uprops.icu main and auxiliary tries
490 * for all non-normalization properties.
492 * Ideas for representing ranges and combining them:
494 * - A UnicodeSet could hold just the start code points of ranges.
495 * Multiple sets are easily combined by or-ing them together.
497 * - Alternatively, a UnicodeSet could hold each even-numbered range.
498 * All ranges could be enumerated by using each start code point
499 * (for the even-numbered ranges) as well as each limit (end+1) code point
500 * (for the odd-numbered ranges).
501 * It should be possible to combine two such sets by xor-ing them,
502 * but no more than two.
504 * The second way to represent ranges may(?!) yield smaller UnicodeSet arrays,
505 * but the first one is certainly simpler and applicable for combining more than
508 * It is possible to combine all range sets for all uprops/unorm tries into one
509 * set that can be used for all properties.
510 * As an optimization, there could be less-combined range sets for certain
511 * groups of properties.
512 * The relationship of which less-combined range set to use for which property
513 * depends on the implementation of the properties and must be hardcoded
514 * - somewhat error-prone and higher maintenance but can be tested easily
515 * by building property sets "the simple way" in test code.
519 * Do not use a UnicodeSet pattern because that causes infinite recursion;
520 * UnicodeSet depends on the inclusions set.
524 * uprv_getInclusions() is commented out starting 2004-sep-13 because
525 * uniset_props.cpp now calls the uxyz_addPropertyStarts() directly,
526 * and only for the relevant property source.
530 U_CAPI
void U_EXPORT2
531 uprv_getInclusions(const USetAdder
*sa
, UErrorCode
*pErrorCode
) {
532 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
536 #if !UCONFIG_NO_NORMALIZATION
537 unorm_addPropertyStarts(sa
, pErrorCode
);
539 uchar_addPropertyStarts(sa
, pErrorCode
);
540 uhst_addPropertyStarts(sa
, pErrorCode
);
541 ucase_addPropertyStarts(ucase_getSingleton(pErrorCode
), sa
, pErrorCode
);
542 ubidi_addPropertyStarts(ubidi_getSingleton(pErrorCode
), sa
, pErrorCode
);