intl/icu/source/common/utrie2.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 ******************************************************************************
michael@0 3 *
michael@0 4 * Copyright (C) 2001-2013, International Business Machines
michael@0 5 * Corporation and others. All Rights Reserved.
michael@0 6 *
michael@0 7 ******************************************************************************
michael@0 8 * file name: utrie2.h
michael@0 9 * encoding: US-ASCII
michael@0 10 * tab size: 8 (not used)
michael@0 11 * indentation:4
michael@0 12 *
michael@0 13 * created on: 2008aug16 (starting from a copy of utrie.h)
michael@0 14 * created by: Markus W. Scherer
michael@0 15 */
michael@0 16
michael@0 17 #ifndef __UTRIE2_H__
michael@0 18 #define __UTRIE2_H__
michael@0 19
michael@0 20 #include "unicode/utypes.h"
michael@0 21 #include "putilimp.h"
michael@0 22 #include "udataswp.h"
michael@0 23
michael@0 24 U_CDECL_BEGIN
michael@0 25
michael@0 26 struct UTrie; /* forward declaration */
michael@0 27 #ifndef __UTRIE_H__
michael@0 28 typedef struct UTrie UTrie;
michael@0 29 #endif
michael@0 30
michael@0 31 /**
michael@0 32 * \file
michael@0 33 *
michael@0 34 * This is a common implementation of a Unicode trie.
michael@0 35 * It is a kind of compressed, serializable table of 16- or 32-bit values associated with
michael@0 36 * Unicode code points (0..0x10ffff). (A map from code points to integers.)
michael@0 37 *
michael@0 38 * This is the second common version of a Unicode trie (hence the name UTrie2).
michael@0 39 * Compared with UTrie version 1:
michael@0 40 * - Still splitting BMP code points 11:5 bits for index and data table lookups.
michael@0 41 * - Still separate data for lead surrogate code _units_ vs. code _points_,
michael@0 42 * but the lead surrogate code unit values are not required any more
michael@0 43 * for data lookup for supplementary code points.
michael@0 44 * - The "folding" mechanism is removed. In UTrie version 1, this somewhat
michael@0 45 * hard-to-explain mechanism was meant to be used for optimized UTF-16
michael@0 46 * processing, with application-specific encoding of indexing bits
michael@0 47 * in the lead surrogate data for the associated supplementary code points.
michael@0 48 * - For the last single-value code point range (ending with U+10ffff),
michael@0 49 * the starting code point ("highStart") and the value are stored.
michael@0 50 * - For supplementary code points U+10000..highStart-1 a three-table lookup
michael@0 51 * (two index tables and one data table) is used. The first index
michael@0 52 * is truncated, omitting both the BMP portion and the high range.
michael@0 53 * - There is a special small index for 2-byte UTF-8, and the initial data
michael@0 54 * entries are designed for fast 1/2-byte UTF-8 lookup.
michael@0 55 */
michael@0 56
michael@0 57 /**
michael@0 58 * Trie structure.
michael@0 59 * Use only with public API macros and functions.
michael@0 60 */
michael@0 61 struct UTrie2;
michael@0 62 typedef struct UTrie2 UTrie2;
michael@0 63
michael@0 64 /* Public UTrie2 API functions: read-only access ---------------------------- */
michael@0 65
michael@0 66 /**
michael@0 67 * Selectors for the width of a UTrie2 data value.
michael@0 68 */
michael@0 69 enum UTrie2ValueBits {
michael@0 70 /** 16 bits per UTrie2 data value. */
michael@0 71 UTRIE2_16_VALUE_BITS,
michael@0 72 /** 32 bits per UTrie2 data value. */
michael@0 73 UTRIE2_32_VALUE_BITS,
michael@0 74 /** Number of selectors for the width of UTrie2 data values. */
michael@0 75 UTRIE2_COUNT_VALUE_BITS
michael@0 76 };
michael@0 77 typedef enum UTrie2ValueBits UTrie2ValueBits;
michael@0 78
michael@0 79 /**
michael@0 80 * Open a frozen trie from its serialized from, stored in 32-bit-aligned memory.
michael@0 81 * Inverse of utrie2_serialize().
michael@0 82 * The memory must remain valid and unchanged as long as the trie is used.
michael@0 83 * You must utrie2_close() the trie once you are done using it.
michael@0 84 *
michael@0 85 * @param valueBits selects the data entry size; results in an
michael@0 86 * U_INVALID_FORMAT_ERROR if it does not match the serialized form
michael@0 87 * @param data a pointer to 32-bit-aligned memory containing the serialized form of a UTrie2
michael@0 88 * @param length the number of bytes available at data;
michael@0 89 * can be more than necessary
michael@0 90 * @param pActualLength receives the actual number of bytes at data taken up by the trie data;
michael@0 91 * can be NULL
michael@0 92 * @param pErrorCode an in/out ICU UErrorCode
michael@0 93 * @return the unserialized trie
michael@0 94 *
michael@0 95 * @see utrie2_open
michael@0 96 * @see utrie2_serialize
michael@0 97 */
michael@0 98 U_CAPI UTrie2 * U_EXPORT2
michael@0 99 utrie2_openFromSerialized(UTrie2ValueBits valueBits,
michael@0 100 const void *data, int32_t length, int32_t *pActualLength,
michael@0 101 UErrorCode *pErrorCode);
michael@0 102
michael@0 103 /**
michael@0 104 * Open a frozen, empty "dummy" trie.
michael@0 105 * A dummy trie is an empty trie, used when a real data trie cannot
michael@0 106 * be loaded. Equivalent to calling utrie2_open() and utrie2_freeze(),
michael@0 107 * but without internally creating and compacting/serializing the
michael@0 108 * builder data structure.
michael@0 109 *
michael@0 110 * The trie always returns the initialValue,
michael@0 111 * or the errorValue for out-of-range code points and illegal UTF-8.
michael@0 112 *
michael@0 113 * You must utrie2_close() the trie once you are done using it.
michael@0 114 *
michael@0 115 * @param valueBits selects the data entry size
michael@0 116 * @param initialValue the initial value that is set for all code points
michael@0 117 * @param errorValue the value for out-of-range code points and illegal UTF-8
michael@0 118 * @param pErrorCode an in/out ICU UErrorCode
michael@0 119 * @return the dummy trie
michael@0 120 *
michael@0 121 * @see utrie2_openFromSerialized
michael@0 122 * @see utrie2_open
michael@0 123 */
michael@0 124 U_CAPI UTrie2 * U_EXPORT2
michael@0 125 utrie2_openDummy(UTrie2ValueBits valueBits,
michael@0 126 uint32_t initialValue, uint32_t errorValue,
michael@0 127 UErrorCode *pErrorCode);
michael@0 128
michael@0 129 /**
michael@0 130 * Get a value from a code point as stored in the trie.
michael@0 131 * Easier to use than UTRIE2_GET16() and UTRIE2_GET32() but slower.
michael@0 132 * Easier to use because, unlike the macros, this function works on all UTrie2
michael@0 133 * objects, frozen or not, holding 16-bit or 32-bit data values.
michael@0 134 *
michael@0 135 * @param trie the trie
michael@0 136 * @param c the code point
michael@0 137 * @return the value
michael@0 138 */
michael@0 139 U_CAPI uint32_t U_EXPORT2
michael@0 140 utrie2_get32(const UTrie2 *trie, UChar32 c);
michael@0 141
michael@0 142 /* enumeration callback types */
michael@0 143
michael@0 144 /**
michael@0 145 * Callback from utrie2_enum(), extracts a uint32_t value from a
michael@0 146 * trie value. This value will be passed on to the UTrie2EnumRange function.
michael@0 147 *
michael@0 148 * @param context an opaque pointer, as passed into utrie2_enum()
michael@0 149 * @param value a value from the trie
michael@0 150 * @return the value that is to be passed on to the UTrie2EnumRange function
michael@0 151 */
michael@0 152 typedef uint32_t U_CALLCONV
michael@0 153 UTrie2EnumValue(const void *context, uint32_t value);
michael@0 154
michael@0 155 /**
michael@0 156 * Callback from utrie2_enum(), is called for each contiguous range
michael@0 157 * of code points with the same value as retrieved from the trie and
michael@0 158 * transformed by the UTrie2EnumValue function.
michael@0 159 *
michael@0 160 * The callback function can stop the enumeration by returning FALSE.
michael@0 161 *
michael@0 162 * @param context an opaque pointer, as passed into utrie2_enum()
michael@0 163 * @param start the first code point in a contiguous range with value
michael@0 164 * @param end the last code point in a contiguous range with value (inclusive)
michael@0 165 * @param value the value that is set for all code points in [start..end]
michael@0 166 * @return FALSE to stop the enumeration
michael@0 167 */
michael@0 168 typedef UBool U_CALLCONV
michael@0 169 UTrie2EnumRange(const void *context, UChar32 start, UChar32 end, uint32_t value);
michael@0 170
michael@0 171 /**
michael@0 172 * Enumerate efficiently all values in a trie.
michael@0 173 * Do not modify the trie during the enumeration.
michael@0 174 *
michael@0 175 * For each entry in the trie, the value to be delivered is passed through
michael@0 176 * the UTrie2EnumValue function.
michael@0 177 * The value is unchanged if that function pointer is NULL.
michael@0 178 *
michael@0 179 * For each contiguous range of code points with a given (transformed) value,
michael@0 180 * the UTrie2EnumRange function is called.
michael@0 181 *
michael@0 182 * @param trie a pointer to the trie
michael@0 183 * @param enumValue a pointer to a function that may transform the trie entry value,
michael@0 184 * or NULL if the values from the trie are to be used directly
michael@0 185 * @param enumRange a pointer to a function that is called for each contiguous range
michael@0 186 * of code points with the same (transformed) value
michael@0 187 * @param context an opaque pointer that is passed on to the callback functions
michael@0 188 */
michael@0 189 U_CAPI void U_EXPORT2
michael@0 190 utrie2_enum(const UTrie2 *trie,
michael@0 191 UTrie2EnumValue *enumValue, UTrie2EnumRange *enumRange, const void *context);
michael@0 192
michael@0 193 /* Building a trie ---------------------------------------------------------- */
michael@0 194
michael@0 195 /**
michael@0 196 * Open an empty, writable trie. At build time, 32-bit data values are used.
michael@0 197 * utrie2_freeze() takes a valueBits parameter
michael@0 198 * which determines the data value width in the serialized and frozen forms.
michael@0 199 * You must utrie2_close() the trie once you are done using it.
michael@0 200 *
michael@0 201 * @param initialValue the initial value that is set for all code points
michael@0 202 * @param errorValue the value for out-of-range code points and illegal UTF-8
michael@0 203 * @param pErrorCode an in/out ICU UErrorCode
michael@0 204 * @return a pointer to the allocated and initialized new trie
michael@0 205 */
michael@0 206 U_CAPI UTrie2 * U_EXPORT2
michael@0 207 utrie2_open(uint32_t initialValue, uint32_t errorValue, UErrorCode *pErrorCode);
michael@0 208
michael@0 209 /**
michael@0 210 * Clone a trie.
michael@0 211 * You must utrie2_close() the clone once you are done using it.
michael@0 212 *
michael@0 213 * @param other the trie to clone
michael@0 214 * @param pErrorCode an in/out ICU UErrorCode
michael@0 215 * @return a pointer to the new trie clone
michael@0 216 */
michael@0 217 U_CAPI UTrie2 * U_EXPORT2
michael@0 218 utrie2_clone(const UTrie2 *other, UErrorCode *pErrorCode);
michael@0 219
michael@0 220 /**
michael@0 221 * Clone a trie. The clone will be mutable/writable even if the other trie
michael@0 222 * is frozen. (See utrie2_freeze().)
michael@0 223 * You must utrie2_close() the clone once you are done using it.
michael@0 224 *
michael@0 225 * @param other the trie to clone
michael@0 226 * @param pErrorCode an in/out ICU UErrorCode
michael@0 227 * @return a pointer to the new trie clone
michael@0 228 */
michael@0 229 U_CAPI UTrie2 * U_EXPORT2
michael@0 230 utrie2_cloneAsThawed(const UTrie2 *other, UErrorCode *pErrorCode);
michael@0 231
michael@0 232 /**
michael@0 233 * Close a trie and release associated memory.
michael@0 234 *
michael@0 235 * @param trie the trie
michael@0 236 */
michael@0 237 U_CAPI void U_EXPORT2
michael@0 238 utrie2_close(UTrie2 *trie);
michael@0 239
michael@0 240 /**
michael@0 241 * Set a value for a code point.
michael@0 242 *
michael@0 243 * @param trie the unfrozen trie
michael@0 244 * @param c the code point
michael@0 245 * @param value the value
michael@0 246 * @param pErrorCode an in/out ICU UErrorCode; among other possible error codes:
michael@0 247 * - U_NO_WRITE_PERMISSION if the trie is frozen
michael@0 248 */
michael@0 249 U_CAPI void U_EXPORT2
michael@0 250 utrie2_set32(UTrie2 *trie, UChar32 c, uint32_t value, UErrorCode *pErrorCode);
michael@0 251
michael@0 252 /**
michael@0 253 * Set a value in a range of code points [start..end].
michael@0 254 * All code points c with start<=c<=end will get the value if
michael@0 255 * overwrite is TRUE or if the old value is the initial value.
michael@0 256 *
michael@0 257 * @param trie the unfrozen trie
michael@0 258 * @param start the first code point to get the value
michael@0 259 * @param end the last code point to get the value (inclusive)
michael@0 260 * @param value the value
michael@0 261 * @param overwrite flag for whether old non-initial values are to be overwritten
michael@0 262 * @param pErrorCode an in/out ICU UErrorCode; among other possible error codes:
michael@0 263 * - U_NO_WRITE_PERMISSION if the trie is frozen
michael@0 264 */
michael@0 265 U_CAPI void U_EXPORT2
michael@0 266 utrie2_setRange32(UTrie2 *trie,
michael@0 267 UChar32 start, UChar32 end,
michael@0 268 uint32_t value, UBool overwrite,
michael@0 269 UErrorCode *pErrorCode);
michael@0 270
michael@0 271 /**
michael@0 272 * Freeze a trie. Make it immutable (read-only) and compact it,
michael@0 273 * ready for serialization and for use with fast macros.
michael@0 274 * Functions to set values will fail after serializing.
michael@0 275 *
michael@0 276 * A trie can be frozen only once. If this function is called again with different
michael@0 277 * valueBits then it will set a U_ILLEGAL_ARGUMENT_ERROR.
michael@0 278 *
michael@0 279 * @param trie the trie
michael@0 280 * @param valueBits selects the data entry size; if smaller than 32 bits, then
michael@0 281 * the values stored in the trie will be truncated
michael@0 282 * @param pErrorCode an in/out ICU UErrorCode; among other possible error codes:
michael@0 283 * - U_INDEX_OUTOFBOUNDS_ERROR if the compacted index or data arrays are too long
michael@0 284 * for serialization
michael@0 285 * (the trie will be immutable and usable,
michael@0 286 * but not frozen and not usable with the fast macros)
michael@0 287 *
michael@0 288 * @see utrie2_cloneAsThawed
michael@0 289 */
michael@0 290 U_CAPI void U_EXPORT2
michael@0 291 utrie2_freeze(UTrie2 *trie, UTrie2ValueBits valueBits, UErrorCode *pErrorCode);
michael@0 292
michael@0 293 /**
michael@0 294 * Test if the trie is frozen. (See utrie2_freeze().)
michael@0 295 *
michael@0 296 * @param trie the trie
michael@0 297 * @return TRUE if the trie is frozen, that is, immutable, ready for serialization
michael@0 298 * and for use with fast macros
michael@0 299 */
michael@0 300 U_CAPI UBool U_EXPORT2
michael@0 301 utrie2_isFrozen(const UTrie2 *trie);
michael@0 302
michael@0 303 /**
michael@0 304 * Serialize a frozen trie into 32-bit aligned memory.
michael@0 305 * If the trie is not frozen, then the function returns with a U_ILLEGAL_ARGUMENT_ERROR.
michael@0 306 * A trie can be serialized multiple times.
michael@0 307 *
michael@0 308 * @param trie the frozen trie
michael@0 309 * @param data a pointer to 32-bit-aligned memory to be filled with the trie data,
michael@0 310 * can be NULL if capacity==0
michael@0 311 * @param capacity the number of bytes available at data,
michael@0 312 * or 0 for preflighting
michael@0 313 * @param pErrorCode an in/out ICU UErrorCode; among other possible error codes:
michael@0 314 * - U_BUFFER_OVERFLOW_ERROR if the data storage block is too small for serialization
michael@0 315 * - U_ILLEGAL_ARGUMENT_ERROR if the trie is not frozen or the data and capacity
michael@0 316 * parameters are bad
michael@0 317 * @return the number of bytes written or needed for the trie
michael@0 318 *
michael@0 319 * @see utrie2_openFromSerialized()
michael@0 320 */
michael@0 321 U_CAPI int32_t U_EXPORT2
michael@0 322 utrie2_serialize(UTrie2 *trie,
michael@0 323 void *data, int32_t capacity,
michael@0 324 UErrorCode *pErrorCode);
michael@0 325
michael@0 326 /* Public UTrie2 API: miscellaneous functions ------------------------------- */
michael@0 327
michael@0 328 /**
michael@0 329 * Get the UTrie version from 32-bit-aligned memory containing the serialized form
michael@0 330 * of either a UTrie (version 1) or a UTrie2 (version 2).
michael@0 331 *
michael@0 332 * @param data a pointer to 32-bit-aligned memory containing the serialized form
michael@0 333 * of a UTrie, version 1 or 2
michael@0 334 * @param length the number of bytes available at data;
michael@0 335 * can be more than necessary (see return value)
michael@0 336 * @param anyEndianOk If FALSE, only platform-endian serialized forms are recognized.
michael@0 337 * If TRUE, opposite-endian serialized forms are recognized as well.
michael@0 338 * @return the UTrie version of the serialized form, or 0 if it is not
michael@0 339 * recognized as a serialized UTrie
michael@0 340 */
michael@0 341 U_CAPI int32_t U_EXPORT2
michael@0 342 utrie2_getVersion(const void *data, int32_t length, UBool anyEndianOk);
michael@0 343
michael@0 344 /**
michael@0 345 * Swap a serialized UTrie2.
michael@0 346 * @internal
michael@0 347 */
michael@0 348 U_CAPI int32_t U_EXPORT2
michael@0 349 utrie2_swap(const UDataSwapper *ds,
michael@0 350 const void *inData, int32_t length, void *outData,
michael@0 351 UErrorCode *pErrorCode);
michael@0 352
michael@0 353 /**
michael@0 354 * Swap a serialized UTrie or UTrie2.
michael@0 355 * @internal
michael@0 356 */
michael@0 357 U_CAPI int32_t U_EXPORT2
michael@0 358 utrie2_swapAnyVersion(const UDataSwapper *ds,
michael@0 359 const void *inData, int32_t length, void *outData,
michael@0 360 UErrorCode *pErrorCode);
michael@0 361
michael@0 362 /**
michael@0 363 * Build a UTrie2 (version 2) from a UTrie (version 1).
michael@0 364 * Enumerates all values in the UTrie and builds a UTrie2 with the same values.
michael@0 365 * The resulting UTrie2 will be frozen.
michael@0 366 *
michael@0 367 * @param trie1 the runtime UTrie structure to be enumerated
michael@0 368 * @param errorValue the value for out-of-range code points and illegal UTF-8
michael@0 369 * @param pErrorCode an in/out ICU UErrorCode
michael@0 370 * @return The frozen UTrie2 with the same values as the UTrie.
michael@0 371 */
michael@0 372 U_CAPI UTrie2 * U_EXPORT2
michael@0 373 utrie2_fromUTrie(const UTrie *trie1, uint32_t errorValue, UErrorCode *pErrorCode);
michael@0 374
michael@0 375 /* Public UTrie2 API macros ------------------------------------------------- */
michael@0 376
michael@0 377 /*
michael@0 378 * These macros provide fast data lookup from a frozen trie.
michael@0 379 * They will crash when used on an unfrozen trie.
michael@0 380 */
michael@0 381
michael@0 382 /**
michael@0 383 * Return a 16-bit trie value from a code point, with range checking.
michael@0 384 * Returns trie->errorValue if c is not in the range 0..U+10ffff.
michael@0 385 *
michael@0 386 * @param trie (const UTrie2 *, in) a frozen trie
michael@0 387 * @param c (UChar32, in) the input code point
michael@0 388 * @return (uint16_t) The code point's trie value.
michael@0 389 */
michael@0 390 #define UTRIE2_GET16(trie, c) _UTRIE2_GET((trie), index, (trie)->indexLength, (c))
michael@0 391
michael@0 392 /**
michael@0 393 * Return a 32-bit trie value from a code point, with range checking.
michael@0 394 * Returns trie->errorValue if c is not in the range 0..U+10ffff.
michael@0 395 *
michael@0 396 * @param trie (const UTrie2 *, in) a frozen trie
michael@0 397 * @param c (UChar32, in) the input code point
michael@0 398 * @return (uint32_t) The code point's trie value.
michael@0 399 */
michael@0 400 #define UTRIE2_GET32(trie, c) _UTRIE2_GET((trie), data32, 0, (c))
michael@0 401
michael@0 402 /**
michael@0 403 * UTF-16: Get the next code point (UChar32 c, out), post-increment src,
michael@0 404 * and get a 16-bit value from the trie.
michael@0 405 *
michael@0 406 * @param trie (const UTrie2 *, in) a frozen trie
michael@0 407 * @param src (const UChar *, in/out) the source text pointer
michael@0 408 * @param limit (const UChar *, in) the limit pointer for the text, or NULL if NUL-terminated
michael@0 409 * @param c (UChar32, out) variable for the code point
michael@0 410 * @param result (uint16_t, out) uint16_t variable for the trie lookup result
michael@0 411 */
michael@0 412 #define UTRIE2_U16_NEXT16(trie, src, limit, c, result) _UTRIE2_U16_NEXT(trie, index, src, limit, c, result)
michael@0 413
michael@0 414 /**
michael@0 415 * UTF-16: Get the next code point (UChar32 c, out), post-increment src,
michael@0 416 * and get a 32-bit value from the trie.
michael@0 417 *
michael@0 418 * @param trie (const UTrie2 *, in) a frozen trie
michael@0 419 * @param src (const UChar *, in/out) the source text pointer
michael@0 420 * @param limit (const UChar *, in) the limit pointer for the text, or NULL if NUL-terminated
michael@0 421 * @param c (UChar32, out) variable for the code point
michael@0 422 * @param result (uint32_t, out) uint32_t variable for the trie lookup result
michael@0 423 */
michael@0 424 #define UTRIE2_U16_NEXT32(trie, src, limit, c, result) _UTRIE2_U16_NEXT(trie, data32, src, limit, c, result)
michael@0 425
michael@0 426 /**
michael@0 427 * UTF-16: Get the previous code point (UChar32 c, out), pre-decrement src,
michael@0 428 * and get a 16-bit value from the trie.
michael@0 429 *
michael@0 430 * @param trie (const UTrie2 *, in) a frozen trie
michael@0 431 * @param start (const UChar *, in) the start pointer for the text
michael@0 432 * @param src (const UChar *, in/out) the source text pointer
michael@0 433 * @param c (UChar32, out) variable for the code point
michael@0 434 * @param result (uint16_t, out) uint16_t variable for the trie lookup result
michael@0 435 */
michael@0 436 #define UTRIE2_U16_PREV16(trie, start, src, c, result) _UTRIE2_U16_PREV(trie, index, start, src, c, result)
michael@0 437
michael@0 438 /**
michael@0 439 * UTF-16: Get the previous code point (UChar32 c, out), pre-decrement src,
michael@0 440 * and get a 32-bit value from the trie.
michael@0 441 *
michael@0 442 * @param trie (const UTrie2 *, in) a frozen trie
michael@0 443 * @param start (const UChar *, in) the start pointer for the text
michael@0 444 * @param src (const UChar *, in/out) the source text pointer
michael@0 445 * @param c (UChar32, out) variable for the code point
michael@0 446 * @param result (uint32_t, out) uint32_t variable for the trie lookup result
michael@0 447 */
michael@0 448 #define UTRIE2_U16_PREV32(trie, start, src, c, result) _UTRIE2_U16_PREV(trie, data32, start, src, c, result)
michael@0 449
michael@0 450 /**
michael@0 451 * UTF-8: Post-increment src and get a 16-bit value from the trie.
michael@0 452 *
michael@0 453 * @param trie (const UTrie2 *, in) a frozen trie
michael@0 454 * @param src (const char *, in/out) the source text pointer
michael@0 455 * @param limit (const char *, in) the limit pointer for the text (must not be NULL)
michael@0 456 * @param result (uint16_t, out) uint16_t variable for the trie lookup result
michael@0 457 */
michael@0 458 #define UTRIE2_U8_NEXT16(trie, src, limit, result)\
michael@0 459 _UTRIE2_U8_NEXT(trie, data16, index, src, limit, result)
michael@0 460
michael@0 461 /**
michael@0 462 * UTF-8: Post-increment src and get a 32-bit value from the trie.
michael@0 463 *
michael@0 464 * @param trie (const UTrie2 *, in) a frozen trie
michael@0 465 * @param src (const char *, in/out) the source text pointer
michael@0 466 * @param limit (const char *, in) the limit pointer for the text (must not be NULL)
michael@0 467 * @param result (uint16_t, out) uint32_t variable for the trie lookup result
michael@0 468 */
michael@0 469 #define UTRIE2_U8_NEXT32(trie, src, limit, result) \
michael@0 470 _UTRIE2_U8_NEXT(trie, data32, data32, src, limit, result)
michael@0 471
michael@0 472 /**
michael@0 473 * UTF-8: Pre-decrement src and get a 16-bit value from the trie.
michael@0 474 *
michael@0 475 * @param trie (const UTrie2 *, in) a frozen trie
michael@0 476 * @param start (const char *, in) the start pointer for the text
michael@0 477 * @param src (const char *, in/out) the source text pointer
michael@0 478 * @param result (uint16_t, out) uint16_t variable for the trie lookup result
michael@0 479 */
michael@0 480 #define UTRIE2_U8_PREV16(trie, start, src, result) \
michael@0 481 _UTRIE2_U8_PREV(trie, data16, index, start, src, result)
michael@0 482
michael@0 483 /**
michael@0 484 * UTF-8: Pre-decrement src and get a 32-bit value from the trie.
michael@0 485 *
michael@0 486 * @param trie (const UTrie2 *, in) a frozen trie
michael@0 487 * @param start (const char *, in) the start pointer for the text
michael@0 488 * @param src (const char *, in/out) the source text pointer
michael@0 489 * @param result (uint16_t, out) uint32_t variable for the trie lookup result
michael@0 490 */
michael@0 491 #define UTRIE2_U8_PREV32(trie, start, src, result) \
michael@0 492 _UTRIE2_U8_PREV(trie, data32, data32, start, src, result)
michael@0 493
michael@0 494 /* Public UTrie2 API: optimized UTF-16 access ------------------------------- */
michael@0 495
michael@0 496 /*
michael@0 497 * The following functions and macros are used for highly optimized UTF-16
michael@0 498 * text processing. The UTRIE2_U16_NEXTxy() macros do not depend on these.
michael@0 499 *
michael@0 500 * A UTrie2 stores separate values for lead surrogate code _units_ vs. code _points_.
michael@0 501 * UTF-16 text processing can be optimized by detecting surrogate pairs and
michael@0 502 * assembling supplementary code points only when there is non-trivial data
michael@0 503 * available.
michael@0 504 *
michael@0 505 * At build-time, use utrie2_enumForLeadSurrogate() to see if there
michael@0 506 * is non-trivial (non-initialValue) data for any of the supplementary
michael@0 507 * code points associated with a lead surrogate.
michael@0 508 * If so, then set a special (application-specific) value for the
michael@0 509 * lead surrogate code _unit_, with utrie2_set32ForLeadSurrogateCodeUnit().
michael@0 510 *
michael@0 511 * At runtime, use UTRIE2_GET16_FROM_U16_SINGLE_LEAD() or
michael@0 512 * UTRIE2_GET32_FROM_U16_SINGLE_LEAD() per code unit. If there is non-trivial
michael@0 513 * data and the code unit is a lead surrogate, then check if a trail surrogate
michael@0 514 * follows. If so, assemble the supplementary code point with
michael@0 515 * U16_GET_SUPPLEMENTARY() and look up its value with UTRIE2_GET16_FROM_SUPP()
michael@0 516 * or UTRIE2_GET32_FROM_SUPP(); otherwise reset the lead
michael@0 517 * surrogate's value or do a code point lookup for it.
michael@0 518 *
michael@0 519 * If there is only trivial data for lead and trail surrogates, then processing
michael@0 520 * can often skip them. For example, in normalization or case mapping
michael@0 521 * all characters that do not have any mappings are simply copied as is.
michael@0 522 */
michael@0 523
michael@0 524 /**
michael@0 525 * Get a value from a lead surrogate code unit as stored in the trie.
michael@0 526 *
michael@0 527 * @param trie the trie
michael@0 528 * @param c the code unit (U+D800..U+DBFF)
michael@0 529 * @return the value
michael@0 530 */
michael@0 531 U_CAPI uint32_t U_EXPORT2
michael@0 532 utrie2_get32FromLeadSurrogateCodeUnit(const UTrie2 *trie, UChar32 c);
michael@0 533
michael@0 534 /**
michael@0 535 * Enumerate the trie values for the 1024=0x400 code points
michael@0 536 * corresponding to a given lead surrogate.
michael@0 537 * For example, for the lead surrogate U+D87E it will enumerate the values
michael@0 538 * for [U+2F800..U+2FC00[.
michael@0 539 * Used by data builder code that sets special lead surrogate code unit values
michael@0 540 * for optimized UTF-16 string processing.
michael@0 541 *
michael@0 542 * Do not modify the trie during the enumeration.
michael@0 543 *
michael@0 544 * Except for the limited code point range, this functions just like utrie2_enum():
michael@0 545 * For each entry in the trie, the value to be delivered is passed through
michael@0 546 * the UTrie2EnumValue function.
michael@0 547 * The value is unchanged if that function pointer is NULL.
michael@0 548 *
michael@0 549 * For each contiguous range of code points with a given (transformed) value,
michael@0 550 * the UTrie2EnumRange function is called.
michael@0 551 *
michael@0 552 * @param trie a pointer to the trie
michael@0 553 * @param enumValue a pointer to a function that may transform the trie entry value,
michael@0 554 * or NULL if the values from the trie are to be used directly
michael@0 555 * @param enumRange a pointer to a function that is called for each contiguous range
michael@0 556 * of code points with the same (transformed) value
michael@0 557 * @param context an opaque pointer that is passed on to the callback functions
michael@0 558 */
michael@0 559 U_CAPI void U_EXPORT2
michael@0 560 utrie2_enumForLeadSurrogate(const UTrie2 *trie, UChar32 lead,
michael@0 561 UTrie2EnumValue *enumValue, UTrie2EnumRange *enumRange,
michael@0 562 const void *context);
michael@0 563
michael@0 564 /**
michael@0 565 * Set a value for a lead surrogate code unit.
michael@0 566 *
michael@0 567 * @param trie the unfrozen trie
michael@0 568 * @param lead the lead surrogate code unit (U+D800..U+DBFF)
michael@0 569 * @param value the value
michael@0 570 * @param pErrorCode an in/out ICU UErrorCode; among other possible error codes:
michael@0 571 * - U_NO_WRITE_PERMISSION if the trie is frozen
michael@0 572 */
michael@0 573 U_CAPI void U_EXPORT2
michael@0 574 utrie2_set32ForLeadSurrogateCodeUnit(UTrie2 *trie,
michael@0 575 UChar32 lead, uint32_t value,
michael@0 576 UErrorCode *pErrorCode);
michael@0 577
michael@0 578 /**
michael@0 579 * Return a 16-bit trie value from a UTF-16 single/lead code unit (<=U+ffff).
michael@0 580 * Same as UTRIE2_GET16() if c is a BMP code point except for lead surrogates,
michael@0 581 * but smaller and faster.
michael@0 582 *
michael@0 583 * @param trie (const UTrie2 *, in) a frozen trie
michael@0 584 * @param c (UChar32, in) the input code unit, must be 0<=c<=U+ffff
michael@0 585 * @return (uint16_t) The code unit's trie value.
michael@0 586 */
michael@0 587 #define UTRIE2_GET16_FROM_U16_SINGLE_LEAD(trie, c) _UTRIE2_GET_FROM_U16_SINGLE_LEAD((trie), index, c)
michael@0 588
michael@0 589 /**
michael@0 590 * Return a 32-bit trie value from a UTF-16 single/lead code unit (<=U+ffff).
michael@0 591 * Same as UTRIE2_GET32() if c is a BMP code point except for lead surrogates,
michael@0 592 * but smaller and faster.
michael@0 593 *
michael@0 594 * @param trie (const UTrie2 *, in) a frozen trie
michael@0 595 * @param c (UChar32, in) the input code unit, must be 0<=c<=U+ffff
michael@0 596 * @return (uint32_t) The code unit's trie value.
michael@0 597 */
michael@0 598 #define UTRIE2_GET32_FROM_U16_SINGLE_LEAD(trie, c) _UTRIE2_GET_FROM_U16_SINGLE_LEAD((trie), data32, c)
michael@0 599
michael@0 600 /**
michael@0 601 * Return a 16-bit trie value from a supplementary code point (U+10000..U+10ffff).
michael@0 602 *
michael@0 603 * @param trie (const UTrie2 *, in) a frozen trie
michael@0 604 * @param c (UChar32, in) the input code point, must be U+10000<=c<=U+10ffff
michael@0 605 * @return (uint16_t) The code point's trie value.
michael@0 606 */
michael@0 607 #define UTRIE2_GET16_FROM_SUPP(trie, c) _UTRIE2_GET_FROM_SUPP((trie), index, c)
michael@0 608
michael@0 609 /**
michael@0 610 * Return a 32-bit trie value from a supplementary code point (U+10000..U+10ffff).
michael@0 611 *
michael@0 612 * @param trie (const UTrie2 *, in) a frozen trie
michael@0 613 * @param c (UChar32, in) the input code point, must be U+10000<=c<=U+10ffff
michael@0 614 * @return (uint32_t) The code point's trie value.
michael@0 615 */
michael@0 616 #define UTRIE2_GET32_FROM_SUPP(trie, c) _UTRIE2_GET_FROM_SUPP((trie), data32, c)
michael@0 617
michael@0 618 U_CDECL_END
michael@0 619
michael@0 620 /* C++ convenience wrappers ------------------------------------------------- */
michael@0 621
michael@0 622 #ifdef __cplusplus
michael@0 623
michael@0 624 #include "unicode/utf.h"
michael@0 625 #include "mutex.h"
michael@0 626
michael@0 627 U_NAMESPACE_BEGIN
michael@0 628
michael@0 629 // Use the Forward/Backward subclasses below.
michael@0 630 class UTrie2StringIterator : public UMemory {
michael@0 631 public:
michael@0 632 UTrie2StringIterator(const UTrie2 *t, const UChar *p) :
michael@0 633 trie(t), codePointStart(p), codePointLimit(p), codePoint(U_SENTINEL) {}
michael@0 634
michael@0 635 const UTrie2 *trie;
michael@0 636 const UChar *codePointStart, *codePointLimit;
michael@0 637 UChar32 codePoint;
michael@0 638 };
michael@0 639
michael@0 640 class BackwardUTrie2StringIterator : public UTrie2StringIterator {
michael@0 641 public:
michael@0 642 BackwardUTrie2StringIterator(const UTrie2 *t, const UChar *s, const UChar *p) :
michael@0 643 UTrie2StringIterator(t, p), start(s) {}
michael@0 644
michael@0 645 uint16_t previous16();
michael@0 646
michael@0 647 const UChar *start;
michael@0 648 };
michael@0 649
michael@0 650 class ForwardUTrie2StringIterator : public UTrie2StringIterator {
michael@0 651 public:
michael@0 652 // Iteration limit l can be NULL.
michael@0 653 // In that case, the caller must detect c==0 and stop.
michael@0 654 ForwardUTrie2StringIterator(const UTrie2 *t, const UChar *p, const UChar *l) :
michael@0 655 UTrie2StringIterator(t, p), limit(l) {}
michael@0 656
michael@0 657 uint16_t next16();
michael@0 658
michael@0 659 const UChar *limit;
michael@0 660 };
michael@0 661
michael@0 662 U_NAMESPACE_END
michael@0 663
michael@0 664 #endif
michael@0 665
michael@0 666 /* Internal definitions ----------------------------------------------------- */
michael@0 667
michael@0 668 U_CDECL_BEGIN
michael@0 669
michael@0 670 /** Build-time trie structure. */
michael@0 671 struct UNewTrie2;
michael@0 672 typedef struct UNewTrie2 UNewTrie2;
michael@0 673
michael@0 674 /*
michael@0 675 * Trie structure definition.
michael@0 676 *
michael@0 677 * Either the data table is 16 bits wide and accessed via the index
michael@0 678 * pointer, with each index item increased by indexLength;
michael@0 679 * in this case, data32==NULL, and data16 is used for direct ASCII access.
michael@0 680 *
michael@0 681 * Or the data table is 32 bits wide and accessed via the data32 pointer.
michael@0 682 */
michael@0 683 struct UTrie2 {
michael@0 684 /* protected: used by macros and functions for reading values */
michael@0 685 const uint16_t *index;
michael@0 686 const uint16_t *data16; /* for fast UTF-8 ASCII access, if 16b data */
michael@0 687 const uint32_t *data32; /* NULL if 16b data is used via index */
michael@0 688
michael@0 689 int32_t indexLength, dataLength;
michael@0 690 uint16_t index2NullOffset; /* 0xffff if there is no dedicated index-2 null block */
michael@0 691 uint16_t dataNullOffset;
michael@0 692 uint32_t initialValue;
michael@0 693 /** Value returned for out-of-range code points and illegal UTF-8. */
michael@0 694 uint32_t errorValue;
michael@0 695
michael@0 696 /* Start of the last range which ends at U+10ffff, and its value. */
michael@0 697 UChar32 highStart;
michael@0 698 int32_t highValueIndex;
michael@0 699
michael@0 700 /* private: used by builder and unserialization functions */
michael@0 701 void *memory; /* serialized bytes; NULL if not frozen yet */
michael@0 702 int32_t length; /* number of serialized bytes at memory; 0 if not frozen yet */
michael@0 703 UBool isMemoryOwned; /* TRUE if the trie owns the memory */
michael@0 704 UBool padding1;
michael@0 705 int16_t padding2;
michael@0 706 UNewTrie2 *newTrie; /* builder object; NULL when frozen */
michael@0 707 };
michael@0 708
michael@0 709 /**
michael@0 710 * Trie constants, defining shift widths, index array lengths, etc.
michael@0 711 *
michael@0 712 * These are needed for the runtime macros but users can treat these as
michael@0 713 * implementation details and skip to the actual public API further below.
michael@0 714 */
michael@0 715 enum {
michael@0 716 /** Shift size for getting the index-1 table offset. */
michael@0 717 UTRIE2_SHIFT_1=6+5,
michael@0 718
michael@0 719 /** Shift size for getting the index-2 table offset. */
michael@0 720 UTRIE2_SHIFT_2=5,
michael@0 721
michael@0 722 /**
michael@0 723 * Difference between the two shift sizes,
michael@0 724 * for getting an index-1 offset from an index-2 offset. 6=11-5
michael@0 725 */
michael@0 726 UTRIE2_SHIFT_1_2=UTRIE2_SHIFT_1-UTRIE2_SHIFT_2,
michael@0 727
michael@0 728 /**
michael@0 729 * Number of index-1 entries for the BMP. 32=0x20
michael@0 730 * This part of the index-1 table is omitted from the serialized form.
michael@0 731 */
michael@0 732 UTRIE2_OMITTED_BMP_INDEX_1_LENGTH=0x10000>>UTRIE2_SHIFT_1,
michael@0 733
michael@0 734 /** Number of code points per index-1 table entry. 2048=0x800 */
michael@0 735 UTRIE2_CP_PER_INDEX_1_ENTRY=1<<UTRIE2_SHIFT_1,
michael@0 736
michael@0 737 /** Number of entries in an index-2 block. 64=0x40 */
michael@0 738 UTRIE2_INDEX_2_BLOCK_LENGTH=1<<UTRIE2_SHIFT_1_2,
michael@0 739
michael@0 740 /** Mask for getting the lower bits for the in-index-2-block offset. */
michael@0 741 UTRIE2_INDEX_2_MASK=UTRIE2_INDEX_2_BLOCK_LENGTH-1,
michael@0 742
michael@0 743 /** Number of entries in a data block. 32=0x20 */
michael@0 744 UTRIE2_DATA_BLOCK_LENGTH=1<<UTRIE2_SHIFT_2,
michael@0 745
michael@0 746 /** Mask for getting the lower bits for the in-data-block offset. */
michael@0 747 UTRIE2_DATA_MASK=UTRIE2_DATA_BLOCK_LENGTH-1,
michael@0 748
michael@0 749 /**
michael@0 750 * Shift size for shifting left the index array values.
michael@0 751 * Increases possible data size with 16-bit index values at the cost
michael@0 752 * of compactability.
michael@0 753 * This requires data blocks to be aligned by UTRIE2_DATA_GRANULARITY.
michael@0 754 */
michael@0 755 UTRIE2_INDEX_SHIFT=2,
michael@0 756
michael@0 757 /** The alignment size of a data block. Also the granularity for compaction. */
michael@0 758 UTRIE2_DATA_GRANULARITY=1<<UTRIE2_INDEX_SHIFT,
michael@0 759
michael@0 760 /* Fixed layout of the first part of the index array. ------------------- */
michael@0 761
michael@0 762 /**
michael@0 763 * The BMP part of the index-2 table is fixed and linear and starts at offset 0.
michael@0 764 * Length=2048=0x800=0x10000>>UTRIE2_SHIFT_2.
michael@0 765 */
michael@0 766 UTRIE2_INDEX_2_OFFSET=0,
michael@0 767
michael@0 768 /**
michael@0 769 * The part of the index-2 table for U+D800..U+DBFF stores values for
michael@0 770 * lead surrogate code _units_ not code _points_.
michael@0 771 * Values for lead surrogate code _points_ are indexed with this portion of the table.
michael@0 772 * Length=32=0x20=0x400>>UTRIE2_SHIFT_2. (There are 1024=0x400 lead surrogates.)
michael@0 773 */
michael@0 774 UTRIE2_LSCP_INDEX_2_OFFSET=0x10000>>UTRIE2_SHIFT_2,
michael@0 775 UTRIE2_LSCP_INDEX_2_LENGTH=0x400>>UTRIE2_SHIFT_2,
michael@0 776
michael@0 777 /** Count the lengths of both BMP pieces. 2080=0x820 */
michael@0 778 UTRIE2_INDEX_2_BMP_LENGTH=UTRIE2_LSCP_INDEX_2_OFFSET+UTRIE2_LSCP_INDEX_2_LENGTH,
michael@0 779
michael@0 780 /**
michael@0 781 * The 2-byte UTF-8 version of the index-2 table follows at offset 2080=0x820.
michael@0 782 * Length 32=0x20 for lead bytes C0..DF, regardless of UTRIE2_SHIFT_2.
michael@0 783 */
michael@0 784 UTRIE2_UTF8_2B_INDEX_2_OFFSET=UTRIE2_INDEX_2_BMP_LENGTH,
michael@0 785 UTRIE2_UTF8_2B_INDEX_2_LENGTH=0x800>>6, /* U+0800 is the first code point after 2-byte UTF-8 */
michael@0 786
michael@0 787 /**
michael@0 788 * The index-1 table, only used for supplementary code points, at offset 2112=0x840.
michael@0 789 * Variable length, for code points up to highStart, where the last single-value range starts.
michael@0 790 * Maximum length 512=0x200=0x100000>>UTRIE2_SHIFT_1.
michael@0 791 * (For 0x100000 supplementary code points U+10000..U+10ffff.)
michael@0 792 *
michael@0 793 * The part of the index-2 table for supplementary code points starts
michael@0 794 * after this index-1 table.
michael@0 795 *
michael@0 796 * Both the index-1 table and the following part of the index-2 table
michael@0 797 * are omitted completely if there is only BMP data.
michael@0 798 */
michael@0 799 UTRIE2_INDEX_1_OFFSET=UTRIE2_UTF8_2B_INDEX_2_OFFSET+UTRIE2_UTF8_2B_INDEX_2_LENGTH,
michael@0 800 UTRIE2_MAX_INDEX_1_LENGTH=0x100000>>UTRIE2_SHIFT_1,
michael@0 801
michael@0 802 /*
michael@0 803 * Fixed layout of the first part of the data array. -----------------------
michael@0 804 * Starts with 4 blocks (128=0x80 entries) for ASCII.
michael@0 805 */
michael@0 806
michael@0 807 /**
michael@0 808 * The illegal-UTF-8 data block follows the ASCII block, at offset 128=0x80.
michael@0 809 * Used with linear access for single bytes 0..0xbf for simple error handling.
michael@0 810 * Length 64=0x40, not UTRIE2_DATA_BLOCK_LENGTH.
michael@0 811 */
michael@0 812 UTRIE2_BAD_UTF8_DATA_OFFSET=0x80,
michael@0 813
michael@0 814 /** The start of non-linear-ASCII data blocks, at offset 192=0xc0. */
michael@0 815 UTRIE2_DATA_START_OFFSET=0xc0
michael@0 816 };
michael@0 817
michael@0 818 /* Internal functions and macros -------------------------------------------- */
michael@0 819
michael@0 820 /**
michael@0 821 * Internal function for part of the UTRIE2_U8_NEXTxx() macro implementations.
michael@0 822 * Do not call directly.
michael@0 823 * @internal
michael@0 824 */
michael@0 825 U_INTERNAL int32_t U_EXPORT2
michael@0 826 utrie2_internalU8NextIndex(const UTrie2 *trie, UChar32 c,
michael@0 827 const uint8_t *src, const uint8_t *limit);
michael@0 828
michael@0 829 /**
michael@0 830 * Internal function for part of the UTRIE2_U8_PREVxx() macro implementations.
michael@0 831 * Do not call directly.
michael@0 832 * @internal
michael@0 833 */
michael@0 834 U_INTERNAL int32_t U_EXPORT2
michael@0 835 utrie2_internalU8PrevIndex(const UTrie2 *trie, UChar32 c,
michael@0 836 const uint8_t *start, const uint8_t *src);
michael@0 837
michael@0 838
michael@0 839 /** Internal low-level trie getter. Returns a data index. */
michael@0 840 #define _UTRIE2_INDEX_RAW(offset, trieIndex, c) \
michael@0 841 (((int32_t)((trieIndex)[(offset)+((c)>>UTRIE2_SHIFT_2)]) \
michael@0 842 <<UTRIE2_INDEX_SHIFT)+ \
michael@0 843 ((c)&UTRIE2_DATA_MASK))
michael@0 844
michael@0 845 /** Internal trie getter from a UTF-16 single/lead code unit. Returns the data index. */
michael@0 846 #define _UTRIE2_INDEX_FROM_U16_SINGLE_LEAD(trieIndex, c) _UTRIE2_INDEX_RAW(0, trieIndex, c)
michael@0 847
michael@0 848 /** Internal trie getter from a lead surrogate code point (D800..DBFF). Returns the data index. */
michael@0 849 #define _UTRIE2_INDEX_FROM_LSCP(trieIndex, c) \
michael@0 850 _UTRIE2_INDEX_RAW(UTRIE2_LSCP_INDEX_2_OFFSET-(0xd800>>UTRIE2_SHIFT_2), trieIndex, c)
michael@0 851
michael@0 852 /** Internal trie getter from a BMP code point. Returns the data index. */
michael@0 853 #define _UTRIE2_INDEX_FROM_BMP(trieIndex, c) \
michael@0 854 _UTRIE2_INDEX_RAW(U_IS_LEAD(c) ? UTRIE2_LSCP_INDEX_2_OFFSET-(0xd800>>UTRIE2_SHIFT_2) : 0, \
michael@0 855 trieIndex, c)
michael@0 856
michael@0 857 /** Internal trie getter from a supplementary code point below highStart. Returns the data index. */
michael@0 858 #define _UTRIE2_INDEX_FROM_SUPP(trieIndex, c) \
michael@0 859 (((int32_t)((trieIndex)[ \
michael@0 860 (trieIndex)[(UTRIE2_INDEX_1_OFFSET-UTRIE2_OMITTED_BMP_INDEX_1_LENGTH)+ \
michael@0 861 ((c)>>UTRIE2_SHIFT_1)]+ \
michael@0 862 (((c)>>UTRIE2_SHIFT_2)&UTRIE2_INDEX_2_MASK)]) \
michael@0 863 <<UTRIE2_INDEX_SHIFT)+ \
michael@0 864 ((c)&UTRIE2_DATA_MASK))
michael@0 865
michael@0 866 /**
michael@0 867 * Internal trie getter from a code point, with checking that c is in 0..10FFFF.
michael@0 868 * Returns the data index.
michael@0 869 */
michael@0 870 #define _UTRIE2_INDEX_FROM_CP(trie, asciiOffset, c) \
michael@0 871 ((uint32_t)(c)<0xd800 ? \
michael@0 872 _UTRIE2_INDEX_RAW(0, (trie)->index, c) : \
michael@0 873 (uint32_t)(c)<=0xffff ? \
michael@0 874 _UTRIE2_INDEX_RAW( \
michael@0 875 (c)<=0xdbff ? UTRIE2_LSCP_INDEX_2_OFFSET-(0xd800>>UTRIE2_SHIFT_2) : 0, \
michael@0 876 (trie)->index, c) : \
michael@0 877 (uint32_t)(c)>0x10ffff ? \
michael@0 878 (asciiOffset)+UTRIE2_BAD_UTF8_DATA_OFFSET : \
michael@0 879 (c)>=(trie)->highStart ? \
michael@0 880 (trie)->highValueIndex : \
michael@0 881 _UTRIE2_INDEX_FROM_SUPP((trie)->index, c))
michael@0 882
michael@0 883 /** Internal trie getter from a UTF-16 single/lead code unit. Returns the data. */
michael@0 884 #define _UTRIE2_GET_FROM_U16_SINGLE_LEAD(trie, data, c) \
michael@0 885 (trie)->data[_UTRIE2_INDEX_FROM_U16_SINGLE_LEAD((trie)->index, c)]
michael@0 886
michael@0 887 /** Internal trie getter from a supplementary code point. Returns the data. */
michael@0 888 #define _UTRIE2_GET_FROM_SUPP(trie, data, c) \
michael@0 889 (trie)->data[(c)>=(trie)->highStart ? (trie)->highValueIndex : \
michael@0 890 _UTRIE2_INDEX_FROM_SUPP((trie)->index, c)]
michael@0 891
michael@0 892 /**
michael@0 893 * Internal trie getter from a code point, with checking that c is in 0..10FFFF.
michael@0 894 * Returns the data.
michael@0 895 */
michael@0 896 #define _UTRIE2_GET(trie, data, asciiOffset, c) \
michael@0 897 (trie)->data[_UTRIE2_INDEX_FROM_CP(trie, asciiOffset, c)]
michael@0 898
michael@0 899 /** Internal next-post-increment: get the next code point (c) and its data. */
michael@0 900 #define _UTRIE2_U16_NEXT(trie, data, src, limit, c, result) { \
michael@0 901 { \
michael@0 902 uint16_t __c2; \
michael@0 903 (c)=*(src)++; \
michael@0 904 if(!U16_IS_LEAD(c)) { \
michael@0 905 (result)=_UTRIE2_GET_FROM_U16_SINGLE_LEAD(trie, data, c); \
michael@0 906 } else if((src)==(limit) || !U16_IS_TRAIL(__c2=*(src))) { \
michael@0 907 (result)=(trie)->data[_UTRIE2_INDEX_FROM_LSCP((trie)->index, c)]; \
michael@0 908 } else { \
michael@0 909 ++(src); \
michael@0 910 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
michael@0 911 (result)=_UTRIE2_GET_FROM_SUPP((trie), data, (c)); \
michael@0 912 } \
michael@0 913 } \
michael@0 914 }
michael@0 915
michael@0 916 /** Internal pre-decrement-previous: get the previous code point (c) and its data */
michael@0 917 #define _UTRIE2_U16_PREV(trie, data, start, src, c, result) { \
michael@0 918 { \
michael@0 919 uint16_t __c2; \
michael@0 920 (c)=*--(src); \
michael@0 921 if(!U16_IS_TRAIL(c) || (src)==(start) || !U16_IS_LEAD(__c2=*((src)-1))) { \
michael@0 922 (result)=(trie)->data[_UTRIE2_INDEX_FROM_BMP((trie)->index, c)]; \
michael@0 923 } else { \
michael@0 924 --(src); \
michael@0 925 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
michael@0 926 (result)=_UTRIE2_GET_FROM_SUPP((trie), data, (c)); \
michael@0 927 } \
michael@0 928 } \
michael@0 929 }
michael@0 930
michael@0 931 /** Internal UTF-8 next-post-increment: get the next code point's data. */
michael@0 932 #define _UTRIE2_U8_NEXT(trie, ascii, data, src, limit, result) { \
michael@0 933 uint8_t __lead=(uint8_t)*(src)++; \
michael@0 934 if(__lead<0xc0) { \
michael@0 935 (result)=(trie)->ascii[__lead]; \
michael@0 936 } else { \
michael@0 937 uint8_t __t1, __t2; \
michael@0 938 if( /* handle U+0000..U+07FF inline */ \
michael@0 939 __lead<0xe0 && (src)<(limit) && \
michael@0 940 (__t1=(uint8_t)(*(src)-0x80))<=0x3f \
michael@0 941 ) { \
michael@0 942 ++(src); \
michael@0 943 (result)=(trie)->data[ \
michael@0 944 (trie)->index[(UTRIE2_UTF8_2B_INDEX_2_OFFSET-0xc0)+__lead]+ \
michael@0 945 __t1]; \
michael@0 946 } else if( /* handle U+0000..U+CFFF inline */ \
michael@0 947 __lead<0xed && ((src)+1)<(limit) && \
michael@0 948 (__t1=(uint8_t)(*(src)-0x80))<=0x3f && (__lead>0xe0 || __t1>=0x20) && \
michael@0 949 (__t2=(uint8_t)(*((src)+1)-0x80))<= 0x3f \
michael@0 950 ) { \
michael@0 951 (src)+=2; \
michael@0 952 (result)=(trie)->data[ \
michael@0 953 ((int32_t)((trie)->index[((__lead-0xe0)<<(12-UTRIE2_SHIFT_2))+ \
michael@0 954 (__t1<<(6-UTRIE2_SHIFT_2))+(__t2>>UTRIE2_SHIFT_2)]) \
michael@0 955 <<UTRIE2_INDEX_SHIFT)+ \
michael@0 956 (__t2&UTRIE2_DATA_MASK)]; \
michael@0 957 } else { \
michael@0 958 int32_t __index=utrie2_internalU8NextIndex((trie), __lead, (const uint8_t *)(src), \
michael@0 959 (const uint8_t *)(limit)); \
michael@0 960 (src)+=__index&7; \
michael@0 961 (result)=(trie)->data[__index>>3]; \
michael@0 962 } \
michael@0 963 } \
michael@0 964 }
michael@0 965
michael@0 966 /** Internal UTF-8 pre-decrement-previous: get the previous code point's data. */
michael@0 967 #define _UTRIE2_U8_PREV(trie, ascii, data, start, src, result) { \
michael@0 968 uint8_t __b=(uint8_t)*--(src); \
michael@0 969 if(__b<0x80) { \
michael@0 970 (result)=(trie)->ascii[__b]; \
michael@0 971 } else { \
michael@0 972 int32_t __index=utrie2_internalU8PrevIndex((trie), __b, (const uint8_t *)(start), \
michael@0 973 (const uint8_t *)(src)); \
michael@0 974 (src)-=__index&7; \
michael@0 975 (result)=(trie)->data[__index>>3]; \
michael@0 976 } \
michael@0 977 }
michael@0 978
michael@0 979 U_CDECL_END
michael@0 980
michael@0 981 /**
michael@0 982 * Work around MSVC 2003 optimization bugs.
michael@0 983 */
michael@0 984 #if defined (U_HAVE_MSVC_2003_OR_EARLIER)
michael@0 985 #pragma optimize("", off)
michael@0 986 #endif
michael@0 987
michael@0 988 #endif

mercurial