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