1 // Boost string_algo library predicate.hpp header file ---------------------------//
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)
8 // See http://www.boost.org for updates, documentation, and revision history.
10 #ifndef BOOST_STRING_PREDICATE_HPP
11 #define BOOST_STRING_PREDICATE_HPP
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>
19 #include <boost/algorithm/string/compare.hpp>
20 #include <boost/algorithm/string/find.hpp>
21 #include <boost/algorithm/string/detail/predicate.hpp>
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
31 All predicates provide the strong exception guarantee.
37 // starts_with predicate -----------------------------------------------//
39 //! 'Starts with' predicate
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
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
51 \note This function provides the strong exception-safety guarantee
53 template<typename Range1T, typename Range2T, typename PredicateT>
54 inline bool starts_with(
59 typedef BOOST_STRING_TYPENAME
60 range_const_iterator<Range1T>::type Iterator1T;
61 typedef BOOST_STRING_TYPENAME
62 range_const_iterator<Range2T>::type Iterator2T;
64 Iterator1T InputEnd=end(Input);
65 Iterator2T TestEnd=end(Test);
67 Iterator1T it=begin(Input);
68 Iterator2T pit=begin(Test);
70 it!=InputEnd && pit!=TestEnd;
73 if( !(Comp(*it,*pit)) )
80 //! 'Starts with' predicate
84 template<typename Range1T, typename Range2T>
85 inline bool starts_with(
89 return starts_with(Input, Test, is_equal());
92 //! 'Starts with' predicate ( case insensitive )
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.
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
103 \note This function provides the strong exception-safety guarantee
105 template<typename Range1T, typename Range2T>
106 inline bool istarts_with(
107 const Range1T& Input,
109 const std::locale& Loc=std::locale())
111 return starts_with(Input, Test, is_iequal(Loc));
115 // ends_with predicate -----------------------------------------------//
117 //! 'Ends with' predicate
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
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
130 \note This function provides the strong exception-safety guarantee
132 template<typename Range1T, typename Range2T, typename PredicateT>
133 inline bool ends_with(
134 const Range1T& Input,
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;
144 ends_with_iter_select(
154 //! 'Ends with' predicate
158 template<typename Range1T, typename Range2T>
159 inline bool ends_with(
160 const Range1T& Input,
163 return ends_with(Input, Test, is_equal());
166 //! 'Ends with' predicate ( case insensitive )
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.
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
177 \note This function provides the strong exception-safety guarantee
179 template<typename Range1T, typename Range2T>
180 inline bool iends_with(
181 const Range1T& Input,
183 const std::locale& Loc=std::locale())
185 return ends_with(Input, Test, is_iequal(Loc));
188 // contains predicate -----------------------------------------------//
190 //! 'Contains' predicate
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
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
201 \note This function provides the strong exception-safety guarantee
203 template<typename Range1T, typename Range2T, typename PredicateT>
204 inline bool contains(
205 const Range1T& Input,
211 // Empty range is contained always
215 // Use the temporary variable to make VACPP happy
216 bool bResult=(first_finder(Test,Comp)(begin(Input), end(Input)));
220 //! 'Contains' predicate
224 template<typename Range1T, typename Range2T>
225 inline bool contains(
226 const Range1T& Input,
229 return contains(Input, Test, is_equal());
232 //! 'Contains' predicate ( case insensitive )
234 This predicate holds when the test container is contained in the Input.
235 Elements are compared case insensitively.
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
242 \note This function provides the strong exception-safety guarantee
244 template<typename Range1T, typename Range2T>
245 inline bool icontains(
246 const Range1T& Input,
248 const std::locale& Loc=std::locale())
250 return contains(Input, Test, is_iequal(Loc));
253 // equals predicate -----------------------------------------------//
255 //! 'Equals' predicate
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
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
267 \note This is a two-way version of \c std::equal algorithm
269 \note This function provides the strong exception-safety guarantee
271 template<typename Range1T, typename Range2T, typename PredicateT>
273 const Range1T& Input,
277 typedef BOOST_STRING_TYPENAME
278 range_const_iterator<Range1T>::type Iterator1T;
279 typedef BOOST_STRING_TYPENAME
280 range_const_iterator<Range2T>::type Iterator2T;
282 Iterator1T InputEnd=end(Input);
283 Iterator2T TestEnd=end(Test);
285 Iterator1T it=begin(Input);
286 Iterator2T pit=begin(Test);
288 it!=InputEnd && pit!=TestEnd;
291 if( !(Comp(*it,*pit)) )
295 return (pit==TestEnd) && (it==InputEnd);
298 //! 'Equals' predicate
302 template<typename Range1T, typename Range2T>
304 const Range1T& Input,
307 return equals(Input, Test, is_equal());
310 //! 'Equals' predicate ( case insensitive )
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.
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
321 \note This is a two-way version of \c std::equal algorithm
323 \note This function provides the strong exception-safety guarantee
325 template<typename Range1T, typename Range2T>
327 const Range1T& Input,
329 const std::locale& Loc=std::locale())
331 return equals(Input, Test, is_iequal(Loc));
334 // lexicographical_compare predicate -----------------------------//
336 //! Lexicographical compare predicate
338 This predicate is an overload of std::lexicographical_compare
341 It check whether the first argument is lexicographically less
344 If the optional predicate is specified, it is used for character-wise
347 \param Arg1 First argument
348 \param Arg2 Second argument
349 \param Pred Comparison predicate
350 \return The result of the test
352 \note This function provides the strong exception-safety guarantee
354 template<typename Range1T, typename Range2T, typename PredicateT>
355 inline bool lexicographical_compare(
360 return std::lexicographical_compare(
368 //! Lexicographical compare predicate
372 template<typename Range1T, typename Range2T>
373 inline bool lexicographical_compare(
377 return std::lexicographical_compare(
385 //! Lexicographical compare predicate (case-insensitive)
387 This predicate is an overload of std::lexicographical_compare
389 It check whether the first argument is lexicographically less
391 Elements are compared case insensitively
394 \param Arg1 First argument
395 \param Arg2 Second argument
396 \return The result of the test
398 \note This function provides the strong exception-safety guarantee
400 template<typename Range1T, typename Range2T>
401 inline bool ilexicographical_compare(
405 return std::lexicographical_compare(
414 // all predicate -----------------------------------------------//
418 This predicate holds it all its elements satisfy a given
419 condition, represented by the predicate.
421 \param Input An input sequence
422 \param Pred A predicate
423 \return The result of the test
425 \note This function provides the strong exception-safety guarantee
427 template<typename RangeT, typename PredicateT>
432 typedef BOOST_STRING_TYPENAME
433 range_const_iterator<RangeT>::type Iterator1T;
435 Iterator1T InputEnd=end(Input);
436 for( Iterator1T It=begin(Input); It!=InputEnd; ++It)
445 } // namespace algorithm
447 // pull names to the boost namespace
448 using algorithm::starts_with;
449 using algorithm::istarts_with;
450 using algorithm::ends_with;
451 using algorithm::iends_with;
452 using algorithm::contains;
453 using algorithm::icontains;
454 using algorithm::equals;
455 using algorithm::iequals;
456 using algorithm::all;
457 using algorithm::lexicographical_compare;
458 using algorithm::ilexicographical_compare;
463 #endif // BOOST_STRING_PREDICATE_HPP