Boost.Locale
collator.hpp
1//
2// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3//
4// Distributed under the Boost Software License, Version 1.0.
5// https://www.boost.org/LICENSE_1_0.txt
6
7#ifndef BOOST_LOCALE_COLLATOR_HPP_INCLUDED
8#define BOOST_LOCALE_COLLATOR_HPP_INCLUDED
9
10#include <boost/locale/config.hpp>
11#include <locale>
12
13#ifdef BOOST_MSVC
14# pragma warning(push)
15# pragma warning(disable : 4275 4251 4231 4660)
16#endif
17
18namespace boost { namespace locale {
19
24
26 enum class collate_level {
27 primary = 0,
28 secondary = 1,
29 tertiary = 2,
30 quaternary = 3,
31 identical = 4
32 };
33
34 class BOOST_DEPRECATED("Use collate_level") collator_base {
35 public:
36 using level_type = collate_level;
37 static constexpr auto primary = collate_level::primary;
38 static constexpr auto secondary = collate_level::secondary;
39 static constexpr auto tertiary = collate_level::tertiary;
40 static constexpr auto quaternary = collate_level::quaternary;
41 static constexpr auto identical = collate_level::identical;
42 };
43
48 template<typename CharType>
49 class collator : public std::collate<CharType> {
50 public:
52 typedef CharType char_type;
54 typedef std::basic_string<CharType> string_type;
55
61 const char_type* b1,
62 const char_type* e1,
63 const char_type* b2,
64 const char_type* e2) const
65 {
66 return do_compare(level, b1, e1, b2, e2);
67 }
68
78 string_type transform(collate_level level, const char_type* b, const char_type* e) const
79 {
80 return do_transform(level, b, e);
81 }
82
88 long hash(collate_level level, const char_type* b, const char_type* e) const { return do_hash(level, b, e); }
89
94 int compare(collate_level level, const string_type& l, const string_type& r) const
95 {
96 return do_compare(level, l.data(), l.data() + l.size(), r.data(), r.data() + r.size());
97 }
98
102 long hash(collate_level level, const string_type& s) const
103 {
104 return do_hash(level, s.data(), s.data() + s.size());
105 }
106
115 {
116 return do_transform(level, s.data(), s.data() + s.size());
117 }
118
119 protected:
121 collator(size_t refs = 0) : std::collate<CharType>(refs) {}
122
125 int
126 do_compare(const char_type* b1, const char_type* e1, const char_type* b2, const char_type* e2) const override
127 {
128 return do_compare(collate_level::identical, b1, e1, b2, e2);
129 }
130
133 string_type do_transform(const char_type* b, const char_type* e) const override
134 {
136 }
137
140 long do_hash(const char_type* b, const char_type* e) const override
141 {
142 return do_hash(collate_level::identical, b, e);
143 }
144
147 virtual int do_compare(collate_level level,
148 const char_type* b1,
149 const char_type* e1,
150 const char_type* b2,
151 const char_type* e2) const = 0;
152
154 virtual string_type do_transform(collate_level level, const char_type* b, const char_type* e) const = 0;
156 virtual long do_hash(collate_level level, const char_type* b, const char_type* e) const = 0;
157 };
158
169 template<typename CharType, collate_level default_level = collate_level::identical>
170 struct comparator {
171 public:
175 comparator(const std::locale& l = std::locale(), collate_level level = default_level) :
176 locale_(l), level_(level)
177 {}
178
180 bool operator()(const std::basic_string<CharType>& left, const std::basic_string<CharType>& right) const
181 {
182 return std::use_facet<collator<CharType>>(locale_).compare(level_, left, right) < 0;
183 }
184
185 private:
186 std::locale locale_;
187 collate_level level_;
188 };
189
191}} // namespace boost::locale
192
193#ifdef BOOST_MSVC
194# pragma warning(pop)
195#endif
196
201
202#endif
Collation facet.
Definition: collator.hpp:49
long hash(collate_level level, const string_type &s) const
Definition: collator.hpp:102
std::basic_string< CharType > string_type
Type of string used with this facet.
Definition: collator.hpp:54
int compare(collate_level level, const char_type *b1, const char_type *e1, const char_type *b2, const char_type *e2) const
Definition: collator.hpp:60
collator(size_t refs=0)
constructor of the collator object
Definition: collator.hpp:121
CharType char_type
Type of the underlying character.
Definition: collator.hpp:52
virtual int do_compare(collate_level level, const char_type *b1, const char_type *e1, const char_type *b2, const char_type *e2) const =0
int do_compare(const char_type *b1, const char_type *e1, const char_type *b2, const char_type *e2) const override
Definition: collator.hpp:126
virtual long do_hash(collate_level level, const char_type *b, const char_type *e) const =0
Actual function that calculates hash. For details see hash member function. Can be overridden.
string_type do_transform(const char_type *b, const char_type *e) const override
Definition: collator.hpp:133
long do_hash(const char_type *b, const char_type *e) const override
Definition: collator.hpp:140
long hash(collate_level level, const char_type *b, const char_type *e) const
Definition: collator.hpp:88
int compare(collate_level level, const string_type &l, const string_type &r) const
Definition: collator.hpp:94
virtual string_type do_transform(collate_level level, const char_type *b, const char_type *e) const =0
Actual function that performs transformation. For details see transform member function....
string_type transform(collate_level level, const char_type *b, const char_type *e) const
Definition: collator.hpp:78
string_type transform(collate_level level, const string_type &s) const
Definition: collator.hpp:114
collate_level
Unicode collation level types.
Definition: collator.hpp:26
@ secondary
2nd collation level: letters and accents
@ primary
1st collation level: base letters
@ quaternary
4th collation level: letters, accents, case and punctuation
@ tertiary
3rd collation level: letters, accents and case
@ identical
identical collation level: include code-point comparison
This class can be used in STL algorithms and containers for comparison of strings with a level other ...
Definition: collator.hpp:170
comparator(const std::locale &l=std::locale(), collate_level level=default_level)
Definition: collator.hpp:175
bool operator()(const std::basic_string< CharType > &left, const std::basic_string< CharType > &right) const
Compare two strings – equivalent to return left < right according to collation rules.
Definition: collator.hpp:180