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

📄 vmstring.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
📖 第 1 页 / 共 4 页
字号:
int VMString::FindCount( const char* pchToFind ) const
{
  _ASSERT( pchToFind );
  return FindCount( 0, pchToFind );
}
/* End of function "VMString::FindCount"
/*****************************************************************************/


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

     FUNCTION NAME:  VMString::FindCount

       DESCRIPTION:  counts the number of times that pchToFind occurs in this
                     after the starting position (inclusive)

             INPUT:  iStartAt - index from which to start the search
                     pchToFind - pointer to string to search for in this
            OUTPUT:  

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

  LPTSTR  pchHead = m_pchBuffer + iStartAt;
  LPTSTR  pchNext;
  int     iCount = 0;
  int     iSubSize = strlen( pchToFind );
  
  if ( 0 == iSubSize )
  {
    return( 0 );
  }

  pchNext = strstr( pchHead, pchToFind );

  while ( pchNext )
  {
    iCount++;
    pchHead = pchNext + iSubSize;
    pchNext = strstr( pchHead, pchToFind );
  }

  return( iCount );
}
/* End of function "VMString::FindCount"
/*****************************************************************************/


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

     FUNCTION NAME:  VMString::Find

       DESCRIPTION:  find the first occurance of pchToFind in this and return
                     the index of that occurance

             INPUT:  pchToFind - pointer to the string to search for
            OUTPUT:  none

           RETURNS:  the index if found, -1 if not found
*/
int VMString::Find( const char* pchToFind, int iStartAt ) const
{
  _ASSERT( pchToFind );
  LPTSTR  pchNext;

  if ( iStartAt > m_iLength )
  {
    return( -1 );
  }

  pchNext = strstr( m_pchBuffer + iStartAt, pchToFind );
  if ( pchNext )
  {
    return( pchNext - m_pchBuffer );
  }  
  return( -1 );
}
/* End of function "VMString::Find"
/*****************************************************************************/


int VMString::Find( const char chToFind, int iStartAt ) const
{
  char achToFind[ 2 ];
  achToFind[ 0 ] = chToFind;
  achToFind[ 1 ] = 0;

  if ( iStartAt > m_iLength )
  {
    return( -1 );
  }

  char* pchNext = strstr( m_pchBuffer + iStartAt, achToFind );
  if ( pchNext )
  {
    return( pchNext - m_pchBuffer );
  }  
  return( -1 );
}


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

     FUNCTION NAME:  VMString::Find

       DESCRIPTION:  find the first occurance of chToFind in this and return
                     the index of that occurance

             INPUT:  chToFind - the character to search for
            OUTPUT:  none

           RETURNS:  the index if found, -1 if not found
*/
int VMString::Find( const char chToFind ) const
{
  char* pchNext = strchr( m_pchBuffer, chToFind );
  if ( pchNext )
  {
    return( pchNext - m_pchBuffer );
  }  
  return( -1 );
}
/* End of function "VMString::Find"
/*****************************************************************************/


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

     FUNCTION NAME:  VMString::Find

       DESCRIPTION:  find the first occurance of pchToFind in this and return
                     the index of that occurance

             INPUT:  pchToFind - pointer to the string to search for
            OUTPUT:  none

           RETURNS:  the index if found, -1 if not found
*/
int VMString::Find( const char* pchToFind ) const
{
  _ASSERT( pchToFind );
  LPTSTR  pchNext;
  
  pchNext = strstr( m_pchBuffer, pchToFind );
  if ( pchNext )
  {
    return( pchNext - m_pchBuffer );
  }  
  return( -1 );
}
/* End of function "VMString::Find"
/*****************************************************************************/


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

     FUNCTION NAME:  VMString::FindNthOf

       DESCRIPTION:  locates the 'nth' occurance of pchToFind in this

             INPUT:  iOccurance - the occurance to locate
                     pchToFind - the string to locate
            OUTPUT:  

           RETURNS:  index of the given occurance, -1 if not found
