Home | History | Annotate | Download | only in common
      1 /*
      2 ******************************************************************************
      3 *
      4 *   Copyright (C) 2001-2009, International Business Machines
      5 *   Corporation and others.  All Rights Reserved.
      6 *
      7 ******************************************************************************
      8 *   file name:  utrie2.h
      9 *   encoding:   US-ASCII
     10 *   tab size:   8 (not used)
     11 *   indentation:4
     12 *
     13 *   created on: 2008aug16 (starting from a copy of utrie.h)
     14 *   created by: Markus W. Scherer
     15 */
     16 
     17 #ifndef __UTRIE2_H__
     18 #define __UTRIE2_H__
     19 
     20 #include "unicode/utypes.h"
     21 #include "udataswp.h"
     22 
     23 U_CDECL_BEGIN
     24 
     25 struct UTrie;  /* forward declaration */
     26 #ifndef __UTRIE_H__
     27 typedef struct UTrie UTrie;
     28 #endif
     29 
     30 /**
     31  * \file
     32  *
     33  * This is a common implementation of a Unicode trie.
     34  * It is a kind of compressed, serializable table of 16- or 32-bit values associated with
     35  * Unicode code points (0..0x10ffff). (A map from code points to integers.)
     36  *
     37  * This is the second common version of a Unicode trie (hence the name UTrie2).
     38  * Compared with UTrie version 1:
     39  * - Still splitting BMP code points 11:5 bits for index and data table lookups.
     40  * - Still separate data for lead surrogate code _units_ vs. code _points_,
     41  *   but the lead surrogate code unit values are not required any more
     42  *   for data lookup for supplementary code points.
     43  * - The "folding" mechanism is removed. In UTrie version 1, this somewhat
     44  *   hard-to-explain mechanism was meant to be used for optimized UTF-16
     45  *   processing, with application-specific encoding of indexing bits
     46  *   in the lead surrogate data for the associated supplementary code points.
     47  * - For the last single-value code point range (ending with U+10ffff),
     48  *   the starting code point ("highStart") and the value are stored.
     49  * - For supplementary code points U+10000..highStart-1 a three-table lookup
     50  *   (two index tables and one data table) is used. The first index
     51  *   is truncated, omitting both the BMP portion and the high range.
     52  * - There is a special small index for 2-byte UTF-8, and the initial data
     53  *   entries are designed for fast 1/2-byte UTF-8 lookup.
     54  */
     55 
     56 /**
     57  * Trie structure.
     58  * Use only with public API macros and functions.
     59  */
     60 struct UTrie2;
     61 typedef struct UTrie2 UTrie2;
     62 
     63 /* Public UTrie2 API functions: read-only access ---------------------------- */
     64 
     65 /**
     66  * Selectors for the width of a UTrie2 data value.
     67  */
     68 enum UTrie2ValueBits {
     69     /** 16 bits per UTrie2 data value. */
     70     UTRIE2_16_VALUE_BITS,
     71     /** 32 bits per UTrie2 data value. */
     72     UTRIE2_32_VALUE_BITS,
     73     /** Number of selectors for the width of UTrie2 data values. */
     74     UTRIE2_COUNT_VALUE_BITS
     75 };
     76 typedef enum UTrie2ValueBits UTrie2ValueBits;
     77 
     78 /**
     79  * Open a frozen trie from its serialized from, stored in 32-bit-aligned memory.
     80  * Inverse of utrie2_serialize().
     81  * The memory must remain valid and unchanged as long as the trie is used.
     82  * You must utrie2_close() the trie once you are done using it.
     83  *
     84  * @param valueBits selects the data entry size; results in an
     85  *                  U_INVALID_FORMAT_ERROR if it does not match the serialized form
     86  * @param data a pointer to 32-bit-aligned memory containing the serialized form of a UTrie2
     87  * @param length the number of bytes available at data;
     88  *               can be more than necessary
     89  * @param pActualLength receives the actual number of bytes at data taken up by the trie data;
     90  *                      can be NULL
     91  * @param pErrorCode an in/out ICU UErrorCode
     92  * @return the unserialized trie
     93  *
     94  * @see utrie2_open
     95  * @see utrie2_serialize
     96  */
     97 U_CAPI UTrie2 * U_EXPORT2
     98 utrie2_openFromSerialized(UTrie2ValueBits valueBits,
     99                           const void *data, int32_t length, int32_t *pActualLength,
    100                           UErrorCode *pErrorCode);
    101 
    102 /**
    103  * Open a frozen, empty "dummy" trie.
    104  * A dummy trie is an empty trie, used when a real data trie cannot
    105  * be loaded. Equivalent to calling utrie2_open() and utrie2_freeze(),
    106  * but without internally creating and compacting/serializing the
    107  * builder data structure.
    108  *
    109  * The trie always returns the initialValue,
    110  * or the errorValue for out-of-range code points and illegal UTF-8.
    111  *
    112  * You must utrie2_close() the trie once you are done using it.
    113  *
    114  * @param valueBits selects the data entry size
    115  * @param initialValue the initial value that is set for all code points
    116  * @param errorValue the value for out-of-range code points and illegal UTF-8
    117  * @param pErrorCode an in/out ICU UErrorCode
    118  * @return the dummy trie
    119  *
    120  * @see utrie2_openFromSerialized
    121  * @see utrie2_open
    122  */
    123 U_CAPI UTrie2 * U_EXPORT2
    124 utrie2_openDummy(UTrie2ValueBits valueBits,
    125                  uint32_t initialValue, uint32_t errorValue,
    126                  UErrorCode *pErrorCode);
    127 
    128 /**
    129  * Get a value from a code point as stored in the trie.
    130  * Easier to use than UTRIE2_GET16() and UTRIE2_GET32() but slower.
    131  * Easier to use because, unlike the macros, this function works on all UTrie2
    132  * objects, frozen or not, holding 16-bit or 32-bit data values.
    133  *
    134  * @param trie the trie
    135  * @param c the code point
    136  * @return the value
    137  */
    138 U_CAPI uint32_t U_EXPORT2
    139 utrie2_get32(const UTrie2 *trie, UChar32 c);
    140 
    141 /* enumeration callback types */
    142 
    143 /**
    144  * Callback from utrie2_enum(), extracts a uint32_t value from a
    145  * trie value. This value will be passed on to the UTrie2EnumRange function.
    146  *
    147  * @param context an opaque pointer, as passed into utrie2_enum()
    148  * @param value a value from the trie
    149  * @return the value that is to be passed on to the UTrie2EnumRange function
    150  */
    151 typedef uint32_t U_CALLCONV
    152 UTrie2EnumValue(const void *context, uint32_t value);
    153 
    154 /**
    155  * Callback from utrie2_enum(), is called for each contiguous range
    156  * of code points with the same value as retrieved from the trie and
    157  * transformed by the UTrie2EnumValue function.
    158  *
    159  * The callback function can stop the enumeration by returning FALSE.
    160  *
    161  * @param context an opaque pointer, as passed into utrie2_enum()
    162  * @param start the first code point in a contiguous range with value
    163  * @param end the last code point in a contiguous range with value (inclusive)
    164  * @param value the value that is set for all code points in [start..end]
    165  * @return FALSE to stop the enumeration
    166  */
    167 typedef UBool U_CALLCONV
    168 UTrie2EnumRange(const void *context, UChar32 start, UChar32 end, uint32_t value);
    169 
    170 /**
    171  * Enumerate efficiently all values in a trie.
    172  * Do not modify the trie during the enumeration.
    173  *
    174  * For each entry in the trie, the value to be delivered is passed through
    175  * the UTrie2EnumValue function.
    176  * The value is unchanged if that function pointer is NULL.
    177  *
    178  * For each contiguous range of code points with a given (transformed) value,
    179  * the UTrie2EnumRange function is called.
    180  *
    181  * @param trie a pointer to the trie
    182  * @param enumValue a pointer to a function that may transform the trie entry value,
    183  *                  or NULL if the values from the trie are to be used directly
    184  * @param enumRange a pointer to a function that is called for each contiguous range
    185  *                  of code points with the same (transformed) value
    186  * @param context an opaque pointer that is passed on to the callback functions
    187  */
    188 U_CAPI void U_EXPORT2
    189 utrie2_enum(const UTrie2 *trie,
    190             UTrie2EnumValue *enumValue, UTrie2EnumRange *enumRange, const void *context);
    191 
    192 /* Building a trie ---------------------------------------------------------- */
    193 
    194 /**
    195  * Open an empty, writable trie. At build time, 32-bit data values are used.
    196  * utrie2_freeze() takes a valueBits parameter
    197  * which determines the data value width in the serialized and frozen forms.
    198  * You must utrie2_close() the trie once you are done using it.
    199  *
    200  * @param initialValue the initial value that is set for all code points
    201  * @param errorValue the value for out-of-range code points and illegal UTF-8
    202  * @param pErrorCode an in/out ICU UErrorCode
    203  * @return a pointer to the allocated and initialized new trie
    204  */
    205 U_CAPI UTrie2 * U_EXPORT2
    206 utrie2_open(uint32_t initialValue, uint32_t errorValue, UErrorCode *pErrorCode);
    207 
    208 /**
    209  * Clone a trie.
    210  * You must utrie2_close() the clone once you are done using it.
    211  *
    212  * @param other the trie to clone
    213  * @param pErrorCode an in/out ICU UErrorCode
    214  * @return a pointer to the new trie clone
    215  */
    216 U_CAPI UTrie2 * U_EXPORT2
    217 utrie2_clone(const UTrie2 *other, UErrorCode *pErrorCode);
    218 
    219 /**
    220  * Clone a trie. The clone will be mutable/writable even if the other trie
    221  * is frozen. (See utrie2_freeze().)
    222  * You must utrie2_close() the clone once you are done using it.
    223  *
    224  * @param other the trie to clone
    225  * @param pErrorCode an in/out ICU UErrorCode
    226  * @return a pointer to the new trie clone
    227  */
    228 U_CAPI UTrie2 * U_EXPORT2
    229 utrie2_cloneAsThawed(const UTrie2 *other, UErrorCode *pErrorCode);
    230 
    231 /**
    232  * Close a trie and release associated memory.
    233  *
    234  * @param trie the trie
    235  */
    236 U_CAPI void U_EXPORT2
    237 utrie2_close(UTrie2 *trie);
    238 
    239 /**
    240  * Set a value for a code point.
    241  *
    242  * @param trie the unfrozen trie
    243  * @param c the code point
    244  * @param value the value
    245  * @param pErrorCode an in/out ICU UErrorCode; among other possible error codes:
    246  * - U_NO_WRITE_PERMISSION if the trie is frozen
    247  */
    248 U_CAPI void U_EXPORT2
    249 utrie2_set32(UTrie2 *trie, UChar32 c, uint32_t value, UErrorCode *pErrorCode);
    250 
    251 /**
    252  * Set a value in a range of code points [start..end].
    253  * All code points c with start<=c<=end will get the value if
    254  * overwrite is TRUE or if the old value is the initial value.
    255  *
    256  * @param trie the unfrozen trie
    257  * @param start the first code point to get the value
    258  * @param end the last code point to get the value (inclusive)
    259  * @param value the value
    260  * @param overwrite flag for whether old non-initial values are to be overwritten
    261  * @param pErrorCode an in/out ICU UErrorCode; among other possible error codes:
    262  * - U_NO_WRITE_PERMISSION if the trie is frozen
    263  */
    264 U_CAPI void U_EXPORT2
    265 utrie2_setRange32(UTrie2 *trie,
    266                   UChar32 start, UChar32 end,
    267                   uint32_t value, UBool overwrite,
    268                   UErrorCode *pErrorCode);
    269 
    270 /**
    271  * Freeze a trie. Make it immutable (read-only) and compact it,
    272  * ready for serialization and for use with fast macros.
    273  * Functions to set values will fail after serializing.
    274  *
    275  * A trie can be frozen only once. If this function is called again with different
    276  * valueBits then it will set a U_ILLEGAL_ARGUMENT_ERROR.
    277  *
    278  * @param trie the trie
    279  * @param valueBits selects the data entry size; if smaller than 32 bits, then
    280  *                  the values stored in the trie will be truncated
    281  * @param pErrorCode an in/out ICU UErrorCode; among other possible error codes:
    282  * - U_INDEX_OUTOFBOUNDS_ERROR if the compacted index or data arrays are too long
    283  *                             for serialization
    284  *                             (the trie will be immutable and usable,
    285  *                             but not frozen and not usable with the fast macros)
    286  *
    287  * @see utrie2_cloneAsThawed
    288  */
    289 U_CAPI void U_EXPORT2
    290 utrie2_freeze(UTrie2 *trie, UTrie2ValueBits valueBits, UErrorCode *pErrorCode);
    291 
    292 /**
    293  * Test if the trie is frozen. (See utrie2_freeze().)
    294  *
    295  * @param trie the trie
    296  * @return TRUE if the trie is frozen, that is, immutable, ready for serialization
    297  *         and for use with fast macros
    298  */
    299 U_CAPI UBool U_EXPORT2
    300 utrie2_isFrozen(const UTrie2 *trie);
    301 
    302 /**
    303  * Serialize a frozen trie into 32-bit aligned memory.
    304  * If the trie is not frozen, then the function returns with a U_ILLEGAL_ARGUMENT_ERROR.
    305  * A trie can be serialized multiple times.
    306  *
    307  * @param trie the frozen trie
    308  * @param data a pointer to 32-bit-aligned memory to be filled with the trie data,
    309  *             can be NULL if capacity==0
    310  * @param capacity the number of bytes available at data,
    311  *                 or 0 for preflighting
    312  * @param pErrorCode an in/out ICU UErrorCode; among other possible error codes:
    313  * - U_BUFFER_OVERFLOW_ERROR if the data storage block is too small for serialization
    314  * - U_ILLEGAL_ARGUMENT_ERROR if the trie is not frozen or the data and capacity
    315  *                            parameters are bad
    316  * @return the number of bytes written or needed for the trie
    317  *
    318  * @see utrie2_openFromSerialized()
    319  */
    320 U_CAPI int32_t U_EXPORT2
    321 utrie2_serialize(UTrie2 *trie,
    322                  void *data, int32_t capacity,
    323                  UErrorCode *pErrorCode);
    324 
    325 /* Public UTrie2 API: miscellaneous functions ------------------------------- */
    326 
    327 /**
    328  * Get the UTrie version from 32-bit-aligned memory containing the serialized form
    329  * of either a UTrie (version 1) or a UTrie2 (version 2).
    330  *
    331  * @param data a pointer to 32-bit-aligned memory containing the serialized form
    332  *             of a UTrie, version 1 or 2
    333  * @param length the number of bytes available at data;
    334  *               can be more than necessary (see return value)
    335  * @param anyEndianOk If FALSE, only platform-endian serialized forms are recognized.
    336  *                    If TRUE, opposite-endian serialized forms are recognized as well.
    337  * @return the UTrie version of the serialized form, or 0 if it is not
    338  *         recognized as a serialized UTrie
    339  */
    340 U_CAPI int32_t U_EXPORT2
    341 utrie2_getVersion(const void *data, int32_t length, UBool anyEndianOk);
    342 
    343 /**
    344  * Swap a serialized UTrie2.
    345  * @internal
    346  */
    347 U_CAPI int32_t U_EXPORT2
    348 utrie2_swap(const UDataSwapper *ds,
    349             const void *inData, int32_t length, void *outData,
    350             UErrorCode *pErrorCode);
    351 
    352 /**
    353  * Build a UTrie2 (version 2) from a UTrie (version 1).
    354  * Enumerates all values in the UTrie and builds a UTrie2 with the same values.
    355  * The resulting UTrie2 will be frozen.
    356  *
    357  * @param trie1 the runtime UTrie structure to be enumerated
    358  * @param errorValue the value for out-of-range code points and illegal UTF-8
    359  * @param pErrorCode an in/out ICU UErrorCode
    360  * @return The frozen UTrie2 with the same values as the UTrie.
    361  */
    362 U_CAPI UTrie2 * U_EXPORT2
    363 utrie2_fromUTrie(const UTrie *trie1, uint32_t errorValue, UErrorCode *pErrorCode);
    364 
    365 /* Public UTrie2 API macros ------------------------------------------------- */
    366 
    367 /*
    368  * These macros provide fast data lookup from a frozen trie.
    369  * They will crash when used on an unfrozen trie.
    370  */
    371 
    372 /**
    373  * Return a 16-bit trie value from a code point, with range checking.
    374  * Returns trie->errorValue if c is not in the range 0..U+10ffff.
    375  *
    376  * @param trie (const UTrie2 *, in) a frozen trie
    377  * @param c (UChar32, in) the input code point
    378  * @return (uint16_t) The code point's trie value.
    379  */
    380 #define UTRIE2_GET16(trie, c) _UTRIE2_GET((trie), index, (trie)->indexLength, (c))
    381 
    382 /**
    383  * Return a 32-bit trie value from a code point, with range checking.
    384  * Returns trie->errorValue if c is not in the range 0..U+10ffff.
    385  *
    386  * @param trie (const UTrie2 *, in) a frozen trie
    387  * @param c (UChar32, in) the input code point
    388  * @return (uint32_t) The code point's trie value.
    389  */
    390 #define UTRIE2_GET32(trie, c) _UTRIE2_GET((trie), data32, 0, (c))
    391 
    392 /**
    393  * UTF-16: Get the next code point (UChar32 c, out), post-increment src,
    394  * and get a 16-bit value from the trie.
    395  *
    396  * @param trie (const UTrie2 *, in) a frozen trie
    397  * @param src (const UChar *, in/out) the source text pointer
    398  * @param limit (const UChar *, in) the limit pointer for the text, or NULL if NUL-terminated
    399  * @param c (UChar32, out) variable for the code point
    400  * @param result (uint16_t, out) uint16_t variable for the trie lookup result
    401  */
    402 #define UTRIE2_U16_NEXT16(trie, src, limit, c, result) _UTRIE2_U16_NEXT(trie, index, src, limit, c, result)
    403 
    404 /**
    405  * UTF-16: Get the next code point (UChar32 c, out), post-increment src,
    406  * and get a 32-bit value from the trie.
    407  *
    408  * @param trie (const UTrie2 *, in) a frozen trie
    409  * @param src (const UChar *, in/out) the source text pointer
    410  * @param limit (const UChar *, in) the limit pointer for the text, or NULL if NUL-terminated
    411  * @param c (UChar32, out) variable for the code point
    412  * @param result (uint32_t, out) uint32_t variable for the trie lookup result
    413  */
    414 #define UTRIE2_U16_NEXT32(trie, src, limit, c, result) _UTRIE2_U16_NEXT(trie, data32, src, limit, c, result)
    415 
    416 /**
    417  * UTF-16: Get the previous code point (UChar32 c, out), pre-decrement src,
    418  * and get a 16-bit value from the trie.
    419  *
    420  * @param trie (const UTrie2 *, in) a frozen trie
    421  * @param start (const UChar *, in) the start pointer for the text
    422  * @param src (const UChar *, in/out) the source text pointer
    423  * @param c (UChar32, out) variable for the code point
    424  * @param result (uint16_t, out) uint16_t variable for the trie lookup result
    425  */
    426 #define UTRIE2_U16_PREV16(trie, start, src, c, result) _UTRIE2_U16_PREV(trie, index, start, src, c, result)
    427 
    428 /**
    429  * UTF-16: Get the previous code point (UChar32 c, out), pre-decrement src,
    430  * and get a 32-bit value from the trie.
    431  *
    432  * @param trie (const UTrie2 *, in) a frozen trie
    433  * @param start (const UChar *, in) the start pointer for the text
    434  * @param src (const UChar *, in/out) the source text pointer
    435  * @param c (UChar32, out) variable for the code point
    436  * @param result (uint32_t, out) uint32_t variable for the trie lookup result
    437  */
    438 #define UTRIE2_U16_PREV32(trie, start, src, c, result) _UTRIE2_U16_PREV(trie, data32, start, src, c, result)
    439 
    440 /**
    441  * UTF-8: Post-increment src and get a 16-bit value from the trie.
    442  *
    443  * @param trie (const UTrie2 *, in) a frozen trie
    444  * @param src (const char *, in/out) the source text pointer
    445  * @param limit (const char *, in) the limit pointer for the text (must not be NULL)
    446  * @param result (uint16_t, out) uint16_t variable for the trie lookup result
    447  */
    448 #define UTRIE2_U8_NEXT16(trie, src, limit, result)\
    449     _UTRIE2_U8_NEXT(trie, data16, index, src, limit, result)
    450 
    451 /**
    452  * UTF-8: Post-increment src and get a 32-bit value from the trie.
    453  *
    454  * @param trie (const UTrie2 *, in) a frozen trie
    455  * @param src (const char *, in/out) the source text pointer
    456  * @param limit (const char *, in) the limit pointer for the text (must not be NULL)
    457  * @param result (uint16_t, out) uint32_t variable for the trie lookup result
    458  */
    459 #define UTRIE2_U8_NEXT32(trie, src, limit, result) \
    460     _UTRIE2_U8_NEXT(trie, data32, data32, src, limit, result)
    461 
    462 /**
    463  * UTF-8: Pre-decrement src and get a 16-bit value from the trie.
    464  *
    465  * @param trie (const UTrie2 *, in) a frozen trie
    466  * @param start (const char *, in) the start pointer for the text
    467  * @param src (const char *, in/out) the source text pointer
    468  * @param result (uint16_t, out) uint16_t variable for the trie lookup result
    469  */
    470 #define UTRIE2_U8_PREV16(trie, start, src, result) \
    471     _UTRIE2_U8_PREV(trie, data16, index, start, src, result)
    472 
    473 /**
    474  * UTF-8: Pre-decrement src and get a 32-bit value from the trie.
    475  *
    476  * @param trie (const UTrie2 *, in) a frozen trie
    477  * @param start (const char *, in) the start pointer for the text
    478  * @param src (const char *, in/out) the source text pointer
    479  * @param result (uint16_t, out) uint32_t variable for the trie lookup result
    480  */
    481 #define UTRIE2_U8_PREV32(trie, start, src, result) \
    482     _UTRIE2_U8_PREV(trie, data32, data32, start, src, result)
    483 
    484 /* Public UTrie2 API: optimized UTF-16 access ------------------------------- */
    485 
    486 /*
    487  * The following functions and macros are used for highly optimized UTF-16
    488  * text processing. The UTRIE2_U16_NEXTxy() macros do not depend on these.
    489  *
    490  * A UTrie2 stores separate values for lead surrogate code _units_ vs. code _points_.
    491  * UTF-16 text processing can be optimized by detecting surrogate pairs and
    492  * assembling supplementary code points only when there is non-trivial data
    493  * available.
    494  *
    495  * At build-time, use utrie2_enumForLeadSurrogate() to see if there
    496  * is non-trivial (non-initialValue) data for any of the supplementary
    497  * code points associated with a lead surrogate.
    498  * If so, then set a special (application-specific) value for the
    499  * lead surrogate code _unit_, with utrie2_set32ForLeadSurrogateCodeUnit().
    500  *
    501  * At runtime, use UTRIE2_GET16_FROM_U16_SINGLE_LEAD() or
    502  * UTRIE2_GET32_FROM_U16_SINGLE_LEAD() per code unit. If there is non-trivial
    503  * data and the code unit is a lead surrogate, then check if a trail surrogate
    504  * follows. If so, assemble the supplementary code point with
    505  * U16_GET_SUPPLEMENTARY() and look up its value with UTRIE2_GET16_FROM_SUPP()
    506  * or UTRIE2_GET32_FROM_SUPP(); otherwise reset the lead
    507  * surrogate's value or do a code point lookup for it.
    508  *
    509  * If there is only trivial data for lead and trail surrogates, then processing
    510  * can often skip them. For example, in normalization or case mapping
    511  * all characters that do not have any mappings are simply copied as is.
    512  */
    513 
    514 /**
    515  * Get a value from a lead surrogate code unit as stored in the trie.
    516  *
    517  * @param trie the trie
    518  * @param c the code unit (U+D800..U+DBFF)
    519  * @return the value
    520  */
    521 U_CAPI uint32_t U_EXPORT2
    522 utrie2_get32FromLeadSurrogateCodeUnit(const UTrie2 *trie, UChar32 c);
    523 
    524 /**
    525  * Enumerate the trie values for the 1024=0x400 code points
    526  * corresponding to a given lead surrogate.
    527  * For example, for the lead surrogate U+D87E it will enumerate the values
    528  * for [U+2F800..U+2FC00[.
    529  * Used by data builder code that sets special lead surrogate code unit values
    530  * for optimized UTF-16 string processing.
    531  *
    532  * Do not modify the trie during the enumeration.
    533  *
    534  * Except for the limited code point range, this functions just like utrie2_enum():
    535  * For each entry in the trie, the value to be delivered is passed through
    536  * the UTrie2EnumValue function.
    537  * The value is unchanged if that function pointer is NULL.
    538  *
    539  * For each contiguous range of code points with a given (transformed) value,
    540  * the UTrie2EnumRange function is called.
    541  *
    542  * @param trie a pointer to the trie
    543  * @param enumValue a pointer to a function that may transform the trie entry value,
    544  *                  or NULL if the values from the trie are to be used directly
    545  * @param enumRange a pointer to a function that is called for each contiguous range
    546  *                  of code points with the same (transformed) value
    547  * @param context an opaque pointer that is passed on to the callback functions
    548  */
    549 U_CAPI void U_EXPORT2
    550 utrie2_enumForLeadSurrogate(const UTrie2 *trie, UChar32 lead,
    551                             UTrie2EnumValue *enumValue, UTrie2EnumRange *enumRange,
    552                             const void *context);
    553 
    554 /**
    555  * Set a value for a lead surrogate code unit.
    556  *
    557  * @param trie the unfrozen trie
    558  * @param lead the lead surrogate code unit (U+D800..U+DBFF)
    559  * @param value the value
    560  * @param pErrorCode an in/out ICU UErrorCode; among other possible error codes:
    561  * - U_NO_WRITE_PERMISSION if the trie is frozen
    562  */
    563 U_CAPI void U_EXPORT2
    564 utrie2_set32ForLeadSurrogateCodeUnit(UTrie2 *trie,
    565                                      UChar32 lead, uint32_t value,
    566                                      UErrorCode *pErrorCode);
    567 
    568 /**
    569  * Return a 16-bit trie value from a UTF-16 single/lead code unit (<=U+ffff).
    570  * Same as UTRIE2_GET16() if c is a BMP code point except for lead surrogates,
    571  * but smaller and faster.
    572  *
    573  * @param trie (const UTrie2 *, in) a frozen trie
    574  * @param c (UChar32, in) the input code unit, must be 0<=c<=U+ffff
    575  * @return (uint16_t) The code unit's trie value.
    576  */
    577 #define UTRIE2_GET16_FROM_U16_SINGLE_LEAD(trie, c) _UTRIE2_GET_FROM_U16_SINGLE_LEAD((trie), index, c)
    578 
    579 /**
    580  * Return a 32-bit trie value from a UTF-16 single/lead code unit (<=U+ffff).
    581  * Same as UTRIE2_GET32() if c is a BMP code point except for lead surrogates,
    582  * but smaller and faster.
    583  *
    584  * @param trie (const UTrie2 *, in) a frozen trie
    585  * @param c (UChar32, in) the input code unit, must be 0<=c<=U+ffff
    586  * @return (uint32_t) The code unit's trie value.
    587  */
    588 #define UTRIE2_GET32_FROM_U16_SINGLE_LEAD(trie, c) _UTRIE2_GET_FROM_U16_SINGLE_LEAD((trie), data32, c)
    589 
    590 /**
    591  * Return a 16-bit trie value from a supplementary code point (U+10000..U+10ffff).
    592  *
    593  * @param trie (const UTrie2 *, in) a frozen trie
    594  * @param c (UChar32, in) the input code point, must be U+10000<=c<=U+10ffff
    595  * @return (uint16_t) The code point's trie value.
    596  */
    597 #define UTRIE2_GET16_FROM_SUPP(trie, c) _UTRIE2_GET_FROM_SUPP((trie), index, c)
    598 
    599 /**
    600  * Return a 32-bit trie value from a supplementary code point (U+10000..U+10ffff).
    601  *
    602  * @param trie (const UTrie2 *, in) a frozen trie
    603  * @param c (UChar32, in) the input code point, must be U+10000<=c<=U+10ffff
    604  * @return (uint32_t) The code point's trie value.
    605  */
    606 #define UTRIE2_GET32_FROM_SUPP(trie, c) _UTRIE2_GET_FROM_SUPP((trie), data32, c)
    607 
    608 /* Internal definitions ----------------------------------------------------- */
    609 
    610 /** Build-time trie structure. */
    611 struct UNewTrie2;
    612 typedef struct UNewTrie2 UNewTrie2;
    613 
    614 /*
    615  * Trie structure definition.
    616  *
    617  * Either the data table is 16 bits wide and accessed via the index
    618  * pointer, with each index item increased by indexLength;
    619  * in this case, data32==NULL, and data16 is used for direct ASCII access.
    620  *
    621  * Or the data table is 32 bits wide and accessed via the data32 pointer.
    622  */
    623 struct UTrie2 {
    624     /* protected: used by macros and functions for reading values */
    625     const uint16_t *index;
    626     const uint16_t *data16;     /* for fast UTF-8 ASCII access, if 16b data */
    627     const uint32_t *data32;     /* NULL if 16b data is used via index */
    628 
    629     int32_t indexLength, dataLength;
    630     uint16_t index2NullOffset;  /* 0xffff if there is no dedicated index-2 null block */
    631     uint16_t dataNullOffset;
    632     uint32_t initialValue;
    633     /** Value returned for out-of-range code points and illegal UTF-8. */
    634     uint32_t errorValue;
    635 
    636     /* Start of the last range which ends at U+10ffff, and its value. */
    637     UChar32 highStart;
    638     int32_t highValueIndex;
    639 
    640     /* private: used by builder and unserialization functions */
    641     void *memory;           /* serialized bytes; NULL if not frozen yet */
    642     int32_t length;         /* number of serialized bytes at memory; 0 if not frozen yet */
    643     UBool isMemoryOwned;    /* TRUE if the trie owns the memory */
    644     UBool padding1;
    645     int16_t padding2;
    646     UNewTrie2 *newTrie;     /* builder object; NULL when frozen */
    647 };
    648 
    649 /**
    650  * Trie constants, defining shift widths, index array lengths, etc.
    651  *
    652  * These are needed for the runtime macros but users can treat these as
    653  * implementation details and skip to the actual public API further below.
    654  */
    655 enum {
    656     /** Shift size for getting the index-1 table offset. */
    657     UTRIE2_SHIFT_1=6+5,
    658 
    659     /** Shift size for getting the index-2 table offset. */
    660     UTRIE2_SHIFT_2=5,
    661 
    662     /**
    663      * Difference between the two shift sizes,
    664      * for getting an index-1 offset from an index-2 offset. 6=11-5
    665      */
    666     UTRIE2_SHIFT_1_2=UTRIE2_SHIFT_1-UTRIE2_SHIFT_2,
    667 
    668     /**
    669      * Number of index-1 entries for the BMP. 32=0x20
    670      * This part of the index-1 table is omitted from the serialized form.
    671      */
    672     UTRIE2_OMITTED_BMP_INDEX_1_LENGTH=0x10000>>UTRIE2_SHIFT_1,
    673 
    674     /** Number of code points per index-1 table entry. 2048=0x800 */
    675     UTRIE2_CP_PER_INDEX_1_ENTRY=1<<UTRIE2_SHIFT_1,
    676 
    677     /** Number of entries in an index-2 block. 64=0x40 */
    678     UTRIE2_INDEX_2_BLOCK_LENGTH=1<<UTRIE2_SHIFT_1_2,
    679 
    680     /** Mask for getting the lower bits for the in-index-2-block offset. */
    681     UTRIE2_INDEX_2_MASK=UTRIE2_INDEX_2_BLOCK_LENGTH-1,
    682 
    683     /** Number of entries in a data block. 32=0x20 */
    684     UTRIE2_DATA_BLOCK_LENGTH=1<<UTRIE2_SHIFT_2,
    685 
    686     /** Mask for getting the lower bits for the in-data-block offset. */
    687     UTRIE2_DATA_MASK=UTRIE2_DATA_BLOCK_LENGTH-1,
    688 
    689     /**
    690      * Shift size for shifting left the index array values.
    691      * Increases possible data size with 16-bit index values at the cost
    692      * of compactability.
    693      * This requires data blocks to be aligned by UTRIE2_DATA_GRANULARITY.
    694      */
    695     UTRIE2_INDEX_SHIFT=2,
    696 
    697     /** The alignment size of a data block. Also the granularity for compaction. */
    698     UTRIE2_DATA_GRANULARITY=1<<UTRIE2_INDEX_SHIFT,
    699 
    700     /* Fixed layout of the first part of the index array. ------------------- */
    701 
    702     /**
    703      * The BMP part of the index-2 table is fixed and linear and starts at offset 0.
    704      * Length=2048=0x800=0x10000>>UTRIE2_SHIFT_2.
    705      */
    706     UTRIE2_INDEX_2_OFFSET=0,
    707 
    708     /**
    709      * The part of the index-2 table for U+D800..U+DBFF stores values for
    710      * lead surrogate code _units_ not code _points_.
    711      * Values for lead surrogate code _points_ are indexed with this portion of the table.
    712      * Length=32=0x20=0x400>>UTRIE2_SHIFT_2. (There are 1024=0x400 lead surrogates.)
    713      */
    714     UTRIE2_LSCP_INDEX_2_OFFSET=0x10000>>UTRIE2_SHIFT_2,
    715     UTRIE2_LSCP_INDEX_2_LENGTH=0x400>>UTRIE2_SHIFT_2,
    716 
    717     /** Count the lengths of both BMP pieces. 2080=0x820 */
    718     UTRIE2_INDEX_2_BMP_LENGTH=UTRIE2_LSCP_INDEX_2_OFFSET+UTRIE2_LSCP_INDEX_2_LENGTH,
    719 
    720     /**
    721      * The 2-byte UTF-8 version of the index-2 table follows at offset 2080=0x820.
    722      * Length 32=0x20 for lead bytes C0..DF, regardless of UTRIE2_SHIFT_2.
    723      */
    724     UTRIE2_UTF8_2B_INDEX_2_OFFSET=UTRIE2_INDEX_2_BMP_LENGTH,
    725     UTRIE2_UTF8_2B_INDEX_2_LENGTH=0x800>>6,  /* U+0800 is the first code point after 2-byte UTF-8 */
    726 
    727     /**
    728      * The index-1 table, only used for supplementary code points, at offset 2112=0x840.
    729      * Variable length, for code points up to highStart, where the last single-value range starts.
    730      * Maximum length 512=0x200=0x100000>>UTRIE2_SHIFT_1.
    731      * (For 0x100000 supplementary code points U+10000..U+10ffff.)
    732      *
    733      * The part of the index-2 table for supplementary code points starts
    734      * after this index-1 table.
    735      *
    736      * Both the index-1 table and the following part of the index-2 table
    737      * are omitted completely if there is only BMP data.
    738      */
    739     UTRIE2_INDEX_1_OFFSET=UTRIE2_UTF8_2B_INDEX_2_OFFSET+UTRIE2_UTF8_2B_INDEX_2_LENGTH,
    740     UTRIE2_MAX_INDEX_1_LENGTH=0x100000>>UTRIE2_SHIFT_1,
    741 
    742     /*
    743      * Fixed layout of the first part of the data array. -----------------------
    744      * Starts with 4 blocks (128=0x80 entries) for ASCII.
    745      */
    746 
    747     /**
    748      * The illegal-UTF-8 data block follows the ASCII block, at offset 128=0x80.
    749      * Used with linear access for single bytes 0..0xbf for simple error handling.
    750      * Length 64=0x40, not UTRIE2_DATA_BLOCK_LENGTH.
    751      */
    752     UTRIE2_BAD_UTF8_DATA_OFFSET=0x80,
    753 
    754     /** The start of non-linear-ASCII data blocks, at offset 192=0xc0. */
    755     UTRIE2_DATA_START_OFFSET=0xc0
    756 };
    757 
    758 /* Internal functions and macros -------------------------------------------- */
    759 
    760 /**
    761  * Internal function for part of the UTRIE2_U8_NEXTxx() macro implementations.
    762  * Do not call directly.
    763  * @internal
    764  */
    765 U_INTERNAL int32_t U_EXPORT2
    766 utrie2_internalU8NextIndex(const UTrie2 *trie, UChar32 c,
    767                            const uint8_t *src, const uint8_t *limit);
    768 
    769 /**
    770  * Internal function for part of the UTRIE2_U8_PREVxx() macro implementations.
    771  * Do not call directly.
    772  * @internal
    773  */
    774 U_INTERNAL int32_t U_EXPORT2
    775 utrie2_internalU8PrevIndex(const UTrie2 *trie, UChar32 c,
    776                            const uint8_t *start, const uint8_t *src);
    777 
    778 
    779 /** Internal low-level trie getter. Returns a data index. */
    780 #define _UTRIE2_INDEX_RAW(offset, trieIndex, c) \
    781     (((int32_t)((trieIndex)[(offset)+((c)>>UTRIE2_SHIFT_2)]) \
    782     <<UTRIE2_INDEX_SHIFT)+ \
    783     ((c)&UTRIE2_DATA_MASK))
    784 
    785 /** Internal trie getter from a UTF-16 single/lead code unit. Returns the data index. */
    786 #define _UTRIE2_INDEX_FROM_U16_SINGLE_LEAD(trieIndex, c) _UTRIE2_INDEX_RAW(0, trieIndex, c)
    787 
    788 /** Internal trie getter from a lead surrogate code point (D800..DBFF). Returns the data index. */
    789 #define _UTRIE2_INDEX_FROM_LSCP(trieIndex, c) \
    790     _UTRIE2_INDEX_RAW(UTRIE2_LSCP_INDEX_2_OFFSET-(0xd800>>UTRIE2_SHIFT_2), trieIndex, c)
    791 
    792 /** Internal trie getter from a BMP code point. Returns the data index. */
    793 #define _UTRIE2_INDEX_FROM_BMP(trieIndex, c) \
    794     _UTRIE2_INDEX_RAW(U_IS_LEAD(c) ? UTRIE2_LSCP_INDEX_2_OFFSET-(0xd800>>UTRIE2_SHIFT_2) : 0, \
    795                       trieIndex, c)
    796 
    797 /** Internal trie getter from a supplementary code point below highStart. Returns the data index. */
    798 #define _UTRIE2_INDEX_FROM_SUPP(trieIndex, c) \
    799     (((int32_t)((trieIndex)[ \
    800         (trieIndex)[(UTRIE2_INDEX_1_OFFSET-UTRIE2_OMITTED_BMP_INDEX_1_LENGTH)+ \
    801                       ((c)>>UTRIE2_SHIFT_1)]+ \
    802         (((c)>>UTRIE2_SHIFT_2)&UTRIE2_INDEX_2_MASK)]) \
    803     <<UTRIE2_INDEX_SHIFT)+ \
    804     ((c)&UTRIE2_DATA_MASK))
    805 
    806 /**
    807  * Internal trie getter from a code point, with checking that c is in 0..10FFFF.
    808  * Returns the data index.
    809  */
    810 #define _UTRIE2_INDEX_FROM_CP(trie, asciiOffset, c) \
    811     ((uint32_t)(c)<0xd800 ? \
    812         _UTRIE2_INDEX_RAW(0, (trie)->index, c) : \
    813         (uint32_t)(c)<=0xffff ? \
    814             _UTRIE2_INDEX_RAW( \
    815                 (c)<=0xdbff ? UTRIE2_LSCP_INDEX_2_OFFSET-(0xd800>>UTRIE2_SHIFT_2) : 0, \
    816                 (trie)->index, c) : \
    817             (uint32_t)(c)>0x10ffff ? \
    818                 (asciiOffset)+UTRIE2_BAD_UTF8_DATA_OFFSET : \
    819                 (c)>=(trie)->highStart ? \
    820                     (trie)->highValueIndex : \
    821                     _UTRIE2_INDEX_FROM_SUPP((trie)->index, c))
    822 
    823 /** Internal trie getter from a UTF-16 single/lead code unit. Returns the data. */
    824 #define _UTRIE2_GET_FROM_U16_SINGLE_LEAD(trie, data, c) \
    825     (trie)->data[_UTRIE2_INDEX_FROM_U16_SINGLE_LEAD((trie)->index, c)]
    826 
    827 /** Internal trie getter from a supplementary code point. Returns the data. */
    828 #define _UTRIE2_GET_FROM_SUPP(trie, data, c) \
    829     (trie)->data[(c)>=(trie)->highStart ? (trie)->highValueIndex : \
    830                  _UTRIE2_INDEX_FROM_SUPP((trie)->index, c)]
    831 
    832 /**
    833  * Internal trie getter from a code point, with checking that c is in 0..10FFFF.
    834  * Returns the data.
    835  */
    836 #define _UTRIE2_GET(trie, data, asciiOffset, c) \
    837     (trie)->data[_UTRIE2_INDEX_FROM_CP(trie, asciiOffset, c)]
    838 
    839 /** Internal next-post-increment: get the next code point (c) and its data. */
    840 #define _UTRIE2_U16_NEXT(trie, data, src, limit, c, result) { \
    841     { \
    842         uint16_t __c2; \
    843         (c)=*(src)++; \
    844         if(!U16_IS_LEAD(c)) { \
    845             (result)=_UTRIE2_GET_FROM_U16_SINGLE_LEAD(trie, data, c); \
    846         } else if((src)==(limit) || !U16_IS_TRAIL(__c2=*(src))) { \
    847             (result)=(trie)->data[_UTRIE2_INDEX_FROM_LSCP((trie)->index, c)]; \
    848         } else { \
    849             ++(src); \
    850             (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
    851             (result)=_UTRIE2_GET_FROM_SUPP((trie), data, (c)); \
    852         } \
    853     } \
    854 }
    855 
    856 /** Internal pre-decrement-previous: get the previous code point (c) and its data */
    857 #define _UTRIE2_U16_PREV(trie, data, start, src, c, result) { \
    858     { \
    859         uint16_t __c2; \
    860         (c)=*--(src); \
    861         if(!U16_IS_TRAIL(c) || (src)==(start) || !U16_IS_LEAD(__c2=*((src)-1))) { \
    862             (result)=(trie)->data[_UTRIE2_INDEX_FROM_BMP((trie)->index, c)]; \
    863         } else { \
    864             --(src); \
    865             (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
    866             (result)=_UTRIE2_GET_FROM_SUPP((trie), data, (c)); \
    867         } \
    868     } \
    869 }
    870 
    871 /** Internal UTF-8 next-post-increment: get the next code point's data. */
    872 #define _UTRIE2_U8_NEXT(trie, ascii, data, src, limit, result) { \
    873     uint8_t __lead=(uint8_t)*(src)++; \
    874     if(__lead<0xc0) { \
    875         (result)=(trie)->ascii[__lead]; \
    876     } else { \
    877         uint8_t __t1, __t2; \
    878         if( /* handle U+0000..U+07FF inline */ \
    879             __lead<0xe0 && (src)<(limit) && \
    880             (__t1=(uint8_t)(*(src)-0x80))<=0x3f \
    881         ) { \
    882             ++(src); \
    883             (result)=(trie)->data[ \
    884                 (trie)->index[(UTRIE2_UTF8_2B_INDEX_2_OFFSET-0xc0)+__lead]+ \
    885                 __t1]; \
    886         } else if( /* handle U+0000..U+CFFF inline */ \
    887             __lead<0xed && ((src)+1)<(limit) && \
    888             (__t1=(uint8_t)(*(src)-0x80))<=0x3f && (__lead>0xe0 || __t1>=0x20) && \
    889             (__t2=(uint8_t)(*((src)+1)-0x80))<= 0x3f \
    890         ) { \
    891             (src)+=2; \
    892             (result)=(trie)->data[ \
    893                 ((int32_t)((trie)->index[((__lead-0xe0)<<(12-UTRIE2_SHIFT_2))+ \
    894                                          (__t1<<(6-UTRIE2_SHIFT_2))+(__t2>>UTRIE2_SHIFT_2)]) \
    895                 <<UTRIE2_INDEX_SHIFT)+ \
    896                 (__t2&UTRIE2_DATA_MASK)]; \
    897         } else { \
    898             int32_t __index=utrie2_internalU8NextIndex((trie), __lead, (const uint8_t *)(src), \
    899                                                                        (const uint8_t *)(limit)); \
    900             (src)+=__index&7; \
    901             (result)=(trie)->data[__index>>3]; \
    902         } \
    903     } \
    904 }
    905 
    906 /** Internal UTF-8 pre-decrement-previous: get the previous code point's data. */
    907 #define _UTRIE2_U8_PREV(trie, ascii, data, start, src, result) { \
    908     uint8_t __b=(uint8_t)*--(src); \
    909     if(__b<0x80) { \
    910         (result)=(trie)->ascii[__b]; \
    911     } else { \
    912         int32_t __index=utrie2_internalU8PrevIndex((trie), __b, (const uint8_t *)(start), \
    913                                                                 (const uint8_t *)(src)); \
    914         (src)-=__index&7; \
    915         (result)=(trie)->data[__index>>3]; \
    916     } \
    917 }
    918 
    919 U_CDECL_END
    920 
    921 /**
    922  * Work around MSVC 2003 optimization bugs.
    923  */
    924 #if defined (U_HAVE_MSVC_2003_OR_EARLIER)
    925 #pragma optimize("", off)
    926 #endif
    927 
    928 #endif
    929