collator.h
//
// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOSTER_LOCALE_COLLATOR_H_INCLUDED
#define BOOSTER_LOCALE_COLLATOR_H_INCLUDED
#include <booster/config.h>
#ifdef BOOSTER_MSVC
# pragma warning(push)
# pragma warning(disable : 4275 4251 4231 4660)
#endif
#include <locale>
namespace booster {
namespace locale {
class info;
class collator_base {
public:
typedef enum {
primary = 0,
secondary = 1,
tertiary = 2,
quaternary = 3,
identical = 4
} level_type;
};
template<typename CharType>
class collator :
public std::collate<CharType>,
public collator_base
{
public:
typedef CharType char_type;
typedef std::basic_string<CharType> string_type;
int compare(level_type level,
char_type const *b1,char_type const *e1,
char_type const *b2,char_type const *e2) const
{
return do_compare(level,b1,e1,b2,e2);
}
string_type transform(level_type level,char_type const *b,char_type const *e) const
{
return do_transform(level,b,e);
}
long hash(level_type level,char_type const *b,char_type const *e) const
{
return do_hash(level,b,e);
}
int compare(level_type level,string_type const &l,string_type const &r) const
{
return do_compare(level,l.data(),l.data()+l.size(),r.data(),r.data()+r.size());
}
long hash(level_type level,string_type const &s) const
{
return do_hash(level,s.data(),s.data()+s.size());
}
string_type transform(level_type level,string_type const &s) const
{
return do_transform(level,s.data(),s.data()+s.size());
}
protected:
collator(size_t refs = 0) : std::collate<CharType>(refs)
{
}
virtual ~collator()
{
}
virtual int do_compare( char_type const *b1,char_type const *e1,
char_type const *b2,char_type const *e2) const
{
return do_compare(identical,b1,e1,b2,e2);
}
virtual string_type do_transform(char_type const *b,char_type const *e) const
{
return do_transform(identical,b,e);
}
virtual long do_hash(char_type const *b,char_type const *e) const
{
return do_hash(identical,b,e);
}
virtual int do_compare( level_type level,
char_type const *b1,char_type const *e1,
char_type const *b2,char_type const *e2) const = 0;
virtual string_type do_transform(level_type level,char_type const *b,char_type const *e) const = 0;
virtual long do_hash(level_type level,char_type const *b,char_type const *e) const = 0;
};
template<typename CharType,collator_base::level_type default_level = collator_base::identical>
struct comparator
{
public:
comparator(std::locale const &l=std::locale(),collator_base::level_type level=default_level) :
locale_(l),
level_(level)
{
}
bool operator()(std::basic_string<CharType> const &left,std::basic_string<CharType> const &right) const
{
return std::use_facet<collator<CharType> >(locale_).compare(level_,left,right) < 0;
}
private:
std::locale locale_;
collator_base::level_type level_;
};
} // locale
} // boost
#ifdef BOOSTER_MSVC
#pragma warning(pop)
#endif
#endif
// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
booster::locale::collator_base
a base class that includes collation level flags
Definition: collator.h:35
booster::locale::collator::do_compare
virtual int do_compare(char_type const *b1, char_type const *e1, char_type const *b2, char_type const *e2) const
Definition: collator.h:164
booster::locale::collator_base::quaternary
4th collation level: letters, accents, case and punctuation
Definition: collator.h:44
booster::locale::collator_base::secondary
2nd collation level: letters and accents
Definition: collator.h:42
booster::locale::collator_base::identical
identical collation level: include code-point comparison
Definition: collator.h:45
booster::locale::collator::hash
long hash(level_type level, string_type const &s) const
Definition: collator.h:129
booster::locale::comparator::comparator
comparator(std::locale const &l=std::locale(), collator_base::level_type level=default_level)
Definition: collator.h:225
booster::locale::collator::char_type
CharType char_type
Definition: collator.h:64
booster::locale::collator::compare
int compare(level_type level, char_type const *b1, char_type const *e1, char_type const *b2, char_type const *e2) const
Definition: collator.h:77
booster::locale::collator::transform
string_type transform(level_type level, string_type const &s) const
Definition: collator.h:142
booster::locale::collator_base::primary
1st collation level: base letters
Definition: collator.h:41
booster::locale::collator::string_type
std::basic_string< CharType > string_type
Definition: collator.h:68
booster::locale::collator
Collation facet.
Definition: collator.h:56
booster::locale::collator_base::tertiary
3rd collation level: letters, accents and case
Definition: collator.h:43
booster::locale::collator::transform
string_type transform(level_type level, char_type const *b, char_type const *e) const
Definition: collator.h:94
booster::locale::collator::do_transform
virtual string_type do_transform(char_type const *b, char_type const *e) const
Definition: collator.h:173
booster::locale::comparator
This class can be used in STL algorithms and containers for comparison of strings with a level other ...
Definition: collator.h:217
booster::locale::collator::collator
collator(size_t refs=0)
Definition: collator.h:152
booster::locale::comparator::operator()
bool operator()(std::basic_string< CharType > const &left, std::basic_string< CharType > const &right) const
Definition: collator.h:234
booster::locale::collator::compare
int compare(level_type level, string_type const &l, string_type const &r) const
Definition: collator.h:118
booster::locale::collator::hash
long hash(level_type level, char_type const *b, char_type const *e) const
Definition: collator.h:106
booster::locale::collator::do_hash
virtual long do_hash(char_type const *b, char_type const *e) const
Definition: collator.h:181
booster::locale::collator_base::level_type
level_type
Definition: collator.h:40