设为首页 加入收藏

TOP

标准C++实现字符串类CString(六)
2014-11-24 03:17:21 来源: 作者: 【 】 浏览:4
Tags:标准 实现 字符串 CString
TCHAR
const CString& CString::operator+=(const char *lpsz)
{
int len = strlen(lpsz);

if (m_nSize < m_nLength + len + 1)
{
AssignNewSpace(m_nLength + len + 1, 1); // allocate new space and move orignal data
}
Defrag();
memcpy(m_pDataEnd, lpsz, len);
m_pDataEnd += len;
*m_pDataEnd = 0;

return *this;
}

// concatenate a single character
const CString& CString::operator+=(char ch)
{
if (m_nSize < m_nLength + 1 + 1)
{
AssignNewSpace(m_nLength + 1 + 1, 1); // allocate new space and move orignal data
}
Defrag();
memcpy(m_pDataEnd, &ch, 1);
m_pDataEnd += 1;
*m_pDataEnd = 0;

return *this;
}

// concatenate from another CString
const CString& CString::operator+=(CString& string)
{
if (m_nSize < m_nLength + string.GetLength() + 1)
{
AssignNewSpace(m_nLength + string.GetLength() + 1, 1); // allocate new space and move orignal data
}
Defrag();
memcpy(m_pDataEnd, string.GetData(), string.GetLength());
m_pDataEnd += string.GetLength();
*m_pDataEnd = 0;

return *this;
}


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

首页 上一页 3 4 5 6 下一页 尾页 6/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++面试题-链表栈二叉树数据结构 下一篇单独编译Android源代码中的模块

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C语言中如何将结构体 (2025-12-24 22:20:09)
·纯C语言结构体成员变 (2025-12-24 22:20:06)
·C语言中,指针函数和 (2025-12-24 22:20:03)
·哈希表 - 菜鸟教程 (2025-12-24 20:18:55)
·MySQL存储引擎InnoDB (2025-12-24 20:18:53)