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