📄 string.cpp
字号:
#include "Core.h"
#include <iostream.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
#include <assert.h>
/**
字符串拷贝函数,解决len小于src字符串长度的情况
*/
char* y_strncpy(char *io_pDst, const char *i_pSrc, int i_nLen)
{
assert(io_pDst != NULL && i_pSrc != NULL);
strncpy(io_pDst, i_pSrc, i_nLen);
if (i_nLen > 0)
io_pDst[i_nLen] = '\0';
return io_pDst;
}
/**
\class C_String y_core.h
\brief 字符串处理类.
实现了跨平台的字符串处理类,内部采用ascii码的单字节数组实现存储
方式,对于服务端的字符串处理效率很高.
*/
/**
默认构造函数
*/
C_String::C_String():
m_nLen(0),
m_nSize(1)
{
m_pBuf = new char[1];
assert (m_pBuf != NULL);
*m_pBuf = '\0';
}
/**
构造函数
*/
C_String::C_String(char i_mChar, int i_nCount)
{
int t_nCount = i_nCount;
if (i_nCount < 1)
{
t_nCount = 1;
}
m_pBuf = new char[t_nCount+1];
assert (m_pBuf != NULL);
for (int i= 0; i< t_nCount; ++i)
{
*(m_pBuf+i) = i_mChar;
}
*(m_pBuf + t_nCount) = '\0';
m_nLen = t_nCount;
m_nSize = t_nCount+1;
}
/**
构造函数
*/
C_String::C_String(const char* i_pStr)
{
assert(i_pStr != NULL);
int tLen = strlen(i_pStr);
m_pBuf = new char[tLen + 1];
assert(m_pBuf != NULL);
memcpy(m_pBuf, i_pStr, tLen);
m_pBuf[tLen] = '\0';
m_nLen = tLen;
m_nSize = tLen + 1;
}
/**
构造函数
*/
C_String::C_String(const char* i_pStr, int i_nLen)
{
assert(i_pStr != NULL);
if (i_nLen <= 0)
{
m_pBuf = new char[1];
assert (m_pBuf != NULL);
*m_pBuf = '\0';
m_nLen = 0;
m_nSize = 1;
return;
}
int tLen = strlen(i_pStr);
if (i_nLen < tLen)
{
m_pBuf = new char[i_nLen + 1];
assert (m_pBuf != NULL);
m_nLen = i_nLen;
}
else
{
m_pBuf = new char[tLen + 1];
assert(m_pBuf != NULL);
m_nLen = tLen;
}
y_strncpy(m_pBuf, i_pStr, i_nLen);
m_nSize = m_nLen + 1;
}
/**
构造函数
*/
C_String::C_String(const C_String& i_ysStr)
{
int n = i_ysStr.length();
m_pBuf = new char[n+1];
assert(m_pBuf != NULL);
strcpy(m_pBuf, i_ysStr.m_pBuf);
m_nLen = n;
m_nSize = n+1;
}
/**
构造函数
*/
C_String::C_String(int i_nInitSize)
{
if (i_nInitSize < 1)
{
i_nInitSize = 1;
}
m_pBuf = new char[i_nInitSize];
assert(m_pBuf != NULL);
*m_pBuf = '\0';
m_nLen = 0;
m_nSize = i_nInitSize;
}
/**
构造函数
*/
C_String::C_String(int i_nInitSize, const char* i_pStr)
{
int tLen = strlen(i_pStr);
if (i_nInitSize <= tLen)
{
i_nInitSize = tLen + 1;
}
m_pBuf = new char[i_nInitSize];
assert(m_pBuf != NULL);
y_strncpy(m_pBuf, i_pStr, tLen);
m_nLen = tLen;
m_nSize = i_nInitSize;
}
/**
析构函数
*/
C_String::~C_String()
{
if (m_pBuf != NULL)
{
delete []m_pBuf;
}
}
/**
增加字符个数
*/
bool C_String::addNum(int i_nNumAdd)
{
char *temp = NULL;
int t_nNum = 0;
if (i_nNumAdd == 0)
{
return true;
}
t_nNum = m_nLen + i_nNumAdd; // 改变后的个数
if (t_nNum >= m_nSize) // 需重新分配内存
{
temp = new char[t_nNum + 1];
assert (temp != NULL);
memcpy(temp, m_pBuf, m_nLen);
temp[m_nLen] = 0;
if (t_nNum > m_nLen)
{
m_nLen = t_nNum;
}
m_nSize = t_nNum + 1;
delete []m_pBuf;
m_pBuf = temp;
}
else // 内存够用
{
m_nLen = t_nNum;
*(m_pBuf + m_nLen) = 0;
}
return true;
}
/**
增加缓冲区大小
*/
bool C_String::addSize(int i_nSizeAdd)
{
char *temp = NULL;
int t_nSize = 0;
t_nSize = m_nSize + i_nSizeAdd;
if (t_nSize < 0)
{
t_nSize = 0;
}
temp = new char[t_nSize + 1];
assert(temp != NULL);
if (i_nSizeAdd > 0)
{
memcpy(temp, m_pBuf, m_nLen);
temp[m_nLen] = '\0';
}
else
{
memcpy(temp, m_pBuf, t_nSize);
temp[t_nSize] = '\0';
}
delete []m_pBuf;
m_pBuf = temp;
if (m_nLen > t_nSize)
{
m_nLen = t_nSize;
m_pBuf[t_nSize-1] = '\0';
}
m_nSize = t_nSize;
return true;
}
/**
设置缓冲区大小
*/
bool C_String::setSize(int i_nSize)
{
if (i_nSize < 1)
{
i_nSize = 1;
}
return addSize(i_nSize - m_nSize);
}
/**
清空
*/
void C_String::clear()
{
if (m_pBuf != NULL)
{
m_nLen = 0;
*m_pBuf = '\0';
}
else
{
m_pBuf = new char[1];
assert (m_pBuf != NULL);
*m_pBuf = '\0';
m_nLen = 0;
m_nSize = 1;
}
}
/**
清掉多余内存
*/
void C_String::freeExtra()
{
addSize(m_nLen-m_nSize+1);
}
/**
重载"+"
*/
C_String operator+ (const C_String& i_ysStr1, const C_String& i_ysStr2)
{
C_String mTempStr(i_ysStr1);
mTempStr += i_ysStr2;
return mTempStr;
}
/**
重载"+"
*/
C_String operator+ (const C_String& i_ysStr, const char *i_pStr)
{
C_String mTempStr(i_ysStr);
mTempStr += i_pStr;
return mTempStr;
}
/**
重载"+"
*/
C_String operator+ (const char *i_pStr, const C_String& i_ysStr)
{
if (i_pStr == NULL)
{
return i_ysStr;
}
C_String mTempStr(i_pStr);
mTempStr += i_ysStr;
return mTempStr;
}
/**
重载"+"
*/
C_String operator+ (const C_String& i_ysStr, char i_mChar)
{
C_String mTempStr(i_ysStr);
mTempStr += i_mChar;
return mTempStr;
}
/**
重载"+"
*/
C_String operator+ (char i_mChar, const C_String& i_ysStr)
{
C_String mTempStr(i_mChar);
mTempStr += i_ysStr;
return mTempStr;
}
/**
重载"="
*/
C_String& C_String::operator= (const C_String& i_ysStr)
{
if (this != &i_ysStr)
{
clear(); // 先清空
addNum(i_ysStr.m_nLen);
strcpy(m_pBuf, i_ysStr.m_pBuf);
}
return *this;
}
/**
重载"="
*/
C_String& C_String::operator= (const char* i_pStr)
{
if (i_pStr != NULL)
{
clear(); // 先清空
addNum(strlen(i_pStr));
strcpy(m_pBuf, i_pStr);
}
return *this;
}
/**
重载"="
*/
C_String& C_String::operator= (char i_mChar)
{
char tmp[2];
tmp[0] = i_mChar;
tmp[1] = 0;
clear(); // 先清空
addNum(1);
strcpy(m_pBuf, tmp);
return *this;
}
/**
重载"="
*/
C_String& C_String::operator= (int i_nVal)
{
char tmp[32];
sprintf(tmp, "%u", i_nVal);
clear(); // 先清空
addNum(strlen(tmp));
strcpy(m_pBuf, tmp);
return *this;
}
/**
重载"+="
*/
C_String& C_String::operator+= (const C_String& i_ysStr)
{
addNum(i_ysStr.m_nLen);
strcat(m_pBuf, i_ysStr.cstr());
return *this;
}
/**
重载"+="
*/
C_String& C_String::operator+= (const char* i_pStr)
{
if (i_pStr != NULL)
{
addNum(strlen(i_pStr));
strcat(m_pBuf, i_pStr);
}
return *this;
}
/**
重载"+="
*/
C_String& C_String::operator+= (char i_mChar)
{
char tmp[2];
tmp[0] = i_mChar;
tmp[1] = 0;
addNum(1);
strcat(m_pBuf, tmp);
return *this;
}
/**
重载"+="
*/
C_String& C_String::operator+= (int i_nVal)
{
char tmp[32];
sprintf(tmp, "%u", i_nVal);
addNum(strlen(tmp));
strcat(m_pBuf, tmp);
return *this;
}
/**
转换成大写
*/
void C_String::toUpper()
{
for (int i = 0; i < m_nLen; ++i)
{
if (*(m_pBuf+i) > 0)
{
*(m_pBuf+i) = toupper(*(m_pBuf+i));
}
}
}
/**
转换成小写
*/
void C_String::toLower()
{
for (int i= 0; i< m_nLen; ++i)
{
if (*(m_pBuf + i) > 0)
{
*(m_pBuf + i) = tolower(*(m_pBuf+i));
}
}
}
/**
互换大小写
*/
void C_String::toReverse()
{
for (int i= 0; i< m_nLen; ++i)
{
if (*(m_pBuf+i) > 0)
{
if (!isupper(*(m_pBuf+i)))
{
*(m_pBuf+i) = toupper(*(m_pBuf+i));
}
else
{
*(m_pBuf+i) = tolower(*(m_pBuf+i));
}
}
}
}
/**
在索引字符前增加字符串
*/
bool C_String::addBefore(int i_nIndex, const char *i_pStr)
{
assert(i_pStr != NULL);
int len = strlen(i_pStr);
int oldLen = m_nLen;
if (i_nIndex > m_nLen || i_nIndex < 0)
{
return false;
}
addNum(len);
memmove(m_pBuf+i_nIndex+len, m_pBuf+i_nIndex, oldLen-i_nIndex+1);
memcpy(m_pBuf+i_nIndex, i_pStr, len);
return true;
}
/**
在索引字符后增加字符串
*/
bool C_String::addAfter(int i_nIndex, const char *i_pStr)
{
assert(i_pStr != NULL);
int len = strlen(i_pStr);
int oldLen = m_nLen;
if (i_nIndex >= m_nLen || i_nIndex < 0)
{
return false;
}
addNum(len);
memmove(m_pBuf + i_nIndex + 1 + len, m_pBuf + i_nIndex +1, oldLen - i_nIndex);
memcpy(m_pBuf + i_nIndex + 1, i_pStr, len);
return true;
}
/**
删除索引字符开始一定长度的字符
*/
bool C_String::removeAt(int i_nIndex, int i_nLen)
{
if (i_nLen <= 0)
{
return true;
}
if (i_nIndex > m_nLen-1 || i_nIndex < 0 || i_nLen < 0 || i_nLen > m_nLen-i_nIndex)
{
return false;
}
memmove(m_pBuf + i_nIndex, m_pBuf + i_nIndex + i_nLen, m_nLen - i_nIndex - i_nLen);
addNum(-i_nLen);
return true;
}
/**
替换索引字符开始一定长度的字符
*/
bool C_String::replaceAt(int i_nIndex, int i_nLen, const char *i_pStr)
{
if (!removeAt(i_nIndex, i_nLen) || !addBefore(i_nIndex, i_pStr))
{
return false;
}
return true;
}
/**
查找字符串
*/
int C_String::find(const char *i_pStr, int i_nNum, int i_nIndex) const
{
assert(i_pStr != NULL);
char *pos = NULL, *oldPos = NULL;
if (i_nIndex > m_nLen -1 || i_nIndex < 0)
{
return -1;
}
oldPos = m_pBuf + i_nIndex;
for (int i= 0; i< i_nNum; ++i)
{
pos = strstr(oldPos, i_pStr);
if (pos == NULL)
{
return -2;
}
oldPos = pos + strlen(i_pStr);
}
return (int)(pos -m_pBuf);
}
/**
不区分大小写查找字符串
*/
int C_String::findNoCase(const char *i_pStr, int i_nNum, int i_nIndex) const
{
assert(i_pStr != NULL);
C_String tSourceStr(*this);
tSourceStr.toUpper();
C_String tFindStr(i_pStr);
tFindStr.toUpper();
return tSourceStr.find(tFindStr.cstr(), i_nNum, i_nIndex);
}
/**
获取字符串出现的个数
*/
int C_String::strCount(const char *i_pStr, int i_nIndex) const
{
assert(i_pStr != NULL);
int count = 0;
char *pos = NULL, *oldPos = NULL;
oldPos = m_pBuf + i_nIndex;
while (true)
{
pos = strstr(oldPos, i_pStr);
if (pos == NULL)
{
break;
}
++count;
oldPos = pos + strlen(i_pStr);
}
return count;
}
/**
不区分大小写获取字符串出现的个数
*/
int C_String::strCountNoCase(const char *i_pStr, int i_nIndex) const
{
assert(i_pStr != NULL);
C_String tSourceStr(*this);
tSourceStr.toUpper();
C_String tCountStr(i_pStr);
tCountStr.toUpper();
return tSourceStr.strCount(tCountStr.cstr(), i_nIndex);
}
/**
替换查找
*/
int C_String::replaceFind(const char *i_pSrc, const char *i_pDst, int i_nIndex)
{
assert(i_pSrc != NULL && i_pDst != NULL);
int count = 0;
int tOldPos = 0;
char *pos = NULL, *oldPos = NULL;
oldPos = m_pBuf + i_nIndex;
int tSrcLen = strlen(i_pSrc);
int tDstLen = strlen(i_pDst);
while (true)
{
pos = strstr(m_pBuf + tOldPos, i_pSrc);
if (pos == NULL)
{
break;
}
++count;
tOldPos = pos + tDstLen - m_pBuf;
replaceAt(pos-m_pBuf, tSrcLen, i_pDst);
}
return count;
}
/**
格式化字符串
*/
C_String& C_String::format(const char *i_pFormat, ...)
{
assert(i_pFormat != NULL);
va_list ap;
va_start(ap, i_pFormat);
setSize(256);
vsprintf(m_pBuf, i_pFormat, ap);
setSize(strlen(m_pBuf)+1);
m_nLen = strlen(m_pBuf); // 设置长度
va_end(ap);
return *this;
}
/**
将字符串转换为整型值
*/
int C_String::toInt()
{
return atoi(m_pBuf);
}
/**
将字符串转换为浮点值
*/
double C_String::toReal()
{
return atof(m_pBuf);
}
/**
取字符串左开始一定长度的字符串
*/
C_String C_String::left(int i_nLen) const
{
int t_nLen = 0;
if (i_nLen <= 0)
{
return "";
}
if (i_nLen > m_nLen)
{
t_nLen = m_nLen;
}
else
{
t_nLen = i_nLen;
}
return C_String(m_pBuf, t_nLen);
}
/**
取字符串右开始一定长度的字符串
*/
C_String C_String::right(int i_nLen) const
{
int t_nLen = 0;
if (i_nLen <= 0)
{
return "";
}
else if (i_nLen > m_nLen)
{
t_nLen = m_nLen;
}
else
{
t_nLen = i_nLen;
}
return C_String(m_pBuf+m_nLen-t_nLen, t_nLen);
}
/**
取字符串中间索引处开始一定长度的字符串
*/
C_String C_String::mid(int i_nIndex, int i_nLen) const
{
if (i_nIndex < 0 || i_nIndex >= m_nLen || i_nLen == 0)
{
return "";
}
int t_nLen = 0;
if (i_nLen == -1 || i_nLen > m_nLen - i_nIndex + 1)
{
t_nLen = m_nLen - i_nIndex;
}
else
{
t_nLen = i_nLen;
}
return C_String(m_pBuf+i_nIndex, t_nLen);
}
/**
去除字符串左边的无效字符
*/
void C_String::trimLeft()
{
int tCount = 0;
// 去除开始的空格
for (int i= 0; i< m_nLen; ++i)
{
if (*(m_pBuf+i) == ' ' || *(m_pBuf+i) == '\n'
|| *(m_pBuf+i) == '\t' || *(m_pBuf+i) == '\r')
{
++tCount;
}
else
{
break;
}
}
if (tCount > 0)
{
removeAt(0, tCount);
}
}
/**
去除字符串右边的无效字符
*/
void C_String::trimRight()
{
// 去除结尾的空格
for (int j= m_nLen-1; j> 0; --j)
{
if (*(m_pBuf+j) == ' ' || *(m_pBuf+j) == '\n'
|| *(m_pBuf+j) == '\t' || *(m_pBuf+j) == '\r')
{
*(m_pBuf + j) = 0;
--m_nLen;
}
else
{
break;
}
}
}
/**
去除字符串的无效字符
*/
void C_String::trimAll()
{
int tCount = 0;
// 去除开始的空格
for (int i= 0; i< m_nLen; ++i)
{
if (*(m_pBuf+i) == ' ' || *(m_pBuf+i) == '\n'
|| *(m_pBuf+i) == '\t' || *(m_pBuf+i) == '\r')
{
++tCount;
}
else
{
break;
}
}
if (tCount > 0)
{
removeAt(0, tCount);
}
// 去除结尾的空格
for (int j= m_nLen-1; j> 0; --j)
{
if (*(m_pBuf+j) == ' ' || *(m_pBuf+j) == '\n'
|| *(m_pBuf+j) == '\t' || *(m_pBuf+j) == '\r')
{
*(m_pBuf + j) = 0;
m_nLen --;
}
else
{
break;
}
}
}
/**
获取索引处的字符
*/
char C_String::operator[](int i_nIndex) const
{
char tChar;
if (i_nIndex < 0)
{
i_nIndex = 0;
}
else if (i_nIndex >= m_nLen-1)
{
i_nIndex = m_nLen -1;
}
tChar = *(m_pBuf + i_nIndex);
return tChar;
}
/**
获取索引处的字符
*/
char& C_String::operator[](int i_nIndex)
{
if (i_nIndex < 0)
{
i_nIndex = 0;
}
else if (i_nIndex >= m_nLen-1)
{
i_nIndex = m_nLen -1;
}
return *(m_pBuf + i_nIndex);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -