• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  Boost string_algo library classification.hpp header file  ---------------------------//
2 
3 //  Copyright Pavol Droba 2002-2003.
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 //    (See accompanying file LICENSE_1_0.txt or copy at
7 //          http://www.boost.org/LICENSE_1_0.txt)
8 
9 //  See http://www.boost.org/ for updates, documentation, and revision history.
10 
11 #ifndef BOOST_STRING_CLASSIFICATION_HPP
12 #define BOOST_STRING_CLASSIFICATION_HPP
13 
14 #include <algorithm>
15 #include <locale>
16 #include <boost/range/value_type.hpp>
17 #include <boost/range/as_literal.hpp>
18 #include <boost/algorithm/string/detail/classification.hpp>
19 #include <boost/algorithm/string/predicate_facade.hpp>
20 
21 
22 /*! \file
23     Classification predicates are included in the library to give
24     some more convenience when using algorithms like \c trim() and \c all().
25     They wrap functionality of STL classification functions ( e.g. \c std::isspace() )
26     into generic functors.
27 */
28 
29 namespace boost {
30     namespace algorithm {
31 
32 //  classification functor generator -------------------------------------//
33 
34         //! is_classified predicate
35         /*!
36             Construct the \c is_classified predicate. This predicate holds if the input is
37             of specified \c std::ctype category.
38 
39             \param Type A \c std::ctype category
40             \param Loc A locale used for classification
41             \return An instance of the \c is_classified predicate
42         */
43         inline detail::is_classifiedF
is_classified(std::ctype_base::mask Type,const std::locale & Loc=std::locale ())44         is_classified(std::ctype_base::mask Type, const std::locale& Loc=std::locale())
45         {
46             return detail::is_classifiedF(Type, Loc);
47         }
48 
49         //! is_space predicate
50         /*!
51             Construct the \c is_classified predicate for the \c ctype_base::space category.
52 
53             \param Loc A locale used for classification
54             \return An instance of the \c is_classified predicate
55         */
56         inline detail::is_classifiedF
is_space(const std::locale & Loc=std::locale ())57         is_space(const std::locale& Loc=std::locale())
58         {
59             return detail::is_classifiedF(std::ctype_base::space, Loc);
60         }
61 
62         //! is_alnum predicate
63         /*!
64             Construct the \c is_classified predicate for the \c ctype_base::alnum category.
65 
66             \param Loc A locale used for classification
67             \return An instance of the \c is_classified predicate
68         */
69         inline detail::is_classifiedF
is_alnum(const std::locale & Loc=std::locale ())70         is_alnum(const std::locale& Loc=std::locale())
71         {
72             return detail::is_classifiedF(std::ctype_base::alnum, Loc);
73         }
74 
75         //! is_alpha predicate
76         /*!
77             Construct the \c is_classified predicate for the \c ctype_base::alpha category.
78 
79             \param Loc A locale used for classification
80             \return An instance of the \c is_classified predicate
81         */
82         inline detail::is_classifiedF
is_alpha(const std::locale & Loc=std::locale ())83         is_alpha(const std::locale& Loc=std::locale())
84         {
85             return detail::is_classifiedF(std::ctype_base::alpha, Loc);
86         }
87 
88         //! is_cntrl predicate
89         /*!
90             Construct the \c is_classified predicate for the \c ctype_base::cntrl category.
91 
92             \param Loc A locale used for classification
93             \return An instance of the \c is_classified predicate
94         */
95         inline detail::is_classifiedF
is_cntrl(const std::locale & Loc=std::locale ())96         is_cntrl(const std::locale& Loc=std::locale())
97         {
98             return detail::is_classifiedF(std::ctype_base::cntrl, Loc);
99         }
100 
101         //! is_digit predicate
102         /*!
103             Construct the \c is_classified predicate for the \c ctype_base::digit category.
104 
105             \param Loc A locale used for classification
106             \return An instance of the \c is_classified predicate
107         */
108         inline detail::is_classifiedF
is_digit(const std::locale & Loc=std::locale ())109         is_digit(const std::locale& Loc=std::locale())
110         {
111             return detail::is_classifiedF(std::ctype_base::digit, Loc);
112         }
113 
114         //! is_graph predicate
115         /*!
116             Construct the \c is_classified predicate for the \c ctype_base::graph category.
117 
118             \param Loc A locale used for classification
119             \return An instance of the \c is_classified predicate
120         */
121         inline detail::is_classifiedF
is_graph(const std::locale & Loc=std::locale ())122         is_graph(const std::locale& Loc=std::locale())
123         {
124             return detail::is_classifiedF(std::ctype_base::graph, Loc);
125         }
126 
127         //! is_lower predicate
128         /*!
129             Construct the \c is_classified predicate for the \c ctype_base::lower category.
130 
131             \param Loc A locale used for classification
132             \return An instance of \c is_classified predicate
133         */
134         inline detail::is_classifiedF
is_lower(const std::locale & Loc=std::locale ())135         is_lower(const std::locale& Loc=std::locale())
136         {
137             return detail::is_classifiedF(std::ctype_base::lower, Loc);
138         }
139 
140         //! is_print predicate
141         /*!
142             Construct the \c is_classified predicate for the \c ctype_base::print category.
143 
144             \param Loc A locale used for classification
145             \return An instance of the \c is_classified predicate
146         */
147         inline detail::is_classifiedF
is_print(const std::locale & Loc=std::locale ())148         is_print(const std::locale& Loc=std::locale())
149         {
150             return detail::is_classifiedF(std::ctype_base::print, Loc);
151         }
152 
153         //! is_punct predicate
154         /*!
155             Construct the \c is_classified predicate for the \c ctype_base::punct category.
156 
157             \param Loc A locale used for classification
158             \return An instance of the \c is_classified predicate
159         */
160         inline detail::is_classifiedF
is_punct(const std::locale & Loc=std::locale ())161         is_punct(const std::locale& Loc=std::locale())
162         {
163             return detail::is_classifiedF(std::ctype_base::punct, Loc);
164         }
165 
166         //! is_upper predicate
167         /*!
168             Construct the \c is_classified predicate for the \c ctype_base::upper category.
169 
170             \param Loc A locale used for classification
171             \return An instance of the \c is_classified predicate
172         */
173         inline detail::is_classifiedF
is_upper(const std::locale & Loc=std::locale ())174         is_upper(const std::locale& Loc=std::locale())
175         {
176             return detail::is_classifiedF(std::ctype_base::upper, Loc);
177         }
178 
179         //! is_xdigit predicate
180         /*!
181             Construct the \c is_classified predicate for the \c ctype_base::xdigit category.
182 
183             \param Loc A locale used for classification
184             \return An instance of the \c is_classified predicate
185         */
186         inline detail::is_classifiedF
is_xdigit(const std::locale & Loc=std::locale ())187         is_xdigit(const std::locale& Loc=std::locale())
188         {
189             return detail::is_classifiedF(std::ctype_base::xdigit, Loc);
190         }
191 
192         //! is_any_of predicate
193         /*!
194             Construct the \c is_any_of predicate. The predicate holds if the input
195             is included in the specified set of characters.
196 
197             \param Set A set of characters to be recognized
198             \return An instance of the \c is_any_of predicate
199         */
200         template<typename RangeT>
201         inline detail::is_any_ofF<
202             BOOST_STRING_TYPENAME range_value<RangeT>::type>
is_any_of(const RangeT & Set)203         is_any_of( const RangeT& Set )
204         {
205             iterator_range<BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> lit_set(boost::as_literal(Set));
206             return detail::is_any_ofF<BOOST_STRING_TYPENAME range_value<RangeT>::type>(lit_set);
207         }
208 
209         //! is_from_range predicate
210         /*!
211             Construct the \c is_from_range predicate. The predicate holds if the input
212             is included in the specified range. (i.e. From <= Ch <= To )
213 
214             \param From The start of the range
215             \param To The end of the range
216             \return An instance of the \c is_from_range predicate
217         */
218         template<typename CharT>
is_from_range(CharT From,CharT To)219         inline detail::is_from_rangeF<CharT> is_from_range(CharT From, CharT To)
220         {
221             return detail::is_from_rangeF<CharT>(From,To);
222         }
223 
224         // predicate combinators ---------------------------------------------------//
225 
226         //! predicate 'and' composition predicate
227         /*!
228             Construct the \c class_and predicate. This predicate can be used
229             to logically combine two classification predicates. \c class_and holds,
230             if both predicates return true.
231 
232             \param Pred1 The first predicate
233             \param Pred2 The second predicate
234             \return An instance of the \c class_and predicate
235         */
236         template<typename Pred1T, typename Pred2T>
237         inline detail::pred_andF<Pred1T, Pred2T>
operator &&(const predicate_facade<Pred1T> & Pred1,const predicate_facade<Pred2T> & Pred2)238         operator&&(
239             const predicate_facade<Pred1T>& Pred1,
240             const predicate_facade<Pred2T>& Pred2 )
241         {
242             // Doing the static_cast with the pointer instead of the reference
243             // is a workaround for some compilers which have problems with
244             // static_cast's of template references, i.e. CW8. /grafik/
245             return detail::pred_andF<Pred1T,Pred2T>(
246                 *static_cast<const Pred1T*>(&Pred1),
247                 *static_cast<const Pred2T*>(&Pred2) );
248         }
249 
250         //! predicate 'or' composition predicate
251         /*!
252             Construct the \c class_or predicate. This predicate can be used
253             to logically combine two classification predicates. \c class_or holds,
254             if one of the predicates return true.
255 
256             \param Pred1 The first predicate
257             \param Pred2 The second predicate
258             \return An instance of the \c class_or predicate
259         */
260         template<typename Pred1T, typename Pred2T>
261         inline detail::pred_orF<Pred1T, Pred2T>
operator ||(const predicate_facade<Pred1T> & Pred1,const predicate_facade<Pred2T> & Pred2)262         operator||(
263             const predicate_facade<Pred1T>& Pred1,
264             const predicate_facade<Pred2T>& Pred2 )
265         {
266             // Doing the static_cast with the pointer instead of the reference
267             // is a workaround for some compilers which have problems with
268             // static_cast's of template references, i.e. CW8. /grafik/
269             return detail::pred_orF<Pred1T,Pred2T>(
270                 *static_cast<const Pred1T*>(&Pred1),
271                 *static_cast<const Pred2T*>(&Pred2));
272         }
273 
274         //! predicate negation operator
275         /*!
276             Construct the \c class_not predicate. This predicate represents a negation.
277             \c class_or holds if of the predicates return false.
278 
279             \param Pred The predicate to be negated
280             \return An instance of the \c class_not predicate
281         */
282         template<typename PredT>
283         inline detail::pred_notF<PredT>
operator !(const predicate_facade<PredT> & Pred)284         operator!( const predicate_facade<PredT>& Pred )
285         {
286             // Doing the static_cast with the pointer instead of the reference
287             // is a workaround for some compilers which have problems with
288             // static_cast's of template references, i.e. CW8. /grafik/
289             return detail::pred_notF<PredT>(*static_cast<const PredT*>(&Pred));
290         }
291 
292     } // namespace algorithm
293 
294     // pull names to the boost namespace
295     using algorithm::is_classified;
296     using algorithm::is_space;
297     using algorithm::is_alnum;
298     using algorithm::is_alpha;
299     using algorithm::is_cntrl;
300     using algorithm::is_digit;
301     using algorithm::is_graph;
302     using algorithm::is_lower;
303     using algorithm::is_upper;
304     using algorithm::is_print;
305     using algorithm::is_punct;
306     using algorithm::is_xdigit;
307     using algorithm::is_any_of;
308     using algorithm::is_from_range;
309 
310 } // namespace boost
311 
312 #endif  // BOOST_STRING_PREDICATE_HPP
313