compare.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // (C) Copyright Gennadiy Rozental 2001.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision$
  10. //
  11. // Description : class basic_cstring comparisons implementation
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_UTILS_BASIC_CSTRING_COMPARE_HPP
  14. #define BOOST_TEST_UTILS_BASIC_CSTRING_COMPARE_HPP
  15. // Boost.Test
  16. #include <boost/test/utils/basic_cstring/basic_cstring.hpp>
  17. // STL
  18. #include <functional>
  19. #include <cctype>
  20. #include <boost/test/detail/suppress_warnings.hpp>
  21. //____________________________________________________________________________//
  22. # if defined(BOOST_NO_STDC_NAMESPACE) && !BOOST_WORKAROUND(__BORLANDC__, <= 0x570)
  23. namespace std { using ::toupper; }
  24. # endif
  25. namespace boost {
  26. namespace unit_test {
  27. // ************************************************************************** //
  28. // ************** case_ins_compare ************** //
  29. // ************************************************************************** //
  30. namespace ut_detail {
  31. template<class CharT>
  32. struct case_ins
  33. {
  34. static bool eq( CharT c1, CharT c2 ) { return (std::toupper)( c1 ) == (std::toupper)( c2 ); }
  35. static bool lt( CharT c1, CharT c2 ) { return (std::toupper)( c1 ) < (std::toupper)( c2 ); }
  36. static int compare( CharT const* s1, CharT const* s2, std::size_t n )
  37. {
  38. for( std::size_t i = 0; i < n; ++i ) {
  39. if( !eq( s1[i], s2[i] ) )
  40. return lt( s1[i], s2[i] ) ? -1 : 1;
  41. }
  42. return 0;
  43. }
  44. };
  45. } // namespace ut_detail
  46. // ************************************************************************** //
  47. // ************** case_ins_eq ************** //
  48. // ************************************************************************** //
  49. template<class CharT>
  50. inline bool
  51. case_ins_eq( basic_cstring<CharT> x, basic_cstring<CharT> y )
  52. {
  53. return x.size() == y.size() && ut_detail::case_ins<CharT>::compare( x.begin(), y.begin(), x.size() ) == 0;
  54. }
  55. //____________________________________________________________________________//
  56. // ************************************************************************** //
  57. // ************** case_ins_less ************** //
  58. // ************************************************************************** //
  59. template<class CharT>
  60. class case_ins_less
  61. {
  62. public:
  63. typedef bool result_type;
  64. typedef basic_cstring<CharT> first_argument_type;
  65. typedef basic_cstring<CharT> second_argument_type;
  66. bool operator()( basic_cstring<CharT> x, basic_cstring<CharT> y ) const
  67. {
  68. return x.size() != y.size()
  69. ? x.size() < y.size()
  70. : ut_detail::case_ins<CharT>::compare( x.begin(), y.begin(), x.size() ) < 0;
  71. }
  72. };
  73. //____________________________________________________________________________//
  74. // ************************************************************************** //
  75. // ************** operators <,> ************** //
  76. // ************************************************************************** //
  77. template<class CharT>
  78. inline bool
  79. operator <( boost::unit_test::basic_cstring<CharT> const& x,
  80. boost::unit_test::basic_cstring<CharT> const& y )
  81. {
  82. typedef typename boost::unit_test::basic_cstring<CharT>::traits_type traits_type;
  83. return x.size() != y.size()
  84. ? x.size() < y.size()
  85. : traits_type::compare( x.begin(), y.begin(), x.size() ) < 0;
  86. }
  87. //____________________________________________________________________________//
  88. template<class CharT>
  89. inline bool
  90. operator <=( boost::unit_test::basic_cstring<CharT> const& x,
  91. boost::unit_test::basic_cstring<CharT> const& y )
  92. {
  93. return !(y < x);
  94. }
  95. //____________________________________________________________________________//
  96. template<class CharT>
  97. inline bool
  98. operator >( boost::unit_test::basic_cstring<CharT> const& x,
  99. boost::unit_test::basic_cstring<CharT> const& y )
  100. {
  101. return y < x;
  102. }
  103. //____________________________________________________________________________//
  104. template<class CharT>
  105. inline bool
  106. operator >=( boost::unit_test::basic_cstring<CharT> const& x,
  107. boost::unit_test::basic_cstring<CharT> const& y )
  108. {
  109. return !(x < y);
  110. }
  111. //____________________________________________________________________________//
  112. } // namespace unit_test
  113. } // namespace boost
  114. //____________________________________________________________________________//
  115. #include <boost/test/detail/enable_warnings.hpp>
  116. #endif // BOOST_TEST_BASIC_CSTRING_COMPARE_HPP_071894GER