1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 // ucptrie.h (modified from utrie2.h)
5 // created: 2017dec29 Markus W. Scherer
10 #include "unicode/utypes.h"
12 #include "unicode/localpointer.h"
13 #include "unicode/ucpmap.h"
14 #include "unicode/utf8.h"
21 * This file defines an immutable Unicode code point trie.
29 typedef union UCPTrieData
{
33 const uint16_t *ptr16
;
35 const uint32_t *ptr32
;
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
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.
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.
53 * The macros will return bogus values, or may crash, if used on the wrong type or value width.
61 const uint16_t *index
;
69 /** Start of the last range which ends at U+10FFFF. @internal */
71 /** highStart>>12 @internal */
72 uint16_t shifted12HighStart
;
75 int8_t type
; // UCPTrieType
77 int8_t valueWidth
; // UCPTrieValueWidth
79 /** padding/reserved @internal */
81 /** padding/reserved @internal */
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.
89 uint16_t index3NullOffset
;
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.
95 int32_t dataNullOffset
;
106 typedef struct UCPTrie UCPTrie
;
110 * Selectors for the type of a UCPTrie.
111 * Different trade-offs for size vs. speed.
113 * @see umutablecptrie_buildImmutable
114 * @see ucptrie_openFromBinary
115 * @see ucptrie_getType
120 * For ucptrie_openFromBinary() to accept any type.
121 * ucptrie_getType() will return the actual type.
124 UCPTRIE_TYPE_ANY
= -1,
126 * Fast/simple/larger BMP data structure. Use functions and "fast" macros.
131 * Small/slower BMP data structure. Use functions and "small" macros.
137 typedef enum UCPTrieType UCPTrieType
;
141 * Selectors for the number of bits in a UCPTrie data value.
143 * @see umutablecptrie_buildImmutable
144 * @see ucptrie_openFromBinary
145 * @see ucptrie_getValueWidth
148 enum UCPTrieValueWidth
{
150 * For ucptrie_openFromBinary() to accept any data value width.
151 * ucptrie_getValueWidth() will return the actual data value width.
154 UCPTRIE_VALUE_BITS_ANY
= -1,
156 * The trie stores 16 bits per data value.
157 * It returns them as unsigned values 0..0xffff=65535.
160 UCPTRIE_VALUE_BITS_16
,
162 * The trie stores 32 bits per data value.
165 UCPTRIE_VALUE_BITS_32
,
167 * The trie stores 8 bits per data value.
168 * It returns them as unsigned values 0..0xff=255.
174 typedef enum UCPTrieValueWidth UCPTrieValueWidth
;
178 * Opens a trie from its binary form, stored in 32-bit-aligned memory.
179 * Inverse of ucptrie_toBinary().
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.
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;
195 * @param pErrorCode an in/out ICU UErrorCode
198 * @see umutablecptrie_open
199 * @see umutablecptrie_buildImmutable
200 * @see ucptrie_toBinary
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
);
209 * Closes a trie and releases associated memory.
211 * @param trie the trie
214 U_CAPI
void U_EXPORT2
215 ucptrie_close(UCPTrie
*trie
);
218 * Returns the trie type.
220 * @param trie the trie
221 * @return the trie type
222 * @see ucptrie_openFromBinary
223 * @see UCPTRIE_TYPE_ANY
226 U_CAPI UCPTrieType U_EXPORT2
227 ucptrie_getType(const UCPTrie
*trie
);
230 * Returns the number of bits in a trie data value.
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
238 U_CAPI UCPTrieValueWidth U_EXPORT2
239 ucptrie_getValueWidth(const UCPTrie
*trie
);
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.
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.
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
255 U_CAPI
uint32_t U_EXPORT2
256 ucptrie_get(const UCPTrie
*trie
, UChar32 c
);
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.)
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.
271 * UChar32 start = 0, end;
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.
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
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
);
300 * Writes a memory-mappable form of the trie into 32-bit aligned memory.
301 * Inverse of ucptrie_openFromBinary().
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
311 * @see ucptrie_openFromBinary()
314 U_CAPI
int32_t U_EXPORT2
315 ucptrie_toBinary(const UCPTrie
*trie
, void *data
, int32_t capacity
, UErrorCode
*pErrorCode
);
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.
322 * @see UCPTRIE_VALUE_BITS_16
325 #define UCPTRIE_16(trie, i) ((trie)->data.ptr16[i])
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.
332 * @see UCPTRIE_VALUE_BITS_32
335 #define UCPTRIE_32(trie, i) ((trie)->data.ptr32[i])
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.
342 * @see UCPTRIE_VALUE_BITS_8
345 #define UCPTRIE_8(trie, i) ((trie)->data.ptr8[i])
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.
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.
357 #define UCPTRIE_FAST_GET(trie, dataAccess, c) dataAccess(trie, _UCPTRIE_CP_INDEX(trie, 0xffff, c))
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.
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.
369 #define UCPTRIE_SMALL_GET(trie, dataAccess, c) \
370 dataAccess(trie, _UCPTRIE_CP_INDEX(trie, UCPTRIE_SMALL_MAX, c))
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.
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
385 #define UCPTRIE_FAST_U16_NEXT(trie, dataAccess, src, limit, c, result) UPRV_BLOCK_MACRO_BEGIN { \
388 if (!U16_IS_SURROGATE(c)) { \
389 __index = _UCPTRIE_FAST_INDEX(trie, c); \
392 if (U16_IS_SURROGATE_LEAD(c) && (src) != (limit) && U16_IS_TRAIL(__c2 = *(src))) { \
394 (c) = U16_GET_SUPPLEMENTARY((c), __c2); \
395 __index = _UCPTRIE_SMALL_INDEX(trie, c); \
397 __index = (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET; \
400 (result) = dataAccess(trie, __index); \
401 } UPRV_BLOCK_MACRO_END
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.
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
416 #define UCPTRIE_FAST_U16_PREV(trie, dataAccess, start, src, c, result) UPRV_BLOCK_MACRO_BEGIN { \
419 if (!U16_IS_SURROGATE(c)) { \
420 __index = _UCPTRIE_FAST_INDEX(trie, c); \
423 if (U16_IS_SURROGATE_TRAIL(c) && (src) != (start) && U16_IS_LEAD(__c2 = *((src) - 1))) { \
425 (c) = U16_GET_SUPPLEMENTARY(__c2, (c)); \
426 __index = _UCPTRIE_SMALL_INDEX(trie, c); \
428 __index = (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET; \
431 (result) = dataAccess(trie, __index); \
432 } UPRV_BLOCK_MACRO_END
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.
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.
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
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) && \
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))) { \
474 __lead = (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET; /* ill-formed*/ \
477 (result) = dataAccess(trie, __lead); \
478 } UPRV_BLOCK_MACRO_END
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.
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.
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
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; \
504 (result) = dataAccess(trie, __index); \
505 } UPRV_BLOCK_MACRO_END
508 * Returns a trie value for an ASCII code point, without range checking.
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.
516 #define UCPTRIE_ASCII_GET(trie, dataAccess, c) dataAccess(trie, c)
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.
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.
529 #define UCPTRIE_FAST_BMP_GET(trie, dataAccess, c) dataAccess(trie, _UCPTRIE_FAST_INDEX(trie, c))
532 * Returns a trie value for a supplementary code point (U+10000..U+10FFFF),
533 * without range checking.
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.
541 #define UCPTRIE_FAST_SUPP_GET(trie, dataAccess, c) dataAccess(trie, _UCPTRIE_SMALL_INDEX(trie, c))
543 /* Internal definitions ----------------------------------------------------- */
548 * Internal implementation constants.
549 * These are needed for the API macros, but users should not use these directly.
554 UCPTRIE_FAST_SHIFT
= 6,
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
,
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,
563 UCPTRIE_SMALL_MAX
= 0xfff,
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.
570 UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET
= 1,
572 * Offset from dataLength (to be subtracted) for fetching the
573 * value returned for code points highStart..U+10FFFF.
576 UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET
= 2
579 /* Internal functions and macros -------------------------------------------- */
580 // Do not conditionalize with #ifndef U_HIDE_INTERNAL_API, needed for public API
583 U_INTERNAL
int32_t U_EXPORT2
584 ucptrie_internalSmallIndex(const UCPTrie
*trie
, UChar32 c
);
587 U_INTERNAL
int32_t U_EXPORT2
588 ucptrie_internalSmallU8Index(const UCPTrie
*trie
, int32_t lt1
, uint8_t t2
, uint8_t t3
);
591 * Internal function for part of the UCPTRIE_FAST_U8_PREVxx() macro implementations.
592 * Do not call directly.
595 U_INTERNAL
int32_t U_EXPORT2
596 ucptrie_internalU8PrevIndex(const UCPTrie
*trie
, UChar32 c
,
597 const uint8_t *start
, const uint8_t *src
);
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))
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))
610 * Internal trie getter for a code point, with checking that c is in U+0000..10FFFF.
611 * Returns the data index.
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)
623 #endif // U_IN_DOXYGEN
625 #if U_SHOW_CPLUSPLUS_API
630 * \class LocalUCPTriePointer
631 * "Smart pointer" class, closes a UCPTrie via ucptrie_close().
632 * For most methods see the LocalPointerBase base class.
634 * @see LocalPointerBase
638 U_DEFINE_LOCAL_OPEN_POINTER(LocalUCPTriePointer
, UCPTrie
, ucptrie_close
);
642 #endif // U_SHOW_CPLUSPLUS_API