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