_strdef.mh

来自「开放源码的编译器open watcom 1.6.0版的源代码」· MH 代码 · 共 532 行 · 第 1/2 页

MH
532
字号
///////////////////////////////////////////////////////////////////////////
// FILE: _strdef.h (Definition of std::string)
//
:keep CPP_HDR
:include crwatcnt.sp
//
// Description: This is a helper header. It contains the definition of
//              std::string and enough other information so that the
//              standard exception classes can use strings. Yet this
//              header does not require the inclusion of the exception
//              headers and thus mutually recursive inclusions are
//              avoided.
///////////////////////////////////////////////////////////////////////////
#ifndef __STRDEF_H_INCLUDED
#define __STRDEF_H_INCLUDED

:include readonly.sp

#ifndef __cplusplus
#error The header _strdef.h requires C++
#endif

#if !defined(_STDEXCEPT_INCLUDED)
#error The header _strdef.h is not to be directly included in user programs
#endif

#ifndef _CSTRING_INCLUDED
  #include <cstring>
#endif

#ifndef _CWCHAR_INCLUDED
  #include <cwchar>
#endif

#ifndef _ITERATOR_INCLUDED
  #include <iterator>
#endif

#ifndef _MEMORY_INCLUDED
  #include <memory>
#endif

namespace std {

  // struct char_traits
  // ******************
  template< class CharT > struct char_traits { };

  // struct char_traits< char >
  // **************************
  template< >
  struct char_traits< char > {
    typedef char      char_type;
    typedef int       int_type;
    typedef mbstate_t state_type;

    static void assign( char_type &c1, const char_type &c2 )
      { c1 = c2; }

    static bool eq( const char_type &c1, const char_type &c2 )
      { return( c1 == c2 ); }

    static bool lt( const char_type &c1, const char_type &c2 )
      { return( c1 < c2 ); }

    static int compare( const char_type *s1, const char_type *s2, size_t n )
      { return( memcmp(s1, s2, n ) ); }

    static size_t length( const char_type *s )
      { return( strlen(s) ); }

    static const char_type *find( const char_type *s, size_t n, const char_type &a )
      { return( static_cast< char_type * >( memchr(s, a, n) ) ); }

    static char_type *move( char_type *s1, const char_type *s2, size_t n )
      { return( static_cast< char_type * >( memmove(s1, s2, n) ) ); }

    static char_type *copy( char_type *s1, const char_type *s2, size_t n )
      { return( static_cast< char_type * >( memcpy(s1, s2, n) ) ); }

    static char_type *assign( char_type *s, size_t n, char_type a )
      { return( static_cast< char_type * >( memset(s, a, n) ) ); }

    static int_type not_eof( const int_type &c )
      { return( (c != EOF) ? c : 0 ); }

    static char_type to_char_type( const int_type &c )
      { return( static_cast< char_type >(c) ); }

    static int_type to_int_type( const char_type &c )
      { return( static_cast< int_type >(c) ); }

    static bool eq_int_type( const int_type &c1, const int_type &c2 )
      { return( c1 == c2 ); }

    static int_type eof( )
      { return( EOF ); }
  };

  // struct char_traits< wchar_t >
  // *****************************
  template< >
  struct char_traits< wchar_t > {
    typedef wchar_t   char_type;
    typedef wint_t    int_type;
    typedef mbstate_t state_type;

    static void assign( char_type &c1, const char_type &c2 )
      { c1 = c2; }

    static bool eq( const char_type &c1, const char_type &c2 )
      { return( c1 == c2 ); }

    static bool lt( const char_type &c1, const char_type &c2 )
      { return( c1 < c2 ); }

    static int compare( const char_type *s1, const char_type *s2, size_t n )
    {
      for( size_t i = 0; i < n; ++i ) {
        if( *s1 < *s2 ) return( -1 );
        if( *s1 > *s2 ) return(  1 );
        ++s1; ++s2;
      }
      return( 0 );
    }

    static size_t length( const char_type *s )
      { return( wcslen(s) ); }

    static const char_type *find( const char_type *s, size_t n, const char_type &a )
    {
      const char_type *result = 0;
      for( size_t i = 0; i < n; ++i ) {
        if( *s == a ) {
          result = s;
          break;
        }
        ++s;
      }
      return( result );
    }

