os/textandloc/fontservices/textshaperplugin/IcuSource/common/utrie.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
******************************************************************************
sl@0
     3
*
sl@0
     4
*   Copyright (C) 2001-2005, International Business Machines
sl@0
     5
*   Corporation and others.  All Rights Reserved.
sl@0
     6
*
sl@0
     7
******************************************************************************
sl@0
     8
*   file name:  utrie.h
sl@0
     9
*   encoding:   US-ASCII
sl@0
    10
*   tab size:   8 (not used)
sl@0
    11
*   indentation:4
sl@0
    12
*
sl@0
    13
*   created on: 2001nov08
sl@0
    14
*   created by: Markus W. Scherer
sl@0
    15
*/
sl@0
    16
sl@0
    17
#ifndef __UTRIE_H__
sl@0
    18
#define __UTRIE_H__
sl@0
    19
sl@0
    20
#include "unicode/utypes.h"
sl@0
    21
#include "udataswp.h"
sl@0
    22
sl@0
    23
U_CDECL_BEGIN
sl@0
    24
sl@0
    25
/**
sl@0
    26
 * \file
sl@0
    27
 *
sl@0
    28
 * This is a common implementation of a "folded" trie.
sl@0
    29
 * It is a kind of compressed, serializable table of 16- or 32-bit values associated with
sl@0
    30
 * Unicode code points (0..0x10ffff).
sl@0
    31
 *
sl@0
    32
 * This implementation is optimized for getting values while walking forward
sl@0
    33
 * through a UTF-16 string.
sl@0
    34
 * Therefore, the simplest and fastest access macros are the
sl@0
    35
 * _FROM_LEAD() and _FROM_OFFSET_TRAIL() macros.
sl@0
    36
 *
sl@0
    37
 * The _FROM_BMP() macros are a little more complicated; they get values
sl@0
    38
 * even for lead surrogate code _points_, while the _FROM_LEAD() macros
sl@0
    39
 * get special "folded" values for lead surrogate code _units_ if
sl@0
    40
 * there is relevant data associated with them.
sl@0
    41
 * From such a folded value, an offset needs to be extracted to supply
sl@0
    42
 * to the _FROM_OFFSET_TRAIL() macros.
sl@0
    43
 *
sl@0
    44
 * Most of the more complex (and more convenient) functions/macros call a callback function
sl@0
    45
 * to get that offset from the folded value for a lead surrogate unit.
sl@0
    46
 */
sl@0
    47
sl@0
    48
/**
sl@0
    49
 * Trie constants, defining shift widths, index array lengths, etc.
sl@0
    50
 */
sl@0
    51
enum {
sl@0
    52
    /** Shift size for shifting right the input index. 1..9 */
sl@0
    53
    UTRIE_SHIFT=5,
sl@0
    54
sl@0
    55
    /** Number of data values in a stage 2 (data array) block. 2, 4, 8, .., 0x200 */
sl@0
    56
    UTRIE_DATA_BLOCK_LENGTH=1<<UTRIE_SHIFT,
sl@0
    57
sl@0
    58
    /** Mask for getting the lower bits from the input index. */
sl@0
    59
    UTRIE_MASK=UTRIE_DATA_BLOCK_LENGTH-1,
sl@0
    60
sl@0
    61
    /**
sl@0
    62
     * Lead surrogate code points' index displacement in the index array.
sl@0
    63
     * 0x10000-0xd800=0x2800
sl@0
    64
     */
sl@0
    65
    UTRIE_LEAD_INDEX_DISP=0x2800>>UTRIE_SHIFT,
sl@0
    66
sl@0
    67
    /**
sl@0
    68
     * Shift size for shifting left the index array values.
sl@0
    69
     * Increases possible data size with 16-bit index values at the cost
sl@0
    70
     * of compactability.
sl@0
    71
     * This requires blocks of stage 2 data to be aligned by UTRIE_DATA_GRANULARITY.
sl@0
    72
     * 0..UTRIE_SHIFT
sl@0
    73
     */
sl@0
    74
    UTRIE_INDEX_SHIFT=2,
sl@0
    75
sl@0
    76
    /** The alignment size of a stage 2 data block. Also the granularity for compaction. */
sl@0
    77
    UTRIE_DATA_GRANULARITY=1<<UTRIE_INDEX_SHIFT,
sl@0
    78
sl@0
    79
    /** Number of bits of a trail surrogate that are used in index table lookups. */
sl@0
    80
    UTRIE_SURROGATE_BLOCK_BITS=10-UTRIE_SHIFT,
sl@0
    81
sl@0
    82
    /**
sl@0
    83
     * Number of index (stage 1) entries per lead surrogate.
sl@0
    84
     * Same as number of index entries for 1024 trail surrogates,
sl@0
    85
     * ==0x400>>UTRIE_SHIFT
sl@0
    86
     */
sl@0
    87
    UTRIE_SURROGATE_BLOCK_COUNT=(1<<UTRIE_SURROGATE_BLOCK_BITS),
sl@0
    88
sl@0
    89
    /** Length of the BMP portion of the index (stage 1) array. */
sl@0
    90
    UTRIE_BMP_INDEX_LENGTH=0x10000>>UTRIE_SHIFT
sl@0
    91
};
sl@0
    92
sl@0
    93
/**
sl@0
    94
 * Length of the index (stage 1) array before folding.
sl@0
    95
 * Maximum number of Unicode code points (0x110000) shifted right by UTRIE_SHIFT.
sl@0
    96
 */
sl@0
    97
#define UTRIE_MAX_INDEX_LENGTH (0x110000>>UTRIE_SHIFT)
sl@0
    98
sl@0
    99
