ICU 69.1  69.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 
19 
29 #ifndef U_IN_DOXYGEN
31 typedef union UCPTrieData {
33  const void *ptr0;
35  const uint16_t *ptr16;
37  const uint32_t *ptr32;
39  const uint8_t *ptr8;
40 } UCPTrieData;
41 #endif
42 
60 struct UCPTrie {
61 #ifndef U_IN_DOXYGEN
63  const uint16_t *index;
65  UCPTrieData data;
66 
68  int32_t indexLength;
70  int32_t dataLength;
72  UChar32 highStart;
74  uint16_t shifted12HighStart;
75 
77  int8_t type; // UCPTrieType
79  int8_t valueWidth; // UCPTrieValueWidth
80 
82  uint32_t reserved32;
84  uint16_t reserved16;
85 
91  uint16_t index3NullOffset;
97  int32_t dataNullOffset;
99  uint32_t nullValue;
100 
101 #ifdef UCPTRIE_DEBUG
103  const char *name;
104 #endif
105 #endif
106 };
107 #ifndef U_IN_DOXYGEN
108 typedef struct UCPTrie UCPTrie;
109 #endif
110 
137 };
138 #ifndef U_IN_DOXYGEN
139 typedef enum UCPTrieType UCPTrieType;
140 #endif
141 
174 };
175 #ifndef U_IN_DOXYGEN
177 #endif
178 
205 U_CAPI UCPTrie * U_EXPORT2
207  const void *data, int32_t length, int32_t *pActualLength,
208  UErrorCode *pErrorCode);
209 
216 U_CAPI void U_EXPORT2
218 
228 U_CAPI UCPTrieType U_EXPORT2
230 
240 U_CAPI UCPTrieValueWidth U_EXPORT2
242 
257 U_CAPI uint32_t U_EXPORT2
258 ucptrie_get(const UCPTrie *trie, UChar32 c);
259 
296 U_CAPI UChar32 U_EXPORT2
297 ucptrie_getRange(const UCPTrie *trie, UChar32 start,
298  UCPMapRangeOption option, uint32_t surrogateValue,
299  UCPMapValueFilter *filter, const void *context, uint32_t *pValue);
300 
316 U_CAPI int32_t U_EXPORT2
317 ucptrie_toBinary(const UCPTrie *trie, void *data, int32_t capacity, UErrorCode *pErrorCode);
318 
327 #define UCPTRIE_16(trie, i) ((trie)->data.ptr16[i])
328 
337 #define UCPTRIE_32(trie, i) ((trie)->data.ptr32[i])
338 
347 #define UCPTRIE_8(trie, i) ((trie)->data.ptr8[i])
348 
359 #define UCPTRIE_FAST_GET(trie, dataAccess, c) dataAccess(trie, _UCPTRIE_CP_INDEX(trie, 0xffff, c))
360 
371 #define UCPTRIE_SMALL_GET(trie, dataAccess, c) \
372  dataAccess(trie, _UCPTRIE_CP_INDEX(trie, UCPTRIE_SMALL_MAX, c))
373 
387 #define UCPTRIE_FAST_U16_NEXT(trie, dataAccess, src, limit, c, result) UPRV_BLOCK_MACRO_BEGIN { \
388  (c) = *(src)++; \
389  int32_t __index; \
390  if (!U16_IS_SURROGATE(c)) { \
391  __index = _UCPTRIE_FAST_INDEX(trie, c); \
392  } else { \
393  uint16_t __c2; \
394  if (U16_IS_SURROGATE_LEAD(c) && (src) != (limit) && U16_IS_TRAIL(__c2 = *(src))) { \
395  ++(src); \
396  (c) = U16_GET_SUPPLEMENTARY((c), __c2); \
397  __index = _UCPTRIE_SMALL_INDEX(trie, c); \
398  } else { \
399  __index = (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET; \
400  } \
401  } \
402  (result) = dataAccess(trie, __index); \
403 } UPRV_BLOCK_MACRO_END
404 
418 #define UCPTRIE_FAST_U16_PREV(trie, dataAccess, start, src, c, result) UPRV_BLOCK_MACRO_BEGIN { \
419  (c) = *--(src); \
420  int32_t __index; \
421  if (!U16_IS_SURROGATE(c)) { \
422  __index = _UCPTRIE_FAST_INDEX(trie, c); \
423  } else { \
424  uint16_t __c2; \
425  if (U16_IS_SURROGATE_TRAIL(c) && (src) != (start) && U16_IS_LEAD(__c2 = *((src) - 1))) { \
426  --(src); \
427  (c) = U16_GET_SUPPLEMENTARY(__c2, (c)); \
428  __index = _UCPTRIE_SMALL_INDEX(trie, c); \
429  } else { \
430  __index = (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET; \
431  } \
432  } \
433  (result) = dataAccess(trie, __index); \
434 } UPRV_BLOCK_MACRO_END
435 
452 #define UCPTRIE_FAST_U8_NEXT(trie, dataAccess, src, limit, result) UPRV_BLOCK_MACRO_BEGIN { \
453  int32_t __lead = (uint8_t)*(src)++; \
454  if (!U8_IS_SINGLE(__lead)) { \
455  uint8_t __t1, __t2, __t3; \
456  if ((src) != (limit) && \
457  (__lead >= 0xe0 ? \
458  __lead < 0xf0 ? /* U+0800..U+FFFF except surrogates */ \
459  U8_LEAD3_T1_BITS[__lead &= 0xf] & (1 << ((__t1 = *(src)) >> 5)) && \
460  ++(src) != (limit) && (__t2 = *(src) - 0x80) <= 0x3f && \
461  (__lead = ((int32_t)(trie)->index[(__lead << 6) + (__t1 & 0x3f)]) + __t2, 1) \
462  : /* U+10000..U+10FFFF */ \
463  (__lead -= 0xf0) <= 4 && \
464  U8_LEAD4_T1_BITS[(__t1 = *(src)) >> 4] & (1 << __lead) && \
465  (__lead = (__lead << 6) | (__t1 & 0x3f), ++(src) != (limit)) && \
466  (__t2 = *(src) - 0x80) <= 0x3f && \
467  ++(src) != (limit) && (__t3 = *(src) - 0x80) <= 0x3f && \
468  (__lead = __lead >= (trie)->shifted12HighStart ? \
469  (trie)->dataLength - UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET : \
470  ucptrie_internalSmallU8Index((trie), __lead, __t2, __t3), 1) \
471  : /* U+0080..U+07FF */ \
472  __lead >= 0xc2 && (__t1 = *(src) - 0x80) <= 0x3f && \
473  (__lead = (int32_t)(trie)->index[__lead & 0x1f] + __t1, 1))) { \
474  ++(src); \
475  } else { \
476  __lead = (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET; /* ill-formed*/ \
477  } \
478  } \
479  (result) = dataAccess(trie, __lead); \
480 } UPRV_BLOCK_MACRO_END
481 
498 #define UCPTRIE_FAST_U8_PREV(trie, dataAccess, start, src, result) UPRV_BLOCK_MACRO_BEGIN { \
499  int32_t __index = (uint8_t)*--(src); \
500  if (!U8_IS_SINGLE(__index)) { \
501  __index = ucptrie_internalU8PrevIndex((trie), __index, (const uint8_t *)(start), \
502  (const uint8_t *)(src)); \
503  (src) -= __index & 7; \
504  __index >>= 3; \
505  } \
506  (result) = dataAccess(trie, __index); \
507 } UPRV_BLOCK_MACRO_END
508 
518 #define UCPTRIE_ASCII_GET(trie, dataAccess, c) dataAccess(trie, c)
519 
531 #define UCPTRIE_FAST_BMP_GET(trie, dataAccess, c) dataAccess(trie, _UCPTRIE_FAST_INDEX(trie, c))
532 
543 #define UCPTRIE_FAST_SUPP_GET(trie, dataAccess, c) dataAccess(trie, _UCPTRIE_SMALL_INDEX(trie, c))
544 
545 /* Internal definitions ----------------------------------------------------- */
546 
547 #ifndef U_IN_DOXYGEN
548 
554 enum {
556  UCPTRIE_FAST_SHIFT = 6,
557 
559  UCPTRIE_FAST_DATA_BLOCK_LENGTH = 1 << UCPTRIE_FAST_SHIFT,
560 
562  UCPTRIE_FAST_DATA_MASK = UCPTRIE_FAST_DATA_BLOCK_LENGTH - 1,
563 
565  UCPTRIE_SMALL_MAX = 0xfff,
566 
572  UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET = 1,
578  UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET = 2
579 };
580 
581 /* Internal functions and macros -------------------------------------------- */
582 // Do not conditionalize with #ifndef U_HIDE_INTERNAL_API, needed for public API
583 
585 U_CAPI int32_t U_EXPORT2
586 ucptrie_internalSmallIndex(const UCPTrie *trie, UChar32 c);
587 
589 U_CAPI int32_t U_EXPORT2
590 ucptrie_internalSmallU8Index(const UCPTrie *trie, int32_t lt1, uint8_t t2, uint8_t t3);
591 
597 U_CAPI int32_t U_EXPORT2
598 ucptrie_internalU8PrevIndex(const UCPTrie *trie, UChar32 c,
599  const uint8_t *start, const uint8_t *src);
600 
602 #define _UCPTRIE_FAST_INDEX(trie, c) \
603  ((int32_t)(trie)->index[(c) >> UCPTRIE_FAST_SHIFT] + ((c) & UCPTRIE_FAST_DATA_MASK))
604 
606 #define _UCPTRIE_SMALL_INDEX(trie, c) \
607  ((c) >= (trie)->highStart ? \
608  (trie)->dataLength - UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET : \
609  ucptrie_internalSmallIndex(trie, c))
610 
616 #define _UCPTRIE_CP_INDEX(trie, fastMax, c) \
617  ((uint32_t)(c) <= (uint32_t)(fastMax) ? \
618  _UCPTRIE_FAST_INDEX(trie, c) : \
619  (uint32_t)(c) <= 0x10ffff ? \
620  _UCPTRIE_SMALL_INDEX(trie, c) : \
621  (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET)
622 
624 
625 #endif // U_IN_DOXYGEN
626 
627 #if U_SHOW_CPLUSPLUS_API
628 
629 U_NAMESPACE_BEGIN
630 
641 
642 U_NAMESPACE_END
643 
644 #endif // U_SHOW_CPLUSPLUS_API
645 
646 #endif
"Smart pointer" class, closes a UCPTrie via ucptrie_close().
C++ API: "Smart pointers" for use with and in ICU4C C++ code.
#define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction)
"Smart pointer" definition macro, deletes objects via the closeFunction.
Definition: localpointer.h:550
Immutable Unicode code point trie structure.
Definition: ucptrie.h:60
This file defines an abstract map from Unicode code points to integer values.
uint32_t UCPMapValueFilter(const void *context, uint32_t value)
Callback function type: Modifies a map value.
Definition: ucpmap.h:114
UCPMapRangeOption
Selectors for how ucpmap_getRange() etc.
Definition: ucpmap.h:42
UCPTrieType
Selectors for the type of a UCPTrie.
Definition: ucptrie.h:120
@ UCPTRIE_TYPE_FAST
Fast/simple/larger BMP data structure.
Definition: ucptrie.h:131
@ UCPTRIE_TYPE_SMALL
Small/slower BMP data structure.
Definition: ucptrie.h:136
@ UCPTRIE_TYPE_ANY
For ucptrie_openFromBinary() to accept any type.
Definition: ucptrie.h:126
U_CAPI void ucptrie_close(UCPTrie *trie)
Closes a trie and releases associated memory.
U_CAPI UCPTrieValueWidth ucptrie_getValueWidth(const UCPTrie *trie)
Returns the number of bits in a trie data value.
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.
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.
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.
U_CAPI UCPTrieType ucptrie_getType(const UCPTrie *trie)
Returns the trie type.
UCPTrieValueWidth
Selectors for the number of bits in a UCPTrie data value.
Definition: ucptrie.h:150
@ UCPTRIE_VALUE_BITS_ANY
For ucptrie_openFromBinary() to accept any data value width.
Definition: ucptrie.h:156
@ UCPTRIE_VALUE_BITS_8
The trie stores 8 bits per data value.
Definition: ucptrie.h:173
@ UCPTRIE_VALUE_BITS_16
The trie stores 16 bits per data value.
Definition: ucptrie.h:162
@ UCPTRIE_VALUE_BITS_32
The trie stores 32 bits per data value.
Definition: ucptrie.h:167
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.
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition: umachine.h:467
#define U_CDECL_END
This is used to end a declaration of a library private ICU C API.
Definition: umachine.h:86
#define U_CAPI
This is used to declare a function as a public ICU C API.
Definition: umachine.h:110
#define U_CDECL_BEGIN
This is used to begin a declaration of a library private ICU C API.
Definition: umachine.h:85
C API: 8-bit Unicode handling macros.
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415