1 // Boost string_algo library predicate.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_PREDICATE_HPP
12 #define BOOST_STRING_PREDICATE_HPP
14 #include <boost/algorithm/string/config.hpp>
15 #include <boost/range/begin.hpp>
16 #include <boost/range/end.hpp>
17 #include <boost/range/iterator.hpp>
18 #include <boost/range/const_iterator.hpp>
19 #include <boost/range/as_literal.hpp>
20 #include <boost/range/iterator_range_core.hpp>
22 #include <boost/algorithm/string/compare.hpp>
23 #include <boost/algorithm/string/find.hpp>
24 #include <boost/algorithm/string/detail/predicate.hpp>
26 /*! \file boost/algorithm/string/predicate.hpp
27 Defines string-related predicates.
28 The predicates determine whether a substring is contained in the input string
29 under various conditions: a string starts with the substring, ends with the
30 substring, simply contains the substring or if both strings are equal.
31 Additionaly the algorithm \c all() checks all elements of a container to satisfy a
34 All predicates provide the strong exception guarantee.
40 // starts_with predicate -----------------------------------------------//
42 //! 'Starts with' predicate
44 This predicate holds when the test string is a prefix of the Input.
45 In other words, if the input starts with the test.
46 When the optional predicate is specified, it is used for character-wise
49 \param Input An input sequence
50 \param Test A test sequence
51 \param Comp An element comparison predicate
52 \return The result of the test
54 \note This function provides the strong exception-safety guarantee
56 template<typename Range1T, typename Range2T, typename PredicateT>
57 inline bool starts_with(
62 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range1T>::type> lit_input(::boost::as_literal(Input));
63 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range2T>::type> lit_test(::boost::as_literal(Test));
65 typedef BOOST_STRING_TYPENAME
66 range_const_iterator<Range1T>::type Iterator1T;
67 typedef BOOST_STRING_TYPENAME
68 range_const_iterator<Range2T>::type Iterator2T;
70 Iterator1T InputEnd=::boost::end(lit_input);
71 Iterator2T TestEnd=::boost::end(lit_test);
73 Iterator1T it=::boost::begin(lit_input);
74 Iterator2T pit=::boost::begin(lit_test);
76 it!=InputEnd && pit!=TestEnd;
79 if( !(Comp(*it,*pit)) )
86 //! 'Starts with' predicate
90 template<typename Range1T, typename Range2T>
91 inline bool starts_with(
95 return ::boost::algorithm::starts_with(Input, Test, is_equal());
98 //! 'Starts with' predicate ( case insensitive )
100 This predicate holds when the test string is a prefix of the Input.
101 In other words, if the input starts with the test.
102 Elements are compared case insensitively.
104 \param Input An input sequence
105 \param Test A test sequence
106 \param Loc A locale used for case insensitive comparison
107 \return The result of the test
109 \note This function provides the strong exception-safety guarantee
111 template<typename Range1T, typename Range2T>
112 inline bool istarts_with(
113 const Range1T& Input,
115 const std::locale& Loc=std::locale())
117 return ::boost::algorithm::starts_with(Input, Test, is_iequal(Loc));
121 // ends_with predicate -----------------------------------------------//
123 //! 'Ends with' predicate
125 This predicate holds when the test string is a suffix of the Input.
126 In other words, if the input ends with the test.
127 When the optional predicate is specified, it is used for character-wise
131 \param Input An input sequence
132 \param Test A test sequence
133 \param Comp An element comparison predicate
134 \return The result of the test
136 \note This function provides the strong exception-safety guarantee
138 template<typename Range1T, typename Range2T, typename PredicateT>
139 inline bool ends_with(
140 const Range1T& Input,
144 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range1T>::type> lit_input(::boost::as_literal(Input));
145 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range2T>::type> lit_test(::boost::as_literal(Test));
147 typedef BOOST_STRING_TYPENAME
148 range_const_iterator<Range1T>::type Iterator1T;
149 typedef BOOST_STRING_TYPENAME boost::detail::
150 iterator_traits<Iterator1T>::iterator_category category;
153 ends_with_iter_select(
154 ::boost::begin(lit_input),
155 ::boost::end(lit_input),
156 ::boost::begin(lit_test),
157 ::boost::end(lit_test),
163 //! 'Ends with' predicate
167 template<typename Range1T, typename Range2T>
168 inline bool ends_with(
169 const Range1T& Input,
172 return ::boost::algorithm::ends_with(Input, Test, is_equal());
175 //! 'Ends with' predicate ( case insensitive )
177 This predicate holds when the test container is a suffix of the Input.
178 In other words, if the input ends with the test.
179 Elements are compared case insensitively.
181 \param Input An input sequence
182 \param Test A test sequence
183 \param Loc A locale used for case insensitive comparison
184 \return The result of the test
186 \note This function provides the strong exception-safety guarantee
188 template<typename Range1T, typename Range2T>
189 inline bool iends_with(
190 const Range1T& Input,
192 const std::locale& Loc=std::locale())
194 return ::boost::algorithm::ends_with(Input, Test, is_iequal(Loc));
197 // contains predicate -----------------------------------------------//
199 //! 'Contains' predicate
201 This predicate holds when the test container is contained in the Input.
202 When the optional predicate is specified, it is used for character-wise
205 \param Input An input sequence
206 \param Test A test sequence
207 \param Comp An element comparison predicate
208 \return The result of the test
210 \note This function provides the strong exception-safety guarantee
212 template<typename Range1T, typename Range2T, typename PredicateT>
213 inline bool contains(
214 const Range1T& Input,
218 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range1T>::type> lit_input(::boost::as_literal(Input));
219 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range2T>::type> lit_test(::boost::as_literal(Test));
221 if (::boost::empty(lit_test))
223 // Empty range is contained always
227 // Use the temporary variable to make VACPP happy
228 bool bResult=(::boost::algorithm::first_finder(lit_test,Comp)(::boost::begin(lit_input), ::boost::end(lit_input)));
232 //! 'Contains' predicate
236 template<typename Range1T, typename Range2T>
237 inline bool contains(
238 const Range1T& Input,
241 return ::boost::algorithm::contains(Input, Test, is_equal());
244 //! 'Contains' predicate ( case insensitive )
246 This predicate holds when the test container is contained in the Input.
247 Elements are compared case insensitively.
249 \param Input An input sequence
250 \param Test A test sequence
251 \param Loc A locale used for case insensitive comparison
252 \return The result of the test
254 \note This function provides the strong exception-safety guarantee
256 template<typename Range1T, typename Range2T>
257 inline bool icontains(
258 const Range1T& Input,
260 const std::locale& Loc=std::locale())
262 return ::boost::algorithm::contains(Input, Test, is_iequal(Loc));
265 // equals predicate -----------------------------------------------//
267 //! 'Equals' predicate
269 This predicate holds when the test container is equal to the
270 input container i.e. all elements in both containers are same.
271 When the optional predicate is specified, it is used for character-wise
274 \param Input An input sequence
275 \param Test A test sequence
276 \param Comp An element comparison predicate
277 \return The result of the test
279 \note This is a two-way version of \c std::equal algorithm
281 \note This function provides the strong exception-safety guarantee
283 template<typename Range1T, typename Range2T, typename PredicateT>
285 const Range1T& Input,
289 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range1T>::type> lit_input(::boost::as_literal(Input));
290 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range2T>::type> lit_test(::boost::as_literal(Test));
292 typedef BOOST_STRING_TYPENAME
293 range_const_iterator<Range1T>::type Iterator1T;
294 typedef BOOST_STRING_TYPENAME
295 range_const_iterator<Range2T>::type Iterator2T;
297 Iterator1T InputEnd=::boost::end(lit_input);
298 Iterator2T TestEnd=::boost::end(lit_test);
300 Iterator1T it=::boost::begin(lit_input);
301 Iterator2T pit=::boost::begin(lit_test);
303 it!=InputEnd && pit!=TestEnd;
306 if( !(Comp(*it,*pit)) )
310 return (pit==TestEnd) && (it==InputEnd);
313 //! 'Equals' predicate
317 template<typename Range1T, typename Range2T>
319 const Range1T& Input,
322 return ::boost::algorithm::equals(Input, Test, is_equal());
325 //! 'Equals' predicate ( case insensitive )
327 This predicate holds when the test container is equal to the
328 input container i.e. all elements in both containers are same.
329 Elements are compared case insensitively.
331 \param Input An input sequence
332 \param Test A test sequence
333 \param Loc A locale used for case insensitive comparison
334 \return The result of the test
336 \note This is a two-way version of \c std::equal algorithm
338 \note This function provides the strong exception-safety guarantee
340 template<typename Range1T, typename Range2T>
342 const Range1T& Input,
344 const std::locale& Loc=std::locale())
346 return ::boost::algorithm::equals(Input, Test, is_iequal(Loc));
349 // lexicographical_compare predicate -----------------------------//
351 //! Lexicographical compare predicate
353 This predicate is an overload of std::lexicographical_compare
356 It check whether the first argument is lexicographically less
359 If the optional predicate is specified, it is used for character-wise
362 \param Arg1 First argument
363 \param Arg2 Second argument
364 \param Pred Comparison predicate
365 \return The result of the test
367 \note This function provides the strong exception-safety guarantee
369 template<typename Range1T, typename Range2T, typename PredicateT>
370 inline bool lexicographical_compare(
375 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range1T>::type> lit_arg1(::boost::as_literal(Arg1));
376 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range2T>::type> lit_arg2(::boost::as_literal(Arg2));
378 return std::lexicographical_compare(
379 ::boost::begin(lit_arg1),
380 ::boost::end(lit_arg1),
381 ::boost::begin(lit_arg2),
382 ::boost::end(lit_arg2),
386 //! Lexicographical compare predicate
390 template<typename Range1T, typename Range2T>
391 inline bool lexicographical_compare(
395 return ::boost::algorithm::lexicographical_compare(Arg1, Arg2, is_less());
398 //! Lexicographical compare predicate (case-insensitive)
400 This predicate is an overload of std::lexicographical_compare
402 It check whether the first argument is lexicographically less
404 Elements are compared case insensitively
407 \param Arg1 First argument
408 \param Arg2 Second argument
409 \param Loc A locale used for case insensitive comparison
410 \return The result of the test
412 \note This function provides the strong exception-safety guarantee
414 template<typename Range1T, typename Range2T>
415 inline bool ilexicographical_compare(
418 const std::locale& Loc=std::locale())
420 return ::boost::algorithm::lexicographical_compare(Arg1, Arg2, is_iless(Loc));
424 // all predicate -----------------------------------------------//
428 This predicate holds it all its elements satisfy a given
429 condition, represented by the predicate.
431 \param Input An input sequence
432 \param Pred A predicate
433 \return The result of the test
435 \note This function provides the strong exception-safety guarantee
437 template<typename RangeT, typename PredicateT>
442 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> lit_input(::boost::as_literal(Input));
444 typedef BOOST_STRING_TYPENAME
445 range_const_iterator<RangeT>::type Iterator1T;
447 Iterator1T InputEnd=::boost::end(lit_input);
448 for( Iterator1T It=::boost::begin(lit_input); It!=InputEnd; ++It)
457 } // namespace algorithm
459 // pull names to the boost namespace
460 using algorithm::starts_with;
461 using algorithm::istarts_with;
462 using algorithm::ends_with;
463 using algorithm::iends_with;
464 using algorithm::contains;
465 using algorithm::icontains;
466 using algorithm::equals;
467 using algorithm::iequals;
468 using algorithm::all;
469 using algorithm::lexicographical_compare;
470 using algorithm::ilexicographical_compare;
475 #endif // BOOST_STRING_PREDICATE_HPP