/**
sl@0
   100
 * Maximum length of the runtime data (stage 2) array.
sl@0
   101
 * Limited by 16-bit index values that are left-shifted by UTRIE_INDEX_SHIFT.
sl@0
   102
 */
sl@0
   103
#define UTRIE_MAX_DATA_LENGTH (0x10000<<UTRIE_INDEX_SHIFT)
sl@0
   104
sl@0
   105
/**
sl@0
   106
 * Maximum length of the build-time data (stage 2) array.
sl@0
   107
 * The maximum length is 0x110000+UTRIE_DATA_BLOCK_LENGTH+0x400.
sl@0
   108
 * (Number of Unicode code points + one all-initial-value block +
sl@0
   109
 *  possible duplicate entries for 1024 lead surrogates.)
sl@0
   110
 */
sl@0
   111
#define UTRIE_MAX_BUILD_TIME_DATA_LENGTH (0x110000+UTRIE_DATA_BLOCK_LENGTH+0x400)
sl@0
   112
sl@0
   113
/**
sl@0
   114
 * Number of bytes for a dummy trie.
sl@0
   115
 * A dummy trie is an empty runtime trie, used when a real data trie cannot
sl@0
   116
 * be loaded.
sl@0
   117
 * The number of bytes works for Latin-1-linear tries with 32-bit data
sl@0
   118
 * (worst case).
sl@0
   119
 *
sl@0
   120
 * Calculation:
sl@0
   121
 *   BMP index + 1 index block for lead surrogate code points +
sl@0
   122
 *   Latin-1-linear array + 1 data block for lead surrogate code points
sl@0
   123
 *
sl@0
   124
 * Latin-1: if(UTRIE_SHIFT<=8) { 256 } else { included in first data block }
sl@0
   125
 *
sl@0
   126
 * @see utrie_unserializeDummy
sl@0
   127
 */
sl@0
   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)
sl@0
   129
sl@0
   130
/**
sl@0
   131
 * Runtime UTrie callback function.
sl@0
   132
 * Extract from a lead surrogate's data the
sl@0
   133
 * index array offset of the indexes for that lead surrogate.
sl@0
   134
 *
sl@0
   135
 * @param data data value for a surrogate from the trie, including the folding offset
sl@0
   136
 * @return offset>=UTRIE_BMP_INDEX_LENGTH, or 0 if there is no data for the lead surrogate
sl@0
   137
 */
sl@0
   138
typedef int32_t U_CALLCONV
sl@0
   139
UTrieGetFoldingOffset(uint32_t data);
sl@0
   140
sl@0
   141
/**
sl@0
   142
 * Run-time Trie structure.
sl@0
   143
 *
sl@0
   144
 * Either the data table is 16 bits wide and accessed via the index
sl@0
   145
 * pointer, with each index item increased by indexLength;
sl@0
   146
 * in this case, data32==NULL.
sl@0
   147
 *
sl@0
   148
 * Or the data table is 32 bits wide and accessed via the data32 pointer.
sl@0
   149
 */
sl@0
   150
struct UTrie {
sl@0
   151
    const uint16_t *index;
sl@0
   152
    const uint32_t *data32; /* NULL if 16b data is used via index */
sl@0
   153
sl@0
   154
    /**
sl@0
   155
     * This function is not used in _FROM_LEAD, _FROM_BMP, and _FROM_OFFSET_TRAIL macros.
sl@0
   156
     * If convenience macros like _GET16 or _NEXT32 are used, this function must be set.
sl@0
   157
     *
sl@0
   158
     * utrie_unserialize() sets a default function which simply returns
sl@0
   159
     * the lead surrogate's value itself - which is the inverse of the default
sl@0
   160
     * folding function used by utrie_serialize().
sl@0
   161
     *
sl@0
   162
     * @see UTrieGetFoldingOffset
sl@0
   163
     */
sl@0
   164
    UTrieGetFoldingOffset *getFoldingOffset;
sl@0
   165
sl@0
   166
    int32_t indexLength, dataLength;
sl@0
   167
    uint32_t initialValue;
sl@0
   168
    UBool isLatin1Linear;
sl@0
   169
};
sl@0
   170
sl@0
   171
typedef struct UTrie UTrie;
sl@0
   172
sl@0
   173
/** Internal trie getter from an offset (0 if c16 is a BMP/lead units) and a 16-bit unit */
sl@0
   174
#define _UTRIE_GET_RAW(trie, data, offset, c16) \
sl@0
   175
    (trie)->data[ \
sl@0
   176
        ((int32_t)((trie)->index[(offset)+((c16)>>UTRIE_SHIFT)])<<UTRIE_INDEX_SHIFT)+ \
sl@0
   177
        ((c16)&UTRIE_MASK) \
sl@0
   178
    ]
sl@0
   179
sl@0
   180
/** Internal trie getter from a pair of surrogates */
sl@0
   181
#define _UTRIE_GET_FROM_PAIR(trie, data, c, c2, result, resultType) { \
sl@0
   182
    int32_t __offset; \
sl@0
   183
\
sl@0
   184
    /* get data for lead surrogate */ \
sl@0
   185
    (result)=_UTRIE_GET_RAW((trie), data, 0, (c)); \
sl@0
   186
    __offset=(trie)->getFoldingOffset(result); \
sl@0
   187
\
sl@0
   188
    /* get the real data from the folded lead/trail units */ \
sl@0
   189
    if(__offset>0) { \
sl@0
   190
        (result)=_UTRIE_GET_RAW((trie), data, __offset, (c2)&0x3ff); \
sl@0
   191
    } else { \
sl@0
   192
        (result)=(resultType)((trie)->initialValue); \
sl@0
   193
    } \
sl@0
   194
}
sl@0
   195
sl@0
   196
/** Internal trie getter from a BMP code point, treating a lead surrogate as a normal code point */
sl@0
   197
