~ubuntu-branches/ubuntu/gutsy/icu/gutsy-updates

« back to all changes in this revision

Viewing changes to source/common/utrie.h

  • Committer: Package Import Robot
  • Author(s): Jay Berkenbilt
  • Date: 2005-11-19 11:29:31 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20051119112931-vcizkrp10tli4enw
Tags: 3.4-3
Explicitly build with g++ 3.4.  The current ICU fails its test suite
with 4.0 but not with 3.4.  Future versions should work properly with
4.0.

Show diffs side-by-side

added added

removed removed

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