// StringM.cpp: implementation of the CStringM class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "ScriptTest.h"
#include "StringM.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CStringM::CStringM()
{
}
CStringM::CStringM(CString& str):CString(str)
{
}
CStringM::~CStringM()
{
}
int CStringM::GetLengthM()
{
int nLength = this->GetLength();
int nResult = 0;
for(int i=0; i<nLength; i++)
{
// 是中文字符
if(this->GetAt(i) & 0x80)
{
i += 1;
nResult ++;
}
else
{
nResult ++;
}
}
return nResult;
}
CStringM CStringM::MidM(int nFirst)
{
CStringM strResult;
int nStart = 0;
int nLength = this->GetLength();
for(int i=0; i<nLength; i++)
{
if(nStart == nFirst)
{
nStart = i;
break;
}
if(this->GetAt(i) & 0x80)
{
i += 1;
}
nStart ++;
}
strResult = this->Mid(nStart);
return strResult;
}
CStringM CStringM::MidM(int nFirst, int nCount)
{
CStringM strResult;
int nStart = 0;
int nEnd;
int nLength = this->GetLength();
for(int i=0; i<nLength; i++)
{
if(nStart == nFirst)
{
nStart = i;
break;
}
if(this->GetAt(i) & 0x80)
{
i += 1;
}
nStart ++;
}
nEnd = 1;
for(int i = nStart; i<nLength; i++)
{
if(nEnd == nCount)
{
if(this->GetAt(i) & 0x80)
i += 1;
nEnd = i;
break;
}
if(this->GetAt(i) & 0x80)
{
i += 1;
}
nEnd ++;
}
strResult = this->Mid(nStart, nEnd-nStart+1);
return strResult;
}
CStringM CStringM::LeftM(int nCount)
{
CStringM strResult;
strResult = this->MidM(0, nCount);
return strResult;
}
CStringM CStringM::RightM(int nCount)
{
CStringM strResult;
int nAllCount = this->GetLengthM();
strResult = this->MidM(nAllCount - nCount, nCount);
return strResult;
}
int CStringM::FindM(TCHAR ch) const
{
int nFindIndex = -1;
int nIndexM = 0;
int nLength = this->GetLength();
for(int i=0; i<nLength; i++)
{
TCHAR chTemp = this->GetAt(i);
if(chTemp == ch)
{
nFindIndex = nIndexM;
break;
}
if(this->GetAt(i) & 0x80)
{
i += 1;
}
nIndexM++;
}
return nFindIndex;
}
int CStringM::ReverseFindM(TCHAR ch) const
{
int nFindIndex = -1;
int nLength = this->GetLength();
int nLengthM = ((CStringM*) this)->GetLengthM();
int nIndexM = nLengthM-1;
bool bCharHead = false;
for(int i=nLength-1; i>=0; i--)
{
TCHAR chTemp = this->GetAt(i);
if(chTemp == ch)
{
nFindIndex = nIndexM;
break;
}
if(this->GetAt(i) & 0x80)
{
if(!bCharHead)
{
i -= 1;
bCharHead = true;
}
else
{
bCharHead = false;
continue;
}
}
nIndexM--;
}
return nFindIndex;
}