collationfastlatin.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2013-2015, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. *******************************************************************************
  8. * collationfastlatin.h
  9. *
  10. * created on: 2013aug09
  11. * created by: Markus W. Scherer
  12. */
  13. #ifndef __COLLATIONFASTLATIN_H__
  14. #define __COLLATIONFASTLATIN_H__
  15. #include "unicode/utypes.h"
  16. #if !UCONFIG_NO_COLLATION
  17. U_NAMESPACE_BEGIN
  18. struct CollationData;
  19. struct CollationSettings;
  20. class U_I18N_API CollationFastLatin /* all static */ {
  21. public:
  22. /**
  23. * Fast Latin format version (one byte 1..FF).
  24. * Must be incremented for any runtime-incompatible changes,
  25. * in particular, for changes to any of the following constants.
  26. *
  27. * When the major version number of the main data format changes,
  28. * we can reset this fast Latin version to 1.
  29. */
  30. static const uint16_t VERSION = 2;
  31. static const int32_t LATIN_MAX = 0x17f;
  32. static const int32_t LATIN_LIMIT = LATIN_MAX + 1;
  33. static const int32_t LATIN_MAX_UTF8_LEAD = 0xc5; // UTF-8 lead byte of LATIN_MAX
  34. static const int32_t PUNCT_START = 0x2000;
  35. static const int32_t PUNCT_LIMIT = 0x2040;
  36. // excludes U+FFFE & U+FFFF
  37. static const int32_t NUM_FAST_CHARS = LATIN_LIMIT + (PUNCT_LIMIT - PUNCT_START);
  38. // Note on the supported weight ranges:
  39. // Analysis of UCA 6.3 and CLDR 23 non-search tailorings shows that
  40. // the CEs for characters in the above ranges, excluding expansions with length >2,
  41. // excluding contractions of >2 characters, and other restrictions
  42. // (see the builder's getCEsFromCE32()),
  43. // use at most about 150 primary weights,
  44. // where about 94 primary weights are possibly-variable (space/punct/symbol/currency),
  45. // at most 4 secondary before-common weights,
  46. // at most 4 secondary after-common weights,
  47. // at most 16 secondary high weights (in secondary CEs), and
  48. // at most 4 tertiary after-common weights.
  49. // The following ranges are designed to support slightly more weights than that.
  50. // (en_US_POSIX is unusual: It creates about 64 variable + 116 Latin primaries.)
  51. // Digits may use long primaries (preserving more short ones)
  52. // or short primaries (faster) without changing this data structure.
  53. // (If we supported numeric collation, then digits would have to have long primaries
  54. // so that special handling does not affect the fast path.)
  55. static const uint32_t SHORT_PRIMARY_MASK = 0xfc00; // bits 15..10
  56. static const uint32_t INDEX_MASK = 0x3ff; // bits 9..0 for expansions & contractions
  57. static const uint32_t SECONDARY_MASK = 0x3e0; // bits 9..5
  58. static const uint32_t CASE_MASK = 0x18; // bits 4..3
  59. static const uint32_t LONG_PRIMARY_MASK = 0xfff8; // bits 15..3
  60. static const uint32_t TERTIARY_MASK = 7; // bits 2..0
  61. static const uint32_t CASE_AND_TERTIARY_MASK = CASE_MASK | TERTIARY_MASK;
  62. static const uint32_t TWO_SHORT_PRIMARIES_MASK =
  63. (SHORT_PRIMARY_MASK << 16) | SHORT_PRIMARY_MASK; // 0xfc00fc00
  64. static const uint32_t TWO_LONG_PRIMARIES_MASK =
  65. (LONG_PRIMARY_MASK << 16) | LONG_PRIMARY_MASK; // 0xfff8fff8
  66. static const uint32_t TWO_SECONDARIES_MASK =
  67. (SECONDARY_MASK << 16) | SECONDARY_MASK; // 0x3e003e0
  68. static const uint32_t TWO_CASES_MASK =
  69. (CASE_MASK << 16) | CASE_MASK; // 0x180018
  70. static const uint32_t TWO_TERTIARIES_MASK =
  71. (TERTIARY_MASK << 16) | TERTIARY_MASK; // 0x70007
  72. /**
  73. * Contraction with one fast Latin character.
  74. * Use INDEX_MASK to find the start of the contraction list after the fixed table.
  75. * The first entry contains the default mapping.
  76. * Otherwise use CONTR_CHAR_MASK for the contraction character index
  77. * (in ascending order).
  78. * Use CONTR_LENGTH_SHIFT for the length of the entry
  79. * (1=BAIL_OUT, 2=one CE, 3=two CEs).
  80. *
  81. * Also, U+0000 maps to a contraction entry, so that the fast path need not
  82. * check for NUL termination.
  83. * It usually maps to a contraction list with only the completely ignorable default value.
  84. */
  85. static const uint32_t CONTRACTION = 0x400;
  86. /**
  87. * An expansion encodes two CEs.
  88. * Use INDEX_MASK to find the pair of CEs after the fixed table.
  89. *
  90. * The higher a mini CE value, the easier it is to process.
  91. * For expansions and higher, no context needs to be considered.
  92. */
  93. static const uint32_t EXPANSION = 0x800;
  94. /**
  95. * Encodes one CE with a long/low mini primary (there are 128).
  96. * All potentially-variable primaries must be in this range,
  97. * to make the short-primary path as fast as possible.
  98. */
  99. static const uint32_t MIN_LONG = 0xc00;
  100. static const uint32_t LONG_INC = 8;
  101. static const uint32_t MAX_LONG = 0xff8;
  102. /**
  103. * Encodes one CE with a short/high primary (there are 60),
  104. * plus a secondary CE if the secondary weight is high.
  105. * Fast handling: At least all letter primaries should be in this range.
  106. */
  107. static const uint32_t MIN_SHORT = 0x1000;
  108. static const uint32_t SHORT_INC = 0x400;
  109. /** The highest primary weight is reserved for U+FFFF. */
  110. static const uint32_t MAX_SHORT = SHORT_PRIMARY_MASK;
  111. static const uint32_t MIN_SEC_BEFORE = 0; // must add SEC_OFFSET
  112. static const uint32_t SEC_INC = 0x20;
  113. static const uint32_t MAX_SEC_BEFORE = MIN_SEC_BEFORE + 4 * SEC_INC; // 5 before common
  114. static const uint32_t COMMON_SEC = MAX_SEC_BEFORE + SEC_INC;
  115. static const uint32_t MIN_SEC_AFTER = COMMON_SEC + SEC_INC;
  116. static const uint32_t MAX_SEC_AFTER = MIN_SEC_AFTER + 5 * SEC_INC; // 6 after common
  117. static const uint32_t MIN_SEC_HIGH = MAX_SEC_AFTER + SEC_INC; // 20 high secondaries
  118. static const uint32_t MAX_SEC_HIGH = SECONDARY_MASK;
  119. /**
  120. * Lookup: Add this offset to secondary weights, except for completely ignorable CEs.
  121. * Must be greater than any special value, e.g., MERGE_WEIGHT.
  122. * The exact value is not relevant for the format version.
  123. */
  124. static const uint32_t SEC_OFFSET = SEC_INC;
  125. static const uint32_t COMMON_SEC_PLUS_OFFSET = COMMON_SEC + SEC_OFFSET;
  126. static const uint32_t TWO_SEC_OFFSETS =
  127. (SEC_OFFSET << 16) | SEC_OFFSET; // 0x200020
  128. static const uint32_t TWO_COMMON_SEC_PLUS_OFFSET =
  129. (COMMON_SEC_PLUS_OFFSET << 16) | COMMON_SEC_PLUS_OFFSET;
  130. static const uint32_t LOWER_CASE = 8; // case bits include this offset
  131. static const uint32_t TWO_LOWER_CASES = (LOWER_CASE << 16) | LOWER_CASE; // 0x80008
  132. static const uint32_t COMMON_TER = 0; // must add TER_OFFSET
  133. static const uint32_t MAX_TER_AFTER = 7; // 7 after common
  134. /**
  135. * Lookup: Add this offset to tertiary weights, except for completely ignorable CEs.
  136. * Must be greater than any special value, e.g., MERGE_WEIGHT.
  137. * Must be greater than case bits as well, so that with combined case+tertiary weights
  138. * plus the offset the tertiary bits does not spill over into the case bits.
  139. * The exact value is not relevant for the format version.
  140. */
  141. static const uint32_t TER_OFFSET = SEC_OFFSET;
  142. static const uint32_t COMMON_TER_PLUS_OFFSET = COMMON_TER + TER_OFFSET;
  143. static const uint32_t TWO_TER_OFFSETS = (TER_OFFSET << 16) | TER_OFFSET;
  144. static const uint32_t TWO_COMMON_TER_PLUS_OFFSET =
  145. (COMMON_TER_PLUS_OFFSET << 16) | COMMON_TER_PLUS_OFFSET;
  146. static const uint32_t MERGE_WEIGHT = 3;
  147. static const uint32_t EOS = 2; // end of string
  148. static const uint32_t BAIL_OUT = 1;
  149. /**
  150. * Contraction result first word bits 8..0 contain the
  151. * second contraction character, as a char index 0..NUM_FAST_CHARS-1.
  152. * Each contraction list is terminated with a word containing CONTR_CHAR_MASK.
  153. */
  154. static const uint32_t CONTR_CHAR_MASK = 0x1ff;
  155. /**
  156. * Contraction result first word bits 10..9 contain the result length:
  157. * 1=bail out, 2=one mini CE, 3=two mini CEs
  158. */
  159. static const uint32_t CONTR_LENGTH_SHIFT = 9;
  160. /**
  161. * Comparison return value when the regular comparison must be used.
  162. * The exact value is not relevant for the format version.
  163. */
  164. static const int32_t BAIL_OUT_RESULT = -2;
  165. static inline int32_t getCharIndex(UChar c) {
  166. if(c <= LATIN_MAX) {
  167. return c;
  168. } else if(PUNCT_START <= c && c < PUNCT_LIMIT) {
  169. return c - (PUNCT_START - LATIN_LIMIT);
  170. } else {
  171. // Not a fast Latin character.
  172. // Note: U+FFFE & U+FFFF are forbidden in tailorings
  173. // and thus do not occur in any contractions.
  174. return -1;
  175. }
  176. }
  177. /**
  178. * Computes the options value for the compare functions
  179. * and writes the precomputed primary weights.
  180. * Returns -1 if the Latin fastpath is not supported for the data and settings.
  181. * The capacity must be LATIN_LIMIT.
  182. */
  183. static int32_t getOptions(const CollationData *data, const CollationSettings &settings,
  184. uint16_t *primaries, int32_t capacity);
  185. static int32_t compareUTF16(const uint16_t *table, const uint16_t *primaries, int32_t options,
  186. const UChar *left, int32_t leftLength,
  187. const UChar *right, int32_t rightLength);
  188. static int32_t compareUTF8(const uint16_t *table, const uint16_t *primaries, int32_t options,
  189. const uint8_t *left, int32_t leftLength,
  190. const uint8_t *right, int32_t rightLength);
  191. private:
  192. static uint32_t lookup(const uint16_t *table, UChar32 c);
  193. static uint32_t lookupUTF8(const uint16_t *table, UChar32 c,
  194. const uint8_t *s8, int32_t &sIndex, int32_t sLength);
  195. static uint32_t lookupUTF8Unsafe(const uint16_t *table, UChar32 c,
  196. const uint8_t *s8, int32_t &sIndex);
  197. static uint32_t nextPair(const uint16_t *table, UChar32 c, uint32_t ce,
  198. const UChar *s16, const uint8_t *s8, int32_t &sIndex, int32_t &sLength);
  199. static inline uint32_t getPrimaries(uint32_t variableTop, uint32_t pair) {
  200. uint32_t ce = pair & 0xffff;
  201. if(ce >= MIN_SHORT) { return pair & TWO_SHORT_PRIMARIES_MASK; }
  202. if(ce > variableTop) { return pair & TWO_LONG_PRIMARIES_MASK; }
  203. if(ce >= MIN_LONG) { return 0; } // variable
  204. return pair; // special mini CE
  205. }
  206. static inline uint32_t getSecondariesFromOneShortCE(uint32_t ce) {
  207. ce &= SECONDARY_MASK;
  208. if(ce < MIN_SEC_HIGH) {
  209. return ce + SEC_OFFSET;
  210. } else {
  211. return ((ce + SEC_OFFSET) << 16) | COMMON_SEC_PLUS_OFFSET;
  212. }
  213. }
  214. static uint32_t getSecondaries(uint32_t variableTop, uint32_t pair);
  215. static uint32_t getCases(uint32_t variableTop, UBool strengthIsPrimary, uint32_t pair);
  216. static uint32_t getTertiaries(uint32_t variableTop, UBool withCaseBits, uint32_t pair);
  217. static uint32_t getQuaternaries(uint32_t variableTop, uint32_t pair);
  218. private:
  219. CollationFastLatin(); // no constructor
  220. };
  221. /*
  222. * Format of the CollationFastLatin data table.
  223. * CollationFastLatin::VERSION = 2.
  224. *
  225. * This table contains data for a Latin-text collation fastpath.
  226. * The data is stored as an array of uint16_t which contains the following parts.
  227. *
  228. * uint16_t -- version & header length
  229. * Bits 15..8: version, must match the VERSION
  230. * 7..0: length of the header
  231. *
  232. * uint16_t varTops[header length - 1]
  233. * Version 2:
  234. * varTops[m] is the highest CollationFastLatin long-primary weight
  235. * of supported maxVariable group m
  236. * (special reorder group space, punct, symbol, currency).
  237. *
  238. * Version 1:
  239. * Each of these values maps the variable top lead byte of a supported maxVariable group
  240. * to the highest CollationFastLatin long-primary weight.
  241. * The values are stored in ascending order.
  242. * Bits 15..7: max fast-Latin long-primary weight (bits 11..3 shifted left by 4 bits)
  243. * 6..0: regular primary lead byte
  244. *
  245. * uint16_t miniCEs[0x1c0]
  246. * A mini collation element for each character U+0000..U+017F and U+2000..U+203F.
  247. * Each value encodes one or two mini CEs (two are possible if the first one
  248. * has a short mini primary and the second one is a secondary CE, i.e., primary == 0),
  249. * or points to an expansion or to a contraction table.
  250. * U+0000 always has a contraction entry,
  251. * so that NUL-termination need not be tested in the fastpath.
  252. * If the collation elements for a character or contraction cannot be encoded in this format,
  253. * then the BAIL_OUT value is stored.
  254. * For details see the comments for the class constants.
  255. *
  256. * uint16_t expansions[variable length];
  257. * Expansion mini CEs contain an offset relative to just after the miniCEs table.
  258. * An expansions contains exactly 2 mini CEs.
  259. *
  260. * uint16_t contractions[variable length];
  261. * Contraction mini CEs contain an offset relative to just after the miniCEs table.
  262. * It points to a list of tuples which map from a contraction suffix character to a result.
  263. * First uint16_t of each tuple:
  264. * Bits 10..9: Length of the result (1..3), see comments on CONTR_LENGTH_SHIFT.
  265. * Bits 8..0: Contraction character, see comments on CONTR_CHAR_MASK.
  266. * This is followed by 0, 1, or 2 uint16_t according to the length.
  267. * Each list is terminated by an entry with CONTR_CHAR_MASK.
  268. * Each list starts with such an entry which also contains the default result
  269. * for when there is no contraction match.
  270. *
  271. * -----------------
  272. * Changes for version 2 (ICU 55)
  273. *
  274. * Special reorder groups do not necessarily start on whole primary lead bytes any more.
  275. * Therefore, the varTops data has a new format:
  276. * Version 1 stored the lead bytes of the highest root primaries for
  277. * the maxVariable-supported special reorder groups.
  278. * Now the top 16 bits would need to be stored,
  279. * and it is simpler to store only the fast-Latin weights.
  280. */
  281. U_NAMESPACE_END
  282. #endif // !UCONFIG_NO_COLLATION
  283. #endif // __COLLATIONFASTLATIN_H__