📄 sh_string.cpp
字号:
// SH_String.cpp: implementation of the SH_String class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SH_String.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
static const size_t SH_StringInitialSize = 32;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
SH_String::SH_String()
{
m_nLength = 0;
m_nSize = SH_StringInitialSize;
m_pBuffer = (char*)malloc(m_nSize);
Clear();
}
SH_String::SH_String(const char * s)
{
m_nLength = 0;
m_nSize = SH_StringInitialSize;
m_pBuffer = (char*)malloc(m_nSize);
Clear();
Cat(s);
}
SH_String::~SH_String()
{
Delete();
}
VOID SH_String::Resize(const size_t newSize)
{
char * newBuffer = (char*)realloc(m_pBuffer, newSize);
m_nSize = newSize;
m_pBuffer = newBuffer;
}
BOOL SH_String::AutoResize ()
{
BOOL bRet = TRUE;
if (m_nSize <= (LONG)(~0) / 2)
{
const size_t newSize = m_nSize * 2;
Resize (newSize);
}
return TRUE;
}
VOID SH_String::Clear()
{
m_nLength = 0;
m_pBuffer [0] = '\0';
}
VOID SH_String::Delete()
{
if (m_pBuffer != NULL)
{
free(m_pBuffer);
m_pBuffer = NULL;
m_nSize = 0;
m_nLength = 0;
}
}
VOID SH_String::Put(const int c)
{
if (m_nLength == m_nSize)
AutoResize();
m_pBuffer [m_nLength] = c;
if (c != '\0')
m_nLength++;
}
VOID SH_String::Cat(const char * s)
{
const size_t len = strlen (s);
while (m_nLength + len >= m_nSize)
AutoResize();
strcpy (m_pBuffer + m_nLength, s);
m_nLength += len;
}
VOID SH_String::Cat (const char * s,const size_t length)
{
const char *p = s;
size_t remain = length;
while (*p != '\0' && remain > 0)
{
Put(*p);
--remain;
++p;
}
Terminate();
}
VOID SH_String::StripNewLine()
{
const size_t final = m_nLength - 1;
if (m_pBuffer [final] == '\n')
{
m_pBuffer [final] = '\0';
m_nLength--;
}
}
VOID SH_String::StripLeading()
{
while (isspace ((int)m_pBuffer [0]) && m_nLength > 0)
{
size_t i;
for (i = 1 ; i < m_nLength ; ++i)
m_pBuffer [i - 1] = m_pBuffer [i];
--m_nLength;
m_pBuffer [m_nLength] = '\0';
}
}
VOID SH_String::StripLeading(const int c)
{
while (((int)m_pBuffer [0]) == c && m_nLength > 0)
{
size_t i;
for (i = 1 ; i < m_nLength ; ++i)
m_pBuffer [i - 1] = m_pBuffer [i];
--m_nLength;
m_pBuffer [m_nLength] = '\0';
}
}
VOID SH_String::StripTrailing()
{
while (isspace ((int) m_pBuffer [m_nLength - 1]) && m_nLength > 0)
{
m_nLength--;
m_pBuffer [m_nLength] = '\0';
}
}
VOID SH_String::StripTrailing(const int c)
{
while (((int) m_pBuffer [m_nLength - 1]) == c && m_nLength > 0)
{
m_nLength--;
m_pBuffer [m_nLength] = '\0';
}
}
VOID SH_String::Chop()
{
if (m_nLength > 0)
{
--m_nLength;
m_pBuffer [m_nLength] = '\0';
}
}
VOID SH_String::Copy(const char * s)
{
Clear();
Cat(s);
}
VOID SH_String::Copy (const char * s,const size_t length)
{
Clear();
Cat(s, length);
}
BOOL SH_String::CopyToLower (SH_String * dest)
{
const size_t length = m_nLength;
const char * s = m_pBuffer;
char *d;
size_t i;
if(dest == NULL)
return FALSE;
if (dest->GetSize() < m_nSize)
Resize (m_nSize);
d = GetString();
for (i = 0 ; i < length ; ++i)
{
int c = s [i];
d[i] = tolower(c);
}
d[i] = '\0';
return TRUE;
}
VOID SH_String::SetLength()
{
m_nLength = strlen (m_pBuffer);
}
size_t SH_String::GetSize()
{
return m_nSize;
}
size_t SH_String::GetLength()
{
return m_nLength;
}
char * SH_String::GetString()
{
return m_pBuffer;
}
VOID SH_String::Terminate()
{
Put('\0');
}
AFX_INLINE const SH_String & SH_String::operator=(const SH_String & vs)
{
Copy(vs.m_pBuffer);
return *this;
}
AFX_INLINE const SH_String & SH_String::operator=(const char * & vs)
{
Copy(vs);
return *this;
}
AFX_INLINE const SH_String & SH_String::operator+=(const int ch)
{
Put(ch);
return *this;
}
AFX_INLINE const SH_String & SH_String::operator+=(const char ch)
{
Put((int)ch);
return *this;
}
AFX_INLINE const SH_String & SH_String::operator+=(const char * lpsz)
{
Cat(lpsz);
return *this;
}
AFX_INLINE const SH_String & SH_String::operator+=(const SH_String & string)
{
Cat(string.m_pBuffer);
return *this;
}
AFX_INLINE SH_String::operator LPCTSTR ()
{
return (LPCTSTR)GetString();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -