// © 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* collationdata.h8*9* created on: 2010oct2710* created by: Markus W. Scherer11*/1213#ifndef __COLLATIONDATA_H__14#define __COLLATIONDATA_H__1516#include "unicode/utypes.h"1718#if !UCONFIG_NO_COLLATION1920#include "unicode/ucol.h"21#include "unicode/uniset.h"22#include "collation.h"23#include "normalizer2impl.h"24#include "utrie2.h"2526struct UDataMemory;2728U_NAMESPACE_BEGIN2930class UVector32;3132/**33* Collation data container.34* Immutable data created by a CollationDataBuilder, or loaded from a file,35* or deserialized from API-provided binary data.36*37* Includes data for the collation base (root/default), aliased if this is not the base.38*/39struct U_I18N_API CollationData : public UMemory {40// Note: The ucadata.icu loader could discover the reserved ranges by setting an array41// parallel with the ranges, and resetting ranges that are indexed.42// The reordering builder code could clone the resulting template array.43static constexpr int32_t REORDER_RESERVED_BEFORE_LATIN = UCOL_REORDER_CODE_FIRST + 14;44static constexpr int32_t REORDER_RESERVED_AFTER_LATIN = REORDER_RESERVED_BEFORE_LATIN + 1;4546static constexpr int32_t MAX_NUM_SPECIAL_REORDER_CODES = 8;47/** C++ only, data reader check scriptStartsLength. */48static constexpr int32_t MAX_NUM_SCRIPT_RANGES = 256;4950CollationData(const Normalizer2Impl &nfc)51: trie(NULL),52ce32s(NULL), ces(NULL), contexts(NULL), base(NULL),53jamoCE32s(NULL),54nfcImpl(nfc),55numericPrimary(0x12000000),56ce32sLength(0), cesLength(0), contextsLength(0),57compressibleBytes(NULL),58unsafeBackwardSet(NULL),59fastLatinTable(NULL), fastLatinTableLength(0),60numScripts(0), scriptsIndex(NULL), scriptStarts(NULL), scriptStartsLength(0),61rootElements(NULL), rootElementsLength(0) {}6263uint32_t getCE32(UChar32 c) const {64return UTRIE2_GET32(trie, c);65}6667uint32_t getCE32FromSupplementary(UChar32 c) const {68return UTRIE2_GET32_FROM_SUPP(trie, c);69}7071UBool isDigit(UChar32 c) const {72return c < 0x660 ? c <= 0x39 && 0x30 <= c :73Collation::hasCE32Tag(getCE32(c), Collation::DIGIT_TAG);74}7576UBool isUnsafeBackward(UChar32 c, UBool numeric) const {77return unsafeBackwardSet->contains(c) || (numeric && isDigit(c));78}7980UBool isCompressibleLeadByte(uint32_t b) const {81return compressibleBytes[b];82}8384inline UBool isCompressiblePrimary(uint32_t p) const {85return isCompressibleLeadByte(p >> 24);86}8788/**89* Returns the CE32 from two contexts words.90* Access to the defaultCE32 for contraction and prefix matching.91*/92static uint32_t readCE32(const UChar *p) {93return ((uint32_t)p[0] << 16) | p[1];94}9596/**97* Returns the CE32 for an indirect special CE32 (e.g., with DIGIT_TAG).98* Requires that ce32 is special.99*/100uint32_t getIndirectCE32(uint32_t ce32) const;101/**102* Returns the CE32 for an indirect special CE32 (e.g., with DIGIT_TAG),103* if ce32 is special.104*/105uint32_t getFinalCE32(uint32_t ce32) const;106107/**108* Computes a CE from c's ce32 which has the OFFSET_TAG.109*/110int64_t getCEFromOffsetCE32(UChar32 c, uint32_t ce32) const {111int64_t dataCE = ces[Collation::indexFromCE32(ce32)];112return Collation::makeCE(Collation::getThreeBytePrimaryForOffsetData(c, dataCE));113}114115/**116* Returns the single CE that c maps to.117* Sets U_UNSUPPORTED_ERROR if c does not map to a single CE.118*/119int64_t getSingleCE(UChar32 c, UErrorCode &errorCode) const;120121/**122* Returns the FCD16 value for code point c. c must be >= 0.123*/124uint16_t getFCD16(UChar32 c) const {125return nfcImpl.getFCD16(c);126}127128/**129* Returns the first primary for the script's reordering group.130* @return the primary with only the first primary lead byte of the group131* (not necessarily an actual root collator primary weight),132* or 0 if the script is unknown133*/134uint32_t getFirstPrimaryForGroup(int32_t script) const;135136/**137* Returns the last primary for the script's reordering group.138* @return the last primary of the group139* (not an actual root collator primary weight),140* or 0 if the script is unknown141*/142uint32_t getLastPrimaryForGroup(int32_t script) const;143144/**145* Finds the reordering group which contains the primary weight.146* @return the first script of the group, or -1 if the weight is beyond the last group147*/148int32_t getGroupForPrimary(uint32_t p) const;149150int32_t getEquivalentScripts(int32_t script,151int32_t dest[], int32_t capacity, UErrorCode &errorCode) const;152153/**154* Writes the permutation of primary-weight ranges155* for the given reordering of scripts and groups.156* The caller checks for illegal arguments and157* takes care of [DEFAULT] and memory allocation.158*159* Each list element will be a (limit, offset) pair as described160* for the CollationSettings::reorderRanges.161* The list will be empty if no ranges are reordered.162*/163void makeReorderRanges(const int32_t *reorder, int32_t length,164UVector32 &ranges, UErrorCode &errorCode) const;165166/** @see jamoCE32s */167static const int32_t JAMO_CE32S_LENGTH = 19 + 21 + 27;168169/** Main lookup trie. */170const UTrie2 *trie;171/**172* Array of CE32 values.173* At index 0 there must be CE32(U+0000)174* to support U+0000's special-tag for NUL-termination handling.175*/176const uint32_t *ce32s;177/** Array of CE values for expansions and OFFSET_TAG. */178const int64_t *ces;179/** Array of prefix and contraction-suffix matching data. */180const UChar *contexts;181/** Base collation data, or NULL if this data itself is a base. */182const CollationData *base;183/**184* Simple array of JAMO_CE32S_LENGTH=19+21+27 CE32s, one per canonical Jamo L/V/T.185* They are normally simple CE32s, rarely expansions.186* For fast handling of HANGUL_TAG.187*/188const uint32_t *jamoCE32s;189const Normalizer2Impl &nfcImpl;190/** The single-byte primary weight (xx000000) for numeric collation. */191uint32_t numericPrimary;192193int32_t ce32sLength;194int32_t cesLength;195int32_t contextsLength;196197/** 256 flags for which primary-weight lead bytes are compressible. */198const UBool *compressibleBytes;199/**200* Set of code points that are unsafe for starting string comparison after an identical prefix,201* or in backwards CE iteration.202*/203const UnicodeSet *unsafeBackwardSet;204205/**206* Fast Latin table for common-Latin-text string comparisons.207* Data structure see class CollationFastLatin.208*/209const uint16_t *fastLatinTable;210int32_t fastLatinTableLength;211212/**213* Data for scripts and reordering groups.214* Uses include building a reordering permutation table and215* providing script boundaries to AlphabeticIndex.216*/217int32_t numScripts;218/**219* The length of scriptsIndex is numScripts+16.220* It maps from a UScriptCode or a special reorder code to an entry in scriptStarts.221* 16 special reorder codes (not all used) are mapped starting at numScripts.222* Up to MAX_NUM_SPECIAL_REORDER_CODES are codes for special groups like space/punct/digit.223* There are special codes at the end for reorder-reserved primary ranges.224*225* Multiple scripts may share a range and index, for example Hira & Kana.226*/227const uint16_t *scriptsIndex;228/**229* Start primary weight (top 16 bits only) for a group/script/reserved range230* indexed by scriptsIndex.231* The first range (separators & terminators) and the last range (trailing weights)232* are not reorderable, and no scriptsIndex entry points to them.233*/234const uint16_t *scriptStarts;235int32_t scriptStartsLength;236237/**238* Collation elements in the root collator.239* Used by the CollationRootElements class. The data structure is described there.240* NULL in a tailoring.241*/242const uint32_t *rootElements;243int32_t rootElementsLength;244245private:246int32_t getScriptIndex(int32_t script) const;247void makeReorderRanges(const int32_t *reorder, int32_t length,248UBool latinMustMove,249UVector32 &ranges, UErrorCode &errorCode) const;250int32_t addLowScriptRange(uint8_t table[], int32_t index, int32_t lowStart) const;251int32_t addHighScriptRange(uint8_t table[], int32_t index, int32_t highLimit) const;252};253254U_NAMESPACE_END255256#endif // !UCONFIG_NO_COLLATION257#endif // __COLLATIONDATA_H__258259260