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