*/
int VMString::FindNthOf( int iOccurance, const char* pchToFind ) const
{
  _ASSERT( iOccurance > 0 && pchToFind );

  LPTSTR pchNext;
  LPTSTR pchHead  = m_pchBuffer;
  int    iSubSize = strlen( pchToFind );

  pchNext = strstr( pchHead, pchToFind );
  if ( pchNext )
  {
    iOccurance--;
  }
  while ( pchNext && iOccurance > 0 )
  {
    pchHead = pchNext + iSubSize;
    pchNext = strstr( pchHead, pchToFind );
    if ( pchNext ) 
    { 
      iOccurance--; 
    };
  }
  if ( 0 == iOccurance )
  {
    return( pchNext - m_pchBuffer );
  }
  return( -1 );
}
/* End of function "VMString::FindNthOf"
/*****************************************************************************/


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

     FUNCTION NAME:  VMString::ReverseFind

       DESCRIPTION:  finds the first occurance of pchToFind in this, from the
                     "right edge" of the buffer

             INPUT:  pchToFind - pointer to the string to find.
            OUTPUT:  none

           RETURNS:  index of the occurance, -1 if not found
*/
int VMString::ReverseFind( const char* pchToFind ) const
{
  _ASSERT( pchToFind );

  LPTSTR  pchHead = m_pchBuffer;
  LPTSTR  pchNext;
  int     iSubSize = strlen( pchToFind );
  int     iIndex = -1;

  pchNext = strstr( pchHead, pchToFind );
  while ( pchNext )
  {
    iIndex  = pchNext - m_pchBuffer;
    pchHead = pchNext + iSubSize;
    pchNext = strstr( pchHead, pchToFind );
  }
  return( iIndex );
}
/* End of function "VMString::ReverseFind"
/*****************************************************************************/


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

     FUNCTION NAME:  VMString::ReverseFindNthOf

       DESCRIPTION:  locates the nth occurance of pchToFind from the "right end"
                     of the buffer

             INPUT:  iOccurance - the occurance to find
                     pchToFind - the string to search for
            OUTPUT:  

           RETURNS:  index of location , -1 if not found
*/
int VMString::ReverseFindNthOf( int iOccurance, const char* pchToFind ) const
{
  _ASSERT( iOccurance > 0 && pchToFind );

  int iCount = FindCount( pchToFind );
  if ( iCount >= iOccurance )
  {
    return( FindNthOf( iCount + 1 - iOccurance,  pchToFind ) );
  }
  return( -1 );
}
/* End of function "VMString::ReverseFindNthOf"
/*****************************************************************************/


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

     FUNCTION NAME:  VMString::Empty

       DESCRIPTION:  empties the buffer in this

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  self
*/
VMString& VMString::Empty( void )
{
  m_pchBuffer[0] = '\0';
  m_iLength      = 0;
  return( *this );
}
/* End of function "VMString::Empty"
/*****************************************************************************/


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

     FUNCTION NAME:  VMString::Fill

       DESCRIPTION:  fills this with the specified character

             INPUT:  chFillChar - the fill character 
            OUTPUT:  none

           RETURNS:  self
*/
VMString& VMString::Fill( const TCHAR chFillChar )
{
  strnset( m_pchBuffer, chFillChar, m_iLength );
  return( *this );
}
/* End of function "VMString::Fill"
/*****************************************************************************/


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

     FUNCTION NAME:  VMString::Trim

       DESCRIPTION:  trim both

             INPUT:  void
            OUTPUT:  none

           RETURNS:  trimmed version of this