#define _UTRIE_GET_FROM_BMP(trie, data, c16) \
sl@0
   198
    _UTRIE_GET_RAW(trie, data, 0xd800<=(c16) && (c16)<=0xdbff ? UTRIE_LEAD_INDEX_DISP : 0, c16);
sl@0
   199
sl@0
   200
/**
sl@0
   201
 * Internal trie getter from a code point.
sl@0
   202
 * Could be faster(?) but longer with
sl@0
   203
 *   if((c32)<=0xd7ff) { (result)=_UTRIE_GET_RAW(trie, data, 0, c32); }
sl@0
   204
 */
sl@0
   205
#define _UTRIE_GET(trie, data, c32, result, resultType) \
sl@0
   206
    if((uint32_t)(c32)<=0xffff) { \
sl@0
   207
        /* BMP code points */ \
sl@0
   208
        (result)=_UTRIE_GET_FROM_BMP(trie, data, c32); \
sl@0
   209
    } else if((uint32_t)(c32)<=0x10ffff) { \
sl@0
   210
        /* supplementary code point */ \
sl@0
   211
        UChar __lead16=UTF16_LEAD(c32); \
sl@0
   212
        _UTRIE_GET_FROM_PAIR(trie, data, __lead16, c32, result, resultType); \
sl@0
   213
    } else { \
sl@0
   214
        /* out of range */ \
sl@0
   215
        (result)=(resultType)((trie)->initialValue); \
sl@0
   216
    }
sl@0
   217
sl@0
   218
/** Internal next-post-increment: get the next code point (c, c2) and its data */
sl@0
   219
#define _UTRIE_NEXT(trie, data, src, limit, c, c2, result, resultType) { \
sl@0
   220
    (c)=*(src)++; \
sl@0
   221
    if(!UTF_IS_LEAD(c)) { \
sl@0
   222
        (c2)=0; \
sl@0
   223
        (result)=_UTRIE_GET_RAW((trie), data, 0, (c)); \
sl@0
   224
    } else if((src)!=(limit) && UTF_IS_TRAIL((c2)=*(src))) { \
sl@0
   225
        ++(src); \
sl@0
   226
        _UTRIE_GET_FROM_PAIR((trie), data, (c), (c2), (result), resultType); \
sl@0
   227
    } else { \
sl@0
   228
        /* unpaired lead surrogate code point */ \
sl@0
   229
        (c2)=0; \
sl@0
   230
        (result)=_UTRIE_GET_RAW((trie), data, UTRIE_LEAD_INDEX_DISP, (c)); \
sl@0
   231
    } \
sl@0
   232
}
sl@0
   233
sl@0
   234
/** Internal previous: get the previous code point (c, c2) and its data */
sl@0
   235
#define _UTRIE_PREVIOUS(trie, data, start, src, c, c2, result, resultType) { \
sl@0
   236
    (c)=*--(src); \
sl@0
   237
    if(!UTF_IS_SURROGATE(c)) { \
sl@0
   238
        (c2)=0; \
sl@0
   239
        (result)=_UTRIE_GET_RAW((trie), data, 0, (c)); \
sl@0
   240
    } else if(!UTF_IS_SURROGATE_FIRST(c)) { \
sl@0
   241
        /* trail surrogate */ \
sl@0
   242
        if((start)!=(src) && UTF_IS_LEAD((c2)=*((src)-1))) { \
sl@0
   243
            --(src); \
sl@0
   244
            (result)=(c); (c)=(c2); (c2)=(UChar)(result); /* swap c, c2 */ \
sl@0
   245
            _UTRIE_GET_FROM_PAIR((trie), data, (c), (c2), (result), resultType); \
sl@0
   246
        } else { \
sl@0
   247
            /* unpaired trail surrogate code point */ \
sl@0
   248
            (c2)=0; \
sl@0
   249
            (result)=_UTRIE_GET_RAW((trie), data, 0, (c)); \
sl@0
   250
        } \
sl@0
   251
    } else { \
sl@0
   252
        /* unpaired lead surrogate code point */ \
sl@0
   253
        (c2)=0; \
sl@0
   254
        (result)=_UTRIE_GET_RAW((trie), data, UTRIE_LEAD_INDEX_DISP, (c)); \
sl@0
   255
    } \
sl@0
   256
}
sl@0
   257
sl@0
   258
/* Public UTrie API ---------------------------------------------------------*/
sl@0
   259
sl@0
   260
/**
sl@0
   261
 * Get a pointer to the contiguous part of the data array
sl@0
   262
 * for the Latin-1 range (U+0000..U+00ff).
sl@0
   263
 * Must be used only if the Latin-1 range is in fact linear
sl@0
   264
 * (trie->isLatin1Linear).
sl@0
   265
 *
sl@0
   266
 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
sl@0
   267
 * @return (const uint16_t *) pointer to values for Latin-1 code points
sl@0
   268
 */
sl@0
   269
#define UTRIE_GET16_LATIN1(trie) ((trie)->index+(trie)->indexLength+UTRIE_DATA_BLOCK_LENGTH)
sl@0
   270
sl@0
   271
/**
sl@0
   272
 * Get a pointer to the contiguous part of the data array
sl@0
   273
 * for the Latin-1 range (U+0000..U+00ff).
sl@0
   274
 * Must be used only if the Latin-1 range is in fact linear
sl@0
   275
 * (trie->isLatin1Linear).
sl@0
   276
 *
sl@0
   277
 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
sl@0
   278
 * @return (const uint32_t *) pointer to values for Latin-1 code points
sl@0
   279
 */
sl@0
   280
#define UTRIE_GET32_LATIN1(trie) ((trie)->data32+UTRIE_DATA_BLOCK_LENGTH)
sl@0
   281
