#include <ctype.h>
#include <limits.h>
#include "cdp_stringex.h"
#include "cdp_templ.h"
//#include "cdp_logger.h"
namespace CDP
{
// 字符串的结尾字符
char g_CharNil = '\0';
CString g_EmptyString = "";
CString::CString()
{
m_pchData = NULL;
m_iLength = 0;
m_iBufferLength = 0;
}
CString::CString(const CString& stringSrc)
{
m_pchData = NULL;
m_iLength = 0;
m_iBufferLength = 0;
if(AllocBuffer(stringSrc.GetLength()))
{
m_iLength = stringSrc.GetLength();
memcpy(m_pchData, (PCSTR)stringSrc, m_iLength + 1);
}
}
CString::CString(char ch, int nLength)
{
m_pchData = NULL;
m_iLength = 0;
m_iBufferLength = 0;
if(AllocBuffer(nLength))
{
m_iLength = nLength;
if('\0' == ch)
{
m_iLength = 0;
}
memset(m_pchData, ch, nLength);
m_pchData[m_iLength] = '\0';
}
}
CString::CString(const char* lpch, int nLength)
{
m_pchData = NULL;
m_iLength = 0;
m_iBufferLength = 0;
if(AllocBuffer(nLength))
{
if(NULL != lpch)
{
memcpy(m_pchData, lpch, nLength);
m_pchData[nLength] = '\0';
m_iLength = strlen(m_pchData);
}
}
}
CString::CString(const char* lpsz)
{
m_pchData = NULL;
m_iLength = 0;
m_iBufferLength = 0;
int nLength = SafeStrlen(lpsz);
if(AllocBuffer(nLength))
{
if(NULL != lpsz)
{
m_iLength = nLength;
memcpy(m_pchData, lpsz, m_iLength);
m_pchData[m_iLength] = '\0';
}
}
}
CString::CString(int iValue)
{
m_pchData = NULL;
m_iLength = 0;
m_iBufferLength = 0;
*this = iValue;
}
// 分配内存
// First time: alloc nLen + 1, after: 2 * nLen + 1
TBool CString::AllocBuffer(int nLen)
{
if(nLen <= 0)
{
return False;
}
if(NULL == m_pchData)
{
m_pchData = (char *)malloc(nLen + 1);
if(NULL == m_pchData)
{
return False;
}
memset(m_pchData, 0, nLen + 1);
m_iBufferLength = nLen + 1;
return True;
}
else
{
if(m_iBufferLength >= nLen + 1)
{
return True;
}
int iBufferLength = nLen * 2 + 1;
char* pszTmp = (char *)malloc(iBufferLength);
if(NULL != pszTmp)
{
memcpy(pszTmp, m_pchData, m_iBufferLength);
memset(pszTmp + m_iBufferLength, 0, iBufferLength - m_iBufferLength);
free(m_pchData);
m_pchData = pszTmp;
m_iBufferLength = iBufferLength;
return True;
}
else
{
return False;
}
}
}
void CString::FreeData()
{
if(NULL != m_pchData)
{
free(m_pchData);
}
m_pchData = NULL;
m_iLength = 0;
m_iBufferLength = 0;
}
void CString::Release()
{
if(NULL != m_pchData)
{
free(m_pchData);
}
m_pchData = NULL;
m_iLength = 0;
m_iBufferLength = 0;
}
void CString::Empty()
{
if(NULL != m_pchData)
{
memset(m_pchData, 0, m_iBufferLength);
m_iLength = 0;
}
}
CString::~CString()
{
Release();
}
void CString::SetValue(const char* lpch, int nLength)
{
if(0 == nLength || NULL == lpch)
{
Empty();
}
if(AllocBuffer(nLength))
{
memcpy(m_pchData, lpch, nLength);
m_pchData[nLength] = '\0';
m_iLength = nLength;
}
}
const CString& CString::operator=(const CString& stringSrc)
{
if(this == &stringSrc)
{
return *this;
}
int nLength = stringSrc.GetLength();
SetValue((PCSTR)stringSrc, nLength);
return *this;
}
// 赋值操作
const CString& CString::operator=(const char* lpsz)
{
int nLength = SafeStrlen(lpsz);
SetValue(lpsz, nLength);
return *this;
}
const CString& CString::operator=(int iValue)
{
Format("%d", iValue);
return *this;
}
// 字符串连接
void CString::ConcatCopy(int nSrc1Len, const char* lpszSrc1Data,
int nSrc2Len, const char* lpszSrc2Data)
{
int nLength = nSrc1Len + nSrc2Len;
if(nLength <= 0)
{
Empty();
}
if(AllocBuffer(nLength))
{
if(NULL != lpszSrc1Data && nSrc1Len > 0)
{
memcpy(m_pchData, lpszSrc1Data, nSrc1Len);
}
if(NULL != lpszSrc2Data && nSrc2Len > 0)
{
memcpy(m_pchData + nSrc1Len, lpszSrc2Data, nSrc2Len);
}
m_iLength = nLength;
m_pchData[m_iLength] = '\0';
}
}
CString operator+(const CString& string1, const CString& string2)
{
CString s;
s.ConcatCopy(string1.GetLength(), (PCSTR)string1,
string2.GetLength(), (PCSTR)string2);
return s;
}
CString operator+(const CString& string, const char* lpsz)
{
assert(lpsz == NULL || isValidStr(lpsz));
CString s;
s.ConcatCopy(string.GetLength(), (PCSTR)string,
CString::SafeStrlen(lpsz), lpsz);
return s;
}
CString operator+(const char* lpsz, const CString& string)
{
assert(lpsz == NULL || isValidStr(lpsz));
CString s;
s.ConcatCopy(CString::SafeStrlen(lpsz), lpsz, string.GetLength(),
(PCSTR)string);
return s;
}
void CString::Concat(int nSrcLen, const char* lpszSrcData)
{
// 不能连接空字符串
if (nSrcLen <= 0 || NULL == lpszSrcData)
{
return;
}
int nLength = m_iLength + nSrcLen;
if(AllocBuffer(nLength))
{
memcpy(m_pchData + m_iLength, lpszSrcData, nSrcLen);
m_pchData[nLength] = '\0';
m_iLength = nLength;
}
}
const CString& CString::operator+=(const char* lpsz)
{
Concat(SafeStrlen(lpsz), lpsz);
return *this;
}
const CString& CString::operator+=(char ch)
{
Concat(1, &ch);
return *this;
}
const CString& CString::operator+=(const CString& string)
{
Concat(string.GetLength(), string.m_pchData);
return *this;
}
// 直接操作字符串内存的函数
char* CString::GetBuffer()
{
return m_pchData;
}
void CString::FreeExtra()
{
if(NULL == m_pchData || m_iLength >= m_iBufferLength - 1)
{
return;
}
if(0 == m_iLength)
{
this->Release();
return;
}
char* pszTmp = (char *)malloc(m_iLength + 1);
if(NULL != pszTmp)
{
return;
}
else
{
memcpy(pszTmp, m_pchData, m_iLength);
pszTmp[m_iLength] = '\0';
free(m_pchData);
m_pchData = pszTmp;
m_iBufferLength = m_iLength + 1;
}
}
// 字符串高级操作函数
// 查找操作
int CString::Find(char ch) const
{
return Find(ch, 0);
}
int CString::Find(char ch, int nStart) const
{
if (nStart >= m_iLength)
return -1;
// find first single character
char* lpsz = strchr(m_pchData + nStart, (unsigned char)ch);
// return -1 if not found and index otherwise
return (lpsz == NULL) ? -1 : (int)(lpsz - m_pchData);
}
int CString::FindOneOf(const char* lpszCharSet) const
{
if(NULL == lpszCharSet)
{
return NULL;
}
char* lpsz = strpbrk(m_pchData, lpszCharSet);
return (lpsz == NULL) ? -1 : (int)(lpsz - m_pchData);
}
int CString::ReverseFind(char ch) const
{
// find last single character
char* lpsz = strrchr(m_pchData, (unsigned char) ch);
// return -1 if not found, distance from beginning otherwise
return (lpsz == NULL) ? -1 : (int)(lpsz - m_pchData);
}
// 子串查找
int CString::Find(const char* lpszSub) const
{
return Find(lpszSub, 0);
}
int CString::Find(const char* lpszSub, int nStart) const
{
if(NULL == lpszSub)
{
return -1;
}
if (nStart > m_iLength)
return -1;
// 查找第一个匹配的字符串
char* lpsz = strstr(m_pchData + nStart, lpszSub);
// -1表示没有找到
return (lpsz == NULL) ? -1 : (int)(lpsz - m_pchData);
}
void CString::MakeUpper()
{
//CopyBeforeWrite();
CStrOP::ToUpper(m_pchData);
}
void CString::MakeLower()
{
//CopyBeforeWrite();
CStrOP::ToLower(m_pchData);
}
void CString::MakeReverse()
{
//CopyBeforeWrite();
CStrOP::Reverse(m_pchData);
}
void CString::SetAt(int nIndex, char ch)
{
if(nIndex < 0 || nIndex >= m_iLength)
{
return;
}
//CopyBeforeWrite();
m_pchData[nIndex] = ch;
if(ch == '\0')
{
m_iLength = nIndex;
}
}
// 赋值操作
const CString& CString::operator=(char ch)
{
SetValue(&ch, 1);
return *this;
}
// 字符串和字符连接运算符
// String + ch
CString operator+(const CString& string1, char ch)
{
CString s = string1;
s.Concat(1, &ch);
return s;
}
// ch