void CString::AssignNewSpace(int iNewTotalSize, int iNeedMove)
{
char *pNewSpace = NULL; //新的字符串指针,初始化NULL
if (iNewTotalSize <= m_nSize) //确保新的内存空间大于原来的内存空间
return ;
// allocate new space
pNewSpace = new char [iNewTotalSize]; //pNewSpace动态申请内存空间
if (pNewSpace == NULL)
return ;
if (iNeedMove)
{
memcpy(pNewSpace, m_pDataStart, m_nLength + 1); //将原有字符串复制给新申请内存
}
SAFEDELETE(m_pBuffer); //安全删除原有的字符串m_pBuffer
m_pBuffer = pNewSpace;
m_pDataStart = m_pBuffer;
m_pDataEnd = &(m_pDataStart[m_nLength]); //重置m_pBuffer,m_pDataStart,m_pDataEnd
// adjust new size
m_nSize = iNewTotalSize;
}
//////////////////////////////////////////////////////////////////////////////
// concatenation
// NOTE: "operator+" is done as friend functions for simplicity
// There are three variants:
// CString + CString
// and for ? = TCHAR, LPCTSTR
// CString + ?
// ? + CString
void CString::ConcatCopy(const char *str1, int nSrc1Len, const char *str2, int nSrc2Len)
{
int nNewLen = nSrc1Len + nSrc2Len;
AssignNewSpace(nNewLen + 1, 0);
// append two string
Append(str1, nSrc1Len);
Append(str2, nSrc2Len);
}
// friend methods
// Class + Class
CString operator+(const CString& string1, const CString& string2)
{
CString s;
s.ConcatCopy(string1.GetData(), string1.GetLength(), string2.GetData(), string2.GetLength());
return s;
}
// Class + char
CString operator+(const CString& string, char ch)
{
CString s;
char str[2];
str[0] = ch;
str[1] = 0;
s.ConcatCopy(string.GetData(), string.GetLength(), str, 1);
return s;
}
// char + Class
CString operator+(char ch, const CString& string)
{
CString s;
char str[2];
str[0] = ch;
str[1] = 0;
s.ConcatCopy(string.GetData(), string.GetLength(), str, 1);
return s;
}
// Class + char *
CString operator+(const CString& string, const char *lpsz)
{
CString s;
s.ConcatCopy(string.GetData(), string.GetLength(), lpsz, strlen(lpsz));
return s;
}
// char * + Class
CString operator+(const char *lpsz, const CString& string)
{
CString s;
s.ConcatCopy(string.GetData(), string.GetLength(), lpsz, strlen(lpsz));
return s;
}
// Compare operators
bool operator==(const CString& s1, const CString& s2)
{
return (s1.Compare(s2.GetData()) == 0);
}
bool operator==(const CString& s1, const char* s2)
{
return (s1.Compare(s2) == 0);
}
bool operator==(const char* s1, const CString& s2)
{
return (s2.Compare(s1) == 0);
}
bool operator!=(const CString& s1, const CString& s2)
{
return (s1.Compare(s2.GetData()) != 0);
}
bool operator!=(const CString& s1, const char* s2)
{
return (s1.Compare(s2) != 0);
}
bool operator!=(const char* s1, const CString& s2)
{
return (s2.Compare(s1) != 0);
}
bool operator>(const CString& s1, const CString& s2)
{
return (s1.Compare(s2.GetData()) > 0);
}
bool operator>(const CString& s1, const char* s2)
{
return (s1.Compare(s2) > 0);
}
bool operator>(const char* s1, const CString& s2)
{
return (s2.Compare(s1) < 0);
}
bool operator<(const CString& s1, const CString& s2)
{
return (s1.Compare(s2.GetData()) < 0);
}
bool operator<(const CString& s1, const char* s2)
{
return (s1.Compare(s2) < 0);
}
bool operator<(const char* s1, const CString& s2)
{
return (s2.Compare(s1) > 0);
}
bool operator>=(const CString& s1, const CString& s2)
{
return (s1.Compare(s2.GetData()) >= 0);
}
bool operator>=(const CString& s1, const char* s2)
{
return (s1.Compare(s2) >= 0);
}
bool operator>=(const char* s1, const CString& s2)
{
return (s2.Compare(s1) <= 0);
}
bool operator<=(const CString& s1, const CString& s2)
{
return (s1.Compare(s2.GetData()) <= 0);
}
bool operator<=(const CString& s1, const char* s2)
{
return (s1.Compare(s2) <= 0);
}
bool operator<=(const char* s1, const CString& s2)
{
return (s2.Compare(s1) >= 0);
}
标准C++实现字符串类CString(5)
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:http://www.heiqu.com/987761179eb0962ebeff51d44e8e322f.html