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 
18 namespace boost { namespace locale {
19 
20  class info;
21 
26 
28  enum class collate_level {
29  primary = 0,
30  secondary = 1,
31  tertiary = 2,
32  quaternary = 3,
33  identical = 4
34  };
35 
36  class BOOST_DEPRECATED("Use collate_level") collator_base {
37  public:
38  using level_type = collate_level;
39  static constexpr auto primary = collate_level::primary;
40  static constexpr auto secondary = collate_level::secondary;
41  static constexpr auto tertiary = collate_level::tertiary;
42  static constexpr auto quaternary = collate_level::quaternary;
43  static constexpr auto identical = collate_level::identical;
44  };
45 
50  template<typename CharType>
51  class collator : public std::collate<CharType> {
52  public:
54  typedef CharType char_type;
56  typedef std::basic_string<CharType> string_type;
57 
63  const char_type* b1,
64  const char_type* e1,
65  const char_type* b2,
66  const char_type* e2) const
67  {
68  return do_compare(level, b1, e1, b2, e2);
69  }
70 
80  string_type transform(collate_level level, const char_type* b, const char_type* e) const
81  {
82  return do_transform(level, b, e);
83  }
84 
90  long hash(collate_level level, const char_type* b, const char_type* e) const { return do_hash(level, b, e); }
91 
96  int compare(collate_level level, const string_type& l, const string_type& r) const
97  {
98  return do_compare(level, l.data(), l.data() + l.size(), r.data(), r.data() + r.size());
99  }
100 
104  long hash(collate_level level, const string_type& s) const
105  {
106  return do_hash(level, s.data(), s.data() + s.size());
107  }
108 
117  {
118  return do_transform(level, s.data(), s.data() + s.size());
119  }
120 
121  protected:
123  collator(size_t refs = 0) : std::collate<CharType>(refs) {}
124 
127  int
128  do_compare(const char_type* b1, const char_type* e1, const char_type* b2, const char_type* e2) const override
129  {
130  return do_compare(collate_level::identical, b1, e1, b2, e2);
131  }
132 
135  string_type do_transform(const char_type* b, const char_type* e) const override
136  {
138  }
139 
142  long do_hash(const char_type* b, const char_type* e) const override
143  {
144  return do_hash(collate_level::identical, b, e);
145  }
146 
149  virtual int do_compare(collate_level level,
150  const char_type* b1,
151  const char_type* e1,
152  const char_type* b2,
153  const char_type* e2) const = 0;
154 
156  virtual string_type do_transform(collate_level level, const char_type* b, const char_type* e) const = 0;
158  virtual long do_hash(collate_level level, const char_type* b, const char_type* e) const = 0;
159  };
160 
171  template<typename CharType, collate_level default_level = collate_level::identical>
172  struct comparator {
173  public:
177  comparator(const std::locale& l = std::locale(), collate_level level = default_level) :
178  locale_(l), level_(level)
179  {}
180 
182  bool operator()(const std::basic_string<CharType>& left, const std::basic_string<CharType>& right) const
183  {
184  return std::use_facet<collator<CharType>>(locale_).compare(level_, left, right) < 0;
185  }
186 
187  private:
188  std::locale locale_;
189  collate_level level_;
190  };
191 
193 }} // namespace boost::locale
194 
195 #ifdef BOOST_MSVC
196 # pragma warning(pop)
197 #endif
198 
203 
204 #endif
1st collation level: base letters
long do_hash(const char_type *b, const char_type *e) const override
Definition: collator.hpp:142
string_type transform(collate_level level, const char_type *b, const char_type *e) const
Definition: collator.hpp:80
CharType char_type
Type of the underlying character.
Definition: collator.hpp:54
int do_compare(const char_type *b1, const char_type *e1, const char_type *b2, const char_type *e2) const override
Definition: collator.hpp:128
std::basic_string< CharType > string_type
Type of string used with this facet.
Definition: collator.hpp:56
collator(size_t refs=0)
constructor of the collator object
Definition: collator.hpp:123
3rd collation level: letters, accents and case
string_type do_transform(const char_type *b, const char_type *e) const override
Definition: collator.hpp:135
int compare(collate_level level, const string_type &l, const string_type &r) const
Definition: collator.hpp:96
long hash(collate_level level, const char_type *b, const char_type *e) const
Definition: collator.hpp:90
long hash(collate_level level, const string_type &s) const
Definition: collator.hpp:104
string_type transform(collate_level level, const string_type &s) const
Definition: collator.hpp:116
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:62
Collation facet.
Definition: collator.hpp:51
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:182
identical collation level: include code-point comparison
2nd collation level: letters and accents
collate_level
Unicode collation level types.
Definition: collator.hpp:28
4th collation level: letters, accents, case and punctuation
This class can be used in STL algorithms and containers for comparison of strings with a level other ...
Definition: collator.hpp:172
comparator(const std::locale &l=std::locale(), collate_level level=default_level)
Definition: collator.hpp:177