⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmstring.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
📖 第 1 页 / 共 4 页
字号:
             INPUT:  pchToInsert - pointer to string to add to the end
            OUTPUT:  none

           RETURNS:  self 
*/
VMString& VMString::Append( const char* pchToInsert )
{
  _ASSERT( pchToInsert );
  int iParamLength = strlen( pchToInsert );
  int iNewLength   = m_iLength + iParamLength; 

  VMString oTemp( *this );
  UpdateBufferSize( iNewLength + 1 );
  memcpy( m_pchBuffer, 
          oTemp.Buffer(), 
          VMString::TLEN( oTemp.Length() ));
  memcpy( m_pchBuffer + iNewLength - iParamLength, 
          pchToInsert, VMString::TLEN( iParamLength ));

  m_pchBuffer[ iNewLength ] = '\0';
  m_iLength = iNewLength;

  return( *this );
}
/* End of function "VMString::Append"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMString::Remove

       DESCRIPTION:  slices out a section of this

             INPUT:  iStartAt - start slicing here
                     iLength - size to slice out
            OUTPUT:  

           RETURNS:  self 
*/
VMString& VMString::Remove( int iStartAt, int iLength )
{
  _ASSERT( iStartAt >= 0 && iLength >= 0 );

  if ( iStartAt > m_iLength || 0 == iLength )
  {
    return( *this );
  }
  
  int iRemoveLength = ( iStartAt + iLength <= m_iLength ) ? iLength : ( m_iLength - iStartAt );
  int iNewLength    = m_iLength - iRemoveLength;
  
  VMString oTemp( *this );
  UpdateBufferSize( iNewLength + 1 );

  memcpy( m_pchBuffer + iStartAt, 
        oTemp.Buffer() + iStartAt + iRemoveLength, 
        VMString::TLEN( iNewLength - iStartAt ));

  m_pchBuffer[ iNewLength ] = '\0';
  m_iLength = iNewLength;

  return( *this );
}
/* End of function "VMString::Remove"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMString::Replace

       DESCRIPTION:  replace each occurance of pchToFind with pchReplaceWith

             INPUT:  iStartAt - the index to start the replace from
                     pchToFind - replace this text
                     pchReplaceWith - with this text
            OUTPUT:  

           RETURNS:  self
*/
VMString& VMString::Replace( int iStartAt, const char* pchToFind, const char* pchReplaceWith )
{
  _ASSERT( iStartAt >= 0 && pchToFind && pchReplaceWith );

  if ( iStartAt >= m_iLength )
  {
    return( *this );
  }
  
  int iToReplaceLength   = strlen( pchToFind );
  int iReplaceWithLength = strlen( pchReplaceWith );
  int iLengthDifference  = iReplaceWithLength - iToReplaceLength;
  int iReplaceCount      = FindCount( iStartAt, pchToFind );
  int iNewBufferLength   = m_iLength + iLengthDifference * iReplaceCount;
  
  VMString oTemp( m_pchBuffer );
  UpdateBufferSize( iNewBufferLength + 1 );
  
  TCHAR* pchHead = oTemp.Buffer() + iStartAt;
  TCHAR* pchNext = strstr( pchHead, pchToFind );
  int    iCurrentIndex = 0;  
  
  if ( iStartAt > 0 )
  {
    memcpy( m_pchBuffer + iCurrentIndex, 
            oTemp.Buffer(), 
            VMString::TLEN( iStartAt ) );

    iCurrentIndex += iStartAt;
  }

  while ( pchNext )
  {
    memcpy( m_pchBuffer + iCurrentIndex, 
            pchHead, 
            VMString::TLEN( pchNext - pchHead ) );
    iCurrentIndex += pchNext - pchHead;

    memcpy( m_pchBuffer + iCurrentIndex, 
            pchReplaceWith, 
            VMString::TLEN( iReplaceWithLength ) );
    iCurrentIndex += iReplaceWithLength;
    
    pchHead = pchNext + iToReplaceLength;
    pchNext = strstr( pchHead, pchToFind );
  }
  
  iLengthDifference = oTemp.Length() - ( pchHead - oTemp.Buffer() );
  if (  iLengthDifference > 0 )
  {
    memcpy( m_pchBuffer + iCurrentIndex, 
            pchHead, 
            VMString::TLEN( iLengthDifference ) );
  }
  
  m_pchBuffer[ iNewBufferLength ] = '\0';
  m_iLength = iNewBufferLength;

  return( *this );
}
/* End of function "VMString::Replace"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMString::Replace

       DESCRIPTION:  replace each occurance of pchToFind with pchReplaceWith

             INPUT:  pchToFind - replace this text
                     pchReplaceWith - with this text
            OUTPUT:  

           RETURNS:  self
*/
VMString& VMString::Replace( const char* pchToFind, const char* pchReplaceWith )
{
  _ASSERT( pchToFind && pchReplaceWith );

  return( Replace( 0, pchToFind, pchReplaceWith ) );
}
/* End of function "VMString::Replace"
/*****************************************************************************/



