ICU 49.1.1  49.1.1
tblcoll.h
Go to the documentation of this file.
1 /*
2 ******************************************************************************
3 * Copyright (C) 1996-2012, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ******************************************************************************
6 */
7 
59 #ifndef TBLCOLL_H
60 #define TBLCOLL_H
61 
62 #include "unicode/utypes.h"
63 
64 
65 #if !UCONFIG_NO_COLLATION
66 
67 #include "unicode/coll.h"
68 #include "unicode/ucol.h"
69 #include "unicode/sortkey.h"
70 #include "unicode/normlzr.h"
71 
73 
77 class StringSearch;
82 
112 {
113 public:
114 
115  // constructor -------------------------------------------------------------
116 
126  RuleBasedCollator(const UnicodeString& rules, UErrorCode& status);
127 
138  RuleBasedCollator(const UnicodeString& rules,
139  ECollationStrength collationStrength,
140  UErrorCode& status);
141 
152  RuleBasedCollator(const UnicodeString& rules,
153  UColAttributeValue decompositionMode,
154  UErrorCode& status);
155 
167  RuleBasedCollator(const UnicodeString& rules,
168  ECollationStrength collationStrength,
169  UColAttributeValue decompositionMode,
170  UErrorCode& status);
171 
178  RuleBasedCollator(const RuleBasedCollator& other);
179 
180 
198  RuleBasedCollator(const uint8_t *bin, int32_t length,
199  const RuleBasedCollator *base,
200  UErrorCode &status);
201  // destructor --------------------------------------------------------------
202 
207  virtual ~RuleBasedCollator();
208 
209  // public methods ----------------------------------------------------------
210 
216  RuleBasedCollator& operator=(const RuleBasedCollator& other);
217 
224  virtual UBool operator==(const Collator& other) const;
225 
232  virtual UBool operator!=(const Collator& other) const;
233 
240  virtual Collator* clone(void) const;
241 
252  virtual CollationElementIterator* createCollationElementIterator(
253  const UnicodeString& source) const;
254 
264  virtual CollationElementIterator* createCollationElementIterator(
265  const CharacterIterator& source) const;
266 
279  virtual EComparisonResult compare(const UnicodeString& source,
280  const UnicodeString& target) const;
281 
282 
295  virtual UCollationResult compare(const UnicodeString& source,
296  const UnicodeString& target,
297  UErrorCode &status) const;
298 
312  virtual EComparisonResult compare(const UnicodeString& source,
313  const UnicodeString& target,
314  int32_t length) const;
315 
329  virtual UCollationResult compare(const UnicodeString& source,
330  const UnicodeString& target,
331  int32_t length,
332  UErrorCode &status) const;
333 
367  virtual EComparisonResult compare(const UChar* source, int32_t sourceLength,
368  const UChar* target, int32_t targetLength)
369  const;
370 
387  virtual UCollationResult compare(const UChar* source, int32_t sourceLength,
388  const UChar* target, int32_t targetLength,
389  UErrorCode &status) const;
390 
402  virtual UCollationResult compare(UCharIterator &sIter,
403  UCharIterator &tIter,
404  UErrorCode &status) const;
405 
418  virtual CollationKey& getCollationKey(const UnicodeString& source,
419  CollationKey& key,
420  UErrorCode& status) const;
421 
435  virtual CollationKey& getCollationKey(const UChar *source,
436  int32_t sourceLength,
437  CollationKey& key,
438  UErrorCode& status) const;
439 
445  virtual int32_t hashCode(void) const;
446 
457  virtual const Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
458 
465  const UnicodeString& getRules(void) const;
466 
472  virtual void getVersion(UVersionInfo info) const;
473 
484  int32_t getMaxExpansion(int32_t order) const;
485 
496  virtual UClassID getDynamicClassID(void) const;
497 
509  static UClassID U_EXPORT2 getStaticClassID(void);
510 
519  uint8_t *cloneRuleData(int32_t &length, UErrorCode &status);
520 
521 
532  int32_t cloneBinary(uint8_t *buffer, int32_t capacity, UErrorCode &status);
533 
541  void getRules(UColRuleOption delta, UnicodeString &buffer);
542 
550  virtual void setAttribute(UColAttribute attr, UColAttributeValue value,
551  UErrorCode &status);
552 
561  UErrorCode &status);
562 
573  virtual uint32_t setVariableTop(const UChar *varTop, int32_t len, UErrorCode &status);
574 
584  virtual uint32_t setVariableTop(const UnicodeString varTop, UErrorCode &status);
585 
593  virtual void setVariableTop(const uint32_t varTop, UErrorCode &status);
594 
601  virtual uint32_t getVariableTop(UErrorCode &status) const;
602 
612  virtual UnicodeSet *getTailoredSet(UErrorCode &status) const;
613 
619  virtual Collator* safeClone(void);
620 
631  virtual int32_t getSortKey(const UnicodeString& source, uint8_t *result,
632  int32_t resultLength) const;
633 
646  virtual int32_t getSortKey(const UChar *source, int32_t sourceLength,
647  uint8_t *result, int32_t resultLength) const;
648 
659  virtual ECollationStrength getStrength(void) const;
660 
667  virtual void setStrength(ECollationStrength newStrength);
668 
682  virtual int32_t U_EXPORT2 getReorderCodes(int32_t *dest,
683  int32_t destCapacity,
684  UErrorCode& status) const;
685 
696  virtual void U_EXPORT2 setReorderCodes(const int32_t* reorderCodes,
697  int32_t reorderCodesLength,
698  UErrorCode& status) ;
699 
716  static int32_t U_EXPORT2 getEquivalentReorderCodes(int32_t reorderCode,
717  int32_t* dest,
718  int32_t destCapacity,
719  UErrorCode& status);
720 
721 private:
722 
723  // private static constants -----------------------------------------------
724 
725  enum {
726  /* need look up in .commit() */
727  CHARINDEX = 0x70000000,
728  /* Expand index follows */
729  EXPANDCHARINDEX = 0x7E000000,
730  /* contract indexes follows */
731  CONTRACTCHARINDEX = 0x7F000000,
732  /* unmapped character values */
733  UNMAPPED = 0xFFFFFFFF,
734  /* primary strength increment */
735  PRIMARYORDERINCREMENT = 0x00010000,
736  /* secondary strength increment */
737  SECONDARYORDERINCREMENT = 0x00000100,
738  /* tertiary strength increment */
739  TERTIARYORDERINCREMENT = 0x00000001,
740  /* mask off anything but primary order */
741  PRIMARYORDERMASK = 0xffff0000,
742  /* mask off anything but secondary order */
743  SECONDARYORDERMASK = 0x0000ff00,
744  /* mask off anything but tertiary order */
745  TERTIARYORDERMASK = 0x000000ff,
746  /* mask off ignorable char order */
747  IGNORABLEMASK = 0x0000ffff,
748  /* use only the primary difference */
749  PRIMARYDIFFERENCEONLY = 0xffff0000,
750  /* use only the primary and secondary difference */
751  SECONDARYDIFFERENCEONLY = 0xffffff00,
752  /* primary order shift */
753  PRIMARYORDERSHIFT = 16,
754  /* secondary order shift */
755  SECONDARYORDERSHIFT = 8,
756  /* starting value for collation elements */
757  COLELEMENTSTART = 0x02020202,
758  /* testing mask for primary low element */
759  PRIMARYLOWZEROMASK = 0x00FF0000,
760  /* reseting value for secondaries and tertiaries */
761  RESETSECONDARYTERTIARY = 0x00000202,
762  /* reseting value for tertiaries */
763  RESETTERTIARY = 0x00000002,
764 
765  PRIMIGNORABLE = 0x0202
766  };
767 
768  // private data members ---------------------------------------------------
769 
770  UBool dataIsOwned;
771 
772  UBool isWriteThroughAlias;
773 
778  UCollator *ucollator;
779 
783  UnicodeString urulestring;
784 
785  // friend classes --------------------------------------------------------
786 
791 
796  friend class Collator;
797 
801  friend class StringSearch;
802 
803  // private constructors --------------------------------------------------
804 
809 
820  RuleBasedCollator(const Locale& desiredLocale, UErrorCode& status);
821 
830  void
831  construct(const UnicodeString& rules,
832  UColAttributeValue collationStrength,
833  UColAttributeValue decompositionMode,
834  UErrorCode& status);
835 
836  // private methods -------------------------------------------------------
837 
843  void setUCollator(const Locale& locale, UErrorCode& status);
844 
850  void setUCollator(const char* locale, UErrorCode& status);
851 
858  void setUCollator(UCollator *collator);
859 
860 public:
861 #ifndef U_HIDE_INTERNAL_API
862 
867  const UCollator * getUCollator();
868 #endif /* U_HIDE_INTERNAL_API */
869 
870 protected:
878  virtual void setLocales(const Locale& requestedLocale, const Locale& validLocale, const Locale& actualLocale);
879 
880 private:
881 
882  // if not owned and not a write through alias, copy the ucollator
883  void checkOwned(void);
884 
885  // utility to init rule string used by checkOwned and construct
886  void setRuleStringFromCollator();
887 
894  Collator::EComparisonResult getEComparisonResult(
895  const UCollationResult &result) const;
896 
902  Collator::ECollationStrength getECollationStrength(
903  const UCollationStrength &strength) const;
904 
910  UCollationStrength getUCollationStrength(
911  const Collator::ECollationStrength &strength) const;
912  public:
936  virtual int32_t internalGetShortDefinitionString(const char *locale,
937  char *buffer,
938  int32_t capacity,
939  UErrorCode &status) const;
940 };
941 
942 // inline method implementation ---------------------------------------------
943 
944 inline void RuleBasedCollator::setUCollator(const Locale &locale,
945  UErrorCode &status)
946 {
947  setUCollator(locale.getName(), status);
948 }
949 
950 
951 inline void RuleBasedCollator::setUCollator(UCollator *collator)
952 {
953 
954  if (ucollator && dataIsOwned) {
955  ucol_close(ucollator);
956  }
957  ucollator = collator;
958  dataIsOwned = FALSE;
959  isWriteThroughAlias = TRUE;
960  setRuleStringFromCollator();
961 }
962 
963 #ifndef U_HIDE_INTERNAL_API
965 {
966  return ucollator;
967 }
968 #endif
969 
970 inline Collator::EComparisonResult RuleBasedCollator::getEComparisonResult(
971  const UCollationResult &result) const
972 {
973  switch (result)
974  {
975  case UCOL_LESS :
976  return Collator::LESS;
977  case UCOL_EQUAL :
978  return Collator::EQUAL;
979  default :
980  return Collator::GREATER;
981  }
982 }
983 
984 inline Collator::ECollationStrength RuleBasedCollator::getECollationStrength(
985  const UCollationStrength &strength) const
986 {
987  switch (strength)
988  {
989  case UCOL_PRIMARY :
990  return Collator::PRIMARY;
991  case UCOL_SECONDARY :
992  return Collator::SECONDARY;
993  case UCOL_TERTIARY :
994  return Collator::TERTIARY;
995  case UCOL_QUATERNARY :
996  return Collator::QUATERNARY;
997  default :
998  return Collator::IDENTICAL;
999  }
1000 }
1001 
1002 inline UCollationStrength RuleBasedCollator::getUCollationStrength(
1003  const Collator::ECollationStrength &strength) const
1004 {
1005  switch (strength)
1006  {
1007  case Collator::PRIMARY :
1008  return UCOL_PRIMARY;
1009  case Collator::SECONDARY :
1010  return UCOL_SECONDARY;
1011  case Collator::TERTIARY :
1012  return UCOL_TERTIARY;
1013  case Collator::QUATERNARY :
1014  return UCOL_QUATERNARY;
1015  default :
1016  return UCOL_IDENTICAL;
1017  }
1018 }
1019 
1021 
1022 #endif /* #if !UCONFIG_NO_COLLATION */
1023 
1024 #endif