| 1 |
tim |
515 |
// Boost string_algo library classification.hpp header file ---------------------------// |
| 2 |
|
|
|
| 3 |
|
|
// Copyright Pavol Droba 2002-2003. Use, modification and |
| 4 |
|
|
// distribution is subject to the Boost Software License, Version |
| 5 |
|
|
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 6 |
|
|
// http://www.boost.org/LICENSE_1_0.txt) |
| 7 |
|
|
|
| 8 |
|
|
// See http://www.boost.org for updates, documentation, and revision history. |
| 9 |
|
|
#ifndef UTILS_PREDICATE_HPP |
| 10 |
|
|
#define UTILS_PREDICATE_HPP |
| 11 |
|
|
#include <locale> |
| 12 |
|
|
|
| 13 |
|
|
namespace oopse { |
| 14 |
|
|
|
| 15 |
|
|
template<typename Derived> |
| 16 |
|
|
struct PredFacade{}; |
| 17 |
|
|
|
| 18 |
|
|
struct CharClassification: public PredFacade<CharClassification> { |
| 19 |
|
|
|
| 20 |
|
|
CharClassification(std::ctype_base::mask type, std::locale const & loc = std::locale()) : |
| 21 |
|
|
type_(type), loc_(loc) {} |
| 22 |
|
|
|
| 23 |
|
|
template<typename CharT> |
| 24 |
|
|
bool operator()( CharT c ) const { |
| 25 |
|
|
return std::use_facet< std::ctype<CharT> >(loc_).is( type_, c ); |
| 26 |
|
|
} |
| 27 |
|
|
|
| 28 |
|
|
private: |
| 29 |
|
|
const std::ctype_base::mask type_; |
| 30 |
|
|
const std::locale loc_; |
| 31 |
|
|
}; |
| 32 |
|
|
|
| 33 |
|
|
template<typename CharT> |
| 34 |
|
|
struct FromRangeFunctor : public PredFacade< FromRangeFunctor<CharT> > { |
| 35 |
|
|
|
| 36 |
|
|
FromRangeFunctor( CharT from, CharT to ) : from_(from), to_(to) {} |
| 37 |
|
|
|
| 38 |
|
|
template<typename Char2T> |
| 39 |
|
|
bool operator()( Char2T c ) const { |
| 40 |
|
|
return ( from_ <= c ) && ( c <= to_ ); |
| 41 |
|
|
} |
| 42 |
|
|
|
| 43 |
|
|
private: |
| 44 |
|
|
CharT from_; |
| 45 |
|
|
CharT to_; |
| 46 |
|
|
}; |
| 47 |
|
|
|
| 48 |
|
|
template<typename Pred1T, typename Pred2T> |
| 49 |
|
|
struct PredAndFunctor : public PredFacade< PredAndFunctor<Pred1T,Pred2T> > { |
| 50 |
|
|
public: |
| 51 |
|
|
|
| 52 |
|
|
PredAndFunctor( Pred1T pred1, Pred2T pred2 ) : pred1_(pred1), pred2_(pred2) {} |
| 53 |
|
|
|
| 54 |
|
|
template<typename CharT> |
| 55 |
|
|
bool operator()( CharT c ) const { |
| 56 |
|
|
return pred1_(c) && pred2_(c); |
| 57 |
|
|
} |
| 58 |
|
|
|
| 59 |
|
|
private: |
| 60 |
|
|
Pred1T pred1_; |
| 61 |
|
|
Pred2T pred2_; |
| 62 |
|
|
}; |
| 63 |
|
|
|
| 64 |
|
|
template<typename Pred1T, typename Pred2T> |
| 65 |
|
|
struct PredOrFunctor : public PredFacade< PredOrFunctor<Pred1T,Pred2T> > { |
| 66 |
|
|
public: |
| 67 |
|
|
|
| 68 |
|
|
PredOrFunctor( Pred1T pred1, Pred2T pred2 ) : pred1_(pred1), pred2_(pred2) {} |
| 69 |
|
|
|
| 70 |
|
|
template<typename CharT> |
| 71 |
|
|
bool operator()( CharT c ) const { |
| 72 |
|
|
return pred1_(c) || pred2_(c); |
| 73 |
|
|
} |
| 74 |
|
|
|
| 75 |
|
|
private: |
| 76 |
|
|
Pred1T pred1_; |
| 77 |
|
|
Pred2T pred2_; |
| 78 |
|
|
}; |
| 79 |
|
|
|
| 80 |
|
|
template< typename PredT > |
| 81 |
|
|
struct PredNotFunctor : public PredFacade< PredNotFunctor<PredT> > { |
| 82 |
|
|
public: |
| 83 |
|
|
|
| 84 |
|
|
PredNotFunctor( PredT pred ) : pred_(pred) {} |
| 85 |
|
|
|
| 86 |
|
|
template<typename CharT> |
| 87 |
|
|
bool operator()( CharT c ) const { |
| 88 |
|
|
return !pred_(c); |
| 89 |
|
|
} |
| 90 |
|
|
|
| 91 |
|
|
private: |
| 92 |
|
|
PredT pred_; |
| 93 |
|
|
}; |
| 94 |
|
|
|
| 95 |
|
|
inline CharClassification isSpace(const std::locale& loc=std::locale()){ |
| 96 |
|
|
return CharClassification(std::ctype_base::space, loc); |
| 97 |
|
|
} |
| 98 |
|
|
|
| 99 |
|
|
inline CharClassification isAlnum(const std::locale& loc=std::locale()){ |
| 100 |
|
|
return CharClassification(std::ctype_base::alnum, loc); |
| 101 |
|
|
} |
| 102 |
|
|
|
| 103 |
|
|
inline CharClassification isAlpha(const std::locale& loc=std::locale()){ |
| 104 |
|
|
return CharClassification(std::ctype_base::alpha, loc); |
| 105 |
|
|
} |
| 106 |
|
|
|
| 107 |
|
|
inline CharClassification isCntrl(const std::locale& loc=std::locale()) { |
| 108 |
|
|
return CharClassification(std::ctype_base::cntrl, loc); |
| 109 |
|
|
} |
| 110 |
|
|
|
| 111 |
|
|
inline CharClassification isDigit(const std::locale& loc=std::locale()) { |
| 112 |
|
|
return CharClassification(std::ctype_base::digit, loc); |
| 113 |
|
|
} |
| 114 |
|
|
|
| 115 |
|
|
inline CharClassification isGraph(const std::locale& loc=std::locale()) { |
| 116 |
|
|
return CharClassification(std::ctype_base::graph, loc); |
| 117 |
|
|
} |
| 118 |
|
|
|
| 119 |
|
|
inline CharClassification isLower(const std::locale& loc=std::locale()) { |
| 120 |
|
|
return CharClassification(std::ctype_base::lower, loc); |
| 121 |
|
|
} |
| 122 |
|
|
|
| 123 |
|
|
inline CharClassification isPrint(const std::locale& loc=std::locale()) { |
| 124 |
|
|
return CharClassification(std::ctype_base::print, loc); |
| 125 |
|
|
} |
| 126 |
|
|
|
| 127 |
|
|
inline CharClassification isPunct(const std::locale& loc=std::locale()) { |
| 128 |
|
|
return CharClassification(std::ctype_base::punct, loc); |
| 129 |
|
|
} |
| 130 |
|
|
|
| 131 |
|
|
inline CharClassification isUpper(const std::locale& loc=std::locale()) { |
| 132 |
|
|
return CharClassification(std::ctype_base::upper, loc); |
| 133 |
|
|
} |
| 134 |
|
|
|
| 135 |
|
|
inline CharClassification isXDigit(const std::locale& loc=std::locale()) { |
| 136 |
|
|
return CharClassification(std::ctype_base::xdigit, loc); |
| 137 |
|
|
} |
| 138 |
|
|
|
| 139 |
|
|
|
| 140 |
|
|
template<typename CharT> |
| 141 |
|
|
inline FromRangeFunctor<CharT> isFromRange(CharT from, CharT to) { |
| 142 |
|
|
return FromRangeFunctor<CharT>(from, to); |
| 143 |
|
|
} |
| 144 |
|
|
|
| 145 |
|
|
template<typename Pred1T, typename Pred2T> |
| 146 |
|
|
inline PredAndFunctor<Pred1T, Pred2T> |
| 147 |
|
|
operator&&(const PredFacade<Pred1T>& Pred1, const PredFacade<Pred2T>& Pred2) { |
| 148 |
|
|
// Doing the static_cast with the pointer instead of the reference |
| 149 |
|
|
// is a workaround for some compilers which have problems with |
| 150 |
|
|
// static_cast's of template references, i.e. CW8. /grafik/ |
| 151 |
|
|
return PredAndFunctor<Pred1T,Pred2T>( |
| 152 |
|
|
*static_cast<const Pred1T*>(&Pred1), |
| 153 |
|
|
*static_cast<const Pred2T*>(&Pred2) ); |
| 154 |
|
|
} |
| 155 |
|
|
|
| 156 |
|
|
|
| 157 |
|
|
template<typename Pred1T, typename Pred2T> |
| 158 |
|
|
inline PredOrFunctor<Pred1T, Pred2T> |
| 159 |
|
|
operator||( const PredFacade<Pred1T>& Pred1, const PredFacade<Pred2T>& Pred2 ) { |
| 160 |
|
|
// Doing the static_cast with the pointer instead of the reference |
| 161 |
|
|
// is a workaround for some compilers which have problems with |
| 162 |
|
|
// static_cast's of template references, i.e. CW8. /grafik/ |
| 163 |
|
|
return detail::PredOrFunctor<Pred1T,Pred2T>( |
| 164 |
|
|
*static_cast<const Pred1T*>(&Pred1), |
| 165 |
|
|
*static_cast<const Pred2T*>(&Pred2)); |
| 166 |
|
|
} |
| 167 |
|
|
|
| 168 |
|
|
template<typename PredT> |
| 169 |
|
|
inline PredNotFunctor<PredT> |
| 170 |
|
|
operator!( const PredFacade<PredT>& Pred ) { |
| 171 |
|
|
// Doing the static_cast with the pointer instead of the reference |
| 172 |
|
|
// is a workaround for some compilers which have problems with |
| 173 |
|
|
// static_cast's of template references, i.e. CW8. /grafik/ |
| 174 |
|
|
return PredNotFunctor<PredT>(*static_cast<const PredT*>(&Pred)); |
| 175 |
|
|
} |
| 176 |
|
|
|
| 177 |
|
|
} |
| 178 |
|
|
|
| 179 |
|
|
#endif |