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