collation.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2010-2015, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. *******************************************************************************
  8. * collation.h
  9. *
  10. * created on: 2010oct27
  11. * created by: Markus W. Scherer
  12. */
  13. #ifndef __COLLATION_H__
  14. #define __COLLATION_H__
  15. #include "unicode/utypes.h"
  16. #if !UCONFIG_NO_COLLATION
  17. U_NAMESPACE_BEGIN
  18. /**
  19. * Collation v2 basic definitions and static helper functions.
  20. *
  21. * Data structures except for expansion tables store 32-bit CEs which are
  22. * either specials (see tags below) or are compact forms of 64-bit CEs.
  23. */
  24. class U_I18N_API Collation {
  25. public:
  26. // Special sort key bytes for all levels.
  27. static const uint8_t TERMINATOR_BYTE = 0;
  28. static const uint8_t LEVEL_SEPARATOR_BYTE = 1;
  29. /** The secondary/tertiary lower limit for tailoring before any root elements. */
  30. static const uint32_t BEFORE_WEIGHT16 = 0x0100;
  31. /**
  32. * Merge-sort-key separator.
  33. * Same as the unique primary and identical-level weights of U+FFFE.
  34. * Must not be used as primary compression low terminator.
  35. * Otherwise usable.
  36. */
  37. static const uint8_t MERGE_SEPARATOR_BYTE = 2;
  38. static const uint32_t MERGE_SEPARATOR_PRIMARY = 0x02000000; // U+FFFE
  39. static const uint32_t MERGE_SEPARATOR_CE32 = 0x02000505; // U+FFFE
  40. /**
  41. * Primary compression low terminator, must be greater than MERGE_SEPARATOR_BYTE.
  42. * Reserved value in primary second byte if the lead byte is compressible.
  43. * Otherwise usable in all CE weight bytes.
  44. */
  45. static const uint8_t PRIMARY_COMPRESSION_LOW_BYTE = 3;
  46. /**
  47. * Primary compression high terminator.
  48. * Reserved value in primary second byte if the lead byte is compressible.
  49. * Otherwise usable in all CE weight bytes.
  50. */
  51. static const uint8_t PRIMARY_COMPRESSION_HIGH_BYTE = 0xff;
  52. /** Default secondary/tertiary weight lead byte. */
  53. static const uint8_t COMMON_BYTE = 5;
  54. static const uint32_t COMMON_WEIGHT16 = 0x0500;
  55. /** Middle 16 bits of a CE with a common secondary weight. */
  56. static const uint32_t COMMON_SECONDARY_CE = 0x05000000;
  57. /** Lower 16 bits of a CE with a common tertiary weight. */
  58. static const uint32_t COMMON_TERTIARY_CE = 0x0500;
  59. /** Lower 32 bits of a CE with common secondary and tertiary weights. */
  60. static const uint32_t COMMON_SEC_AND_TER_CE = 0x05000500;
  61. static const uint32_t SECONDARY_MASK = 0xffff0000;
  62. static const uint32_t CASE_MASK = 0xc000;
  63. static const uint32_t SECONDARY_AND_CASE_MASK = SECONDARY_MASK | CASE_MASK;
  64. /** Only the 2*6 bits for the pure tertiary weight. */
  65. static const uint32_t ONLY_TERTIARY_MASK = 0x3f3f;
  66. /** Only the secondary & tertiary bits; no case, no quaternary. */
  67. static const uint32_t ONLY_SEC_TER_MASK = SECONDARY_MASK | ONLY_TERTIARY_MASK;
  68. /** Case bits and tertiary bits. */
  69. static const uint32_t CASE_AND_TERTIARY_MASK = CASE_MASK | ONLY_TERTIARY_MASK;
  70. static const uint32_t QUATERNARY_MASK = 0xc0;
  71. /** Case bits and quaternary bits. */
  72. static const uint32_t CASE_AND_QUATERNARY_MASK = CASE_MASK | QUATERNARY_MASK;
  73. static const uint8_t UNASSIGNED_IMPLICIT_BYTE = 0xfe; // compressible
  74. /**
  75. * First unassigned: AlphabeticIndex overflow boundary.
  76. * We want a 3-byte primary so that it fits into the root elements table.
  77. *
  78. * This 3-byte primary will not collide with
  79. * any unassigned-implicit 4-byte primaries because
  80. * the first few hundred Unicode code points all have real mappings.
  81. */
  82. static const uint32_t FIRST_UNASSIGNED_PRIMARY = 0xfe040200;
  83. static const uint8_t TRAIL_WEIGHT_BYTE = 0xff; // not compressible
  84. static const uint32_t FIRST_TRAILING_PRIMARY = 0xff020200; // [first trailing]
  85. static const uint32_t MAX_PRIMARY = 0xffff0000; // U+FFFF
  86. static const uint32_t MAX_REGULAR_CE32 = 0xffff0505; // U+FFFF
  87. // CE32 value for U+FFFD as well as illegal UTF-8 byte sequences (which behave like U+FFFD).
  88. // We use the third-highest primary weight for U+FFFD (as in UCA 6.3+).
  89. static const uint32_t FFFD_PRIMARY = MAX_PRIMARY - 0x20000;
  90. static const uint32_t FFFD_CE32 = MAX_REGULAR_CE32 - 0x20000;
  91. /**
  92. * A CE32 is special if its low byte is this or greater.
  93. * Impossible case bits 11 mark special CE32s.
  94. * This value itself is used to indicate a fallback to the base collator.
  95. */
  96. static const uint8_t SPECIAL_CE32_LOW_BYTE = 0xc0;
  97. static const uint32_t FALLBACK_CE32 = SPECIAL_CE32_LOW_BYTE;
  98. /**
  99. * Low byte of a long-primary special CE32.
  100. */
  101. static const uint8_t LONG_PRIMARY_CE32_LOW_BYTE = 0xc1; // SPECIAL_CE32_LOW_BYTE | LONG_PRIMARY_TAG
  102. static const uint32_t UNASSIGNED_CE32 = 0xffffffff; // Compute an unassigned-implicit CE.
  103. static const uint32_t NO_CE32 = 1;
  104. /** No CE: End of input. Only used in runtime code, not stored in data. */
  105. static const uint32_t NO_CE_PRIMARY = 1; // not a left-adjusted weight
  106. static const uint32_t NO_CE_WEIGHT16 = 0x0100; // weight of LEVEL_SEPARATOR_BYTE
  107. static const int64_t NO_CE = INT64_C(0x101000100); // NO_CE_PRIMARY, NO_CE_WEIGHT16, NO_CE_WEIGHT16
  108. /** Sort key levels. */
  109. enum Level {
  110. /** Unspecified level. */
  111. NO_LEVEL,
  112. PRIMARY_LEVEL,
  113. SECONDARY_LEVEL,
  114. CASE_LEVEL,
  115. TERTIARY_LEVEL,
  116. QUATERNARY_LEVEL,
  117. IDENTICAL_LEVEL,
  118. /** Beyond sort key bytes. */
  119. ZERO_LEVEL
  120. };
  121. /**
  122. * Sort key level flags: xx_FLAG = 1 << xx_LEVEL.
  123. * In Java, use enum Level with flag() getters, or use EnumSet rather than hand-made bit sets.
  124. */
  125. static const uint32_t NO_LEVEL_FLAG = 1;
  126. static const uint32_t PRIMARY_LEVEL_FLAG = 2;
  127. static const uint32_t SECONDARY_LEVEL_FLAG = 4;
  128. static const uint32_t CASE_LEVEL_FLAG = 8;
  129. static const uint32_t TERTIARY_LEVEL_FLAG = 0x10;
  130. static const uint32_t QUATERNARY_LEVEL_FLAG = 0x20;
  131. static const uint32_t IDENTICAL_LEVEL_FLAG = 0x40;
  132. static const uint32_t ZERO_LEVEL_FLAG = 0x80;
  133. /**
  134. * Special-CE32 tags, from bits 3..0 of a special 32-bit CE.
  135. * Bits 31..8 are available for tag-specific data.
  136. * Bits 5..4: Reserved. May be used in the future to indicate lccc!=0 and tccc!=0.
  137. */
  138. enum {
  139. /**
  140. * Fall back to the base collator.
  141. * This is the tag value in SPECIAL_CE32_LOW_BYTE and FALLBACK_CE32.
  142. * Bits 31..8: Unused, 0.
  143. */
  144. FALLBACK_TAG = 0,
  145. /**
  146. * Long-primary CE with COMMON_SEC_AND_TER_CE.
  147. * Bits 31..8: Three-byte primary.
  148. */
  149. LONG_PRIMARY_TAG = 1,
  150. /**
  151. * Long-secondary CE with zero primary.
  152. * Bits 31..16: Secondary weight.
  153. * Bits 15.. 8: Tertiary weight.
  154. */
  155. LONG_SECONDARY_TAG = 2,
  156. /**
  157. * Unused.
  158. * May be used in the future for single-byte secondary CEs (SHORT_SECONDARY_TAG),
  159. * storing the secondary in bits 31..24, the ccc in bits 23..16,
  160. * and the tertiary in bits 15..8.
  161. */
  162. RESERVED_TAG_3 = 3,
  163. /**
  164. * Latin mini expansions of two simple CEs [pp, 05, tt] [00, ss, 05].
  165. * Bits 31..24: Single-byte primary weight pp of the first CE.
  166. * Bits 23..16: Tertiary weight tt of the first CE.
  167. * Bits 15.. 8: Secondary weight ss of the second CE.
  168. */
  169. LATIN_EXPANSION_TAG = 4,
  170. /**
  171. * Points to one or more simple/long-primary/long-secondary 32-bit CE32s.
  172. * Bits 31..13: Index into uint32_t table.
  173. * Bits 12.. 8: Length=1..31.
  174. */
  175. EXPANSION32_TAG = 5,
  176. /**
  177. * Points to one or more 64-bit CEs.
  178. * Bits 31..13: Index into CE table.
  179. * Bits 12.. 8: Length=1..31.
  180. */
  181. EXPANSION_TAG = 6,
  182. /**
  183. * Builder data, used only in the CollationDataBuilder, not in runtime data.
  184. *
  185. * If bit 8 is 0: Builder context, points to a list of context-sensitive mappings.
  186. * Bits 31..13: Index to the builder's list of ConditionalCE32 for this character.
  187. * Bits 12.. 9: Unused, 0.
  188. *
  189. * If bit 8 is 1 (IS_BUILDER_JAMO_CE32): Builder-only jamoCE32 value.
  190. * The builder fetches the Jamo CE32 from the trie.
  191. * Bits 31..13: Jamo code point.
  192. * Bits 12.. 9: Unused, 0.
  193. */
  194. BUILDER_DATA_TAG = 7,
  195. /**
  196. * Points to prefix trie.
  197. * Bits 31..13: Index into prefix/contraction data.
  198. * Bits 12.. 8: Unused, 0.
  199. */
  200. PREFIX_TAG = 8,
  201. /**
  202. * Points to contraction data.
  203. * Bits 31..13: Index into prefix/contraction data.
  204. * Bits 12..11: Unused, 0.
  205. * Bit 10: CONTRACT_TRAILING_CCC flag.
  206. * Bit 9: CONTRACT_NEXT_CCC flag.
  207. * Bit 8: CONTRACT_SINGLE_CP_NO_MATCH flag.
  208. */
  209. CONTRACTION_TAG = 9,
  210. /**
  211. * Decimal digit.
  212. * Bits 31..13: Index into uint32_t table for non-numeric-collation CE32.
  213. * Bit 12: Unused, 0.
  214. * Bits 11.. 8: Digit value 0..9.
  215. */
  216. DIGIT_TAG = 10,
  217. /**
  218. * Tag for U+0000, for moving the NUL-termination handling
  219. * from the regular fastpath into specials-handling code.
  220. * Bits 31..8: Unused, 0.
  221. */
  222. U0000_TAG = 11,
  223. /**
  224. * Tag for a Hangul syllable.
  225. * Bits 31..9: Unused, 0.
  226. * Bit 8: HANGUL_NO_SPECIAL_JAMO flag.
  227. */
  228. HANGUL_TAG = 12,
  229. /**
  230. * Tag for a lead surrogate code unit.
  231. * Optional optimization for UTF-16 string processing.
  232. * Bits 31..10: Unused, 0.
  233. * 9.. 8: =0: All associated supplementary code points are unassigned-implict.
  234. * =1: All associated supplementary code points fall back to the base data.
  235. * else: (Normally 2) Look up the data for the supplementary code point.
  236. */
  237. LEAD_SURROGATE_TAG = 13,
  238. /**
  239. * Tag for CEs with primary weights in code point order.
  240. * Bits 31..13: Index into CE table, for one data "CE".
  241. * Bits 12.. 8: Unused, 0.
  242. *
  243. * This data "CE" has the following bit fields:
  244. * Bits 63..32: Three-byte primary pppppp00.
  245. * 31.. 8: Start/base code point of the in-order range.
  246. * 7: Flag isCompressible primary.
  247. * 6.. 0: Per-code point primary-weight increment.
  248. */
  249. OFFSET_TAG = 14,
  250. /**
  251. * Implicit CE tag. Compute an unassigned-implicit CE.
  252. * All bits are set (UNASSIGNED_CE32=0xffffffff).
  253. */
  254. IMPLICIT_TAG = 15
  255. };
  256. static UBool isAssignedCE32(uint32_t ce32) {
  257. return ce32 != FALLBACK_CE32 && ce32 != UNASSIGNED_CE32;
  258. }
  259. /**
  260. * We limit the number of CEs in an expansion
  261. * so that we can use a small number of length bits in the data structure,
  262. * and so that an implementation can copy CEs at runtime without growing a destination buffer.
  263. */
  264. static const int32_t MAX_EXPANSION_LENGTH = 31;
  265. static const int32_t MAX_INDEX = 0x7ffff;
  266. /**
  267. * Set if there is no match for the single (no-suffix) character itself.
  268. * This is only possible if there is a prefix.
  269. * In this case, discontiguous contraction matching cannot add combining marks
  270. * starting from an empty suffix.
  271. * The default CE32 is used anyway if there is no suffix match.
  272. */
  273. static const uint32_t CONTRACT_SINGLE_CP_NO_MATCH = 0x100;
  274. /** Set if the first character of every contraction suffix has lccc!=0. */
  275. static const uint32_t CONTRACT_NEXT_CCC = 0x200;
  276. /** Set if any contraction suffix ends with lccc!=0. */
  277. static const uint32_t CONTRACT_TRAILING_CCC = 0x400;
  278. /** For HANGUL_TAG: None of its Jamo CE32s isSpecialCE32(). */
  279. static const uint32_t HANGUL_NO_SPECIAL_JAMO = 0x100;
  280. static const uint32_t LEAD_ALL_UNASSIGNED = 0;
  281. static const uint32_t LEAD_ALL_FALLBACK = 0x100;
  282. static const uint32_t LEAD_MIXED = 0x200;
  283. static const uint32_t LEAD_TYPE_MASK = 0x300;
  284. static uint32_t makeLongPrimaryCE32(uint32_t p) { return p | LONG_PRIMARY_CE32_LOW_BYTE; }
  285. /** Turns the long-primary CE32 into a primary weight pppppp00. */
  286. static inline uint32_t primaryFromLongPrimaryCE32(uint32_t ce32) {
  287. return ce32 & 0xffffff00;
  288. }
  289. static inline int64_t ceFromLongPrimaryCE32(uint32_t ce32) {
  290. return ((int64_t)(ce32 & 0xffffff00) << 32) | COMMON_SEC_AND_TER_CE;
  291. }
  292. static uint32_t makeLongSecondaryCE32(uint32_t lower32) {
  293. return lower32 | SPECIAL_CE32_LOW_BYTE | LONG_SECONDARY_TAG;
  294. }
  295. static inline int64_t ceFromLongSecondaryCE32(uint32_t ce32) {
  296. return ce32 & 0xffffff00;
  297. }
  298. /** Makes a special CE32 with tag, index and length. */
  299. static uint32_t makeCE32FromTagIndexAndLength(int32_t tag, int32_t index, int32_t length) {
  300. return (index << 13) | (length << 8) | SPECIAL_CE32_LOW_BYTE | tag;
  301. }
  302. /** Makes a special CE32 with only tag and index. */
  303. static uint32_t makeCE32FromTagAndIndex(int32_t tag, int32_t index) {
  304. return (index << 13) | SPECIAL_CE32_LOW_BYTE | tag;
  305. }
  306. static inline UBool isSpecialCE32(uint32_t ce32) {
  307. return (ce32 & 0xff) >= SPECIAL_CE32_LOW_BYTE;
  308. }
  309. static inline int32_t tagFromCE32(uint32_t ce32) {
  310. return (int32_t)(ce32 & 0xf);
  311. }
  312. static inline UBool hasCE32Tag(uint32_t ce32, int32_t tag) {
  313. return isSpecialCE32(ce32) && tagFromCE32(ce32) == tag;
  314. }
  315. static inline UBool isLongPrimaryCE32(uint32_t ce32) {
  316. return hasCE32Tag(ce32, LONG_PRIMARY_TAG);
  317. }
  318. static UBool isSimpleOrLongCE32(uint32_t ce32) {
  319. return !isSpecialCE32(ce32) ||
  320. tagFromCE32(ce32) == LONG_PRIMARY_TAG ||
  321. tagFromCE32(ce32) == LONG_SECONDARY_TAG;
  322. }
  323. /**
  324. * @return TRUE if the ce32 yields one or more CEs without further data lookups
  325. */
  326. static UBool isSelfContainedCE32(uint32_t ce32) {
  327. return !isSpecialCE32(ce32) ||
  328. tagFromCE32(ce32) == LONG_PRIMARY_TAG ||
  329. tagFromCE32(ce32) == LONG_SECONDARY_TAG ||
  330. tagFromCE32(ce32) == LATIN_EXPANSION_TAG;
  331. }
  332. static inline UBool isPrefixCE32(uint32_t ce32) {
  333. return hasCE32Tag(ce32, PREFIX_TAG);
  334. }
  335. static inline UBool isContractionCE32(uint32_t ce32) {
  336. return hasCE32Tag(ce32, CONTRACTION_TAG);
  337. }
  338. static inline UBool ce32HasContext(uint32_t ce32) {
  339. return isSpecialCE32(ce32) &&
  340. (tagFromCE32(ce32) == PREFIX_TAG ||
  341. tagFromCE32(ce32) == CONTRACTION_TAG);
  342. }
  343. /**
  344. * Get the first of the two Latin-expansion CEs encoded in ce32.
  345. * @see LATIN_EXPANSION_TAG
  346. */
  347. static inline int64_t latinCE0FromCE32(uint32_t ce32) {
  348. return ((int64_t)(ce32 & 0xff000000) << 32) | COMMON_SECONDARY_CE | ((ce32 & 0xff0000) >> 8);
  349. }
  350. /**
  351. * Get the second of the two Latin-expansion CEs encoded in ce32.
  352. * @see LATIN_EXPANSION_TAG
  353. */
  354. static inline int64_t latinCE1FromCE32(uint32_t ce32) {
  355. return ((ce32 & 0xff00) << 16) | COMMON_TERTIARY_CE;
  356. }
  357. /**
  358. * Returns the data index from a special CE32.
  359. */
  360. static inline int32_t indexFromCE32(uint32_t ce32) {
  361. return (int32_t)(ce32 >> 13);
  362. }
  363. /**
  364. * Returns the data length from a ce32.
  365. */
  366. static inline int32_t lengthFromCE32(uint32_t ce32) {
  367. return (ce32 >> 8) & 31;
  368. }
  369. /**
  370. * Returns the digit value from a DIGIT_TAG ce32.
  371. */
  372. static inline char digitFromCE32(uint32_t ce32) {
  373. return (char)((ce32 >> 8) & 0xf);
  374. }
  375. /** Returns a 64-bit CE from a simple CE32 (not special). */
  376. static inline int64_t ceFromSimpleCE32(uint32_t ce32) {
  377. // normal form ppppsstt -> pppp0000ss00tt00
  378. // assert (ce32 & 0xff) < SPECIAL_CE32_LOW_BYTE
  379. return ((int64_t)(ce32 & 0xffff0000) << 32) | ((ce32 & 0xff00) << 16) | ((ce32 & 0xff) << 8);
  380. }
  381. /** Returns a 64-bit CE from a simple/long-primary/long-secondary CE32. */
  382. static inline int64_t ceFromCE32(uint32_t ce32) {
  383. uint32_t tertiary = ce32 & 0xff;
  384. if(tertiary < SPECIAL_CE32_LOW_BYTE) {
  385. // normal form ppppsstt -> pppp0000ss00tt00
  386. return ((int64_t)(ce32 & 0xffff0000) << 32) | ((ce32 & 0xff00) << 16) | (tertiary << 8);
  387. } else {
  388. ce32 -= tertiary;
  389. if((tertiary & 0xf) == LONG_PRIMARY_TAG) {
  390. // long-primary form ppppppC1 -> pppppp00050000500
  391. return ((int64_t)ce32 << 32) | COMMON_SEC_AND_TER_CE;
  392. } else {
  393. // long-secondary form ssssttC2 -> 00000000sssstt00
  394. // assert (tertiary & 0xf) == LONG_SECONDARY_TAG
  395. return ce32;
  396. }
  397. }
  398. }
  399. /** Creates a CE from a primary weight. */
  400. static inline int64_t makeCE(uint32_t p) {
  401. return ((int64_t)p << 32) | COMMON_SEC_AND_TER_CE;
  402. }
  403. /**
  404. * Creates a CE from a primary weight,
  405. * 16-bit secondary/tertiary weights, and a 2-bit quaternary.
  406. */
  407. static inline int64_t makeCE(uint32_t p, uint32_t s, uint32_t t, uint32_t q) {
  408. return ((int64_t)p << 32) | (s << 16) | t | (q << 6);
  409. }
  410. /**
  411. * Increments a 2-byte primary by a code point offset.
  412. */
  413. static uint32_t incTwoBytePrimaryByOffset(uint32_t basePrimary, UBool isCompressible,
  414. int32_t offset);
  415. /**
  416. * Increments a 3-byte primary by a code point offset.
  417. */
  418. static uint32_t incThreeBytePrimaryByOffset(uint32_t basePrimary, UBool isCompressible,
  419. int32_t offset);
  420. /**
  421. * Decrements a 2-byte primary by one range step (1..0x7f).
  422. */
  423. static uint32_t decTwoBytePrimaryByOneStep(uint32_t basePrimary, UBool isCompressible, int32_t step);
  424. /**
  425. * Decrements a 3-byte primary by one range step (1..0x7f).
  426. */
  427. static uint32_t decThreeBytePrimaryByOneStep(uint32_t basePrimary, UBool isCompressible, int32_t step);
  428. /**
  429. * Computes a 3-byte primary for c's OFFSET_TAG data "CE".
  430. */
  431. static uint32_t getThreeBytePrimaryForOffsetData(UChar32 c, int64_t dataCE);
  432. /**
  433. * Returns the unassigned-character implicit primary weight for any valid code point c.
  434. */
  435. static uint32_t unassignedPrimaryFromCodePoint(UChar32 c);
  436. static inline int64_t unassignedCEFromCodePoint(UChar32 c) {
  437. return makeCE(unassignedPrimaryFromCodePoint(c));
  438. }
  439. private:
  440. Collation(); // No instantiation.
  441. };
  442. U_NAMESPACE_END
  443. #endif // !UCONFIG_NO_COLLATION
  444. #endif // __COLLATION_H__