1 // Boost string_algo library classification.hpp header file ---------------------------//
3 // Copyright Pavol Droba 2002-2003.
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)
9 // See http://www.boost.org/ for updates, documentation, and revision history.
11 #ifndef BOOST_STRING_CLASSIFICATION_HPP
12 #define BOOST_STRING_CLASSIFICATION_HPP
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>
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.
32 // classification functor generator -------------------------------------//
34 //! is_classified predicate
36 Construct the \c is_classified predicate. This predicate holds if the input is
37 of specified \c std::ctype category.
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
43 inline detail::is_classifiedF
44 is_classified(std::ctype_base::mask Type, const std::locale& Loc=std::locale())
46 return detail::is_classifiedF(Type, Loc);
49 //! is_space predicate
51 Construct the \c is_classified predicate for the \c ctype_base::space category.
53 \param Loc A locale used for classification
54 \return An instance of the \c is_classified predicate
56 inline detail::is_classifiedF
57 is_space(const std::locale& Loc=std::locale())
59 return detail::is_classifiedF(std::ctype_base::space, Loc);
62 //! is_alnum predicate
64 Construct the \c is_classified predicate for the \c ctype_base::alnum category.
66 \param Loc A locale used for classification
67 \return An instance of the \c is_classified predicate
69 inline detail::is_classifiedF
70 is_alnum(const std::locale& Loc=std::locale())
72 return detail::is_classifiedF(std::ctype_base::alnum, Loc);
75 //! is_alpha predicate
77 Construct the \c is_classified predicate for the \c ctype_base::alpha category.
79 \param Loc A locale used for classification
80 \return An instance of the \c is_classified predicate
82 inline detail::is_classifiedF
83 is_alpha(const std::locale& Loc=std::locale())
85 return detail::is_classifiedF(std::ctype_base::alpha, Loc);
88 //! is_cntrl predicate
90 Construct the \c is_classified predicate for the \c ctype_base::cntrl category.
92 \param Loc A locale used for classification
93 \return An instance of the \c is_classified predicate
95 inline detail::is_classifiedF
96 is_cntrl(const std::locale& Loc=std::locale())
98 return detail::is_classifiedF(std::ctype_base::cntrl, Loc);
101 //! is_digit predicate
103 Construct the \c is_classified predicate for the \c ctype_base::digit category.
105 \param Loc A locale used for classification
106 \return An instance of the \c is_classified predicate
108 inline detail::is_classifiedF
109 is_digit(const std::locale& Loc=std::locale())
111 return detail::is_classifiedF(std::ctype_base::digit, Loc);
114 //! is_graph predicate
116 Construct the \c is_classified predicate for the \c ctype_base::graph category.
118 \param Loc A locale used for classification
119 \return An instance of the \c is_classified predicate
121 inline detail::is_classifiedF
122 is_graph(const std::locale& Loc=std::locale())
124 return detail::is_classifiedF(std::ctype_base::graph, Loc);
127 //! is_lower predicate
129 Construct the \c is_classified predicate for the \c ctype_base::lower category.
131 \param Loc A locale used for classification
132 \return An instance of \c is_classified predicate
134 inline detail::is_classifiedF
135 is_lower(const std::locale& Loc=std::locale())
137 return detail::is_classifiedF(std::ctype_base::lower, Loc);
140 //! is_print predicate
142 Construct the \c is_classified predicate for the \c ctype_base::print category.
144 \param Loc A locale used for classification
145 \return An instance of the \c is_classified predicate
147 inline detail::is_classifiedF
148 is_print(const std::locale& Loc=std::locale())
150 return detail::is_classifiedF(std::ctype_base::print, Loc);
153 //! is_punct predicate
155 Construct the \c is_classified predicate for the \c ctype_base::punct category.
157 \param Loc A locale used for classification
158 \return An instance of the \c is_classified predicate
160 inline detail::is_classifiedF
161 is_punct(const std::locale& Loc=std::locale())
163 return detail::is_classifiedF(std::ctype_base::punct, Loc);
166 //! is_upper predicate
168 Construct the \c is_classified predicate for the \c ctype_base::upper category.
170 \param Loc A locale used for classification
171 \return An instance of the \c is_classified predicate
173 inline detail::is_classifiedF
174 is_upper(const std::locale& Loc=std::locale())
176 return detail::is_classifiedF(std::ctype_base::upper, Loc);
179 //! is_xdigit predicate
181 Construct the \c is_classified predicate for the \c ctype_base::xdigit category.
183 \param Loc A locale used for classification
184 \return An instance of the \c is_classified predicate
186 inline detail::is_classifiedF
187 is_xdigit(const std::locale& Loc=std::locale())
189 return detail::is_classifiedF(std::ctype_base::xdigit, Loc);
192 //! is_any_of predicate
194 Construct the \c is_any_of predicate. The predicate holds if the input
195 is included in the specified set of characters.
197 \param Set A set of characters to be recognized
198 \return An instance of the \c is_any_of predicate
200 template<typename RangeT>
201 inline detail::is_any_ofF<
202 BOOST_STRING_TYPENAME range_value<RangeT>::type>
203 is_any_of( const RangeT& Set )
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);
209 //! is_from_range predicate
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 )
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
218 template<typename CharT>
219 inline detail::is_from_rangeF<CharT> is_from_range(CharT From, CharT To)
221 return detail::is_from_rangeF<CharT>(From,To);
224 // predicate combinators ---------------------------------------------------//
226 //! predicate 'and' composition predicate
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.
232 \param Pred1 The first predicate
233 \param Pred2 The second predicate
234 \return An instance of the \c class_and predicate
236 template<typename Pred1T, typename Pred2T>
237 inline detail::pred_andF<Pred1T, Pred2T>
239 const predicate_facade<Pred1T>& Pred1,
240 const predicate_facade<Pred2T>& Pred2 )
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) );
250 //! predicate 'or' composition predicate
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.
256 \param Pred1 The first predicate
257 \param Pred2 The second predicate
258 \return An instance of the \c class_or predicate
260 template<typename Pred1T, typename Pred2T>
261 inline detail::pred_orF<Pred1T, Pred2T>
263 const predicate_facade<Pred1T>& Pred1,
264 const predicate_facade<Pred2T>& Pred2 )
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));
274 //! predicate negation operator
276 Construct the \c class_not predicate. This predicate represents a negation.
277 \c class_or holds if of the predicates return false.
279 \param Pred The predicate to be negated
280 \return An instance of the \c class_not predicate
282 template<typename PredT>
283 inline detail::pred_notF<PredT>
284 operator!( const predicate_facade<PredT>& Pred )
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));
292 } // namespace algorithm
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;
312 #endif // BOOST_STRING_PREDICATE_HPP