ICU 4.6 4.6
tblcoll.h
Go to the documentation of this file.
00001 /*
00002 ******************************************************************************
00003 * Copyright (C) 1996-2010, 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 
00678     virtual int32_t getReorderCodes(int32_t* dest,
00679                                     int32_t destCapacity,
00680                                     UErrorCode& status) const;
00681 
00689     virtual void setReorderCodes(const int32_t* reorderCodes,
00690                                  int32_t reorderCodesLength,
00691                                  UErrorCode& status);
00692 
00693 
00694 private:
00695 
00696     // private static constants -----------------------------------------------
00697 
00698     enum {
00699         /* need look up in .commit() */
00700         CHARINDEX = 0x70000000,
00701         /* Expand index follows */
00702         EXPANDCHARINDEX = 0x7E000000,
00703         /* contract indexes follows */
00704         CONTRACTCHARINDEX = 0x7F000000,
00705         /* unmapped character values */
00706         UNMAPPED = 0xFFFFFFFF,
00707         /* primary strength increment */
00708         PRIMARYORDERINCREMENT = 0x00010000,
00709         /* secondary strength increment */
00710         SECONDARYORDERINCREMENT = 0x00000100,
00711         /* tertiary strength increment */
00712         TERTIARYORDERINCREMENT = 0x00000001,
00713         /* mask off anything but primary order */
00714         PRIMARYORDERMASK = 0xffff0000,
00715         /* mask off anything but secondary order */
00716         SECONDARYORDERMASK = 0x0000ff00,
00717         /* mask off anything but tertiary order */
00718         TERTIARYORDERMASK = 0x000000ff,
00719         /* mask off ignorable char order */
00720         IGNORABLEMASK = 0x0000ffff,
00721         /* use only the primary difference */
00722         PRIMARYDIFFERENCEONLY = 0xffff0000,
00723         /* use only the primary and secondary difference */
00724         SECONDARYDIFFERENCEONLY = 0xffffff00,
00725         /* primary order shift */
00726         PRIMARYORDERSHIFT = 16,
00727         /* secondary order shift */
00728         SECONDARYORDERSHIFT = 8,
00729         /* starting value for collation elements */
00730         COLELEMENTSTART = 0x02020202,
00731         /* testing mask for primary low element */
00732         PRIMARYLOWZEROMASK = 0x00FF0000,
00733         /* reseting value for secondaries and tertiaries */
00734         RESETSECONDARYTERTIARY = 0x00000202,
00735         /* reseting value for tertiaries */
00736         RESETTERTIARY = 0x00000002,
00737 
00738         PRIMIGNORABLE = 0x0202
00739     };
00740 
00741     // private data members ---------------------------------------------------
00742 
00743     UBool dataIsOwned;
00744 
00745     UBool isWriteThroughAlias;
00746 
00751     UCollator *ucollator;
00752 
00756     UnicodeString urulestring;
00757 
00758     // friend classes --------------------------------------------------------
00759 
00763     friend class CollationElementIterator;
00764 
00769     friend class Collator;
00770 
00774     friend class StringSearch;
00775 
00776     // private constructors --------------------------------------------------
00777 
00781     RuleBasedCollator();
00782 
00793     RuleBasedCollator(const Locale& desiredLocale, UErrorCode& status);
00794 
00803     void
00804     construct(const UnicodeString& rules,
00805               UColAttributeValue collationStrength,
00806               UColAttributeValue decompositionMode,
00807               UErrorCode& status);
00808 
00809     // private methods -------------------------------------------------------
00810 
00816     void setUCollator(const Locale& locale, UErrorCode& status);
00817 
00823     void setUCollator(const char* locale, UErrorCode& status);
00824 
00832     void setUCollator(UCollator *collator);
00833 
00834 public:
00840     const UCollator * getUCollator();
00841 
00842 protected:
00850     virtual void setLocales(const Locale& requestedLocale, const Locale& validLocale, const Locale& actualLocale);
00851 
00852 private:
00853 
00854     // if not owned and not a write through alias, copy the ucollator
00855     void checkOwned(void);
00856 
00857     // utility to init rule string used by checkOwned and construct
00858     void setRuleStringFromCollator();
00859 
00866     Collator::EComparisonResult getEComparisonResult(
00867                                             const UCollationResult &result) const;
00868 
00874     Collator::ECollationStrength getECollationStrength(
00875                                         const UCollationStrength &strength) const;
00876 
00882     UCollationStrength getUCollationStrength(
00883       const Collator::ECollationStrength &strength) const;
00884 };
00885 
00886 // inline method implementation ---------------------------------------------
00887 
00888 inline void RuleBasedCollator::setUCollator(const Locale &locale,
00889                                                UErrorCode &status)
00890 {
00891     setUCollator(locale.getName(), status);
00892 }
00893 
00894 
00895 inline void RuleBasedCollator::setUCollator(UCollator     *collator)
00896 {
00897 
00898     if (ucollator && dataIsOwned) {
00899         ucol_close(ucollator);
00900     }
00901     ucollator   = collator;
00902     dataIsOwned = FALSE;
00903     isWriteThroughAlias = TRUE;
00904     setRuleStringFromCollator();
00905 }
00906 
00907 inline const UCollator * RuleBasedCollator::getUCollator()
00908 {
00909     return ucollator;
00910 }
00911 
00912 inline Collator::EComparisonResult RuleBasedCollator::getEComparisonResult(
00913                                            const UCollationResult &result) const
00914 {
00915     switch (result)
00916     {
00917     case UCOL_LESS :
00918         return Collator::LESS;
00919     case UCOL_EQUAL :
00920         return Collator::EQUAL;
00921     default :
00922         return Collator::GREATER;
00923     }
00924 }
00925 
00926 inline Collator::ECollationStrength RuleBasedCollator::getECollationStrength(
00927                                        const UCollationStrength &strength) const
00928 {
00929     switch (strength)
00930     {
00931     case UCOL_PRIMARY :
00932         return Collator::PRIMARY;
00933     case UCOL_SECONDARY :
00934         return Collator::SECONDARY;
00935     case UCOL_TERTIARY :
00936         return Collator::TERTIARY;
00937     case UCOL_QUATERNARY :
00938         return Collator::QUATERNARY;
00939     default :
00940         return Collator::IDENTICAL;
00941     }
00942 }
00943 
00944 inline UCollationStrength RuleBasedCollator::getUCollationStrength(
00945                              const Collator::ECollationStrength &strength) const
00946 {
00947     switch (strength)
00948     {
00949     case Collator::PRIMARY :
00950         return UCOL_PRIMARY;
00951     case Collator::SECONDARY :
00952         return UCOL_SECONDARY;
00953     case Collator::TERTIARY :
00954         return UCOL_TERTIARY;
00955     case Collator::QUATERNARY :
00956         return UCOL_QUATERNARY;
00957     default :
00958         return UCOL_IDENTICAL;
00959     }
00960 }
00961 
00962 U_NAMESPACE_END
00963 
00964 #endif /* #if !UCONFIG_NO_COLLATION */
00965 
00966 #endif
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines