Imported Upstream version 0.8~alpha1
[platform/upstream/syncevolution.git] / src / boost / algorithm / string / predicate.hpp
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 ( case 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 // lexicographical_compare predicate -----------------------------//
335
336         //! Lexicographical compare predicate
337         /*!
338              This predicate is an overload of std::lexicographical_compare
339              for range arguments
340
341              It check whether the first argument is lexicographically less
342              then the second one.
343
344              If the optional predicate is specified, it is used for character-wise
345              comparison
346
347              \param Arg1 First argument 
348              \param Arg2 Second argument
349              \param Pred Comparison predicate
350              \return The result of the test
351
352              \note This function provides the strong exception-safety guarantee
353          */
354         template<typename Range1T, typename Range2T, typename PredicateT>
355         inline bool lexicographical_compare(
356             const Range1T& Arg1,
357             const Range2T& Arg2,
358             PredicateT Pred)
359         {
360             return std::lexicographical_compare(
361                 begin(Arg1),
362                 end(Arg1),
363                 begin(Arg2),
364                 end(Arg2),
365                 Pred);
366         }
367
368         //! Lexicographical compare predicate
369         /*!
370             \overload
371          */
372         template<typename Range1T, typename Range2T>
373             inline bool lexicographical_compare(
374             const Range1T& Arg1,
375             const Range2T& Arg2)
376         {
377             return std::lexicographical_compare(
378                 begin(Arg1),
379                 end(Arg1),
380                 begin(Arg2),
381                 end(Arg2),
382                 is_less());
383         }
384
385         //! Lexicographical compare predicate (case-insensitive)
386         /*!
387             This predicate is an overload of std::lexicographical_compare
388             for range arguments.
389             It check whether the first argument is lexicographically less
390             then the second one.
391             Elements are compared case insensitively
392
393
394              \param Arg1 First argument 
395              \param Arg2 Second argument
396              \return The result of the test
397
398              \note This function provides the strong exception-safety guarantee
399          */
400         template<typename Range1T, typename Range2T>
401         inline bool ilexicographical_compare(
402             const Range1T& Arg1,
403             const Range2T& Arg2)
404         {
405             return std::lexicographical_compare(
406                 begin(Arg1),
407                 end(Arg1),
408                 begin(Arg2),
409                 end(Arg2),
410                 is_iless());
411         }
412         
413
414 //  all predicate  -----------------------------------------------//
415
416         //! 'All' predicate
417         /*!
418             This predicate holds it all its elements satisfy a given 
419             condition, represented by the predicate.
420             
421             \param Input An input sequence
422             \param Pred A predicate
423             \return The result of the test
424
425             \note This function provides the strong exception-safety guarantee
426         */
427         template<typename RangeT, typename PredicateT>
428         inline bool all( 
429             const RangeT& Input, 
430             PredicateT Pred)
431         {
432             typedef BOOST_STRING_TYPENAME 
433                 range_const_iterator<RangeT>::type Iterator1T;
434
435             Iterator1T InputEnd=end(Input);
436             for( Iterator1T It=begin(Input); It!=InputEnd; ++It)
437             {
438                 if (!Pred(*It))
439                     return false;
440             }
441             
442             return true;
443         }
444
445     } // namespace algorithm
446
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;
459
460 } // namespace boost
461
462
463 #endif  // BOOST_STRING_PREDICATE_HPP