设为首页 加入收藏

TOP

标准C++实现字符串类CString(五)
2014-11-24 03:17:21 来源: 作者: 【 】 浏览:5
Tags:标准 实现 字符串 CString
}

// TODO: check the space left in the two pading of the whole buffer

// trim the left spaces
void CString::TrimLeft(void) // //将字符串右边的空格去掉
{
int start = 0;

while (isspace(m_pDataStart[start]) && start < m_nLength)
start ++;

if (start > 0)
{
m_pDataStart += start;
}
}


// trim the right spaces
void CString::TrimRight(void) //将字符串右边的空格去掉
{
int end = m_nLength - 1;

while (isspace(m_pDataStart[end]) && end >= 0)
end --;

if (end < 0)
{
end = 0;
m_pDataEnd = &(m_pDataStart[end]);
}
else
{
m_pDataEnd = &(m_pDataStart[end]);

}

}

// trim both sides
void CString::Trim(void) // //将字符串的空格去掉
{
TrimLeft();
TrimRight();
}


int CString::Replace(const char *lpszOld, const char *lpszNew)
{
// can't have empty or NULL lpszOld
if (!lpszOld)
return 0;
int nOldLen = strlen(lpszOld); //获得旧字符串的长度
if (nOldLen <= 0)
return 0;
int nNewLen = strlen(lpszNew); //获得新字符串的长度

// loop once to figure out the size of the result string
int nCount = 0;
char *lpszStart = m_pDataStart;
char *lpszEnd = m_pDataEnd;
char *lpszTarget;
while (lpszStart < lpszEnd) //循环处理原有字符串
{
while ((lpszTarget = strstr(lpszStart, lpszOld)) != NULL) //如果在字符串lpszStart中发现子串lpszOld
{
nCount++; //子串数量+1
lpszStart = lpszTarget + nOldLen; //往后定位字符串lpszStart,从第一个子串后开始
}
lpszStart += strlen(lpszStart) + 1; //往后查找
}

// if any changes were made, make them
if (nCount > 0) //如果有重复的字符串
{
// allocate a new buffer (slow but sure)
int nNewLength = m_nLength + (nNewLen - nOldLen) * nCount; //覆盖后总字符串的大小
AssignNewSpace(nNewLength + 1, 1); //为新的字符串分配内存空间
// then we just do it in-place
lpszStart = m_pDataStart; //重新初始化m_pDataStart,lpszStart,lpszEnd
lpszEnd = m_pDataEnd;

// loop again to actually do the work
while (lpszStart < lpszEnd) //循环处理原来的字符串
{
while ( (lpszTarget = strstr(lpszStart, lpszOld)) != NULL) //如果在字符串lpszStart中发现子串lpszOld
{
int nBalance = lpszEnd - (lpszTarget + nOldLen); //字符串lpszTarget后面的字符数量
memmove(lpszTarget + nNewLen, lpszTarget + nOldLen,
nBalance * sizeof(char)); //移走lpszTarget原来的字符串,并为lpszTarget重新设置为nNewLen大小内存
memcpy(lpszTarget, lpszNew, nNewLen * sizeof(char)); //将新字符串lpszNew覆盖旧的子串lpszTarget
lpszStart = lpszTarget + nNewLen; //寻找目标字符串后移nNewLen
lpszStart[nBalance] = '\0';
}
lpszStart += strlen(lpszStart) + 1; //寻找目标字符串往后走
}
m_nLength = nNewLength;
}

return nCount;
}

// format a string
void CString::Format(char *fmt, ...)
{
char TmpBuffer[STR_PAGE_SIZE]; // TODO, should calculate this size dynamically.

va_list argList;
va_start(argList, fmt);
#ifdef WIN32
_vsnprintf(TmpBuffer, STR_PAGE_SIZE, fmt, argList); // just not overwrite something
#else
vsnprintf(TmpBuffer, STR_PAGE_SIZE, fmt, argList); // just not overwrite something
#endif
va_end(argList);
}

// copy string content from ANSI string (converts to TCHAR)
const CString& CString::operator=(const char *lpsz)
{
int len = strlen(lpsz);

if (m_nSize < len) // c + '\0' length
{ // it needs to realloc space, but we use new and delete pair
SafeDestroy();
m_pBuffer = new char[len + 1];
m_nSize = len + 1;
//TODO, I don't check the value of this pointer, unkown result. :)
}
m_pDataStart = m_pBuffer;
strcpy((char *)m_pDataStart, lpsz);
m_pDataStart[len] = 0;
m_pDataEnd = &(m_pDataStart[len]);
m_nLength = len;

return *this;
}

// concatenate a UNICODE character after converting it to

首页 上一页 2 3 4 5 6 下一页 尾页 5/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)