1 | // Boost string_algo library predicate.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_PREDICATE_HPP
|
---|
11 | #define BOOST_STRING_PREDICATE_HPP
|
---|
12 |
|
---|
13 | #include <boost/algorithm/string/config.hpp>
|
---|
14 | #include <boost/range/begin.hpp>
|
---|
15 | #include <boost/range/end.hpp>
|
---|
16 | #include <boost/range/iterator.hpp>
|
---|
17 | #include <boost/range/const_iterator.hpp>
|
---|
18 |
|
---|
19 | #include <boost/algorithm/string/compare.hpp>
|
---|
20 | #include <boost/algorithm/string/find.hpp>
|
---|
21 | #include <boost/algorithm/string/detail/predicate.hpp>
|
---|
22 |
|
---|
23 | /*! \file boost/algorithm/string/predicate.hpp
|
---|
24 | Defines string-related predicates.
|
---|
25 | The predicates determine whether a substring is contained in the input string
|
---|
26 | under various conditions: a string starts with the substring, ends with the
|
---|
27 | substring, simply contains the substring or if both strings are equal.
|
---|
28 | Additionaly the algorithm \c all() checks all elements of a container to satisfy a
|
---|
29 | condition.
|
---|
30 |
|
---|
31 | All predicates provide the strong exception guarantee.
|
---|
32 | */
|
---|
33 |
|
---|
34 | namespace boost {
|
---|
35 | namespace algorithm {
|
---|
36 |
|
---|
37 | // starts_with predicate -----------------------------------------------//
|
---|
38 |
|
---|
39 | //! 'Starts with' predicate
|
---|
40 | /*!
|
---|
41 | This predicate holds when the test string is a prefix of the Input.
|
---|
42 | In other words, if the input starts with the test.
|
---|
43 | When the optional predicate is specified, it is used for character-wise
|
---|
44 | comparison.
|
---|
45 |
|
---|
46 | \param Input An input sequence
|
---|
47 | \param Test A test sequence
|
---|
48 | \param Comp An element comparison predicate
|
---|
49 | \return The result of the test
|
---|
50 |
|
---|
51 | \note This function provides the strong exception-safety guarantee
|
---|
52 | */
|
---|
53 | template<typename Range1T, typename Range2T, typename PredicateT>
|
---|
54 | inline bool starts_with(
|
---|
55 | const Range1T& Input,
|
---|
56 | const Range2T& Test,
|
---|
57 | PredicateT Comp)
|
---|
58 | {
|
---|
59 | typedef BOOST_STRING_TYPENAME
|
---|
60 | range_const_iterator<Range1T>::type Iterator1T;
|
---|
61 | typedef BOOST_STRING_TYPENAME
|
---|
62 | range_const_iterator<Range2T>::type Iterator2T;
|
---|
63 |
|
---|
64 | Iterator1T InputEnd=end(Input);
|
---|
65 | Iterator2T TestEnd=end(Test);
|
---|
66 |
|
---|
67 | Iterator1T it=begin(Input);
|
---|
68 | Iterator2T pit=begin(Test);
|
---|
69 | for(;
|
---|
70 | it!=InputEnd && pit!=TestEnd;
|
---|
71 | ++it,++pit)
|
---|
72 | {
|
---|
73 | if( !(Comp(*it,*pit)) )
|
---|
74 | return false;
|
---|
75 | }
|
---|
76 |
|
---|
77 | return pit==TestEnd;
|
---|
78 | }
|
---|
79 |
|
---|
80 | //! 'Starts with' predicate
|
---|
81 | /*!
|
---|
82 | \overload
|
---|
83 | */
|
---|
84 | template<typename Range1T, typename Range2T>
|
---|
85 | inline bool starts_with(
|
---|
86 | const Range1T& Input,
|
---|
87 | const Range2T& Test)
|
---|
88 | {
|
---|
89 | return starts_with(Input, Test, is_equal());
|
---|
90 | }
|
---|
91 |
|
---|
92 | //! 'Starts with' predicate ( case insensitive )
|
---|
93 | /*!
|
---|
94 | This predicate holds when the test string is a prefix of the Input.
|
---|
95 | In other words, if the input starts with the test.
|
---|
96 | Elements are compared case insensitively.
|
---|
97 |
|
---|
98 | \param Input An input sequence
|
---|
99 | \param Test A test sequence
|
---|
100 | \param Loc A locale used for case insensitive comparison
|
---|
101 | \return The result of the test
|
---|
102 |
|
---|
103 | \note This function provides the strong exception-safety guarantee
|
---|
104 | */
|
---|
105 | template<typename Range1T, typename Range2T>
|
---|
106 | inline bool istarts_with(
|
---|
107 | const Range1T& Input,
|
---|
108 | const Range2T& Test,
|
---|
109 | const std::locale& Loc=std::locale())
|
---|
110 | {
|
---|
111 | return starts_with(Input, Test, is_iequal(Loc));
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | // ends_with predicate -----------------------------------------------//
|
---|
116 |
|
---|
117 | //! 'Ends with' predicate
|
---|
118 | /*!
|
---|
119 | This predicate holds when the test string is a suffix of the Input.
|
---|
120 | In other words, if the input ends with the test.
|
---|
121 | When the optional predicate is specified, it is used for character-wise
|
---|
122 | comparison.
|
---|
123 |
|
---|
124 |
|
---|
125 | \param Input An input sequence
|
---|
126 | \param Test A test sequence
|
---|
127 | \param Comp An element comparison predicate
|
---|
128 | \return The result of the test
|
---|
129 |
|
---|
130 | \note This function provides the strong exception-safety guarantee
|
---|
131 | */
|
---|
132 | template<typename Range1T, typename Range2T, typename PredicateT>
|
---|
133 | inline bool ends_with(
|
---|
134 | const Range1T& Input,
|
---|
135 | const Range2T& Test,
|
---|
136 | PredicateT Comp)
|
---|
137 | {
|
---|
138 | typedef BOOST_STRING_TYPENAME
|
---|
139 | range_const_iterator<Range1T>::type Iterator1T;
|
---|
140 | typedef BOOST_STRING_TYPENAME boost::detail::
|
---|
141 | iterator_traits<Iterator1T>::iterator_category category;
|
---|
142 |
|
---|
143 | return detail::
|
---|
144 | ends_with_iter_select(
|
---|
145 | begin(Input),
|
---|
146 | end(Input),
|
---|
147 | begin(Test),
|
---|
148 | end(Test),
|
---|
149 | Comp,
|
---|
150 | category());
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | //! 'Ends with' predicate
|
---|
155 | /*!
|
---|
156 | \overload
|
---|
157 | */
|
---|
158 | template<typename Range1T, typename Range2T>
|
---|
159 | inline bool ends_with(
|
---|
160 | const Range1T& Input,
|
---|
161 | const Range2T& Test)
|
---|
162 | {
|
---|
163 | return ends_with(Input, Test, is_equal());
|
---|
164 | }
|
---|
165 |
|
---|
166 | //! 'Ends with' predicate ( case insensitive )
|
---|
167 | /*!
|
---|
168 | This predicate holds when the test container is a suffix of the Input.
|
---|
169 | In other words, if the input ends with the test.
|
---|
170 | Elements are compared case insensitively.
|
---|
171 |
|
---|
172 | \param Input An input sequence
|
---|
173 | \param Test A test sequence
|
---|
174 | \param Loc A locale used for case insensitive comparison
|
---|
175 | \return The result of the test
|
---|
176 |
|
---|
177 | \note This function provides the strong exception-safety guarantee
|
---|
178 | */
|
---|
179 | template<typename Range1T, typename Range2T>
|
---|
180 | inline bool iends_with(
|
---|
181 | const Range1T& Input,
|
---|
182 | const Range2T& Test,
|
---|
183 | const std::locale& Loc=std::locale())
|
---|
184 | {
|
---|
185 | return ends_with(Input, Test, is_iequal(Loc));
|
---|
186 | }
|
---|
187 |
|
---|
188 | // contains predicate -----------------------------------------------//
|
---|
189 |
|
---|
190 | //! 'Contains' predicate
|
---|
191 | /*!
|
---|
192 | This predicate holds when the test container is contained in the Input.
|
---|
193 | When the optional predicate is specified, it is used for character-wise
|
---|
194 | comparison.
|
---|
195 |
|
---|
196 | \param Input An input sequence
|
---|
197 | \param Test A test sequence
|
---|
198 | \param Comp An element comparison predicate
|
---|
199 | \return The result of the test
|
---|
200 |
|
---|
201 | \note This function provides the strong exception-safety guarantee
|
---|
202 | */
|
---|
203 | template<typename Range1T, typename Range2T, typename PredicateT>
|
---|
204 | inline bool contains(
|
---|
205 | const Range1T& Input,
|
---|
206 | const Range2T& Test,
|
---|
207 | PredicateT Comp)
|
---|
208 | {
|
---|
209 | if (empty(Test))
|
---|
210 | {
|
---|
211 | // Empty range is contained always
|
---|
212 | return true;
|
---|
213 | }
|
---|
214 |
|
---|
215 | // Use the temporary variable to make VACPP happy
|
---|
216 | bool bResult=(first_finder(Test,Comp)(begin(Input), end(Input)));
|
---|
217 | return bResult;
|
---|
218 | }
|
---|
219 |
|
---|
220 | //! 'Contains' predicate
|
---|
221 | /*!
|
---|
222 | \overload
|
---|
223 | */
|
---|
224 | template<typename Range1T, typename Range2T>
|
---|
225 | inline bool contains(
|
---|
226 | const Range1T& Input,
|
---|
227 | const Range2T& Test)
|
---|
228 | {
|
---|
229 | return contains(Input, Test, is_equal());
|
---|
230 | }
|
---|
231 |
|
---|
232 | //! 'Contains' predicate ( case insensitive )
|
---|
233 | /*!
|
---|
234 | This predicate holds when the test container is contained in the Input.
|
---|
235 | Elements are compared case insensitively.
|
---|
236 |
|
---|
237 | \param Input An input sequence
|
---|
238 | \param Test A test sequence
|
---|
239 | \param Loc A locale used for case insensitive comparison
|
---|
240 | \return The result of the test
|
---|
241 |
|
---|
242 | \note This function provides the strong exception-safety guarantee
|
---|
243 | */
|
---|
244 | template<typename Range1T, typename Range2T>
|
---|
245 | inline bool icontains(
|
---|
246 | const Range1T& Input,
|
---|
247 | const Range2T& Test,
|
---|
248 | const std::locale& Loc=std::locale())
|
---|
249 | {
|
---|
250 | return contains(Input, Test, is_iequal(Loc));
|
---|
251 | }
|
---|
252 |
|
---|
253 | // equals predicate -----------------------------------------------//
|
---|
254 |
|
---|
255 | //! 'Equals' predicate
|
---|
256 | /*!
|
---|
257 | This predicate holds when the test container is equal to the
|
---|
258 | input container i.e. all elements in both containers are same.
|
---|
259 | When the optional predicate is specified, it is used for character-wise
|
---|
260 | comparison.
|
---|
261 |
|
---|
262 | \param Input An input sequence
|
---|
263 | \param Test A test sequence
|
---|
264 | \param Comp An element comparison predicate
|
---|
265 | \return The result of the test
|
---|
266 |
|
---|
267 | \note This is a two-way version of \c std::equal algorithm
|
---|
268 |
|
---|
269 | \note This function provides the strong exception-safety guarantee
|
---|
270 | */
|
---|
271 | template<typename Range1T, typename Range2T, typename PredicateT>
|
---|
272 | inline bool equals(
|
---|
273 | const Range1T& Input,
|
---|
274 | const Range2T& Test,
|
---|
275 | PredicateT Comp)
|
---|
276 | {
|
---|
277 | typedef BOOST_STRING_TYPENAME
|
---|
278 | range_const_iterator<Range1T>::type Iterator1T;
|
---|
279 | typedef BOOST_STRING_TYPENAME
|
---|
280 | range_const_iterator<Range2T>::type Iterator2T;
|
---|
281 |
|
---|
282 | Iterator1T InputEnd=end(Input);
|
---|
283 | Iterator2T TestEnd=end(Test);
|
---|
284 |
|
---|
285 | Iterator1T it=begin(Input);
|
---|
286 | Iterator2T pit=begin(Test);
|
---|
287 | for(;
|
---|
288 | it!=InputEnd && pit!=TestEnd;
|
---|
289 | ++it,++pit)
|
---|
290 | {
|
---|
291 | if( !(Comp(*it,*pit)) )
|
---|
292 | return false;
|
---|
293 | }
|
---|
294 |
|
---|
295 | return (pit==TestEnd) && (it==InputEnd);
|
---|
296 | }
|
---|
297 |
|
---|
298 | //! 'Equals' predicate
|
---|
299 | /*!
|
---|
300 | \overload
|
---|
301 | */
|
---|
302 | template<typename Range1T, typename Range2T>
|
---|
303 | inline bool equals(
|
---|
304 | const Range1T& Input,
|
---|
305 | const Range2T& Test)
|
---|
306 | {
|
---|
307 | return equals(Input, Test, is_equal());
|
---|
308 | }
|
---|
309 |
|
---|
310 | //! 'Equals' predicate ( casa insensitive )
|
---|
311 | /*!
|
---|
312 | This predicate holds when the test container is equal to the
|
---|
313 | input container i.e. all elements in both containers are same.
|
---|
314 | Elements are compared case insensitively.
|
---|
315 |
|
---|
316 | \param Input An input sequence
|
---|
317 | \param Test A test sequence
|
---|
318 | \param Loc A locale used for case insensitive comparison
|
---|
319 | \return The result of the test
|
---|
320 |
|
---|
321 | \note This is a two-way version of \c std::equal algorithm
|
---|
322 |
|
---|
323 | \note This function provides the strong exception-safety guarantee
|
---|
324 | */
|
---|
325 | template<typename Range1T, typename Range2T>
|
---|
326 | inline bool iequals(
|
---|
327 | const Range1T& Input,
|
---|
328 | const Range2T& Test,
|
---|
329 | const std::locale& Loc=std::locale())
|
---|
330 | {
|
---|
331 | return equals(Input, Test, is_iequal(Loc));
|
---|
332 | }
|
---|
333 |
|
---|
334 | // all predicate -----------------------------------------------//
|
---|
335 |
|
---|
336 | //! 'All' predicate
|
---|
337 | /*!
|
---|
338 | This predicate holds it all its elements satisfy a given
|
---|
339 | condition, represented by the predicate.
|
---|
340 |
|
---|
341 | \param Input An input sequence
|
---|
342 | \param Pred A predicate
|
---|
343 | \return The result of the test
|
---|
344 |
|
---|
345 | \note This function provides the strong exception-safety guarantee
|
---|
346 | */
|
---|
347 | template<typename RangeT, typename PredicateT>
|
---|
348 | inline bool all(
|
---|
349 | const RangeT& Input,
|
---|
350 | PredicateT Pred)
|
---|
351 | {
|
---|
352 | typedef BOOST_STRING_TYPENAME
|
---|
353 | range_const_iterator<RangeT>::type Iterator1T;
|
---|
354 |
|
---|
355 | Iterator1T InputEnd=end(Input);
|
---|
356 | for( Iterator1T It=begin(Input); It!=InputEnd; ++It)
|
---|
357 | {
|
---|
358 | if (!Pred(*It))
|
---|
359 | return false;
|
---|
360 | }
|
---|
361 |
|
---|
362 | return true;
|
---|
363 | }
|
---|
364 |
|
---|
365 | } // namespace algorithm
|
---|
366 |
|
---|
367 | // pull names to the boost namespace
|
---|
368 | using algorithm::starts_with;
|
---|
369 | using algorithm::istarts_with;
|
---|
370 | using algorithm::ends_with;
|
---|
371 | using algorithm::iends_with;
|
---|
372 | using algorithm::contains;
|
---|
373 | using algorithm::icontains;
|
---|
374 | using algorithm::equals;
|
---|
375 | using algorithm::iequals;
|
---|
376 | using algorithm::all;
|
---|
377 |
|
---|
378 | } // namespace boost
|
---|
379 |
|
---|
380 |
|
---|
381 | #endif // BOOST_STRING_PREDICATE_HPP
|
---|