📄 cmstring.cpp
字号:
#pragma warning( disable : 4290 )
#pragma warning( disable : 4291 )
#include <windows.h>
#include <TChar.h>
#include ".\MatrixCore\System\CMString.h"
using namespace MatrixCore::System;
CMString::CMString()
: chData( 0 ), stringLength( 0 ), allocSize( 0 )
{
allocData( StringBlockSize );
}
CMString::CMString( const CMString& source )
: chData( 0 ), stringLength( 0 ), allocSize( 0 )
{
allocData( source.stringLength + 1 );
_tcscpy( chData, source.chData );
stringLength = source.stringLength;
}
CMString::CMString( LPTSTR str )
: chData( 0 ), stringLength( 0 ), allocSize( 0 )
{
*this = str;
}
CMString::CMString( LPCTSTR str )
: chData( 0 ), stringLength( 0 ), allocSize( 0 )
{
*this = str;
}
CMString::CMString( int n, bool hex )
: chData( 0 ), stringLength( 0 ), allocSize( 0 )
{
//NULL 捞搁 救笼绢 持绢霖促..
allocData( StringBlockSize );
if( n == NULL)
return;
if( hex == true )
format( _T( "%x" ), n );
else
format( _T( "%d" ), n );
}
CMString::~CMString()
{
if( chData != 0 )
delete [] chData;
}
void CMString::allocData( int strLen )
{
if( strLen > allocSize )
{
while( strLen > allocSize )
allocSize += StringBlockSize;
if( chData != 0 )
delete [] chData;
chData = new TCHAR [allocSize];
::ZeroMemory( chData, allocSize );
}
}
void CMString::calcStringSize()
{
int i;
stringLength = 0;
for( i = 0 ; i < allocSize ; i++, stringLength++ )
if( chData[i] == 0 )
break;
}
void CMString::format( LPCTSTR strFormat, ... )
{
TCHAR buffer[1024]; // The maximum size of the buffer is 1024 bytes in wsprintf func.
va_list args;
va_start( args, strFormat );
wvsprintf( buffer, strFormat, args );
va_end( args );
*this = buffer;
}
LPTSTR CMString::getBuffer()
{
if(chData == NULL)
return NULL;
return chData;
}
bool CMString::isEmpty()
{
if( chData == 0 || _tcslen( chData ) == 0 )
return true;
return false;
}
void CMString::empty()
{
if( chData == 0 )
return;
format( _T( "" ) );
}
TCHAR CMString::getAt( int nIndex )
{
if( nIndex >= stringLength )
return 0;
return chData[nIndex];
}
void CMString::setAt( int nIndex, TCHAR ch )
{
if( nIndex >= stringLength )
return;
chData[nIndex] = ch;
}
void CMString::left( int nCount )
{
chData[nCount] = 0;
calcStringSize();
}
void CMString::right( int nCount )
{
LPTSTR temp = new TCHAR[stringLength + 1];
int nOffset = stringLength - nCount;
memcpy( temp, ( chData + nOffset ), sizeof( TCHAR ) * ( nCount + 1 ) );
memcpy( chData, temp, sizeof( TCHAR ) * ( nCount + 1 ) );
delete [] ( LPTSTR )temp;
calcStringSize();
}
void CMString::makeUpper()
{
_tcsupr( chData );
}
void CMString::makeLower()
{
_tcslwr( chData );
}
int CMString::compareTo( CMString& str )
{
return _tcscmp( getBuffer(), str.getBuffer() );
}
int CMString::compareTo( LPCTSTR str )
{
if(str == NULL)
{
if(getBuffer() == NULL)
return 0;
else
return 1;
}
return _tcscmp( getBuffer(), str );
}
int CMString::compareTo( CMString& str, int count )
{
return _tcsncmp( getBuffer(), str.getBuffer(), count );
}
int CMString::compareTo( LPCTSTR str, int count )
{
return _tcsncmp( getBuffer(), str, count );
}
LPCWSTR CMString::toWideChar( LPCSTR str )
{
static WCHAR wideCharBuff[1024];
int len = strlen( str );
::MultiByteToWideChar( CP_ACP, 0, str, -1, wideCharBuff, len + 1 );
return wideCharBuff;
}
LPCSTR CMString::toMultiByte( LPCWSTR str )
{
static CHAR multiByteBuff[1024];
int len = wcslen( str );
::WideCharToMultiByte( CP_ACP, 0, str, -1, multiByteBuff, len + 1, 0, 0 );
return multiByteBuff;
}
const CMString& CMString::operator = ( CMString& str )
{
*this = str.getBuffer();
return *this;
}
const CMString& CMString::operator = ( LPTSTR str )
{
stringLength = _tcslen( str );
allocData( stringLength + 1 );
_tcscpy( chData, str );
calcStringSize();
return *this;
}
const CMString& CMString::operator = ( LPCTSTR str )
{
stringLength = _tcslen( str );
allocData( stringLength + 1 );
_tcscpy( chData, str );
calcStringSize();
return *this;
}
const CMString& CMString::operator += ( CMString& str )
{
LPTSTR temp = new TCHAR[allocSize];
// Save my string value.
_tcscpy( temp, chData );
// Resizing memory.
stringLength += str.stringLength;
allocData( stringLength + 1 );
// Recover my string.
_tcscpy( chData, temp );
// Add new string to mine.
_tcscat( chData, str.chData );
delete [] ( LPTSTR )temp;
calcStringSize();
return *this;
}
const CMString& CMString::operator += ( LPTSTR str )
{
CMString temp( str );
return ( *this += temp );
}
TCHAR CMString::operator [] ( int nIndex ) const
{
if( nIndex >= stringLength )
return 0;
return chData[nIndex];
}
CMString::operator LPTSTR() const
{
return chData;
}
bool MatrixCore::System::operator == ( CMString& str1, CMString& str2 )
{
return ( str1.compareTo( str2 ) == 0 );
}
bool MatrixCore::System::operator == ( CMString& str1, LPTSTR str2 )
{
return ( str1.compareTo( str2 ) == 0 );
}
bool MatrixCore::System::operator == ( LPTSTR str1, CMString& str2 )
{
return ( str2.compareTo( str1 ) == 0 );
}
bool MatrixCore::System::operator != ( CMString& str1, CMString& str2 )
{
return ( str1.compareTo( str2 ) != 0 );
}
bool MatrixCore::System::operator != ( CMString& str1, LPTSTR str2 )
{
return ( str1.compareTo( str2 ) != 0 );
}
bool MatrixCore::System::operator != ( LPTSTR str1, CMString& str2 )
{
return ( str2.compareTo( str1 ) != 0 );
}
CMString MatrixCore::System::operator + ( CMString& ls, CMString& rs )
{
CMString ret;
ret = ls;
ret += rs;
return ret;
}
CMString MatrixCore::System::operator + ( CMString& ls, LPTSTR rs )
{
CMString ret;
ret = ls;
ret += rs;
return ret;
}
CMString MatrixCore::System::operator + ( LPTSTR ls, CMString& rs )
{
CMString ret;
ret = ls;
ret += rs;
return ret;
}
CMString MatrixCore::System::operator + ( CMString& ls, int n )
{
CMString ret, temp;
temp.format( _T( "%d" ), n );
ret = ls + temp;
return ret;
}
CMString MatrixCore::System::operator + ( int n, CMString& rs )
{
CMString ret, temp;
temp.format( _T( "%d" ), n );
ret = temp + rs;
return ret;
}
CMString MatrixCore::System::operator + ( CMString& ls, DWORD n )
{
CMString ret, temp;
temp.format( _T( "%d" ), n );
ret = ls + temp;
return ret;
}
CMString MatrixCore::System::operator + ( DWORD n, CMString& rs )
{
CMString ret, temp;
temp.format( _T( "%d" ), n );
ret = temp + rs;
return ret;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -