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 {
19 namespace locale {
20 
21  class info;
22 
29 
33 
34  class collator_base {
35  public:
39  typedef enum {
40  primary = 0,
41  secondary = 1,
42  tertiary = 2,
43  quaternary = 3,
44  identical = 4
45  } level_type;
46  };
47 
54  template<typename CharType>
55  class collator :
56  public std::collate<CharType>,
57  public collator_base
58  {
59  public:
63  typedef CharType char_type;
67  typedef std::basic_string<CharType> string_type;
68 
69 
76  int compare(level_type level,
77  char_type const *b1,char_type const *e1,
78  char_type const *b2,char_type const *e2) const
79  {
80  return do_compare(level,b1,e1,b2,e2);
81  }
93  string_type transform(level_type level,char_type const *b,char_type const *e) const
94  {
95  return do_transform(level,b,e);
96  }
97 
105  long hash(level_type level,char_type const *b,char_type const *e) const
106  {
107  return do_hash(level,b,e);
108  }
109 
117  int compare(level_type level,string_type const &l,string_type const &r) const
118  {
119  return do_compare(level,l.data(),l.data()+l.size(),r.data(),r.data()+r.size());
120  }
121 
127 
128  long hash(level_type level,string_type const &s) const
129  {
130  return do_hash(level,s.data(),s.data()+s.size());
131  }
142  {
143  return do_transform(level,s.data(),s.data()+s.size());
144  }
145 
146  protected:
147 
151  collator(size_t refs = 0) : std::collate<CharType>(refs)
152  {
153  }
154 
159  int do_compare( char_type const *b1,char_type const *e1,
160  char_type const *b2,char_type const *e2) const BOOST_OVERRIDE
161  {
162  return do_compare(identical,b1,e1,b2,e2);
163  }
168  string_type do_transform(char_type const *b,char_type const *e) const BOOST_OVERRIDE
169  {
170  return do_transform(identical,b,e);
171  }
176  long do_hash(char_type const *b,char_type const *e) const BOOST_OVERRIDE
177  {
178  return do_hash(identical,b,e);
179  }
180 
184  virtual int do_compare( level_type level,
185  char_type const *b1,char_type const *e1,
186  char_type const *b2,char_type const *e2) const = 0;
190  virtual string_type do_transform(level_type level,char_type const *b,char_type const *e) const = 0;
194  virtual long do_hash(level_type level,char_type const *b,char_type const *e) const = 0;
195 
196 
197  };
198 
211  template<typename CharType,collator_base::level_type default_level = collator_base::identical>
212  struct comparator
213  {
214  public:
220  comparator(std::locale const &l=std::locale(),collator_base::level_type level=default_level) :
221  locale_(l),
222  level_(level)
223  {
224  }
225 
229  bool operator()(std::basic_string<CharType> const &left,std::basic_string<CharType> const &right) const
230  {
231  return std::use_facet<collator<CharType> >(locale_).compare(level_,left,right) < 0;
232  }
233  private:
234  std::locale locale_;
236  };
237 
238 
242 
243  } // locale
244 } // boost
245 
246 #ifdef BOOST_MSVC
247 #pragma warning(pop)
248 #endif
249 
250 
251 #endif
252 
int do_compare(char_type const *b1, char_type const *e1, char_type const *b2, char_type const *e2) const BOOST_OVERRIDE
Definition: collator.hpp:159
a facet that holds general information about locale
Definition: info.hpp:27
int compare(level_type level, char_type const *b1, char_type const *e1, char_type const *b2, char_type const *e2) const
Definition: collator.hpp:76
1st collation level: base letters
Definition: collator.hpp:40
2nd collation level: letters and accents
Definition: collator.hpp:41
level_type
Definition: collator.hpp:39
CharType char_type
Definition: collator.hpp:63
bool operator()(std::basic_string< CharType > const &left, std::basic_string< CharType > const &right) const
Definition: collator.hpp:229
long hash(level_type level, char_type const *b, char_type const *e) const
Definition: collator.hpp:105
identical collation level: include code-point comparison
Definition: collator.hpp:44
std::basic_string< CharType > string_type
Definition: collator.hpp:67
a base class that includes collation level flags
Definition: collator.hpp:34
string_type do_transform(char_type const *b, char_type const *e) const BOOST_OVERRIDE
Definition: collator.hpp:168
string_type transform(level_type level, char_type const *b, char_type const *e) const
Definition: collator.hpp:93
collator(size_t refs=0)
Definition: collator.hpp:151
4th collation level: letters, accents, case and punctuation
Definition: collator.hpp:43
int compare(level_type level, string_type const &l, string_type const &r) const
Definition: collator.hpp:117
long do_hash(char_type const *b, char_type const *e) const BOOST_OVERRIDE
Definition: collator.hpp:176
Collation facet.
Definition: collator.hpp:55
3rd collation level: letters, accents and case
Definition: collator.hpp:42
comparator(std::locale const &l=std::locale(), collator_base::level_type level=default_level)
Definition: collator.hpp:220
string_type transform(level_type level, string_type const &s) const
Definition: collator.hpp:141
This class can be used in STL algorithms and containers for comparison of strings with a level other ...
Definition: collator.hpp:212
long hash(level_type level, string_type const &s) const
Definition: collator.hpp:128