Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 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" |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 11 | #include "unicode/ucpmap.h" |
| 12 | #include "unicode/utf8.h" |
| 13 | |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 14 | #if U_SHOW_CPLUSPLUS_API |
| 15 | #include "unicode/localpointer.h" |
| 16 | #endif // U_SHOW_CPLUSPLUS_API |
| 17 | |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 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. |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame^] | 46 | * For details see https://icu.unicode.org/design/struct/utrie |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 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 |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 58 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 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 |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 118 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 119 | */ |
| 120 | enum UCPTrieType { |
| 121 | /** |
| 122 | * For ucptrie_openFromBinary() to accept any type. |
| 123 | * ucptrie_getType() will return the actual type. |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 124 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 125 | */ |
| 126 | UCPTRIE_TYPE_ANY = -1, |
| 127 | /** |
| 128 | * Fast/simple/larger BMP data structure. Use functions and "fast" macros. |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 129 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 130 | */ |
| 131 | UCPTRIE_TYPE_FAST, |
| 132 | /** |
| 133 | * Small/slower BMP data structure. Use functions and "small" macros. |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 134 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 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 |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 148 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 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. |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 154 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 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. |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 160 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 161 | */ |
| 162 | UCPTRIE_VALUE_BITS_16, |
| 163 | /** |
| 164 | * The trie stores 32 bits per data value. |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 165 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 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. |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 171 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 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 |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 203 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 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 |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 214 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 215 | */ |
| 216 | U_CAPI void U_EXPORT2 |
| 217 | ucptrie_close(UCPTrie *trie); |
| 218 | |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 219 | /** |
| 220 | * Returns the trie type. |
| 221 | * |
| 222 | * @param trie the trie |
| 223 | * @return the trie type |
| 224 | * @see ucptrie_openFromBinary |
| 225 | * @see UCPTRIE_TYPE_ANY |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 226 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 227 | */ |
| 228 | U_CAPI UCPTrieType U_EXPORT2 |
| 229 | ucptrie_getType(const UCPTrie *trie); |
| 230 | |
| 231 | /** |
| 232 | * Returns the number of bits in a trie data value. |
| 233 | * |
| 234 | * @param trie the trie |
| 235 | * @return the number of bits in a trie data value |
| 236 | * @see ucptrie_openFromBinary |
| 237 | * @see UCPTRIE_VALUE_BITS_ANY |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 238 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 239 | */ |
| 240 | U_CAPI UCPTrieValueWidth U_EXPORT2 |
| 241 | ucptrie_getValueWidth(const UCPTrie *trie); |
| 242 | |
| 243 | /** |
| 244 | * Returns the value for a code point as stored in the trie, with range checking. |
| 245 | * Returns the trie error value if c is not in the range 0..U+10FFFF. |
| 246 | * |
| 247 | * Easier to use than UCPTRIE_FAST_GET() and similar macros but slower. |
| 248 | * Easier to use because, unlike the macros, this function works on all UCPTrie |
| 249 | * objects, for all types and value widths. |
| 250 | * |
| 251 | * @param trie the trie |
| 252 | * @param c the code point |
| 253 | * @return the trie value, |
| 254 | * or the trie error value if the code point is not in the range 0..U+10FFFF |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 255 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 256 | */ |
| 257 | U_CAPI uint32_t U_EXPORT2 |
| 258 | ucptrie_get(const UCPTrie *trie, UChar32 c); |
| 259 | |
| 260 | /** |
| 261 | * Returns the last code point such that all those from start to there have the same value. |
| 262 | * Can be used to efficiently iterate over all same-value ranges in a trie. |
| 263 | * (This is normally faster than iterating over code points and get()ting each value, |
| 264 | * but much slower than a data structure that stores ranges directly.) |
| 265 | * |
| 266 | * If the UCPMapValueFilter function pointer is not NULL, then |
| 267 | * the value to be delivered is passed through that function, and the return value is the end |
| 268 | * of the range where all values are modified to the same actual value. |
| 269 | * The value is unchanged if that function pointer is NULL. |
| 270 | * |
| 271 | * Example: |
| 272 | * \code |
| 273 | * UChar32 start = 0, end; |
| 274 | * uint32_t value; |
| 275 | * while ((end = ucptrie_getRange(trie, start, UCPMAP_RANGE_NORMAL, 0, |
| 276 | * NULL, NULL, &value)) >= 0) { |
| 277 | * // Work with the range start..end and its value. |
| 278 | * start = end + 1; |
| 279 | * } |
| 280 | * \endcode |
| 281 | * |
| 282 | * @param trie the trie |
| 283 | * @param start range start |
| 284 | * @param option defines whether surrogates are treated normally, |
| 285 | * or as having the surrogateValue; usually UCPMAP_RANGE_NORMAL |
| 286 | * @param surrogateValue value for surrogates; ignored if option==UCPMAP_RANGE_NORMAL |
| 287 | * @param filter a pointer to a function that may modify the trie data value, |
| 288 | * or NULL if the values from the trie are to be used unmodified |
| 289 | * @param context an opaque pointer that is passed on to the filter function |
| 290 | * @param pValue if not NULL, receives the value that every code point start..end has; |
| 291 | * may have been modified by filter(context, trie value) |
| 292 | * if that function pointer is not NULL |
| 293 | * @return the range end code point, or -1 if start is not a valid code point |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 294 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 295 | */ |
| 296 | U_CAPI UChar32 U_EXPORT2 |
| 297 | ucptrie_getRange(const UCPTrie *trie, UChar32 start, |
| 298 | UCPMapRangeOption option, uint32_t surrogateValue, |
| 299 | UCPMapValueFilter *filter, const void *context, uint32_t *pValue); |
| 300 | |
| 301 | /** |
| 302 | * Writes a memory-mappable form of the trie into 32-bit aligned memory. |
| 303 | * Inverse of ucptrie_openFromBinary(). |
| 304 | * |
| 305 | * @param trie the trie |
| 306 | * @param data a pointer to 32-bit-aligned memory to be filled with the trie data; |
| 307 | * can be NULL if capacity==0 |
| 308 | * @param capacity the number of bytes available at data, or 0 for pure preflighting |
| 309 | * @param pErrorCode an in/out ICU UErrorCode; |
| 310 | * U_BUFFER_OVERFLOW_ERROR if the capacity is too small |
| 311 | * @return the number of bytes written or (if buffer overflow) needed for the trie |
| 312 | * |
| 313 | * @see ucptrie_openFromBinary() |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 314 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 315 | */ |
| 316 | U_CAPI int32_t U_EXPORT2 |
| 317 | ucptrie_toBinary(const UCPTrie *trie, void *data, int32_t capacity, UErrorCode *pErrorCode); |
| 318 | |
| 319 | /** |
| 320 | * Macro parameter value for a trie with 16-bit data values. |
| 321 | * Use the name of this macro as a "dataAccess" parameter in other macros. |
| 322 | * Do not use this macro in any other way. |
| 323 | * |
| 324 | * @see UCPTRIE_VALUE_BITS_16 |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 325 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 326 | */ |
| 327 | #define UCPTRIE_16(trie, i) ((trie)->data.ptr16[i]) |
| 328 | |
| 329 | /** |
| 330 | * Macro parameter value for a trie with 32-bit data values. |
| 331 | * Use the name of this macro as a "dataAccess" parameter in other macros. |
| 332 | * Do not use this macro in any other way. |
| 333 | * |
| 334 | * @see UCPTRIE_VALUE_BITS_32 |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 335 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 336 | */ |
| 337 | #define UCPTRIE_32(trie, i) ((trie)->data.ptr32[i]) |
| 338 | |
| 339 | /** |
| 340 | * Macro parameter value for a trie with 8-bit data values. |
| 341 | * Use the name of this macro as a "dataAccess" parameter in other macros. |
| 342 | * Do not use this macro in any other way. |
| 343 | * |
| 344 | * @see UCPTRIE_VALUE_BITS_8 |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 345 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 346 | */ |
| 347 | #define UCPTRIE_8(trie, i) ((trie)->data.ptr8[i]) |
| 348 | |
| 349 | /** |
| 350 | * Returns a trie value for a code point, with range checking. |
| 351 | * Returns the trie error value if c is not in the range 0..U+10FFFF. |
| 352 | * |
| 353 | * @param trie (const UCPTrie *, in) the trie; must have type UCPTRIE_TYPE_FAST |
| 354 | * @param dataAccess UCPTRIE_16, UCPTRIE_32, or UCPTRIE_8 according to the trie’s value width |
| 355 | * @param c (UChar32, in) the input code point |
| 356 | * @return The code point's trie value. |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 357 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 358 | */ |
| 359 | #define UCPTRIE_FAST_GET(trie, dataAccess, c) dataAccess(trie, _UCPTRIE_CP_INDEX(trie, 0xffff, c)) |
| 360 | |
| 361 | /** |
| 362 | * Returns a 16-bit trie value for a code point, with range checking. |
| 363 | * Returns the trie error value if c is not in the range U+0000..U+10FFFF. |
| 364 | * |
| 365 | * @param trie (const UCPTrie *, in) the trie; must have type UCPTRIE_TYPE_SMALL |
| 366 | * @param dataAccess UCPTRIE_16, UCPTRIE_32, or UCPTRIE_8 according to the trie’s value width |
| 367 | * @param c (UChar32, in) the input code point |
| 368 | * @return The code point's trie value. |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 369 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 370 | */ |
| 371 | #define UCPTRIE_SMALL_GET(trie, dataAccess, c) \ |
| 372 | dataAccess(trie, _UCPTRIE_CP_INDEX(trie, UCPTRIE_SMALL_MAX, c)) |
| 373 | |
| 374 | /** |
| 375 | * UTF-16: Reads the next code point (UChar32 c, out), post-increments src, |
| 376 | * and gets a value from the trie. |
| 377 | * Sets the trie error value if c is an unpaired surrogate. |
| 378 | * |
| 379 | * @param trie (const UCPTrie *, in) the trie; must have type UCPTRIE_TYPE_FAST |
| 380 | * @param dataAccess UCPTRIE_16, UCPTRIE_32, or UCPTRIE_8 according to the trie’s value width |
| 381 | * @param src (const UChar *, in/out) the source text pointer |
| 382 | * @param limit (const UChar *, in) the limit pointer for the text, or NULL if NUL-terminated |
| 383 | * @param c (UChar32, out) variable for the code point |
| 384 | * @param result (out) variable for the trie lookup result |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 385 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 386 | */ |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 387 | #define UCPTRIE_FAST_U16_NEXT(trie, dataAccess, src, limit, c, result) UPRV_BLOCK_MACRO_BEGIN { \ |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 388 | (c) = *(src)++; \ |
| 389 | int32_t __index; \ |
| 390 | if (!U16_IS_SURROGATE(c)) { \ |
| 391 | __index = _UCPTRIE_FAST_INDEX(trie, c); \ |
| 392 | } else { \ |
| 393 | uint16_t __c2; \ |
| 394 | if (U16_IS_SURROGATE_LEAD(c) && (src) != (limit) && U16_IS_TRAIL(__c2 = *(src))) { \ |
| 395 | ++(src); \ |
| 396 | (c) = U16_GET_SUPPLEMENTARY((c), __c2); \ |
| 397 | __index = _UCPTRIE_SMALL_INDEX(trie, c); \ |
| 398 | } else { \ |
| 399 | __index = (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET; \ |
| 400 | } \ |
| 401 | } \ |
| 402 | (result) = dataAccess(trie, __index); \ |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 403 | } UPRV_BLOCK_MACRO_END |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 404 | |
| 405 | /** |
| 406 | * UTF-16: Reads the previous code point (UChar32 c, out), pre-decrements src, |
| 407 | * and gets a value from the trie. |
| 408 | * Sets the trie error value if c is an unpaired surrogate. |
| 409 | * |
| 410 | * @param trie (const UCPTrie *, in) the trie; must have type UCPTRIE_TYPE_FAST |
| 411 | * @param dataAccess UCPTRIE_16, UCPTRIE_32, or UCPTRIE_8 according to the trie’s value width |
| 412 | * @param start (const UChar *, in) the start pointer for the text |
| 413 | * @param src (const UChar *, in/out) the source text pointer |
| 414 | * @param c (UChar32, out) variable for the code point |
| 415 | * @param result (out) variable for the trie lookup result |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 416 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 417 | */ |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 418 | #define UCPTRIE_FAST_U16_PREV(trie, dataAccess, start, src, c, result) UPRV_BLOCK_MACRO_BEGIN { \ |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 419 | (c) = *--(src); \ |
| 420 | int32_t __index; \ |
| 421 | if (!U16_IS_SURROGATE(c)) { \ |
| 422 | __index = _UCPTRIE_FAST_INDEX(trie, c); \ |
| 423 | } else { \ |
| 424 | uint16_t __c2; \ |
| 425 | if (U16_IS_SURROGATE_TRAIL(c) && (src) != (start) && U16_IS_LEAD(__c2 = *((src) - 1))) { \ |
| 426 | --(src); \ |
| 427 | (c) = U16_GET_SUPPLEMENTARY(__c2, (c)); \ |
| 428 | __index = _UCPTRIE_SMALL_INDEX(trie, c); \ |
| 429 | } else { \ |
| 430 | __index = (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET; \ |
| 431 | } \ |
| 432 | } \ |
| 433 | (result) = dataAccess(trie, __index); \ |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 434 | } UPRV_BLOCK_MACRO_END |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 435 | |
| 436 | /** |
| 437 | * UTF-8: Post-increments src and gets a value from the trie. |
| 438 | * Sets the trie error value for an ill-formed byte sequence. |
| 439 | * |
| 440 | * Unlike UCPTRIE_FAST_U16_NEXT() this UTF-8 macro does not provide the code point |
| 441 | * because it would be more work to do so and is often not needed. |
| 442 | * If the trie value differs from the error value, then the byte sequence is well-formed, |
| 443 | * and the code point can be assembled without revalidation. |
| 444 | * |
| 445 | * @param trie (const UCPTrie *, in) the trie; must have type UCPTRIE_TYPE_FAST |
| 446 | * @param dataAccess UCPTRIE_16, UCPTRIE_32, or UCPTRIE_8 according to the trie’s value width |
| 447 | * @param src (const char *, in/out) the source text pointer |
| 448 | * @param limit (const char *, in) the limit pointer for the text (must not be NULL) |
| 449 | * @param result (out) variable for the trie lookup result |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 450 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 451 | */ |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 452 | #define UCPTRIE_FAST_U8_NEXT(trie, dataAccess, src, limit, result) UPRV_BLOCK_MACRO_BEGIN { \ |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 453 | int32_t __lead = (uint8_t)*(src)++; \ |
| 454 | if (!U8_IS_SINGLE(__lead)) { \ |
| 455 | uint8_t __t1, __t2, __t3; \ |
| 456 | if ((src) != (limit) && \ |
| 457 | (__lead >= 0xe0 ? \ |
| 458 | __lead < 0xf0 ? /* U+0800..U+FFFF except surrogates */ \ |
| 459 | U8_LEAD3_T1_BITS[__lead &= 0xf] & (1 << ((__t1 = *(src)) >> 5)) && \ |
| 460 | ++(src) != (limit) && (__t2 = *(src) - 0x80) <= 0x3f && \ |
| 461 | (__lead = ((int32_t)(trie)->index[(__lead << 6) + (__t1 & 0x3f)]) + __t2, 1) \ |
| 462 | : /* U+10000..U+10FFFF */ \ |
| 463 | (__lead -= 0xf0) <= 4 && \ |
| 464 | U8_LEAD4_T1_BITS[(__t1 = *(src)) >> 4] & (1 << __lead) && \ |
| 465 | (__lead = (__lead << 6) | (__t1 & 0x3f), ++(src) != (limit)) && \ |
| 466 | (__t2 = *(src) - 0x80) <= 0x3f && \ |
| 467 | ++(src) != (limit) && (__t3 = *(src) - 0x80) <= 0x3f && \ |
| 468 | (__lead = __lead >= (trie)->shifted12HighStart ? \ |
| 469 | (trie)->dataLength - UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET : \ |
| 470 | ucptrie_internalSmallU8Index((trie), __lead, __t2, __t3), 1) \ |
| 471 | : /* U+0080..U+07FF */ \ |
| 472 | __lead >= 0xc2 && (__t1 = *(src) - 0x80) <= 0x3f && \ |
| 473 | (__lead = (int32_t)(trie)->index[__lead & 0x1f] + __t1, 1))) { \ |
| 474 | ++(src); \ |
| 475 | } else { \ |
| 476 | __lead = (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET; /* ill-formed*/ \ |
| 477 | } \ |
| 478 | } \ |
| 479 | (result) = dataAccess(trie, __lead); \ |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 480 | } UPRV_BLOCK_MACRO_END |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 481 | |
| 482 | /** |
| 483 | * UTF-8: Pre-decrements src and gets a value from the trie. |
| 484 | * Sets the trie error value for an ill-formed byte sequence. |
| 485 | * |
| 486 | * Unlike UCPTRIE_FAST_U16_PREV() this UTF-8 macro does not provide the code point |
| 487 | * because it would be more work to do so and is often not needed. |
| 488 | * If the trie value differs from the error value, then the byte sequence is well-formed, |
| 489 | * and the code point can be assembled without revalidation. |
| 490 | * |
| 491 | * @param trie (const UCPTrie *, in) the trie; must have type UCPTRIE_TYPE_FAST |
| 492 | * @param dataAccess UCPTRIE_16, UCPTRIE_32, or UCPTRIE_8 according to the trie’s value width |
| 493 | * @param start (const char *, in) the start pointer for the text |
| 494 | * @param src (const char *, in/out) the source text pointer |
| 495 | * @param result (out) variable for the trie lookup result |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 496 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 497 | */ |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 498 | #define UCPTRIE_FAST_U8_PREV(trie, dataAccess, start, src, result) UPRV_BLOCK_MACRO_BEGIN { \ |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 499 | int32_t __index = (uint8_t)*--(src); \ |
| 500 | if (!U8_IS_SINGLE(__index)) { \ |
| 501 | __index = ucptrie_internalU8PrevIndex((trie), __index, (const uint8_t *)(start), \ |
| 502 | (const uint8_t *)(src)); \ |
| 503 | (src) -= __index & 7; \ |
| 504 | __index >>= 3; \ |
| 505 | } \ |
| 506 | (result) = dataAccess(trie, __index); \ |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 507 | } UPRV_BLOCK_MACRO_END |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 508 | |
| 509 | /** |
| 510 | * Returns a trie value for an ASCII code point, without range checking. |
| 511 | * |
| 512 | * @param trie (const UCPTrie *, in) the trie (of either fast or small type) |
| 513 | * @param dataAccess UCPTRIE_16, UCPTRIE_32, or UCPTRIE_8 according to the trie’s value width |
| 514 | * @param c (UChar32, in) the input code point; must be U+0000..U+007F |
| 515 | * @return The ASCII code point's trie value. |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 516 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 517 | */ |
| 518 | #define UCPTRIE_ASCII_GET(trie, dataAccess, c) dataAccess(trie, c) |
| 519 | |
| 520 | /** |
| 521 | * Returns a trie value for a BMP code point (U+0000..U+FFFF), without range checking. |
| 522 | * Can be used to look up a value for a UTF-16 code unit if other parts of |
| 523 | * the string processing check for surrogates. |
| 524 | * |
| 525 | * @param trie (const UCPTrie *, in) the trie; must have type UCPTRIE_TYPE_FAST |
| 526 | * @param dataAccess UCPTRIE_16, UCPTRIE_32, or UCPTRIE_8 according to the trie’s value width |
| 527 | * @param c (UChar32, in) the input code point, must be U+0000..U+FFFF |
| 528 | * @return The BMP code point's trie value. |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 529 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 530 | */ |
| 531 | #define UCPTRIE_FAST_BMP_GET(trie, dataAccess, c) dataAccess(trie, _UCPTRIE_FAST_INDEX(trie, c)) |
| 532 | |
| 533 | /** |
| 534 | * Returns a trie value for a supplementary code point (U+10000..U+10FFFF), |
| 535 | * without range checking. |
| 536 | * |
| 537 | * @param trie (const UCPTrie *, in) the trie; must have type UCPTRIE_TYPE_FAST |
| 538 | * @param dataAccess UCPTRIE_16, UCPTRIE_32, or UCPTRIE_8 according to the trie’s value width |
| 539 | * @param c (UChar32, in) the input code point, must be U+10000..U+10FFFF |
| 540 | * @return The supplementary code point's trie value. |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 541 | * @stable ICU 63 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 542 | */ |
| 543 | #define UCPTRIE_FAST_SUPP_GET(trie, dataAccess, c) dataAccess(trie, _UCPTRIE_SMALL_INDEX(trie, c)) |
| 544 | |
| 545 | /* Internal definitions ----------------------------------------------------- */ |
| 546 | |
| 547 | #ifndef U_IN_DOXYGEN |
| 548 | |
| 549 | /** |
| 550 | * Internal implementation constants. |
| 551 | * These are needed for the API macros, but users should not use these directly. |
| 552 | * @internal |
| 553 | */ |
| 554 | enum { |
| 555 | /** @internal */ |
| 556 | UCPTRIE_FAST_SHIFT = 6, |
| 557 | |
| 558 | /** Number of entries in a data block for code points below the fast limit. 64=0x40 @internal */ |
| 559 | UCPTRIE_FAST_DATA_BLOCK_LENGTH = 1 << UCPTRIE_FAST_SHIFT, |
| 560 | |
| 561 | /** Mask for getting the lower bits for the in-fast-data-block offset. @internal */ |
| 562 | UCPTRIE_FAST_DATA_MASK = UCPTRIE_FAST_DATA_BLOCK_LENGTH - 1, |
| 563 | |
| 564 | /** @internal */ |
| 565 | UCPTRIE_SMALL_MAX = 0xfff, |
| 566 | |
| 567 | /** |
| 568 | * Offset from dataLength (to be subtracted) for fetching the |
| 569 | * value returned for out-of-range code points and ill-formed UTF-8/16. |
| 570 | * @internal |
| 571 | */ |
| 572 | UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET = 1, |
| 573 | /** |
| 574 | * Offset from dataLength (to be subtracted) for fetching the |
| 575 | * value returned for code points highStart..U+10FFFF. |
| 576 | * @internal |
| 577 | */ |
| 578 | UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET = 2 |
| 579 | }; |
| 580 | |
| 581 | /* Internal functions and macros -------------------------------------------- */ |
| 582 | // Do not conditionalize with #ifndef U_HIDE_INTERNAL_API, needed for public API |
| 583 | |
| 584 | /** @internal */ |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 585 | U_CAPI int32_t U_EXPORT2 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 586 | ucptrie_internalSmallIndex(const UCPTrie *trie, UChar32 c); |
| 587 | |
| 588 | /** @internal */ |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 589 | U_CAPI int32_t U_EXPORT2 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 590 | ucptrie_internalSmallU8Index(const UCPTrie *trie, int32_t lt1, uint8_t t2, uint8_t t3); |
| 591 | |
| 592 | /** |
| 593 | * Internal function for part of the UCPTRIE_FAST_U8_PREVxx() macro implementations. |
| 594 | * Do not call directly. |
| 595 | * @internal |
| 596 | */ |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 597 | U_CAPI int32_t U_EXPORT2 |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 598 | ucptrie_internalU8PrevIndex(const UCPTrie *trie, UChar32 c, |
| 599 | const uint8_t *start, const uint8_t *src); |
| 600 | |
| 601 | /** Internal trie getter for a code point below the fast limit. Returns the data index. @internal */ |
| 602 | #define _UCPTRIE_FAST_INDEX(trie, c) \ |
| 603 | ((int32_t)(trie)->index[(c) >> UCPTRIE_FAST_SHIFT] + ((c) & UCPTRIE_FAST_DATA_MASK)) |
| 604 | |
| 605 | /** Internal trie getter for a code point at or above the fast limit. Returns the data index. @internal */ |
| 606 | #define _UCPTRIE_SMALL_INDEX(trie, c) \ |
| 607 | ((c) >= (trie)->highStart ? \ |
| 608 | (trie)->dataLength - UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET : \ |
| 609 | ucptrie_internalSmallIndex(trie, c)) |
| 610 | |
| 611 | /** |
| 612 | * Internal trie getter for a code point, with checking that c is in U+0000..10FFFF. |
| 613 | * Returns the data index. |
| 614 | * @internal |
| 615 | */ |
| 616 | #define _UCPTRIE_CP_INDEX(trie, fastMax, c) \ |
| 617 | ((uint32_t)(c) <= (uint32_t)(fastMax) ? \ |
| 618 | _UCPTRIE_FAST_INDEX(trie, c) : \ |
| 619 | (uint32_t)(c) <= 0x10ffff ? \ |
| 620 | _UCPTRIE_SMALL_INDEX(trie, c) : \ |
| 621 | (trie)->dataLength - UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET) |
| 622 | |
| 623 | U_CDECL_END |
| 624 | |
| 625 | #endif // U_IN_DOXYGEN |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 626 | |
| 627 | #if U_SHOW_CPLUSPLUS_API |
| 628 | |
| 629 | U_NAMESPACE_BEGIN |
| 630 | |
| 631 | /** |
| 632 | * \class LocalUCPTriePointer |
| 633 | * "Smart pointer" class, closes a UCPTrie via ucptrie_close(). |
| 634 | * For most methods see the LocalPointerBase base class. |
| 635 | * |
| 636 | * @see LocalPointerBase |
| 637 | * @see LocalPointer |
| 638 | * @stable ICU 63 |
| 639 | */ |
| 640 | U_DEFINE_LOCAL_OPEN_POINTER(LocalUCPTriePointer, UCPTrie, ucptrie_close); |
| 641 | |
| 642 | U_NAMESPACE_END |
| 643 | |
| 644 | #endif // U_SHOW_CPLUSPLUS_API |
| 645 | |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 646 | #endif |