sl@0
   282
/**
sl@0
   283
 * Get a 16-bit trie value from a BMP code point (UChar, <=U+ffff).
sl@0
   284
 * c16 may be a lead surrogate, which may have a value including a folding offset.
sl@0
   285
 *
sl@0
   286
 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
sl@0
   287
 * @param c16 (UChar, in) the input BMP code point
sl@0
   288
 * @return (uint16_t) trie lookup result
sl@0
   289
 */
sl@0
   290
#define UTRIE_GET16_FROM_LEAD(trie, c16) _UTRIE_GET_RAW(trie, index, 0, c16)
sl@0
   291
sl@0
   292
/**
sl@0
   293
 * Get a 32-bit trie value from a BMP code point (UChar, <=U+ffff).
sl@0
   294
 * c16 may be a lead surrogate, which may have a value including a folding offset.
sl@0
   295
 *
sl@0
   296
 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
sl@0
   297
 * @param c16 (UChar, in) the input BMP code point
sl@0
   298
 * @return (uint32_t) trie lookup result
sl@0
   299
 */
sl@0
   300
#define UTRIE_GET32_FROM_LEAD(trie, c16) _UTRIE_GET_RAW(trie, data32, 0, c16)
sl@0
   301
sl@0
   302
/**
sl@0
   303
 * Get a 16-bit trie value from a BMP code point (UChar, <=U+ffff).
sl@0
   304
 * Even lead surrogate code points are treated as normal code points,
sl@0
   305
 * with unfolded values that may differ from _FROM_LEAD() macro results for them.
sl@0
   306
 *
sl@0
   307
 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
sl@0
   308
 * @param c16 (UChar, in) the input BMP code point
sl@0
   309
 * @return (uint16_t) trie lookup result
sl@0
   310
 */
sl@0
   311
#define UTRIE_GET16_FROM_BMP(trie, c16) _UTRIE_GET_FROM_BMP(trie, index, c16)
sl@0
   312
sl@0
   313
/**
sl@0
   314
 * Get a 32-bit trie value from a BMP code point (UChar, <=U+ffff).
sl@0
   315
 * Even lead surrogate code points are treated as normal code points,
sl@0
   316
 * with unfolded values that may differ from _FROM_LEAD() macro results for them.
sl@0
   317
 *
sl@0
   318
 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
sl@0
   319
 * @param c16 (UChar, in) the input BMP code point
sl@0
   320
 * @return (uint32_t) trie lookup result
sl@0
   321
 */
sl@0
   322
#define UTRIE_GET32_FROM_BMP(trie, c16) _UTRIE_GET_FROM_BMP(trie, data32, c16)
sl@0
   323
sl@0
   324
/**
sl@0
   325
 * Get a 16-bit trie value from a code point.
sl@0
   326
 * Even lead surrogate code points are treated as normal code points,
sl@0
   327
 * with unfolded values that may differ from _FROM_LEAD() macro results for them.
sl@0
   328
 *
sl@0
   329
 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
sl@0
   330
 * @param c32 (UChar32, in) the input code point
sl@0
   331
 * @param result (uint16_t, out) uint16_t variable for the trie lookup result
sl@0
   332
 */
sl@0
   333
#define UTRIE_GET16(trie, c32, result) _UTRIE_GET(trie, index, c32, result, uint16_t)
sl@0
   334
sl@0
   335
/**
sl@0
   336
 * Get a 32-bit trie value from a code point.
sl@0
   337
 * Even lead surrogate code points are treated as normal code points,
sl@0
   338
 * with unfolded values that may differ from _FROM_LEAD() macro results for them.
sl@0
   339
 *
sl@0
   340
 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
sl@0
   341
 * @param c32 (UChar32, in) the input code point
sl@0
   342
 * @param result (uint32_t, out) uint32_t variable for the trie lookup result
sl@0
   343
 */
sl@0
   344
#define UTRIE_GET32(trie, c32, result) _UTRIE_GET(trie, data32, c32, result, uint32_t)
sl@0
   345
sl@0
   346
/**
sl@0
   347
 * Get the next code point (c, c2), post-increment src,
sl@0
   348
 * and get a 16-bit value from the trie.
sl@0
   349
 *
sl@0
   350
 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
sl@0
   351
 * @param src (const UChar *, in/out) the source text pointer
sl@0
   352
 * @param limit (const UChar *, in) the limit pointer for the text, or NULL
sl@0
   353
 * @param c (UChar, out) variable for the BMP or lead code unit
sl@0
   354
 * @param c2 (UChar, out) variable for 0 or the trail code unit
sl@0
   355
 * @param result (uint16_t, out) uint16_t variable for the trie lookup result
sl@0
   356
 */
sl@0
   357
#define UTRIE_NEXT16(trie, src, limit, c, c2, result) _UTRIE_NEXT(trie, index, src, limit, c, c2, result, uint16_t)
sl@0
   358
sl@0
   359
/**
sl@0
   360
 * Get the next code point (c, c2), post-increment src,
sl@0
   361
 * and get a 32-bit value from the trie.
sl@0
   362
 *
sl@0
   363
 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
sl@0
   364
 * @param src (const UChar *, in/out) the source text pointer
sl@0
   365
 * @param limit (const UChar *, in) the limit pointer for the text, or NULL
sl@0
   366
 * @param c (UChar, out) variable for the BMP or lead code unit
sl@0
   367
 * @param c2 (UChar, out) variable for 0 or the trail code unit
sl@0
   368
 * @param result (uint32_t, out) uint32_t variable for the trie lookup result
sl@0
   369
 */
sl@0
   370
#define UTRIE_NEXT32(trie, src, limit, c, c2, result) _UTRIE_NEXT(trie, data32, src, limit, c, c2, result, uint32_t)
sl@0
   371
