string.mh

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

MH
1,867
字号
#ifdef  __INLINE_FUNCTIONS__
:include ext.sp
 #pragma intrinsic(movedata,_fmemchr,_fmemcmp,_fmemcpy,_fmemset)
 #pragma intrinsic(_fstrcat,_fstrcmp,_fstrcpy,_fstrlen)
#endif
:segment CNAME
namespace std {
  #pragma intrinsic(memchr,memcmp,memcpy,strcat,strcpy,strlen,strchr)
  #if defined(M_I86)
    #pragma intrinsic(memset,strcmp)
  #endif
}
:elsesegment
 #pragma intrinsic(memchr,memcmp,memcpy,strcat,strcpy,strlen,strchr)
 #if defined(M_I86)
  #pragma intrinsic(memset,strcmp)
 #endif
:endsegment
#endif  /* __INLINE_FUNCTIONS__ */

:segment !CNAME
:include saferpro.sp

_WCRTLINK extern errno_t memcpy_s( void * __restrict __s1, rsize_t __s1max, const void * __restrict __s2, rsize_t __n );
_WCRTLINK extern errno_t memmove_s( void *__s1, rsize_t __s1max, const void *__s2, rsize_t __n );
_WCRTLINK extern errno_t strcpy_s( char * __restrict __s1, rsize_t __s1max, const char * __restrict __s2 );
_WCRTLINK extern errno_t strncpy_s( char * __restrict s1, rsize_t __s1max, const char * __restrict __s2, rsize_t __n );

_WCRTLINK extern errno_t strcat_s( char * __restrict __s1, rsize_t __s1max, const char * __restrict __s2 );
_WCRTLINK extern errno_t strncat_s( char * __restrict __s1, rsize_t __s1max, const char * __restrict __s2, rsize_t __n );

_WCRTLINK extern char    *strtok_s( char * __restrict __s1, rsize_t * __restrict __s1max, const char * __restrict __s2, char ** __restrict __ptr );

_WCRTLINK extern errno_t strerror_s( char *__s, rsize_t __maxsize, errno_t __errnum );
_WCRTLINK extern size_t  strerrorlen_s( errno_t __errnum );
_WCRTLINK extern size_t  strnlen_s( const char *__s, size_t __maxsize );

:include saferepi.sp
:endsegment

:segment CNAME
:include cplusepi.sp
:endsegment

:segment !CNAME
#endif /* __cplusplus */
:endsegment

#endif
:elsesegment
///////////////////////////////////////////////////////////////////////////
// FILE: string (Definition of character traits and std::string)
//
:keep CPP_HDR
:include crwatcnt.sp
//
// Description: This header is part of the C++ standard library. It
//              defines the character traits template and the std::
//              basic_string template.
///////////////////////////////////////////////////////////////////////////
#ifndef _STRING_INCLUDED
#define _STRING_INCLUDED
:include readonly.sp

#ifndef __cplusplus
#error The header string requires C++
#endif

#ifndef _CCTYPE_INCLUDED
  #include <cctype>
#endif

:: The header <_strdef.h> is indirectly included below.
#ifndef _STDEXCEPT_INCLUDED
  #include <stdexcep>
#endif

namespace _watcom {

  // ============================================================
  // Character traits specialization for case insensitive strings
  // ============================================================

  struct ichar_traits : std::char_traits< char > {

    // Use tolower for compatibility with _memicmp.
    static bool eq( const char_type &c1, const char_type &c2 )
      { return( std::tolower( c1 ) == std::tolower( c2 ) ); }

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

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

    // Too bad there's no _memichr.
    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( std::tolower( *s ) == std::tolower( a ) ) {
          result = s;
          break;
        }
        ++s;
      }
      return( result );
    }

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

  };
  typedef std::basic_string< char, ichar_traits > istring;
}
:include namwat.sp

namespace std {

  // ================================
  // Member functions of basic_string
  // ================================

  // basic_string( const basic_string &, size_type, size_type, ... )
  // ***************************************************************
  template< class CharT, class Traits, class Allocator >
  basic_string< CharT, Traits, Allocator >::basic_string(
    const basic_string &other,
    size_type  pos,
    size_type  n,
    const Allocator &a ) : mem( a )
  {
    if( pos > other.str_length )
      throw out_of_range( "basic_string::basic_string" );

    size_type tail_length = other.str_length - pos;
    size_type result_length = ( tail_length <= n ) ? tail_length : n;

    buffer = alloc( result_length + 1, buf_length );
    Traits::copy( buffer, other.buffer + pos, result_length );
    str_length = result_length;
  }

  // basic_string( const CharT *, size_type, const Allocator & )
  // ***********************************************************
  template< class CharT, class Traits, class Allocator >
  basic_string< CharT, Traits, Allocator >::basic_string(
    const CharT     *s,
    size_type        n,
    const Allocator &a ) : mem( a )
  {
    if( n > max_size( ) )
      throw length_error( "basic_string::basic_string" );

    buffer = alloc( n + 1, buf_length );
    Traits::copy( buffer, s, n );
    str_length = n;
  }

  // basic_string( size_type, CharT, const Allocator & )
  // ***************************************************
  template< class CharT, class Traits, class Allocator >
  basic_string< CharT, Traits, Allocator >::basic_string(
          size_type  n,
          CharT      c,
    const Allocator &a ) : mem( a )
  {
    if( n > max_size( ) )
      throw length_error( "basic_string::basic_string" );

    buffer = alloc( n + 1, buf_length );
    Traits::assign( buffer, n, c );
    str_length = n;
  }

