tblcoll.h

Go to the documentation of this file.
00001 /*
00002 ******************************************************************************
00003 * Copyright (C) 1996-2008, 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 
00403     virtual CollationKey& getCollationKey(const UnicodeString& source,
00404                                           CollationKey& key,
00405                                           UErrorCode& status) const;
00406 
00420     virtual CollationKey& getCollationKey(const UChar *source,
00421                                           int32_t sourceLength,
00422                                           CollationKey& key,
00423                                           UErrorCode& status) const;
00424 
00430     virtual int32_t hashCode(void) const;
00431 
00442     virtual const Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
00443 
00450     const UnicodeString& getRules(void) const;
00451 
00457     virtual void getVersion(UVersionInfo info) const;
00458 
00469     int32_t getMaxExpansion(int32_t order) const;
00470 
00481     virtual UClassID getDynamicClassID(void) const;
00482 
00494     static UClassID U_EXPORT2 getStaticClassID(void);
00495 
00504     uint8_t *cloneRuleData(int32_t &length, UErrorCode &status);
00505 
00506 
00517     int32_t cloneBinary(uint8_t *buffer, int32_t capacity, UErrorCode &status);
00518 
00526     void getRules(UColRuleOption delta, UnicodeString &buffer);
00527 
00535     virtual void setAttribute(UColAttribute attr, UColAttributeValue value,
00536                               UErrorCode &status);
00537 
00545     virtual UColAttributeValue getAttribute(UColAttribute attr,
00546                                             UErrorCode &status);
00547 
00558     virtual uint32_t setVariableTop(const UChar *varTop, int32_t len, UErrorCode &status);
00559 
00569     virtual uint32_t setVariableTop(const UnicodeString varTop, UErrorCode &status);
00570 
00578     virtual void setVariableTop(const uint32_t varTop, UErrorCode &status);
00579 
00586     virtual uint32_t getVariableTop(UErrorCode &status) const;
00587 
00597     virtual UnicodeSet *getTailoredSet(UErrorCode &status) const;
00598 
00604     virtual Collator* safeClone(void);
00605 
00616     virtual int32_t getSortKey(const UnicodeString& source, uint8_t *result,
00617                                int32_t resultLength) const;
00618 
00631     virtual int32_t getSortKey(const UChar *source, int32_t sourceLength,
00632                                uint8_t *result, int32_t resultLength) const;
00633 
00644     virtual ECollationStrength getStrength(void) const;
00645 
00652     virtual void setStrength(ECollationStrength newStrength);
00653 
00654 private:
00655 
00656     // private static constants -----------------------------------------------
00657 
00658     enum {
00659         /* need look up in .commit() */
00660         CHARINDEX = 0x70000000,
00661         /* Expand index follows */
00662         EXPANDCHARINDEX = 0x7E000000,
00663         /* contract indexes follows */
00664         CONTRACTCHARINDEX = 0x7F000000,
00665         /* unmapped character values */
00666         UNMAPPED = 0xFFFFFFFF,
00667         /* primary strength increment */
00668         PRIMARYORDERINCREMENT = 0x00010000,
00669         /* secondary strength increment */
00670         SECONDARYORDERINCREMENT = 0x00000100,
00671         /* tertiary strength increment */
00672         TERTIARYORDERINCREMENT = 0x00000001,
00673         /* mask off anything but primary order */
00674         PRIMARYORDERMASK = 0xffff0000,
00675         /* mask off anything but secondary order */
00676         SECONDARYORDERMASK = 0x0000ff00,
00677         /* mask off anything but tertiary order */
00678         TERTIARYORDERMASK = 0x000000ff,
00679         /* mask off ignorable char order */
00680         IGNORABLEMASK = 0x0000ffff,
00681         /* use only the primary difference */
00682         PRIMARYDIFFERENCEONLY = 0xffff0000,
00683         /* use only the primary and secondary difference */
00684         SECONDARYDIFFERENCEONLY = 0xffffff00,
00685         /* primary order shift */
00686         PRIMARYORDERSHIFT = 16,
00687         /* secondary order shift */
00688         SECONDARYORDERSHIFT = 8,
00689         /* starting value for collation elements */
00690         COLELEMENTSTART = 0x02020202,
00691         /* testing mask for primary low element */
00692         PRIMARYLOWZEROMASK = 0x00FF0000,
00693         /* reseting value for secondaries and tertiaries */
00694         RESETSECONDARYTERTIARY = 0x00000202,
00695         /* reseting value for tertiaries */
00696         RESETTERTIARY = 0x00000002,
00697 
00698         PRIMIGNORABLE = 0x0202
00699     };
00700 
00701     // private data members ---------------------------------------------------
00702 
00703     UBool dataIsOwned;
00704 
00705     UBool isWriteThroughAlias;
00706 
00711     UCollator *ucollator;
00712 
00716     UnicodeString urulestring;
00717 
00718     // friend classes --------------------------------------------------------
00719 
00723     friend class CollationElementIterator;
00724 
00729     friend class Collator;
00730 
00734     friend class StringSearch;
00735 
00736     // private constructors --------------------------------------------------
00737 
00741     RuleBasedCollator();
00742 
00753     RuleBasedCollator(const Locale& desiredLocale, UErrorCode& status);
00754 
00763     void
00764     construct(const UnicodeString& rules,
00765               UColAttributeValue collationStrength,
00766               UColAttributeValue decompositionMode,
00767               UErrorCode& status);
00768 
00769     // private methods -------------------------------------------------------
00770 
00776     void setUCollator(const Locale& locale, UErrorCode& status);
00777 
00783     void setUCollator(const char* locale, UErrorCode& status);
00784 
00792     void setUCollator(UCollator *collator);
00793 
00794 public:
00800     const UCollator * getUCollator();
00801 
00802 protected:
00810     virtual void setLocales(const Locale& requestedLocale, const Locale& validLocale, const Locale& actualLocale);
00811 
00812 private:
00813 
00814     // if not owned and not a write through alias, copy the ucollator
00815     void checkOwned(void);
00816 
00817     // utility to init rule string used by checkOwned and construct
00818     void setRuleStringFromCollator();
00819 
00826     Collator::EComparisonResult getEComparisonResult(
00827                                             const UCollationResult &result) const;
00828 
00834     Collator::ECollationStrength getECollationStrength(
00835                                         const UCollationStrength &strength) const;
00836 
00842     UCollationStrength getUCollationStrength(
00843       const Collator::ECollationStrength &strength) const;
00844 };
00845 
00846 // inline method implementation ---------------------------------------------
00847 
00848 inline void RuleBasedCollator::setUCollator(const Locale &locale,
00849                                                UErrorCode &status)
00850 {
00851     setUCollator(locale.getName(), status);
00852 }
00853 
00854 
00855 inline void RuleBasedCollator::setUCollator(UCollator     *collator)
00856 {
00857 
00858     if (ucollator && dataIsOwned) {
00859         ucol_close(ucollator);
00860     }
00861     ucollator   = collator;
00862     dataIsOwned = FALSE;
00863     isWriteThroughAlias = TRUE;
00864     setRuleStringFromCollator();
00865 }
00866 
00867 inline const UCollator * RuleBasedCollator::getUCollator()
00868 {
00869     return ucollator;
00870 }
00871 
00872 inline Collator::EComparisonResult RuleBasedCollator::getEComparisonResult(
00873                                            const UCollationResult &result) const
00874 {
00875     switch (result)
00876     {
00877     case UCOL_LESS :
00878         return Collator::LESS;
00879     case UCOL_EQUAL :
00880         return Collator::EQUAL;
00881     default :
00882         return Collator::GREATER;
00883     }
00884 }
00885 
00886 inline Collator::ECollationStrength RuleBasedCollator::getECollationStrength(
00887                                        const UCollationStrength &strength) const
00888 {
00889     switch (strength)
00890     {
00891     case UCOL_PRIMARY :
00892         return Collator::PRIMARY;
00893     case UCOL_SECONDARY :
00894         return Collator::SECONDARY;
00895     case UCOL_TERTIARY :
00896         return Collator::TERTIARY;
00897     case UCOL_QUATERNARY :
00898         return Collator::QUATERNARY;
00899     default :
00900         return Collator::IDENTICAL;
00901     }
00902 }
00903 
00904 inline UCollationStrength RuleBasedCollator::getUCollationStrength(
00905                              const Collator::ECollationStrength &strength) const
00906 {
00907     switch (strength)
00908     {
00909     case Collator::PRIMARY :
00910         return UCOL_PRIMARY;
00911     case Collator::SECONDARY :
00912         return UCOL_SECONDARY;
00913     case Collator::TERTIARY :
00914         return UCOL_TERTIARY;
00915     case Collator::QUATERNARY :
00916         return UCOL_QUATERNARY;
00917     default :
00918         return UCOL_IDENTICAL;
00919     }
00920 }
00921 
00922 U_NAMESPACE_END
00923 
00924 #endif /* #if !UCONFIG_NO_COLLATION */
00925 
00926 #endif

Generated on Sat Oct 3 23:25:33 2009 for ICU 4.0 by  doxygen 1.4.7