sl@0
   372
/**
sl@0
   373
 * Get the previous code point (c, c2), pre-decrement src,
sl@0
   374
 * and get a 16-bit value from the trie.
sl@0
   375
 *
sl@0
   376
 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
sl@0
   377
 * @param start (const UChar *, in) the start pointer for the text, or NULL
sl@0
   378
 * @param src (const UChar *, in/out) the source text pointer
sl@0
   379
 * @param c (UChar, out) variable for the BMP or lead code unit
sl@0
   380
 * @param c2 (UChar, out) variable for 0 or the trail code unit
sl@0
   381
 * @param result (uint16_t, out) uint16_t variable for the trie lookup result
sl@0
   382
 */
sl@0
   383
#define UTRIE_PREVIOUS16(trie, start, src, c, c2, result) _UTRIE_PREVIOUS(trie, index, start, src, c, c2, result, uint16_t)
sl@0
   384
sl@0
   385
/**
sl@0
   386
 * Get the previous code point (c, c2), pre-decrement src,
sl@0
   387
 * and get a 32-bit value from the trie.
sl@0
   388
 *
sl@0
   389
 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
sl@0
   390
 * @param start (const UChar *, in) the start pointer for the text, or NULL
sl@0
   391
 * @param src (const UChar *, in/out) the source text pointer
sl@0
   392
 * @param c (UChar, out) variable for the BMP or lead code unit
sl@0
   393
 * @param c2 (UChar, out) variable for 0 or the trail code unit
sl@0
   394
 * @param result (uint32_t, out) uint32_t variable for the trie lookup result
sl@0
   395
 */
sl@0
   396
#define UTRIE_PREVIOUS32(trie, start, src, c, c2, result) _UTRIE_PREVIOUS(trie, data32, start, src, c, c2, result, uint32_t)
sl@0
   397
sl@0
   398
/**
sl@0
   399
 * Get a 16-bit trie value from a pair of surrogates.
sl@0
   400
 *
sl@0
   401
 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
sl@0
   402
 * @param c (UChar, in) a lead surrogate
sl@0
   403
 * @param c2 (UChar, in) a trail surrogate
sl@0
   404
 * @param result (uint16_t, out) uint16_t variable for the trie lookup result
sl@0
   405
 */
sl@0
   406
#define UTRIE_GET16_FROM_PAIR(trie, c, c2, result) _UTRIE_GET_FROM_PAIR(trie, index, c, c2, result, uint16_t)
sl@0
   407
sl@0
   408
/**
sl@0
   409
 * Get a 32-bit trie value from a pair of surrogates.
sl@0
   410
 *
sl@0
   411
 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
sl@0
   412
 * @param c (UChar, in) a lead surrogate
sl@0
   413
 * @param c2 (UChar, in) a trail surrogate
sl@0
   414
 * @param result (uint32_t, out) uint32_t variable for the trie lookup result
sl@0
   415
 */
sl@0
   416
#define UTRIE_GET32_FROM_PAIR(trie, c, c2, result) _UTRIE_GET_FROM_PAIR(trie, data32, c, c2, result, uint32_t)
sl@0
   417
sl@0
   418
/**
sl@0
   419
 * Get a 16-bit trie value from a folding offset (from the value of a lead surrogate)
sl@0
   420
 * and a trail surrogate.
sl@0
   421
 *
sl@0
   422
 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
sl@0
   423
 * @param offset (int32_t, in) the folding offset from the value of a lead surrogate
sl@0
   424
 * @param c2 (UChar, in) a trail surrogate (only the 10 low bits are significant)
sl@0
   425
 * @return (uint16_t) trie lookup result
sl@0
   426
 */
sl@0
   427
#define UTRIE_GET16_FROM_OFFSET_TRAIL(trie, offset, c2) _UTRIE_GET_RAW(trie, index, offset, (c2)&0x3ff)
sl@0
   428
sl@0
   429
/**
sl@0
   430
 * Get a 32-bit trie value from a folding offset (from the value of a lead surrogate)
sl@0
   431
 * and a trail surrogate.
sl@0
   432
 *
sl@0
   433
 * @param trie (const UTrie *, in) a pointer to the runtime trie structure
sl@0
   434
 * @param offset (int32_t, in) the folding offset from the value of a lead surrogate
sl@0
   435
 * @param c2 (UChar, in) a trail surrogate (only the 10 low bits are significant)
sl@0
   436
 * @return (uint32_t) trie lookup result
sl@0
   437
 */
sl@0
   438
#define UTRIE_GET32_FROM_OFFSET_TRAIL(trie, offset, c2) _UTRIE_GET_RAW(trie, data32, offset, (c2)&0x3ff)
sl@0
   439
sl@0
   440
/* enumeration callback types */
sl@0
   441
sl@0
   442
/**
sl@0
   443
 * Callback from utrie_enum(), extracts a uint32_t value from a
sl@0
   444
 * trie value. This value will be passed on to the UTrieEnumRange function.
sl@0
   445
 *
sl@0
   446
 * @param context an opaque pointer, as passed into utrie_enum()
sl@0
   447
 * @param value a value from the trie
sl@0
   448
 * @return the value that is to be passed on to the UTrieEnumRange function
sl@0
   449
 */
sl@0
   450
typedef uint32_t U_CALLCONV
sl@0
   451
UTrieEnumValue(const void *context, uint32_t value);
sl@0
   452
sl@0
   453
