xref: /haiku/headers/os/locale/Collator.h (revision b671e9bbdbd10268a042b4f4cc4317ccd03d105e)
1 #ifndef _COLLATOR_H_
2 #define _COLLATOR_H_
3 
4 
5 #include <SupportDefs.h>
6 #include <Archivable.h>
7 
8 class BString;
9 class BCollatorAddOn;
10 
11 
12 enum collator_strengths {
13 	B_COLLATE_DEFAULT = -1,
14 
15 	B_COLLATE_PRIMARY = 1,		// e.g.: no diacritical differences, e = é
16 	B_COLLATE_SECONDARY,		// diacritics are different from their base characters, a != ä
17 	B_COLLATE_TERTIARY,			// case sensitive comparison
18 	B_COLLATE_QUATERNARY,
19 
20 	B_COLLATE_IDENTICAL = 127	// Unicode value
21 };
22 
23 
24 class BCollator : public BArchivable {
25 	public:
26 		BCollator();
27 		BCollator(BCollatorAddOn *collator, int8 strength, bool ignorePunctuation);
28 		BCollator(BMessage *archive);
29 		~BCollator();
30 
31 		void SetDefaultStrength(int8 strength);
32 		int8 DefaultStrength() const;
33 
34 		void SetIgnorePunctuation(bool ignore);
35 		bool IgnorePunctuation() const;
36 
37 		status_t GetSortKey(const char *string, BString *key, int8 strength = B_COLLATE_DEFAULT);
38 
39 		int Compare(const char *, const char *, int32 len = -1, int8 strength = B_COLLATE_DEFAULT);
40 		bool Equal(const char *, const char *, int32 len = -1, int8 strength = B_COLLATE_DEFAULT);
41 		bool Greater(const char *, const char *, int32 len = -1, int8 strength = B_COLLATE_DEFAULT);
42 		bool GreaterOrEqual(const char *, const char *, int32 len = -1, int8 strength = B_COLLATE_DEFAULT);
43 
44 		// (un-)archiving API
45 		status_t Archive(BMessage *archive, bool deep) const;
46 		static BArchivable *Instantiate(BMessage *archive);
47 
48 	private:
49 		BCollatorAddOn	*fCollator;
50 		image_id		fCollatorImage;
51 		int8			fStrength;
52 		bool			fIgnorePunctuation;
53 };
54 
55 
56 inline bool
57 BCollator::Equal(const char *s1, const char *s2, int32 len, int8 strength)
58 {
59 	return Compare(s1, s2, len, strength) == 0;
60 }
61 
62 
63 inline bool
64 BCollator::Greater(const char *s1, const char *s2, int32 len, int8 strength)
65 {
66 	return Compare(s1, s2, len, strength) > 0;
67 }
68 
69 
70 inline bool
71 BCollator::GreaterOrEqual(const char *s1, const char *s2, int32 len, int8 strength)
72 {
73 	return Compare(s1, s2, len, strength) >= 0;
74 }
75 
76 
77 /************************************************************************/
78 
79 // For BCollator add-on implementations:
80 
81 class BCollatorAddOn : public BArchivable {
82 	public:
83 		BCollatorAddOn();
84 		BCollatorAddOn(BMessage *archive);
85 		virtual ~BCollatorAddOn();
86 
87 		virtual status_t GetSortKey(const char *string, BString *key, int8 strength,
88 						bool ignorePunctuation);
89 		virtual int Compare(const char *a, const char *b, int32 length, int8 strength,
90 						bool ignorePunctuation);
91 
92 		// (un-)archiving API
93 		virtual status_t Archive(BMessage *archive, bool deep) const;
94 		static BArchivable *Instantiate(BMessage *archive);
95 
96 	protected:
97 		struct input_context {
98 			input_context(bool ignorePunctuation);
99 
100 			bool	ignore_punctuation;
101 			uint32	next_char;
102 			int32	reserved1;
103 			int32	reserved2;
104 		};
105 
106 		virtual uint32 GetNextChar(const char **string, input_context &context);
107 		virtual size_t PrimaryKeyLength(size_t length);
108 		virtual char *PutPrimaryKey(const char *string, char *buffer, int32 length,
109 						bool ignorePunctuation);
110 };
111 
112 // If your add-on should work with the standard tool to create a language, it
113 // should export that function. However, once the language file has been written
114 // only the archived collator is used, and that function won't be called anymore.
115 extern "C" BCollatorAddOn *instantiate_collator(void);
116 
117 #endif	/* _COLLATOR_H_ */
118