compare.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Boost string_algo library compare.hpp header file -------------------------//
  2. // Copyright Pavol Droba 2002-2006.
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // See http://www.boost.org/ for updates, documentation, and revision history.
  8. #ifndef BOOST_STRING_COMPARE_HPP
  9. #define BOOST_STRING_COMPARE_HPP
  10. #include <boost/algorithm/string/config.hpp>
  11. #include <locale>
  12. /*! \file
  13. Defines element comparison predicates. Many algorithms in this library can
  14. take an additional argument with a predicate used to compare elements.
  15. This makes it possible, for instance, to have case insensitive versions
  16. of the algorithms.
  17. */
  18. namespace boost {
  19. namespace algorithm {
  20. // is_equal functor -----------------------------------------------//
  21. //! is_equal functor
  22. /*!
  23. Standard STL equal_to only handle comparison between arguments
  24. of the same type. This is a less restrictive version which wraps operator ==.
  25. */
  26. struct is_equal
  27. {
  28. //! Function operator
  29. /*!
  30. Compare two operands for equality
  31. */
  32. template< typename T1, typename T2 >
  33. bool operator()( const T1& Arg1, const T2& Arg2 ) const
  34. {
  35. return Arg1==Arg2;
  36. }
  37. };
  38. //! case insensitive version of is_equal
  39. /*!
  40. Case insensitive comparison predicate. Comparison is done using
  41. specified locales.
  42. */
  43. struct is_iequal
  44. {
  45. //! Constructor
  46. /*!
  47. \param Loc locales used for comparison
  48. */
  49. is_iequal( const std::locale& Loc=std::locale() ) :
  50. m_Loc( Loc ) {}
  51. //! Function operator
  52. /*!
  53. Compare two operands. Case is ignored.
  54. */
  55. template< typename T1, typename T2 >
  56. bool operator()( const T1& Arg1, const T2& Arg2 ) const
  57. {
  58. #if defined(BOOST_BORLANDC) && (BOOST_BORLANDC >= 0x560) && (BOOST_BORLANDC <= 0x564) && !defined(_USE_OLD_RW_STL)
  59. return std::toupper(Arg1)==std::toupper(Arg2);
  60. #else
  61. return std::toupper<T1>(Arg1,m_Loc)==std::toupper<T2>(Arg2,m_Loc);
  62. #endif
  63. }
  64. private:
  65. std::locale m_Loc;
  66. };
  67. // is_less functor -----------------------------------------------//
  68. //! is_less functor
  69. /*!
  70. Convenient version of standard std::less. Operation is templated, therefore it is
  71. not required to specify the exact types upon the construction
  72. */
  73. struct is_less
  74. {
  75. //! Functor operation
  76. /*!
  77. Compare two operands using > operator
  78. */
  79. template< typename T1, typename T2 >
  80. bool operator()( const T1& Arg1, const T2& Arg2 ) const
  81. {
  82. return Arg1<Arg2;
  83. }
  84. };
  85. //! case insensitive version of is_less
  86. /*!
  87. Case insensitive comparison predicate. Comparison is done using
  88. specified locales.
  89. */
  90. struct is_iless
  91. {
  92. //! Constructor
  93. /*!
  94. \param Loc locales used for comparison
  95. */
  96. is_iless( const std::locale& Loc=std::locale() ) :
  97. m_Loc( Loc ) {}
  98. //! Function operator
  99. /*!
  100. Compare two operands. Case is ignored.
  101. */
  102. template< typename T1, typename T2 >
  103. bool operator()( const T1& Arg1, const T2& Arg2 ) const
  104. {
  105. #if defined(BOOST_BORLANDC) && (BOOST_BORLANDC >= 0x560) && (BOOST_BORLANDC <= 0x564) && !defined(_USE_OLD_RW_STL)
  106. return std::toupper(Arg1)<std::toupper(Arg2);
  107. #else
  108. return std::toupper<T1>(Arg1,m_Loc)<std::toupper<T2>(Arg2,m_Loc);
  109. #endif
  110. }
  111. private:
  112. std::locale m_Loc;
  113. };
  114. // is_not_greater functor -----------------------------------------------//
  115. //! is_not_greater functor
  116. /*!
  117. Convenient version of standard std::not_greater_to. Operation is templated, therefore it is
  118. not required to specify the exact types upon the construction
  119. */
  120. struct is_not_greater
  121. {
  122. //! Functor operation
  123. /*!
  124. Compare two operands using > operator
  125. */
  126. template< typename T1, typename T2 >
  127. bool operator()( const T1& Arg1, const T2& Arg2 ) const
  128. {
  129. return Arg1<=Arg2;
  130. }
  131. };
  132. //! case insensitive version of is_not_greater
  133. /*!
  134. Case insensitive comparison predicate. Comparison is done using
  135. specified locales.
  136. */
  137. struct is_not_igreater
  138. {
  139. //! Constructor
  140. /*!
  141. \param Loc locales used for comparison
  142. */
  143. is_not_igreater( const std::locale& Loc=std::locale() ) :
  144. m_Loc( Loc ) {}
  145. //! Function operator
  146. /*!
  147. Compare two operands. Case is ignored.
  148. */
  149. template< typename T1, typename T2 >
  150. bool operator()( const T1& Arg1, const T2& Arg2 ) const
  151. {
  152. #if defined(BOOST_BORLANDC) && (BOOST_BORLANDC >= 0x560) && (BOOST_BORLANDC <= 0x564) && !defined(_USE_OLD_RW_STL)
  153. return std::toupper(Arg1)<=std::toupper(Arg2);
  154. #else
  155. return std::toupper<T1>(Arg1,m_Loc)<=std::toupper<T2>(Arg2,m_Loc);
  156. #endif
  157. }
  158. private:
  159. std::locale m_Loc;
  160. };
  161. } // namespace algorithm
  162. // pull names to the boost namespace
  163. using algorithm::is_equal;
  164. using algorithm::is_iequal;
  165. using algorithm::is_less;
  166. using algorithm::is_iless;
  167. using algorithm::is_not_greater;
  168. using algorithm::is_not_igreater;
  169. } // namespace boost
  170. #endif // BOOST_STRING_COMPARE_HPP