2 ******************************************************************************
4 * Copyright (C) 2001-2006, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
10 * tab size: 8 (not used)
13 * created on: 2001nov08
14 * created by: Markus W. Scherer
20 #include "unicode/utypes.h"
28 * This is a common implementation of a "folded" trie.
29 * It is a kind of compressed, serializable table of 16- or 32-bit values associated with
30 * Unicode code points (0..0x10ffff).
32 * This implementation is optimized for getting values while walking forward
33 * through a UTF-16 string.
34 * Therefore, the simplest and fastest access macros are the
35 * _FROM_LEAD() and _FROM_OFFSET_TRAIL() macros.
37 * The _FROM_BMP() macros are a little more complicated; they get values
38 * even for lead surrogate code _points_, while the _FROM_LEAD() macros
39 * get special "folded" values for lead surrogate code _units_ if
40 * there is relevant data associated with them.
41 * From such a folded value, an offset needs to be extracted to supply
42 * to the _FROM_OFFSET_TRAIL() macros.
44 * Most of the more complex (and more convenient) functions/macros call a callback function
45 * to get that offset from the folded value for a lead surrogate unit.
49 * Trie constants, defining shift widths, index array lengths, etc.
52 /** Shift size for shifting right the input index. 1..9 */
55 /** Number of data values in a stage 2 (data array) block. 2, 4, 8, .., 0x200 */
56 UTRIE_DATA_BLOCK_LENGTH
=1<<UTRIE_SHIFT
,
58 /** Mask for getting the lower bits from the input index. */
59 UTRIE_MASK
=UTRIE_DATA_BLOCK_LENGTH
-1,
62 * Lead surrogate code points' index displacement in the index array.
63 * 0x10000-0xd800=0x2800
65 UTRIE_LEAD_INDEX_DISP
=0x2800>>UTRIE_SHIFT
,
68 * Shift size for shifting left the index array values.
69 * Increases possible data size with 16-bit index values at the cost
71 * This requires blocks of stage 2 data to be aligned by UTRIE_DATA_GRANULARITY.
76 /** The alignment size of a stage 2 data block. Also the granularity for compaction. */
77 UTRIE_DATA_GRANULARITY
=1<<UTRIE_INDEX_SHIFT
,
79 /** Number of bits of a trail surrogate that are used in index table lookups. */
80 UTRIE_SURROGATE_BLOCK_BITS
=10-UTRIE_SHIFT
,
83 * Number of index (stage 1) entries per lead surrogate.
84 * Same as number of index entries for 1024 trail surrogates,
85 * ==0x400>>UTRIE_SHIFT
87 UTRIE_SURROGATE_BLOCK_COUNT
=(1<<UTRIE_SURROGATE_BLOCK_BITS
),
89 /** Length of the BMP portion of the index (stage 1) array. */
90 UTRIE_BMP_INDEX_LENGTH
=0x10000>>UTRIE_SHIFT
94 * Length of the index (stage 1) array before folding.
95 * Maximum number of Unicode code points (0x110000) shifted right by UTRIE_SHIFT.
97 #define UTRIE_MAX_INDEX_LENGTH (0x110000>>UTRIE_SHIFT)
100 * Maximum length of the runtime data (stage 2) array.
101 * Limited by 16-bit index values that are left-shifted by UTRIE_INDEX_SHIFT.
103 #define UTRIE_MAX_DATA_LENGTH (0x10000<<UTRIE_INDEX_SHIFT)
106 * Maximum length of the build-time data (stage 2) array.
107 * The maximum length is 0x110000+UTRIE_DATA_BLOCK_LENGTH+0x400.
108 * (Number of Unicode code points + one all-initial-value block +
109 * possible duplicate entries for 1024 lead surrogates.)
111 #define UTRIE_MAX_BUILD_TIME_DATA_LENGTH (0x110000+UTRIE_DATA_BLOCK_LENGTH+0x400)
114 * Number of bytes for a dummy trie.
115 * A dummy trie is an empty runtime trie, used when a real data trie cannot
117 * The number of bytes works for Latin-1-linear tries with 32-bit data
121 * BMP index + 1 index block for lead surrogate code points +
122 * Latin-1-linear array + 1 data block for lead surrogate code points
124 * Latin-1: if(UTRIE_SHIFT<=8) { 256 } else { included in first data block }
126 * @see utrie_unserializeDummy
128 #define UTRIE_DUMMY_SIZE ((UTRIE_BMP_INDEX_LENGTH+UTRIE_SURROGATE_BLOCK_COUNT)*2+(UTRIE_SHIFT<=8?256:UTRIE_DATA_BLOCK_LENGTH)*4+UTRIE_DATA_BLOCK_LENGTH*4)
131 * Runtime UTrie callback function.
132 * Extract from a lead surrogate's data the
133 * index array offset of the indexes for that lead surrogate.
135 * @param data data value for a surrogate from the trie, including the folding offset
136 * @return offset>=UTRIE_BMP_INDEX_LENGTH, or 0 if there is no data for the lead surrogate
138 typedef int32_t U_CALLCONV
139 UTrieGetFoldingOffset(uint32_t data
);
142 * Run-time Trie structure.
144 * Either the data table is 16 bits wide and accessed via the index
145 * pointer, with each index item increased by indexLength;
146 * in this case, data32==NULL.
148 * Or the data table is 32 bits wide and accessed via the data32 pointer.
151 const uint16_t *index
;
152 const uint32_t *data32
; /* NULL if 16b data is used via index */
155 * This function is not used in _FROM_LEAD, _FROM_BMP, and _FROM_OFFSET_TRAIL macros.
156 * If convenience macros like _GET16 or _NEXT32 are used, this function must be set.
158 * utrie_unserialize() sets a default function which simply returns
159 * the lead surrogate's value itself - which is the inverse of the default
160 * folding function used by utrie_serialize().
162 * @see UTrieGetFoldingOffset
164 UTrieGetFoldingOffset
*getFoldingOffset
;
166 int32_t indexLength
, dataLength
;
167 uint32_t initialValue
;
168 UBool isLatin1Linear
;
171 typedef struct UTrie UTrie
;
173 /** Internal trie getter from an offset (0 if c16 is a BMP/lead units) and a 16-bit unit */
174 #define _UTRIE_GET_RAW(trie, data, offset, c16) \
176 ((int32_t)((trie)->index[(offset)+((c16)>>UTRIE_SHIFT)])<<UTRIE_INDEX_SHIFT)+ \
180 /** Internal trie getter from a pair of surrogates */
181 #define _UTRIE_GET_FROM_PAIR(trie, data, c, c2, result, resultType) { \
184 /* get data for lead surrogate */ \
185 (result)=_UTRIE_GET_RAW((trie), data, 0, (c)); \
186 __offset=(trie)->getFoldingOffset(result); \
188 /* get the real data from the folded lead/trail units */ \
190 (result)=_UTRIE_GET_RAW((trie), data, __offset, (c2)&0x3ff); \
192 (result)=(resultType)((trie)->initialValue); \
196 /** Internal trie getter from a BMP code point, treating a lead surrogate as a normal code point */
197 #define _UTRIE_GET_FROM_BMP(trie, data, c16) \
198 _UTRIE_GET_RAW(trie, data, 0xd800<=(c16) && (c16)<=0xdbff ? UTRIE_LEAD_INDEX_DISP : 0, c16);
201 * Internal trie getter from a code point.
202 * Could be faster(?) but longer with
203 * if((c32)<=0xd7ff) { (result)=_UTRIE_GET_RAW(trie, data, 0, c32); }
205 #define _UTRIE_GET(trie, data, c32, result, resultType) \
206 if((uint32_t)(c32)<=0xffff) { \
207 /* BMP code points */ \
208 (result)=_UTRIE_GET_FROM_BMP(trie, data, c32); \
209 } else if((uint32_t)(c32)<=0x10ffff) { \
210 /* supplementary code point */ \
211 UChar __lead16=UTF16_LEAD(c32); \
212 _UTRIE_GET_FROM_PAIR(trie, data, __lead16, c32, result, resultType); \
215 (result)=(resultType)((trie)->initialValue); \
218 /** Internal next-post-increment: get the next code point (c, c2) and its data */
219 #define _UTRIE_NEXT(trie, data, src, limit, c, c2, result, resultType) { \
221 if(!UTF_IS_LEAD(c)) { \
223 (result)=_UTRIE_GET_RAW((trie), data, 0, (c)); \
224 } else if((src)!=(limit) && UTF_IS_TRAIL((c2)=*(src))) { \
226 _UTRIE_GET_FROM_PAIR((trie), data, (c), (c2), (result), resultType); \
228 /* unpaired lead surrogate code point */ \
230 (result)=_UTRIE_GET_RAW((trie), data, UTRIE_LEAD_INDEX_DISP, (c)); \
234 /** Internal previous: get the previous code point (c, c2) and its data */
235 #define _UTRIE_PREVIOUS(trie, data, start, src, c, c2, result, resultType) { \
237 if(!UTF_IS_SURROGATE(c)) { \
239 (result)=_UTRIE_GET_RAW((trie), data, 0, (c)); \
240 } else if(!UTF_IS_SURROGATE_FIRST(c)) { \
241 /* trail surrogate */ \
242 if((start)!=(src) && UTF_IS_LEAD((c2)=*((src)-1))) { \
244 (result)=(c); (c)=(c2); (c2)=(UChar)(result); /* swap c, c2 */ \
245 _UTRIE_GET_FROM_PAIR((trie), data, (c), (c2), (result), resultType); \
247 /* unpaired trail surrogate code point */ \
249 (result)=_UTRIE_GET_RAW((trie), data, 0, (c)); \
252 /* unpaired lead surrogate code point */ \
254 (result)=_UTRIE_GET_RAW((trie), data, UTRIE_LEAD_INDEX_DISP, (c)); \
258 /* Public UTrie API ---------------------------------------------------------*/
261 * Get a pointer to the contiguous part of the data array
262 * for the Latin-1 range (U+0000..U+00ff).
263 * Must be used only if the Latin-1 range is in fact linear
264 * (trie->isLatin1Linear).
266 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
267 * @return (const uint16_t *) pointer to values for Latin-1 code points
269 #define UTRIE_GET16_LATIN1(trie) ((trie)->index+(trie)->indexLength+UTRIE_DATA_BLOCK_LENGTH)
272 * Get a pointer to the contiguous part of the data array
273 * for the Latin-1 range (U+0000..U+00ff).
274 * Must be used only if the Latin-1 range is in fact linear
275 * (trie->isLatin1Linear).
277 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
278 * @return (const uint32_t *) pointer to values for Latin-1 code points
280 #define UTRIE_GET32_LATIN1(trie) ((trie)->data32+UTRIE_DATA_BLOCK_LENGTH)
283 * Get a 16-bit trie value from a BMP code point (UChar, <=U+ffff).
284 * c16 may be a lead surrogate, which may have a value including a folding offset.
286 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
287 * @param c16 (UChar, in) the input BMP code point
288 * @return (uint16_t) trie lookup result
290 #define UTRIE_GET16_FROM_LEAD(trie, c16) _UTRIE_GET_RAW(trie, index, 0, c16)
293 * Get a 32-bit trie value from a BMP code point (UChar, <=U+ffff).
294 * c16 may be a lead surrogate, which may have a value including a folding offset.
296 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
297 * @param c16 (UChar, in) the input BMP code point
298 * @return (uint32_t) trie lookup result
300 #define UTRIE_GET32_FROM_LEAD(trie, c16) _UTRIE_GET_RAW(trie, data32, 0, c16)
303 * Get a 16-bit trie value from a BMP code point (UChar, <=U+ffff).
304 * Even lead surrogate code points are treated as normal code points,
305 * with unfolded values that may differ from _FROM_LEAD() macro results for them.
307 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
308 * @param c16 (UChar, in) the input BMP code point
309 * @return (uint16_t) trie lookup result
311 #define UTRIE_GET16_FROM_BMP(trie, c16) _UTRIE_GET_FROM_BMP(trie, index, c16)
314 * Get a 32-bit trie value from a BMP code point (UChar, <=U+ffff).
315 * Even lead surrogate code points are treated as normal code points,
316 * with unfolded values that may differ from _FROM_LEAD() macro results for them.
318 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
319 * @param c16 (UChar, in) the input BMP code point
320 * @return (uint32_t) trie lookup result
322 #define UTRIE_GET32_FROM_BMP(trie, c16) _UTRIE_GET_FROM_BMP(trie, data32, c16)
325 * Get a 16-bit trie value from a code point.
326 * Even lead surrogate code points are treated as normal code points,
327 * with unfolded values that may differ from _FROM_LEAD() macro results for them.
329 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
330 * @param c32 (UChar32, in) the input code point
331 * @param result (uint16_t, out) uint16_t variable for the trie lookup result
333 #define UTRIE_GET16(trie, c32, result) _UTRIE_GET(trie, index, c32, result, uint16_t)
336 * Get a 32-bit trie value from a code point.
337 * Even lead surrogate code points are treated as normal code points,
338 * with unfolded values that may differ from _FROM_LEAD() macro results for them.
340 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
341 * @param c32 (UChar32, in) the input code point
342 * @param result (uint32_t, out) uint32_t variable for the trie lookup result
344 #define UTRIE_GET32(trie, c32, result) _UTRIE_GET(trie, data32, c32, result, uint32_t)
347 * Get the next code point (c, c2), post-increment src,
348 * and get a 16-bit value from the trie.
350 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
351 * @param src (const UChar *, in/out) the source text pointer
352 * @param limit (const UChar *, in) the limit pointer for the text, or NULL
353 * @param c (UChar, out) variable for the BMP or lead code unit
354 * @param c2 (UChar, out) variable for 0 or the trail code unit
355 * @param result (uint16_t, out) uint16_t variable for the trie lookup result
357 #define UTRIE_NEXT16(trie, src, limit, c, c2, result) _UTRIE_NEXT(trie, index, src, limit, c, c2, result, uint16_t)
360 * Get the next code point (c, c2), post-increment src,
361 * and get a 32-bit value from the trie.
363 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
364 * @param src (const UChar *, in/out) the source text pointer
365 * @param limit (const UChar *, in) the limit pointer for the text, or NULL
366 * @param c (UChar, out) variable for the BMP or lead code unit
367 * @param c2 (UChar, out) variable for 0 or the trail code unit
368 * @param result (uint32_t, out) uint32_t variable for the trie lookup result
370 #define UTRIE_NEXT32(trie, src, limit, c, c2, result) _UTRIE_NEXT(trie, data32, src, limit, c, c2, result, uint32_t)
373 * Get the previous code point (c, c2), pre-decrement src,
374 * and get a 16-bit value from the trie.
376 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
377 * @param start (const UChar *, in) the start pointer for the text, or NULL
378 * @param src (const UChar *, in/out) the source text pointer
379 * @param c (UChar, out) variable for the BMP or lead code unit
380 * @param c2 (UChar, out) variable for 0 or the trail code unit
381 * @param result (uint16_t, out) uint16_t variable for the trie lookup result
383 #define UTRIE_PREVIOUS16(trie, start, src, c, c2, result) _UTRIE_PREVIOUS(trie, index, start, src, c, c2, result, uint16_t)
386 * Get the previous code point (c, c2), pre-decrement src,
387 * and get a 32-bit value from the trie.
389 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
390 * @param start (const UChar *, in) the start pointer for the text, or NULL
391 * @param src (const UChar *, in/out) the source text pointer
392 * @param c (UChar, out) variable for the BMP or lead code unit
393 * @param c2 (UChar, out) variable for 0 or the trail code unit
394 * @param result (uint32_t, out) uint32_t variable for the trie lookup result
396 #define UTRIE_PREVIOUS32(trie, start, src, c, c2, result) _UTRIE_PREVIOUS(trie, data32, start, src, c, c2, result, uint32_t)
399 * Get a 16-bit trie value from a pair of surrogates.
401 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
402 * @param c (UChar, in) a lead surrogate
403 * @param c2 (UChar, in) a trail surrogate
404 * @param result (uint16_t, out) uint16_t variable for the trie lookup result
406 #define UTRIE_GET16_FROM_PAIR(trie, c, c2, result) _UTRIE_GET_FROM_PAIR(trie, index, c, c2, result, uint16_t)
409 * Get a 32-bit trie value from a pair of surrogates.
411 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
412 * @param c (UChar, in) a lead surrogate
413 * @param c2 (UChar, in) a trail surrogate
414 * @param result (uint32_t, out) uint32_t variable for the trie lookup result
416 #define UTRIE_GET32_FROM_PAIR(trie, c, c2, result) _UTRIE_GET_FROM_PAIR(trie, data32, c, c2, result, uint32_t)
419 * Get a 16-bit trie value from a folding offset (from the value of a lead surrogate)
420 * and a trail surrogate.
422 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
423 * @param offset (int32_t, in) the folding offset from the value of a lead surrogate
424 * @param c2 (UChar, in) a trail surrogate (only the 10 low bits are significant)
425 * @return (uint16_t) trie lookup result
427 #define UTRIE_GET16_FROM_OFFSET_TRAIL(trie, offset, c2) _UTRIE_GET_RAW(trie, index, offset, (c2)&0x3ff)
430 * Get a 32-bit trie value from a folding offset (from the value of a lead surrogate)
431 * and a trail surrogate.
433 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
434 * @param offset (int32_t, in) the folding offset from the value of a lead surrogate
435 * @param c2 (UChar, in) a trail surrogate (only the 10 low bits are significant)
436 * @return (uint32_t) trie lookup result
438 #define UTRIE_GET32_FROM_OFFSET_TRAIL(trie, offset, c2) _UTRIE_GET_RAW(trie, data32, offset, (c2)&0x3ff)
440 /* enumeration callback types */
443 * Callback from utrie_enum(), extracts a uint32_t value from a
444 * trie value. This value will be passed on to the UTrieEnumRange function.
446 * @param context an opaque pointer, as passed into utrie_enum()
447 * @param value a value from the trie
448 * @return the value that is to be passed on to the UTrieEnumRange function
450 typedef uint32_t U_CALLCONV
451 UTrieEnumValue(const void *context
, uint32_t value
);
454 * Callback from utrie_enum(), is called for each contiguous range
455 * of code points with the same value as retrieved from the trie and
456 * transformed by the UTrieEnumValue function.
458 * The callback function can stop the enumeration by returning FALSE.
460 * @param context an opaque pointer, as passed into utrie_enum()
461 * @param start the first code point in a contiguous range with value
462 * @param limit one past the last code point in a contiguous range with value
463 * @param value the value that is set for all code points in [start..limit[
464 * @return FALSE to stop the enumeration
466 typedef UBool U_CALLCONV
467 UTrieEnumRange(const void *context
, UChar32 start
, UChar32 limit
, uint32_t value
);
470 * Enumerate efficiently all values in a trie.
471 * For each entry in the trie, the value to be delivered is passed through
472 * the UTrieEnumValue function.
473 * The value is unchanged if that function pointer is NULL.
475 * For each contiguous range of code points with a given value,
476 * the UTrieEnumRange function is called.
478 * @param trie a pointer to the runtime trie structure
479 * @param enumValue a pointer to a function that may transform the trie entry value,
480 * or NULL if the values from the trie are to be used directly
481 * @param enumRange a pointer to a function that is called for each contiguous range
482 * of code points with the same value
483 * @param context an opaque pointer that is passed on to the callback functions
485 U_CAPI
void U_EXPORT2
486 utrie_enum(const UTrie
*trie
,
487 UTrieEnumValue
*enumValue
, UTrieEnumRange
*enumRange
, const void *context
);
490 * Unserialize a trie from 32-bit-aligned memory.
491 * Inverse of utrie_serialize().
492 * Fills the UTrie runtime trie structure with the settings for the trie data.
494 * @param trie a pointer to the runtime trie structure
495 * @param data a pointer to 32-bit-aligned memory containing trie data
496 * @param length the number of bytes available at data
497 * @param pErrorCode an in/out ICU UErrorCode
498 * @return the number of bytes at data taken up by the trie data
500 U_CAPI
int32_t U_EXPORT2
501 utrie_unserialize(UTrie
*trie
, const void *data
, int32_t length
, UErrorCode
*pErrorCode
);
504 * "Unserialize" a dummy trie.
505 * A dummy trie is an empty runtime trie, used when a real data trie cannot
508 * The input memory is filled so that the trie always returns the initialValue,
509 * or the leadUnitValue for lead surrogate code points.
510 * The Latin-1 part is always set up to be linear.
512 * @param trie a pointer to the runtime trie structure
513 * @param data a pointer to 32-bit-aligned memory to be filled with the dummy trie data
514 * @param length the number of bytes available at data (recommended to use UTRIE_DUMMY_SIZE)
515 * @param initialValue the initial value that is set for all code points
516 * @param leadUnitValue the value for lead surrogate code _units_ that do not
517 * have associated supplementary data
518 * @param pErrorCode an in/out ICU UErrorCode
520 * @see UTRIE_DUMMY_SIZE
523 U_CAPI
int32_t U_EXPORT2
524 utrie_unserializeDummy(UTrie
*trie
,
525 void *data
, int32_t length
,
526 uint32_t initialValue
, uint32_t leadUnitValue
,
528 UErrorCode
*pErrorCode
);
531 * Default implementation for UTrie.getFoldingOffset, set automatically by
532 * utrie_unserialize().
533 * Simply returns the lead surrogate's value itself - which is the inverse
534 * of the default folding function used by utrie_serialize().
535 * Exported for static const UTrie structures.
537 * @see UTrieGetFoldingOffset
539 U_CAPI
int32_t U_EXPORT2
540 utrie_defaultGetFoldingOffset(uint32_t data
);
542 /* Building a trie ----------------------------------------------------------*/
545 * Build-time trie structure.
546 * Opaque definition, here only to make fillIn parameters possible
547 * for utrie_open() and utrie_clone().
551 * Index values at build-time are 32 bits wide for easier processing.
552 * Bit 31 is set if the data block is used by multiple index values (from utrie_setRange()).
554 int32_t index
[UTRIE_MAX_INDEX_LENGTH
];
557 uint32_t leadUnitValue
;
558 int32_t indexLength
, dataCapacity
, dataLength
;
559 UBool isAllocated
, isDataAllocated
;
560 UBool isLatin1Linear
, isCompacted
;
563 * Map of adjusted indexes, used in utrie_compact().
564 * Maps from original indexes to new ones.
566 int32_t map
[UTRIE_MAX_BUILD_TIME_DATA_LENGTH
>>UTRIE_SHIFT
];
569 typedef struct UNewTrie UNewTrie
;
572 * Build-time trie callback function, used with utrie_serialize().
573 * This function calculates a lead surrogate's value including a folding offset
574 * from the 1024 supplementary code points [start..start+1024[ .
575 * It is U+10000 <= start <= U+10fc00 and (start&0x3ff)==0.
577 * The folding offset is provided by the caller.
578 * It is offset=UTRIE_BMP_INDEX_LENGTH+n*UTRIE_SURROGATE_BLOCK_COUNT with n=0..1023.
579 * Instead of the offset itself, n can be stored in 10 bits -
580 * or fewer if it can be assumed that few lead surrogates have associated data.
582 * The returned value must be
583 * - not zero if and only if there is relevant data
584 * for the corresponding 1024 supplementary code points
585 * - such that UTrie.getFoldingOffset(UNewTrieGetFoldedValue(..., offset))==offset
587 * @return a folded value, or 0 if there is no relevant data for the lead surrogate.
589 typedef uint32_t U_CALLCONV
590 UNewTrieGetFoldedValue(UNewTrie
*trie
, UChar32 start
, int32_t offset
);
593 * Open a build-time trie structure.
594 * The size of the build-time data array is specified to avoid allocating a large
595 * array in all cases. The array itself can also be passed in.
597 * Although the trie is never fully expanded to a linear array, especially when
598 * utrie_setRange32() is used, the data array could be large during build time.
599 * The maximum length is
600 * UTRIE_MAX_BUILD_TIME_DATA_LENGTH=0x110000+UTRIE_DATA_BLOCK_LENGTH+0x400.
601 * (Number of Unicode code points + one all-initial-value block +
602 * possible duplicate entries for 1024 lead surrogates.)
603 * (UTRIE_DATA_BLOCK_LENGTH<=0x200 in all cases.)
605 * @param fillIn a pointer to a UNewTrie structure to be initialized (will not be released), or
606 * NULL if one is to be allocated
607 * @param aliasData a pointer to a data array to be used (will not be released), or
608 * NULL if one is to be allocated
609 * @param maxDataLength the capacity of aliasData (if not NULL) or
610 * the length of the data array to be allocated
611 * @param initialValue the initial value that is set for all code points
612 * @param leadUnitValue the value for lead surrogate code _units_ that do not
613 * have associated supplementary data
614 * @param latin1Linear a flag indicating whether the Latin-1 range is to be allocated and
615 * kept in a linear, contiguous part of the data array
616 * @return a pointer to the initialized fillIn or the allocated and initialized new UNewTrie
618 U_CAPI UNewTrie
* U_EXPORT2
619 utrie_open(UNewTrie
*fillIn
,
620 uint32_t *aliasData
, int32_t maxDataLength
,
621 uint32_t initialValue
, uint32_t leadUnitValue
,
625 * Clone a build-time trie structure with all entries.
627 * @param fillIn like in utrie_open()
628 * @param other the build-time trie structure to clone
629 * @param aliasData like in utrie_open(),
630 * used if aliasDataLength>=(capacity of other's data array)
631 * @param aliasDataLength the length of aliasData
632 * @return a pointer to the initialized fillIn or the allocated and initialized new UNewTrie
634 U_CAPI UNewTrie
* U_EXPORT2
635 utrie_clone(UNewTrie
*fillIn
, const UNewTrie
*other
, uint32_t *aliasData
, int32_t aliasDataLength
);
638 * Close a build-time trie structure, and release memory
639 * that was allocated by utrie_open() or utrie_clone().
641 * @param trie the build-time trie
643 U_CAPI
void U_EXPORT2
644 utrie_close(UNewTrie
*trie
);
647 * Get the data array of a build-time trie.
648 * The data may be modified, but entries that are equal before
649 * must still be equal after modification.
651 * @param trie the build-time trie
652 * @param pLength (out) a pointer to a variable that receives the number
653 * of entries in the data array
654 * @return the data array
656 U_CAPI
uint32_t * U_EXPORT2
657 utrie_getData(UNewTrie
*trie
, int32_t *pLength
);
660 * Set a value for a code point.
662 * @param trie the build-time trie
663 * @param c the code point
664 * @param value the value
665 * @return FALSE if a failure occurred (illegal argument or data array overrun)
667 U_CAPI UBool U_EXPORT2
668 utrie_set32(UNewTrie
*trie
, UChar32 c
, uint32_t value
);
671 * Get a value from a code point as stored in the build-time trie.
673 * @param trie the build-time trie
674 * @param c the code point
675 * @param pInBlockZero if not NULL, then *pInBlockZero is set to TRUE
676 * iff the value is retrieved from block 0;
677 * block 0 is the all-initial-value initial block
680 U_CAPI
uint32_t U_EXPORT2
681 utrie_get32(UNewTrie
*trie
, UChar32 c
, UBool
*pInBlockZero
);
684 * Set a value in a range of code points [start..limit[.
685 * All code points c with start<=c<limit will get the value if
686 * overwrite is TRUE or if the old value is 0.
688 * @param trie the build-time trie
689 * @param start the first code point to get the value
690 * @param limit one past the last code point to get the value
691 * @param value the value
692 * @param overwrite flag for whether old non-initial values are to be overwritten
693 * @return FALSE if a failure occurred (illegal argument or data array overrun)
695 U_CAPI UBool U_EXPORT2
696 utrie_setRange32(UNewTrie
*trie
, UChar32 start
, UChar32 limit
, uint32_t value
, UBool overwrite
);
699 * Compact the build-time trie after all values are set, and then
700 * serialize it into 32-bit aligned memory.
702 * After this, the trie can only be serizalized again and/or closed;
703 * no further values can be added.
705 * @see utrie_unserialize()
707 * @param trie the build-time trie
708 * @param data a pointer to 32-bit-aligned memory for the trie data
709 * @param capacity the number of bytes available at data
710 * @param getFoldedValue a callback function that calculates the value for
711 * a lead surrogate from all of its supplementary code points
712 * and the folding offset;
713 * if NULL, then a default function is used which returns just
714 * the input offset when there are any non-initial-value entries
715 * @param reduceTo16Bits flag for whether the values are to be reduced to a
716 * width of 16 bits for serialization and runtime
717 * @param pErrorCode a UErrorCode argument; among other possible error codes:
718 * - U_BUFFER_OVERFLOW_ERROR if the data storage block is too small for serialization
719 * - U_MEMORY_ALLOCATION_ERROR if the trie data array is too small
720 * - U_INDEX_OUTOFBOUNDS_ERROR if the index or data arrays are too long after compaction for serialization
722 * @return the number of bytes written for the trie
724 U_CAPI
int32_t U_EXPORT2
725 utrie_serialize(UNewTrie
*trie
, void *data
, int32_t capacity
,
726 UNewTrieGetFoldedValue
*getFoldedValue
,
727 UBool reduceTo16Bits
,
728 UErrorCode
*pErrorCode
);
731 * Swap a serialized UTrie.
734 U_CAPI
int32_t U_EXPORT2
735 utrie_swap(const UDataSwapper
*ds
,
736 const void *inData
, int32_t length
, void *outData
,
737 UErrorCode
*pErrorCode
);
739 /* serialization ------------------------------------------------------------ */
742 * Trie data structure in serialized form:
744 * UTrieHeader header;
745 * uint16_t index[header.indexLength];
746 * uint16_t data[header.dataLength];
749 typedef struct UTrieHeader
{
750 /** "Trie" in big-endian US-ASCII (0x54726965) */
755 * 9 1=Latin-1 data is stored linearly at data+UTRIE_DATA_BLOCK_LENGTH
756 * 8 0=16-bit data, 1=32-bit data
757 * 7..4 UTRIE_INDEX_SHIFT // 0..UTRIE_SHIFT
758 * 3..0 UTRIE_SHIFT // 1..9
762 /** indexLength is a multiple of UTRIE_SURROGATE_BLOCK_COUNT */
765 /** dataLength>=UTRIE_DATA_BLOCK_LENGTH */
770 * Constants for use with UTrieHeader.options.
774 /** Mask to get the UTRIE_SHIFT value from options. */
775 UTRIE_OPTIONS_SHIFT_MASK
=0xf,
777 /** Shift options right this much to get the UTRIE_INDEX_SHIFT value. */
778 UTRIE_OPTIONS_INDEX_SHIFT
=4,
780 /** If set, then the data (stage 2) array is 32 bits wide. */
781 UTRIE_OPTIONS_DATA_IS_32_BIT
=0x100,
784 * If set, then Latin-1 data (for U+0000..U+00ff) is stored in the data (stage 2) array
785 * as a simple, linear array at data+UTRIE_DATA_BLOCK_LENGTH.
787 UTRIE_OPTIONS_LATIN1_IS_LINEAR
=0x200