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 <boost/locale/detail/facet_id.hpp>
12#include <locale>
13
14#ifdef BOOST_MSVC
15# pragma warning(push)
16# pragma warning(disable : 4275 4251 4231 4660)
17#endif
18
19namespace boost { namespace locale {
20
25
27 enum class collate_level {
28 primary = 0,
29 secondary = 1,
30 tertiary = 2,
31 quaternary = 3,
32 identical = 4
33 };
34
35 class BOOST_DEPRECATED("Use collate_level") collator_base {
36 public:
37 using level_type = collate_level;
38 static constexpr auto primary = collate_level::primary;
39 static constexpr auto secondary = collate_level::secondary;
40 static constexpr auto tertiary = collate_level::tertiary;
41 static constexpr auto quaternary = collate_level::quaternary;
42 static constexpr auto identical = collate_level::identical;
43 };
44
48 template<typename CharType>
49 class BOOST_SYMBOL_VISIBLE collator : public std::locale::facet, public detail::facet_id<collator<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
71 int compare(const char_type* b1, const char_type* e1, const char_type* b2, const char_type* e2) const
72 {
73 return compare(collate_level::identical, b1, e1, b2, e2);
74 }
75
85 string_type transform(collate_level level, const char_type* b, const char_type* e) const
86 {
87 return do_transform(level, b, e);
88 }
89
92 string_type transform(const char_type* b, const char_type* e) const
93 {
94 return transform(collate_level::identical, b, e);
95 }
96
102 long hash(collate_level level, const char_type* b, const char_type* e) const { return do_hash(level, b, e); }
103
106 long hash(const char_type* b, const char_type* e) const { return hash(collate_level::identical, b, e); }
107
112 int compare(collate_level level, const string_type& l, const string_type& r) const
113 {
114 return do_compare(level, l.data(), l.data() + l.size(), r.data(), r.data() + r.size());
115 }
116
120 long hash(collate_level level, const string_type& s) const
121 {
122 return do_hash(level, s.data(), s.data() + s.size());
123 }
124
133 {
134 return do_transform(level, s.data(), s.data() + s.size());
135 }
136
137 protected:
139 collator(size_t refs = 0) : std::locale::facet(refs) {}
140
143 virtual int do_compare(collate_level level,
144 const char_type* b1,
145 const char_type* e1,
146 const char_type* b2,
147 const char_type* e2) const = 0;
148
150 virtual string_type do_transform(collate_level level, const char_type* b, const char_type* e) const = 0;
152 virtual long do_hash(collate_level level, const char_type* b, const char_type* e) const = 0;
153 };
154
165 template<typename CharType, collate_level default_level = collate_level::identical>
166 struct comparator {
167 public:
171 comparator(const std::locale& l = std::locale(), collate_level level = default_level) :
172 locale_(l), collator_(std::use_facet<collator<CharType>>(locale_)), level_(level)
173 {}
174
176 bool operator()(const std::basic_string<CharType>& left, const std::basic_string<CharType>& right) const
177 {
178 return collator_.compare(level_, left, right) < 0;
179 }
180
181 private:
182 std::locale locale_;
183 const collator<CharType>& collator_;
184 collate_level level_;
185 };
186
188}} // namespace boost::locale
189
190#ifdef BOOST_MSVC
191# pragma warning(pop)
192#endif
193
198
199#endif
Collation facet.
Definition: collator.hpp:49
long hash(collate_level level, const string_type &s) const
Definition: collator.hpp:120
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
int compare(const char_type *b1, const char_type *e1, const char_type *b2, const char_type *e2) const
Definition: collator.hpp:71
collator(size_t refs=0)
constructor of the collator object
Definition: collator.hpp:139
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
string_type transform(const char_type *b, const char_type *e) const
Definition: collator.hpp:92
long hash(const char_type *b, const char_type *e) const
Definition: collator.hpp:106
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.
long hash(collate_level level, const char_type *b, const char_type *e) const
Definition: collator.hpp:102
int compare(collate_level level, const string_type &l, const string_type &r) const
Definition: collator.hpp:112
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:85
string_type transform(collate_level level, const string_type &s) const
Definition: collator.hpp:132
collate_level
Unicode collation level types.
Definition: collator.hpp:27
@ 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:166
comparator(const std::locale &l=std::locale(), collate_level level=default_level)
Definition: collator.hpp:171
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:176