VMString& VMString::Replace( const char  chToReplace, const char  chNew )
{
  char  achReplace[ 2 ];
  char  achWith[ 2 ];

  achReplace[ 0 ] = chToReplace;
  achReplace[ 1 ] = 0;

  achWith[ 0 ] = chNew;
  achWith[ 1 ] = 0;

  return( Replace( 0, achReplace, achWith ) );
}


/*****************************************************************************/
/*

     FUNCTION NAME:  VMString::Format

       DESCRIPTION:  formatted assignment function

             INPUT:  pchFormat - pointer to the format string
                     ... - arguments
            OUTPUT:  

           RETURNS:  self
*/
VMString& VMString::Format( const char* pchFormat, ... )
{
  _ASSERT( pchFormat );

  va_list args;
  va_start( args, pchFormat );
  FormatV( pchFormat, args );
  va_end( args );
  return( *this );
}
/* End of function "VMString::Format"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMString::FormatV

       DESCRIPTION:  formatted assignment function

             INPUT:  pchFormat - pointer to format string
                     pvArgs - arguments
            OUTPUT:  

           RETURNS:  self
*/
VMString& VMString::FormatV( const char* pchFormat, va_list pvArgs )
{
  _ASSERT( pchFormat );

  int  iNewSize      = m_iLength + 1;
  int  iWrittenCount = -1;
  
  iNewSize += strlen( pchFormat) + FORMAT_EXTRA_SIZE;
  while ( iWrittenCount < 0 )
  {
    UpdateBufferSize( iNewSize + 1 );
    iWrittenCount = _vsntprintf( m_pchBuffer, iNewSize, pchFormat, pvArgs );
    iNewSize <<= 1;
  }
  m_pchBuffer[ iWrittenCount ] = '\0';
  m_iLength = iWrittenCount;
  
  return( *this );
}
/* End of function "VMString::FormatV"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMString::SetInt

       DESCRIPTION:  integer assignment

             INPUT:  iValue - the value to assign to this
            OUTPUT:  none

           RETURNS:  self
*/
VMString& VMString::SetInt( const int iValue )
{
  UpdateBufferSize( INT_SIZE ); 

  _itot( iValue, m_pchBuffer, 10 );
  m_iLength = strlen( m_pchBuffer );
  
  return( *this );
}
/* End of function "VMString::SetInt"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMString::SetLong

       DESCRIPTION:  long assignment

             INPUT:  lValue - the value to assign to this
            OUTPUT:  none

           RETURNS:  self
*/
VMString& VMString::SetLong( const long lValue )
{
  UpdateBufferSize( LONG_SIZE ); 

  _ltot( lValue, m_pchBuffer, 10 );
  m_iLength = strlen( m_pchBuffer );
  
  return( *this );
}
/* End of function "VMString::SetLong"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMString::SetULong

       DESCRIPTION:  unsigned long assignment

             INPUT:  ulValue - the value to assign to this
            OUTPUT:  none

           RETURNS:  self
*/
VMString& VMString::SetULong( const unsigned long ulValue )
{
  UpdateBufferSize( ULONG_SIZE ); 

  _ultot( ulValue, m_pchBuffer, 10 );
  m_iLength = strlen( m_pchBuffer );
  
  return( *this );
}
/* End of function "VMString::SetULong"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMString::SetDouble

       DESCRIPTION:  double assignment

             INPUT:  dblValue - the value to assign to this
            OUTPUT:  none

           RETURNS:  self
*/
VMString& VMString::SetDouble( const double dblValue )
{
  return( Format( "%g", dblValue ) );
}
/* End of function "VMString::SetDouble"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMString::GetInt

       DESCRIPTION:  return the value of this as an integer

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  value of this
*/
int VMString::GetInt( void ) const
{
  return( _ttoi( m_pchBuffer ) );
}
VMString::operator int ( void ) const
{
  return( _ttoi( m_pchBuffer ) );
}
/* End of function "VMString::GetInt"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMString::GetLong

       DESCRIPTION:  return the value of this as a long

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  value of this
*/
long VMString::GetLong( void ) const
{
  return( _ttol( m_pchBuffer ) );
}
VMString::operator long () const
{
  return( _ttol( m_pchBuffer ) );
}
/* End of function "VMString::GetLong"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMString::GetULong

       DESCRIPTION:  return the value of this as an unsigned long

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  value of this
*/
unsigned long VMString::GetULong( void ) const
{
  return( strtoul( m_pchBuffer, NULL, 10 ) );
}
VMString::operator unsigned long () const
{
  return( strtoul( m_pchBuffer, NULL, 10 ) );
}
/* End of function "VMString::GetULong"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMString::GetDouble

       DESCRIPTION:  return the value of this as a double

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  value of this
*/
double VMString::GetDouble( void ) const
{
  return( strtod( m_pchBuffer, NULL ) );
}
VMString::operator double () const
{
  return( strtod( m_pchBuffer, NULL ) );
}
/* End of function "VMString::GetDouble"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMString::operator += 

       DESCRIPTION:  append a character to this

             INPUT:  chToAppend - character to append to this
            OUTPUT:  none

           RETURNS:  void
*/
void VMString::operator += ( const char chToAppend )
{
  char achTwo[ 2 ];
  achTwo[ 0 ] = chToAppend;
  achTwo[ 1 ] = 0;
  Append( achTwo );
}
/* End of function "VMString::operator +="
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMString::operator += 

       DESCRIPTION:  append another string on to this

             INPUT:  roOther - character to append to this
            OUTPUT:  none

           RETURNS:  void
*/
void VMString::operator += ( const VMString& roOther )
{
  Append( roOther.Buffer() );
}
/* End of function "VMString::operator +="
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMString::operator += 

       DESCRIPTION:  append another string on to this

             INPUT:  pchToAppend - string to append to this
            OUTPUT:  none

           RETURNS:  void
*/
void VMString::operator += ( const char* pchToAppend )
{
  Append( pchToAppend );
}
/* End of function "VMString::operator +="
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  operator << 

       DESCRIPTION:  various concatenation operators

             INPUT:  roTarget - target of the operation
                     varies   - to concat on to the target
                     
            OUTPUT:  

           RETURNS:  self
*/
VMString& operator << ( VMString& roTarget, const char* pchToAppend )
{
  _ASSERT( pchToAppend );
  return( roTarget.Append( pchToAppend ) );
}

VMString& operator << ( VMString& roTarget, int iValue )
{
  return( roTarget.Append( VMString( VMString::INT_SIZE ).SetInt( iValue ) ) );
}

VMString& operator << ( VMString& roTarget, long lValue )
{
  return( roTarget.Append( VMString( VMString::LONG_SIZE ).SetLong( lValue ) ) );
}

VMString& operator << ( VMString& roTarget, unsigned long ulValue )
{
  return( roTarget.Append( VMString( VMString::ULONG_SIZE ).SetULong( ulValue ) ) );
}

VMString& operator << ( VMString& roTarget, double dblValue )
{
  return( roTarget.Append( VMString( VMString::FORMAT_EXTRA_SIZE + 4 ).SetDouble( dblValue ) ) );
}
/* End of function "VMString::operator <<"
/*****************************************************************************/



/*****************************************************************************/
/* Check-in history */
/*
 *$Log:  $
*/
/*****************************************************************************/


⌨️ 快捷键说明

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