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