ucptrie.h 23 KB

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