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