/**
sl@0
   454
 * Callback from utrie_enum(), is called for each contiguous range
sl@0
   455
 * of code points with the same value as retrieved from the trie and
sl@0
   456
 * transformed by the UTrieEnumValue function.
sl@0
   457
 *
sl@0
   458
 * The callback function can stop the enumeration by returning FALSE.
sl@0
   459
 *
sl@0
   460
 * @param context an opaque pointer, as passed into utrie_enum()
sl@0
   461
 * @param start the first code point in a contiguous range with value
sl@0
   462
 * @param limit one past the last code point in a contiguous range with value
sl@0
   463
 * @param value the value that is set for all code points in [start..limit[
sl@0
   464
 * @return FALSE to stop the enumeration
sl@0
   465
 */
sl@0
   466
typedef UBool U_CALLCONV
sl@0
   467
UTrieEnumRange(const void *context, UChar32 start, UChar32 limit, uint32_t value);
sl@0
   468
sl@0
   469
/**
sl@0
   470
 * Enumerate efficiently all values in a trie.
sl@0
   471
 * For each entry in the trie, the value to be delivered is passed through
sl@0
   472
 * the UTrieEnumValue function.
sl@0
   473
 * The value is unchanged if that function pointer is NULL.
sl@0
   474
 *
sl@0
   475
 * For each contiguous range of code points with a given value,
sl@0
   476
 * the UTrieEnumRange function is called.
sl@0
   477
 *
sl@0
   478
 * @param trie a pointer to the runtime trie structure
sl@0
   479
 * @param enumValue a pointer to a function that may transform the trie entry value,
sl@0
   480
 *                  or NULL if the values from the trie are to be used directly
sl@0
   481
 * @param enumRange a pointer to a function that is called for each contiguous range
sl@0
   482
 *                  of code points with the same value
sl@0
   483
 * @param context an opaque pointer that is passed on to the callback functions
sl@0
   484
 */
sl@0
   485
U_CAPI void U_EXPORT2
sl@0
   486
utrie_enum(const UTrie *trie,
sl@0
   487
           UTrieEnumValue *enumValue, UTrieEnumRange *enumRange, const void *context);
sl@0
   488
sl@0
   489
/**
sl@0
   490
 * Unserialize a trie from 32-bit-aligned memory.
sl@0
   491
 * Inverse of utrie_serialize().
sl@0
   492
 * Fills the UTrie runtime trie structure with the settings for the trie data.
sl@0
   493
 *
sl@0
   494
 * @param trie a pointer to the runtime trie structure
sl@0
   495
 * @param data a pointer to 32-bit-aligned memory containing trie data
sl@0
   496
 * @param length the number of bytes available at data
sl@0
   497
 * @param pErrorCode an in/out ICU UErrorCode
sl@0
   498
 * @return the number of bytes at data taken up by the trie data
sl@0
   499
 */
sl@0
   500
U_CAPI int32_t U_EXPORT2
sl@0
   501
utrie_unserialize(UTrie *trie, const void *data, int32_t length, UErrorCode *pErrorCode);
sl@0
   502
sl@0
   503
/**
sl@0
   504
 * "Unserialize" a dummy trie.
sl@0
   505
 * A dummy trie is an empty runtime trie, used when a real data trie cannot
sl@0
   506
 * be loaded.
sl@0
   507
 *
sl@0
   508
 * The input memory is filled so that the trie always returns the initialValue,
sl@0
   509
 * or the leadUnitValue for lead surrogate code points.
sl@0
   510
 * The Latin-1 part is always set up to be linear.
sl@0
   511
 *
sl@0
   512
 * @param trie a pointer to the runtime trie structure
sl@0
   513
 * @param data a pointer to 32-bit-aligned memory to be filled with the dummy trie data
sl@0
   514
 * @param length the number of bytes available at data (recommended to use UTRIE_DUMMY_SIZE)
sl@0
   515
 * @param initialValue the initial value that is set for all code points
sl@0
   516
 * @param leadUnitValue the value for lead surrogate code _units_ that do not
sl@0
   517
 *                      have associated supplementary data
sl@0
   518
 * @param pErrorCode an in/out ICU UErrorCode
sl@0
   519
 *
sl@0
   520
 * @see UTRIE_DUMMY_SIZE
sl@0
   521
 * @see utrie_open
sl@0
   522
 */
sl@0
   523
U_CAPI int32_t U_EXPORT2
sl@0
   524
utrie_unserializeDummy(UTrie *trie,
sl@0
   525
                       void *data, int32_t length,
sl@0
   526
                       uint32_t initialValue, uint32_t leadUnitValue,
sl@0
   527
                       UBool make16BitTrie,
sl@0
   528
                       UErrorCode *pErrorCode);
sl@0
   529
sl@0
   530
/**
sl@0
   531
 * Default implementation for UTrie.getFoldingOffset, set automatically by
sl@0
   532
 * utrie_unserialize().
sl@0
   533
 * Simply returns the lead surrogate's value itself - which is the inverse
sl@0
   534
 * of the default folding function used by utrie_serialize().
sl@0
   535
 * Exported for static const UTrie structures.
sl@0
   536
 *
sl@0
   537
 * @see UTrieGetFoldingOffset
sl@0
   538
 */
sl@0
   539
U_CAPI int32_t U_EXPORT2
sl@0
   540
utrie_defaultGetFoldingOffset(uint32_t data);
sl@0
   541
sl@0
   542
/* Building a trie ----------------------------------------------------------*/
sl@0
   543
sl@0
   544
/**
sl@0
   545
 * Build-time trie structure.
sl@0
   546
 * Opaque definition, here only to make fillIn parameters possible
sl@0
   547
 * for utrie_open() and utrie_clone().
sl@0
   548
 */
sl@0
   549
