1 /* 2 * Copyright 2003-2011, Haiku, Inc. 3 * Distributed under the terms of the MIT Licence. 4 */ 5 #ifndef _COLLATOR_H_ 6 #define _COLLATOR_H_ 7 8 9 #include <Archivable.h> 10 #include <SupportDefs.h> 11 12 13 namespace icu { 14 class Collator; 15 }; 16 17 class BString; 18 class BCollatorAddOn; 19 20 21 enum collator_strengths { 22 B_COLLATE_DEFAULT = -1, 23 24 B_COLLATE_PRIMARY = 1, // e.g.: no diacritical differences, e = é 25 B_COLLATE_SECONDARY, // diacritics are different from their base 26 // characters, a != ä 27 B_COLLATE_TERTIARY, // case sensitive comparison 28 B_COLLATE_QUATERNARY, 29 30 B_COLLATE_IDENTICAL = 127 // Unicode value 31 }; 32 33 34 // N.B.: This class is not multithread-safe, as Compare() and GetKey() change 35 // the ICUCollator (the strength). So if you want to use a BCollator from 36 // more than one thread, you need to protect it with a lock 37 class BCollator : public BArchivable { 38 public: 39 BCollator(); 40 BCollator(const char* locale, 41 int8 strength = B_COLLATE_PRIMARY, 42 bool ignorePunctuation = false); 43 BCollator(BMessage* archive); 44 45 BCollator(const BCollator& other); 46 47 ~BCollator(); 48 49 BCollator& operator=(const BCollator& source); 50 51 void SetDefaultStrength(int8 strength); 52 int8 DefaultStrength() const; 53 54 void SetIgnorePunctuation(bool ignore); 55 bool IgnorePunctuation() const; 56 57 status_t GetSortKey(const char* string, BString* key, 58 int8 strength = B_COLLATE_DEFAULT) const; 59 60 int Compare(const char* s1, const char* s2, 61 int8 strength = B_COLLATE_DEFAULT) const; 62 bool Equal(const char* s1, const char* s2, 63 int8 strength = B_COLLATE_DEFAULT) const; 64 bool Greater(const char* s1, const char* s2, 65 int8 strength = B_COLLATE_DEFAULT) const; 66 bool GreaterOrEqual(const char* s1, const char* s2, 67 int8 strength = B_COLLATE_DEFAULT) const; 68 69 // (un-)archiving API 70 status_t Archive(BMessage* archive, bool deep) const; 71 static BArchivable* Instantiate(BMessage* archive); 72 73 private: 74 status_t _SetStrength(int8 strength) const; 75 76 mutable icu::Collator* fICUCollator; 77 int8 fDefaultStrength; 78 bool fIgnorePunctuation; 79 }; 80 81 82 inline bool 83 BCollator::Equal(const char *s1, const char *s2, int8 strength) const 84 { 85 return Compare(s1, s2, strength) == 0; 86 } 87 88 89 inline bool 90 BCollator::Greater(const char *s1, const char *s2, int8 strength) const 91 { 92 return Compare(s1, s2, strength) > 0; 93 } 94 95 96 inline bool 97 BCollator::GreaterOrEqual(const char *s1, const char *s2, int8 strength) const 98 { 99 return Compare(s1, s2, strength) >= 0; 100 } 101 102 103 #endif /* _COLLATOR_H_ */ 104