Boost.Locale
collator.hpp
1 //
2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOST_LOCALE_COLLATOR_HPP_INCLUDED
9 #define BOOST_LOCALE_COLLATOR_HPP_INCLUDED
10 
11 #include <boost/locale/config.hpp>
12 #ifdef BOOST_MSVC
13 # pragma warning(push)
14 # pragma warning(disable : 4275 4251 4231 4660)
15 #endif
16 #include <locale>
17 
18 
19 namespace boost {
20 namespace locale {
21 
22  class info;
23 
30 
34 
35  class collator_base {
36  public:
40  typedef enum {
41  primary = 0,
42  secondary = 1,
43  tertiary = 2,
44  quaternary = 3,
45  identical = 4
46  } level_type;
47  };
48 
55  template<typename CharType>
56  class collator :
57  public std::collate<CharType>,
58  public collator_base
59  {
60  public:
64  typedef CharType char_type;
68  typedef std::basic_string<CharType> string_type;
69 
70 
77  int compare(level_type level,
78  char_type const *b1,char_type const *e1,
79  char_type const *b2,char_type const *e2) const
80  {
81  return do_compare(level,b1,e1,b2,e2);
82  }
94  string_type transform(level_type level,char_type const *b,char_type const *e) const
95  {
96  return do_transform(level,b,e);
97  }
98 
106  long hash(level_type level,char_type const *b,char_type const *e) const
107  {
108  return do_hash(level,b,e);
109  }
110 
118  int compare(level_type level,string_type const &l,string_type const &r) const
119  {
120  return do_compare(level,l.data(),l.data()+l.size(),r.data(),r.data()+r.size());
121  }
122 
128 
129  long hash(level_type level,string_type const &s) const
130  {
131  return do_hash(level,s.data(),s.data()+s.size());
132  }
143  {
144  return do_transform(level,s.data(),s.data()+s.size());
145  }
146 
147  protected:
148 
152  collator(size_t refs = 0) : std::collate<CharType>(refs)
153  {
154  }
155 
156  virtual ~collator()
157  {
158  }
159 
164  virtual int do_compare( char_type const *b1,char_type const *e1,
165  char_type const *b2,char_type const *e2) const
166  {
167  return do_compare(identical,b1,e1,b2,e2);
168  }
173  virtual string_type do_transform(char_type const *b,char_type const *e) const
174  {
175  return do_transform(identical,b,e);
176  }
181  virtual long do_hash(char_type const *b,char_type const *e) const
182  {
183  return do_hash(identical,b,e);
184  }
185 
189  virtual int do_compare( level_type level,
190  char_type const *b1,char_type const *e1,
191  char_type const *b2,char_type const *e2) const = 0;
195  virtual string_type do_transform(level_type level,char_type const *b,char_type const *e) const = 0;
199  virtual long do_hash(level_type level,char_type const *b,char_type const *e) const = 0;
200 
201 
202  };
203 
216  template<typename CharType,collator_base::level_type default_level = collator_base::identical>
217  struct comparator
218  {
219  public:
225  comparator(std::locale const &l=std::locale(),collator_base::level_type level=default_level) :
226  locale_(l),
227  level_(level)
228  {
229  }
230 
234  bool operator()(std::basic_string<CharType> const &left,std::basic_string<CharType> const &right) const
235  {
236  return std::use_facet<collator<CharType> >(locale_).compare(level_,left,right) < 0;
237  }
238  private:
239  std::locale locale_;
241  };
242 
243 
247 
248  } // locale
249 } // boost
250 
251 #ifdef BOOST_MSVC
252 #pragma warning(pop)
253 #endif
254 
255 
256 #endif
257 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
a facet that holds general information about locale
Definition: info.hpp:27
1st collation level: base letters
Definition: collator.hpp:41
2nd collation level: letters and accents
Definition: collator.hpp:42
virtual int do_compare(char_type const *b1, char_type const *e1, char_type const *b2, char_type const *e2) const
Definition: collator.hpp:164
level_type
Definition: collator.hpp:40
CharType char_type
Definition: collator.hpp:64
identical collation level: include code-point comparison
Definition: collator.hpp:45
long hash(level_type level, char_type const *b, char_type const *e) const
Definition: collator.hpp:106
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:77
std::basic_string< CharType > string_type
Definition: collator.hpp:68
a base class that includes collation level flags
Definition: collator.hpp:35
collator(size_t refs=0)
Definition: collator.hpp:152
virtual string_type do_transform(char_type const *b, char_type const *e) const
Definition: collator.hpp:173
long hash(level_type level, string_type const &s) const
Definition: collator.hpp:129
int compare(level_type level, string_type const &l, string_type const &r) const
Definition: collator.hpp:118
string_type transform(level_type level, char_type const *b, char_type const *e) const
Definition: collator.hpp:94
bool operator()(std::basic_string< CharType > const &left, std::basic_string< CharType > const &right) const
Definition: collator.hpp:234
4th collation level: letters, accents, case and punctuation
Definition: collator.hpp:44
Collation facet.
Definition: collator.hpp:56
string_type transform(level_type level, string_type const &s) const
Definition: collator.hpp:142
virtual long do_hash(char_type const *b, char_type const *e) const
Definition: collator.hpp:181
3rd collation level: letters, accents and case
Definition: collator.hpp:43
comparator(std::locale const &l=std::locale(), collator_base::level_type level=default_level)
Definition: collator.hpp:225
This class can be used in STL algorithms and containers for comparison of strings with a level other ...
Definition: collator.hpp:217