// © 2016 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html2/*3*******************************************************************************4* Copyright (C) 2010-2015, International Business Machines5* Corporation and others. All Rights Reserved.6*******************************************************************************7* collation.h8*9* created on: 2010oct2710* created by: Markus W. Scherer11*/1213#ifndef __COLLATION_H__14#define __COLLATION_H__1516#include "unicode/utypes.h"1718#if !UCONFIG_NO_COLLATION1920U_NAMESPACE_BEGIN2122/**23* Collation v2 basic definitions and static helper functions.24*25* Data structures except for expansion tables store 32-bit CEs which are26* either specials (see tags below) or are compact forms of 64-bit CEs.27*/28class U_I18N_API Collation {29public:30// Special sort key bytes for all levels.31static const uint8_t TERMINATOR_BYTE = 0;32static const uint8_t LEVEL_SEPARATOR_BYTE = 1;3334/** The secondary/tertiary lower limit for tailoring before any root elements. */35static const uint32_t BEFORE_WEIGHT16 = 0x0100;3637/**38* Merge-sort-key separator.39* Same as the unique primary and identical-level weights of U+FFFE.40* Must not be used as primary compression low terminator.41* Otherwise usable.42*/43static const uint8_t MERGE_SEPARATOR_BYTE = 2;44static const uint32_t MERGE_SEPARATOR_PRIMARY = 0x02000000; // U+FFFE45static const uint32_t MERGE_SEPARATOR_CE32 = 0x02000505; // U+FFFE4647/**48* Primary compression low terminator, must be greater than MERGE_SEPARATOR_BYTE.49* Reserved value in primary second byte if the lead byte is compressible.50* Otherwise usable in all CE weight bytes.51*/52static const uint8_t PRIMARY_COMPRESSION_LOW_BYTE = 3;53/**54* Primary compression high terminator.55* Reserved value in primary second byte if the lead byte is compressible.56* Otherwise usable in all CE weight bytes.57*/58static const uint8_t PRIMARY_COMPRESSION_HIGH_BYTE = 0xff;5960/** Default secondary/tertiary weight lead byte. */61static const uint8_t COMMON_BYTE = 5;62static const uint32_t COMMON_WEIGHT16 = 0x0500;63/** Middle 16 bits of a CE with a common secondary weight. */64static const uint32_t COMMON_SECONDARY_CE = 0x05000000;65/** Lower 16 bits of a CE with a common tertiary weight. */66static const uint32_t COMMON_TERTIARY_CE = 0x0500;67/** Lower 32 bits of a CE with common secondary and tertiary weights. */68static const uint32_t COMMON_SEC_AND_TER_CE = 0x05000500;6970static const uint32_t SECONDARY_MASK = 0xffff0000;71static const uint32_t CASE_MASK = 0xc000;72static const uint32_t SECONDARY_AND_CASE_MASK = SECONDARY_MASK | CASE_MASK;73/** Only the 2*6 bits for the pure tertiary weight. */74static const uint32_t ONLY_TERTIARY_MASK = 0x3f3f;75/** Only the secondary & tertiary bits; no case, no quaternary. */76static const uint32_t ONLY_SEC_TER_MASK = SECONDARY_MASK | ONLY_TERTIARY_MASK;77/** Case bits and tertiary bits. */78static const uint32_t CASE_AND_TERTIARY_MASK = CASE_MASK | ONLY_TERTIARY_MASK;79static const uint32_t QUATERNARY_MASK = 0xc0;80/** Case bits and quaternary bits. */81static const uint32_t CASE_AND_QUATERNARY_MASK = CASE_MASK | QUATERNARY_MASK;8283static const uint8_t UNASSIGNED_IMPLICIT_BYTE = 0xfe; // compressible84/**85* First unassigned: AlphabeticIndex overflow boundary.86* We want a 3-byte primary so that it fits into the root elements table.87*88* This 3-byte primary will not collide with89* any unassigned-implicit 4-byte primaries because90* the first few hundred Unicode code points all have real mappings.91*/92static const uint32_t FIRST_UNASSIGNED_PRIMARY = 0xfe040200;9394static const uint8_t TRAIL_WEIGHT_BYTE = 0xff; // not compressible95static const uint32_t FIRST_TRAILING_PRIMARY = 0xff020200; // [first trailing]96static const uint32_t MAX_PRIMARY = 0xffff0000; // U+FFFF97static const uint32_t MAX_REGULAR_CE32 = 0xffff0505; // U+FFFF9899// CE32 value for U+FFFD as well as illegal UTF-8 byte sequences (which behave like U+FFFD).100// We use the third-highest primary weight for U+FFFD (as in UCA 6.3+).101static const uint32_t FFFD_PRIMARY = MAX_PRIMARY - 0x20000;102static const uint32_t FFFD_CE32 = MAX_REGULAR_CE32 - 0x20000;103104/**105* A CE32 is special if its low byte is this or greater.106* Impossible case bits 11 mark special CE32s.107* This value itself is used to indicate a fallback to the base collator.108*/109static const uint8_t SPECIAL_CE32_LOW_BYTE = 0xc0;110static const uint32_t FALLBACK_CE32 = SPECIAL_CE32_LOW_BYTE;111/**112* Low byte of a long-primary special CE32.113*/114static const uint8_t LONG_PRIMARY_CE32_LOW_BYTE = 0xc1; // SPECIAL_CE32_LOW_BYTE | LONG_PRIMARY_TAG115116static const uint32_t UNASSIGNED_CE32 = 0xffffffff; // Compute an unassigned-implicit CE.117118static const uint32_t NO_CE32 = 1;119120/** No CE: End of input. Only used in runtime code, not stored in data. */121static const uint32_t NO_CE_PRIMARY = 1; // not a left-adjusted weight122static const uint32_t NO_CE_WEIGHT16 = 0x0100; // weight of LEVEL_SEPARATOR_BYTE123static const int64_t NO_CE = INT64_C(0x101000100); // NO_CE_PRIMARY, NO_CE_WEIGHT16, NO_CE_WEIGHT16124125/** Sort key levels. */126enum Level {127/** Unspecified level. */128NO_LEVEL,129PRIMARY_LEVEL,130SECONDARY_LEVEL,131CASE_LEVEL,132TERTIARY_LEVEL,133QUATERNARY_LEVEL,134IDENTICAL_LEVEL,135/** Beyond sort key bytes. */136ZERO_LEVEL137};138139/**140* Sort key level flags: xx_FLAG = 1 << xx_LEVEL.141* In Java, use enum Level with flag() getters, or use EnumSet rather than hand-made bit sets.142*/143static const uint32_t NO_LEVEL_FLAG = 1;144static const uint32_t PRIMARY_LEVEL_FLAG = 2;145static const uint32_t SECONDARY_LEVEL_FLAG = 4;146static const uint32_t CASE_LEVEL_FLAG = 8;147static const uint32_t TERTIARY_LEVEL_FLAG = 0x10;148static const uint32_t QUATERNARY_LEVEL_FLAG = 0x20;149static const uint32_t IDENTICAL_LEVEL_FLAG = 0x40;150static const uint32_t ZERO_LEVEL_FLAG = 0x80;151152/**153* Special-CE32 tags, from bits 3..0 of a special 32-bit CE.154* Bits 31..8 are available for tag-specific data.155* Bits 5..4: Reserved. May be used in the future to indicate lccc!=0 and tccc!=0.156*/157enum {158/**159* Fall back to the base collator.160* This is the tag value in SPECIAL_CE32_LOW_BYTE and FALLBACK_CE32.161* Bits 31..8: Unused, 0.162*/163FALLBACK_TAG = 0,164/**165* Long-primary CE with COMMON_SEC_AND_TER_CE.166* Bits 31..8: Three-byte primary.167*/168LONG_PRIMARY_TAG = 1,169/**170* Long-secondary CE with zero primary.171* Bits 31..16: Secondary weight.172* Bits 15.. 8: Tertiary weight.173*/174LONG_SECONDARY_TAG = 2,175/**176* Unused.177* May be used in the future for single-byte secondary CEs (SHORT_SECONDARY_TAG),178* storing the secondary in bits 31..24, the ccc in bits 23..16,179* and the tertiary in bits 15..8.180*/181RESERVED_TAG_3 = 3,182/**183* Latin mini expansions of two simple CEs [pp, 05, tt] [00, ss, 05].184* Bits 31..24: Single-byte primary weight pp of the first CE.185* Bits 23..16: Tertiary weight tt of the first CE.186* Bits 15.. 8: Secondary weight ss of the second CE.187*/188LATIN_EXPANSION_TAG = 4,189/**190* Points to one or more simple/long-primary/long-secondary 32-bit CE32s.191* Bits 31..13: Index into uint32_t table.192* Bits 12.. 8: Length=1..31.193*/194EXPANSION32_TAG = 5,195/**196* Points to one or more 64-bit CEs.197* Bits 31..13: Index into CE table.198* Bits 12.. 8: Length=1..31.199*/200EXPANSION_TAG = 6,201/**202* Builder data, used only in the CollationDataBuilder, not in runtime data.203*204* If bit 8 is 0: Builder context, points to a list of context-sensitive mappings.205* Bits 31..13: Index to the builder's list of ConditionalCE32 for this character.206* Bits 12.. 9: Unused, 0.207*208* If bit 8 is 1 (IS_BUILDER_JAMO_CE32): Builder-only jamoCE32 value.209* The builder fetches the Jamo CE32 from the trie.210* Bits 31..13: Jamo code point.211* Bits 12.. 9: Unused, 0.212*/213BUILDER_DATA_TAG = 7,214/**215* Points to prefix trie.216* Bits 31..13: Index into prefix/contraction data.217* Bits 12.. 8: Unused, 0.218*/219PREFIX_TAG = 8,220/**221* Points to contraction data.222* Bits 31..13: Index into prefix/contraction data.223* Bit 12: Unused, 0.224* Bit 11: CONTRACT_HAS_STARTER flag. (Used by ICU4X only.)225* Bit 10: CONTRACT_TRAILING_CCC flag.226* Bit 9: CONTRACT_NEXT_CCC flag.227* Bit 8: CONTRACT_SINGLE_CP_NO_MATCH flag.228*/229CONTRACTION_TAG = 9,230/**231* Decimal digit.232* Bits 31..13: Index into uint32_t table for non-numeric-collation CE32.233* Bit 12: Unused, 0.234* Bits 11.. 8: Digit value 0..9.235*/236DIGIT_TAG = 10,237/**238* Tag for U+0000, for moving the NUL-termination handling239* from the regular fastpath into specials-handling code.240* Bits 31..8: Unused, 0.241*/242U0000_TAG = 11,243/**244* Tag for a Hangul syllable.245* Bits 31..9: Unused, 0.246* Bit 8: HANGUL_NO_SPECIAL_JAMO flag.247*/248HANGUL_TAG = 12,249/**250* Tag for a lead surrogate code unit.251* Optional optimization for UTF-16 string processing.252* Bits 31..10: Unused, 0.253* 9.. 8: =0: All associated supplementary code points are unassigned-implicit.254* =1: All associated supplementary code points fall back to the base data.255* else: (Normally 2) Look up the data for the supplementary code point.256*/257LEAD_SURROGATE_TAG = 13,258/**259* Tag for CEs with primary weights in code point order.260* Bits 31..13: Index into CE table, for one data "CE".261* Bits 12.. 8: Unused, 0.262*263* This data "CE" has the following bit fields:264* Bits 63..32: Three-byte primary pppppp00.265* 31.. 8: Start/base code point of the in-order range.266* 7: Flag isCompressible primary.267* 6.. 0: Per-code point primary-weight increment.268*/269OFFSET_TAG = 14,270/**271* Implicit CE tag. Compute an unassigned-implicit CE.272* All bits are set (UNASSIGNED_CE32=0xffffffff).273*/274IMPLICIT_TAG = 15275};276277static UBool isAssignedCE32(uint32_t ce32) {278return ce32 != FALLBACK_CE32 && ce32 != UNASSIGNED_CE32;279}280281/**282* We limit the number of CEs in an expansion283* so that we can use a small number of length bits in the data structure,284* and so that an implementation can copy CEs at runtime without growing a destination buffer.285*/286static const int32_t MAX_EXPANSION_LENGTH = 31;287static const int32_t MAX_INDEX = 0x7ffff;288289/**290* Set if there is no match for the single (no-suffix) character itself.291* This is only possible if there is a prefix.292* In this case, discontiguous contraction matching cannot add combining marks293* starting from an empty suffix.294* The default CE32 is used anyway if there is no suffix match.295*/296static const uint32_t CONTRACT_SINGLE_CP_NO_MATCH = 0x100;297/** Set if the first character of every contraction suffix has lccc!=0. */298static const uint32_t CONTRACT_NEXT_CCC = 0x200;299/** Set if any contraction suffix ends with lccc!=0. */300static const uint32_t CONTRACT_TRAILING_CCC = 0x400;301/** Set if any contraction suffix contains a starter. (Used by ICU4X only.) */302static const uint32_t CONTRACT_HAS_STARTER = 0x800;303304/** For HANGUL_TAG: None of its Jamo CE32s isSpecialCE32(). */305static const uint32_t HANGUL_NO_SPECIAL_JAMO = 0x100;306307static const uint32_t LEAD_ALL_UNASSIGNED = 0;308static const uint32_t LEAD_ALL_FALLBACK = 0x100;309static const uint32_t LEAD_MIXED = 0x200;310static const uint32_t LEAD_TYPE_MASK = 0x300;311312static uint32_t makeLongPrimaryCE32(uint32_t p) { return p | LONG_PRIMARY_CE32_LOW_BYTE; }313314/** Turns the long-primary CE32 into a primary weight pppppp00. */315static inline uint32_t primaryFromLongPrimaryCE32(uint32_t ce32) {316return ce32 & 0xffffff00;317}318static inline int64_t ceFromLongPrimaryCE32(uint32_t ce32) {319return ((int64_t)(ce32 & 0xffffff00) << 32) | COMMON_SEC_AND_TER_CE;320}321322static uint32_t makeLongSecondaryCE32(uint32_t lower32) {323return lower32 | SPECIAL_CE32_LOW_BYTE | LONG_SECONDARY_TAG;324}325static inline int64_t ceFromLongSecondaryCE32(uint32_t ce32) {326return ce32 & 0xffffff00;327}328329/** Makes a special CE32 with tag, index and length. */330static uint32_t makeCE32FromTagIndexAndLength(int32_t tag, int32_t index, int32_t length) {331return (index << 13) | (length << 8) | SPECIAL_CE32_LOW_BYTE | tag;332}333/** Makes a special CE32 with only tag and index. */334static uint32_t makeCE32FromTagAndIndex(int32_t tag, int32_t index) {335return (index << 13) | SPECIAL_CE32_LOW_BYTE | tag;336}337338static inline UBool isSpecialCE32(uint32_t ce32) {339return (ce32 & 0xff) >= SPECIAL_CE32_LOW_BYTE;340}341342static inline int32_t tagFromCE32(uint32_t ce32) {343return (int32_t)(ce32 & 0xf);344}345346static inline UBool hasCE32Tag(uint32_t ce32, int32_t tag) {347return isSpecialCE32(ce32) && tagFromCE32(ce32) == tag;348}349350static inline UBool isLongPrimaryCE32(uint32_t ce32) {351return hasCE32Tag(ce32, LONG_PRIMARY_TAG);352}353354static UBool isSimpleOrLongCE32(uint32_t ce32) {355return !isSpecialCE32(ce32) ||356tagFromCE32(ce32) == LONG_PRIMARY_TAG ||357tagFromCE32(ce32) == LONG_SECONDARY_TAG;358}359360/**361* @return true if the ce32 yields one or more CEs without further data lookups362*/363static UBool isSelfContainedCE32(uint32_t ce32) {364return !isSpecialCE32(ce32) ||365tagFromCE32(ce32) == LONG_PRIMARY_TAG ||366tagFromCE32(ce32) == LONG_SECONDARY_TAG ||367tagFromCE32(ce32) == LATIN_EXPANSION_TAG;368}369370static inline UBool isPrefixCE32(uint32_t ce32) {371return hasCE32Tag(ce32, PREFIX_TAG);372}373374static inline UBool isContractionCE32(uint32_t ce32) {375return hasCE32Tag(ce32, CONTRACTION_TAG);376}377378static inline UBool ce32HasContext(uint32_t ce32) {379return isSpecialCE32(ce32) &&380(tagFromCE32(ce32) == PREFIX_TAG ||381tagFromCE32(ce32) == CONTRACTION_TAG);382}383384/**385* Get the first of the two Latin-expansion CEs encoded in ce32.386* @see LATIN_EXPANSION_TAG387*/388static inline int64_t latinCE0FromCE32(uint32_t ce32) {389return ((int64_t)(ce32 & 0xff000000) << 32) | COMMON_SECONDARY_CE | ((ce32 & 0xff0000) >> 8);390}391392/**393* Get the second of the two Latin-expansion CEs encoded in ce32.394* @see LATIN_EXPANSION_TAG395*/396static inline int64_t latinCE1FromCE32(uint32_t ce32) {397return ((ce32 & 0xff00) << 16) | COMMON_TERTIARY_CE;398}399400/**401* Returns the data index from a special CE32.402*/403static inline int32_t indexFromCE32(uint32_t ce32) {404return (int32_t)(ce32 >> 13);405}406407/**408* Returns the data length from a ce32.409*/410static inline int32_t lengthFromCE32(uint32_t ce32) {411return (ce32 >> 8) & 31;412}413414/**415* Returns the digit value from a DIGIT_TAG ce32.416*/417static inline char digitFromCE32(uint32_t ce32) {418return (char)((ce32 >> 8) & 0xf);419}420421/** Returns a 64-bit CE from a simple CE32 (not special). */422static inline int64_t ceFromSimpleCE32(uint32_t ce32) {423// normal form ppppsstt -> pppp0000ss00tt00424// assert (ce32 & 0xff) < SPECIAL_CE32_LOW_BYTE425return ((int64_t)(ce32 & 0xffff0000) << 32) | ((ce32 & 0xff00) << 16) | ((ce32 & 0xff) << 8);426}427428/** Returns a 64-bit CE from a simple/long-primary/long-secondary CE32. */429static inline int64_t ceFromCE32(uint32_t ce32) {430uint32_t tertiary = ce32 & 0xff;431if(tertiary < SPECIAL_CE32_LOW_BYTE) {432// normal form ppppsstt -> pppp0000ss00tt00433return ((int64_t)(ce32 & 0xffff0000) << 32) | ((ce32 & 0xff00) << 16) | (tertiary << 8);434} else {435ce32 -= tertiary;436if((tertiary & 0xf) == LONG_PRIMARY_TAG) {437// long-primary form ppppppC1 -> pppppp00050000500438return ((int64_t)ce32 << 32) | COMMON_SEC_AND_TER_CE;439} else {440// long-secondary form ssssttC2 -> 00000000sssstt00441// assert (tertiary & 0xf) == LONG_SECONDARY_TAG442return ce32;443}444}445}446447/** Creates a CE from a primary weight. */448static inline int64_t makeCE(uint32_t p) {449return ((int64_t)p << 32) | COMMON_SEC_AND_TER_CE;450}451/**452* Creates a CE from a primary weight,453* 16-bit secondary/tertiary weights, and a 2-bit quaternary.454*/455static inline int64_t makeCE(uint32_t p, uint32_t s, uint32_t t, uint32_t q) {456return ((int64_t)p << 32) | (s << 16) | t | (q << 6);457}458459/**460* Increments a 2-byte primary by a code point offset.461*/462static uint32_t incTwoBytePrimaryByOffset(uint32_t basePrimary, UBool isCompressible,463int32_t offset);464465/**466* Increments a 3-byte primary by a code point offset.467*/468static uint32_t incThreeBytePrimaryByOffset(uint32_t basePrimary, UBool isCompressible,469int32_t offset);470471/**472* Decrements a 2-byte primary by one range step (1..0x7f).473*/474static uint32_t decTwoBytePrimaryByOneStep(uint32_t basePrimary, UBool isCompressible, int32_t step);475476/**477* Decrements a 3-byte primary by one range step (1..0x7f).478*/479static uint32_t decThreeBytePrimaryByOneStep(uint32_t basePrimary, UBool isCompressible, int32_t step);480481/**482* Computes a 3-byte primary for c's OFFSET_TAG data "CE".483*/484static uint32_t getThreeBytePrimaryForOffsetData(UChar32 c, int64_t dataCE);485486/**487* Returns the unassigned-character implicit primary weight for any valid code point c.488*/489static uint32_t unassignedPrimaryFromCodePoint(UChar32 c);490491static inline int64_t unassignedCEFromCodePoint(UChar32 c) {492return makeCE(unassignedPrimaryFromCodePoint(c));493}494495private:496Collation() = delete; // No instantiation.497};498499U_NAMESPACE_END500501#endif // !UCONFIG_NO_COLLATION502#endif // __COLLATION_H__503504505