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