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