00001
00002
00003
00004
00005
00006
00007 #ifndef __UCPTRIE_H__
00008 #define __UCPTRIE_H__
00009
00010 #include "unicode/utypes.h"
00011 #include "unicode/ucpmap.h"
00012 #include "unicode/utf8.h"
00013
00014 #if U_SHOW_CPLUSPLUS_API
00015 #include "unicode/localpointer.h"
00016 #endif // U_SHOW_CPLUSPLUS_API
00017
00018 U_CDECL_BEGIN
00019
00029 #ifndef U_IN_DOXYGEN
00030
00031 typedef union UCPTrieData {
00033 const void *ptr0;
00035 const uint16_t *ptr16;
00037 const uint32_t *ptr32;
00039 const uint8_t *ptr8;
00040 } UCPTrieData;
00041 #endif
00042
00060 struct UCPTrie {
00061 #ifndef U_IN_DOXYGEN
00062
00063 const uint16_t *index;
00065 UCPTrieData data;
00066
00068 int32_t indexLength;
00070 int32_t dataLength;
00072 UChar32 highStart;
00074 uint16_t shifted12HighStart;
00075
00077 int8_t type;
00079 int8_t valueWidth;
00080
00082 uint32_t reserved32;
00084 uint16_t reserved16;
00085
00091 uint16_t index3NullOffset;
00097 int32_t dataNullOffset;
00099 uint32_t nullValue;
00100
00101 #ifdef UCPTRIE_DEBUG
00102
00103 const char *name;
00104 #endif
00105 #endif
00106 };
00107 #ifndef U_IN_DOXYGEN
00108 typedef struct UCPTrie UCPTrie;
00109 #endif
00110
00120 enum UCPTrieType {
00126 UCPTRIE_TYPE_ANY = -1,
00131 UCPTRIE_TYPE_FAST,
00136 UCPTRIE_TYPE_SMALL
00137 };
00138 #ifndef U_IN_DOXYGEN
00139 typedef enum UCPTrieType UCPTrieType;
00140 #endif
00141
00150 enum UCPTrieValueWidth {
00156 UCPTRIE_VALUE_BITS_ANY = -1,
00162 UCPTRIE_VALUE_BITS_16,
00167 UCPTRIE_VALUE_BITS_32,
00173 UCPTRIE_VALUE_BITS_8
00174 };
00175 #ifndef U_IN_DOXYGEN
00176 typedef enum UCPTrieValueWidth UCPTrieValueWidth;
00177 #endif
00178
00205 U_CAPI UCPTrie * U_EXPORT2
00206 ucptrie_openFromBinary(UCPTrieType type, UCPTrieValueWidth valueWidth,
00207 const void *data, int32_t length, int32_t *pActualLength,
00208 UErrorCode *pErrorCode);
00209
00216 U_CAPI void U_EXPORT2
00217 ucptrie_close(UCPTrie *trie);
00218
00228 U_CAPI UCPTrieType U_EXPORT2
00229 ucptrie_getType(const UCPTrie *trie);
00230
00240 U_CAPI UCPTrieValueWidth U_EXPORT2
00241 ucptrie_getValueWidth(const UCPTrie *trie);
00242
00257 U_CAPI uint32_t U_EXPORT2
00258 ucptrie_get(const UCPTrie *trie, UChar32 c);
00259
00296 U_CAPI UChar32 U_EXPORT2
00297 ucptrie_getRange(const UCPTrie *trie, UChar32 start,
00298 UCPMapRangeOption option, uint32_t surrogateValue,
00299 UCPMapValueFilter *filter, const void *context, uint32_t *pValue);
00300
00316 U_CAPI int32_t U_EXPORT2
00317 ucptrie_toBinary(const UCPTrie *trie, void *data, int32_t capacity, UErrorCode *pErrorCode);
00318
00327 #define UCPTRIE_16(trie, i) ((trie)->data.ptr16[i])
00328
00337 #define UCPTRIE_32(trie, i) ((trie)->data.ptr32[i])
00338
00347 #define UCPTRIE_8(trie, i) ((trie)->data.ptr8[i])
00348
00359 #define UCPTRIE_FAST_GET(trie, dataAccess, c) dataAccess(trie, _UCPTRIE_CP_INDEX(trie, 0xffff, c))
00360
00371 #define UCPTRIE_SMALL_GET(trie, dataAccess, c) \
00372 dataAccess(trie, _UCPTRIE_CP_INDEX(trie, UCPTRIE_SMALL_MAX, c))
00373
00387 #define UCPTRIE_FAST_U16_NEXT(trie, dataAccess, src, limit, c, result) UPRV_BLOCK_MACRO_BEGIN { \
00388 (c) = *(src)++; \
00389 int32_t __index; \
00390 if (!U16_IS_SURROGATE(c)) { \
00391 __index = _UCPTRIE_FAST_INDEX(trie, c); \
00392 } else { \
00393 uint16_t __c2; \
00394 if (U16_IS_SURROGATE_LEAD(c) && (src) != (limit) && U16_IS_TRAIL(__c2 = *(src))) { \
00395 ++(src); \
00396 (c) = U16_GET_SUPPLEMENTARY((c), __c2); \
00397 __index = _UCPTRIE_SMALL_INDEX(trie, c); \
00398 } else { \
00399 __index = (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET; \
00400 } \
00401 } \
00402 (result) = dataAccess(trie, __index); \
00403 } UPRV_BLOCK_MACRO_END
00404
00418 #define UCPTRIE_FAST_U16_PREV(trie, dataAccess, start, src, c, result) UPRV_BLOCK_MACRO_BEGIN { \
00419 (c) = *--(src); \
00420 int32_t __index; \
00421 if (!U16_IS_SURROGATE(c)) { \
00422 __index = _UCPTRIE_FAST_INDEX(trie, c); \
00423 } else { \
00424 uint16_t __c2; \
00425 if (U16_IS_SURROGATE_TRAIL(c) && (src) != (start) && U16_IS_LEAD(__c2 = *((src) - 1))) { \
00426 --(src); \
00427 (c) = U16_GET_SUPPLEMENTARY(__c2, (c)); \
00428 __index = _UCPTRIE_SMALL_INDEX(trie, c); \
00429 } else { \
00430 __index = (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET; \
00431 } \
00432 } \
00433 (result) = dataAccess(trie, __index); \
00434 } UPRV_BLOCK_MACRO_END
00435
00452 #define UCPTRIE_FAST_U8_NEXT(trie, dataAccess, src, limit, result) UPRV_BLOCK_MACRO_BEGIN { \
00453 int32_t __lead = (uint8_t)*(src)++; \
00454 if (!U8_IS_SINGLE(__lead)) { \
00455 uint8_t __t1, __t2, __t3; \
00456 if ((src) != (limit) && \
00457 (__lead >= 0xe0 ? \
00458 __lead < 0xf0 ? \
00459 U8_LEAD3_T1_BITS[__lead &= 0xf] & (1 << ((__t1 = *(src)) >> 5)) && \
00460 ++(src) != (limit) && (__t2 = *(src) - 0x80) <= 0x3f && \
00461 (__lead = ((int32_t)(trie)->index[(__lead << 6) + (__t1 & 0x3f)]) + __t2, 1) \
00462 : \
00463 (__lead -= 0xf0) <= 4 && \
00464 U8_LEAD4_T1_BITS[(__t1 = *(src)) >> 4] & (1 << __lead) && \
00465 (__lead = (__lead << 6) | (__t1 & 0x3f), ++(src) != (limit)) && \
00466 (__t2 = *(src) - 0x80) <= 0x3f && \
00467 ++(src) != (limit) && (__t3 = *(src) - 0x80) <= 0x3f && \
00468 (__lead = __lead >= (trie)->shifted12HighStart ? \
00469 (trie)->dataLength - UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET : \
00470 ucptrie_internalSmallU8Index((trie), __lead, __t2, __t3), 1) \
00471 : \
00472 __lead >= 0xc2 && (__t1 = *(src) - 0x80) <= 0x3f && \
00473 (__lead = (int32_t)(trie)->index[__lead & 0x1f] + __t1, 1))) { \
00474 ++(src); \
00475 } else { \
00476 __lead = (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET; \
00477 } \
00478 } \
00479 (result) = dataAccess(trie, __lead); \
00480 } UPRV_BLOCK_MACRO_END
00481
00498 #define UCPTRIE_FAST_U8_PREV(trie, dataAccess, start, src, result) UPRV_BLOCK_MACRO_BEGIN { \
00499 int32_t __index = (uint8_t)*--(src); \
00500 if (!U8_IS_SINGLE(__index)) { \
00501 __index = ucptrie_internalU8PrevIndex((trie), __index, (const uint8_t *)(start), \
00502 (const uint8_t *)(src)); \
00503 (src) -= __index & 7; \
00504 __index >>= 3; \
00505 } \
00506 (result) = dataAccess(trie, __index); \
00507 } UPRV_BLOCK_MACRO_END
00508
00518 #define UCPTRIE_ASCII_GET(trie, dataAccess, c) dataAccess(trie, c)
00519
00531 #define UCPTRIE_FAST_BMP_GET(trie, dataAccess, c) dataAccess(trie, _UCPTRIE_FAST_INDEX(trie, c))
00532
00543 #define UCPTRIE_FAST_SUPP_GET(trie, dataAccess, c) dataAccess(trie, _UCPTRIE_SMALL_INDEX(trie, c))
00544
00545
00546
00547 #ifndef U_IN_DOXYGEN
00548
00554 enum {
00556 UCPTRIE_FAST_SHIFT = 6,
00557
00559 UCPTRIE_FAST_DATA_BLOCK_LENGTH = 1 << UCPTRIE_FAST_SHIFT,
00560
00562 UCPTRIE_FAST_DATA_MASK = UCPTRIE_FAST_DATA_BLOCK_LENGTH - 1,
00563
00565 UCPTRIE_SMALL_MAX = 0xfff,
00566
00572 UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET = 1,
00578 UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET = 2
00579 };
00580
00581
00582
00583
00585 U_CAPI int32_t U_EXPORT2
00586 ucptrie_internalSmallIndex(const UCPTrie *trie, UChar32 c);
00587
00589 U_CAPI int32_t U_EXPORT2
00590 ucptrie_internalSmallU8Index(const UCPTrie *trie, int32_t lt1, uint8_t t2, uint8_t t3);
00591
00597 U_CAPI int32_t U_EXPORT2
00598 ucptrie_internalU8PrevIndex(const UCPTrie *trie, UChar32 c,
00599 const uint8_t *start, const uint8_t *src);
00600
00602 #define _UCPTRIE_FAST_INDEX(trie, c) \
00603 ((int32_t)(trie)->index[(c) >> UCPTRIE_FAST_SHIFT] + ((c) & UCPTRIE_FAST_DATA_MASK))
00604
00606 #define _UCPTRIE_SMALL_INDEX(trie, c) \
00607 ((c) >= (trie)->highStart ? \
00608 (trie)->dataLength - UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET : \
00609 ucptrie_internalSmallIndex(trie, c))
00610
00616 #define _UCPTRIE_CP_INDEX(trie, fastMax, c) \
00617 ((uint32_t)(c) <= (uint32_t)(fastMax) ? \
00618 _UCPTRIE_FAST_INDEX(trie, c) : \
00619 (uint32_t)(c) <= 0x10ffff ? \
00620 _UCPTRIE_SMALL_INDEX(trie, c) : \
00621 (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET)
00622
00623 U_CDECL_END
00624
00625 #endif // U_IN_DOXYGEN
00626
00627 #if U_SHOW_CPLUSPLUS_API
00628
00629 U_NAMESPACE_BEGIN
00630
00640 U_DEFINE_LOCAL_OPEN_POINTER(LocalUCPTriePointer, UCPTrie, ucptrie_close);
00641
00642 U_NAMESPACE_END
00643
00644 #endif // U_SHOW_CPLUSPLUS_API
00645
00646 #endif