    static char_type *move( char_type *s1, const char_type *s2, size_t n )
    {
      return( static_cast< char_type * >
              ( memmove(s1, s2, n * sizeof( char_type ) ) ) );
    }

    static char_type *copy( char_type *s1, const char_type *s2, size_t n )
    {
      return( static_cast< char_type * >
              ( memcpy(s1, s2, n * sizeof( char_type ) ) ) );
    }

    static char_type *assign( char_type *s, size_t n, char_type a )
    {
      char_type *p = s;
      for( size_t i = 0; i < n; ++i ) {
        *p = a;
        ++p;
      }
      return( s );
    }

    static int_type not_eof( const int_type &c )
      { return( (c != WEOF) ? c : static_cast< int_type >( 0 ) ); }

    static char_type to_char_type( const int_type &c )
      { return( static_cast< char_type >(c) ); }

    static int_type to_int_type( const char_type &c )
      { return( static_cast< int_type >(c) ); }

    static bool eq_int_type( const int_type &c1, const int_type &c2 )
      { return( c1 == c2 ); }

    static int_type eof( )
      { return( WEOF ); }
  };

} // namespace std


namespace std {

  // ===================================
  // Definition of basic_string template
  // ===================================

  template< class CharT,
            class Traits = char_traits< CharT >,
            class Allocator = allocator< CharT > >
  class basic_string {

    friend
    bool operator==( const basic_string< CharT, Traits, Allocator > &left,
                     const basic_string< CharT, Traits, Allocator > &right );
          
    friend
    bool operator==( const CharT *left,
                     const basic_string< CharT, Traits, Allocator > &right );

    friend
    bool operator==( const basic_string< CharT, Traits, Allocator > &left,
                     const CharT *right );

    friend
    bool operator<( const basic_string< CharT, Traits, Allocator > &left,
                    const basic_string< CharT, Traits, Allocator > &right );

    friend
    bool operator<( const CharT *left,
                    const basic_string< CharT, Traits, Allocator > &right );

    friend
    bool operator<( const basic_string< CharT, Traits, Allocator > &left,
                    const CharT *right );

    friend
    void swap( basic_string< CharT, Traits, Allocator > &left,
               basic_string< CharT, Traits, Allocator > &right );

    public:
      typedef          Traits                     traits_type;
      typedef typename Traits::char_type          value_type;
      typedef          Allocator                  allocator_type;
      typedef typename Allocator::size_type       size_type;
      typedef typename Allocator::difference_type difference_type;
      typedef typename Allocator::reference       reference;
      typedef typename Allocator::const_reference const_reference;
      typedef typename Allocator::pointer         pointer;
      typedef typename Allocator::const_pointer   const_pointer;

      typedef pointer                                 iterator;
      typedef const_pointer                           const_iterator;
      typedef std::reverse_iterator< iterator >       reverse_iterator;
      typedef std::reverse_iterator< const_iterator > const_reverse_iterator;

      static const size_type npos = static_cast< size_type >( -1 );

      // Constructors.
      explicit basic_string( const Allocator &a = Allocator( ) );
      basic_string( const basic_string &str );
      basic_string( const basic_string &str, size_type pos, size_type n = npos, const Allocator &a = Allocator( ) );
      basic_string( const CharT *s, size_type n, const Allocator &a = Allocator( ) );
      basic_string( const CharT *s, const Allocator &a = Allocator( ) );
      basic_string( size_type n, CharT c, const Allocator &a = Allocator( ) );

     ~basic_string( );

      basic_string &operator=( const basic_string &str );
      basic_string &operator=( const CharT *s );
      basic_string &operator=( CharT c );

      basic_string &assign( const basic_string &str, size_type pos = 0, size_type n = npos );
      basic_string &assign( const CharT *s, size_type n );
      basic_string &assign( const CharT *s );
      basic_string &assign( size_type n, CharT c );

      iterator       begin( )         { return( buffer ); }
      const_iterator begin( ) const   { return( buffer ); }
      iterator       end( )           { return( buffer + str_length ); }
      const_iterator end( )   const   { return( buffer + str_length ); }

      reverse_iterator rbegin( )
          { return( reverse_iterator( buffer + str_length ) ); }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?