Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 09:55:54

0001 //  Boost string_algo library predicate.hpp header file  ---------------------------//
0002 
0003 //  Copyright Pavol Droba 2002-2003.
0004 //
0005 // Distributed under the Boost Software License, Version 1.0.
0006 //    (See accompanying file LICENSE_1_0.txt or copy at
0007 //          http://www.boost.org/LICENSE_1_0.txt)
0008 
0009 //  See http://www.boost.org/ for updates, documentation, and revision history.
0010 
0011 #ifndef BOOST_STRING_PREDICATE_HPP
0012 #define BOOST_STRING_PREDICATE_HPP
0013 
0014 #include <iterator>
0015 #include <boost/algorithm/string/config.hpp>
0016 #include <boost/range/begin.hpp>
0017 #include <boost/range/end.hpp>
0018 #include <boost/range/iterator.hpp>
0019 #include <boost/range/const_iterator.hpp>
0020 #include <boost/range/as_literal.hpp>
0021 #include <boost/range/iterator_range_core.hpp>
0022 
0023 #include <boost/algorithm/string/compare.hpp>
0024 #include <boost/algorithm/string/find.hpp>
0025 #include <boost/algorithm/string/detail/predicate.hpp>
0026 
0027 /*! \file boost/algorithm/string/predicate.hpp
0028     Defines string-related predicates. 
0029     The predicates determine whether a substring is contained in the input string 
0030     under various conditions: a string starts with the substring, ends with the 
0031     substring, simply contains the substring or if both strings are equal.
0032     Additionaly the algorithm \c all() checks all elements of a container to satisfy a 
0033     condition.
0034 
0035     All predicates provide the strong exception guarantee.
0036 */
0037 
0038 namespace boost {
0039     namespace algorithm {
0040 
0041 //  starts_with predicate  -----------------------------------------------//
0042 
0043         //! 'Starts with' predicate
0044         /*!
0045             This predicate holds when the test string is a prefix of the Input.
0046             In other words, if the input starts with the test.
0047             When the optional predicate is specified, it is used for character-wise
0048             comparison.
0049 
0050             \param Input An input sequence
0051             \param Test A test sequence
0052             \param Comp An element comparison predicate
0053             \return The result of the test
0054 
0055               \note This function provides the strong exception-safety guarantee
0056         */
0057         template<typename Range1T, typename Range2T, typename PredicateT>
0058             inline bool starts_with( 
0059             const Range1T& Input, 
0060             const Range2T& Test,
0061             PredicateT Comp)
0062         {
0063             iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range1T>::type> lit_input(::boost::as_literal(Input));
0064             iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range2T>::type> lit_test(::boost::as_literal(Test));
0065 
0066             typedef BOOST_STRING_TYPENAME 
0067                 range_const_iterator<Range1T>::type Iterator1T;
0068             typedef BOOST_STRING_TYPENAME 
0069                 range_const_iterator<Range2T>::type Iterator2T;
0070 
0071             Iterator1T InputEnd=::boost::end(lit_input);
0072             Iterator2T TestEnd=::boost::end(lit_test);
0073 
0074             Iterator1T it=::boost::begin(lit_input);
0075             Iterator2T pit=::boost::begin(lit_test);
0076             for(;
0077                 it!=InputEnd && pit!=TestEnd;
0078                 ++it,++pit)
0079             {
0080                 if( !(Comp(*it,*pit)) )
0081                     return false;
0082             }
0083 
0084             return pit==TestEnd;
0085         }
0086 
0087         //! 'Starts with' predicate
0088         /*!
0089             \overload
0090         */
0091         template<typename Range1T, typename Range2T>
0092         inline bool starts_with( 
0093             const Range1T& Input, 
0094             const Range2T& Test)
0095         {
0096             return ::boost::algorithm::starts_with(Input, Test, is_equal());
0097         }
0098 
0099         //! 'Starts with' predicate ( case insensitive )
0100         /*!
0101             This predicate holds when the test string is a prefix of the Input.
0102             In other words, if the input starts with the test.
0103             Elements are compared case insensitively.
0104 
0105             \param Input An input sequence
0106             \param Test A test sequence
0107             \param Loc A locale used for case insensitive comparison
0108             \return The result of the test
0109 
0110             \note This function provides the strong exception-safety guarantee
0111         */
0112         template<typename Range1T, typename Range2T>
0113         inline bool istarts_with( 
0114             const Range1T& Input, 
0115             const Range2T& Test,
0116             const std::locale& Loc=std::locale())
0117         {
0118             return ::boost::algorithm::starts_with(Input, Test, is_iequal(Loc));
0119         }
0120 
0121 
0122 //  ends_with predicate  -----------------------------------------------//
0123 
0124         //! 'Ends with' predicate
0125         /*!
0126             This predicate holds when the test string is a suffix of the Input.
0127             In other words, if the input ends with the test.
0128             When the optional predicate is specified, it is used for character-wise
0129             comparison.
0130 
0131 
0132             \param Input An input sequence
0133             \param Test A test sequence
0134             \param Comp An element comparison predicate
0135             \return The result of the test
0136 
0137               \note This function provides the strong exception-safety guarantee
0138         */
0139         template<typename Range1T, typename Range2T, typename PredicateT>
0140         inline bool ends_with( 
0141             const Range1T& Input, 
0142             const Range2T& Test,
0143             PredicateT Comp)
0144         {
0145             iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range1T>::type> lit_input(::boost::as_literal(Input));
0146             iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range2T>::type> lit_test(::boost::as_literal(Test));
0147 
0148             typedef BOOST_STRING_TYPENAME
0149                 range_const_iterator<Range1T>::type Iterator1T;
0150             typedef BOOST_STRING_TYPENAME
0151                 std::iterator_traits<Iterator1T>::iterator_category category;
0152 
0153             return detail::
0154                 ends_with_iter_select( 
0155                     ::boost::begin(lit_input), 
0156                     ::boost::end(lit_input), 
0157                     ::boost::begin(lit_test), 
0158                     ::boost::end(lit_test), 
0159                     Comp,
0160                     category());
0161         }
0162 
0163 
0164         //! 'Ends with' predicate
0165         /*!
0166             \overload
0167         */
0168         template<typename Range1T, typename Range2T>
0169         inline bool ends_with( 
0170             const Range1T& Input, 
0171             const Range2T& Test)
0172         {
0173             return ::boost::algorithm::ends_with(Input, Test, is_equal());
0174         }
0175 
0176         //! 'Ends with' predicate ( case insensitive )
0177         /*!
0178             This predicate holds when the test container is a suffix of the Input.
0179             In other words, if the input ends with the test.
0180             Elements are compared case insensitively.
0181 
0182             \param Input An input sequence
0183             \param Test A test sequence
0184             \param Loc A locale used for case insensitive comparison
0185             \return The result of the test
0186 
0187             \note This function provides the strong exception-safety guarantee
0188         */
0189         template<typename Range1T, typename Range2T>
0190         inline bool iends_with( 
0191             const Range1T& Input, 
0192             const Range2T& Test,
0193             const std::locale& Loc=std::locale())
0194         {
0195             return ::boost::algorithm::ends_with(Input, Test, is_iequal(Loc));
0196         }
0197 
0198 //  contains predicate  -----------------------------------------------//
0199 
0200         //! 'Contains' predicate
0201         /*!
0202             This predicate holds when the test container is contained in the Input.
0203             When the optional predicate is specified, it is used for character-wise
0204             comparison.
0205 
0206             \param Input An input sequence
0207             \param Test A test sequence
0208             \param Comp An element comparison predicate
0209             \return The result of the test
0210 
0211                \note This function provides the strong exception-safety guarantee
0212         */
0213         template<typename Range1T, typename Range2T, typename PredicateT>
0214         inline bool contains( 
0215             const Range1T& Input, 
0216             const Range2T& Test,
0217             PredicateT Comp)
0218         {
0219             iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range1T>::type> lit_input(::boost::as_literal(Input));
0220             iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range2T>::type> lit_test(::boost::as_literal(Test));
0221 
0222             if (::boost::empty(lit_test))
0223             {
0224                 // Empty range is contained always
0225                 return true;
0226             }
0227             
0228             // Use the temporary variable to make VACPP happy
0229             bool bResult=(::boost::algorithm::first_finder(lit_test,Comp)(::boost::begin(lit_input), ::boost::end(lit_input)));
0230             return bResult;
0231         }
0232 
0233         //! 'Contains' predicate
0234         /*!
0235             \overload
0236         */
0237         template<typename Range1T, typename Range2T>
0238         inline bool contains( 
0239             const Range1T& Input, 
0240             const Range2T& Test)
0241         {
0242             return ::boost::algorithm::contains(Input, Test, is_equal());
0243         }
0244 
0245         //! 'Contains' predicate ( case insensitive )
0246         /*!
0247             This predicate holds when the test container is contained in the Input.
0248             Elements are compared case insensitively.
0249 
0250             \param Input An input sequence
0251             \param Test A test sequence
0252             \param Loc A locale used for case insensitive comparison
0253             \return The result of the test
0254 
0255             \note This function provides the strong exception-safety guarantee
0256         */
0257         template<typename Range1T, typename Range2T>
0258         inline bool icontains( 
0259             const Range1T& Input, 
0260             const Range2T& Test, 
0261             const std::locale& Loc=std::locale())
0262         {
0263             return ::boost::algorithm::contains(Input, Test, is_iequal(Loc));
0264         }
0265 
0266 //  equals predicate  -----------------------------------------------//
0267 
0268         //! 'Equals' predicate
0269         /*!
0270             This predicate holds when the test container is equal to the
0271             input container i.e. all elements in both containers are same.
0272             When the optional predicate is specified, it is used for character-wise
0273             comparison.
0274 
0275             \param Input An input sequence
0276             \param Test A test sequence
0277             \param Comp An element comparison predicate
0278             \return The result of the test
0279 
0280             \note This is a two-way version of \c std::equal algorithm
0281 
0282             \note This function provides the strong exception-safety guarantee
0283         */
0284         template<typename Range1T, typename Range2T, typename PredicateT>
0285         inline bool equals( 
0286             const Range1T& Input, 
0287             const Range2T& Test,
0288             PredicateT Comp)
0289         {
0290             iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range1T>::type> lit_input(::boost::as_literal(Input));
0291             iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range2T>::type> lit_test(::boost::as_literal(Test));
0292 
0293             typedef BOOST_STRING_TYPENAME 
0294                 range_const_iterator<Range1T>::type Iterator1T;
0295             typedef BOOST_STRING_TYPENAME 
0296                 range_const_iterator<Range2T>::type Iterator2T;
0297                 
0298             Iterator1T InputEnd=::boost::end(lit_input);
0299             Iterator2T TestEnd=::boost::end(lit_test);
0300 
0301             Iterator1T it=::boost::begin(lit_input);
0302             Iterator2T pit=::boost::begin(lit_test);
0303             for(;
0304                 it!=InputEnd && pit!=TestEnd;
0305                 ++it,++pit)
0306             {
0307                 if( !(Comp(*it,*pit)) )
0308                     return false;
0309             }
0310 
0311             return  (pit==TestEnd) && (it==InputEnd);
0312         }
0313 
0314         //! 'Equals' predicate
0315         /*!
0316             \overload
0317         */
0318         template<typename Range1T, typename Range2T>
0319         inline bool equals( 
0320             const Range1T& Input, 
0321             const Range2T& Test)
0322         {
0323             return ::boost::algorithm::equals(Input, Test, is_equal());
0324         }
0325 
0326         //! 'Equals' predicate ( case insensitive )
0327         /*!
0328             This predicate holds when the test container is equal to the
0329             input container i.e. all elements in both containers are same.
0330             Elements are compared case insensitively.
0331 
0332             \param Input An input sequence
0333             \param Test A test sequence
0334             \param Loc A locale used for case insensitive comparison
0335             \return The result of the test
0336 
0337             \note This is a two-way version of \c std::equal algorithm
0338 
0339             \note This function provides the strong exception-safety guarantee
0340         */
0341         template<typename Range1T, typename Range2T>
0342         inline bool iequals( 
0343             const Range1T& Input, 
0344             const Range2T& Test,
0345             const std::locale& Loc=std::locale())
0346         {
0347             return ::boost::algorithm::equals(Input, Test, is_iequal(Loc));
0348         }
0349 
0350 // lexicographical_compare predicate -----------------------------//
0351 
0352         //! Lexicographical compare predicate
0353         /*!
0354              This predicate is an overload of std::lexicographical_compare
0355              for range arguments
0356 
0357              It check whether the first argument is lexicographically less
0358              then the second one.
0359 
0360              If the optional predicate is specified, it is used for character-wise
0361              comparison
0362 
0363              \param Arg1 First argument 
0364              \param Arg2 Second argument
0365              \param Pred Comparison predicate
0366              \return The result of the test
0367 
0368              \note This function provides the strong exception-safety guarantee
0369          */
0370         template<typename Range1T, typename Range2T, typename PredicateT>
0371         inline bool lexicographical_compare(
0372             const Range1T& Arg1,
0373             const Range2T& Arg2,
0374             PredicateT Pred)
0375         {
0376             iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range1T>::type> lit_arg1(::boost::as_literal(Arg1));
0377             iterator_range<BOOST_STRING_TYPENAME range_const_iterator<Range2T>::type> lit_arg2(::boost::as_literal(Arg2));
0378 
0379             return std::lexicographical_compare(
0380                 ::boost::begin(lit_arg1),
0381                 ::boost::end(lit_arg1),
0382                 ::boost::begin(lit_arg2),
0383                 ::boost::end(lit_arg2),
0384                 Pred);
0385         }
0386 
0387         //! Lexicographical compare predicate
0388         /*!
0389             \overload
0390          */
0391         template<typename Range1T, typename Range2T>
0392             inline bool lexicographical_compare(
0393             const Range1T& Arg1,
0394             const Range2T& Arg2)
0395         {
0396             return ::boost::algorithm::lexicographical_compare(Arg1, Arg2, is_less());
0397         }
0398 
0399         //! Lexicographical compare predicate (case-insensitive)
0400         /*!
0401             This predicate is an overload of std::lexicographical_compare
0402             for range arguments.
0403             It check whether the first argument is lexicographically less
0404             then the second one.
0405             Elements are compared case insensitively
0406 
0407 
0408              \param Arg1 First argument 
0409              \param Arg2 Second argument
0410              \param Loc A locale used for case insensitive comparison
0411              \return The result of the test
0412 
0413              \note This function provides the strong exception-safety guarantee
0414          */
0415         template<typename Range1T, typename Range2T>
0416         inline bool ilexicographical_compare(
0417             const Range1T& Arg1,
0418             const Range2T& Arg2,
0419             const std::locale& Loc=std::locale())
0420         {
0421             return ::boost::algorithm::lexicographical_compare(Arg1, Arg2, is_iless(Loc));
0422         }
0423         
0424 
0425 //  all predicate  -----------------------------------------------//
0426 
0427         //! 'All' predicate
0428         /*!
0429             This predicate holds it all its elements satisfy a given 
0430             condition, represented by the predicate.
0431             
0432             \param Input An input sequence
0433             \param Pred A predicate
0434             \return The result of the test
0435 
0436             \note This function provides the strong exception-safety guarantee
0437         */
0438         template<typename RangeT, typename PredicateT>
0439         inline bool all( 
0440             const RangeT& Input, 
0441             PredicateT Pred)
0442         {
0443             iterator_range<BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> lit_input(::boost::as_literal(Input));
0444 
0445             typedef BOOST_STRING_TYPENAME 
0446                 range_const_iterator<RangeT>::type Iterator1T;
0447 
0448             Iterator1T InputEnd=::boost::end(lit_input);
0449             for( Iterator1T It=::boost::begin(lit_input); It!=InputEnd; ++It)
0450             {
0451                 if (!Pred(*It))
0452                     return false;
0453             }
0454             
0455             return true;
0456         }
0457 
0458     } // namespace algorithm
0459 
0460     // pull names to the boost namespace
0461     using algorithm::starts_with;
0462     using algorithm::istarts_with;
0463     using algorithm::ends_with;
0464     using algorithm::iends_with;
0465     using algorithm::contains;
0466     using algorithm::icontains;
0467     using algorithm::equals;
0468     using algorithm::iequals;
0469     using algorithm::all;
0470     using algorithm::lexicographical_compare;
0471     using algorithm::ilexicographical_compare;
0472 
0473 } // namespace boost
0474 
0475 
0476 #endif  // BOOST_STRING_PREDICATE_HPP