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-2014, International Business Machines 6 * Corporation and others. All Rights Reserved. 7 ******************************************************************************* 8 * collationbuilder.h 9 * 10 * created on: 2013may06 11 * created by: Markus W. Scherer 12 */ 13 14 #ifndef __COLLATIONBUILDER_H__ 15 #define __COLLATIONBUILDER_H__ 16 17 #include "unicode/utypes.h" 18 19 #if !UCONFIG_NO_COLLATION 20 21 #include "unicode/uniset.h" 22 #include "unicode/unistr.h" 23 #include "collationrootelements.h" 24 #include "collationruleparser.h" 25 #include "uvectr32.h" 26 #include "uvectr64.h" 27 28 struct UParseError; 29 30 U_NAMESPACE_BEGIN 31 32 struct CollationData; 33 struct CollationTailoring; 34 35 class CEFinalizer; 36 class CollationDataBuilder; 37 class Normalizer2; 38 class Normalizer2Impl; 39 40 class U_I18N_API CollationBuilder : public CollationRuleParser::Sink { 41 public: 42 CollationBuilder(const CollationTailoring *b, UBool icu4xMode, UErrorCode &errorCode); 43 CollationBuilder(const CollationTailoring *base, UErrorCode &errorCode); 44 virtual ~CollationBuilder(); 45 disableFastLatin()46 void disableFastLatin() { fastLatinEnabled = false; } 47 48 CollationTailoring *parseAndBuild(const UnicodeString &ruleString, 49 const UVersionInfo rulesVersion, 50 CollationRuleParser::Importer *importer, 51 UParseError *outParseError, 52 UErrorCode &errorCode); 53 getErrorReason()54 const char *getErrorReason() const { return errorReason; } 55 56 private: 57 friend class CEFinalizer; 58 59 /** Implements CollationRuleParser::Sink. */ 60 virtual void addReset(int32_t strength, const UnicodeString &str, 61 const char *&errorReason, UErrorCode &errorCode) override; 62 /** 63 * Returns the secondary or tertiary weight preceding the current node's weight. 64 * node=nodes[index]. 65 */ 66 uint32_t getWeight16Before(int32_t index, int64_t node, int32_t level); 67 68 int64_t getSpecialResetPosition(const UnicodeString &str, 69 const char *&parserErrorReason, UErrorCode &errorCode); 70 71 /** Implements CollationRuleParser::Sink. */ 72 virtual void addRelation(int32_t strength, const UnicodeString &prefix, 73 const UnicodeString &str, const UnicodeString &extension, 74 const char *&errorReason, UErrorCode &errorCode) override; 75 76 /** 77 * Picks one of the current CEs and finds or inserts a node in the graph 78 * for the CE + strength. 79 */ 80 int32_t findOrInsertNodeForCEs(int32_t strength, const char *&parserErrorReason, 81 UErrorCode &errorCode); 82 int32_t findOrInsertNodeForRootCE(int64_t ce, int32_t strength, UErrorCode &errorCode); 83 /** Finds or inserts the node for a root CE's primary weight. */ 84 int32_t findOrInsertNodeForPrimary(uint32_t p, UErrorCode &errorCode); 85 /** Finds or inserts the node for a secondary or tertiary weight. */ 86 int32_t findOrInsertWeakNode(int32_t index, uint32_t weight16, int32_t level, 87 UErrorCode &errorCode); 88 89 /** 90 * Makes and inserts a new tailored node into the list, after the one at index. 91 * Skips over nodes of weaker strength to maintain collation order 92 * ("postpone insertion"). 93 * @return the new node's index 94 */ 95 int32_t insertTailoredNodeAfter(int32_t index, int32_t strength, UErrorCode &errorCode); 96 97 /** 98 * Inserts a new node into the list, between list-adjacent items. 99 * The node's previous and next indexes must not be set yet. 100 * @return the new node's index 101 */ 102 int32_t insertNodeBetween(int32_t index, int32_t nextIndex, int64_t node, 103 UErrorCode &errorCode); 104 105 /** 106 * Finds the node which implies or contains a common=05 weight of the given strength 107 * (secondary or tertiary), if the current node is stronger. 108 * Skips weaker nodes and tailored nodes if the current node is stronger 109 * and is followed by an explicit-common-weight node. 110 * Always returns the input index if that node is no stronger than the given strength. 111 */ 112 int32_t findCommonNode(int32_t index, int32_t strength) const; 113 114 void setCaseBits(const UnicodeString &nfdString, 115 const char *&parserErrorReason, UErrorCode &errorCode); 116 117 /** Implements CollationRuleParser::Sink. */ 118 virtual void suppressContractions(const UnicodeSet &set, const char *&parserErrorReason, 119 UErrorCode &errorCode) override; 120 121 /** Implements CollationRuleParser::Sink. */ 122 virtual void optimize(const UnicodeSet &set, const char *&parserErrorReason, 123 UErrorCode &errorCode) override; 124 125 /** 126 * Adds the mapping and its canonical closure. 127 * Takes ce32=dataBuilder->encodeCEs(...) so that the data builder 128 * need not re-encode the CEs multiple times. 129 */ 130 uint32_t addWithClosure(const UnicodeString &nfdPrefix, const UnicodeString &nfdString, 131 const int64_t newCEs[], int32_t newCEsLength, uint32_t ce32, 132 UErrorCode &errorCode); 133 uint32_t addOnlyClosure(const UnicodeString &nfdPrefix, const UnicodeString &nfdString, 134 const int64_t newCEs[], int32_t newCEsLength, uint32_t ce32, 135 UErrorCode &errorCode); 136 void addTailComposites(const UnicodeString &nfdPrefix, const UnicodeString &nfdString, 137 UErrorCode &errorCode); 138 UBool mergeCompositeIntoString(const UnicodeString &nfdString, int32_t indexAfterLastStarter, 139 UChar32 composite, const UnicodeString &decomp, 140 UnicodeString &newNFDString, UnicodeString &newString, 141 UErrorCode &errorCode) const; 142 143 UBool ignorePrefix(const UnicodeString &s, UErrorCode &errorCode) const; 144 UBool ignoreString(const UnicodeString &s, UErrorCode &errorCode) const; 145 UBool isFCD(const UnicodeString &s, UErrorCode &errorCode) const; 146 147 void closeOverComposites(UErrorCode &errorCode); 148 149 uint32_t addIfDifferent(const UnicodeString &prefix, const UnicodeString &str, 150 const int64_t newCEs[], int32_t newCEsLength, uint32_t ce32, 151 UErrorCode &errorCode); 152 static UBool sameCEs(const int64_t ces1[], int32_t ces1Length, 153 const int64_t ces2[], int32_t ces2Length); 154 155 /** 156 * Walks the tailoring graph and overwrites tailored nodes with new CEs. 157 * After this, the graph is destroyed. 158 * The nodes array can then be used only as a source of tailored CEs. 159 */ 160 void makeTailoredCEs(UErrorCode &errorCode); 161 /** 162 * Counts the tailored nodes of the given strength up to the next node 163 * which is either stronger or has an explicit weight of this strength. 164 */ 165 static int32_t countTailoredNodes(const int64_t *nodesArray, int32_t i, int32_t strength); 166 167 /** Replaces temporary CEs with the final CEs they point to. */ 168 void finalizeCEs(UErrorCode &errorCode); 169 170 /** 171 * Encodes "temporary CE" data into a CE that fits into the CE32 data structure, 172 * with 2-byte primary, 1-byte secondary and 6-bit tertiary, 173 * with valid CE byte values. 174 * 175 * The index must not exceed 20 bits (0xfffff). 176 * The strength must fit into 2 bits (UCOL_PRIMARY..UCOL_QUATERNARY). 177 * 178 * Temporary CEs are distinguished from real CEs by their use of 179 * secondary weights 06..45 which are otherwise reserved for compressed sort keys. 180 * 181 * The case bits are unused and available. 182 */ tempCEFromIndexAndStrength(int32_t index,int32_t strength)183 static inline int64_t tempCEFromIndexAndStrength(int32_t index, int32_t strength) { 184 return 185 // CE byte offsets, to ensure valid CE bytes, and case bits 11 186 INT64_C(0x4040000006002000) + 187 // index bits 19..13 -> primary byte 1 = CE bits 63..56 (byte values 40..BF) 188 ((int64_t)(index & 0xfe000) << 43) + 189 // index bits 12..6 -> primary byte 2 = CE bits 55..48 (byte values 40..BF) 190 ((int64_t)(index & 0x1fc0) << 42) + 191 // index bits 5..0 -> secondary byte 1 = CE bits 31..24 (byte values 06..45) 192 ((index & 0x3f) << 24) + 193 // strength bits 1..0 -> tertiary byte 1 = CE bits 13..8 (byte values 20..23) 194 (strength << 8); 195 } indexFromTempCE(int64_t tempCE)196 static inline int32_t indexFromTempCE(int64_t tempCE) { 197 tempCE -= INT64_C(0x4040000006002000); 198 return 199 ((int32_t)(tempCE >> 43) & 0xfe000) | 200 ((int32_t)(tempCE >> 42) & 0x1fc0) | 201 ((int32_t)(tempCE >> 24) & 0x3f); 202 } strengthFromTempCE(int64_t tempCE)203 static inline int32_t strengthFromTempCE(int64_t tempCE) { 204 return ((int32_t)tempCE >> 8) & 3; 205 } isTempCE(int64_t ce)206 static inline UBool isTempCE(int64_t ce) { 207 uint32_t sec = (uint32_t)ce >> 24; 208 return 6 <= sec && sec <= 0x45; 209 } 210 indexFromTempCE32(uint32_t tempCE32)211 static inline int32_t indexFromTempCE32(uint32_t tempCE32) { 212 tempCE32 -= 0x40400620; 213 return 214 ((int32_t)(tempCE32 >> 11) & 0xfe000) | 215 ((int32_t)(tempCE32 >> 10) & 0x1fc0) | 216 ((int32_t)(tempCE32 >> 8) & 0x3f); 217 } isTempCE32(uint32_t ce32)218 static inline UBool isTempCE32(uint32_t ce32) { 219 return 220 (ce32 & 0xff) >= 2 && // not a long-primary/long-secondary CE32 221 6 <= ((ce32 >> 8) & 0xff) && ((ce32 >> 8) & 0xff) <= 0x45; 222 } 223 224 static int32_t ceStrength(int64_t ce); 225 226 /** At most 1M nodes, limited by the 20 bits in node bit fields. */ 227 static const int32_t MAX_INDEX = 0xfffff; 228 /** 229 * Node bit 6 is set on a primary node if there are nodes 230 * with secondary values below the common secondary weight (05). 231 */ 232 static const int32_t HAS_BEFORE2 = 0x40; 233 /** 234 * Node bit 5 is set on a primary or secondary node if there are nodes 235 * with tertiary values below the common tertiary weight (05). 236 */ 237 static const int32_t HAS_BEFORE3 = 0x20; 238 /** 239 * Node bit 3 distinguishes a tailored node, which has no weight value, 240 * from a node with an explicit (root or default) weight. 241 */ 242 static const int32_t IS_TAILORED = 8; 243 nodeFromWeight32(uint32_t weight32)244 static inline int64_t nodeFromWeight32(uint32_t weight32) { 245 return (int64_t)weight32 << 32; 246 } nodeFromWeight16(uint32_t weight16)247 static inline int64_t nodeFromWeight16(uint32_t weight16) { 248 return (int64_t)weight16 << 48; 249 } nodeFromPreviousIndex(int32_t previous)250 static inline int64_t nodeFromPreviousIndex(int32_t previous) { 251 return (int64_t)previous << 28; 252 } nodeFromNextIndex(int32_t next)253 static inline int64_t nodeFromNextIndex(int32_t next) { 254 return next << 8; 255 } nodeFromStrength(int32_t strength)256 static inline int64_t nodeFromStrength(int32_t strength) { 257 return strength; 258 } 259 weight32FromNode(int64_t node)260 static inline uint32_t weight32FromNode(int64_t node) { 261 return (uint32_t)(node >> 32); 262 } weight16FromNode(int64_t node)263 static inline uint32_t weight16FromNode(int64_t node) { 264 return (uint32_t)(node >> 48) & 0xffff; 265 } previousIndexFromNode(int64_t node)266 static inline int32_t previousIndexFromNode(int64_t node) { 267 return (int32_t)(node >> 28) & MAX_INDEX; 268 } nextIndexFromNode(int64_t node)269 static inline int32_t nextIndexFromNode(int64_t node) { 270 return ((int32_t)node >> 8) & MAX_INDEX; 271 } strengthFromNode(int64_t node)272 static inline int32_t strengthFromNode(int64_t node) { 273 return (int32_t)node & 3; 274 } 275 nodeHasBefore2(int64_t node)276 static inline UBool nodeHasBefore2(int64_t node) { 277 return (node & HAS_BEFORE2) != 0; 278 } nodeHasBefore3(int64_t node)279 static inline UBool nodeHasBefore3(int64_t node) { 280 return (node & HAS_BEFORE3) != 0; 281 } nodeHasAnyBefore(int64_t node)282 static inline UBool nodeHasAnyBefore(int64_t node) { 283 return (node & (HAS_BEFORE2 | HAS_BEFORE3)) != 0; 284 } isTailoredNode(int64_t node)285 static inline UBool isTailoredNode(int64_t node) { 286 return (node & IS_TAILORED) != 0; 287 } 288 changeNodePreviousIndex(int64_t node,int32_t previous)289 static inline int64_t changeNodePreviousIndex(int64_t node, int32_t previous) { 290 return (node & INT64_C(0xffff00000fffffff)) | nodeFromPreviousIndex(previous); 291 } changeNodeNextIndex(int64_t node,int32_t next)292 static inline int64_t changeNodeNextIndex(int64_t node, int32_t next) { 293 return (node & INT64_C(0xfffffffff00000ff)) | nodeFromNextIndex(next); 294 } 295 296 const Normalizer2 &nfd, &fcd; 297 const Normalizer2Impl &nfcImpl; 298 299 const CollationTailoring *base; 300 const CollationData *baseData; 301 const CollationRootElements rootElements; 302 uint32_t variableTop; 303 304 CollationDataBuilder *dataBuilder; 305 UBool fastLatinEnabled; 306 UBool icu4xMode; 307 UnicodeSet optimizeSet; 308 const char *errorReason; 309 310 int64_t ces[Collation::MAX_EXPANSION_LENGTH]; 311 int32_t cesLength; 312 313 /** 314 * Indexes of nodes with root primary weights, sorted by primary. 315 * Compact form of a TreeMap from root primary to node index. 316 * 317 * This is a performance optimization for finding reset positions. 318 * Without this, we would have to search through the entire nodes list. 319 * It also allows storing root primary weights in list head nodes, 320 * without previous index, leaving room in root primary nodes for 32-bit primary weights. 321 */ 322 UVector32 rootPrimaryIndexes; 323 /** 324 * Data structure for assigning tailored weights and CEs. 325 * Doubly-linked lists of nodes in mostly collation order. 326 * Each list starts with a root primary node and ends with a nextIndex of 0. 327 * 328 * When there are any nodes in the list, then there is always a root primary node at index 0. 329 * This allows some code not to have to check explicitly for nextIndex==0. 330 * 331 * Root primary nodes have 32-bit weights but do not have previous indexes. 332 * All other nodes have at most 16-bit weights and do have previous indexes. 333 * 334 * Nodes with explicit weights store root collator weights, 335 * or default weak weights (e.g., secondary 05) for stronger nodes. 336 * "Tailored" nodes, with the IS_TAILORED bit set, 337 * do not store explicit weights but rather 338 * create a difference of a certain strength from the preceding node. 339 * 340 * A root node is followed by either 341 * - a root/default node of the same strength, or 342 * - a root/default node of the next-weaker strength, or 343 * - a tailored node of the same strength. 344 * 345 * A node of a given strength normally implies "common" weights on weaker levels. 346 * 347 * A node with HAS_BEFORE2 must be immediately followed by 348 * a secondary node with an explicit below-common weight, then a secondary tailored node, 349 * and later an explicit common-secondary node. 350 * The below-common weight can be a root weight, 351 * or it can be BEFORE_WEIGHT16 for tailoring before an implied common weight 352 * or before the lowest root weight. 353 * (&[before 2] resets to an explicit secondary node so that 354 * the following addRelation(secondary) tailors right after that. 355 * If we did not have this node and instead were to reset on the primary node, 356 * then addRelation(secondary) would skip forward to the the COMMON_WEIGHT16 node.) 357 * 358 * If the flag is not set, then there are no explicit secondary nodes 359 * with the common or lower weights. 360 * 361 * Same for HAS_BEFORE3 for tertiary nodes and weights. 362 * A node must not have both flags set. 363 * 364 * Tailored CEs are initially represented in a CollationDataBuilder as temporary CEs 365 * which point to stable indexes in this list, 366 * and temporary CEs stored in a CollationDataBuilder only point to tailored nodes. 367 * 368 * A temporary CE in the ces[] array may point to a non-tailored reset-before-position node, 369 * until the next relation is added. 370 * 371 * At the end, the tailored weights are allocated as necessary, 372 * then the tailored nodes are replaced with final CEs, 373 * and the CollationData is rewritten by replacing temporary CEs with final ones. 374 * 375 * We cannot simply insert new nodes in the middle of the array 376 * because that would invalidate the indexes stored in existing temporary CEs. 377 * We need to use a linked graph with stable indexes to existing nodes. 378 * A doubly-linked list seems easiest to maintain. 379 * 380 * Each node is stored as an int64_t, with its fields stored as bit fields. 381 * 382 * Root primary node: 383 * - primary weight: 32 bits 63..32 384 * - reserved/unused/zero: 4 bits 31..28 385 * 386 * Weaker root nodes & tailored nodes: 387 * - a weight: 16 bits 63..48 388 * + a root or default weight for a non-tailored node 389 * + unused/zero for a tailored node 390 * - index to the previous node: 20 bits 47..28 391 * 392 * All types of nodes: 393 * - index to the next node: 20 bits 27..8 394 * + nextIndex=0 in last node per root-primary list 395 * - reserved/unused/zero bits: bits 7, 4, 2 396 * - HAS_BEFORE2: bit 6 397 * - HAS_BEFORE3: bit 5 398 * - IS_TAILORED: bit 3 399 * - the difference strength (primary/secondary/tertiary/quaternary): 2 bits 1..0 400 * 401 * We could allocate structs with pointers, but we would have to store them 402 * in a pointer list so that they can be indexed from temporary CEs, 403 * and they would require more memory allocations. 404 */ 405 UVector64 nodes; 406 }; 407 408 U_NAMESPACE_END 409 410 #endif // !UCONFIG_NO_COLLATION 411 #endif // __COLLATIONBUILDER_H__ 412