ucptrie.h 22 KB

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