bcs_char_traits.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 : generic char traits class; wraps std::char_traits
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_UTILS_BCS_CHAR_TRAITS_HPP
  14. #define BOOST_TEST_UTILS_BCS_CHAR_TRAITS_HPP
  15. // Boost
  16. #include <boost/config.hpp>
  17. #include <boost/detail/workaround.hpp>
  18. #include <boost/test/detail/config.hpp>
  19. #include <boost/type_traits/add_const.hpp>
  20. // STL
  21. #include <string> // std::char_traits
  22. #include <cstddef> // std::size_t
  23. #include <boost/test/detail/suppress_warnings.hpp>
  24. //____________________________________________________________________________//
  25. namespace boost {
  26. namespace unit_test {
  27. namespace ut_detail {
  28. template<typename CharT> struct bcs_base_char { typedef CharT type; };
  29. template<> struct bcs_base_char<char const> { typedef char type; };
  30. template<> struct bcs_base_char<unsigned char> { typedef char type; };
  31. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
  32. template<> struct bcs_base_char<unsigned char const> { typedef char type; };
  33. #endif
  34. template<> struct bcs_base_char<wchar_t const> { typedef wchar_t type; };
  35. // ************************************************************************** //
  36. // ************** bcs_char_traits ************** //
  37. // ************************************************************************** //
  38. template<typename CharT>
  39. struct bcs_char_traits_impl
  40. {
  41. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  42. typedef CharT const const_char;
  43. #else
  44. typedef typename boost::add_const<CharT>::type const_char;
  45. #endif
  46. static bool eq( CharT c1, CharT c2 )
  47. {
  48. return c1 == c2;
  49. }
  50. static bool lt( CharT c1, CharT c2 )
  51. {
  52. return c1 < c2;
  53. }
  54. static int compare( const_char* cstr1, const_char* cstr2, std::size_t n )
  55. {
  56. while( n > 0 ) {
  57. if( !eq( *cstr1, *cstr2 ) )
  58. return lt( *cstr1, *cstr2 ) ? -1 : 1;
  59. ++cstr1;
  60. ++cstr2;
  61. --n;
  62. }
  63. return 0;
  64. }
  65. static std::size_t length( const_char* cstr )
  66. {
  67. const_char null_char = CharT();
  68. const_char* ptr = cstr;
  69. while( !eq( *ptr, null_char ) )
  70. ++ptr;
  71. return ptr - cstr;
  72. }
  73. static const_char* find( const_char* s, std::size_t n, CharT c )
  74. {
  75. while( n > 0 ) {
  76. if( eq( *s, c ) )
  77. return s;
  78. ++s;
  79. --n;
  80. }
  81. return 0;
  82. }
  83. };
  84. #ifdef BOOST_CLASSIC_IOSTREAMS
  85. template<typename CharT>
  86. struct char_traits_with_find : std::string_char_traits<CharT> {
  87. static CharT const* find( CharT const* s, std::size_t n, CharT c )
  88. {
  89. while( n > 0 ) {
  90. if( eq( *s, c ) )
  91. return s;
  92. ++s;
  93. --n;
  94. }
  95. return 0;
  96. }
  97. };
  98. template<> struct bcs_char_traits_impl<char> : public char_traits_with_find<char> {};
  99. template<> struct bcs_char_traits_impl<wchar_t> : public char_traits_with_find<wchar_t> {};
  100. #else
  101. template<> struct bcs_char_traits_impl<char> : public std::char_traits<char> {};
  102. template<> struct bcs_char_traits_impl<wchar_t> : public std::char_traits<wchar_t> {};
  103. #endif
  104. template<typename CharT>
  105. class bcs_char_traits : public bcs_char_traits_impl<CharT> {
  106. typedef typename ut_detail::bcs_base_char<CharT>::type the_base_char;
  107. public:
  108. #ifdef BOOST_CLASSIC_IOSTREAMS
  109. typedef std::basic_string<the_base_char, std::string_char_traits<the_base_char> > std_string;
  110. #else
  111. typedef std::basic_string<the_base_char, std::char_traits<the_base_char> > std_string;
  112. #endif
  113. };
  114. } // namespace ut_detail
  115. } // namespace unit_test
  116. } // namespace boost
  117. //____________________________________________________________________________//
  118. #include <boost/test/detail/enable_warnings.hpp>
  119. #endif // BOOST_TEST_UTILS_BCS_CHAR_TRAITS_HPP