jpayne@69
|
1 // © 2017 and later: Unicode, Inc. and others.
|
jpayne@69
|
2 // License & terms of use: http://www.unicode.org/copyright.html
|
jpayne@69
|
3
|
jpayne@69
|
4 // ucptrie.h (modified from utrie2.h)
|
jpayne@69
|
5 // created: 2017dec29 Markus W. Scherer
|
jpayne@69
|
6
|
jpayne@69
|
7 #ifndef __UCPTRIE_H__
|
jpayne@69
|
8 #define __UCPTRIE_H__
|
jpayne@69
|
9
|
jpayne@69
|
10 #include "unicode/utypes.h"
|
jpayne@69
|
11
|
jpayne@69
|
12 #include "unicode/localpointer.h"
|
jpayne@69
|
13 #include "unicode/ucpmap.h"
|
jpayne@69
|
14 #include "unicode/utf8.h"
|
jpayne@69
|
15
|
jpayne@69
|
16 U_CDECL_BEGIN
|
jpayne@69
|
17
|
jpayne@69
|
18 /**
|
jpayne@69
|
19 * \file
|
jpayne@69
|
20 *
|
jpayne@69
|
21 * This file defines an immutable Unicode code point trie.
|
jpayne@69
|
22 *
|
jpayne@69
|
23 * @see UCPTrie
|
jpayne@69
|
24 * @see UMutableCPTrie
|
jpayne@69
|
25 */
|
jpayne@69
|
26
|
jpayne@69
|
27 #ifndef U_IN_DOXYGEN
|
jpayne@69
|
28 /** @internal */
|
jpayne@69
|
29 typedef union UCPTrieData {
|
jpayne@69
|
30 /** @internal */
|
jpayne@69
|
31 const void *ptr0;
|
jpayne@69
|
32 /** @internal */
|
jpayne@69
|
33 const uint16_t *ptr16;
|
jpayne@69
|
34 /** @internal */
|
jpayne@69
|
35 const uint32_t *ptr32;
|
jpayne@69
|
36 /** @internal */
|
jpayne@69
|
37 const uint8_t *ptr8;
|
jpayne@69
|
38 } UCPTrieData;
|
jpayne@69
|
39 #endif
|
jpayne@69
|
40
|
jpayne@69
|
41 /**
|
jpayne@69
|
42 * Immutable Unicode code point trie structure.
|
jpayne@69
|
43 * Fast, reasonably compact, map from Unicode code points (U+0000..U+10FFFF) to integer values.
|
jpayne@69
|
44 * For details see http://site.icu-project.org/design/struct/utrie
|
jpayne@69
|
45 *
|
jpayne@69
|
46 * Do not access UCPTrie fields directly; use public functions and macros.
|
jpayne@69
|
47 * Functions are easy to use: They support all trie types and value widths.
|
jpayne@69
|
48 *
|
jpayne@69
|
49 * When performance is really important, macros provide faster access.
|
jpayne@69
|
50 * Most macros are specific to either "fast" or "small" tries, see UCPTrieType.
|
jpayne@69
|
51 * There are "fast" macros for special optimized use cases.
|
jpayne@69
|
52 *
|
jpayne@69
|
53 * The macros will return bogus values, or may crash, if used on the wrong type or value width.
|
jpayne@69
|
54 *
|
jpayne@69
|
55 * @see UMutableCPTrie
|
jpayne@69
|
56 * @stable ICU 63
|
jpayne@69
|
57 */
|
jpayne@69
|
58 struct UCPTrie {
|
jpayne@69
|
59 #ifndef U_IN_DOXYGEN
|
jpayne@69
|
60 /** @internal */
|
jpayne@69
|
61 const uint16_t *index;
|
jpayne@69
|
62 /** @internal */
|
jpayne@69
|
63 UCPTrieData data;
|
jpayne@69
|
64
|
jpayne@69
|
65 /** @internal */
|
jpayne@69
|
66 int32_t indexLength;
|
jpayne@69
|
67 /** @internal */
|
jpayne@69
|
68 int32_t dataLength;
|
jpayne@69
|
69 /** Start of the last range which ends at U+10FFFF. @internal */
|
jpayne@69
|
70 UChar32 highStart;
|
jpayne@69
|
71 /** highStart>>12 @internal */
|
jpayne@69
|
72 uint16_t shifted12HighStart;
|
jpayne@69
|
73
|
jpayne@69
|
74 /** @internal */
|
jpayne@69
|
75 int8_t type; // UCPTrieType
|
jpayne@69
|
76 /** @internal */
|
jpayne@69
|
77 int8_t valueWidth; // UCPTrieValueWidth
|
jpayne@69
|
78
|
jpayne@69
|
79 /** padding/reserved @internal */
|
jpayne@69
|
80 uint32_t reserved32;
|
jpayne@69
|
81 /** padding/reserved @internal */
|
jpayne@69
|
82 uint16_t reserved16;
|
jpayne@69
|
83
|
jpayne@69
|
84 /**
|
jpayne@69
|
85 * Internal index-3 null block offset.
|
jpayne@69
|
86 * Set to an impossibly high value (e.g., 0xffff) if there is no dedicated index-3 null block.
|
jpayne@69
|
87 * @internal
|
jpayne@69
|
88 */
|
jpayne@69
|
89 uint16_t index3NullOffset;
|
jpayne@69
|
90 /**
|
jpayne@69
|
91 * Internal data null block offset, not shifted.
|
jpayne@69
|
92 * Set to an impossibly high value (e.g., 0xfffff) if there is no dedicated data null block.
|
jpayne@69
|
93 * @internal
|
jpayne@69
|
94 */
|
jpayne@69
|
95 int32_t dataNullOffset;
|
jpayne@69
|
96 /** @internal */
|
jpayne@69
|
97 uint32_t nullValue;
|
jpayne@69
|
98
|
jpayne@69
|
99 #ifdef UCPTRIE_DEBUG
|
jpayne@69
|
100 /** @internal */
|
jpayne@69
|
101 const char *name;
|
jpayne@69
|
102 #endif
|
jpayne@69
|
103 #endif
|
jpayne@69
|
104 };
|
jpayne@69
|
105 #ifndef U_IN_DOXYGEN
|
jpayne@69
|
106 typedef struct UCPTrie UCPTrie;
|
jpayne@69
|
107 #endif
|
jpayne@69
|
108
|
jpayne@69
|
109 /**
|
jpayne@69
|
110 * Selectors for the type of a UCPTrie.
|
jpayne@69
|
111 * Different trade-offs for size vs. speed.
|
jpayne@69
|
112 *
|
jpayne@69
|
113 * @see umutablecptrie_buildImmutable
|
jpayne@69
|
114 * @see ucptrie_openFromBinary
|
jpayne@69
|
115 * @see ucptrie_getType
|
jpayne@69
|
116 * @stable ICU 63
|
jpayne@69
|
117 */
|
jpayne@69
|
118 enum UCPTrieType {
|
jpayne@69
|
119 /**
|
jpayne@69
|
120 * For ucptrie_openFromBinary() to accept any type.
|
jpayne@69
|
121 * ucptrie_getType() will return the actual type.
|
jpayne@69
|
122 * @stable ICU 63
|
jpayne@69
|
123 */
|
jpayne@69
|
124 UCPTRIE_TYPE_ANY = -1,
|
jpayne@69
|
125 /**
|
jpayne@69
|
126 * Fast/simple/larger BMP data structure. Use functions and "fast" macros.
|
jpayne@69
|
127 * @stable ICU 63
|
jpayne@69
|
128 */
|
jpayne@69
|
129 UCPTRIE_TYPE_FAST,
|
jpayne@69
|
130 /**
|
jpayne@69
|
131 * Small/slower BMP data structure. Use functions and "small" macros.
|
jpayne@69
|
132 * @stable ICU 63
|
jpayne@69
|
133 */
|
jpayne@69
|
134 UCPTRIE_TYPE_SMALL
|
jpayne@69
|
135 };
|
jpayne@69
|
136 #ifndef U_IN_DOXYGEN
|
jpayne@69
|
137 typedef enum UCPTrieType UCPTrieType;
|
jpayne@69
|
138 #endif
|
jpayne@69
|
139
|
jpayne@69
|
140 /**
|
jpayne@69
|
141 * Selectors for the number of bits in a UCPTrie data value.
|
jpayne@69
|
142 *
|
jpayne@69
|
143 * @see umutablecptrie_buildImmutable
|
jpayne@69
|
144 * @see ucptrie_openFromBinary
|
jpayne@69
|
145 * @see ucptrie_getValueWidth
|
jpayne@69
|
146 * @stable ICU 63
|
jpayne@69
|
147 */
|
jpayne@69
|
148 enum UCPTrieValueWidth {
|
jpayne@69
|
149 /**
|
jpayne@69
|
150 * For ucptrie_openFromBinary() to accept any data value width.
|
jpayne@69
|
151 * ucptrie_getValueWidth() will return the actual data value width.
|
jpayne@69
|
152 * @stable ICU 63
|
jpayne@69
|
153 */
|
jpayne@69
|
154 UCPTRIE_VALUE_BITS_ANY = -1,
|
jpayne@69
|
155 /**
|
jpayne@69
|
156 * The trie stores 16 bits per data value.
|
jpayne@69
|
157 * It returns them as unsigned values 0..0xffff=65535.
|
jpayne@69
|
158 * @stable ICU 63
|
jpayne@69
|
159 */
|
jpayne@69
|
160 UCPTRIE_VALUE_BITS_16,
|
jpayne@69
|
161 /**
|
jpayne@69
|
162 * The trie stores 32 bits per data value.
|
jpayne@69
|
163 * @stable ICU 63
|
jpayne@69
|
164 */
|
jpayne@69
|
165 UCPTRIE_VALUE_BITS_32,
|
jpayne@69
|
166 /**
|
jpayne@69
|
167 * The trie stores 8 bits per data value.
|
jpayne@69
|
168 * It returns them as unsigned values 0..0xff=255.
|
jpayne@69
|
169 * @stable ICU 63
|
jpayne@69
|
170 */
|
jpayne@69
|
171 UCPTRIE_VALUE_BITS_8
|
jpayne@69
|
172 };
|
jpayne@69
|
173 #ifndef U_IN_DOXYGEN
|
jpayne@69
|
174 typedef enum UCPTrieValueWidth UCPTrieValueWidth;
|
jpayne@69
|
175 #endif
|
jpayne@69
|
176
|
jpayne@69
|
177 /**
|
jpayne@69
|
178 * Opens a trie from its binary form, stored in 32-bit-aligned memory.
|
jpayne@69
|
179 * Inverse of ucptrie_toBinary().
|
jpayne@69
|
180 *
|
jpayne@69
|
181 * The memory must remain valid and unchanged as long as the trie is used.
|
jpayne@69
|
182 * You must ucptrie_close() the trie once you are done using it.
|
jpayne@69
|
183 *
|
jpayne@69
|
184 * @param type selects the trie type; results in an
|
jpayne@69
|
185 * U_INVALID_FORMAT_ERROR if it does not match the binary data;
|
jpayne@69
|
186 * use UCPTRIE_TYPE_ANY to accept any type
|
jpayne@69
|
187 * @param valueWidth selects the number of bits in a data value; results in an
|
jpayne@69
|
188 * U_INVALID_FORMAT_ERROR if it does not match the binary data;
|
jpayne@69
|
189 * use UCPTRIE_VALUE_BITS_ANY to accept any data value width
|
jpayne@69
|
190 * @param data a pointer to 32-bit-aligned memory containing the binary data of a UCPTrie
|
jpayne@69
|
191 * @param length the number of bytes available at data;
|
jpayne@69
|
192 * can be more than necessary
|
jpayne@69
|
193 * @param pActualLength receives the actual number of bytes at data taken up by the trie data;
|
jpayne@69
|
194 * can be NULL
|
jpayne@69
|
195 * @param pErrorCode an in/out ICU UErrorCode
|
jpayne@69
|
196 * @return the trie
|
jpayne@69
|
197 *
|
jpayne@69
|
198 * @see umutablecptrie_open
|
jpayne@69
|
199 * @see umutablecptrie_buildImmutable
|
jpayne@69
|
200 * @see ucptrie_toBinary
|
jpayne@69
|
201 * @stable ICU 63
|
jpayne@69
|
202 */
|
jpayne@69
|
203 U_CAPI UCPTrie * U_EXPORT2
|
jpayne@69
|
204 ucptrie_openFromBinary(UCPTrieType type, UCPTrieValueWidth valueWidth,
|
jpayne@69
|
205 const void *data, int32_t length, int32_t *pActualLength,
|
jpayne@69
|
206 UErrorCode *pErrorCode);
|
jpayne@69
|
207
|
jpayne@69
|
208 /**
|
jpayne@69
|
209 * Closes a trie and releases associated memory.
|
jpayne@69
|
210 *
|
jpayne@69
|
211 * @param trie the trie
|
jpayne@69
|
212 * @stable ICU 63
|
jpayne@69
|
213 */
|
jpayne@69
|
214 U_CAPI void U_EXPORT2
|
jpayne@69
|
215 ucptrie_close(UCPTrie *trie);
|
jpayne@69
|
216
|
jpayne@69
|
217 /**
|
jpayne@69
|
218 * Returns the trie type.
|
jpayne@69
|
219 *
|
jpayne@69
|
220 * @param trie the trie
|
jpayne@69
|
221 * @return the trie type
|
jpayne@69
|
222 * @see ucptrie_openFromBinary
|
jpayne@69
|
223 * @see UCPTRIE_TYPE_ANY
|
jpayne@69
|
224 * @stable ICU 63
|
jpayne@69
|
225 */
|
jpayne@69
|
226 U_CAPI UCPTrieType U_EXPORT2
|
jpayne@69
|
227 ucptrie_getType(const UCPTrie *trie);
|
jpayne@69
|
228
|
jpayne@69
|
229 /**
|
jpayne@69
|
230 * Returns the number of bits in a trie data value.
|
jpayne@69
|
231 *
|
jpayne@69
|
232 * @param trie the trie
|
jpayne@69
|
233 * @return the number of bits in a trie data value
|
jpayne@69
|
234 * @see ucptrie_openFromBinary
|
jpayne@69
|
235 * @see UCPTRIE_VALUE_BITS_ANY
|
jpayne@69
|
236 * @stable ICU 63
|
jpayne@69
|
237 */
|
jpayne@69
|
238 U_CAPI UCPTrieValueWidth U_EXPORT2
|
jpayne@69
|
239 ucptrie_getValueWidth(const UCPTrie *trie);
|
jpayne@69
|
240
|
jpayne@69
|
241 /**
|
jpayne@69
|
242 * Returns the value for a code point as stored in the trie, with range checking.
|
jpayne@69
|
243 * Returns the trie error value if c is not in the range 0..U+10FFFF.
|
jpayne@69
|
244 *
|
jpayne@69
|
245 * Easier to use than UCPTRIE_FAST_GET() and similar macros but slower.
|
jpayne@69
|
246 * Easier to use because, unlike the macros, this function works on all UCPTrie
|
jpayne@69
|
247 * objects, for all types and value widths.
|
jpayne@69
|
248 *
|
jpayne@69
|
249 * @param trie the trie
|
jpayne@69
|
250 * @param c the code point
|
jpayne@69
|
251 * @return the trie value,
|
jpayne@69
|
252 * or the trie error value if the code point is not in the range 0..U+10FFFF
|
jpayne@69
|
253 * @stable ICU 63
|
jpayne@69
|
254 */
|
jpayne@69
|
255 U_CAPI uint32_t U_EXPORT2
|
jpayne@69
|
256 ucptrie_get(const UCPTrie *trie, UChar32 c);
|
jpayne@69
|
257
|
jpayne@69
|
258 /**
|
jpayne@69
|
259 * Returns the last code point such that all those from start to there have the same value.
|
jpayne@69
|
260 * Can be used to efficiently iterate over all same-value ranges in a trie.
|
jpayne@69
|
261 * (This is normally faster than iterating over code points and get()ting each value,
|
jpayne@69
|
262 * but much slower than a data structure that stores ranges directly.)
|
jpayne@69
|
263 *
|
jpayne@69
|
264 * If the UCPMapValueFilter function pointer is not NULL, then
|
jpayne@69
|
265 * the value to be delivered is passed through that function, and the return value is the end
|
jpayne@69
|
266 * of the range where all values are modified to the same actual value.
|
jpayne@69
|
267 * The value is unchanged if that function pointer is NULL.
|
jpayne@69
|
268 *
|
jpayne@69
|
269 * Example:
|
jpayne@69
|
270 * \code
|
jpayne@69
|
271 * UChar32 start = 0, end;
|
jpayne@69
|
272 * uint32_t value;
|
jpayne@69
|
273 * while ((end = ucptrie_getRange(trie, start, UCPMAP_RANGE_NORMAL, 0,
|
jpayne@69
|
274 * NULL, NULL, &value)) >= 0) {
|
jpayne@69
|
275 * // Work with the range start..end and its value.
|
jpayne@69
|
276 * start = end + 1;
|
jpayne@69
|
277 * }
|
jpayne@69
|
278 * \endcode
|
jpayne@69
|
279 *
|
jpayne@69
|
280 * @param trie the trie
|
jpayne@69
|
281 * @param start range start
|
jpayne@69
|
282 * @param option defines whether surrogates are treated normally,
|
jpayne@69
|
283 * or as having the surrogateValue; usually UCPMAP_RANGE_NORMAL
|
jpayne@69
|
284 * @param surrogateValue value for surrogates; ignored if option==UCPMAP_RANGE_NORMAL
|
jpayne@69
|
285 * @param filter a pointer to a function that may modify the trie data value,
|
jpayne@69
|
286 * or NULL if the values from the trie are to be used unmodified
|
jpayne@69
|
287 * @param context an opaque pointer that is passed on to the filter function
|
jpayne@69
|
288 * @param pValue if not NULL, receives the value that every code point start..end has;
|
jpayne@69
|
289 * may have been modified by filter(context, trie value)
|
jpayne@69
|
290 * if that function pointer is not NULL
|
jpayne@69
|
291 * @return the range end code point, or -1 if start is not a valid code point
|
jpayne@69
|
292 * @stable ICU 63
|
jpayne@69
|
293 */
|
jpayne@69
|
294 U_CAPI UChar32 U_EXPORT2
|
jpayne@69
|
295 ucptrie_getRange(const UCPTrie *trie, UChar32 start,
|
jpayne@69
|
296 UCPMapRangeOption option, uint32_t surrogateValue,
|
jpayne@69
|
297 UCPMapValueFilter *filter, const void *context, uint32_t *pValue);
|
jpayne@69
|
298
|
jpayne@69
|
299 /**
|
jpayne@69
|
300 * Writes a memory-mappable form of the trie into 32-bit aligned memory.
|
jpayne@69
|
301 * Inverse of ucptrie_openFromBinary().
|
jpayne@69
|
302 *
|
jpayne@69
|
303 * @param trie the trie
|
jpayne@69
|
304 * @param data a pointer to 32-bit-aligned memory to be filled with the trie data;
|
jpayne@69
|
305 * can be NULL if capacity==0
|
jpayne@69
|
306 * @param capacity the number of bytes available at data, or 0 for pure preflighting
|
jpayne@69
|
307 * @param pErrorCode an in/out ICU UErrorCode;
|
jpayne@69
|
308 * U_BUFFER_OVERFLOW_ERROR if the capacity is too small
|
jpayne@69
|
309 * @return the number of bytes written or (if buffer overflow) needed for the trie
|
jpayne@69
|
310 *
|
jpayne@69
|
311 * @see ucptrie_openFromBinary()
|
jpayne@69
|
312 * @stable ICU 63
|
jpayne@69
|
313 */
|
jpayne@69
|
314 U_CAPI int32_t U_EXPORT2
|
jpayne@69
|
315 ucptrie_toBinary(const UCPTrie *trie, void *data, int32_t capacity, UErrorCode *pErrorCode);
|
jpayne@69
|
316
|
jpayne@69
|
317 /**
|
jpayne@69
|
318 * Macro parameter value for a trie with 16-bit data values.
|
jpayne@69
|
319 * Use the name of this macro as a "dataAccess" parameter in other macros.
|
jpayne@69
|
320 * Do not use this macro in any other way.
|
jpayne@69
|
321 *
|
jpayne@69
|
322 * @see UCPTRIE_VALUE_BITS_16
|
jpayne@69
|
323 * @stable ICU 63
|
jpayne@69
|
324 */
|
jpayne@69
|
325 #define UCPTRIE_16(trie, i) ((trie)->data.ptr16[i])
|
jpayne@69
|
326
|
jpayne@69
|
327 /**
|
jpayne@69
|
328 * Macro parameter value for a trie with 32-bit data values.
|
jpayne@69
|
329 * Use the name of this macro as a "dataAccess" parameter in other macros.
|
jpayne@69
|
330 * Do not use this macro in any other way.
|
jpayne@69
|
331 *
|
jpayne@69
|
332 * @see UCPTRIE_VALUE_BITS_32
|
jpayne@69
|
333 * @stable ICU 63
|
jpayne@69
|
334 */
|
jpayne@69
|
335 #define UCPTRIE_32(trie, i) ((trie)->data.ptr32[i])
|
jpayne@69
|
336
|
jpayne@69
|
337 /**
|
jpayne@69
|
338 * Macro parameter value for a trie with 8-bit data values.
|
jpayne@69
|
339 * Use the name of this macro as a "dataAccess" parameter in other macros.
|
jpayne@69
|
340 * Do not use this macro in any other way.
|
jpayne@69
|
341 *
|
jpayne@69
|
342 * @see UCPTRIE_VALUE_BITS_8
|
jpayne@69
|
343 * @stable ICU 63
|
jpayne@69
|
344 */
|
jpayne@69
|
345 #define UCPTRIE_8(trie, i) ((trie)->data.ptr8[i])
|
jpayne@69
|
346
|
jpayne@69
|
347 /**
|
jpayne@69
|
348 * Returns a trie value for a code point, with range checking.
|
jpayne@69
|
349 * Returns the trie error value if c is not in the range 0..U+10FFFF.
|
jpayne@69
|
350 *
|
jpayne@69
|
351 * @param trie (const UCPTrie *, in) the trie; must have type UCPTRIE_TYPE_FAST
|
jpayne@69
|
352 * @param dataAccess UCPTRIE_16, UCPTRIE_32, or UCPTRIE_8 according to the trie’s value width
|
jpayne@69
|
353 * @param c (UChar32, in) the input code point
|
jpayne@69
|
354 * @return The code point's trie value.
|
jpayne@69
|
355 * @stable ICU 63
|
jpayne@69
|
356 */
|
jpayne@69
|
357 #define UCPTRIE_FAST_GET(trie, dataAccess, c) dataAccess(trie, _UCPTRIE_CP_INDEX(trie, 0xffff, c))
|
jpayne@69
|
358
|
jpayne@69
|
359 /**
|
jpayne@69
|
360 * Returns a 16-bit trie value for a code point, with range checking.
|
jpayne@69
|
361 * Returns the trie error value if c is not in the range U+0000..U+10FFFF.
|
jpayne@69
|
362 *
|
jpayne@69
|
363 * @param trie (const UCPTrie *, in) the trie; must have type UCPTRIE_TYPE_SMALL
|
jpayne@69
|
364 * @param dataAccess UCPTRIE_16, UCPTRIE_32, or UCPTRIE_8 according to the trie’s value width
|
jpayne@69
|
365 * @param c (UChar32, in) the input code point
|
jpayne@69
|
366 * @return The code point's trie value.
|
jpayne@69
|
367 * @stable ICU 63
|
jpayne@69
|
368 */
|
jpayne@69
|
369 #define UCPTRIE_SMALL_GET(trie, dataAccess, c) \
|
jpayne@69
|
370 dataAccess(trie, _UCPTRIE_CP_INDEX(trie, UCPTRIE_SMALL_MAX, c))
|
jpayne@69
|
371
|
jpayne@69
|
372 /**
|
jpayne@69
|
373 * UTF-16: Reads the next code point (UChar32 c, out), post-increments src,
|
jpayne@69
|
374 * and gets a value from the trie.
|
jpayne@69
|
375 * Sets the trie error value if c is an unpaired surrogate.
|
jpayne@69
|
376 *
|
jpayne@69
|
377 * @param trie (const UCPTrie *, in) the trie; must have type UCPTRIE_TYPE_FAST
|
jpayne@69
|
378 * @param dataAccess UCPTRIE_16, UCPTRIE_32, or UCPTRIE_8 according to the trie’s value width
|
jpayne@69
|
379 * @param src (const UChar *, in/out) the source text pointer
|
jpayne@69
|
380 * @param limit (const UChar *, in) the limit pointer for the text, or NULL if NUL-terminated
|
jpayne@69
|
381 * @param c (UChar32, out) variable for the code point
|
jpayne@69
|
382 * @param result (out) variable for the trie lookup result
|
jpayne@69
|
383 * @stable ICU 63
|
jpayne@69
|
384 */
|
jpayne@69
|
385 #define UCPTRIE_FAST_U16_NEXT(trie, dataAccess, src, limit, c, result) UPRV_BLOCK_MACRO_BEGIN { \
|
jpayne@69
|
386 (c) = *(src)++; \
|
jpayne@69
|
387 int32_t __index; \
|
jpayne@69
|
388 if (!U16_IS_SURROGATE(c)) { \
|
jpayne@69
|
389 __index = _UCPTRIE_FAST_INDEX(trie, c); \
|
jpayne@69
|
390 } else { \
|
jpayne@69
|
391 uint16_t __c2; \
|
jpayne@69
|
392 if (U16_IS_SURROGATE_LEAD(c) && (src) != (limit) && U16_IS_TRAIL(__c2 = *(src))) { \
|
jpayne@69
|
393 ++(src); \
|
jpayne@69
|
394 (c) = U16_GET_SUPPLEMENTARY((c), __c2); \
|
jpayne@69
|
395 __index = _UCPTRIE_SMALL_INDEX(trie, c); \
|
jpayne@69
|
396 } else { \
|
jpayne@69
|
397 __index = (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET; \
|
jpayne@69
|
398 } \
|
jpayne@69
|
399 } \
|
jpayne@69
|
400 (result) = dataAccess(trie, __index); \
|
jpayne@69
|
401 } UPRV_BLOCK_MACRO_END
|
jpayne@69
|
402
|
jpayne@69
|
403 /**
|
jpayne@69
|
404 * UTF-16: Reads the previous code point (UChar32 c, out), pre-decrements src,
|
jpayne@69
|
405 * and gets a value from the trie.
|
jpayne@69
|
406 * Sets the trie error value if c is an unpaired surrogate.
|
jpayne@69
|
407 *
|
jpayne@69
|
408 * @param trie (const UCPTrie *, in) the trie; must have type UCPTRIE_TYPE_FAST
|
jpayne@69
|
409 * @param dataAccess UCPTRIE_16, UCPTRIE_32, or UCPTRIE_8 according to the trie’s value width
|
jpayne@69
|
410 * @param start (const UChar *, in) the start pointer for the text
|
jpayne@69
|
411 * @param src (const UChar *, in/out) the source text pointer
|
jpayne@69
|
412 * @param c (UChar32, out) variable for the code point
|
jpayne@69
|
413 * @param result (out) variable for the trie lookup result
|
jpayne@69
|
414 * @stable ICU 63
|
jpayne@69
|
415 */
|
jpayne@69
|
416 #define UCPTRIE_FAST_U16_PREV(trie, dataAccess, start, src, c, result) UPRV_BLOCK_MACRO_BEGIN { \
|
jpayne@69
|
417 (c) = *--(src); \
|
jpayne@69
|
418 int32_t __index; \
|
jpayne@69
|
419 if (!U16_IS_SURROGATE(c)) { \
|
jpayne@69
|
420 __index = _UCPTRIE_FAST_INDEX(trie, c); \
|
jpayne@69
|
421 } else { \
|
jpayne@69
|
422 uint16_t __c2; \
|
jpayne@69
|
423 if (U16_IS_SURROGATE_TRAIL(c) && (src) != (start) && U16_IS_LEAD(__c2 = *((src) - 1))) { \
|
jpayne@69
|
424 --(src); \
|
jpayne@69
|
425 (c) = U16_GET_SUPPLEMENTARY(__c2, (c)); \
|
jpayne@69
|
426 __index = _UCPTRIE_SMALL_INDEX(trie, c); \
|
jpayne@69
|
427 } else { \
|
jpayne@69
|
428 __index = (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET; \
|
jpayne@69
|
429 } \
|
jpayne@69
|
430 } \
|
jpayne@69
|
431 (result) = dataAccess(trie, __index); \
|
jpayne@69
|
432 } UPRV_BLOCK_MACRO_END
|
jpayne@69
|
433
|
jpayne@69
|
434 /**
|
jpayne@69
|
435 * UTF-8: Post-increments src and gets a value from the trie.
|
jpayne@69
|
436 * Sets the trie error value for an ill-formed byte sequence.
|
jpayne@69
|
437 *
|
jpayne@69
|
438 * Unlike UCPTRIE_FAST_U16_NEXT() this UTF-8 macro does not provide the code point
|
jpayne@69
|
439 * because it would be more work to do so and is often not needed.
|
jpayne@69
|
440 * If the trie value differs from the error value, then the byte sequence is well-formed,
|
jpayne@69
|
441 * and the code point can be assembled without revalidation.
|
jpayne@69
|
442 *
|
jpayne@69
|
443 * @param trie (const UCPTrie *, in) the trie; must have type UCPTRIE_TYPE_FAST
|
jpayne@69
|
444 * @param dataAccess UCPTRIE_16, UCPTRIE_32, or UCPTRIE_8 according to the trie’s value width
|
jpayne@69
|
445 * @param src (const char *, in/out) the source text pointer
|
jpayne@69
|
446 * @param limit (const char *, in) the limit pointer for the text (must not be NULL)
|
jpayne@69
|
447 * @param result (out) variable for the trie lookup result
|
jpayne@69
|
448 * @stable ICU 63
|
jpayne@69
|
449 */
|
jpayne@69
|
450 #define UCPTRIE_FAST_U8_NEXT(trie, dataAccess, src, limit, result) UPRV_BLOCK_MACRO_BEGIN { \
|
jpayne@69
|
451 int32_t __lead = (uint8_t)*(src)++; \
|
jpayne@69
|
452 if (!U8_IS_SINGLE(__lead)) { \
|
jpayne@69
|
453 uint8_t __t1, __t2, __t3; \
|
jpayne@69
|
454 if ((src) != (limit) && \
|
jpayne@69
|
455 (__lead >= 0xe0 ? \
|
jpayne@69
|
456 __lead < 0xf0 ? /* U+0800..U+FFFF except surrogates */ \
|
jpayne@69
|
457 U8_LEAD3_T1_BITS[__lead &= 0xf] & (1 << ((__t1 = *(src)) >> 5)) && \
|
jpayne@69
|
458 ++(src) != (limit) && (__t2 = *(src) - 0x80) <= 0x3f && \
|
jpayne@69
|
459 (__lead = ((int32_t)(trie)->index[(__lead << 6) + (__t1 & 0x3f)]) + __t2, 1) \
|
jpayne@69
|
460 : /* U+10000..U+10FFFF */ \
|
jpayne@69
|
461 (__lead -= 0xf0) <= 4 && \
|
jpayne@69
|
462 U8_LEAD4_T1_BITS[(__t1 = *(src)) >> 4] & (1 << __lead) && \
|
jpayne@69
|
463 (__lead = (__lead << 6) | (__t1 & 0x3f), ++(src) != (limit)) && \
|
jpayne@69
|
464 (__t2 = *(src) - 0x80) <= 0x3f && \
|
jpayne@69
|
465 ++(src) != (limit) && (__t3 = *(src) - 0x80) <= 0x3f && \
|
jpayne@69
|
466 (__lead = __lead >= (trie)->shifted12HighStart ? \
|
jpayne@69
|
467 (trie)->dataLength - UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET : \
|
jpayne@69
|
468 ucptrie_internalSmallU8Index((trie), __lead, __t2, __t3), 1) \
|
jpayne@69
|
469 : /* U+0080..U+07FF */ \
|
jpayne@69
|
470 __lead >= 0xc2 && (__t1 = *(src) - 0x80) <= 0x3f && \
|
jpayne@69
|
471 (__lead = (int32_t)(trie)->index[__lead & 0x1f] + __t1, 1))) { \
|
jpayne@69
|
472 ++(src); \
|
jpayne@69
|
473 } else { \
|
jpayne@69
|
474 __lead = (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET; /* ill-formed*/ \
|
jpayne@69
|
475 } \
|
jpayne@69
|
476 } \
|
jpayne@69
|
477 (result) = dataAccess(trie, __lead); \
|
jpayne@69
|
478 } UPRV_BLOCK_MACRO_END
|
jpayne@69
|
479
|
jpayne@69
|
480 /**
|
jpayne@69
|
481 * UTF-8: Pre-decrements src and gets a value from the trie.
|
jpayne@69
|
482 * Sets the trie error value for an ill-formed byte sequence.
|
jpayne@69
|
483 *
|
jpayne@69
|
484 * Unlike UCPTRIE_FAST_U16_PREV() this UTF-8 macro does not provide the code point
|
jpayne@69
|
485 * because it would be more work to do so and is often not needed.
|
jpayne@69
|
486 * If the trie value differs from the error value, then the byte sequence is well-formed,
|
jpayne@69
|
487 * and the code point can be assembled without revalidation.
|
jpayne@69
|
488 *
|
jpayne@69
|
489 * @param trie (const UCPTrie *, in) the trie; must have type UCPTRIE_TYPE_FAST
|
jpayne@69
|
490 * @param dataAccess UCPTRIE_16, UCPTRIE_32, or UCPTRIE_8 according to the trie’s value width
|
jpayne@69
|
491 * @param start (const char *, in) the start pointer for the text
|
jpayne@69
|
492 * @param src (const char *, in/out) the source text pointer
|
jpayne@69
|
493 * @param result (out) variable for the trie lookup result
|
jpayne@69
|
494 * @stable ICU 63
|
jpayne@69
|
495 */
|
jpayne@69
|
496 #define UCPTRIE_FAST_U8_PREV(trie, dataAccess, start, src, result) UPRV_BLOCK_MACRO_BEGIN { \
|
jpayne@69
|
497 int32_t __index = (uint8_t)*--(src); \
|
jpayne@69
|
498 if (!U8_IS_SINGLE(__index)) { \
|
jpayne@69
|
499 __index = ucptrie_internalU8PrevIndex((trie), __index, (const uint8_t *)(start), \
|
jpayne@69
|
500 (const uint8_t *)(src)); \
|
jpayne@69
|
501 (src) -= __index & 7; \
|
jpayne@69
|
502 __index >>= 3; \
|
jpayne@69
|
503 } \
|
jpayne@69
|
504 (result) = dataAccess(trie, __index); \
|
jpayne@69
|
505 } UPRV_BLOCK_MACRO_END
|
jpayne@69
|
506
|
jpayne@69
|
507 /**
|
jpayne@69
|
508 * Returns a trie value for an ASCII code point, without range checking.
|
jpayne@69
|
509 *
|
jpayne@69
|
510 * @param trie (const UCPTrie *, in) the trie (of either fast or small type)
|
jpayne@69
|
511 * @param dataAccess UCPTRIE_16, UCPTRIE_32, or UCPTRIE_8 according to the trie’s value width
|
jpayne@69
|
512 * @param c (UChar32, in) the input code point; must be U+0000..U+007F
|
jpayne@69
|
513 * @return The ASCII code point's trie value.
|
jpayne@69
|
514 * @stable ICU 63
|
jpayne@69
|
515 */
|
jpayne@69
|
516 #define UCPTRIE_ASCII_GET(trie, dataAccess, c) dataAccess(trie, c)
|
jpayne@69
|
517
|
jpayne@69
|
518 /**
|
jpayne@69
|
519 * Returns a trie value for a BMP code point (U+0000..U+FFFF), without range checking.
|
jpayne@69
|
520 * Can be used to look up a value for a UTF-16 code unit if other parts of
|
jpayne@69
|
521 * the string processing check for surrogates.
|
jpayne@69
|
522 *
|
jpayne@69
|
523 * @param trie (const UCPTrie *, in) the trie; must have type UCPTRIE_TYPE_FAST
|
jpayne@69
|
524 * @param dataAccess UCPTRIE_16, UCPTRIE_32, or UCPTRIE_8 according to the trie’s value width
|
jpayne@69
|
525 * @param c (UChar32, in) the input code point, must be U+0000..U+FFFF
|
jpayne@69
|
526 * @return The BMP code point's trie value.
|
jpayne@69
|
527 * @stable ICU 63
|
jpayne@69
|
528 */
|
jpayne@69
|
529 #define UCPTRIE_FAST_BMP_GET(trie, dataAccess, c) dataAccess(trie, _UCPTRIE_FAST_INDEX(trie, c))
|
jpayne@69
|
530
|
jpayne@69
|
531 /**
|
jpayne@69
|
532 * Returns a trie value for a supplementary code point (U+10000..U+10FFFF),
|
jpayne@69
|
533 * without range checking.
|
jpayne@69
|
534 *
|
jpayne@69
|
535 * @param trie (const UCPTrie *, in) the trie; must have type UCPTRIE_TYPE_FAST
|
jpayne@69
|
536 * @param dataAccess UCPTRIE_16, UCPTRIE_32, or UCPTRIE_8 according to the trie’s value width
|
jpayne@69
|
537 * @param c (UChar32, in) the input code point, must be U+10000..U+10FFFF
|
jpayne@69
|
538 * @return The supplementary code point's trie value.
|
jpayne@69
|
539 * @stable ICU 63
|
jpayne@69
|
540 */
|
jpayne@69
|
541 #define UCPTRIE_FAST_SUPP_GET(trie, dataAccess, c) dataAccess(trie, _UCPTRIE_SMALL_INDEX(trie, c))
|
jpayne@69
|
542
|
jpayne@69
|
543 /* Internal definitions ----------------------------------------------------- */
|
jpayne@69
|
544
|
jpayne@69
|
545 #ifndef U_IN_DOXYGEN
|
jpayne@69
|
546
|
jpayne@69
|
547 /**
|
jpayne@69
|
548 * Internal implementation constants.
|
jpayne@69
|
549 * These are needed for the API macros, but users should not use these directly.
|
jpayne@69
|
550 * @internal
|
jpayne@69
|
551 */
|
jpayne@69
|
552 enum {
|
jpayne@69
|
553 /** @internal */
|
jpayne@69
|
554 UCPTRIE_FAST_SHIFT = 6,
|
jpayne@69
|
555
|
jpayne@69
|
556 /** Number of entries in a data block for code points below the fast limit. 64=0x40 @internal */
|
jpayne@69
|
557 UCPTRIE_FAST_DATA_BLOCK_LENGTH = 1 << UCPTRIE_FAST_SHIFT,
|
jpayne@69
|
558
|
jpayne@69
|
559 /** Mask for getting the lower bits for the in-fast-data-block offset. @internal */
|
jpayne@69
|
560 UCPTRIE_FAST_DATA_MASK = UCPTRIE_FAST_DATA_BLOCK_LENGTH - 1,
|
jpayne@69
|
561
|
jpayne@69
|
562 /** @internal */
|
jpayne@69
|
563 UCPTRIE_SMALL_MAX = 0xfff,
|
jpayne@69
|
564
|
jpayne@69
|
565 /**
|
jpayne@69
|
566 * Offset from dataLength (to be subtracted) for fetching the
|
jpayne@69
|
567 * value returned for out-of-range code points and ill-formed UTF-8/16.
|
jpayne@69
|
568 * @internal
|
jpayne@69
|
569 */
|
jpayne@69
|
570 UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET = 1,
|
jpayne@69
|
571 /**
|
jpayne@69
|
572 * Offset from dataLength (to be subtracted) for fetching the
|
jpayne@69
|
573 * value returned for code points highStart..U+10FFFF.
|
jpayne@69
|
574 * @internal
|
jpayne@69
|
575 */
|
jpayne@69
|
576 UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET = 2
|
jpayne@69
|
577 };
|
jpayne@69
|
578
|
jpayne@69
|
579 /* Internal functions and macros -------------------------------------------- */
|
jpayne@69
|
580 // Do not conditionalize with #ifndef U_HIDE_INTERNAL_API, needed for public API
|
jpayne@69
|
581
|
jpayne@69
|
582 /** @internal */
|
jpayne@69
|
583 U_INTERNAL int32_t U_EXPORT2
|
jpayne@69
|
584 ucptrie_internalSmallIndex(const UCPTrie *trie, UChar32 c);
|
jpayne@69
|
585
|
jpayne@69
|
586 /** @internal */
|
jpayne@69
|
587 U_INTERNAL int32_t U_EXPORT2
|
jpayne@69
|
588 ucptrie_internalSmallU8Index(const UCPTrie *trie, int32_t lt1, uint8_t t2, uint8_t t3);
|
jpayne@69
|
589
|
jpayne@69
|
590 /**
|
jpayne@69
|
591 * Internal function for part of the UCPTRIE_FAST_U8_PREVxx() macro implementations.
|
jpayne@69
|
592 * Do not call directly.
|
jpayne@69
|
593 * @internal
|
jpayne@69
|
594 */
|
jpayne@69
|
595 U_INTERNAL int32_t U_EXPORT2
|
jpayne@69
|
596 ucptrie_internalU8PrevIndex(const UCPTrie *trie, UChar32 c,
|
jpayne@69
|
597 const uint8_t *start, const uint8_t *src);
|
jpayne@69
|
598
|
jpayne@69
|
599 /** Internal trie getter for a code point below the fast limit. Returns the data index. @internal */
|
jpayne@69
|
600 #define _UCPTRIE_FAST_INDEX(trie, c) \
|
jpayne@69
|
601 ((int32_t)(trie)->index[(c) >> UCPTRIE_FAST_SHIFT] + ((c) & UCPTRIE_FAST_DATA_MASK))
|
jpayne@69
|
602
|
jpayne@69
|
603 /** Internal trie getter for a code point at or above the fast limit. Returns the data index. @internal */
|
jpayne@69
|
604 #define _UCPTRIE_SMALL_INDEX(trie, c) \
|
jpayne@69
|
605 ((c) >= (trie)->highStart ? \
|
jpayne@69
|
606 (trie)->dataLength - UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET : \
|
jpayne@69
|
607 ucptrie_internalSmallIndex(trie, c))
|
jpayne@69
|
608
|
jpayne@69
|
609 /**
|
jpayne@69
|
610 * Internal trie getter for a code point, with checking that c is in U+0000..10FFFF.
|
jpayne@69
|
611 * Returns the data index.
|
jpayne@69
|
612 * @internal
|
jpayne@69
|
613 */
|
jpayne@69
|
614 #define _UCPTRIE_CP_INDEX(trie, fastMax, c) \
|
jpayne@69
|
615 ((uint32_t)(c) <= (uint32_t)(fastMax) ? \
|
jpayne@69
|
616 _UCPTRIE_FAST_INDEX(trie, c) : \
|
jpayne@69
|
617 (uint32_t)(c) <= 0x10ffff ? \
|
jpayne@69
|
618 _UCPTRIE_SMALL_INDEX(trie, c) : \
|
jpayne@69
|
619 (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET)
|
jpayne@69
|
620
|
jpayne@69
|
621 U_CDECL_END
|
jpayne@69
|
622
|
jpayne@69
|
623 #endif // U_IN_DOXYGEN
|
jpayne@69
|
624
|
jpayne@69
|
625 #if U_SHOW_CPLUSPLUS_API
|
jpayne@69
|
626
|
jpayne@69
|
627 U_NAMESPACE_BEGIN
|
jpayne@69
|
628
|
jpayne@69
|
629 /**
|
jpayne@69
|
630 * \class LocalUCPTriePointer
|
jpayne@69
|
631 * "Smart pointer" class, closes a UCPTrie via ucptrie_close().
|
jpayne@69
|
632 * For most methods see the LocalPointerBase base class.
|
jpayne@69
|
633 *
|
jpayne@69
|
634 * @see LocalPointerBase
|
jpayne@69
|
635 * @see LocalPointer
|
jpayne@69
|
636 * @stable ICU 63
|
jpayne@69
|
637 */
|
jpayne@69
|
638 U_DEFINE_LOCAL_OPEN_POINTER(LocalUCPTriePointer, UCPTrie, ucptrie_close);
|
jpayne@69
|
639
|
jpayne@69
|
640 U_NAMESPACE_END
|
jpayne@69
|
641
|
jpayne@69
|
642 #endif // U_SHOW_CPLUSPLUS_API
|
jpayne@69
|
643
|
jpayne@69
|
644 #endif
|