Chris@16: // Boost string_algo library compare.hpp header file -------------------------// Chris@16: Chris@16: // Copyright Pavol Droba 2002-2006. Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org/ for updates, documentation, and revision history. Chris@16: Chris@16: #ifndef BOOST_STRING_COMPARE_HPP Chris@16: #define BOOST_STRING_COMPARE_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: /*! \file Chris@16: Defines element comparison predicates. Many algorithms in this library can Chris@16: take an additional argument with a predicate used to compare elements. Chris@16: This makes it possible, for instance, to have case insensitive versions Chris@16: of the algorithms. Chris@16: */ Chris@16: Chris@16: namespace boost { Chris@16: namespace algorithm { Chris@16: Chris@16: // is_equal functor -----------------------------------------------// Chris@16: Chris@16: //! is_equal functor Chris@16: /*! Chris@16: Standard STL equal_to only handle comparison between arguments Chris@16: of the same type. This is a less restrictive version which wraps operator ==. Chris@16: */ Chris@16: struct is_equal Chris@16: { Chris@16: //! Function operator Chris@16: /*! Chris@16: Compare two operands for equality Chris@16: */ Chris@16: template< typename T1, typename T2 > Chris@16: bool operator()( const T1& Arg1, const T2& Arg2 ) const Chris@16: { Chris@16: return Arg1==Arg2; Chris@16: } Chris@16: }; Chris@16: Chris@16: //! case insensitive version of is_equal Chris@16: /*! Chris@16: Case insensitive comparison predicate. Comparison is done using Chris@16: specified locales. Chris@16: */ Chris@16: struct is_iequal Chris@16: { Chris@16: //! Constructor Chris@16: /*! Chris@16: \param Loc locales used for comparison Chris@16: */ Chris@16: is_iequal( const std::locale& Loc=std::locale() ) : Chris@16: m_Loc( Loc ) {} Chris@16: Chris@16: //! Function operator Chris@16: /*! Chris@16: Compare two operands. Case is ignored. Chris@16: */ Chris@16: template< typename T1, typename T2 > Chris@16: bool operator()( const T1& Arg1, const T2& Arg2 ) const Chris@16: { Chris@16: #if defined(__BORLANDC__) && (__BORLANDC__ >= 0x560) && (__BORLANDC__ <= 0x564) && !defined(_USE_OLD_RW_STL) Chris@16: return std::toupper(Arg1)==std::toupper(Arg2); Chris@16: #else Chris@16: return std::toupper(Arg1,m_Loc)==std::toupper(Arg2,m_Loc); Chris@16: #endif Chris@16: } Chris@16: Chris@16: private: Chris@16: std::locale m_Loc; Chris@16: }; Chris@16: Chris@16: // is_less functor -----------------------------------------------// Chris@16: Chris@16: //! is_less functor Chris@16: /*! Chris@16: Convenient version of standard std::less. Operation is templated, therefore it is Chris@16: not required to specify the exact types upon the construction Chris@16: */ Chris@16: struct is_less Chris@16: { Chris@16: //! Functor operation Chris@16: /*! Chris@16: Compare two operands using > operator Chris@16: */ Chris@16: template< typename T1, typename T2 > Chris@16: bool operator()( const T1& Arg1, const T2& Arg2 ) const Chris@16: { Chris@16: return Arg1 Chris@16: bool operator()( const T1& Arg1, const T2& Arg2 ) const Chris@16: { Chris@16: #if defined(__BORLANDC__) && (__BORLANDC__ >= 0x560) && (__BORLANDC__ <= 0x564) && !defined(_USE_OLD_RW_STL) Chris@16: return std::toupper(Arg1)(Arg1,m_Loc)(Arg2,m_Loc); Chris@16: #endif Chris@16: } Chris@16: Chris@16: private: Chris@16: std::locale m_Loc; Chris@16: }; Chris@16: Chris@16: // is_not_greater functor -----------------------------------------------// Chris@16: Chris@16: //! is_not_greater functor Chris@16: /*! Chris@16: Convenient version of standard std::not_greater_to. Operation is templated, therefore it is Chris@16: not required to specify the exact types upon the construction Chris@16: */ Chris@16: struct is_not_greater Chris@16: { Chris@16: //! Functor operation Chris@16: /*! Chris@16: Compare two operands using > operator Chris@16: */ Chris@16: template< typename T1, typename T2 > Chris@16: bool operator()( const T1& Arg1, const T2& Arg2 ) const Chris@16: { Chris@16: return Arg1<=Arg2; Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: //! case insensitive version of is_not_greater Chris@16: /*! Chris@16: Case insensitive comparison predicate. Comparison is done using Chris@16: specified locales. Chris@16: */ Chris@16: struct is_not_igreater Chris@16: { Chris@16: //! Constructor Chris@16: /*! Chris@16: \param Loc locales used for comparison Chris@16: */ Chris@16: is_not_igreater( const std::locale& Loc=std::locale() ) : Chris@16: m_Loc( Loc ) {} Chris@16: Chris@16: //! Function operator Chris@16: /*! Chris@16: Compare two operands. Case is ignored. Chris@16: */ Chris@16: template< typename T1, typename T2 > Chris@16: bool operator()( const T1& Arg1, const T2& Arg2 ) const Chris@16: { Chris@16: #if defined(__BORLANDC__) && (__BORLANDC__ >= 0x560) && (__BORLANDC__ <= 0x564) && !defined(_USE_OLD_RW_STL) Chris@16: return std::toupper(Arg1)<=std::toupper(Arg2); Chris@16: #else Chris@16: return std::toupper(Arg1,m_Loc)<=std::toupper(Arg2,m_Loc); Chris@16: #endif Chris@16: } Chris@16: Chris@16: private: Chris@16: std::locale m_Loc; Chris@16: }; Chris@16: Chris@16: Chris@16: } // namespace algorithm Chris@16: Chris@16: // pull names to the boost namespace Chris@16: using algorithm::is_equal; Chris@16: using algorithm::is_iequal; Chris@16: using algorithm::is_less; Chris@16: using algorithm::is_iless; Chris@16: using algorithm::is_not_greater; Chris@16: using algorithm::is_not_igreater; Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: Chris@16: #endif // BOOST_STRING_COMPARE_HPP