// © 2016 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html2/*3*******************************************************************************4* Copyright (C) 2013-2015, International Business Machines5* Corporation and others. All Rights Reserved.6*******************************************************************************7* collationfastlatin.h8*9* created on: 2013aug0910* created by: Markus W. Scherer11*/1213#ifndef __COLLATIONFASTLATIN_H__14#define __COLLATIONFASTLATIN_H__1516#include "unicode/utypes.h"1718#if !UCONFIG_NO_COLLATION1920U_NAMESPACE_BEGIN2122struct CollationData;23struct CollationSettings;2425class U_I18N_API CollationFastLatin /* all static */ {26public:27/**28* Fast Latin format version (one byte 1..FF).29* Must be incremented for any runtime-incompatible changes,30* in particular, for changes to any of the following constants.31*32* When the major version number of the main data format changes,33* we can reset this fast Latin version to 1.34*/35static const uint16_t VERSION = 2;3637static const int32_t LATIN_MAX = 0x17f;38static const int32_t LATIN_LIMIT = LATIN_MAX + 1;3940static const int32_t LATIN_MAX_UTF8_LEAD = 0xc5; // UTF-8 lead byte of LATIN_MAX4142static const int32_t PUNCT_START = 0x2000;43static const int32_t PUNCT_LIMIT = 0x2040;4445// excludes U+FFFE & U+FFFF46static const int32_t NUM_FAST_CHARS = LATIN_LIMIT + (PUNCT_LIMIT - PUNCT_START);4748// Note on the supported weight ranges:49// Analysis of UCA 6.3 and CLDR 23 non-search tailorings shows that50// the CEs for characters in the above ranges, excluding expansions with length >2,51// excluding contractions of >2 characters, and other restrictions52// (see the builder's getCEsFromCE32()),53// use at most about 150 primary weights,54// where about 94 primary weights are possibly-variable (space/punct/symbol/currency),55// at most 4 secondary before-common weights,56// at most 4 secondary after-common weights,57// at most 16 secondary high weights (in secondary CEs), and58// at most 4 tertiary after-common weights.59// The following ranges are designed to support slightly more weights than that.60// (en_US_POSIX is unusual: It creates about 64 variable + 116 Latin primaries.)6162// Digits may use long primaries (preserving more short ones)63// or short primaries (faster) without changing this data structure.64// (If we supported numeric collation, then digits would have to have long primaries65// so that special handling does not affect the fast path.)6667static const uint32_t SHORT_PRIMARY_MASK = 0xfc00; // bits 15..1068static const uint32_t INDEX_MASK = 0x3ff; // bits 9..0 for expansions & contractions69static const uint32_t SECONDARY_MASK = 0x3e0; // bits 9..570static const uint32_t CASE_MASK = 0x18; // bits 4..371static const uint32_t LONG_PRIMARY_MASK = 0xfff8; // bits 15..372static const uint32_t TERTIARY_MASK = 7; // bits 2..073static const uint32_t CASE_AND_TERTIARY_MASK = CASE_MASK | TERTIARY_MASK;7475static const uint32_t TWO_SHORT_PRIMARIES_MASK =76(SHORT_PRIMARY_MASK << 16) | SHORT_PRIMARY_MASK; // 0xfc00fc0077static const uint32_t TWO_LONG_PRIMARIES_MASK =78(LONG_PRIMARY_MASK << 16) | LONG_PRIMARY_MASK; // 0xfff8fff879static const uint32_t TWO_SECONDARIES_MASK =80(SECONDARY_MASK << 16) | SECONDARY_MASK; // 0x3e003e081static const uint32_t TWO_CASES_MASK =82(CASE_MASK << 16) | CASE_MASK; // 0x18001883static const uint32_t TWO_TERTIARIES_MASK =84(TERTIARY_MASK << 16) | TERTIARY_MASK; // 0x700078586/**87* Contraction with one fast Latin character.88* Use INDEX_MASK to find the start of the contraction list after the fixed table.89* The first entry contains the default mapping.90* Otherwise use CONTR_CHAR_MASK for the contraction character index91* (in ascending order).92* Use CONTR_LENGTH_SHIFT for the length of the entry93* (1=BAIL_OUT, 2=one CE, 3=two CEs).94*95* Also, U+0000 maps to a contraction entry, so that the fast path need not96* check for NUL termination.97* It usually maps to a contraction list with only the completely ignorable default value.98*/99static const uint32_t CONTRACTION = 0x400;100/**101* An expansion encodes two CEs.102* Use INDEX_MASK to find the pair of CEs after the fixed table.103*104* The higher a mini CE value, the easier it is to process.105* For expansions and higher, no context needs to be considered.106*/107static const uint32_t EXPANSION = 0x800;108/**109* Encodes one CE with a long/low mini primary (there are 128).110* All potentially-variable primaries must be in this range,111* to make the short-primary path as fast as possible.112*/113static const uint32_t MIN_LONG = 0xc00;114static const uint32_t LONG_INC = 8;115static const uint32_t MAX_LONG = 0xff8;116/**117* Encodes one CE with a short/high primary (there are 60),118* plus a secondary CE if the secondary weight is high.119* Fast handling: At least all letter primaries should be in this range.120*/121static const uint32_t MIN_SHORT = 0x1000;122static const uint32_t SHORT_INC = 0x400;123/** The highest primary weight is reserved for U+FFFF. */124static const uint32_t MAX_SHORT = SHORT_PRIMARY_MASK;125126static const uint32_t MIN_SEC_BEFORE = 0; // must add SEC_OFFSET127static const uint32_t SEC_INC = 0x20;128static const uint32_t MAX_SEC_BEFORE = MIN_SEC_BEFORE + 4 * SEC_INC; // 5 before common129static const uint32_t COMMON_SEC = MAX_SEC_BEFORE + SEC_INC;130static const uint32_t MIN_SEC_AFTER = COMMON_SEC + SEC_INC;131static const uint32_t MAX_SEC_AFTER = MIN_SEC_AFTER + 5 * SEC_INC; // 6 after common132static const uint32_t MIN_SEC_HIGH = MAX_SEC_AFTER + SEC_INC; // 20 high secondaries133static const uint32_t MAX_SEC_HIGH = SECONDARY_MASK;134135/**136* Lookup: Add this offset to secondary weights, except for completely ignorable CEs.137* Must be greater than any special value, e.g., MERGE_WEIGHT.138* The exact value is not relevant for the format version.139*/140static const uint32_t SEC_OFFSET = SEC_INC;141static const uint32_t COMMON_SEC_PLUS_OFFSET = COMMON_SEC + SEC_OFFSET;142143static const uint32_t TWO_SEC_OFFSETS =144(SEC_OFFSET << 16) | SEC_OFFSET; // 0x200020145static const uint32_t TWO_COMMON_SEC_PLUS_OFFSET =146(COMMON_SEC_PLUS_OFFSET << 16) | COMMON_SEC_PLUS_OFFSET;147148static const uint32_t LOWER_CASE = 8; // case bits include this offset149static const uint32_t TWO_LOWER_CASES = (LOWER_CASE << 16) | LOWER_CASE; // 0x80008150151static const uint32_t COMMON_TER = 0; // must add TER_OFFSET152static const uint32_t MAX_TER_AFTER = 7; // 7 after common153154/**155* Lookup: Add this offset to tertiary weights, except for completely ignorable CEs.156* Must be greater than any special value, e.g., MERGE_WEIGHT.157* Must be greater than case bits as well, so that with combined case+tertiary weights158* plus the offset the tertiary bits does not spill over into the case bits.159* The exact value is not relevant for the format version.160*/161static const uint32_t TER_OFFSET = SEC_OFFSET;162static const uint32_t COMMON_TER_PLUS_OFFSET = COMMON_TER + TER_OFFSET;163164static const uint32_t TWO_TER_OFFSETS = (TER_OFFSET << 16) | TER_OFFSET;165static const uint32_t TWO_COMMON_TER_PLUS_OFFSET =166(COMMON_TER_PLUS_OFFSET << 16) | COMMON_TER_PLUS_OFFSET;167168static const uint32_t MERGE_WEIGHT = 3;169static const uint32_t EOS = 2; // end of string170static const uint32_t BAIL_OUT = 1;171172/**173* Contraction result first word bits 8..0 contain the174* second contraction character, as a char index 0..NUM_FAST_CHARS-1.175* Each contraction list is terminated with a word containing CONTR_CHAR_MASK.176*/177static const uint32_t CONTR_CHAR_MASK = 0x1ff;178/**179* Contraction result first word bits 10..9 contain the result length:180* 1=bail out, 2=one mini CE, 3=two mini CEs181*/182static const uint32_t CONTR_LENGTH_SHIFT = 9;183184/**185* Comparison return value when the regular comparison must be used.186* The exact value is not relevant for the format version.187*/188static const int32_t BAIL_OUT_RESULT = -2;189190static inline int32_t getCharIndex(UChar c) {191if(c <= LATIN_MAX) {192return c;193} else if(PUNCT_START <= c && c < PUNCT_LIMIT) {194return c - (PUNCT_START - LATIN_LIMIT);195} else {196// Not a fast Latin character.197// Note: U+FFFE & U+FFFF are forbidden in tailorings198// and thus do not occur in any contractions.199return -1;200}201}202203/**204* Computes the options value for the compare functions205* and writes the precomputed primary weights.206* Returns -1 if the Latin fastpath is not supported for the data and settings.207* The capacity must be LATIN_LIMIT.208*/209static int32_t getOptions(const CollationData *data, const CollationSettings &settings,210uint16_t *primaries, int32_t capacity);211212static int32_t compareUTF16(const uint16_t *table, const uint16_t *primaries, int32_t options,213const UChar *left, int32_t leftLength,214const UChar *right, int32_t rightLength);215216static int32_t compareUTF8(const uint16_t *table, const uint16_t *primaries, int32_t options,217const uint8_t *left, int32_t leftLength,218const uint8_t *right, int32_t rightLength);219220private:221static uint32_t lookup(const uint16_t *table, UChar32 c);222static uint32_t lookupUTF8(const uint16_t *table, UChar32 c,223const uint8_t *s8, int32_t &sIndex, int32_t sLength);224static uint32_t lookupUTF8Unsafe(const uint16_t *table, UChar32 c,225const uint8_t *s8, int32_t &sIndex);226227static uint32_t nextPair(const uint16_t *table, UChar32 c, uint32_t ce,228const UChar *s16, const uint8_t *s8, int32_t &sIndex, int32_t &sLength);229230static inline uint32_t getPrimaries(uint32_t variableTop, uint32_t pair) {231uint32_t ce = pair & 0xffff;232if(ce >= MIN_SHORT) { return pair & TWO_SHORT_PRIMARIES_MASK; }233if(ce > variableTop) { return pair & TWO_LONG_PRIMARIES_MASK; }234if(ce >= MIN_LONG) { return 0; } // variable235return pair; // special mini CE236}237static inline uint32_t getSecondariesFromOneShortCE(uint32_t ce) {238ce &= SECONDARY_MASK;239if(ce < MIN_SEC_HIGH) {240return ce + SEC_OFFSET;241} else {242return ((ce + SEC_OFFSET) << 16) | COMMON_SEC_PLUS_OFFSET;243}244}245static uint32_t getSecondaries(uint32_t variableTop, uint32_t pair);246static uint32_t getCases(uint32_t variableTop, UBool strengthIsPrimary, uint32_t pair);247static uint32_t getTertiaries(uint32_t variableTop, UBool withCaseBits, uint32_t pair);248static uint32_t getQuaternaries(uint32_t variableTop, uint32_t pair);249250private:251CollationFastLatin() = delete; // no constructor252};253254/*255* Format of the CollationFastLatin data table.256* CollationFastLatin::VERSION = 2.257*258* This table contains data for a Latin-text collation fastpath.259* The data is stored as an array of uint16_t which contains the following parts.260*261* uint16_t -- version & header length262* Bits 15..8: version, must match the VERSION263* 7..0: length of the header264*265* uint16_t varTops[header length - 1]266* Version 2:267* varTops[m] is the highest CollationFastLatin long-primary weight268* of supported maxVariable group m269* (special reorder group space, punct, symbol, currency).270*271* Version 1:272* Each of these values maps the variable top lead byte of a supported maxVariable group273* to the highest CollationFastLatin long-primary weight.274* The values are stored in ascending order.275* Bits 15..7: max fast-Latin long-primary weight (bits 11..3 shifted left by 4 bits)276* 6..0: regular primary lead byte277*278* uint16_t miniCEs[0x1c0]279* A mini collation element for each character U+0000..U+017F and U+2000..U+203F.280* Each value encodes one or two mini CEs (two are possible if the first one281* has a short mini primary and the second one is a secondary CE, i.e., primary == 0),282* or points to an expansion or to a contraction table.283* U+0000 always has a contraction entry,284* so that NUL-termination need not be tested in the fastpath.285* If the collation elements for a character or contraction cannot be encoded in this format,286* then the BAIL_OUT value is stored.287* For details see the comments for the class constants.288*289* uint16_t expansions[variable length];290* Expansion mini CEs contain an offset relative to just after the miniCEs table.291* An expansions contains exactly 2 mini CEs.292*293* uint16_t contractions[variable length];294* Contraction mini CEs contain an offset relative to just after the miniCEs table.295* It points to a list of tuples which map from a contraction suffix character to a result.296* First uint16_t of each tuple:297* Bits 10..9: Length of the result (1..3), see comments on CONTR_LENGTH_SHIFT.298* Bits 8..0: Contraction character, see comments on CONTR_CHAR_MASK.299* This is followed by 0, 1, or 2 uint16_t according to the length.300* Each list is terminated by an entry with CONTR_CHAR_MASK.301* Each list starts with such an entry which also contains the default result302* for when there is no contraction match.303*304* -----------------305* Changes for version 2 (ICU 55)306*307* Special reorder groups do not necessarily start on whole primary lead bytes any more.308* Therefore, the varTops data has a new format:309* Version 1 stored the lead bytes of the highest root primaries for310* the maxVariable-supported special reorder groups.311* Now the top 16 bits would need to be stored,312* and it is simpler to store only the fast-Latin weights.313*/314315U_NAMESPACE_END316317#endif // !UCONFIG_NO_COLLATION318#endif // __COLLATIONFASTLATIN_H__319320321