ICU 49.1.1  49.1.1
tblcoll.h
Go to the documentation of this file.
00001 /*
00002 ******************************************************************************
00003 * Copyright (C) 1996-2012, International Business Machines Corporation and
00004 * others. All Rights Reserved.
00005 ******************************************************************************
00006 */
00007 
00059 #ifndef TBLCOLL_H
00060 #define TBLCOLL_H
00061 
00062 #include "unicode/utypes.h"
00063 
00064  
00065 #if !UCONFIG_NO_COLLATION
00066 
00067 #include "unicode/coll.h"
00068 #include "unicode/ucol.h"
00069 #include "unicode/sortkey.h"
00070 #include "unicode/normlzr.h"
00071 
00072 U_NAMESPACE_BEGIN
00073 
00077 class StringSearch;
00081 class CollationElementIterator;
00082 
00111 class U_I18N_API RuleBasedCollator : public Collator
00112 {
00113 public:
00114 
00115   // constructor -------------------------------------------------------------
00116 
00126     RuleBasedCollator(const UnicodeString& rules, UErrorCode& status);
00127 
00138     RuleBasedCollator(const UnicodeString& rules,
00139                        ECollationStrength collationStrength,
00140                        UErrorCode& status);
00141 
00152     RuleBasedCollator(const UnicodeString& rules,
00153                     UColAttributeValue decompositionMode,
00154                     UErrorCode& status);
00155 
00167     RuleBasedCollator(const UnicodeString& rules,
00168                     ECollationStrength collationStrength,
00169                     UColAttributeValue decompositionMode,
00170                     UErrorCode& status);
00171 
00178     RuleBasedCollator(const RuleBasedCollator& other);
00179 
00180 
00198     RuleBasedCollator(const uint8_t *bin, int32_t length, 
00199                     const RuleBasedCollator *base, 
00200                     UErrorCode &status);
00201     // destructor --------------------------------------------------------------
00202 
00207     virtual ~RuleBasedCollator();
00208 
00209     // public methods ----------------------------------------------------------
00210 
00216     RuleBasedCollator& operator=(const RuleBasedCollator& other);
00217 
00224     virtual UBool operator==(const Collator& other) const;
00225 
00232     virtual UBool operator!=(const Collator& other) const;
00233 
00240     virtual Collator* clone(void) const;
00241 
00252     virtual CollationElementIterator* createCollationElementIterator(
00253                                            const UnicodeString& source) const;
00254 
00264     virtual CollationElementIterator* createCollationElementIterator(
00265                                          const CharacterIterator& source) const;
00266 
00279     virtual EComparisonResult compare(const UnicodeString& source,
00280                                       const UnicodeString& target) const;
00281 
00282 
00295     virtual UCollationResult compare(const UnicodeString& source,
00296                                       const UnicodeString& target,
00297                                       UErrorCode &status) const;
00298 
00312     virtual EComparisonResult compare(const UnicodeString& source,
00313                                       const UnicodeString&  target,
00314                                       int32_t length) const;
00315 
00329     virtual UCollationResult compare(const UnicodeString& source,
00330                                       const UnicodeString& target,
00331                                       int32_t length,
00332                                       UErrorCode &status) const;
00333 
00367     virtual EComparisonResult compare(const UChar* source, int32_t sourceLength,
00368                                       const UChar* target, int32_t targetLength)
00369                                       const;
00370 
00387     virtual UCollationResult compare(const UChar* source, int32_t sourceLength,
00388                                       const UChar* target, int32_t targetLength,
00389                                       UErrorCode &status) const;
00390 
00402     virtual UCollationResult compare(UCharIterator &sIter,
00403                                      UCharIterator &tIter,
00404                                      UErrorCode &status) const;
00405 
00418     virtual CollationKey& getCollationKey(const UnicodeString& source,
00419                                           CollationKey& key,
00420                                           UErrorCode& status) const;
00421 
00435     virtual CollationKey& getCollationKey(const UChar *source,
00436                                           int32_t sourceLength,
00437                                           CollationKey& key,
00438                                           UErrorCode& status) const;
00439 
00445     virtual int32_t hashCode(void) const;
00446 
00457     virtual const Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
00458 
00465     const UnicodeString& getRules(void) const;
00466 
00472     virtual void getVersion(UVersionInfo info) const;
00473 
00484     int32_t getMaxExpansion(int32_t order) const;
00485 
00496     virtual UClassID getDynamicClassID(void) const;
00497 
00509     static UClassID U_EXPORT2 getStaticClassID(void);
00510 
00519     uint8_t *cloneRuleData(int32_t &length, UErrorCode &status);
00520 
00521 
00532     int32_t cloneBinary(uint8_t *buffer, int32_t capacity, UErrorCode &status);
00533 
00541     void getRules(UColRuleOption delta, UnicodeString &buffer);
00542 
00550     virtual void setAttribute(UColAttribute attr, UColAttributeValue value,
00551                               UErrorCode &status);
00552 
00560     virtual UColAttributeValue getAttribute(UColAttribute attr,
00561                                             UErrorCode &status);
00562 
00573     virtual uint32_t setVariableTop(const UChar *varTop, int32_t len, UErrorCode &status);
00574 
00584     virtual uint32_t setVariableTop(const UnicodeString varTop, UErrorCode &status);
00585 
00593     virtual void setVariableTop(const uint32_t varTop, UErrorCode &status);
00594 
00601     virtual uint32_t getVariableTop(UErrorCode &status) const;
00602 
00612     virtual UnicodeSet *getTailoredSet(UErrorCode &status) const;
00613 
00619     virtual Collator* safeClone(void);
00620 
00631     virtual int32_t getSortKey(const UnicodeString& source, uint8_t *result,
00632                                int32_t resultLength) const;
00633 
00646     virtual int32_t getSortKey(const UChar *source, int32_t sourceLength,
00647                                uint8_t *result, int32_t resultLength) const;
00648 
00659     virtual ECollationStrength getStrength(void) const;
00660 
00667     virtual void setStrength(ECollationStrength newStrength);
00668 
00682      virtual int32_t U_EXPORT2 getReorderCodes(int32_t *dest,
00683                                     int32_t destCapacity,
00684                                     UErrorCode& status) const;
00685 
00696      virtual void U_EXPORT2 setReorderCodes(const int32_t* reorderCodes,
00697                                 int32_t reorderCodesLength,
00698                                 UErrorCode& status) ;
00699 
00716     static int32_t U_EXPORT2 getEquivalentReorderCodes(int32_t reorderCode,
00717                                 int32_t* dest,
00718                                 int32_t destCapacity,
00719                                 UErrorCode& status);
00720 
00721 private:
00722 
00723     // private static constants -----------------------------------------------
00724 
00725     enum {
00726         /* need look up in .commit() */
00727         CHARINDEX = 0x70000000,
00728         /* Expand index follows */
00729         EXPANDCHARINDEX = 0x7E000000,
00730         /* contract indexes follows */
00731         CONTRACTCHARINDEX = 0x7F000000,
00732         /* unmapped character values */
00733         UNMAPPED = 0xFFFFFFFF,
00734         /* primary strength increment */
00735         PRIMARYORDERINCREMENT = 0x00010000,
00736         /* secondary strength increment */
00737         SECONDARYORDERINCREMENT = 0x00000100,
00738         /* tertiary strength increment */
00739         TERTIARYORDERINCREMENT = 0x00000001,
00740         /* mask off anything but primary order */
00741         PRIMARYORDERMASK = 0xffff0000,
00742         /* mask off anything but secondary order */
00743         SECONDARYORDERMASK = 0x0000ff00,
00744         /* mask off anything but tertiary order */
00745         TERTIARYORDERMASK = 0x000000ff,
00746         /* mask off ignorable char order */
00747         IGNORABLEMASK = 0x0000ffff,
00748         /* use only the primary difference */
00749         PRIMARYDIFFERENCEONLY = 0xffff0000,
00750         /* use only the primary and secondary difference */
00751         SECONDARYDIFFERENCEONLY = 0xffffff00,
00752         /* primary order shift */
00753         PRIMARYORDERSHIFT = 16,
00754         /* secondary order shift */
00755         SECONDARYORDERSHIFT = 8,
00756         /* starting value for collation elements */
00757         COLELEMENTSTART = 0x02020202,
00758         /* testing mask for primary low element */
00759         PRIMARYLOWZEROMASK = 0x00FF0000,
00760         /* reseting value for secondaries and tertiaries */
00761         RESETSECONDARYTERTIARY = 0x00000202,
00762         /* reseting value for tertiaries */
00763         RESETTERTIARY = 0x00000002,
00764 
00765         PRIMIGNORABLE = 0x0202
00766     };
00767 
00768     // private data members ---------------------------------------------------
00769 
00770     UBool dataIsOwned;
00771 
00772     UBool isWriteThroughAlias;
00773 
00778     UCollator *ucollator;
00779 
00783     UnicodeString urulestring;
00784 
00785     // friend classes --------------------------------------------------------
00786 
00790     friend class CollationElementIterator;
00791 
00796     friend class Collator;
00797 
00801     friend class StringSearch;
00802 
00803     // private constructors --------------------------------------------------
00804 
00808     RuleBasedCollator();
00809 
00820     RuleBasedCollator(const Locale& desiredLocale, UErrorCode& status);
00821 
00830     void
00831     construct(const UnicodeString& rules,
00832               UColAttributeValue collationStrength,
00833               UColAttributeValue decompositionMode,
00834               UErrorCode& status);
00835 
00836     // private methods -------------------------------------------------------
00837 
00843     void setUCollator(const Locale& locale, UErrorCode& status);
00844 
00850     void setUCollator(const char* locale, UErrorCode& status);
00851 
00858     void setUCollator(UCollator *collator);
00859 
00860 public:
00861 #ifndef U_HIDE_INTERNAL_API
00862 
00867     const UCollator * getUCollator();
00868 #endif  /* U_HIDE_INTERNAL_API */
00869 
00870 protected:
00878     virtual void setLocales(const Locale& requestedLocale, const Locale& validLocale, const Locale& actualLocale);
00879 
00880 private:
00881 
00882     // if not owned and not a write through alias, copy the ucollator
00883     void checkOwned(void);
00884 
00885     // utility to init rule string used by checkOwned and construct
00886     void setRuleStringFromCollator();
00887 
00894     Collator::EComparisonResult getEComparisonResult(
00895                                             const UCollationResult &result) const;
00896 
00902     Collator::ECollationStrength getECollationStrength(
00903                                         const UCollationStrength &strength) const;
00904 
00910     UCollationStrength getUCollationStrength(
00911       const Collator::ECollationStrength &strength) const;
00912  public:
00936     virtual int32_t internalGetShortDefinitionString(const char *locale,
00937                                                      char *buffer,
00938                                                      int32_t capacity,
00939                                                      UErrorCode &status) const;
00940 };
00941 
00942 // inline method implementation ---------------------------------------------
00943 
00944 inline void RuleBasedCollator::setUCollator(const Locale &locale,
00945                                                UErrorCode &status)
00946 {
00947     setUCollator(locale.getName(), status);
00948 }
00949 
00950 
00951 inline void RuleBasedCollator::setUCollator(UCollator     *collator)
00952 {
00953 
00954     if (ucollator && dataIsOwned) {
00955         ucol_close(ucollator);
00956     }
00957     ucollator   = collator;
00958     dataIsOwned = FALSE;
00959     isWriteThroughAlias = TRUE;
00960     setRuleStringFromCollator();
00961 }
00962 
00963 #ifndef U_HIDE_INTERNAL_API
00964 inline const UCollator * RuleBasedCollator::getUCollator()
00965 {
00966     return ucollator;
00967 }
00968 #endif
00969 
00970 inline Collator::EComparisonResult RuleBasedCollator::getEComparisonResult(
00971                                            const UCollationResult &result) const
00972 {
00973     switch (result)
00974     {
00975     case UCOL_LESS :
00976         return Collator::LESS;
00977     case UCOL_EQUAL :
00978         return Collator::EQUAL;
00979     default :
00980         return Collator::GREATER;
00981     }
00982 }
00983 
00984 inline Collator::ECollationStrength RuleBasedCollator::getECollationStrength(
00985                                        const UCollationStrength &strength) const
00986 {
00987     switch (strength)
00988     {
00989     case UCOL_PRIMARY :
00990         return Collator::PRIMARY;
00991     case UCOL_SECONDARY :
00992         return Collator::SECONDARY;
00993     case UCOL_TERTIARY :
00994         return Collator::TERTIARY;
00995     case UCOL_QUATERNARY :
00996         return Collator::QUATERNARY;
00997     default :
00998         return Collator::IDENTICAL;
00999     }
01000 }
01001 
01002 inline UCollationStrength RuleBasedCollator::getUCollationStrength(
01003                              const Collator::ECollationStrength &strength) const
01004 {
01005     switch (strength)
01006     {
01007     case Collator::PRIMARY :
01008         return UCOL_PRIMARY;
01009     case Collator::SECONDARY :
01010         return UCOL_SECONDARY;
01011     case Collator::TERTIARY :
01012         return UCOL_TERTIARY;
01013     case Collator::QUATERNARY :
01014         return UCOL_QUATERNARY;
01015     default :
01016         return UCOL_IDENTICAL;
01017     }
01018 }
01019 
01020 U_NAMESPACE_END
01021 
01022 #endif /* #if !UCONFIG_NO_COLLATION */
01023 
01024 #endif