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