ICU 75.1 75.1

ucptrie.h
Go to the documentation of this file.
1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3
4 // ucptrie.h (modified from utrie2.h)
5 // created: 2017dec29 Markus W. Scherer
6
7 #ifndef __UCPTRIE_H__
8 #define __UCPTRIE_H__
9
10 #include "unicode/utypes.h"
11 #include "unicode/ucpmap.h"
12 #include "unicode/utf8.h"
13
14 #if U_SHOW_CPLUSPLUS_API
15 #include "unicode/localpointer.h"
16 #endif // U_SHOW_CPLUSPLUS_API
17
18 U_CDECL_BEGIN
19
28 #ifndef U_IN_DOXYGEN
30 typedef union UCPTrieData {
32 const void *ptr0;
34 const uint16_t *ptr16;
36 const uint32_t *ptr32;
38 const uint8_t *ptr8;
39 } UCPTrieData;
40 #endif
41
59 struct UCPTrie {
60 #ifndef U_IN_DOXYGEN
62 const uint16_t *index;
64 UCPTrieData data;
65
67 int32_t indexLength;
69 int32_t dataLength;
71 UChar32 highStart;
73 uint16_t shifted12HighStart;
74
76 int8_t type; // UCPTrieType
78 int8_t valueWidth; // UCPTrieValueWidth
79
81 uint32_t reserved32;
83 uint16_t reserved16;
84
90 uint16_t index3NullOffset;
96 int32_t dataNullOffset;
98 uint32_t nullValue;
99
100 #ifdef UCPTRIE_DEBUG
102 const char *name;
103 #endif
104 #endif
105 };
106 #ifndef U_IN_DOXYGEN
107 typedef struct UCPTrie UCPTrie;
108 #endif
109
119 enum UCPTrieType {
125 UCPTRIE_TYPE_ANY = -1,
130 UCPTRIE_TYPE_FAST,
135 UCPTRIE_TYPE_SMALL
136 };
137 #ifndef U_IN_DOXYGEN
138 typedef enum UCPTrieType UCPTrieType;
139 #endif
140
149 enum UCPTrieValueWidth {
155 UCPTRIE_VALUE_BITS_ANY = -1,
161 UCPTRIE_VALUE_BITS_16,
166 UCPTRIE_VALUE_BITS_32,
172 UCPTRIE_VALUE_BITS_8
173 };
174 #ifndef U_IN_DOXYGEN
175 typedef enum UCPTrieValueWidth UCPTrieValueWidth;
176 #endif
177
204 U_CAPI UCPTrie * U_EXPORT2
205 ucptrie_openFromBinary(UCPTrieType type, UCPTrieValueWidth valueWidth,
206 const void *data, int32_t length, int32_t *pActualLength,
207 UErrorCode *pErrorCode);
208
215 U_CAPI void U_EXPORT2
216 ucptrie_close(UCPTrie *trie);
217
227 U_CAPI UCPTrieType U_EXPORT2
228 ucptrie_getType(const UCPTrie *trie);
229
239 U_CAPI UCPTrieValueWidth U_EXPORT2
240 ucptrie_getValueWidth(const UCPTrie *trie);
241
256 U_CAPI uint32_t U_EXPORT2
257 ucptrie_get(const UCPTrie *trie, UChar32 c);
258
295 U_CAPI UChar32 U_EXPORT2
296 ucptrie_getRange(const UCPTrie *trie, UChar32 start,
297 UCPMapRangeOption option, uint32_t surrogateValue,
298 UCPMapValueFilter *filter, const void *context, uint32_t *pValue);
299
315 U_CAPI int32_t U_EXPORT2
316 ucptrie_toBinary(const UCPTrie *trie, void *data, int32_t capacity, UErrorCode *pErrorCode);
317
326 #define UCPTRIE_16(trie, i) ((trie)->data.ptr16[i])
327
336 #define UCPTRIE_32(trie, i) ((trie)->data.ptr32[i])
337
346 #define UCPTRIE_8(trie, i) ((trie)->data.ptr8[i])
347
358 #define UCPTRIE_FAST_GET(trie, dataAccess, c) dataAccess(trie, _UCPTRIE_CP_INDEX(trie, 0xffff, c))
359
370 #define UCPTRIE_SMALL_GET(trie, dataAccess, c) \
371 dataAccess(trie, _UCPTRIE_CP_INDEX(trie, UCPTRIE_SMALL_MAX, c))
372
386 #define UCPTRIE_FAST_U16_NEXT(trie, dataAccess, src, limit, c, result) UPRV_BLOCK_MACRO_BEGIN { \
387 (c) = *(src)++; \
388 int32_t __index; \
389 if (!U16_IS_SURROGATE(c)) { \
390 __index = _UCPTRIE_FAST_INDEX(trie, c); \
391 } else { \
392 uint16_t __c2; \
393 if (U16_IS_SURROGATE_LEAD(c) && (src) != (limit) && U16_IS_TRAIL(__c2 = *(src))) { \
394 ++(src); \
395 (c) = U16_GET_SUPPLEMENTARY((c), __c2); \
396 __index = _UCPTRIE_SMALL_INDEX(trie, c); \
397 } else { \
398 __index = (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET; \
399 } \
400 } \
401 (result) = dataAccess(trie, __index); \
402 } UPRV_BLOCK_MACRO_END
403
417 #define UCPTRIE_FAST_U16_PREV(trie, dataAccess, start, src, c, result) UPRV_BLOCK_MACRO_BEGIN { \
418 (c) = *--(src); \
419 int32_t __index; \
420 if (!U16_IS_SURROGATE(c)) { \
421 __index = _UCPTRIE_FAST_INDEX(trie, c); \
422 } else { \
423 uint16_t __c2; \
424 if (U16_IS_SURROGATE_TRAIL(c) && (src) != (start) && U16_IS_LEAD(__c2 = *((src) - 1))) { \
425 --(src); \
426 (c) = U16_GET_SUPPLEMENTARY(__c2, (c)); \
427 __index = _UCPTRIE_SMALL_INDEX(trie, c); \
428 } else { \
429 __index = (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET; \
430 } \
431 } \
432 (result) = dataAccess(trie, __index); \
433 } UPRV_BLOCK_MACRO_END
434
451 #define UCPTRIE_FAST_U8_NEXT(trie, dataAccess, src, limit, result) UPRV_BLOCK_MACRO_BEGIN { \
452 int32_t __lead = (uint8_t)*(src)++; \
453 if (!U8_IS_SINGLE(__lead)) { \
454 uint8_t __t1, __t2, __t3; \
455 if ((src) != (limit) && \
456 (__lead >= 0xe0 ? \
457 __lead < 0xf0 ? /* U+0800..U+FFFF except surrogates */ \
458 U8_LEAD3_T1_BITS[__lead &= 0xf] & (1 << ((__t1 = *(src)) >> 5)) && \
459 ++(src) != (limit) && (__t2 = *(src) - 0x80) <= 0x3f && \
460 (__lead = ((int32_t)(trie)->index[(__lead << 6) + (__t1 & 0x3f)]) + __t2, 1) \
461 : /* U+10000..U+10FFFF */ \
462 (__lead -= 0xf0) <= 4 && \
463 U8_LEAD4_T1_BITS[(__t1 = *(src)) >> 4] & (1 << __lead) && \
464 (__lead = (__lead << 6) | (__t1 & 0x3f), ++(src) != (limit)) && \
465 (__t2 = *(src) - 0x80) <= 0x3f && \
466 ++(src) != (limit) && (__t3 = *(src) - 0x80) <= 0x3f && \
467 (__lead = __lead >= (trie)->shifted12HighStart ? \
468 (trie)->dataLength - UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET : \
469 ucptrie_internalSmallU8Index((trie), __lead, __t2, __t3), 1) \
470 : /* U+0080..U+07FF */ \
471 __lead >= 0xc2 && (__t1 = *(src) - 0x80) <= 0x3f && \
472 (__lead = (int32_t)(trie)->index[__lead & 0x1f] + __t1, 1))) { \
473 ++(src); \
474 } else { \
475 __lead = (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET; /* ill-formed*/ \
476 } \
477 } \
478 (result) = dataAccess(trie, __lead); \
479 } UPRV_BLOCK_MACRO_END
480
497 #define UCPTRIE_FAST_U8_PREV(trie, dataAccess, start, src, result) UPRV_BLOCK_MACRO_BEGIN { \
498 int32_t __index = (uint8_t)*--(src); \
499 if (!U8_IS_SINGLE(__index)) { \
500 __index = ucptrie_internalU8PrevIndex((trie), __index, (const uint8_t *)(start), \
501 (const uint8_t *)(src)); \
502 (src) -= __index & 7; \
503 __index >>= 3; \
504 } \
505 (result) = dataAccess(trie, __index); \
506 } UPRV_BLOCK_MACRO_END
507
517 #define UCPTRIE_ASCII_GET(trie, dataAccess, c) dataAccess(trie, c)
518
530 #define UCPTRIE_FAST_BMP_GET(trie, dataAccess, c) dataAccess(trie, _UCPTRIE_FAST_INDEX(trie, c))
531
542 #define UCPTRIE_FAST_SUPP_GET(trie, dataAccess, c) dataAccess(trie, _UCPTRIE_SMALL_INDEX(trie, c))
543
544 /* Internal definitions ----------------------------------------------------- */
545
546 #ifndef U_IN_DOXYGEN
547
553 enum {
555 UCPTRIE_FAST_SHIFT = 6,
556
558 UCPTRIE_FAST_DATA_BLOCK_LENGTH = 1 << UCPTRIE_FAST_SHIFT,
559
561 UCPTRIE_FAST_DATA_MASK = UCPTRIE_FAST_DATA_BLOCK_LENGTH - 1,
562
564 UCPTRIE_SMALL_MAX = 0xfff,
565
571 UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET = 1,
577 UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET = 2
578 };
579
580 /* Internal functions and macros -------------------------------------------- */
581 // Do not conditionalize with #ifndef U_HIDE_INTERNAL_API, needed for public API
582
584 U_CAPI int32_t U_EXPORT2
585 ucptrie_internalSmallIndex(const UCPTrie *trie, UChar32 c);
586
588 U_CAPI int32_t U_EXPORT2
589 ucptrie_internalSmallU8Index(const UCPTrie *trie, int32_t lt1, uint8_t t2, uint8_t t3);
590
596 U_CAPI int32_t U_EXPORT2
597 ucptrie_internalU8PrevIndex(const UCPTrie *trie, UChar32 c,
598 const uint8_t *start, const uint8_t *src);
599
601 #define _UCPTRIE_FAST_INDEX(trie, c) \
602 ((int32_t)(trie)->index[(c) >> UCPTRIE_FAST_SHIFT] + ((c) & UCPTRIE_FAST_DATA_MASK))
603
605 #define _UCPTRIE_SMALL_INDEX(trie, c) \
606 ((c) >= (trie)->highStart ? \
607 (trie)->dataLength - UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET : \
608 ucptrie_internalSmallIndex(trie, c))
609
615 #define _UCPTRIE_CP_INDEX(trie, fastMax, c) \
616 ((uint32_t)(c) <= (uint32_t)(fastMax) ? \
617 _UCPTRIE_FAST_INDEX(trie, c) : \
618 (uint32_t)(c) <= 0x10ffff ? \
619 _UCPTRIE_SMALL_INDEX(trie, c) : \
620 (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET)
621
622 U_CDECL_END
623
624 #endif // U_IN_DOXYGEN
625
626 #if U_SHOW_CPLUSPLUS_API
627
628 U_NAMESPACE_BEGIN
629
639 U_DEFINE_LOCAL_OPEN_POINTER(LocalUCPTriePointer, UCPTrie, ucptrie_close);
640
641 U_NAMESPACE_END
642
643 #endif // U_SHOW_CPLUSPLUS_API
644
645 #endif
LocalUCPTriePointer
"Smart pointer" class, closes a UCPTrie via ucptrie_close().
localpointer.h
C++ API: "Smart pointers" for use with and in ICU4C C++ code.
U_DEFINE_LOCAL_OPEN_POINTER
#define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction)
"Smart pointer" definition macro, deletes objects via the closeFunction.
Definition: localpointer.h:550
UCPTrie
Immutable Unicode code point trie structure.
Definition: ucptrie.h:59
ucpmap.h
C API: This file defines an abstract map from Unicode code points to integer values.
UCPMapValueFilter
uint32_t UCPMapValueFilter(const void *context, uint32_t value)
Callback function type: Modifies a map value.
Definition: ucpmap.h:113
UCPMapRangeOption
UCPMapRangeOption
Selectors for how ucpmap_getRange() etc.
Definition: ucpmap.h:41
UCPTrieType
UCPTrieType
Selectors for the type of a UCPTrie.
Definition: ucptrie.h:119
UCPTRIE_TYPE_FAST
@ UCPTRIE_TYPE_FAST
Fast/simple/larger BMP data structure.
Definition: ucptrie.h:130
UCPTRIE_TYPE_SMALL
@ UCPTRIE_TYPE_SMALL
Small/slower BMP data structure.
Definition: ucptrie.h:135
UCPTRIE_TYPE_ANY
@ UCPTRIE_TYPE_ANY
For ucptrie_openFromBinary() to accept any type.
Definition: ucptrie.h:125
ucptrie_close
U_CAPI void ucptrie_close(UCPTrie *trie)
Closes a trie and releases associated memory.
ucptrie_getValueWidth
U_CAPI UCPTrieValueWidth ucptrie_getValueWidth(const UCPTrie *trie)
Returns the number of bits in a trie data value.
ucptrie_getRange
U_CAPI UChar32 ucptrie_getRange(const UCPTrie *trie, UChar32 start, UCPMapRangeOption option, uint32_t surrogateValue, UCPMapValueFilter *filter, const void *context, uint32_t *pValue)
Returns the last code point such that all those from start to there have the same value.
ucptrie_openFromBinary
U_CAPI UCPTrie * ucptrie_openFromBinary(UCPTrieType type, UCPTrieValueWidth valueWidth, const void *data, int32_t length, int32_t *pActualLength, UErrorCode *pErrorCode)
Opens a trie from its binary form, stored in 32-bit-aligned memory.
ucptrie_toBinary
U_CAPI int32_t ucptrie_toBinary(const UCPTrie *trie, void *data, int32_t capacity, UErrorCode *pErrorCode)
Writes a memory-mappable form of the trie into 32-bit aligned memory.
ucptrie_getType
U_CAPI UCPTrieType ucptrie_getType(const UCPTrie *trie)
Returns the trie type.
UCPTrieValueWidth
UCPTrieValueWidth
Selectors for the number of bits in a UCPTrie data value.
Definition: ucptrie.h:149
UCPTRIE_VALUE_BITS_ANY
@ UCPTRIE_VALUE_BITS_ANY
For ucptrie_openFromBinary() to accept any data value width.
Definition: ucptrie.h:155
UCPTRIE_VALUE_BITS_8
@ UCPTRIE_VALUE_BITS_8
The trie stores 8 bits per data value.
Definition: ucptrie.h:172
UCPTRIE_VALUE_BITS_16
@ UCPTRIE_VALUE_BITS_16
The trie stores 16 bits per data value.
Definition: ucptrie.h:161
UCPTRIE_VALUE_BITS_32
@ UCPTRIE_VALUE_BITS_32
The trie stores 32 bits per data value.
Definition: ucptrie.h:166
ucptrie_get
U_CAPI uint32_t ucptrie_get(const UCPTrie *trie, UChar32 c)
Returns the value for a code point as stored in the trie, with range checking.
UChar32
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition: umachine.h:427
U_CDECL_END
#define U_CDECL_END
This is used to end a declaration of a library private ICU C API.
Definition: umachine.h:86
U_CAPI
#define U_CAPI
This is used to declare a function as a public ICU C API.
Definition: umachine.h:110
U_CDECL_BEGIN
#define U_CDECL_BEGIN
This is used to begin a declaration of a library private ICU C API.
Definition: umachine.h:85
utf8.h
C API: 8-bit Unicode handling macros.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415