sl@0: /* sl@0: ****************************************************************************** sl@0: * sl@0: * Copyright (C) 2001-2005, International Business Machines sl@0: * Corporation and others. All Rights Reserved. sl@0: * sl@0: ****************************************************************************** sl@0: * file name: utrie.h sl@0: * encoding: US-ASCII sl@0: * tab size: 8 (not used) sl@0: * indentation:4 sl@0: * sl@0: * created on: 2001nov08 sl@0: * created by: Markus W. Scherer sl@0: */ sl@0: sl@0: #ifndef __UTRIE_H__ sl@0: #define __UTRIE_H__ sl@0: sl@0: #include "unicode/utypes.h" sl@0: #include "udataswp.h" sl@0: sl@0: U_CDECL_BEGIN sl@0: sl@0: /** sl@0: * \file sl@0: * sl@0: * This is a common implementation of a "folded" trie. sl@0: * It is a kind of compressed, serializable table of 16- or 32-bit values associated with sl@0: * Unicode code points (0..0x10ffff). sl@0: * sl@0: * This implementation is optimized for getting values while walking forward sl@0: * through a UTF-16 string. sl@0: * Therefore, the simplest and fastest access macros are the sl@0: * _FROM_LEAD() and _FROM_OFFSET_TRAIL() macros. sl@0: * sl@0: * The _FROM_BMP() macros are a little more complicated; they get values sl@0: * even for lead surrogate code _points_, while the _FROM_LEAD() macros sl@0: * get special "folded" values for lead surrogate code _units_ if sl@0: * there is relevant data associated with them. sl@0: * From such a folded value, an offset needs to be extracted to supply sl@0: * to the _FROM_OFFSET_TRAIL() macros. sl@0: * sl@0: * Most of the more complex (and more convenient) functions/macros call a callback function sl@0: * to get that offset from the folded value for a lead surrogate unit. sl@0: */ sl@0: sl@0: /** sl@0: * Trie constants, defining shift widths, index array lengths, etc. sl@0: */ sl@0: enum { sl@0: /** Shift size for shifting right the input index. 1..9 */ sl@0: UTRIE_SHIFT=5, sl@0: sl@0: /** Number of data values in a stage 2 (data array) block. 2, 4, 8, .., 0x200 */ sl@0: UTRIE_DATA_BLOCK_LENGTH=1<>UTRIE_SHIFT, sl@0: sl@0: /** sl@0: * Shift size for shifting left the index array values. sl@0: * Increases possible data size with 16-bit index values at the cost sl@0: * of compactability. sl@0: * This requires blocks of stage 2 data to be aligned by UTRIE_DATA_GRANULARITY. sl@0: * 0..UTRIE_SHIFT sl@0: */ sl@0: UTRIE_INDEX_SHIFT=2, sl@0: sl@0: /** The alignment size of a stage 2 data block. Also the granularity for compaction. */ sl@0: UTRIE_DATA_GRANULARITY=1<>UTRIE_SHIFT sl@0: */ sl@0: UTRIE_SURROGATE_BLOCK_COUNT=(1<>UTRIE_SHIFT sl@0: }; sl@0: sl@0: /** sl@0: * Length of the index (stage 1) array before folding. sl@0: * Maximum number of Unicode code points (0x110000) shifted right by UTRIE_SHIFT. sl@0: */ sl@0: #define UTRIE_MAX_INDEX_LENGTH (0x110000>>UTRIE_SHIFT) sl@0: sl@0: /** sl@0: * Maximum length of the runtime data (stage 2) array. sl@0: * Limited by 16-bit index values that are left-shifted by UTRIE_INDEX_SHIFT. sl@0: */ sl@0: #define UTRIE_MAX_DATA_LENGTH (0x10000<=UTRIE_BMP_INDEX_LENGTH, or 0 if there is no data for the lead surrogate sl@0: */ sl@0: typedef int32_t U_CALLCONV sl@0: UTrieGetFoldingOffset(uint32_t data); sl@0: sl@0: /** sl@0: * Run-time Trie structure. sl@0: * sl@0: * Either the data table is 16 bits wide and accessed via the index sl@0: * pointer, with each index item increased by indexLength; sl@0: * in this case, data32==NULL. sl@0: * sl@0: * Or the data table is 32 bits wide and accessed via the data32 pointer. sl@0: */ sl@0: struct UTrie { sl@0: const uint16_t *index; sl@0: const uint32_t *data32; /* NULL if 16b data is used via index */ sl@0: sl@0: /** sl@0: * This function is not used in _FROM_LEAD, _FROM_BMP, and _FROM_OFFSET_TRAIL macros. sl@0: * If convenience macros like _GET16 or _NEXT32 are used, this function must be set. sl@0: * sl@0: * utrie_unserialize() sets a default function which simply returns sl@0: * the lead surrogate's value itself - which is the inverse of the default sl@0: * folding function used by utrie_serialize(). sl@0: * sl@0: * @see UTrieGetFoldingOffset sl@0: */ sl@0: UTrieGetFoldingOffset *getFoldingOffset; sl@0: sl@0: int32_t indexLength, dataLength; sl@0: uint32_t initialValue; sl@0: UBool isLatin1Linear; sl@0: }; sl@0: sl@0: typedef struct UTrie UTrie; sl@0: sl@0: /** Internal trie getter from an offset (0 if c16 is a BMP/lead units) and a 16-bit unit */ sl@0: #define _UTRIE_GET_RAW(trie, data, offset, c16) \ sl@0: (trie)->data[ \ sl@0: ((int32_t)((trie)->index[(offset)+((c16)>>UTRIE_SHIFT)])<getFoldingOffset(result); \ sl@0: \ sl@0: /* get the real data from the folded lead/trail units */ \ sl@0: if(__offset>0) { \ sl@0: (result)=_UTRIE_GET_RAW((trie), data, __offset, (c2)&0x3ff); \ sl@0: } else { \ sl@0: (result)=(resultType)((trie)->initialValue); \ sl@0: } \ sl@0: } sl@0: sl@0: /** Internal trie getter from a BMP code point, treating a lead surrogate as a normal code point */ sl@0: #define _UTRIE_GET_FROM_BMP(trie, data, c16) \ sl@0: _UTRIE_GET_RAW(trie, data, 0xd800<=(c16) && (c16)<=0xdbff ? UTRIE_LEAD_INDEX_DISP : 0, c16); sl@0: sl@0: /** sl@0: * Internal trie getter from a code point. sl@0: * Could be faster(?) but longer with sl@0: * if((c32)<=0xd7ff) { (result)=_UTRIE_GET_RAW(trie, data, 0, c32); } sl@0: */ sl@0: #define _UTRIE_GET(trie, data, c32, result, resultType) \ sl@0: if((uint32_t)(c32)<=0xffff) { \ sl@0: /* BMP code points */ \ sl@0: (result)=_UTRIE_GET_FROM_BMP(trie, data, c32); \ sl@0: } else if((uint32_t)(c32)<=0x10ffff) { \ sl@0: /* supplementary code point */ \ sl@0: UChar __lead16=UTF16_LEAD(c32); \ sl@0: _UTRIE_GET_FROM_PAIR(trie, data, __lead16, c32, result, resultType); \ sl@0: } else { \ sl@0: /* out of range */ \ sl@0: (result)=(resultType)((trie)->initialValue); \ sl@0: } sl@0: sl@0: /** Internal next-post-increment: get the next code point (c, c2) and its data */ sl@0: #define _UTRIE_NEXT(trie, data, src, limit, c, c2, result, resultType) { \ sl@0: (c)=*(src)++; \ sl@0: if(!UTF_IS_LEAD(c)) { \ sl@0: (c2)=0; \ sl@0: (result)=_UTRIE_GET_RAW((trie), data, 0, (c)); \ sl@0: } else if((src)!=(limit) && UTF_IS_TRAIL((c2)=*(src))) { \ sl@0: ++(src); \ sl@0: _UTRIE_GET_FROM_PAIR((trie), data, (c), (c2), (result), resultType); \ sl@0: } else { \ sl@0: /* unpaired lead surrogate code point */ \ sl@0: (c2)=0; \ sl@0: (result)=_UTRIE_GET_RAW((trie), data, UTRIE_LEAD_INDEX_DISP, (c)); \ sl@0: } \ sl@0: } sl@0: sl@0: /** Internal previous: get the previous code point (c, c2) and its data */ sl@0: #define _UTRIE_PREVIOUS(trie, data, start, src, c, c2, result, resultType) { \ sl@0: (c)=*--(src); \ sl@0: if(!UTF_IS_SURROGATE(c)) { \ sl@0: (c2)=0; \ sl@0: (result)=_UTRIE_GET_RAW((trie), data, 0, (c)); \ sl@0: } else if(!UTF_IS_SURROGATE_FIRST(c)) { \ sl@0: /* trail surrogate */ \ sl@0: if((start)!=(src) && UTF_IS_LEAD((c2)=*((src)-1))) { \ sl@0: --(src); \ sl@0: (result)=(c); (c)=(c2); (c2)=(UChar)(result); /* swap c, c2 */ \ sl@0: _UTRIE_GET_FROM_PAIR((trie), data, (c), (c2), (result), resultType); \ sl@0: } else { \ sl@0: /* unpaired trail surrogate code point */ \ sl@0: (c2)=0; \ sl@0: (result)=_UTRIE_GET_RAW((trie), data, 0, (c)); \ sl@0: } \ sl@0: } else { \ sl@0: /* unpaired lead surrogate code point */ \ sl@0: (c2)=0; \ sl@0: (result)=_UTRIE_GET_RAW((trie), data, UTRIE_LEAD_INDEX_DISP, (c)); \ sl@0: } \ sl@0: } sl@0: sl@0: /* Public UTrie API ---------------------------------------------------------*/ sl@0: sl@0: /** sl@0: * Get a pointer to the contiguous part of the data array sl@0: * for the Latin-1 range (U+0000..U+00ff). sl@0: * Must be used only if the Latin-1 range is in fact linear sl@0: * (trie->isLatin1Linear). sl@0: * sl@0: * @param trie (const UTrie *, in) a pointer to the runtime trie structure sl@0: * @return (const uint16_t *) pointer to values for Latin-1 code points sl@0: */ sl@0: #define UTRIE_GET16_LATIN1(trie) ((trie)->index+(trie)->indexLength+UTRIE_DATA_BLOCK_LENGTH) sl@0: sl@0: /** sl@0: * Get a pointer to the contiguous part of the data array sl@0: * for the Latin-1 range (U+0000..U+00ff). sl@0: * Must be used only if the Latin-1 range is in fact linear sl@0: * (trie->isLatin1Linear). sl@0: * sl@0: * @param trie (const UTrie *, in) a pointer to the runtime trie structure sl@0: * @return (const uint32_t *) pointer to values for Latin-1 code points sl@0: */ sl@0: #define UTRIE_GET32_LATIN1(trie) ((trie)->data32+UTRIE_DATA_BLOCK_LENGTH) sl@0: sl@0: /** sl@0: * Get a 16-bit trie value from a BMP code point (UChar, <=U+ffff). sl@0: * c16 may be a lead surrogate, which may have a value including a folding offset. sl@0: * sl@0: * @param trie (const UTrie *, in) a pointer to the runtime trie structure sl@0: * @param c16 (UChar, in) the input BMP code point sl@0: * @return (uint16_t) trie lookup result sl@0: */ sl@0: #define UTRIE_GET16_FROM_LEAD(trie, c16) _UTRIE_GET_RAW(trie, index, 0, c16) sl@0: sl@0: /** sl@0: * Get a 32-bit trie value from a BMP code point (UChar, <=U+ffff). sl@0: * c16 may be a lead surrogate, which may have a value including a folding offset. sl@0: * sl@0: * @param trie (const UTrie *, in) a pointer to the runtime trie structure sl@0: * @param c16 (UChar, in) the input BMP code point sl@0: * @return (uint32_t) trie lookup result sl@0: */ sl@0: #define UTRIE_GET32_FROM_LEAD(trie, c16) _UTRIE_GET_RAW(trie, data32, 0, c16) sl@0: sl@0: /** sl@0: * Get a 16-bit trie value from a BMP code point (UChar, <=U+ffff). sl@0: * Even lead surrogate code points are treated as normal code points, sl@0: * with unfolded values that may differ from _FROM_LEAD() macro results for them. sl@0: * sl@0: * @param trie (const UTrie *, in) a pointer to the runtime trie structure sl@0: * @param c16 (UChar, in) the input BMP code point sl@0: * @return (uint16_t) trie lookup result sl@0: */ sl@0: #define UTRIE_GET16_FROM_BMP(trie, c16) _UTRIE_GET_FROM_BMP(trie, index, c16) sl@0: sl@0: /** sl@0: * Get a 32-bit trie value from a BMP code point (UChar, <=U+ffff). sl@0: * Even lead surrogate code points are treated as normal code points, sl@0: * with unfolded values that may differ from _FROM_LEAD() macro results for them. sl@0: * sl@0: * @param trie (const UTrie *, in) a pointer to the runtime trie structure sl@0: * @param c16 (UChar, in) the input BMP code point sl@0: * @return (uint32_t) trie lookup result sl@0: */ sl@0: #define UTRIE_GET32_FROM_BMP(trie, c16) _UTRIE_GET_FROM_BMP(trie, data32, c16) sl@0: sl@0: /** sl@0: * Get a 16-bit trie value from a code point. sl@0: * Even lead surrogate code points are treated as normal code points, sl@0: * with unfolded values that may differ from _FROM_LEAD() macro results for them. sl@0: * sl@0: * @param trie (const UTrie *, in) a pointer to the runtime trie structure sl@0: * @param c32 (UChar32, in) the input code point sl@0: * @param result (uint16_t, out) uint16_t variable for the trie lookup result sl@0: */ sl@0: #define UTRIE_GET16(trie, c32, result) _UTRIE_GET(trie, index, c32, result, uint16_t) sl@0: sl@0: /** sl@0: * Get a 32-bit trie value from a code point. sl@0: * Even lead surrogate code points are treated as normal code points, sl@0: * with unfolded values that may differ from _FROM_LEAD() macro results for them. sl@0: * sl@0: * @param trie (const UTrie *, in) a pointer to the runtime trie structure sl@0: * @param c32 (UChar32, in) the input code point sl@0: * @param result (uint32_t, out) uint32_t variable for the trie lookup result sl@0: */ sl@0: #define UTRIE_GET32(trie, c32, result) _UTRIE_GET(trie, data32, c32, result, uint32_t) sl@0: sl@0: /** sl@0: * Get the next code point (c, c2), post-increment src, sl@0: * and get a 16-bit value from the trie. sl@0: * sl@0: * @param trie (const UTrie *, in) a pointer to the runtime trie structure sl@0: * @param src (const UChar *, in/out) the source text pointer sl@0: * @param limit (const UChar *, in) the limit pointer for the text, or NULL sl@0: * @param c (UChar, out) variable for the BMP or lead code unit sl@0: * @param c2 (UChar, out) variable for 0 or the trail code unit sl@0: * @param result (uint16_t, out) uint16_t variable for the trie lookup result sl@0: */ sl@0: #define UTRIE_NEXT16(trie, src, limit, c, c2, result) _UTRIE_NEXT(trie, index, src, limit, c, c2, result, uint16_t) sl@0: sl@0: /** sl@0: * Get the next code point (c, c2), post-increment src, sl@0: * and get a 32-bit value from the trie. sl@0: * sl@0: * @param trie (const UTrie *, in) a pointer to the runtime trie structure sl@0: * @param src (const UChar *, in/out) the source text pointer sl@0: * @param limit (const UChar *, in) the limit pointer for the text, or NULL sl@0: * @param c (UChar, out) variable for the BMP or lead code unit sl@0: * @param c2 (UChar, out) variable for 0 or the trail code unit sl@0: * @param result (uint32_t, out) uint32_t variable for the trie lookup result sl@0: */ sl@0: #define UTRIE_NEXT32(trie, src, limit, c, c2, result) _UTRIE_NEXT(trie, data32, src, limit, c, c2, result, uint32_t) sl@0: sl@0: /** sl@0: * Get the previous code point (c, c2), pre-decrement src, sl@0: * and get a 16-bit value from the trie. sl@0: * sl@0: * @param trie (const UTrie *, in) a pointer to the runtime trie structure sl@0: * @param start (const UChar *, in) the start pointer for the text, or NULL sl@0: * @param src (const UChar *, in/out) the source text pointer sl@0: * @param c (UChar, out) variable for the BMP or lead code unit sl@0: * @param c2 (UChar, out) variable for 0 or the trail code unit sl@0: * @param result (uint16_t, out) uint16_t variable for the trie lookup result sl@0: */ sl@0: #define UTRIE_PREVIOUS16(trie, start, src, c, c2, result) _UTRIE_PREVIOUS(trie, index, start, src, c, c2, result, uint16_t) sl@0: sl@0: /** sl@0: * Get the previous code point (c, c2), pre-decrement src, sl@0: * and get a 32-bit value from the trie. sl@0: * sl@0: * @param trie (const UTrie *, in) a pointer to the runtime trie structure sl@0: * @param start (const UChar *, in) the start pointer for the text, or NULL sl@0: * @param src (const UChar *, in/out) the source text pointer sl@0: * @param c (UChar, out) variable for the BMP or lead code unit sl@0: * @param c2 (UChar, out) variable for 0 or the trail code unit sl@0: * @param result (uint32_t, out) uint32_t variable for the trie lookup result sl@0: */ sl@0: #define UTRIE_PREVIOUS32(trie, start, src, c, c2, result) _UTRIE_PREVIOUS(trie, data32, start, src, c, c2, result, uint32_t) sl@0: sl@0: /** sl@0: * Get a 16-bit trie value from a pair of surrogates. sl@0: * sl@0: * @param trie (const UTrie *, in) a pointer to the runtime trie structure sl@0: * @param c (UChar, in) a lead surrogate sl@0: * @param c2 (UChar, in) a trail surrogate sl@0: * @param result (uint16_t, out) uint16_t variable for the trie lookup result sl@0: */ sl@0: #define UTRIE_GET16_FROM_PAIR(trie, c, c2, result) _UTRIE_GET_FROM_PAIR(trie, index, c, c2, result, uint16_t) sl@0: sl@0: /** sl@0: * Get a 32-bit trie value from a pair of surrogates. sl@0: * sl@0: * @param trie (const UTrie *, in) a pointer to the runtime trie structure sl@0: * @param c (UChar, in) a lead surrogate sl@0: * @param c2 (UChar, in) a trail surrogate sl@0: * @param result (uint32_t, out) uint32_t variable for the trie lookup result sl@0: */ sl@0: #define UTRIE_GET32_FROM_PAIR(trie, c, c2, result) _UTRIE_GET_FROM_PAIR(trie, data32, c, c2, result, uint32_t) sl@0: sl@0: /** sl@0: * Get a 16-bit trie value from a folding offset (from the value of a lead surrogate) sl@0: * and a trail surrogate. sl@0: * sl@0: * @param trie (const UTrie *, in) a pointer to the runtime trie structure sl@0: * @param offset (int32_t, in) the folding offset from the value of a lead surrogate sl@0: * @param c2 (UChar, in) a trail surrogate (only the 10 low bits are significant) sl@0: * @return (uint16_t) trie lookup result sl@0: */ sl@0: #define UTRIE_GET16_FROM_OFFSET_TRAIL(trie, offset, c2) _UTRIE_GET_RAW(trie, index, offset, (c2)&0x3ff) sl@0: sl@0: /** sl@0: * Get a 32-bit trie value from a folding offset (from the value of a lead surrogate) sl@0: * and a trail surrogate. sl@0: * sl@0: * @param trie (const UTrie *, in) a pointer to the runtime trie structure sl@0: * @param offset (int32_t, in) the folding offset from the value of a lead surrogate sl@0: * @param c2 (UChar, in) a trail surrogate (only the 10 low bits are significant) sl@0: * @return (uint32_t) trie lookup result sl@0: */ sl@0: #define UTRIE_GET32_FROM_OFFSET_TRAIL(trie, offset, c2) _UTRIE_GET_RAW(trie, data32, offset, (c2)&0x3ff) sl@0: sl@0: /* enumeration callback types */ sl@0: sl@0: /** sl@0: * Callback from utrie_enum(), extracts a uint32_t value from a sl@0: * trie value. This value will be passed on to the UTrieEnumRange function. sl@0: * sl@0: * @param context an opaque pointer, as passed into utrie_enum() sl@0: * @param value a value from the trie sl@0: * @return the value that is to be passed on to the UTrieEnumRange function sl@0: */ sl@0: typedef uint32_t U_CALLCONV sl@0: UTrieEnumValue(const void *context, uint32_t value); sl@0: sl@0: /** sl@0: * Callback from utrie_enum(), is called for each contiguous range sl@0: * of code points with the same value as retrieved from the trie and sl@0: * transformed by the UTrieEnumValue function. sl@0: * sl@0: * The callback function can stop the enumeration by returning FALSE. sl@0: * sl@0: * @param context an opaque pointer, as passed into utrie_enum() sl@0: * @param start the first code point in a contiguous range with value sl@0: * @param limit one past the last code point in a contiguous range with value sl@0: * @param value the value that is set for all code points in [start..limit[ sl@0: * @return FALSE to stop the enumeration sl@0: */ sl@0: typedef UBool U_CALLCONV sl@0: UTrieEnumRange(const void *context, UChar32 start, UChar32 limit, uint32_t value); sl@0: sl@0: /** sl@0: * Enumerate efficiently all values in a trie. sl@0: * For each entry in the trie, the value to be delivered is passed through sl@0: * the UTrieEnumValue function. sl@0: * The value is unchanged if that function pointer is NULL. sl@0: * sl@0: * For each contiguous range of code points with a given value, sl@0: * the UTrieEnumRange function is called. sl@0: * sl@0: * @param trie a pointer to the runtime trie structure sl@0: * @param enumValue a pointer to a function that may transform the trie entry value, sl@0: * or NULL if the values from the trie are to be used directly sl@0: * @param enumRange a pointer to a function that is called for each contiguous range sl@0: * of code points with the same value sl@0: * @param context an opaque pointer that is passed on to the callback functions sl@0: */ sl@0: U_CAPI void U_EXPORT2 sl@0: utrie_enum(const UTrie *trie, sl@0: UTrieEnumValue *enumValue, UTrieEnumRange *enumRange, const void *context); sl@0: sl@0: /** sl@0: * Unserialize a trie from 32-bit-aligned memory. sl@0: * Inverse of utrie_serialize(). sl@0: * Fills the UTrie runtime trie structure with the settings for the trie data. sl@0: * sl@0: * @param trie a pointer to the runtime trie structure sl@0: * @param data a pointer to 32-bit-aligned memory containing trie data sl@0: * @param length the number of bytes available at data sl@0: * @param pErrorCode an in/out ICU UErrorCode sl@0: * @return the number of bytes at data taken up by the trie data sl@0: */ sl@0: U_CAPI int32_t U_EXPORT2 sl@0: utrie_unserialize(UTrie *trie, const void *data, int32_t length, UErrorCode *pErrorCode); sl@0: sl@0: /** sl@0: * "Unserialize" a dummy trie. sl@0: * A dummy trie is an empty runtime trie, used when a real data trie cannot sl@0: * be loaded. sl@0: * sl@0: * The input memory is filled so that the trie always returns the initialValue, sl@0: * or the leadUnitValue for lead surrogate code points. sl@0: * The Latin-1 part is always set up to be linear. sl@0: * sl@0: * @param trie a pointer to the runtime trie structure sl@0: * @param data a pointer to 32-bit-aligned memory to be filled with the dummy trie data sl@0: * @param length the number of bytes available at data (recommended to use UTRIE_DUMMY_SIZE) sl@0: * @param initialValue the initial value that is set for all code points sl@0: * @param leadUnitValue the value for lead surrogate code _units_ that do not sl@0: * have associated supplementary data sl@0: * @param pErrorCode an in/out ICU UErrorCode sl@0: * sl@0: * @see UTRIE_DUMMY_SIZE sl@0: * @see utrie_open sl@0: */ sl@0: U_CAPI int32_t U_EXPORT2 sl@0: utrie_unserializeDummy(UTrie *trie, sl@0: void *data, int32_t length, sl@0: uint32_t initialValue, uint32_t leadUnitValue, sl@0: UBool make16BitTrie, sl@0: UErrorCode *pErrorCode); sl@0: sl@0: /** sl@0: * Default implementation for UTrie.getFoldingOffset, set automatically by sl@0: * utrie_unserialize(). sl@0: * Simply returns the lead surrogate's value itself - which is the inverse sl@0: * of the default folding function used by utrie_serialize(). sl@0: * Exported for static const UTrie structures. sl@0: * sl@0: * @see UTrieGetFoldingOffset sl@0: */ sl@0: U_CAPI int32_t U_EXPORT2 sl@0: utrie_defaultGetFoldingOffset(uint32_t data); sl@0: sl@0: /* Building a trie ----------------------------------------------------------*/ sl@0: sl@0: /** sl@0: * Build-time trie structure. sl@0: * Opaque definition, here only to make fillIn parameters possible sl@0: * for utrie_open() and utrie_clone(). sl@0: */ sl@0: struct UNewTrie { sl@0: /** sl@0: * Index values at build-time are 32 bits wide for easier processing. sl@0: * Bit 31 is set if the data block is used by multiple index values (from utrie_setRange()). sl@0: */ sl@0: int32_t index[UTRIE_MAX_INDEX_LENGTH]; sl@0: uint32_t *data; sl@0: sl@0: uint32_t leadUnitValue; sl@0: int32_t indexLength, dataCapacity, dataLength; sl@0: UBool isAllocated, isDataAllocated; sl@0: UBool isLatin1Linear, isCompacted; sl@0: sl@0: /** sl@0: * Map of adjusted indexes, used in utrie_compact(). sl@0: * Maps from original indexes to new ones. sl@0: */ sl@0: int32_t map[UTRIE_MAX_BUILD_TIME_DATA_LENGTH>>UTRIE_SHIFT]; sl@0: }; sl@0: sl@0: typedef struct UNewTrie UNewTrie; sl@0: sl@0: /** sl@0: * Build-time trie callback function, used with utrie_serialize(). sl@0: * This function calculates a lead surrogate's value including a folding offset sl@0: * from the 1024 supplementary code points [start..start+1024[ . sl@0: * It is U+10000 <= start <= U+10fc00 and (start&0x3ff)==0. sl@0: * sl@0: * The folding offset is provided by the caller. sl@0: * It is offset=UTRIE_BMP_INDEX_LENGTH+n*UTRIE_SURROGATE_BLOCK_COUNT with n=0..1023. sl@0: * Instead of the offset itself, n can be stored in 10 bits - sl@0: * or fewer if it can be assumed that few lead surrogates have associated data. sl@0: * sl@0: * The returned value must be sl@0: * - not zero if and only if there is relevant data sl@0: * for the corresponding 1024 supplementary code points sl@0: * - such that UTrie.getFoldingOffset(UNewTrieGetFoldedValue(..., offset))==offset sl@0: * sl@0: * @return a folded value, or 0 if there is no relevant data for the lead surrogate. sl@0: */ sl@0: typedef uint32_t U_CALLCONV sl@0: UNewTrieGetFoldedValue(UNewTrie *trie, UChar32 start, int32_t offset); sl@0: sl@0: /** sl@0: * Open a build-time trie structure. sl@0: * The size of the build-time data array is specified to avoid allocating a large sl@0: * array in all cases. The array itself can also be passed in. sl@0: * sl@0: * Although the trie is never fully expanded to a linear array, especially when sl@0: * utrie_setRange32() is used, the data array could be large during build time. sl@0: * The maximum length is sl@0: * UTRIE_MAX_BUILD_TIME_DATA_LENGTH=0x110000+UTRIE_DATA_BLOCK_LENGTH+0x400. sl@0: * (Number of Unicode code points + one all-initial-value block + sl@0: * possible duplicate entries for 1024 lead surrogates.) sl@0: * (UTRIE_DATA_BLOCK_LENGTH<=0x200 in all cases.) sl@0: * sl@0: * @param fillIn a pointer to a UNewTrie structure to be initialized (will not be released), or sl@0: * NULL if one is to be allocated sl@0: * @param aliasData a pointer to a data array to be used (will not be released), or sl@0: * NULL if one is to be allocated sl@0: * @param maxDataLength the capacity of aliasData (if not NULL) or sl@0: * the length of the data array to be allocated sl@0: * @param initialValue the initial value that is set for all code points sl@0: * @param leadUnitValue the value for lead surrogate code _units_ that do not sl@0: * have associated supplementary data sl@0: * @param latin1Linear a flag indicating whether the Latin-1 range is to be allocated and sl@0: * kept in a linear, contiguous part of the data array sl@0: * @return a pointer to the initialized fillIn or the allocated and initialized new UNewTrie sl@0: */ sl@0: U_CAPI UNewTrie * U_EXPORT2 sl@0: utrie_open(UNewTrie *fillIn, sl@0: uint32_t *aliasData, int32_t maxDataLength, sl@0: uint32_t initialValue, uint32_t leadUnitValue, sl@0: UBool latin1Linear); sl@0: sl@0: /** sl@0: * Clone a build-time trie structure with all entries. sl@0: * sl@0: * @param fillIn like in utrie_open() sl@0: * @param other the build-time trie structure to clone sl@0: * @param aliasData like in utrie_open(), sl@0: * used if aliasDataLength>=(capacity of other's data array) sl@0: * @param aliasDataLength the length of aliasData sl@0: * @return a pointer to the initialized fillIn or the allocated and initialized new UNewTrie sl@0: */ sl@0: U_CAPI UNewTrie * U_EXPORT2 sl@0: utrie_clone(UNewTrie *fillIn, const UNewTrie *other, uint32_t *aliasData, int32_t aliasDataLength); sl@0: sl@0: /** sl@0: * Close a build-time trie structure, and release memory sl@0: * that was allocated by utrie_open() or utrie_clone(). sl@0: * sl@0: * @param trie the build-time trie sl@0: */ sl@0: U_CAPI void U_EXPORT2 sl@0: utrie_close(UNewTrie *trie); sl@0: sl@0: /** sl@0: * Get the data array of a build-time trie. sl@0: * The data may be modified, but entries that are equal before sl@0: * must still be equal after modification. sl@0: * sl@0: * @param trie the build-time trie sl@0: * @param pLength (out) a pointer to a variable that receives the number sl@0: * of entries in the data array sl@0: * @return the data array sl@0: */ sl@0: U_CAPI uint32_t * U_EXPORT2 sl@0: utrie_getData(UNewTrie *trie, int32_t *pLength); sl@0: sl@0: /** sl@0: * Set a value for a code point. sl@0: * sl@0: * @param trie the build-time trie sl@0: * @param c the code point sl@0: * @param value the value sl@0: * @return FALSE if a failure occurred (illegal argument or data array overrun) sl@0: */ sl@0: U_CAPI UBool U_EXPORT2 sl@0: utrie_set32(UNewTrie *trie, UChar32 c, uint32_t value); sl@0: sl@0: /** sl@0: * Get a value from a code point as stored in the build-time trie. sl@0: * sl@0: * @param trie the build-time trie sl@0: * @param c the code point sl@0: * @param pInBlockZero if not NULL, then *pInBlockZero is set to TRUE sl@0: * iff the value is retrieved from block 0; sl@0: * block 0 is the all-initial-value initial block sl@0: * @return the value sl@0: */ sl@0: U_CAPI uint32_t U_EXPORT2 sl@0: utrie_get32(UNewTrie *trie, UChar32 c, UBool *pInBlockZero); sl@0: sl@0: /** sl@0: * Set a value in a range of code points [start..limit[. sl@0: * All code points c with start<=c