#include <stdio.h>
#include <new> // for std::bad_alloc
#include <crtdbg.h> // for _ASSERT
#include "str.h"
std::bad_alloc cstrBadAlloc;
/////////////////////// CStr implementation ////////////////////
// Constants
// CStr allocation extra size; if no extra allocation is wanted
// then set CSTRA_EXTRA_SIZE to 0
int CStr::CSTR_EXTRA_SIZE = 16;
// do not modify the following constants
int CStr::FORMAT_EXTRA_SIZE = 32;
int CStr::INT_SIZE = 16;
int CStr::LONG_SIZE = 16;
int CStr::ULONG_SIZE = 20;
///////////////////// private/internal used methods //////////////////////////
void CStr::Alloc_( int dwSize )
{
_ASSERT( dwSize > 0 );
m_size = dwSize;
m_pBuf = (TCHAR *) malloc( CStr::TLEN( m_size ));
_ASSERT( m_pBuf );
if ( !m_pBuf )
{
throw cstrBadAlloc;
}
}
inline void CStr::Free_()
{
_ASSERT( m_pBuf );
if ( m_pBuf )
{
free( m_pBuf );
m_pBuf = NULL;
m_size = 0;
m_len = 0;
}
}
void CStr::Init_( int dwAllocSize )
{
_ASSERT( dwAllocSize > 0 );
m_pBuf = NULL;
m_size = 0;
m_len = 0;
Alloc_( PaddedSize_( dwAllocSize ));
m_pBuf[0] = '\0';
}
inline int CStr::PaddedSize_( int dwSize )
{
_ASSERT( CSTR_EXTRA_SIZE >= 0 );
return dwSize + CSTR_EXTRA_SIZE;
}
void CStr::UpdateBufferSize_( const int dwSize )
{
_ASSERT( dwSize >= 0 );
if ( m_size < dwSize )
{
Free_();
Alloc_( PaddedSize_( dwSize ));
}
}
void CStr::StrCopy_( LPCTSTR source, int dwSize )
{
_ASSERT( source && dwSize >= 0 );
memcpy( m_pBuf, source, CStr::TLEN( dwSize ));
m_pBuf[ dwSize ] = '\0';
m_len = dwSize;
}
inline int CStr::TLEN( int len )
{
return len * sizeof( TCHAR );
}
///////////////////// public methods /////////////////////////////
CStr::CStr()
{
Init_();
}
CStr::~CStr()
{
Free_();
}
CStr::CStr( const CStr &str )
{
Init_( str.Length() + 1 );
*this = str;
}
CStr::CStr( LPCTSTR pszStr )
{
_ASSERT( pszStr );
Init_( _tcslen( pszStr ) + 1 );
*this = pszStr;
}
CStr::CStr( int dwAllocSize )
{
Init_( dwAllocSize > 0 ? dwAllocSize : 1 );
}
CStr& CStr::operator = ( const CStr &str )
{
if ( this == &str )
{
return *this;
}
UpdateBufferSize_( str.Length() + 1 );
StrCopy_( str.Buffer(), str.Length() );
return *this;
}
CStr& CStr::operator = ( LPCTSTR pszStr )
{
_ASSERT( pszStr );
int dwLen = _tcslen( pszStr);
UpdateBufferSize_( dwLen + 1 );
StrCopy_( pszStr, dwLen );
return *this;
}
CStr& CStr::operator = ( int iValue )
{
return SetInt( iValue );
}
CStr& CStr::operator = ( long lValue )
{
return SetLong( lValue );
}
CStr& CStr::operator = ( unsigned long ulValue )
{
return SetULong( ulValue );
}
CStr& CStr::operator = ( double dValue )
{
return SetDouble( dValue );
}
//////// internal buffer access //////////
CStr::operator LPCTSTR () const
{
return m_pBuf;
}
TCHAR* CStr::Buffer() const
{
return m_pBuf;
}
TCHAR* CStr::Buffer()
{
return m_pBuf;
}
int CStr::BufferSize() const
{
return m_size;
}
void CStr::Realloc( int dwSize )
{
_ASSERT( dwSize >= 0 );
Free_();
Alloc_( dwSize );
}
void CStr::Compact()
{
if ( m_len + 1 < (int) m_size )
{
CStr tempStr = *this;
Free_();
Alloc_( tempStr.Length() + 1 );
StrCopy_( tempStr.Buffer(), tempStr.Length() );
}
}
/////////// attributes /////////////
int CStr::Length() const
{
return m_len;
}
BOOL CStr::IsEmpty() const
{
return 0 == m_len ? TRUE : FALSE;
}
/////////// comparision ////////////
BOOL CStr::operator == ( LPCTSTR pszStr ) const
{
return ( 0 == Compare( pszStr ));
}
BOOL CStr::operator != ( const LPCTSTR& pszStr ) const
{
return ( 0 != Compare( pszStr ));
}
int CStr::Compare( LPCTSTR pszStr ) const
{
_ASSERT( pszStr );
return _tcscmp( m_pBuf, pszStr );
}
int CStr::CompareNoCase( LPCTSTR pszStr ) const
{
_ASSERT( pszStr );
return _tcsicmp( m_pBuf, pszStr );
}
//////////// accessors ////////////
TCHAR& CStr::operator [] ( int dwPos )
{
return m_pBuf[ dwPos ];
}
const TCHAR& CStr::operator [] ( int dwPos ) const
{
return m_pBuf[ dwPos ];
}
CStr CStr::Left( int dwSize ) const
{
_ASSERT( dwSize >= 0 );
int copySize;
if ( dwSize > m_len )
{
copySize = m_len;
}
else
{
copySize = dwSize;
}
CStr retStr( copySize + 1 );
retStr.StrCopy_( m_pBuf, copySize );
return retStr;
}
CStr CStr::Right( int dwSize ) const
{
_ASSERT( dwSize >= 0 );
int copySize;
if ( dwSize > m_len )
{
copySize = m_len;
}
else
{
copySize = dwSize;
}
CStr retStr( copySize + 1 );
retStr.StrCopy_( m_pBuf + m_len - copySize, copySize );
return retStr;
}
CStr CStr::Mid( int dwStart, int dwSize ) const
{
_ASSERT( dwStart >= 0 && dwSize >= 0 );
CStr retStr;
if ( dwStart + 1 > m_len )
{
return retStr;
}
int copySize = m_len - dwStart;
if ( copySize > dwSize )
{
copySize = dwSize;
}
retStr.UpdateBufferSize_( copySize + 1 );
retStr.StrCopy_( m_pBuf + dwStart, copySize );
return retStr;
}
int CStr::FindCount( LPCTSTR pszStr ) const
{
_ASSERT( pszStr );
return FindCount( 0, pszStr );
}
int CStr::FindCount( int startPos, LPCTSTR pszStr ) const
{
_ASSERT( startPos >= 0 && pszStr );
LPTSTR pszHead = m_pBuf + startPos;
LPTSTR pszNext;
int count = 0;
int subSize = _tcslen( pszStr );
if ( 0 == subSize )
{
return 0;
}
pszNext = _tcsstr( pszHead, pszStr );
while ( pszNext )
{
count++;
pszHead = pszNext + subSize;
pszNext = _tcsstr( pszHead, pszStr );
}
return count;
}
int CStr::Find( LPCTSTR pszStr ) const
{
_ASSERT( pszStr );
LPTSTR pszNext;
pszNext = _tcsstr( m_pBuf, pszStr );
if ( pszNext )
{
return ( pszNext - m_pBuf );
}
return -1;
}
int CStr::FindNth( int nth, LPCTSTR pszStr ) const
{
_ASSERT( nth > 0 && pszStr );
LPTSTR pszNext;
LPTSTR pszHead = m_pBuf;
int subSize = _tcslen( pszStr );
pszNext = _tcsstr( pszHead, pszStr );
if ( pszNext )
{
nth--;
}
while ( pszNext && nth > 0 )
{
pszHead = pszNext + subSize;
pszNext = _tcsstr( pszHead, pszStr );
if ( pszNext )
{
nth--;
};
}
if ( 0 == nth )
{
return pszNext - m_pBuf;
}
return -1;
}
int CStr::ReverseFind( LPCTSTR pszStr ) const
{
_ASSERT( pszStr );
LPTSTR pszHead = m_pBuf;
LPTSTR pszNext;
int subSize = _tcslen( pszStr );
int pos = -1;
pszNext = _tcsstr( pszHead, pszStr );
while ( pszNext )
{
pos = pszNext - m_pBuf;
pszHead = pszNext + subSize;
pszNext = _tcsstr( pszHead, pszStr );
}
return pos;
}
int CStr::ReverseFindNth( int nth, LPCTSTR pszStr ) const
{
_ASSERT( nth > 0 && pszStr );
int count = FindCount( pszStr );
if ( count >= nth )
{
return FindNth( count + 1 - nth, pszStr );
}
return -1;
}
// operations
CStr& CStr::Empty()
{
m_pBuf[0] = '\0';
m_len = 0;
return *this;
}
CStr& CStr::Fill( const TCHAR chr )
{
_tcsnset( m_pBuf, chr, m_len );
return *this;
}
CStr& CStr::Trim()
{
TrimLeft();
return TrimRight();
}
CStr& CStr::TrimLeft()
{
TCHAR *pszHead = m_pBuf;
while ( _istspace( *pszHead ))
{
pszHead = _tcsinc( pszHead );
}
if ( pszHead != m_pBuf )
{
int moveSize = m_len - ( pszHead - m_pBuf );
memmove( m_pBuf, pszHead, CStr::TLEN( moveSize + 1 ));
m_len -= m_len - moveSize;
}
return *this;
}
CStr& CStr::TrimRight()
{
int tailPos = ( m_len > 0 ) ? m_len - 1 : 0;
TCHAR *pszTail = m_pBuf + tailPos;
int trimSize = 0;
while ( NULL != pszTail && _istspace( *pszTail ) )
{
pszTail = _tcsdec( m_pBuf, pszTail );
trimSize++;
}
if ( trimSize > 0 )
{
m_pBuf[ tailPos + 1 - trimSize] = '\0';
m_len -= trimSize;
}
return *this;;
}
CStr& CStr::Trunc( int dwPos )
{
if ( dwPos + 1 > m_len )
{
return *this;
}
m_pBuf[ dwPos ] = '\0';
m_len = dwPos;
JonSco
- 粉丝: 91
- 资源: 1万+
最新资源
- 非常好的变压器与电感器设计教程100%好用.zip
- Redis 未授权检测、密码爆破、Webshell 写入、SSH 公私钥写入、定时计划重启 Shell .zip
- c#语言的概要介绍与分析
- Python基于RNN的问答系统源代码+权重文件+使用说明
- Unity3d动物狼素材资源
- Redis 支持的一组基本 Python 集合.zip
- 华为FusionCompute云平台HCIA-Cloud FC 6.3安装实验指南
- Redis 容器镜像基于 Red Hat 软件集合,适用于 OpenShift 和一般用途 用户可以在基于 Red Hat Enterprise Linux、Fedora 和 CentOS 的.zip
- Unity语音识别工具
- Redis 地理空间索引的辅助库.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