ucptrie_impl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // © 2017 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. // ucptrie_impl.h (modified from utrie2_impl.h)
  4. // created: 2017dec29 Markus W. Scherer
  5. #ifndef __UCPTRIE_IMPL_H__
  6. #define __UCPTRIE_IMPL_H__
  7. #include "unicode/ucptrie.h"
  8. #ifdef UCPTRIE_DEBUG
  9. #include "unicode/umutablecptrie.h"
  10. #endif
  11. // UCPTrie signature values, in platform endianness and opposite endianness.
  12. // The UCPTrie signature ASCII byte values spell "Tri3".
  13. #define UCPTRIE_SIG 0x54726933
  14. #define UCPTRIE_OE_SIG 0x33697254
  15. /**
  16. * Header data for the binary, memory-mappable representation of a UCPTrie/CodePointTrie.
  17. * @internal
  18. */
  19. struct UCPTrieHeader {
  20. /** "Tri3" in big-endian US-ASCII (0x54726933) */
  21. uint32_t signature;
  22. /**
  23. * Options bit field:
  24. * Bits 15..12: Data length bits 19..16.
  25. * Bits 11..8: Data null block offset bits 19..16.
  26. * Bits 7..6: UCPTrieType
  27. * Bits 5..3: Reserved (0).
  28. * Bits 2..0: UCPTrieValueWidth
  29. */
  30. uint16_t options;
  31. /** Total length of the index tables. */
  32. uint16_t indexLength;
  33. /** Data length bits 15..0. */
  34. uint16_t dataLength;
  35. /** Index-3 null block offset, 0x7fff or 0xffff if none. */
  36. uint16_t index3NullOffset;
  37. /** Data null block offset bits 15..0, 0xfffff if none. */
  38. uint16_t dataNullOffset;
  39. /**
  40. * First code point of the single-value range ending with U+10ffff,
  41. * rounded up and then shifted right by UCPTRIE_SHIFT_2.
  42. */
  43. uint16_t shiftedHighStart;
  44. };
  45. /**
  46. * Constants for use with UCPTrieHeader.options.
  47. * @internal
  48. */
  49. enum {
  50. UCPTRIE_OPTIONS_DATA_LENGTH_MASK = 0xf000,
  51. UCPTRIE_OPTIONS_DATA_NULL_OFFSET_MASK = 0xf00,
  52. UCPTRIE_OPTIONS_RESERVED_MASK = 0x38,
  53. UCPTRIE_OPTIONS_VALUE_BITS_MASK = 7,
  54. /**
  55. * Value for index3NullOffset which indicates that there is no index-3 null block.
  56. * Bit 15 is unused for this value because this bit is used if the index-3 contains
  57. * 18-bit indexes.
  58. */
  59. UCPTRIE_NO_INDEX3_NULL_OFFSET = 0x7fff,
  60. UCPTRIE_NO_DATA_NULL_OFFSET = 0xfffff
  61. };
  62. // Internal constants.
  63. enum {
  64. /** The length of the BMP index table. 1024=0x400 */
  65. UCPTRIE_BMP_INDEX_LENGTH = 0x10000 >> UCPTRIE_FAST_SHIFT,
  66. UCPTRIE_SMALL_LIMIT = 0x1000,
  67. UCPTRIE_SMALL_INDEX_LENGTH = UCPTRIE_SMALL_LIMIT >> UCPTRIE_FAST_SHIFT,
  68. /** Shift size for getting the index-3 table offset. */
  69. UCPTRIE_SHIFT_3 = 4,
  70. /** Shift size for getting the index-2 table offset. */
  71. UCPTRIE_SHIFT_2 = 5 + UCPTRIE_SHIFT_3,
  72. /** Shift size for getting the index-1 table offset. */
  73. UCPTRIE_SHIFT_1 = 5 + UCPTRIE_SHIFT_2,
  74. /**
  75. * Difference between two shift sizes,
  76. * for getting an index-2 offset from an index-3 offset. 5=9-4
  77. */
  78. UCPTRIE_SHIFT_2_3 = UCPTRIE_SHIFT_2 - UCPTRIE_SHIFT_3,
  79. /**
  80. * Difference between two shift sizes,
  81. * for getting an index-1 offset from an index-2 offset. 5=14-9
  82. */
  83. UCPTRIE_SHIFT_1_2 = UCPTRIE_SHIFT_1 - UCPTRIE_SHIFT_2,
  84. /**
  85. * Number of index-1 entries for the BMP. (4)
  86. * This part of the index-1 table is omitted from the serialized form.
  87. */
  88. UCPTRIE_OMITTED_BMP_INDEX_1_LENGTH = 0x10000 >> UCPTRIE_SHIFT_1,
  89. /** Number of entries in an index-2 block. 32=0x20 */
  90. UCPTRIE_INDEX_2_BLOCK_LENGTH = 1 << UCPTRIE_SHIFT_1_2,
  91. /** Mask for getting the lower bits for the in-index-2-block offset. */
  92. UCPTRIE_INDEX_2_MASK = UCPTRIE_INDEX_2_BLOCK_LENGTH - 1,
  93. /** Number of code points per index-2 table entry. 512=0x200 */
  94. UCPTRIE_CP_PER_INDEX_2_ENTRY = 1 << UCPTRIE_SHIFT_2,
  95. /** Number of entries in an index-3 block. 32=0x20 */
  96. UCPTRIE_INDEX_3_BLOCK_LENGTH = 1 << UCPTRIE_SHIFT_2_3,
  97. /** Mask for getting the lower bits for the in-index-3-block offset. */
  98. UCPTRIE_INDEX_3_MASK = UCPTRIE_INDEX_3_BLOCK_LENGTH - 1,
  99. /** Number of entries in a small data block. 16=0x10 */
  100. UCPTRIE_SMALL_DATA_BLOCK_LENGTH = 1 << UCPTRIE_SHIFT_3,
  101. /** Mask for getting the lower bits for the in-small-data-block offset. */
  102. UCPTRIE_SMALL_DATA_MASK = UCPTRIE_SMALL_DATA_BLOCK_LENGTH - 1
  103. };
  104. typedef UChar32
  105. UCPTrieGetRange(const void *trie, UChar32 start,
  106. UCPMapValueFilter *filter, const void *context, uint32_t *pValue);
  107. U_CFUNC UChar32
  108. ucptrie_internalGetRange(UCPTrieGetRange *getRange,
  109. const void *trie, UChar32 start,
  110. UCPMapRangeOption option, uint32_t surrogateValue,
  111. UCPMapValueFilter *filter, const void *context, uint32_t *pValue);
  112. #ifdef UCPTRIE_DEBUG
  113. U_CFUNC void
  114. ucptrie_printLengths(const UCPTrie *trie, const char *which);
  115. U_CFUNC void umutablecptrie_setName(UMutableCPTrie *builder, const char *name);
  116. #endif
  117. /*
  118. * Format of the binary, memory-mappable representation of a UCPTrie/CodePointTrie.
  119. * For overview information see http://site.icu-project.org/design/struct/utrie
  120. *
  121. * The binary trie data should be 32-bit-aligned.
  122. * The overall layout is:
  123. *
  124. * UCPTrieHeader header; -- 16 bytes, see struct definition above
  125. * uint16_t index[header.indexLength];
  126. * uintXY_t data[header.dataLength];
  127. *
  128. * The trie data array is an array of uint16_t, uint32_t, or uint8_t,
  129. * specified via the UCPTrieValueWidth when building the trie.
  130. * The data array is 32-bit-aligned for uint32_t, otherwise 16-bit-aligned.
  131. * The overall length of the trie data is a multiple of 4 bytes.
  132. * (Padding is added at the end of the index array and/or near the end of the data array as needed.)
  133. *
  134. * The length of the data array (dataLength) is stored as an integer split across two fields
  135. * of the header struct (high bits in header.options).
  136. *
  137. * The trie type can be "fast" or "small" which determines the index structure,
  138. * specified via the UCPTrieType when building the trie.
  139. *
  140. * The type and valueWidth are stored in the header.options.
  141. * There are reserved type and valueWidth values, and reserved header.options bits.
  142. * They could be used in future format extensions.
  143. * Code reading the trie structure must fail with an error when unknown values or options are set.
  144. *
  145. * Values for ASCII character (U+0000..U+007F) can always be found at the start of the data array.
  146. *
  147. * Values for code points below a type-specific fast-indexing limit are found via two-stage lookup.
  148. * For a "fast" trie, the limit is the BMP/supplementary boundary at U+10000.
  149. * For a "small" trie, the limit is UCPTRIE_SMALL_MAX+1=U+1000.
  150. *
  151. * All code points in the range highStart..U+10FFFF map to a single highValue
  152. * which is stored at the second-to-last position of the data array.
  153. * (See UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET.)
  154. * The highStart value is header.shiftedHighStart<<UCPTRIE_SHIFT_2.
  155. * (UCPTRIE_SHIFT_2=9)
  156. *
  157. * Values for code points fast_limit..highStart-1 are found via four-stage lookup.
  158. * The data block size is smaller for this range than for the fast range.
  159. * This together with more index stages with small blocks makes this range
  160. * more easily compactable.
  161. *
  162. * There is also a trie error value stored at the last position of the data array.
  163. * (See UCPTRIE_ERROR_VALUE_NEG_DATA_OFFSET.)
  164. * It is intended to be returned for inputs that are not Unicode code points
  165. * (outside U+0000..U+10FFFF), or in string processing for ill-formed input
  166. * (unpaired surrogate in UTF-16, ill-formed UTF-8 subsequence).
  167. *
  168. * For a "fast" trie:
  169. *
  170. * The index array starts with the BMP index table for BMP code point lookup.
  171. * Its length is 1024=0x400.
  172. *
  173. * The supplementary index-1 table follows the BMP index table.
  174. * Variable length, for code points up to highStart-1.
  175. * Maximum length 64=0x40=0x100000>>UCPTRIE_SHIFT_1.
  176. * (For 0x100000 supplementary code points U+10000..U+10ffff.)
  177. *
  178. * After this index-1 table follow the variable-length index-3 and index-2 tables.
  179. *
  180. * The supplementary index tables are omitted completely
  181. * if there is only BMP data (highStart<=U+10000).
  182. *
  183. * For a "small" trie:
  184. *
  185. * The index array starts with a fast-index table for lookup of code points U+0000..U+0FFF.
  186. *
  187. * The "supplementary" index tables are always stored.
  188. * The index-1 table starts from U+0000, its maximum length is 68=0x44=0x110000>>UCPTRIE_SHIFT_1.
  189. *
  190. * For both trie types:
  191. *
  192. * The last index-2 block may be a partial block, storing indexes only for code points
  193. * below highStart.
  194. *
  195. * Lookup for ASCII code point c:
  196. *
  197. * Linear access from the start of the data array.
  198. *
  199. * value = data[c];
  200. *
  201. * Lookup for fast-range code point c:
  202. *
  203. * Shift the code point right by UCPTRIE_FAST_SHIFT=6 bits,
  204. * fetch the index array value at that offset,
  205. * add the lower code point bits, index into the data array.
  206. *
  207. * value = data[index[c>>6] + (c&0x3f)];
  208. *
  209. * (This works for ASCII as well.)
  210. *
  211. * Lookup for small-range code point c below highStart:
  212. *
  213. * Split the code point into four bit fields using several sets of shifts & masks
  214. * to read consecutive values from the index-1, index-2, index-3 and data tables.
  215. *
  216. * If all of the data block offsets in an index-3 block fit within 16 bits (up to 0xffff),
  217. * then the data block offsets are stored directly as uint16_t.
  218. *
  219. * Otherwise (this is very unusual but possible), the index-2 entry for the index-3 block
  220. * has bit 15 (0x8000) set, and each set of 8 index-3 entries is preceded by
  221. * an additional uint16_t word. Data block offsets are 18 bits wide, with the top 2 bits stored
  222. * in the additional word.
  223. *
  224. * See ucptrie_internalSmallIndex() for details.
  225. *
  226. * (In a "small" trie, this works for ASCII and below-fast_limit code points as well.)
  227. *
  228. * Compaction:
  229. *
  230. * Multiple code point ranges ("blocks") that are aligned on certain boundaries
  231. * (determined by the shifting/bit fields of code points) and
  232. * map to the same data values normally share a single subsequence of the data array.
  233. * Data blocks can also overlap partially.
  234. * (Depending on the builder code finding duplicate and overlapping blocks.)
  235. *
  236. * Iteration over same-value ranges:
  237. *
  238. * Range iteration (ucptrie_getRange()) walks the structure from a start code point
  239. * until some code point is found that maps to a different value;
  240. * the end of the returned range is just before that.
  241. *
  242. * The header.dataNullOffset (split across two header fields, high bits in header.options)
  243. * is the offset of a widely shared data block filled with one single value.
  244. * It helps quickly skip over large ranges of data with that value.
  245. * The builder must ensure that if the start of any data block (fast or small)
  246. * matches the dataNullOffset, then the whole block must be filled with the null value.
  247. * Special care must be taken if there is no fast null data block
  248. * but a small one, which is shorter, and it matches the *start* of some fast data block.
  249. *
  250. * Similarly, the header.index3NullOffset is the index-array offset of an index-3 block
  251. * where all index entries point to the dataNullOffset.
  252. * If there is no such data or index-3 block, then these offsets are set to
  253. * values that cannot be reached (data offset out of range/reserved index offset),
  254. * normally UCPTRIE_NO_DATA_NULL_OFFSET or UCPTRIE_NO_INDEX3_NULL_OFFSET respectively.
  255. */
  256. #endif