utrie2.h 37 KB

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