2 ******************************************************************************
4 * Copyright (C) 2001-2014, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
10 * tab size: 8 (not used)
13 * created on: 2008aug16 (starting from a copy of utrie.h)
14 * created by: Markus W. Scherer
20 #include "unicode/utypes.h"
26 struct UTrie
; /* forward declaration */
28 typedef struct UTrie UTrie
;
34 * This is a common implementation of a Unicode trie.
35 * It is a kind of compressed, serializable table of 16- or 32-bit values associated with
36 * Unicode code points (0..0x10ffff). (A map from code points to integers.)
38 * This is the second common version of a Unicode trie (hence the name UTrie2).
39 * Compared with UTrie version 1:
40 * - Still splitting BMP code points 11:5 bits for index and data table lookups.
41 * - Still separate data for lead surrogate code _units_ vs. code _points_,
42 * but the lead surrogate code unit values are not required any more
43 * for data lookup for supplementary code points.
44 * - The "folding" mechanism is removed. In UTrie version 1, this somewhat
45 * hard-to-explain mechanism was meant to be used for optimized UTF-16
46 * processing, with application-specific encoding of indexing bits
47 * in the lead surrogate data for the associated supplementary code points.
48 * - For the last single-value code point range (ending with U+10ffff),
49 * the starting code point ("highStart") and the value are stored.
50 * - For supplementary code points U+10000..highStart-1 a three-table lookup
51 * (two index tables and one data table) is used. The first index
52 * is truncated, omitting both the BMP portion and the high range.
53 * - There is a special small index for 2-byte UTF-8, and the initial data
54 * entries are designed for fast 1/2-byte UTF-8 lookup.
59 * Use only with public API macros and functions.
62 typedef struct UTrie2 UTrie2
;
64 /* Public UTrie2 API functions: read-only access ---------------------------- */
67 * Selectors for the width of a UTrie2 data value.
69 enum UTrie2ValueBits
{
70 /** 16 bits per UTrie2 data value. */
72 /** 32 bits per UTrie2 data value. */
74 /** Number of selectors for the width of UTrie2 data values. */
75 UTRIE2_COUNT_VALUE_BITS
77 typedef enum UTrie2ValueBits UTrie2ValueBits
;
80 * Open a frozen trie from its serialized from, stored in 32-bit-aligned memory.
81 * Inverse of utrie2_serialize().
82 * The memory must remain valid and unchanged as long as the trie is used.
83 * You must utrie2_close() the trie once you are done using it.
85 * @param valueBits selects the data entry size; results in an
86 * U_INVALID_FORMAT_ERROR if it does not match the serialized form
87 * @param data a pointer to 32-bit-aligned memory containing the serialized form of a UTrie2
88 * @param length the number of bytes available at data;
89 * can be more than necessary
90 * @param pActualLength receives the actual number of bytes at data taken up by the trie data;
92 * @param pErrorCode an in/out ICU UErrorCode
93 * @return the unserialized trie
96 * @see utrie2_serialize
98 U_CAPI UTrie2
* U_EXPORT2
99 utrie2_openFromSerialized(UTrie2ValueBits valueBits
,
100 const void *data
, int32_t length
, int32_t *pActualLength
,
101 UErrorCode
*pErrorCode
);
104 * Open a frozen, empty "dummy" trie.
105 * A dummy trie is an empty trie, used when a real data trie cannot
106 * be loaded. Equivalent to calling utrie2_open() and utrie2_freeze(),
107 * but without internally creating and compacting/serializing the
108 * builder data structure.
110 * The trie always returns the initialValue,
111 * or the errorValue for out-of-range code points and illegal UTF-8.
113 * You must utrie2_close() the trie once you are done using it.
115 * @param valueBits selects the data entry size
116 * @param initialValue the initial value that is set for all code points
117 * @param errorValue the value for out-of-range code points and illegal UTF-8
118 * @param pErrorCode an in/out ICU UErrorCode
119 * @return the dummy trie
121 * @see utrie2_openFromSerialized
124 U_CAPI UTrie2
* U_EXPORT2
125 utrie2_openDummy(UTrie2ValueBits valueBits
,
126 uint32_t initialValue
, uint32_t errorValue
,
127 UErrorCode
*pErrorCode
);
130 * Get a value from a code point as stored in the trie.
131 * Easier to use than UTRIE2_GET16() and UTRIE2_GET32() but slower.
132 * Easier to use because, unlike the macros, this function works on all UTrie2
133 * objects, frozen or not, holding 16-bit or 32-bit data values.
135 * @param trie the trie
136 * @param c the code point
139 U_CAPI
uint32_t U_EXPORT2
140 utrie2_get32(const UTrie2
*trie
, UChar32 c
);
142 /* enumeration callback types */
145 * Callback from utrie2_enum(), extracts a uint32_t value from a
146 * trie value. This value will be passed on to the UTrie2EnumRange function.
148 * @param context an opaque pointer, as passed into utrie2_enum()
149 * @param value a value from the trie
150 * @return the value that is to be passed on to the UTrie2EnumRange function
152 typedef uint32_t U_CALLCONV
153 UTrie2EnumValue(const void *context
, uint32_t value
);
156 * Callback from utrie2_enum(), is called for each contiguous range
157 * of code points with the same value as retrieved from the trie and
158 * transformed by the UTrie2EnumValue function.
160 * The callback function can stop the enumeration by returning FALSE.
162 * @param context an opaque pointer, as passed into utrie2_enum()
163 * @param start the first code point in a contiguous range with value
164 * @param end the last code point in a contiguous range with value (inclusive)
165 * @param value the value that is set for all code points in [start..end]
166 * @return FALSE to stop the enumeration
168 typedef UBool U_CALLCONV
169 UTrie2EnumRange(const void *context
, UChar32 start
, UChar32 end
, uint32_t value
);
172 * Enumerate efficiently all values in a trie.
173 * Do not modify the trie during the enumeration.
175 * For each entry in the trie, the value to be delivered is passed through
176 * the UTrie2EnumValue function.
177 * The value is unchanged if that function pointer is NULL.
179 * For each contiguous range of code points with a given (transformed) value,
180 * the UTrie2EnumRange function is called.
182 * @param trie a pointer to the trie
183 * @param enumValue a pointer to a function that may transform the trie entry value,
184 * or NULL if the values from the trie are to be used directly
185 * @param enumRange a pointer to a function that is called for each contiguous range
186 * of code points with the same (transformed) value
187 * @param context an opaque pointer that is passed on to the callback functions
189 U_CAPI
void U_EXPORT2
190 utrie2_enum(const UTrie2
*trie
,
191 UTrie2EnumValue
*enumValue
, UTrie2EnumRange
*enumRange
, const void *context
);
193 /* Building a trie ---------------------------------------------------------- */
196 * Open an empty, writable trie. At build time, 32-bit data values are used.
197 * utrie2_freeze() takes a valueBits parameter
198 * which determines the data value width in the serialized and frozen forms.
199 * You must utrie2_close() the trie once you are done using it.
201 * @param initialValue the initial value that is set for all code points
202 * @param errorValue the value for out-of-range code points and illegal UTF-8
203 * @param pErrorCode an in/out ICU UErrorCode
204 * @return a pointer to the allocated and initialized new trie
206 U_CAPI UTrie2
* U_EXPORT2
207 utrie2_open(uint32_t initialValue
, uint32_t errorValue
, UErrorCode
*pErrorCode
);
211 * You must utrie2_close() the clone once you are done using it.
213 * @param other the trie to clone
214 * @param pErrorCode an in/out ICU UErrorCode
215 * @return a pointer to the new trie clone
217 U_CAPI UTrie2
* U_EXPORT2
218 utrie2_clone(const UTrie2
*other
, UErrorCode
*pErrorCode
);
221 * Clone a trie. The clone will be mutable/writable even if the other trie
222 * is frozen. (See utrie2_freeze().)
223 * You must utrie2_close() the clone once you are done using it.
225 * @param other the trie to clone
226 * @param pErrorCode an in/out ICU UErrorCode
227 * @return a pointer to the new trie clone
229 U_CAPI UTrie2
* U_EXPORT2
230 utrie2_cloneAsThawed(const UTrie2
*other
, UErrorCode
*pErrorCode
);
233 * Close a trie and release associated memory.
235 * @param trie the trie
237 U_CAPI
void U_EXPORT2
238 utrie2_close(UTrie2
*trie
);
241 * Set a value for a code point.
243 * @param trie the unfrozen trie
244 * @param c the code point
245 * @param value the value
246 * @param pErrorCode an in/out ICU UErrorCode; among other possible error codes:
247 * - U_NO_WRITE_PERMISSION if the trie is frozen
249 U_CAPI
void U_EXPORT2
250 utrie2_set32(UTrie2
*trie
, UChar32 c
, uint32_t value
, UErrorCode
*pErrorCode
);
253 * Set a value in a range of code points [start..end].
254 * All code points c with start<=c<=end will get the value if
255 * overwrite is TRUE or if the old value is the initial value.
257 * @param trie the unfrozen trie
258 * @param start the first code point to get the value
259 * @param end the last code point to get the value (inclusive)
260 * @param value the value
261 * @param overwrite flag for whether old non-initial values are to be overwritten
262 * @param pErrorCode an in/out ICU UErrorCode; among other possible error codes:
263 * - U_NO_WRITE_PERMISSION if the trie is frozen
265 U_CAPI
void U_EXPORT2
266 utrie2_setRange32(UTrie2
*trie
,
267 UChar32 start
, UChar32 end
,
268 uint32_t value
, UBool overwrite
,
269 UErrorCode
*pErrorCode
);
272 * Freeze a trie. Make it immutable (read-only) and compact it,
273 * ready for serialization and for use with fast macros.
274 * Functions to set values will fail after serializing.
276 * A trie can be frozen only once. If this function is called again with different
277 * valueBits then it will set a U_ILLEGAL_ARGUMENT_ERROR.
279 * @param trie the trie
280 * @param valueBits selects the data entry size; if smaller than 32 bits, then
281 * the values stored in the trie will be truncated
282 * @param pErrorCode an in/out ICU UErrorCode; among other possible error codes:
283 * - U_INDEX_OUTOFBOUNDS_ERROR if the compacted index or data arrays are too long
285 * (the trie will be immutable and usable,
286 * but not frozen and not usable with the fast macros)
288 * @see utrie2_cloneAsThawed
290 U_CAPI
void U_EXPORT2
291 utrie2_freeze(UTrie2
*trie
, UTrie2ValueBits valueBits
, UErrorCode
*pErrorCode
);
294 * Test if the trie is frozen. (See utrie2_freeze().)
296 * @param trie the trie
297 * @return TRUE if the trie is frozen, that is, immutable, ready for serialization
298 * and for use with fast macros
300 U_CAPI UBool U_EXPORT2
301 utrie2_isFrozen(const UTrie2
*trie
);
304 * Serialize a frozen trie into 32-bit aligned memory.
305 * If the trie is not frozen, then the function returns with a U_ILLEGAL_ARGUMENT_ERROR.
306 * A trie can be serialized multiple times.
308 * @param trie the frozen trie
309 * @param data a pointer to 32-bit-aligned memory to be filled with the trie data,
310 * can be NULL if capacity==0
311 * @param capacity the number of bytes available at data,
312 * or 0 for preflighting
313 * @param pErrorCode an in/out ICU UErrorCode; among other possible error codes:
314 * - U_BUFFER_OVERFLOW_ERROR if the data storage block is too small for serialization
315 * - U_ILLEGAL_ARGUMENT_ERROR if the trie is not frozen or the data and capacity
317 * @return the number of bytes written or needed for the trie
319 * @see utrie2_openFromSerialized()
321 U_CAPI
int32_t U_EXPORT2
322 utrie2_serialize(const UTrie2
*trie
,
323 void *data
, int32_t capacity
,
324 UErrorCode
*pErrorCode
);
326 /* Public UTrie2 API: miscellaneous functions ------------------------------- */
329 * Get the UTrie version from 32-bit-aligned memory containing the serialized form
330 * of either a UTrie (version 1) or a UTrie2 (version 2).
332 * @param data a pointer to 32-bit-aligned memory containing the serialized form
333 * of a UTrie, version 1 or 2
334 * @param length the number of bytes available at data;
335 * can be more than necessary (see return value)
336 * @param anyEndianOk If FALSE, only platform-endian serialized forms are recognized.
337 * If TRUE, opposite-endian serialized forms are recognized as well.
338 * @return the UTrie version of the serialized form, or 0 if it is not
339 * recognized as a serialized UTrie
341 U_CAPI
int32_t U_EXPORT2
342 utrie2_getVersion(const void *data
, int32_t length
, UBool anyEndianOk
);
345 * Swap a serialized UTrie2.
348 U_CAPI
int32_t U_EXPORT2
349 utrie2_swap(const UDataSwapper
*ds
,
350 const void *inData
, int32_t length
, void *outData
,
351 UErrorCode
*pErrorCode
);
354 * Swap a serialized UTrie or UTrie2.
357 U_CAPI
int32_t U_EXPORT2
358 utrie2_swapAnyVersion(const UDataSwapper
*ds
,
359 const void *inData
, int32_t length
, void *outData
,
360 UErrorCode
*pErrorCode
);
363 * Build a UTrie2 (version 2) from a UTrie (version 1).
364 * Enumerates all values in the UTrie and builds a UTrie2 with the same values.
365 * The resulting UTrie2 will be frozen.
367 * @param trie1 the runtime UTrie structure to be enumerated
368 * @param errorValue the value for out-of-range code points and illegal UTF-8
369 * @param pErrorCode an in/out ICU UErrorCode
370 * @return The frozen UTrie2 with the same values as the UTrie.
372 U_CAPI UTrie2
* U_EXPORT2
373 utrie2_fromUTrie(const UTrie
*trie1
, uint32_t errorValue
, UErrorCode
*pErrorCode
);
375 /* Public UTrie2 API macros ------------------------------------------------- */
378 * These macros provide fast data lookup from a frozen trie.
379 * They will crash when used on an unfrozen trie.
383 * Return a 16-bit trie value from a code point, with range checking.
384 * Returns trie->errorValue if c is not in the range 0..U+10ffff.
386 * @param trie (const UTrie2 *, in) a frozen trie
387 * @param c (UChar32, in) the input code point
388 * @return (uint16_t) The code point's trie value.
390 #define UTRIE2_GET16(trie, c) _UTRIE2_GET((trie), index, (trie)->indexLength, (c))
393 * Return a 32-bit trie value from a code point, with range checking.
394 * Returns trie->errorValue if c is not in the range 0..U+10ffff.
396 * @param trie (const UTrie2 *, in) a frozen trie
397 * @param c (UChar32, in) the input code point
398 * @return (uint32_t) The code point's trie value.
400 #define UTRIE2_GET32(trie, c) _UTRIE2_GET((trie), data32, 0, (c))
403 * UTF-16: Get the next code point (UChar32 c, out), post-increment src,
404 * and get a 16-bit value from the trie.
406 * @param trie (const UTrie2 *, in) a frozen trie
407 * @param src (const UChar *, in/out) the source text pointer
408 * @param limit (const UChar *, in) the limit pointer for the text, or NULL if NUL-terminated
409 * @param c (UChar32, out) variable for the code point
410 * @param result (uint16_t, out) uint16_t variable for the trie lookup result
412 #define UTRIE2_U16_NEXT16(trie, src, limit, c, result) _UTRIE2_U16_NEXT(trie, index, src, limit, c, result)
415 * UTF-16: Get the next code point (UChar32 c, out), post-increment src,
416 * and get a 32-bit value from the trie.
418 * @param trie (const UTrie2 *, in) a frozen trie
419 * @param src (const UChar *, in/out) the source text pointer
420 * @param limit (const UChar *, in) the limit pointer for the text, or NULL if NUL-terminated
421 * @param c (UChar32, out) variable for the code point
422 * @param result (uint32_t, out) uint32_t variable for the trie lookup result
424 #define UTRIE2_U16_NEXT32(trie, src, limit, c, result) _UTRIE2_U16_NEXT(trie, data32, src, limit, c, result)
427 * UTF-16: Get the previous code point (UChar32 c, out), pre-decrement src,
428 * and get a 16-bit value from the trie.
430 * @param trie (const UTrie2 *, in) a frozen trie
431 * @param start (const UChar *, in) the start pointer for the text
432 * @param src (const UChar *, in/out) the source text pointer
433 * @param c (UChar32, out) variable for the code point
434 * @param result (uint16_t, out) uint16_t variable for the trie lookup result
436 #define UTRIE2_U16_PREV16(trie, start, src, c, result) _UTRIE2_U16_PREV(trie, index, start, src, c, result)
439 * UTF-16: Get the previous code point (UChar32 c, out), pre-decrement src,
440 * and get a 32-bit value from the trie.
442 * @param trie (const UTrie2 *, in) a frozen trie
443 * @param start (const UChar *, in) the start pointer for the text
444 * @param src (const UChar *, in/out) the source text pointer
445 * @param c (UChar32, out) variable for the code point
446 * @param result (uint32_t, out) uint32_t variable for the trie lookup result
448 #define UTRIE2_U16_PREV32(trie, start, src, c, result) _UTRIE2_U16_PREV(trie, data32, start, src, c, result)
451 * UTF-8: Post-increment src and get a 16-bit value from the trie.
453 * @param trie (const UTrie2 *, in) a frozen trie
454 * @param src (const char *, in/out) the source text pointer
455 * @param limit (const char *, in) the limit pointer for the text (must not be NULL)
456 * @param result (uint16_t, out) uint16_t variable for the trie lookup result
458 #define UTRIE2_U8_NEXT16(trie, src, limit, result)\
459 _UTRIE2_U8_NEXT(trie, data16, index, src, limit, result)
462 * UTF-8: Post-increment src and get a 32-bit value from the trie.
464 * @param trie (const UTrie2 *, in) a frozen trie
465 * @param src (const char *, in/out) the source text pointer
466 * @param limit (const char *, in) the limit pointer for the text (must not be NULL)
467 * @param result (uint16_t, out) uint32_t variable for the trie lookup result
469 #define UTRIE2_U8_NEXT32(trie, src, limit, result) \
470 _UTRIE2_U8_NEXT(trie, data32, data32, src, limit, result)
473 * UTF-8: Pre-decrement src and get a 16-bit value from the trie.
475 * @param trie (const UTrie2 *, in) a frozen trie
476 * @param start (const char *, in) the start pointer for the text
477 * @param src (const char *, in/out) the source text pointer
478 * @param result (uint16_t, out) uint16_t variable for the trie lookup result
480 #define UTRIE2_U8_PREV16(trie, start, src, result) \
481 _UTRIE2_U8_PREV(trie, data16, index, start, src, result)
484 * UTF-8: Pre-decrement src and get a 32-bit value from the trie.
486 * @param trie (const UTrie2 *, in) a frozen trie
487 * @param start (const char *, in) the start pointer for the text
488 * @param src (const char *, in/out) the source text pointer
489 * @param result (uint16_t, out) uint32_t variable for the trie lookup result
491 #define UTRIE2_U8_PREV32(trie, start, src, result) \
492 _UTRIE2_U8_PREV(trie, data32, data32, start, src, result)
494 /* Public UTrie2 API: optimized UTF-16 access ------------------------------- */
497 * The following functions and macros are used for highly optimized UTF-16
498 * text processing. The UTRIE2_U16_NEXTxy() macros do not depend on these.
500 * A UTrie2 stores separate values for lead surrogate code _units_ vs. code _points_.
501 * UTF-16 text processing can be optimized by detecting surrogate pairs and
502 * assembling supplementary code points only when there is non-trivial data
505 * At build-time, use utrie2_enumForLeadSurrogate() to see if there
506 * is non-trivial (non-initialValue) data for any of the supplementary
507 * code points associated with a lead surrogate.
508 * If so, then set a special (application-specific) value for the
509 * lead surrogate code _unit_, with utrie2_set32ForLeadSurrogateCodeUnit().
511 * At runtime, use UTRIE2_GET16_FROM_U16_SINGLE_LEAD() or
512 * UTRIE2_GET32_FROM_U16_SINGLE_LEAD() per code unit. If there is non-trivial
513 * data and the code unit is a lead surrogate, then check if a trail surrogate
514 * follows. If so, assemble the supplementary code point with
515 * U16_GET_SUPPLEMENTARY() and look up its value with UTRIE2_GET16_FROM_SUPP()
516 * or UTRIE2_GET32_FROM_SUPP(); otherwise reset the lead
517 * surrogate's value or do a code point lookup for it.
519 * If there is only trivial data for lead and trail surrogates, then processing
520 * can often skip them. For example, in normalization or case mapping
521 * all characters that do not have any mappings are simply copied as is.
525 * Get a value from a lead surrogate code unit as stored in the trie.
527 * @param trie the trie
528 * @param c the code unit (U+D800..U+DBFF)
531 U_CAPI
uint32_t U_EXPORT2
532 utrie2_get32FromLeadSurrogateCodeUnit(const UTrie2
*trie
, UChar32 c
);
535 * Enumerate the trie values for the 1024=0x400 code points
536 * corresponding to a given lead surrogate.
537 * For example, for the lead surrogate U+D87E it will enumerate the values
538 * for [U+2F800..U+2FC00[.
539 * Used by data builder code that sets special lead surrogate code unit values
540 * for optimized UTF-16 string processing.
542 * Do not modify the trie during the enumeration.
544 * Except for the limited code point range, this functions just like utrie2_enum():
545 * For each entry in the trie, the value to be delivered is passed through
546 * the UTrie2EnumValue function.
547 * The value is unchanged if that function pointer is NULL.
549 * For each contiguous range of code points with a given (transformed) value,
550 * the UTrie2EnumRange function is called.
552 * @param trie a pointer to the trie
553 * @param enumValue a pointer to a function that may transform the trie entry value,
554 * or NULL if the values from the trie are to be used directly
555 * @param enumRange a pointer to a function that is called for each contiguous range
556 * of code points with the same (transformed) value
557 * @param context an opaque pointer that is passed on to the callback functions
559 U_CAPI
void U_EXPORT2
560 utrie2_enumForLeadSurrogate(const UTrie2
*trie
, UChar32 lead
,
561 UTrie2EnumValue
*enumValue
, UTrie2EnumRange
*enumRange
,
562 const void *context
);
565 * Set a value for a lead surrogate code unit.
567 * @param trie the unfrozen trie
568 * @param lead the lead surrogate code unit (U+D800..U+DBFF)
569 * @param value the value
570 * @param pErrorCode an in/out ICU UErrorCode; among other possible error codes:
571 * - U_NO_WRITE_PERMISSION if the trie is frozen
573 U_CAPI
void U_EXPORT2
574 utrie2_set32ForLeadSurrogateCodeUnit(UTrie2
*trie
,
575 UChar32 lead
, uint32_t value
,
576 UErrorCode
*pErrorCode
);
579 * Return a 16-bit trie value from a UTF-16 single/lead code unit (<=U+ffff).
580 * Same as UTRIE2_GET16() if c is a BMP code point except for lead surrogates,
581 * but smaller and faster.
583 * @param trie (const UTrie2 *, in) a frozen trie
584 * @param c (UChar32, in) the input code unit, must be 0<=c<=U+ffff
585 * @return (uint16_t) The code unit's trie value.
587 #define UTRIE2_GET16_FROM_U16_SINGLE_LEAD(trie, c) _UTRIE2_GET_FROM_U16_SINGLE_LEAD((trie), index, c)
590 * Return a 32-bit trie value from a UTF-16 single/lead code unit (<=U+ffff).
591 * Same as UTRIE2_GET32() if c is a BMP code point except for lead surrogates,
592 * but smaller and faster.
594 * @param trie (const UTrie2 *, in) a frozen trie
595 * @param c (UChar32, in) the input code unit, must be 0<=c<=U+ffff
596 * @return (uint32_t) The code unit's trie value.
598 #define UTRIE2_GET32_FROM_U16_SINGLE_LEAD(trie, c) _UTRIE2_GET_FROM_U16_SINGLE_LEAD((trie), data32, c)
601 * Return a 16-bit trie value from a supplementary code point (U+10000..U+10ffff).
603 * @param trie (const UTrie2 *, in) a frozen trie
604 * @param c (UChar32, in) the input code point, must be U+10000<=c<=U+10ffff
605 * @return (uint16_t) The code point's trie value.
607 #define UTRIE2_GET16_FROM_SUPP(trie, c) _UTRIE2_GET_FROM_SUPP((trie), index, c)
610 * Return a 32-bit trie value from a supplementary code point (U+10000..U+10ffff).
612 * @param trie (const UTrie2 *, in) a frozen trie
613 * @param c (UChar32, in) the input code point, must be U+10000<=c<=U+10ffff
614 * @return (uint32_t) The code point's trie value.
616 #define UTRIE2_GET32_FROM_SUPP(trie, c) _UTRIE2_GET_FROM_SUPP((trie), data32, c)
620 /* C++ convenience wrappers ------------------------------------------------- */
624 #include "unicode/utf.h"
629 // Use the Forward/Backward subclasses below.
630 class UTrie2StringIterator
: public UMemory
{
632 UTrie2StringIterator(const UTrie2
*t
, const UChar
*p
) :
633 trie(t
), codePointStart(p
), codePointLimit(p
), codePoint(U_SENTINEL
) {}
636 const UChar
*codePointStart
, *codePointLimit
;
640 class BackwardUTrie2StringIterator
: public UTrie2StringIterator
{
642 BackwardUTrie2StringIterator(const UTrie2
*t
, const UChar
*s
, const UChar
*p
) :
643 UTrie2StringIterator(t
, p
), start(s
) {}
645 uint16_t previous16();
650 class ForwardUTrie2StringIterator
: public UTrie2StringIterator
{
652 // Iteration limit l can be NULL.
653 // In that case, the caller must detect c==0 and stop.
654 ForwardUTrie2StringIterator(const UTrie2
*t
, const UChar
*p
, const UChar
*l
) :
655 UTrie2StringIterator(t
, p
), limit(l
) {}
666 /* Internal definitions ----------------------------------------------------- */
670 /** Build-time trie structure. */
672 typedef struct UNewTrie2 UNewTrie2
;
675 * Trie structure definition.
677 * Either the data table is 16 bits wide and accessed via the index
678 * pointer, with each index item increased by indexLength;
679 * in this case, data32==NULL, and data16 is used for direct ASCII access.
681 * Or the data table is 32 bits wide and accessed via the data32 pointer.
684 /* protected: used by macros and functions for reading values */
685 const uint16_t *index
;
686 const uint16_t *data16
; /* for fast UTF-8 ASCII access, if 16b data */
687 const uint32_t *data32
; /* NULL if 16b data is used via index */
689 int32_t indexLength
, dataLength
;
690 uint16_t index2NullOffset
; /* 0xffff if there is no dedicated index-2 null block */
691 uint16_t dataNullOffset
;
692 uint32_t initialValue
;
693 /** Value returned for out-of-range code points and illegal UTF-8. */
696 /* Start of the last range which ends at U+10ffff, and its value. */
698 int32_t highValueIndex
;
700 /* private: used by builder and unserialization functions */
701 void *memory
; /* serialized bytes; NULL if not frozen yet */
702 int32_t length
; /* number of serialized bytes at memory; 0 if not frozen yet */
703 UBool isMemoryOwned
; /* TRUE if the trie owns the memory */
706 UNewTrie2
*newTrie
; /* builder object; NULL when frozen */
710 * Trie constants, defining shift widths, index array lengths, etc.
712 * These are needed for the runtime macros but users can treat these as
713 * implementation details and skip to the actual public API further below.
716 /** Shift size for getting the index-1 table offset. */
719 /** Shift size for getting the index-2 table offset. */
723 * Difference between the two shift sizes,
724 * for getting an index-1 offset from an index-2 offset. 6=11-5
726 UTRIE2_SHIFT_1_2
=UTRIE2_SHIFT_1
-UTRIE2_SHIFT_2
,
729 * Number of index-1 entries for the BMP. 32=0x20
730 * This part of the index-1 table is omitted from the serialized form.
732 UTRIE2_OMITTED_BMP_INDEX_1_LENGTH
=0x10000>>UTRIE2_SHIFT_1
,
734 /** Number of code points per index-1 table entry. 2048=0x800 */
735 UTRIE2_CP_PER_INDEX_1_ENTRY
=1<<UTRIE2_SHIFT_1
,
737 /** Number of entries in an index-2 block. 64=0x40 */
738 UTRIE2_INDEX_2_BLOCK_LENGTH
=1<<UTRIE2_SHIFT_1_2
,
740 /** Mask for getting the lower bits for the in-index-2-block offset. */
741 UTRIE2_INDEX_2_MASK
=UTRIE2_INDEX_2_BLOCK_LENGTH
-1,
743 /** Number of entries in a data block. 32=0x20 */
744 UTRIE2_DATA_BLOCK_LENGTH
=1<<UTRIE2_SHIFT_2
,
746 /** Mask for getting the lower bits for the in-data-block offset. */
747 UTRIE2_DATA_MASK
=UTRIE2_DATA_BLOCK_LENGTH
-1,
750 * Shift size for shifting left the index array values.
751 * Increases possible data size with 16-bit index values at the cost
753 * This requires data blocks to be aligned by UTRIE2_DATA_GRANULARITY.
755 UTRIE2_INDEX_SHIFT
=2,
757 /** The alignment size of a data block. Also the granularity for compaction. */
758 UTRIE2_DATA_GRANULARITY
=1<<UTRIE2_INDEX_SHIFT
,
760 /* Fixed layout of the first part of the index array. ------------------- */
763 * The BMP part of the index-2 table is fixed and linear and starts at offset 0.
764 * Length=2048=0x800=0x10000>>UTRIE2_SHIFT_2.
766 UTRIE2_INDEX_2_OFFSET
=0,
769 * The part of the index-2 table for U+D800..U+DBFF stores values for
770 * lead surrogate code _units_ not code _points_.
771 * Values for lead surrogate code _points_ are indexed with this portion of the table.
772 * Length=32=0x20=0x400>>UTRIE2_SHIFT_2. (There are 1024=0x400 lead surrogates.)
774 UTRIE2_LSCP_INDEX_2_OFFSET
=0x10000>>UTRIE2_SHIFT_2
,
775 UTRIE2_LSCP_INDEX_2_LENGTH
=0x400>>UTRIE2_SHIFT_2
,
777 /** Count the lengths of both BMP pieces. 2080=0x820 */
778 UTRIE2_INDEX_2_BMP_LENGTH
=UTRIE2_LSCP_INDEX_2_OFFSET
+UTRIE2_LSCP_INDEX_2_LENGTH
,
781 * The 2-byte UTF-8 version of the index-2 table follows at offset 2080=0x820.
782 * Length 32=0x20 for lead bytes C0..DF, regardless of UTRIE2_SHIFT_2.
784 UTRIE2_UTF8_2B_INDEX_2_OFFSET
=UTRIE2_INDEX_2_BMP_LENGTH
,
785 UTRIE2_UTF8_2B_INDEX_2_LENGTH
=0x800>>6, /* U+0800 is the first code point after 2-byte UTF-8 */
788 * The index-1 table, only used for supplementary code points, at offset 2112=0x840.
789 * Variable length, for code points up to highStart, where the last single-value range starts.
790 * Maximum length 512=0x200=0x100000>>UTRIE2_SHIFT_1.
791 * (For 0x100000 supplementary code points U+10000..U+10ffff.)
793 * The part of the index-2 table for supplementary code points starts
794 * after this index-1 table.
796 * Both the index-1 table and the following part of the index-2 table
797 * are omitted completely if there is only BMP data.
799 UTRIE2_INDEX_1_OFFSET
=UTRIE2_UTF8_2B_INDEX_2_OFFSET
+UTRIE2_UTF8_2B_INDEX_2_LENGTH
,
800 UTRIE2_MAX_INDEX_1_LENGTH
=0x100000>>UTRIE2_SHIFT_1
,
803 * Fixed layout of the first part of the data array. -----------------------
804 * Starts with 4 blocks (128=0x80 entries) for ASCII.
808 * The illegal-UTF-8 data block follows the ASCII block, at offset 128=0x80.
809 * Used with linear access for single bytes 0..0xbf for simple error handling.
810 * Length 64=0x40, not UTRIE2_DATA_BLOCK_LENGTH.
812 UTRIE2_BAD_UTF8_DATA_OFFSET
=0x80,
814 /** The start of non-linear-ASCII data blocks, at offset 192=0xc0. */
815 UTRIE2_DATA_START_OFFSET
=0xc0
818 /* Internal functions and macros -------------------------------------------- */
821 * Internal function for part of the UTRIE2_U8_NEXTxx() macro implementations.
822 * Do not call directly.
825 U_INTERNAL
int32_t U_EXPORT2
826 utrie2_internalU8NextIndex(const UTrie2
*trie
, UChar32 c
,
827 const uint8_t *src
, const uint8_t *limit
);
830 * Internal function for part of the UTRIE2_U8_PREVxx() macro implementations.
831 * Do not call directly.
834 U_INTERNAL
int32_t U_EXPORT2
835 utrie2_internalU8PrevIndex(const UTrie2
*trie
, UChar32 c
,
836 const uint8_t *start
, const uint8_t *src
);
839 /** Internal low-level trie getter. Returns a data index. */
840 #define _UTRIE2_INDEX_RAW(offset, trieIndex, c) \
841 (((int32_t)((trieIndex)[(offset)+((c)>>UTRIE2_SHIFT_2)]) \
842 <<UTRIE2_INDEX_SHIFT)+ \
843 ((c)&UTRIE2_DATA_MASK))
845 /** Internal trie getter from a UTF-16 single/lead code unit. Returns the data index. */
846 #define _UTRIE2_INDEX_FROM_U16_SINGLE_LEAD(trieIndex, c) _UTRIE2_INDEX_RAW(0, trieIndex, c)
848 /** Internal trie getter from a lead surrogate code point (D800..DBFF). Returns the data index. */
849 #define _UTRIE2_INDEX_FROM_LSCP(trieIndex, c) \
850 _UTRIE2_INDEX_RAW(UTRIE2_LSCP_INDEX_2_OFFSET-(0xd800>>UTRIE2_SHIFT_2), trieIndex, c)
852 /** Internal trie getter from a BMP code point. Returns the data index. */
853 #define _UTRIE2_INDEX_FROM_BMP(trieIndex, c) \
854 _UTRIE2_INDEX_RAW(U_IS_LEAD(c) ? UTRIE2_LSCP_INDEX_2_OFFSET-(0xd800>>UTRIE2_SHIFT_2) : 0, \
857 /** Internal trie getter from a supplementary code point below highStart. Returns the data index. */
858 #define _UTRIE2_INDEX_FROM_SUPP(trieIndex, c) \
859 (((int32_t)((trieIndex)[ \
860 (trieIndex)[(UTRIE2_INDEX_1_OFFSET-UTRIE2_OMITTED_BMP_INDEX_1_LENGTH)+ \
861 ((c)>>UTRIE2_SHIFT_1)]+ \
862 (((c)>>UTRIE2_SHIFT_2)&UTRIE2_INDEX_2_MASK)]) \
863 <<UTRIE2_INDEX_SHIFT)+ \
864 ((c)&UTRIE2_DATA_MASK))
867 * Internal trie getter from a code point, with checking that c is in 0..10FFFF.
868 * Returns the data index.
870 #define _UTRIE2_INDEX_FROM_CP(trie, asciiOffset, c) \
871 ((uint32_t)(c)<0xd800 ? \
872 _UTRIE2_INDEX_RAW(0, (trie)->index, c) : \
873 (uint32_t)(c)<=0xffff ? \
875 (c)<=0xdbff ? UTRIE2_LSCP_INDEX_2_OFFSET-(0xd800>>UTRIE2_SHIFT_2) : 0, \
876 (trie)->index, c) : \
877 (uint32_t)(c)>0x10ffff ? \
878 (asciiOffset)+UTRIE2_BAD_UTF8_DATA_OFFSET : \
879 (c)>=(trie)->highStart ? \
880 (trie)->highValueIndex : \
881 _UTRIE2_INDEX_FROM_SUPP((trie)->index, c))
883 /** Internal trie getter from a UTF-16 single/lead code unit. Returns the data. */
884 #define _UTRIE2_GET_FROM_U16_SINGLE_LEAD(trie, data, c) \
885 (trie)->data[_UTRIE2_INDEX_FROM_U16_SINGLE_LEAD((trie)->index, c)]
887 /** Internal trie getter from a supplementary code point. Returns the data. */
888 #define _UTRIE2_GET_FROM_SUPP(trie, data, c) \
889 (trie)->data[(c)>=(trie)->highStart ? (trie)->highValueIndex : \
890 _UTRIE2_INDEX_FROM_SUPP((trie)->index, c)]
893 * Internal trie getter from a code point, with checking that c is in 0..10FFFF.
896 #define _UTRIE2_GET(trie, data, asciiOffset, c) \
897 (trie)->data[_UTRIE2_INDEX_FROM_CP(trie, asciiOffset, c)]
899 /** Internal next-post-increment: get the next code point (c) and its data. */
900 #define _UTRIE2_U16_NEXT(trie, data, src, limit, c, result) { \
904 if(!U16_IS_LEAD(c)) { \
905 (result)=_UTRIE2_GET_FROM_U16_SINGLE_LEAD(trie, data, c); \
906 } else if((src)==(limit) || !U16_IS_TRAIL(__c2=*(src))) { \
907 (result)=(trie)->data[_UTRIE2_INDEX_FROM_LSCP((trie)->index, c)]; \
910 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
911 (result)=_UTRIE2_GET_FROM_SUPP((trie), data, (c)); \
916 /** Internal pre-decrement-previous: get the previous code point (c) and its data */
917 #define _UTRIE2_U16_PREV(trie, data, start, src, c, result) { \
921 if(!U16_IS_TRAIL(c) || (src)==(start) || !U16_IS_LEAD(__c2=*((src)-1))) { \
922 (result)=(trie)->data[_UTRIE2_INDEX_FROM_BMP((trie)->index, c)]; \
925 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
926 (result)=_UTRIE2_GET_FROM_SUPP((trie), data, (c)); \
931 /** Internal UTF-8 next-post-increment: get the next code point's data. */
932 #define _UTRIE2_U8_NEXT(trie, ascii, data, src, limit, result) { \
933 uint8_t __lead=(uint8_t)*(src)++; \
935 (result)=(trie)->ascii[__lead]; \
937 uint8_t __t1, __t2; \
938 if( /* handle U+0000..U+07FF inline */ \
939 __lead<0xe0 && (src)<(limit) && \
940 (__t1=(uint8_t)(*(src)-0x80))<=0x3f \
943 (result)=(trie)->data[ \
944 (trie)->index[(UTRIE2_UTF8_2B_INDEX_2_OFFSET-0xc0)+__lead]+ \
946 } else if( /* handle U+0000..U+CFFF inline */ \
947 __lead<0xed && ((src)+1)<(limit) && \
948 (__t1=(uint8_t)(*(src)-0x80))<=0x3f && (__lead>0xe0 || __t1>=0x20) && \
949 (__t2=(uint8_t)(*((src)+1)-0x80))<= 0x3f \
952 (result)=(trie)->data[ \
953 ((int32_t)((trie)->index[((__lead-0xe0)<<(12-UTRIE2_SHIFT_2))+ \
954 (__t1<<(6-UTRIE2_SHIFT_2))+(__t2>>UTRIE2_SHIFT_2)]) \
955 <<UTRIE2_INDEX_SHIFT)+ \
956 (__t2&UTRIE2_DATA_MASK)]; \
958 int32_t __index=utrie2_internalU8NextIndex((trie), __lead, (const uint8_t *)(src), \
959 (const uint8_t *)(limit)); \
961 (result)=(trie)->data[__index>>3]; \
966 /** Internal UTF-8 pre-decrement-previous: get the previous code point's data. */
967 #define _UTRIE2_U8_PREV(trie, ascii, data, start, src, result) { \
968 uint8_t __b=(uint8_t)*--(src); \
970 (result)=(trie)->ascii[__b]; \
972 int32_t __index=utrie2_internalU8PrevIndex((trie), __b, (const uint8_t *)(start), \
973 (const uint8_t *)(src)); \
975 (result)=(trie)->data[__index>>3]; \
982 * Work around MSVC 2003 optimization bugs.
984 #if defined (U_HAVE_MSVC_2003_OR_EARLIER)
985 #pragma optimize("", off)