source: NonGTP/Boost/boost/algorithm/string/detail/classification.hpp @ 857

Revision 857, 6.5 KB checked in by igarcia, 18 years ago (diff)
Line 
1//  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
10#ifndef BOOST_STRING_CLASSIFICATION_DETAIL_HPP
11#define BOOST_STRING_CLASSIFICATION_DETAIL_HPP
12
13#include <boost/algorithm/string/config.hpp>
14#include <algorithm>
15#include <functional>
16#include <locale>
17#include <set>
18
19#include <boost/range/begin.hpp>
20#include <boost/range/end.hpp>
21
22#include <boost/algorithm/string/predicate_facade.hpp>
23#include <boost/type_traits/remove_const.hpp>
24
25namespace boost {
26    namespace algorithm {
27        namespace detail {
28
29//  classification functors -----------------------------------------------//
30
31            // is_classified functor
32            struct is_classifiedF :
33                public predicate_facade<is_classifiedF>
34            {
35                // Boost.Lambda support
36                template <class Args> struct sig { typedef bool type; };
37
38                // Constructor from a locale
39                is_classifiedF(std::ctype_base::mask Type, std::locale const & Loc = std::locale()) :
40                    m_Type(Type), m_Locale(Loc) {}
41
42                // Operation
43                template<typename CharT>
44                bool operator()( CharT Ch ) const
45                {
46                    return std::use_facet< std::ctype<CharT> >(m_Locale).is( m_Type, Ch );
47                }
48
49                #if defined(__BORLANDC__) && (__BORLANDC__ >= 0x560) && (__BORLANDC__ <= 0x564) && !defined(_USE_OLD_RW_STL)
50                    template<>
51                    bool operator()( char const Ch ) const
52                    {
53                        return std::use_facet< std::ctype<char> >(m_Locale).is( m_Type, Ch );
54                    }
55                #endif
56
57            private:
58                const std::ctype_base::mask m_Type;
59                const std::locale m_Locale;
60            };
61
62            // is_any_of functor
63            /*
64                returns true if the value is from the specified set
65            */
66            template<typename CharT>
67            struct is_any_ofF :
68                public predicate_facade<is_any_ofF<CharT> >
69            {
70                // Boost.Lambda support
71                template <class Args> struct sig { typedef bool type; };
72
73                // Constructor
74                template<typename RangeT>
75                is_any_ofF( const RangeT& Range ) :
76                    m_Set( begin(Range), end(Range) ) {}
77
78                // Operation
79                template<typename Char2T>
80                bool operator()( Char2T Ch ) const
81                {
82                    return m_Set.find(Ch)!=m_Set.end();
83                }
84
85            private:
86                // set cannot operate on const value-type
87                typedef typename remove_const<CharT>::type set_value_type;
88                std::set<set_value_type> m_Set;
89            };
90
91            // is_from_range functor
92            /*
93                returns true if the value is from the specified range.
94                (i.e. x>=From && x>=To)
95            */
96            template<typename CharT>
97            struct is_from_rangeF :
98                public predicate_facade< is_from_rangeF<CharT> >
99            {
100                // Boost.Lambda support
101                template <class Args> struct sig { typedef bool type; };
102
103                // Constructor
104                is_from_rangeF( CharT From, CharT To ) : m_From(From), m_To(To) {}
105
106                // Operation
107                template<typename Char2T>
108                bool operator()( Char2T Ch ) const
109                {
110                    return ( m_From <= Ch ) && ( Ch <= m_To );
111                }
112
113            private:
114                CharT m_From;
115                CharT m_To;
116            };
117
118            // class_and composition predicate
119            template<typename Pred1T, typename Pred2T>
120            struct pred_andF :
121                public predicate_facade< pred_andF<Pred1T,Pred2T> >
122            {
123            public:
124
125                // Boost.Lambda support
126                template <class Args> struct sig { typedef bool type; };
127
128                // Constructor
129                pred_andF( Pred1T Pred1, Pred2T Pred2 ) :
130                    m_Pred1(Pred1), m_Pred2(Pred2) {}
131
132                // Operation
133                template<typename CharT>
134                bool operator()( CharT Ch ) const
135                {
136                    return m_Pred1(Ch) && m_Pred2(Ch);
137                }
138
139            private:
140                Pred1T m_Pred1;
141                Pred2T m_Pred2;
142            };
143
144            // class_or composition predicate
145            template<typename Pred1T, typename Pred2T>
146            struct pred_orF :
147                public predicate_facade< pred_orF<Pred1T,Pred2T> >
148            {
149            public:
150                // Boost.Lambda support
151                template <class Args> struct sig { typedef bool type; };
152
153                // Constructor
154                pred_orF( Pred1T Pred1, Pred2T Pred2 ) :
155                    m_Pred1(Pred1), m_Pred2(Pred2) {}
156
157                // Operation
158                template<typename CharT>
159                bool operator()( CharT Ch ) const
160                {
161                    return m_Pred1(Ch) || m_Pred2(Ch);
162                }
163
164            private:
165                Pred1T m_Pred1;
166                Pred2T m_Pred2;
167            };
168
169            // class_not composition predicate
170            template< typename PredT >
171            struct pred_notF :
172                public predicate_facade< pred_notF<PredT> >
173            {
174            public:
175                // Boost.Lambda support
176                template <class Args> struct sig { typedef bool type; };
177
178                // Constructor
179                pred_notF( PredT Pred ) : m_Pred(Pred) {}
180
181                // Operation
182                template<typename CharT>
183                bool operator()( CharT Ch ) const
184                {
185                    return !m_Pred(Ch);
186                }
187
188            private:
189                PredT m_Pred;
190            };
191
192        } // namespace detail
193    } // namespace algorithm
194} // namespace boost
195
196
197#endif  // BOOST_STRING_CLASSIFICATION_DETAIL_HPP
Note: See TracBrowser for help on using the repository browser.