*/
VMString& VMString::Trim( void )
{
  return( TrimLeft().TrimRight() );
}
/* End of function "VMString::Trim"
/*****************************************************************************/


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

     FUNCTION NAME:  VMString::TrimLeft

       DESCRIPTION:  trim leading spaces in this

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  self
*/
VMString& VMString::TrimLeft( void )
{
  TCHAR* pchHead = m_pchBuffer;
  
  while ( isspace( *pchHead ) )
  {
    pchHead = pchHead++;
  }
  if ( pchHead != m_pchBuffer )
  {
    int iMoveBy = m_iLength - ( pchHead - m_pchBuffer );
    memmove( m_pchBuffer, pchHead, VMString::TLEN( iMoveBy + 1 ) );
    m_iLength -= m_iLength - iMoveBy;
  }
  return( *this );
}
/* End of function "VMString::TrimLeft"
/*****************************************************************************/


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

     FUNCTION NAME:  VMString::TrimRight

       DESCRIPTION:  trim trailing spaces from this

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  self
*/
VMString& VMString::TrimRight( void )
{
  int    iTailIndex    = ( m_iLength > 0 ) ? m_iLength - 1 : 0;
  TCHAR* pchTail       = m_pchBuffer + iTailIndex;
  int    iTrimSize     = 0;

  while ( NULL != pchTail && isspace( *pchTail ) )
  {
    pchTail--;
    iTrimSize++;
  }
  if ( iTrimSize > 0 )
  {
    m_pchBuffer[ iTailIndex + 1 - iTrimSize ] = '\0';
    m_iLength -= iTrimSize;
  }
  return( *this );
}
/* End of function "VMString::TrimRight"
/*****************************************************************************/


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

     FUNCTION NAME:  VMString::Truncate

       DESCRIPTION:  shorten this to the size indicated

             INPUT:  dwSize - the new size for this
            OUTPUT:  none

           RETURNS:  self
*/
VMString& VMString::Truncate( int dwSize )
{
  if ( dwSize + 1 > m_iLength )
  {
    return( *this );
  }
  m_pchBuffer[ dwSize ] = '\0';
  m_iLength = dwSize;

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


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

     FUNCTION NAME:  VMString::Lower

       DESCRIPTION:  shift all chars to lower case

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  self
*/
VMString& VMString::Lower( void )
{
  strlwr( m_pchBuffer );
  return( *this );
}
/* End of function "VMString::Lower"
/*****************************************************************************/


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

     FUNCTION NAME:  VMString::Upper

       DESCRIPTION:  shift all chars to upper case

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  self
*/
VMString& VMString::Upper( void )
{
  strupr( m_pchBuffer );
  return( *this );
}
/* End of function "VMString::Upper"
/*****************************************************************************/


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

     FUNCTION NAME:  VMString::Insert

       DESCRIPTION:  insert a string of characters at the given index

             INPUT:  iAtIndex - the insert location
                     pchToInsert - pointer to string to insert
            OUTPUT:  

           RETURNS:  self
*/
VMString& VMString::Insert( int iAtIndex, const char* pchToInsert )
{
  _ASSERT( iAtIndex >= 0 && pchToInsert );
  if ( iAtIndex > m_iLength )
  {
    return( *this );
  }

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

  m_pchBuffer[ iNewLength ] = '\0';
  m_iLength = iNewLength;
      
  return( *this );
}
/* End of function "VMString::Insert"
/*****************************************************************************/


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

     FUNCTION NAME:  VMString::Prepend

       DESCRIPTION:  insert a given string at the beginning of this

             INPUT:  pchToInsert - pointer to string to insert
            OUTPUT:  none

           RETURNS:  self
*/
VMString& VMString::Prepend( const char* pchToInsert )
{
  _ASSERT( pchToInsert );

  int iParamLength = strlen( pchToInsert );
  int iNewLength   = m_iLength + iParamLength; 

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

  m_pchBuffer[ iNewLength ] = '\0';
  m_iLength = iNewLength;
  
  return( *this );
}
/* End of function "VMString::Prepend"
/*****************************************************************************/


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

     FUNCTION NAME:  VMString::Append

       DESCRIPTION:  append a string at the end of this

⌨️ 快捷键说明

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