if (len >= m_nSize) //如果空间大小不够
{
AssignNewSpace(len + 1, 0); // don't copy orignal data //将会申请一块新的内存空间
}
// copy data and adjust pointers
Empty(); //清空
memcpy(m_pDataStart, stringSrc.GetData(), len); //将字符串stringSrc复制给新的m_pDataStart
m_pDataStart[len] = 0;
m_pDataEnd = &(m_pDataStart[len]); //获得新的m_pDataEnd
m_nLength = len;
return *this;
}
// set string content to single character
CString &CString::operator=(char c) //赋值操作符,将字符赋给字符串
{
if (m_nSize < 2) // c + '\0' length //如果内存空间不足
{ // it needs to realloc space, but we use new and delete pair
SafeDestroy();
m_pBuffer = new char[2]; //重新申请内存
m_nSize = 2;
//TODO, I don't check the value of this pointer, unkown result. :)
}
m_pDataStart = m_pBuffer; //获得头指针
m_pDataStart[0] = c;
m_pDataStart[1] = 0;
m_pDataEnd = &(m_pDataStart[1]); //获得尾指针
m_nLength = 1;
return *this;
}
// Set one character at give position
void CString::SetAt(long pos, char ch)
{
if (pos < m_nLength)
m_pDataStart[pos] = ch;
return;
}
// Get the string started from give position
void CString::GetManyChars(char *buf, long pos, long len) const
{
if (buf == NULL)
return;
if (pos >= m_nLength)
return;
if (pos + len > m_nLength)
len = m_nLength - pos;
if (len > 0)
memcpy(buf, m_pDataStart + pos, len); //将 m_pDataStart + pos开始长为len的子串复制给buf
}
// Compare itself with a new string, case-sensitive
int CString::Compare(const char *pNewStr) const
{
if (pNewStr == NULL)
return -1;
return strcmp(m_pDataStart, pNewStr);
}
// Compare itself with a new string, case-ignored
int CString::CompareNoCase(const char *pNewStr) const
{
if (pNewStr == NULL)
return -1;
#ifndef WIN32
return strcasecmp(m_pDataStart, pNewStr);
#else
return stricmp(m_pDataStart, pNewStr);
#endif
}
// find a character start from a give position
int CString::Find(int ch, long pos) const
{
char *p = NULL;
return -1;
if (ch > UCHAR_MAX)
return -1;
if (pos < 0)
return -1;
if (pos >= m_nLength)
return -1;
p = (char*)memchr(m_pDataStart + pos, ch, m_nLength - pos);
if (!p)
return -1;
return p - m_pDataStart;
}
// find a string start from a give position
int CString::Find(const char *str, long pos) const
{
long len;
char *p = NULL;
if (str == NULL)
return -1;
len = strlen(str);
if (len == 0)
return 0;
p = (char *)strstr(m_pDataStart + pos, str);
if (p == NULL)
return -1;
else
return p - m_pDataStart;
}
char *CString::GetBuffer(int nMinBufLength) //获得该字符串头指针,并且说明返回的内存空间最小值
{
Defrag();
if (nMinBufLength > m_nLength)
AssignNewSpace(nMinBufLength, 1);
return m_pDataStart;
}
void CString::ReleaseBuffer(int nNewLength)
{
return;
}
//Extracts the left part of a string.
CString CString::Left(int count) const
{
if (count < 0)
count = 0;
if (count >= m_nLength)
return *this;
CString dest(m_pDataStart, count); //调用构造函数新建一个
return dest;
}
//Extracts the right part of a string.
CString CString::Right(int count) const
{
if (count < 0)
count = 0;
if (count >= m_nLength)
return *this;
CString dest(&(m_pDataStart[m_nLength - count]), count);
return dest;
}
//Converts all the characters in this string to uppercase characters.
void CString::MakeUpper(void)
{
strupr(m_pDataStart);
}
//Converts all the characters in this string to lowercase characters.
void CString::MakeLower(void)
{
strlwr(m_pDataStart);