xref: /aosp_15_r20/external/icu/libicu/cts_headers/uprops.h (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
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.h
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 *   Constants for mostly non-core Unicode character properties
19 *   stored in uprops.icu.
20 */
21 
22 #ifndef __UPROPS_H__
23 #define __UPROPS_H__
24 
25 #include "unicode/utypes.h"
26 #include "unicode/uset.h"
27 #include "uset_imp.h"
28 #include "udataswp.h"
29 
30 /* indexes[] entries */
31 enum {
32     UPROPS_PROPS32_INDEX,
33     UPROPS_EXCEPTIONS_INDEX,
34     UPROPS_EXCEPTIONS_TOP_INDEX,
35 
36     UPROPS_ADDITIONAL_TRIE_INDEX,
37     UPROPS_ADDITIONAL_VECTORS_INDEX,
38     UPROPS_ADDITIONAL_VECTORS_COLUMNS_INDEX,
39 
40     UPROPS_SCRIPT_EXTENSIONS_INDEX,
41 
42     UPROPS_RESERVED_INDEX_7,
43     UPROPS_RESERVED_INDEX_8,
44 
45     /* size of the data file (number of 32-bit units after the header) */
46     UPROPS_DATA_TOP_INDEX,
47 
48     /* maximum values for code values in vector word 0 */
49     UPROPS_MAX_VALUES_INDEX=10,
50     /* maximum values for code values in vector word 2 */
51     UPROPS_MAX_VALUES_2_INDEX,
52 
53     UPROPS_INDEX_COUNT=16
54 };
55 
56 /* definitions for the main properties words */
57 enum {
58     /* general category shift==0                                0 (5 bits) */
59     /* reserved                                                 5 (1 bit) */
60     UPROPS_NUMERIC_TYPE_VALUE_SHIFT=6                       /*  6 (10 bits) */
61 };
62 
63 #define GET_CATEGORY(props) ((props)&0x1f)
64 #define CAT_MASK(props) U_MASK(GET_CATEGORY(props))
65 
66 #define GET_NUMERIC_TYPE_VALUE(props) ((props)>>UPROPS_NUMERIC_TYPE_VALUE_SHIFT)
67 
68 /* constants for the storage form of numeric types and values */
69 enum {
70     /** No numeric value. */
71     UPROPS_NTV_NONE=0,
72     /** Decimal digits: nv=0..9 */
73     UPROPS_NTV_DECIMAL_START=1,
74     /** Other digits: nv=0..9 */
75     UPROPS_NTV_DIGIT_START=11,
76     /** Small integers: nv=0..154 */
77     UPROPS_NTV_NUMERIC_START=21,
78     /** Fractions: ((ntv>>4)-12) / ((ntv&0xf)+1) = -1..17 / 1..16 */
79     UPROPS_NTV_FRACTION_START=0xb0,
80     /**
81      * Large integers:
82      * ((ntv>>5)-14) * 10^((ntv&0x1f)+2) = (1..9)*(10^2..10^33)
83      * (only one significant decimal digit)
84      */
85     UPROPS_NTV_LARGE_START=0x1e0,
86     /**
87      * Sexagesimal numbers:
88      * ((ntv>>2)-0xbf) * 60^((ntv&3)+1) = (1..9)*(60^1..60^4)
89      */
90     UPROPS_NTV_BASE60_START=0x300,
91     /**
92      * Fraction-20 values:
93      * frac20 = ntv-0x324 = 0..0x17 -> 1|3|5|7 / 20|40|80|160|320|640
94      * numerator: num = 2*(frac20&3)+1
95      * denominator: den = 20<<(frac20>>2)
96      */
97     UPROPS_NTV_FRACTION20_START=UPROPS_NTV_BASE60_START+36,  // 0x300+9*4=0x324
98     /**
99      * Fraction-32 values:
100      * frac32 = ntv-0x34c = 0..15 -> 1|3|5|7 / 32|64|128|256
101      * numerator: num = 2*(frac32&3)+1
102      * denominator: den = 32<<(frac32>>2)
103      */
104     UPROPS_NTV_FRACTION32_START=UPROPS_NTV_FRACTION20_START+24,  // 0x324+6*4=0x34c
105     /** No numeric value (yet). */
106     UPROPS_NTV_RESERVED_START=UPROPS_NTV_FRACTION32_START+16,  // 0x34c+4*4=0x35c
107 
108     UPROPS_NTV_MAX_SMALL_INT=UPROPS_NTV_FRACTION_START-UPROPS_NTV_NUMERIC_START-1
109 };
110 
111 #define UPROPS_NTV_GET_TYPE(ntv) \
112     ((ntv==UPROPS_NTV_NONE) ? U_NT_NONE : \
113     (ntv<UPROPS_NTV_DIGIT_START) ?  U_NT_DECIMAL : \
114     (ntv<UPROPS_NTV_NUMERIC_START) ? U_NT_DIGIT : \
115     U_NT_NUMERIC)
116 
117 /* number of properties vector words */
118 #define UPROPS_VECTOR_WORDS     3
119 
120 /*
121  * Properties in vector word 0
122  * Bits
123  * 31..24   DerivedAge version major/minor one nibble each
124  * 23..22   3..1: Bits 21..20 & 7..0 = Script_Extensions index
125  *             3: Script value from Script_Extensions
126  *             2: Script=Inherited
127  *             1: Script=Common
128  *             0: Script=bits 21..20 & 7..0
129  * 21..20   Bits 9..8 of the UScriptCode, or index to Script_Extensions
130  * 19..17   East Asian Width
131  * 16.. 8   UBlockCode
132  *  7.. 0   UScriptCode, or index to Script_Extensions
133  */
134 
135 /* derived age: one nibble each for major and minor version numbers */
136 #define UPROPS_AGE_MASK         0xff000000
137 #define UPROPS_AGE_SHIFT        24
138 
139 /* Script_Extensions: mask includes Script */
140 #define UPROPS_SCRIPT_X_MASK    0x00f000ff
141 #define UPROPS_SCRIPT_X_SHIFT   22
142 
143 // The UScriptCode or Script_Extensions index is split across two bit fields.
144 // (Starting with Unicode 13/ICU 66/2019 due to more varied Script_Extensions.)
145 // Shift the high bits right by 12 to assemble the full value.
146 #define UPROPS_SCRIPT_HIGH_MASK    0x00300000
147 #define UPROPS_SCRIPT_HIGH_SHIFT   12
148 #define UPROPS_MAX_SCRIPT          0x3ff
149 
150 #define UPROPS_EA_MASK          0x000e0000
151 #define UPROPS_EA_SHIFT         17
152 
153 #define UPROPS_BLOCK_MASK       0x0001ff00
154 #define UPROPS_BLOCK_SHIFT      8
155 
156 #define UPROPS_SCRIPT_LOW_MASK  0x000000ff
157 
158 /* UPROPS_SCRIPT_X_WITH_COMMON must be the lowest value that involves Script_Extensions. */
159 #define UPROPS_SCRIPT_X_WITH_COMMON     0x400000
160 #define UPROPS_SCRIPT_X_WITH_INHERITED  0x800000
161 #define UPROPS_SCRIPT_X_WITH_OTHER      0xc00000
162 
163 #ifdef __cplusplus
164 
165 namespace {
166 
uprops_mergeScriptCodeOrIndex(uint32_t scriptX)167 inline uint32_t uprops_mergeScriptCodeOrIndex(uint32_t scriptX) {
168     return
169         ((scriptX & UPROPS_SCRIPT_HIGH_MASK) >> UPROPS_SCRIPT_HIGH_SHIFT) |
170         (scriptX & UPROPS_SCRIPT_LOW_MASK);
171 }
172 
173 }  // namespace
174 
175 #endif  // __cplusplus
176 
177 /*
178  * Properties in vector word 1
179  * Each bit encodes one binary property.
180  * The following constants represent the bit number, use 1<<UPROPS_XYZ.
181  * UPROPS_BINARY_1_TOP<=32!
182  *
183  * Keep this list of property enums in sync with
184  * propListNames[] in icu/source/tools/genprops/props2.c!
185  *
186  * ICU 2.6/uprops format version 3.2 stores full properties instead of "Other_".
187  */
188 enum {
189     UPROPS_WHITE_SPACE,
190     UPROPS_DASH,
191     UPROPS_HYPHEN,
192     UPROPS_QUOTATION_MARK,
193     UPROPS_TERMINAL_PUNCTUATION,
194     UPROPS_MATH,
195     UPROPS_HEX_DIGIT,
196     UPROPS_ASCII_HEX_DIGIT,
197     UPROPS_ALPHABETIC,
198     UPROPS_IDEOGRAPHIC,
199     UPROPS_DIACRITIC,
200     UPROPS_EXTENDER,
201     UPROPS_NONCHARACTER_CODE_POINT,
202     UPROPS_GRAPHEME_EXTEND,
203     UPROPS_GRAPHEME_LINK,
204     UPROPS_IDS_BINARY_OPERATOR,
205     UPROPS_IDS_TRINARY_OPERATOR,
206     UPROPS_RADICAL,
207     UPROPS_UNIFIED_IDEOGRAPH,
208     UPROPS_DEFAULT_IGNORABLE_CODE_POINT,
209     UPROPS_DEPRECATED,
210     UPROPS_LOGICAL_ORDER_EXCEPTION,
211     UPROPS_XID_START,
212     UPROPS_XID_CONTINUE,
213     UPROPS_ID_START,                            /* ICU 2.6, uprops format version 3.2 */
214     UPROPS_ID_CONTINUE,
215     UPROPS_GRAPHEME_BASE,
216     UPROPS_S_TERM,                              /* new in ICU 3.0 and Unicode 4.0.1 */
217     UPROPS_VARIATION_SELECTOR,
218     UPROPS_PATTERN_SYNTAX,                      /* new in ICU 3.4 and Unicode 4.1 */
219     UPROPS_PATTERN_WHITE_SPACE,
220     UPROPS_PREPENDED_CONCATENATION_MARK,        // new in ICU 60 and Unicode 10
221     UPROPS_BINARY_1_TOP                         /* ==32 - full! */
222 };
223 
224 /*
225  * Properties in vector word 2
226  * Bits
227  * 31..26   ICU 75: Identifier_Type bit set
228  *          ICU 70..74: unused
229  *          ICU 57..69: emoji properties; moved to uemoji.icu in ICU 70
230  * 25..20   Line Break
231  * 19..15   Sentence Break
232  * 14..10   Word Break
233  *  9.. 5   Grapheme Cluster Break
234  *  4.. 0   Decomposition Type
235  */
236 
237 #ifdef __cplusplus
238 
239 // https://www.unicode.org/reports/tr39/#Identifier_Status_and_Type
240 // The Identifier_Type maps each code point to a *set* of one or more values.
241 // Some can be combined with others, some can only occur alone.
242 // Exclusion & Limited_Use are combinable bits, but cannot occur together.
243 // We use this forbidden combination for enumerated values.
244 // We use 6 bits for all possible combinations.
245 // If more combinable values are added, then we need to use more bits.
246 //
247 // We do not store separate data for Identifier_Status:
248 // We can derive that from the encoded Identifier_Type via a simple range check.
249 
250 inline constexpr uint32_t UPROPS_2_ID_TYPE_MASK = 0xfc000000;
251 inline constexpr int32_t UPROPS_2_ID_TYPE_SHIFT = 26;
252 
253 enum {
254     // A high bit for use in idTypeToEncoded[] but not used in the data
255     UPROPS_ID_TYPE_BIT = 0x80,
256 
257     // Combinable bits
258     UPROPS_ID_TYPE_EXCLUSION = 0x20,
259     UPROPS_ID_TYPE_LIMITED_USE = 0x10,
260     UPROPS_ID_TYPE_UNCOMMON_USE = 8,
261     UPROPS_ID_TYPE_TECHNICAL = 4,
262     UPROPS_ID_TYPE_OBSOLETE = 2,
263     UPROPS_ID_TYPE_NOT_XID = 1,
264 
265     // Exclusive values
266     UPROPS_ID_TYPE_NOT_CHARACTER = 0,
267 
268     // Forbidden bit combination used for enumerating other exclusive values
269     UPROPS_ID_TYPE_FORBIDDEN = UPROPS_ID_TYPE_EXCLUSION | UPROPS_ID_TYPE_LIMITED_USE, // 0x30
270     UPROPS_ID_TYPE_DEPRECATED = UPROPS_ID_TYPE_FORBIDDEN, // 0x30
271     UPROPS_ID_TYPE_DEFAULT_IGNORABLE, // 0x31
272     UPROPS_ID_TYPE_NOT_NFKC, // 0x32
273 
274     UPROPS_ID_TYPE_ALLOWED_MIN = UPROPS_ID_TYPE_FORBIDDEN + 0xc, // 0x3c
275     UPROPS_ID_TYPE_INCLUSION = UPROPS_ID_TYPE_FORBIDDEN + 0xe, // 0x3e
276     UPROPS_ID_TYPE_RECOMMENDED = UPROPS_ID_TYPE_FORBIDDEN + 0xf, // 0x3f
277 };
278 
279 /**
280  * Maps UIdentifierType to encoded bits.
281  * When UPROPS_ID_TYPE_BIT is set, then use "&" to test whether the value bit is set.
282  * When UPROPS_ID_TYPE_BIT is not set, then compare ("==") the array value with the data value.
283  */
284 inline constexpr uint8_t uprops_idTypeToEncoded[] = {
285     UPROPS_ID_TYPE_NOT_CHARACTER,
286     UPROPS_ID_TYPE_DEPRECATED,
287     UPROPS_ID_TYPE_DEFAULT_IGNORABLE,
288     UPROPS_ID_TYPE_NOT_NFKC,
289     UPROPS_ID_TYPE_BIT | UPROPS_ID_TYPE_NOT_XID,
290     UPROPS_ID_TYPE_BIT | UPROPS_ID_TYPE_EXCLUSION,
291     UPROPS_ID_TYPE_BIT | UPROPS_ID_TYPE_OBSOLETE,
292     UPROPS_ID_TYPE_BIT | UPROPS_ID_TYPE_TECHNICAL,
293     UPROPS_ID_TYPE_BIT | UPROPS_ID_TYPE_UNCOMMON_USE,
294     UPROPS_ID_TYPE_BIT | UPROPS_ID_TYPE_LIMITED_USE,
295     UPROPS_ID_TYPE_INCLUSION,
296     UPROPS_ID_TYPE_RECOMMENDED
297 };
298 
299 #endif  // __cplusplus
300 
301 #define UPROPS_LB_MASK          0x03f00000
302 #define UPROPS_LB_SHIFT         20
303 
304 #define UPROPS_SB_MASK          0x000f8000
305 #define UPROPS_SB_SHIFT         15
306 
307 #define UPROPS_WB_MASK          0x00007c00
308 #define UPROPS_WB_SHIFT         10
309 
310 #define UPROPS_GCB_MASK         0x000003e0
311 #define UPROPS_GCB_SHIFT        5
312 
313 #define UPROPS_DT_MASK          0x0000001f
314 
315 /**
316  * Gets the main properties value for a code point.
317  * Implemented in uchar.c for uprops.cpp.
318  */
319 U_CFUNC uint32_t
320 u_getMainProperties(UChar32 c);
321 
322 /**
323  * Get a properties vector word for a code point.
324  * Implemented in uchar.c for uprops.cpp.
325  * @return 0 if no data or illegal argument
326  */
327 U_CFUNC uint32_t
328 u_getUnicodeProperties(UChar32 c, int32_t column);
329 
330 /**
331  * Get the the maximum values for some enum/int properties.
332  * Use the same column numbers as for u_getUnicodeProperties().
333  * The returned value will contain maximum values stored in the same bit fields
334  * as where the enum values are stored in the u_getUnicodeProperties()
335  * return values for the same columns.
336  *
337  * Valid columns are those for properties words that contain enumerated values.
338  * (ICU 2.6: columns 0 and 2)
339  * For other column numbers, this function will return 0.
340  *
341  * @internal
342  */
343 U_CFUNC int32_t
344 uprv_getMaxValues(int32_t column);
345 
346 /**
347  * Checks if c is alphabetic, or a decimal digit; implements UCHAR_POSIX_ALNUM.
348  * @internal
349  */
350 U_CFUNC UBool
351 u_isalnumPOSIX(UChar32 c);
352 
353 /**
354  * Checks if c is in
355  * [^\p{space}\p{gc=Control}\p{gc=Surrogate}\p{gc=Unassigned}]
356  * with space=\p{Whitespace} and Control=Cc.
357  * Implements UCHAR_POSIX_GRAPH.
358  * @internal
359  */
360 U_CFUNC UBool
361 u_isgraphPOSIX(UChar32 c);
362 
363 /**
364  * Checks if c is in \p{graph}\p{blank} - \p{cntrl}.
365  * Implements UCHAR_POSIX_PRINT.
366  * @internal
367  */
368 U_CFUNC UBool
369 u_isprintPOSIX(UChar32 c);
370 
371 /** Some code points. @internal */
372 enum {
373     TAB     =0x0009,
374     LF      =0x000a,
375     FF      =0x000c,
376     CR      =0x000d,
377     NBSP    =0x00a0,
378     CGJ     =0x034f,
379     FIGURESP=0x2007,
380     HAIRSP  =0x200a,
381     ZWNJ    =0x200c,
382     ZWJ     =0x200d,
383     RLM     =0x200f,
384     NNBSP   =0x202f,
385     ZWNBSP  =0xfeff
386 };
387 
388 /**
389  * Get the maximum length of a (regular/1.0/extended) character name.
390  * @return 0 if no character names available.
391  */
392 U_CAPI int32_t U_EXPORT2
393 uprv_getMaxCharNameLength(void);
394 
395 /**
396  * Fills set with characters that are used in Unicode character names.
397  * Includes all characters that are used in regular/Unicode 1.0/extended names.
398  * Just empties the set if no character names are available.
399  * @param sa USetAdder to receive characters.
400  */
401 U_CAPI void U_EXPORT2
402 uprv_getCharNameCharacters(const USetAdder *sa);
403 
404 /**
405  * Constants for which data and implementation files provide which properties.
406  * Used by UnicodeSet for service-specific property enumeration.
407  * @internal
408  */
409 enum UPropertySource {
410     /** No source, not a supported property. */
411     UPROPS_SRC_NONE,
412     /** From uchar.c/uprops.icu main trie */
413     UPROPS_SRC_CHAR,
414     /** From uchar.c/uprops.icu properties vectors trie */
415     UPROPS_SRC_PROPSVEC,
416     /** From unames.c/unames.icu */
417     UPROPS_SRC_NAMES,
418     /** From ucase.c/ucase.icu */
419     UPROPS_SRC_CASE,
420     /** From ubidi_props.c/ubidi.icu */
421     UPROPS_SRC_BIDI,
422     /** From uchar.c/uprops.icu main trie as well as properties vectors trie */
423     UPROPS_SRC_CHAR_AND_PROPSVEC,
424     /** From ucase.c/ucase.icu as well as unorm.cpp/unorm.icu */
425     UPROPS_SRC_CASE_AND_NORM,
426     /** From normalizer2impl.cpp/nfc.nrm */
427     UPROPS_SRC_NFC,
428     /** From normalizer2impl.cpp/nfkc.nrm */
429     UPROPS_SRC_NFKC,
430     /** From normalizer2impl.cpp/nfkc_cf.nrm */
431     UPROPS_SRC_NFKC_CF,
432     /** From normalizer2impl.cpp/nfc.nrm canonical iterator data */
433     UPROPS_SRC_NFC_CANON_ITER,
434     // Text layout properties.
435     UPROPS_SRC_INPC,
436     UPROPS_SRC_INSC,
437     UPROPS_SRC_VO,
438     UPROPS_SRC_EMOJI,
439     UPROPS_SRC_IDSU,
440     UPROPS_SRC_ID_COMPAT_MATH,
441     /** One more than the highest UPropertySource (UPROPS_SRC_) constant. */
442     UPROPS_SRC_COUNT
443 };
444 typedef enum UPropertySource UPropertySource;
445 
446 /**
447  * @see UPropertySource
448  * @internal
449  */
450 U_CFUNC UPropertySource U_EXPORT2
451 uprops_getSource(UProperty which);
452 
453 /**
454  * Enumerate uprops.icu's main data trie and add the
455  * start of each range of same properties to the set.
456  * @internal
457  */
458 U_CFUNC void U_EXPORT2
459 uchar_addPropertyStarts(const USetAdder *sa, UErrorCode *pErrorCode);
460 
461 /**
462  * Enumerate uprops.icu's properties vectors trie and add the
463  * start of each range of same properties to the set.
464  * @internal
465  */
466 U_CFUNC void U_EXPORT2
467 upropsvec_addPropertyStarts(const USetAdder *sa, UErrorCode *pErrorCode);
468 
469 U_CFUNC void U_EXPORT2
470 uprops_addPropertyStarts(UPropertySource src, const USetAdder *sa, UErrorCode *pErrorCode);
471 
472 /**
473  * Return a set of characters for property enumeration.
474  * For each two consecutive characters (start, limit) in the set,
475  * all of the properties for start..limit-1 are all the same.
476  *
477  * @param sa USetAdder to receive result. Existing contents are lost.
478  * @internal
479  */
480 /*U_CFUNC void U_EXPORT2
481 uprv_getInclusions(const USetAdder *sa, UErrorCode *pErrorCode);
482 */
483 
484 /**
485  * Swap the ICU Unicode character names file. See uchar.c.
486  * @internal
487  */
488 U_CAPI int32_t U_EXPORT2
489 uchar_swapNames(const UDataSwapper *ds,
490                 const void *inData, int32_t length, void *outData,
491                 UErrorCode *pErrorCode);
492 
493 #ifdef __cplusplus
494 
495 U_NAMESPACE_BEGIN
496 
497 class UnicodeSet;
498 
499 class CharacterProperties {
500 public:
501     CharacterProperties() = delete;
502     static const UnicodeSet *getInclusionsForProperty(UProperty prop, UErrorCode &errorCode);
503     static const UnicodeSet *getBinaryPropertySet(UProperty property, UErrorCode &errorCode);
504 };
505 
506 // implemented in uniset_props.cpp
507 U_CFUNC UnicodeSet *
508 uniset_getUnicode32Instance(UErrorCode &errorCode);
509 
510 U_NAMESPACE_END
511 
512 #endif
513 
514 #endif
515