Rework BCollator API

- Strength is now set once, instead of at each comparison, to improve
  performance and fix potential locking issues
- Add a way to enable "numeric" collation (aka "natural order")
This commit is contained in:
Adrien Destugues
2017-05-29 08:52:43 +02:00
parent affb4e25fe
commit a19a18f553
4 changed files with 68 additions and 97 deletions
+19 -23
View File
@@ -34,9 +34,6 @@ enum collator_strengths {
};
// N.B.: This class is not multithread-safe, as Compare() and GetKey() change
// the ICUCollator (the strength). So if you want to use a BCollator from
// more than one thread, you need to protect it with a lock
class BCollator : public BArchivable {
public:
BCollator();
@@ -51,55 +48,54 @@ public:
BCollator& operator=(const BCollator& source);
void SetDefaultStrength(int8 strength);
int8 DefaultStrength() const;
status_t SetStrength(int8 strength) const;
void SetIgnorePunctuation(bool ignore);
bool IgnorePunctuation() const;
status_t GetSortKey(const char* string, BString* key,
int8 strength = B_COLLATE_DEFAULT) const;
status_t SetNumericSorting(bool enable);
int Compare(const char* s1, const char* s2,
int8 strength = B_COLLATE_DEFAULT) const;
bool Equal(const char* s1, const char* s2,
int8 strength = B_COLLATE_DEFAULT) const;
bool Greater(const char* s1, const char* s2,
int8 strength = B_COLLATE_DEFAULT) const;
bool GreaterOrEqual(const char* s1, const char* s2,
int8 strength = B_COLLATE_DEFAULT) const;
status_t GetSortKey(const char* string, BString* key)
const;
int Compare(const char* s1, const char* s2)
const;
bool Equal(const char* s1, const char* s2)
const;
bool Greater(const char* s1, const char* s2)
const;
bool GreaterOrEqual(const char* s1, const char* s2)
const;
// (un-)archiving API
status_t Archive(BMessage* archive, bool deep) const;
static BArchivable* Instantiate(BMessage* archive);
private:
status_t _SetStrength(int8 strength) const;
mutable U_ICU_NAMESPACE::Collator* fICUCollator;
int8 fDefaultStrength;
bool fIgnorePunctuation;
};
inline bool
BCollator::Equal(const char *s1, const char *s2, int8 strength) const
BCollator::Equal(const char *s1, const char *s2) const
{
return Compare(s1, s2, strength) == 0;
return Compare(s1, s2) == 0;
}
inline bool
BCollator::Greater(const char *s1, const char *s2, int8 strength) const
BCollator::Greater(const char *s1, const char *s2) const
{
return Compare(s1, s2, strength) > 0;
return Compare(s1, s2) > 0;
}
inline bool
BCollator::GreaterOrEqual(const char *s1, const char *s2, int8 strength) const
BCollator::GreaterOrEqual(const char *s1, const char *s2) const
{
return Compare(s1, s2, strength) >= 0;
return Compare(s1, s2) >= 0;
}