ICU 4.4.2 4.4.2

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 
00669 private:
00670 
00671     // private static constants -----------------------------------------------
00672 
00673     enum {
00674         /* need look up in .commit() */
00675         CHARINDEX = 0x70000000,
00676         /* Expand index follows */
00677         EXPANDCHARINDEX = 0x7E000000,
00678         /* contract indexes follows */
00679         CONTRACTCHARINDEX = 0x7F000000,
00680         /* unmapped character values */
00681         UNMAPPED = 0xFFFFFFFF,
00682         /* primary strength increment */
00683         PRIMARYORDERINCREMENT = 0x00010000,
00684         /* secondary strength increment */
00685         SECONDARYORDERINCREMENT = 0x00000100,
00686         /* tertiary strength increment */
00687         TERTIARYORDERINCREMENT = 0x00000001,
00688         /* mask off anything but primary order */
00689         PRIMARYORDERMASK = 0xffff0000,
00690         /* mask off anything but secondary order */
00691         SECONDARYORDERMASK = 0x0000ff00,
00692         /* mask off anything but tertiary order */
00693         TERTIARYORDERMASK = 0x000000ff,
00694         /* mask off ignorable char order */
00695         IGNORABLEMASK = 0x0000ffff,
00696         /* use only the primary difference */
00697         PRIMARYDIFFERENCEONLY = 0xffff0000,
00698         /* use only the primary and secondary difference */
00699         SECONDARYDIFFERENCEONLY = 0xffffff00,
00700         /* primary order shift */
00701         PRIMARYORDERSHIFT = 16,
00702         /* secondary order shift */
00703         SECONDARYORDERSHIFT = 8,
00704         /* starting value for collation elements */
00705         COLELEMENTSTART = 0x02020202,
00706         /* testing mask for primary low element */
00707         PRIMARYLOWZEROMASK = 0x00FF0000,
00708         /* reseting value for secondaries and tertiaries */
00709         RESETSECONDARYTERTIARY = 0x00000202,
00710         /* reseting value for tertiaries */
00711         RESETTERTIARY = 0x00000002,
00712 
00713         PRIMIGNORABLE = 0x0202
00714     };
00715 
00716     // private data members ---------------------------------------------------
00717 
00718     UBool dataIsOwned;
00719 
00720     UBool isWriteThroughAlias;
00721 
00726     UCollator *ucollator;
00727 
00731     UnicodeString urulestring;
00732 
00733     // friend classes --------------------------------------------------------
00734 
00738     friend class CollationElementIterator;
00739 
00744     friend class Collator;
00745 
00749     friend class StringSearch;
00750 
00751     // private constructors --------------------------------------------------
00752 
00756     RuleBasedCollator();
00757 
00768     RuleBasedCollator(const Locale& desiredLocale, UErrorCode& status);
00769 
00778     void
00779     construct(const UnicodeString& rules,
00780               UColAttributeValue collationStrength,
00781               UColAttributeValue decompositionMode,
00782               UErrorCode& status);
00783 
00784     // private methods -------------------------------------------------------
00785 
00791     void setUCollator(const Locale& locale, UErrorCode& status);
00792 
00798     void setUCollator(const char* locale, UErrorCode& status);
00799 
00807     void setUCollator(UCollator *collator);
00808 
00809 public:
00815     const UCollator * getUCollator();
00816 
00817 protected:
00825     virtual void setLocales(const Locale& requestedLocale, const Locale& validLocale, const Locale& actualLocale);
00826 
00827 private:
00828 
00829     // if not owned and not a write through alias, copy the ucollator
00830     void checkOwned(void);
00831 
00832     // utility to init rule string used by checkOwned and construct
00833     void setRuleStringFromCollator();
00834 
00841     Collator::EComparisonResult getEComparisonResult(
00842                                             const UCollationResult &result) const;
00843 
00849     Collator::ECollationStrength getECollationStrength(
00850                                         const UCollationStrength &strength) const;
00851 
00857     UCollationStrength getUCollationStrength(
00858       const Collator::ECollationStrength &strength) const;
00859 };
00860 
00861 // inline method implementation ---------------------------------------------
00862 
00863 inline void RuleBasedCollator::setUCollator(const Locale &locale,
00864                                                UErrorCode &status)
00865 {
00866     setUCollator(locale.getName(), status);
00867 }
00868 
00869 
00870 inline void RuleBasedCollator::setUCollator(UCollator     *collator)
00871 {
00872 
00873     if (ucollator && dataIsOwned) {
00874         ucol_close(ucollator);
00875     }
00876     ucollator   = collator;
00877     dataIsOwned = FALSE;
00878     isWriteThroughAlias = TRUE;
00879     setRuleStringFromCollator();
00880 }
00881 
00882 inline const UCollator * RuleBasedCollator::getUCollator()
00883 {
00884     return ucollator;
00885 }
00886 
00887 inline Collator::EComparisonResult RuleBasedCollator::getEComparisonResult(
00888                                            const UCollationResult &result) const
00889 {
00890     switch (result)
00891     {
00892     case UCOL_LESS :
00893         return Collator::LESS;
00894     case UCOL_EQUAL :
00895         return Collator::EQUAL;
00896     default :
00897         return Collator::GREATER;
00898     }
00899 }
00900 
00901 inline Collator::ECollationStrength RuleBasedCollator::getECollationStrength(
00902                                        const UCollationStrength &strength) const
00903 {
00904     switch (strength)
00905     {
00906     case UCOL_PRIMARY :
00907         return Collator::PRIMARY;
00908     case UCOL_SECONDARY :
00909         return Collator::SECONDARY;
00910     case UCOL_TERTIARY :
00911         return Collator::TERTIARY;
00912     case UCOL_QUATERNARY :
00913         return Collator::QUATERNARY;
00914     default :
00915         return Collator::IDENTICAL;
00916     }
00917 }
00918 
00919 inline UCollationStrength RuleBasedCollator::getUCollationStrength(
00920                              const Collator::ECollationStrength &strength) const
00921 {
00922     switch (strength)
00923     {
00924     case Collator::PRIMARY :
00925         return UCOL_PRIMARY;
00926     case Collator::SECONDARY :
00927         return UCOL_SECONDARY;
00928     case Collator::TERTIARY :
00929         return UCOL_TERTIARY;
00930     case Collator::QUATERNARY :
00931         return UCOL_QUATERNARY;
00932     default :
00933         return UCOL_IDENTICAL;
00934     }
00935 }
00936 
00937 U_NAMESPACE_END
00938 
00939 #endif /* #if !UCONFIG_NO_COLLATION */
00940 
00941 #endif
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines