collationdata.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. * collationdata.h
  9. *
  10. * created on: 2010oct27
  11. * created by: Markus W. Scherer
  12. */
  13. #ifndef __COLLATIONDATA_H__
  14. #define __COLLATIONDATA_H__
  15. #include "unicode/utypes.h"
  16. #if !UCONFIG_NO_COLLATION
  17. #include "unicode/ucol.h"
  18. #include "unicode/uniset.h"
  19. #include "collation.h"
  20. #include "normalizer2impl.h"
  21. #include "utrie2.h"
  22. struct UDataMemory;
  23. U_NAMESPACE_BEGIN
  24. class UVector32;
  25. /**
  26. * Collation data container.
  27. * Immutable data created by a CollationDataBuilder, or loaded from a file,
  28. * or deserialized from API-provided binary data.
  29. *
  30. * Includes data for the collation base (root/default), aliased if this is not the base.
  31. */
  32. struct U_I18N_API CollationData : public UMemory {
  33. // Note: The ucadata.icu loader could discover the reserved ranges by setting an array
  34. // parallel with the ranges, and resetting ranges that are indexed.
  35. // The reordering builder code could clone the resulting template array.
  36. enum {
  37. REORDER_RESERVED_BEFORE_LATIN = UCOL_REORDER_CODE_FIRST + 14,
  38. REORDER_RESERVED_AFTER_LATIN
  39. };
  40. enum {
  41. MAX_NUM_SPECIAL_REORDER_CODES = 8,
  42. /** C++ only, data reader check scriptStartsLength. */
  43. MAX_NUM_SCRIPT_RANGES = 256
  44. };
  45. CollationData(const Normalizer2Impl &nfc)
  46. : trie(NULL),
  47. ce32s(NULL), ces(NULL), contexts(NULL), base(NULL),
  48. jamoCE32s(NULL),
  49. nfcImpl(nfc),
  50. numericPrimary(0x12000000),
  51. ce32sLength(0), cesLength(0), contextsLength(0),
  52. compressibleBytes(NULL),
  53. unsafeBackwardSet(NULL),
  54. fastLatinTable(NULL), fastLatinTableLength(0),
  55. numScripts(0), scriptsIndex(NULL), scriptStarts(NULL), scriptStartsLength(0),
  56. rootElements(NULL), rootElementsLength(0) {}
  57. uint32_t getCE32(UChar32 c) const {
  58. return UTRIE2_GET32(trie, c);
  59. }
  60. uint32_t getCE32FromSupplementary(UChar32 c) const {
  61. return UTRIE2_GET32_FROM_SUPP(trie, c);
  62. }
  63. UBool isDigit(UChar32 c) const {
  64. return c < 0x660 ? c <= 0x39 && 0x30 <= c :
  65. Collation::hasCE32Tag(getCE32(c), Collation::DIGIT_TAG);
  66. }
  67. UBool isUnsafeBackward(UChar32 c, UBool numeric) const {
  68. return unsafeBackwardSet->contains(c) || (numeric && isDigit(c));
  69. }
  70. UBool isCompressibleLeadByte(uint32_t b) const {
  71. return compressibleBytes[b];
  72. }
  73. inline UBool isCompressiblePrimary(uint32_t p) const {
  74. return isCompressibleLeadByte(p >> 24);
  75. }
  76. /**
  77. * Returns the CE32 from two contexts words.
  78. * Access to the defaultCE32 for contraction and prefix matching.
  79. */
  80. static uint32_t readCE32(const UChar *p) {
  81. return ((uint32_t)p[0] << 16) | p[1];
  82. }
  83. /**
  84. * Returns the CE32 for an indirect special CE32 (e.g., with DIGIT_TAG).
  85. * Requires that ce32 is special.
  86. */
  87. uint32_t getIndirectCE32(uint32_t ce32) const;
  88. /**
  89. * Returns the CE32 for an indirect special CE32 (e.g., with DIGIT_TAG),
  90. * if ce32 is special.
  91. */
  92. uint32_t getFinalCE32(uint32_t ce32) const;
  93. /**
  94. * Computes a CE from c's ce32 which has the OFFSET_TAG.
  95. */
  96. int64_t getCEFromOffsetCE32(UChar32 c, uint32_t ce32) const {
  97. int64_t dataCE = ces[Collation::indexFromCE32(ce32)];
  98. return Collation::makeCE(Collation::getThreeBytePrimaryForOffsetData(c, dataCE));
  99. }
  100. /**
  101. * Returns the single CE that c maps to.
  102. * Sets U_UNSUPPORTED_ERROR if c does not map to a single CE.
  103. */
  104. int64_t getSingleCE(UChar32 c, UErrorCode &errorCode) const;
  105. /**
  106. * Returns the FCD16 value for code point c. c must be >= 0.
  107. */
  108. uint16_t getFCD16(UChar32 c) const {
  109. return nfcImpl.getFCD16(c);
  110. }
  111. /**
  112. * Returns the first primary for the script's reordering group.
  113. * @return the primary with only the first primary lead byte of the group
  114. * (not necessarily an actual root collator primary weight),
  115. * or 0 if the script is unknown
  116. */
  117. uint32_t getFirstPrimaryForGroup(int32_t script) const;
  118. /**
  119. * Returns the last primary for the script's reordering group.
  120. * @return the last primary of the group
  121. * (not an actual root collator primary weight),
  122. * or 0 if the script is unknown
  123. */
  124. uint32_t getLastPrimaryForGroup(int32_t script) const;
  125. /**
  126. * Finds the reordering group which contains the primary weight.
  127. * @return the first script of the group, or -1 if the weight is beyond the last group
  128. */
  129. int32_t getGroupForPrimary(uint32_t p) const;
  130. int32_t getEquivalentScripts(int32_t script,
  131. int32_t dest[], int32_t capacity, UErrorCode &errorCode) const;
  132. /**
  133. * Writes the permutation of primary-weight ranges
  134. * for the given reordering of scripts and groups.
  135. * The caller checks for illegal arguments and
  136. * takes care of [DEFAULT] and memory allocation.
  137. *
  138. * Each list element will be a (limit, offset) pair as described
  139. * for the CollationSettings::reorderRanges.
  140. * The list will be empty if no ranges are reordered.
  141. */
  142. void makeReorderRanges(const int32_t *reorder, int32_t length,
  143. UVector32 &ranges, UErrorCode &errorCode) const;
  144. /** @see jamoCE32s */
  145. static const int32_t JAMO_CE32S_LENGTH = 19 + 21 + 27;
  146. /** Main lookup trie. */
  147. const UTrie2 *trie;
  148. /**
  149. * Array of CE32 values.
  150. * At index 0 there must be CE32(U+0000)
  151. * to support U+0000's special-tag for NUL-termination handling.
  152. */
  153. const uint32_t *ce32s;
  154. /** Array of CE values for expansions and OFFSET_TAG. */
  155. const int64_t *ces;
  156. /** Array of prefix and contraction-suffix matching data. */
  157. const UChar *contexts;
  158. /** Base collation data, or NULL if this data itself is a base. */
  159. const CollationData *base;
  160. /**
  161. * Simple array of JAMO_CE32S_LENGTH=19+21+27 CE32s, one per canonical Jamo L/V/T.
  162. * They are normally simple CE32s, rarely expansions.
  163. * For fast handling of HANGUL_TAG.
  164. */
  165. const uint32_t *jamoCE32s;
  166. const Normalizer2Impl &nfcImpl;
  167. /** The single-byte primary weight (xx000000) for numeric collation. */
  168. uint32_t numericPrimary;
  169. int32_t ce32sLength;
  170. int32_t cesLength;
  171. int32_t contextsLength;
  172. /** 256 flags for which primary-weight lead bytes are compressible. */
  173. const UBool *compressibleBytes;
  174. /**
  175. * Set of code points that are unsafe for starting string comparison after an identical prefix,
  176. * or in backwards CE iteration.
  177. */
  178. const UnicodeSet *unsafeBackwardSet;
  179. /**
  180. * Fast Latin table for common-Latin-text string comparisons.
  181. * Data structure see class CollationFastLatin.
  182. */
  183. const uint16_t *fastLatinTable;
  184. int32_t fastLatinTableLength;
  185. /**
  186. * Data for scripts and reordering groups.
  187. * Uses include building a reordering permutation table and
  188. * providing script boundaries to AlphabeticIndex.
  189. */
  190. int32_t numScripts;
  191. /**
  192. * The length of scriptsIndex is numScripts+16.
  193. * It maps from a UScriptCode or a special reorder code to an entry in scriptStarts.
  194. * 16 special reorder codes (not all used) are mapped starting at numScripts.
  195. * Up to MAX_NUM_SPECIAL_REORDER_CODES are codes for special groups like space/punct/digit.
  196. * There are special codes at the end for reorder-reserved primary ranges.
  197. *
  198. * Multiple scripts may share a range and index, for example Hira & Kana.
  199. */
  200. const uint16_t *scriptsIndex;
  201. /**
  202. * Start primary weight (top 16 bits only) for a group/script/reserved range
  203. * indexed by scriptsIndex.
  204. * The first range (separators & terminators) and the last range (trailing weights)
  205. * are not reorderable, and no scriptsIndex entry points to them.
  206. */
  207. const uint16_t *scriptStarts;
  208. int32_t scriptStartsLength;
  209. /**
  210. * Collation elements in the root collator.
  211. * Used by the CollationRootElements class. The data structure is described there.
  212. * NULL in a tailoring.
  213. */
  214. const uint32_t *rootElements;
  215. int32_t rootElementsLength;
  216. private:
  217. int32_t getScriptIndex(int32_t script) const;
  218. void makeReorderRanges(const int32_t *reorder, int32_t length,
  219. UBool latinMustMove,
  220. UVector32 &ranges, UErrorCode &errorCode) const;
  221. int32_t addLowScriptRange(uint8_t table[], int32_t index, int32_t lowStart) const;
  222. int32_t addHighScriptRange(uint8_t table[], int32_t index, int32_t highLimit) const;
  223. };
  224. U_NAMESPACE_END
  225. #endif // !UCONFIG_NO_COLLATION
  226. #endif // __COLLATIONDATA_H__