struct UNewTrie {
sl@0
   550
    /**
sl@0
   551
     * Index values at build-time are 32 bits wide for easier processing.
sl@0
   552
     * Bit 31 is set if the data block is used by multiple index values (from utrie_setRange()).
sl@0
   553
     */
sl@0
   554
    int32_t index[UTRIE_MAX_INDEX_LENGTH];
sl@0
   555
    uint32_t *data;
sl@0
   556
sl@0
   557
    uint32_t leadUnitValue;
sl@0
   558
    int32_t indexLength, dataCapacity, dataLength;
sl@0
   559
    UBool isAllocated, isDataAllocated;
sl@0
   560
    UBool isLatin1Linear, isCompacted;
sl@0
   561
sl@0
   562
    /**
sl@0
   563
     * Map of adjusted indexes, used in utrie_compact().
sl@0
   564
     * Maps from original indexes to new ones.
sl@0
   565
     */
sl@0
   566
    int32_t map[UTRIE_MAX_BUILD_TIME_DATA_LENGTH>>UTRIE_SHIFT];
sl@0
   567
};
sl@0
   568
sl@0
   569
typedef struct UNewTrie UNewTrie;
sl@0
   570
sl@0
   571
/**
sl@0
   572
 * Build-time trie callback function, used with utrie_serialize().
sl@0
   573
 * This function calculates a lead surrogate's value including a folding offset
sl@0
   574
 * from the 1024 supplementary code points [start..start+1024[ .
sl@0
   575
 * It is U+10000 <= start <= U+10fc00 and (start&0x3ff)==0.
sl@0
   576
 *
sl@0
   577
 * The folding offset is provided by the caller.
sl@0
   578
 * It is offset=UTRIE_BMP_INDEX_LENGTH+n*UTRIE_SURROGATE_BLOCK_COUNT with n=0..1023.
sl@0
   579
 * Instead of the offset itself, n can be stored in 10 bits -
sl@0
   580
 * or fewer if it can be assumed that few lead surrogates have associated data.
sl@0
   581
 *
sl@0
   582
 * The returned value must be
sl@0
   583
 * - not zero if and only if there is relevant data
sl@0
   584
 *   for the corresponding 1024 supplementary code points
sl@0
   585
 * - such that UTrie.getFoldingOffset(UNewTrieGetFoldedValue(..., offset))==offset
sl@0
   586
 *
sl@0
   587
 * @return a folded value, or 0 if there is no relevant data for the lead surrogate.
sl@0
   588
 */
sl@0
   589
typedef uint32_t U_CALLCONV
sl@0
   590
UNewTrieGetFoldedValue(UNewTrie *trie, UChar32 start, int32_t offset);
sl@0
   591
sl@0
   592
/**
sl@0
   593
 * Open a build-time trie structure.
sl@0
   594
 * The size of the build-time data array is specified to avoid allocating a large
sl@0
   595
 * array in all cases. The array itself can also be passed in.
sl@0
   596
 *
sl@0
   597
 * Although the trie is never fully expanded to a linear array, especially when
sl@0
   598
 * utrie_setRange32() is used, the data array could be large during build time.
sl@0
   599
 * The maximum length is
sl@0
   600
 * UTRIE_MAX_BUILD_TIME_DATA_LENGTH=0x110000+UTRIE_DATA_BLOCK_LENGTH+0x400.
sl@0
   601
 * (Number of Unicode code points + one all-initial-value block +
sl@0
   602
 *  possible duplicate entries for 1024 lead surrogates.)
sl@0
   603
 * (UTRIE_DATA_BLOCK_LENGTH<=0x200 in all cases.)
sl@0
   604
 *
sl@0
   605
 * @param fillIn a pointer to a UNewTrie structure to be initialized (will not be released), or
sl@0
   606
 *               NULL if one is to be allocated
sl@0
   607
 * @param aliasData a pointer to a data array to be used (will not be released), or
sl@0
   608
 *                  NULL if one is to be allocated
sl@0
   609
 * @param maxDataLength the capacity of aliasData (if not NULL) or
sl@0
   610
 *                      the length of the data array to be allocated
sl@0
   611
 * @param initialValue the initial value that is set for all code points
sl@0
   612
 * @param leadUnitValue the value for lead surrogate code _units_ that do not
sl@0
   613
 *                      have associated supplementary data
sl@0
   614
 * @param latin1Linear a flag indicating whether the Latin-1 range is to be allocated and
sl@0
   615
 *                     kept in a linear, contiguous part of the data array
sl@0
   616
 * @return a pointer to the initialized fillIn or the allocated and initialized new UNewTrie
sl@0
   617
 */
sl@0
   618
U_CAPI UNewTrie * U_EXPORT2
sl@0
   619
utrie_open(UNewTrie *fillIn,
sl@0
   620
           uint32_t *aliasData, int32_t maxDataLength,
sl@0
   621
           uint32_t initialValue, uint32_t leadUnitValue,
sl@0
   622
           UBool latin1Linear);
sl@0
   623
sl@0
   624
/**
sl@0
   625
 * Clone a build-time trie structure with all entries.
sl@0
   626
 *
sl@0
   627
 * @param fillIn like in utrie_open()
sl@0
   628
 * @param other the build-time trie structure to clone
sl@0
   629
 * @param aliasData like in utrie_open(),
sl@0
   630
 *                  used if aliasDataLength>=(capacity of other's data array)
sl@0
   631
 * @param aliasDataLength the length of aliasData
sl@0
   632
 * @return a pointer to the initialized fillIn or the allocated and initialized new UNewTrie
sl@0
   633
 */
sl@0
   634
U_CAPI UNewTrie * U_EXPORT2
sl@0
   635
utrie_clone(UNewTrie *fillIn, const UNewTrie *other, uint32_t *aliasData, int32_t aliasDataLength);
sl@0
   636
sl@0
   637
