compare.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // (C) Copyright Gennadiy Rozental 2004-2008.
  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: 49312 $
  10. //
  11. // Description : class basic_cstring comparisons implementation
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_BASIC_CSTRING_COMPARE_HPP_071894GER
  14. #define BOOST_TEST_BASIC_CSTRING_COMPARE_HPP_071894GER
  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 : public std::binary_function<basic_cstring<CharT>,basic_cstring<CharT>,bool>
  61. {
  62. public:
  63. bool operator()( basic_cstring<CharT> x, basic_cstring<CharT> y ) const
  64. {
  65. return x.size() != y.size()
  66. ? x.size() < y.size()
  67. : ut_detail::case_ins<CharT>::compare( x.begin(), y.begin(), x.size() ) < 0;
  68. }
  69. };
  70. //____________________________________________________________________________//
  71. // ************************************************************************** //
  72. // ************** operator < ************** //
  73. // ************************************************************************** //
  74. template<class CharT>
  75. inline bool
  76. operator <( boost::unit_test::basic_cstring<CharT> const& x,
  77. boost::unit_test::basic_cstring<CharT> const& y )
  78. {
  79. typedef typename boost::unit_test::basic_cstring<CharT>::traits_type traits_type;
  80. return x.size() != y.size()
  81. ? x.size() < y.size()
  82. : traits_type::compare( x.begin(), y.begin(), x.size() ) < 0;
  83. }
  84. } // namespace unit_test
  85. } // namespace boost
  86. //____________________________________________________________________________//
  87. #include <boost/test/detail/enable_warnings.hpp>
  88. #endif // BOOST_TEST_BASIC_CSTRING_COMPARE_HPP_071894GER