  // operator=( const CharT * )
  // **************************
  template< class CharT, class Traits, class Allocator >
  basic_string< CharT, Traits, Allocator > &
    basic_string< CharT, Traits, Allocator >::operator=(
      const CharT *s )
  {
    size_type other_length = Traits::length( s );

    if( buf_length > other_length ) {
      Traits::copy( buffer, s, other_length );
      str_length = other_length;
    }
    else {
      replace_buffer( s, other_length );
    }
    return( *this );
  }

  // operator=( CharT )
  // ******************
  template< class CharT, class Traits, class Allocator >
  basic_string< CharT, Traits, Allocator > &
    basic_string< CharT, Traits, Allocator >::operator=(
      CharT c )
  {
    Traits::assign( *buffer, c );
    str_length = 1;
    return( *this );
  }

  // assign( const basic_string &, size_type, size_type )
  // ****************************************************
  template< class CharT, class Traits, class Allocator >
  basic_string< CharT, Traits, Allocator > &
    basic_string< CharT, Traits, Allocator >::assign(
      const basic_string &str,
      size_type pos,
      size_type n )
  {
    basic_string temp( str, pos, n, mem );
    swap( temp );
    return( *this );
  }

  // assign( const CharT *, size_type )
  // **********************************
  template< class CharT, class Traits, class Allocator >
  basic_string< CharT, Traits, Allocator > &
    basic_string< CharT, Traits, Allocator >::assign(
      const CharT *s,
      size_type n )
  {
    basic_string temp( s, n, mem );
    swap( temp );
    return( *this );
  }

  // assign( const CharT * )
  // ***********************
  template< class CharT, class Traits, class Allocator >
  basic_string< CharT, Traits, Allocator > &
    basic_string< CharT, Traits, Allocator >::assign(
      const CharT *s )
  {
    basic_string temp( s, mem );
    swap( temp );
    return( *this );
  }

  // assign( size_type, CharT )
  // **************************
  template< class CharT, class Traits, class Allocator >
  basic_string< CharT, Traits, Allocator > &
    basic_string< CharT, Traits, Allocator >::assign(
      size_type n,
      CharT c )
  {
    basic_string temp( n, c, mem );
    swap( temp );
    return( *this );
  }

  // resize( size_type, CharT )
  // **************************
  template< class CharT, class Traits, class Allocator >
  void basic_string< CharT, Traits, Allocator >::resize(
    size_type n,
    CharT c )
  {
    if( n <= str_length ) {
      str_length = n;
      return;
    }

    if( n < buf_length ) {
      Traits::assign( buffer + str_length, n - str_length, c );
      str_length = n;
      return;
    }

    size_type new_length;
    pointer new_buffer = alloc( n + 1, new_length );
    Traits::copy( new_buffer, buffer, str_length );
    Traits::assign( new_buffer + str_length, n - str_length, c );
    mem.deallocate( buffer, buf_length );
    buffer = new_buffer;
    buf_length = new_length;
    str_length = n;
  }

  // reserve( size_type )
  // ********************
  template< class CharT, class Traits, class Allocator >
  void basic_string< CharT, Traits, Allocator >::reserve(
    size_type new_capacity )
  {
    if( new_capacity < buf_length ) return;

    size_type new_length;
    pointer new_buffer = alloc( new_capacity + 1, new_length );
    Traits::copy( new_buffer, buffer, str_length );
    mem.deallocate( buffer, buf_length );
    buffer = new_buffer;
    buf_length  = new_length;
  }

  // at( size_type ) const
  // *********************
  template< class CharT, class Traits, class Allocator >
  inline
  typename basic_string< CharT, Traits, Allocator >::const_reference
    basic_string< CharT, Traits, Allocator >::at( size_type pos ) const
  {
    if( pos >= str_length )
      throw out_of_range( "basic_string::at" );

    return( buffer[pos] );
  }

  // at( size_type )
  // ***************
  template< class CharT, class Traits, class Allocator >
  inline
  typename basic_string< CharT, Traits, Allocator >::reference
    basic_string< CharT, Traits, Allocator >::at( size_type pos )
  {
    if( pos >= str_length )
      throw out_of_range( "basic_string::at" );

    return( buffer[pos] );
  }

  // operator+=( const basic_string & )
  // **********************************
  template< class CharT, class Traits, class Allocator >
  inline
  basic_string< CharT, Traits, Allocator > &
    basic_string< CharT, Traits, Allocator >::operator+=(
      const basic_string &str )
  {
    return( append( str ) );
  }

  // operator+=( const CharT * )
  // ***************************
  template< class CharT, class Traits, class Allocator >
  inline
  basic_string< CharT, Traits, Allocator > &
    basic_string< CharT, Traits, Allocator >::operator+=( const CharT *s )
  {
    return( append( s ) );
  }

  // operator+=( CharT )
  // *******************
  template< class CharT, class Traits, class Allocator >
  inline
  basic_string< CharT, Traits, Allocator > &
    basic_string< CharT, Traits, Allocator >::operator+=( CharT c )
  {
    return( append( 1, c ) );
  }

  // append( const basic_string &, size_type, size_type )
  // ****************************************************
  template< class CharT, class Traits, class Allocator >
  basic_string< CharT, Traits, Allocator > &
    basic_string< CharT, Traits, Allocator >::append(
      const basic_string &str,
      size_type pos,
      size_type n )
  {
    if( pos > str.str_length )
      throw out_of_range( "basic_string::append" );

    size_type tail_length = str.str_length - pos;
    size_type append_length = ( tail_length <= n ) ? tail_length : n;

⌨️ 快捷键说明

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