/**
sl@0
   638
 * Close a build-time trie structure, and release memory
sl@0
   639
 * that was allocated by utrie_open() or utrie_clone().
sl@0
   640
 *
sl@0
   641
 * @param trie the build-time trie
sl@0
   642
 */
sl@0
   643
U_CAPI void U_EXPORT2
sl@0
   644
utrie_close(UNewTrie *trie);
sl@0
   645
sl@0
   646
/**
sl@0
   647
 * Get the data array of a build-time trie.
sl@0
   648
 * The data may be modified, but entries that are equal before
sl@0
   649
 * must still be equal after modification.
sl@0
   650
 *
sl@0
   651
 * @param trie the build-time trie
sl@0
   652
 * @param pLength (out) a pointer to a variable that receives the number
sl@0
   653
 *                of entries in the data array
sl@0
   654
 * @return the data array
sl@0
   655
 */
sl@0
   656
U_CAPI uint32_t * U_EXPORT2
sl@0
   657
utrie_getData(UNewTrie *trie, int32_t *pLength);
sl@0
   658
sl@0
   659
/**
sl@0
   660
 * Set a value for a code point.
sl@0
   661
 *
sl@0
   662
 * @param trie the build-time trie
sl@0
   663
 * @param c the code point
sl@0
   664
 * @param value the value
sl@0
   665
 * @return FALSE if a failure occurred (illegal argument or data array overrun)
sl@0
   666
 */
sl@0
   667
U_CAPI UBool U_EXPORT2
sl@0
   668
utrie_set32(UNewTrie *trie, UChar32 c, uint32_t value);
sl@0
   669
sl@0
   670
/**
sl@0
   671
 * Get a value from a code point as stored in the build-time trie.
sl@0
   672
 *
sl@0
   673
 * @param trie the build-time trie
sl@0
   674
 * @param c the code point
sl@0
   675
 * @param pInBlockZero if not NULL, then *pInBlockZero is set to TRUE
sl@0
   676
 *                     iff the value is retrieved from block 0;
sl@0
   677
 *                     block 0 is the all-initial-value initial block
sl@0
   678
 * @return the value
sl@0
   679
 */
sl@0
   680
U_CAPI uint32_t U_EXPORT2
sl@0
   681
utrie_get32(UNewTrie *trie, UChar32 c, UBool *pInBlockZero);
sl@0
   682
sl@0
   683
/**
sl@0
   684
 * Set a value in a range of code points [start..limit[.
sl@0
   685
 * All code points c with start<=c<limit will get the value if
sl@0
   686
 * overwrite is TRUE or if the old value is 0.
sl@0
   687
 *
sl@0
   688
 * @param trie the build-time trie
sl@0
   689
 * @param start the first code point to get the value
sl@0
   690
 * @param limit one past the last code point to get the value
sl@0
   691
 * @param value the value
sl@0
   692
 * @param overwrite flag for whether old non-initial values are to be overwritten
sl@0
   693
 * @return FALSE if a failure occurred (illegal argument or data array overrun)
sl@0
   694
 */
sl@0
   695
U_CAPI UBool U_EXPORT2
sl@0
   696
utrie_setRange32(UNewTrie *trie, UChar32 start, UChar32 limit, uint32_t value, UBool overwrite);
sl@0
   697
sl@0
   698
/**
sl@0
   699
 * Compact the build-time trie after all values are set, and then
sl@0
   700
 * serialize it into 32-bit aligned memory.
sl@0
   701
 *
sl@0
   702
 * After this, the trie can only be serizalized again and/or closed;
sl@0
   703
 * no further values can be added.
sl@0
   704
 *
sl@0
   705
 * @see utrie_unserialize()
sl@0
   706
 *
sl@0
   707
 * @param trie the build-time trie
sl@0
   708
 * @param data a pointer to 32-bit-aligned memory for the trie data
sl@0
   709
 * @param capacity the number of bytes available at data
sl@0
   710
 * @param getFoldedValue a callback function that calculates the value for
sl@0
   711
 *                       a lead surrogate from all of its supplementary code points
sl@0
   712
 *                       and the folding offset;
sl@0
   713
 *                       if NULL, then a default function is used which returns just
sl@0
   714
 *                       the input offset when there are any non-initial-value entries
sl@0
   715
 * @param reduceTo16Bits flag for whether the values are to be reduced to a
sl@0
   716
 *                       width of 16 bits for serialization and runtime
sl@0
   717
 * @param pErrorCode a UErrorCode argument; among other possible error codes:
sl@0
   718
 * - U_BUFFER_OVERFLOW_ERROR if the data storage block is too small for serialization
sl@0
   719
 * - U_MEMORY_ALLOCATION_ERROR if the trie data array is too small
sl@0
   720
 * - U_INDEX_OUTOFBOUNDS_ERROR if the index or data arrays are too long after compaction for serialization
sl@0
   721
 *
sl@0
   722
 * @return the number of bytes written for the trie
sl@0
   723
 */
sl@0
   724
U_CAPI int32_t U_EXPORT2
sl@0
   725
utrie_serialize(UNewTrie *trie, void *data, int32_t capacity,
sl@0
   726
                UNewTrieGetFoldedValue *getFoldedValue,
sl@0
   727
                UBool reduceTo16Bits,
sl@0
   728
                UErrorCode *pErrorCode);
sl@0
   729
sl@0
   730
/**
sl@0
   731
 * Swap a serialized UTrie.
sl@0
   732
 * @internal
sl@0
   733
 */
sl@0
   734
U_CAPI int32_t U_EXPORT2
sl@0
   735
utrie_swap(const UDataSwapper *ds,
sl@0
   736
           const void *inData, int32_t length, void *outData,
sl@0
   737
           UErrorCode *pErrorCode);
sl@0
   738
sl@0
   739
U_CDECL_END
sl@0
   740
sl@0
   741
#endif