ICU 4.8.1.1
4.8.1.1
|
00001 /* 00002 ****************************************************************************** 00003 * Copyright (C) 1996-2011, 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 00722 private: 00723 00724 // private static constants ----------------------------------------------- 00725 00726 enum { 00727 /* need look up in .commit() */ 00728 CHARINDEX = 0x70000000, 00729 /* Expand index follows */ 00730 EXPANDCHARINDEX = 0x7E000000, 00731 /* contract indexes follows */ 00732 CONTRACTCHARINDEX = 0x7F000000, 00733 /* unmapped character values */ 00734 UNMAPPED = 0xFFFFFFFF, 00735 /* primary strength increment */ 00736 PRIMARYORDERINCREMENT = 0x00010000, 00737 /* secondary strength increment */ 00738 SECONDARYORDERINCREMENT = 0x00000100, 00739 /* tertiary strength increment */ 00740 TERTIARYORDERINCREMENT = 0x00000001, 00741 /* mask off anything but primary order */ 00742 PRIMARYORDERMASK = 0xffff0000, 00743 /* mask off anything but secondary order */ 00744 SECONDARYORDERMASK = 0x0000ff00, 00745 /* mask off anything but tertiary order */ 00746 TERTIARYORDERMASK = 0x000000ff, 00747 /* mask off ignorable char order */ 00748 IGNORABLEMASK = 0x0000ffff, 00749 /* use only the primary difference */ 00750 PRIMARYDIFFERENCEONLY = 0xffff0000, 00751 /* use only the primary and secondary difference */ 00752 SECONDARYDIFFERENCEONLY = 0xffffff00, 00753 /* primary order shift */ 00754 PRIMARYORDERSHIFT = 16, 00755 /* secondary order shift */ 00756 SECONDARYORDERSHIFT = 8, 00757 /* starting value for collation elements */ 00758 COLELEMENTSTART = 0x02020202, 00759 /* testing mask for primary low element */ 00760 PRIMARYLOWZEROMASK = 0x00FF0000, 00761 /* reseting value for secondaries and tertiaries */ 00762 RESETSECONDARYTERTIARY = 0x00000202, 00763 /* reseting value for tertiaries */ 00764 RESETTERTIARY = 0x00000002, 00765 00766 PRIMIGNORABLE = 0x0202 00767 }; 00768 00769 // private data members --------------------------------------------------- 00770 00771 UBool dataIsOwned; 00772 00773 UBool isWriteThroughAlias; 00774 00779 UCollator *ucollator; 00780 00784 UnicodeString urulestring; 00785 00786 // friend classes -------------------------------------------------------- 00787 00791 friend class CollationElementIterator; 00792 00797 friend class Collator; 00798 00802 friend class StringSearch; 00803 00804 // private constructors -------------------------------------------------- 00805 00809 RuleBasedCollator(); 00810 00821 RuleBasedCollator(const Locale& desiredLocale, UErrorCode& status); 00822 00831 void 00832 construct(const UnicodeString& rules, 00833 UColAttributeValue collationStrength, 00834 UColAttributeValue decompositionMode, 00835 UErrorCode& status); 00836 00837 // private methods ------------------------------------------------------- 00838 00844 void setUCollator(const Locale& locale, UErrorCode& status); 00845 00851 void setUCollator(const char* locale, UErrorCode& status); 00852 00859 void setUCollator(UCollator *collator); 00860 00861 public: 00867 const UCollator * getUCollator(); 00868 00869 protected: 00877 virtual void setLocales(const Locale& requestedLocale, const Locale& validLocale, const Locale& actualLocale); 00878 00879 private: 00880 00881 // if not owned and not a write through alias, copy the ucollator 00882 void checkOwned(void); 00883 00884 // utility to init rule string used by checkOwned and construct 00885 void setRuleStringFromCollator(); 00886 00893 Collator::EComparisonResult getEComparisonResult( 00894 const UCollationResult &result) const; 00895 00901 Collator::ECollationStrength getECollationStrength( 00902 const UCollationStrength &strength) const; 00903 00909 UCollationStrength getUCollationStrength( 00910 const Collator::ECollationStrength &strength) const; 00911 }; 00912 00913 // inline method implementation --------------------------------------------- 00914 00915 inline void RuleBasedCollator::setUCollator(const Locale &locale, 00916 UErrorCode &status) 00917 { 00918 setUCollator(locale.getName(), status); 00919 } 00920 00921 00922 inline void RuleBasedCollator::setUCollator(UCollator *collator) 00923 { 00924 00925 if (ucollator && dataIsOwned) { 00926 ucol_close(ucollator); 00927 } 00928 ucollator = collator; 00929 dataIsOwned = FALSE; 00930 isWriteThroughAlias = TRUE; 00931 setRuleStringFromCollator(); 00932 } 00933 00934 inline const UCollator * RuleBasedCollator::getUCollator() 00935 { 00936 return ucollator; 00937 } 00938 00939 inline Collator::EComparisonResult RuleBasedCollator::getEComparisonResult( 00940 const UCollationResult &result) const 00941 { 00942 switch (result) 00943 { 00944 case UCOL_LESS : 00945 return Collator::LESS; 00946 case UCOL_EQUAL : 00947 return Collator::EQUAL; 00948 default : 00949 return Collator::GREATER; 00950 } 00951 } 00952 00953 inline Collator::ECollationStrength RuleBasedCollator::getECollationStrength( 00954 const UCollationStrength &strength) const 00955 { 00956 switch (strength) 00957 { 00958 case UCOL_PRIMARY : 00959 return Collator::PRIMARY; 00960 case UCOL_SECONDARY : 00961 return Collator::SECONDARY; 00962 case UCOL_TERTIARY : 00963 return Collator::TERTIARY; 00964 case UCOL_QUATERNARY : 00965 return Collator::QUATERNARY; 00966 default : 00967 return Collator::IDENTICAL; 00968 } 00969 } 00970 00971 inline UCollationStrength RuleBasedCollator::getUCollationStrength( 00972 const Collator::ECollationStrength &strength) const 00973 { 00974 switch (strength) 00975 { 00976 case Collator::PRIMARY : 00977 return UCOL_PRIMARY; 00978 case Collator::SECONDARY : 00979 return UCOL_SECONDARY; 00980 case Collator::TERTIARY : 00981 return UCOL_TERTIARY; 00982 case Collator::QUATERNARY : 00983 return UCOL_QUATERNARY; 00984 default : 00985 return UCOL_IDENTICAL; 00986 } 00987 } 00988 00989 U_NAMESPACE_END 00990 00991 #endif /* #if !UCONFIG_NO_COLLATION */ 00992 00993 #endif