📄 bytestring.cpp
字号:
// ByteString.cpp: implementation of the CByteString class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ByteString.h"
#include <memory.h>
#include <string.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CByteString::CByteString()
{
m_curLen = 0;
m_bufLen = 0;
m_pBuf = NULL;
}
CByteString::~CByteString()
{
if( m_pBuf!=NULL )delete[] m_pBuf;
}
CByteString::CByteString(BYTE ch)
{
m_bufLen = 256;
m_curLen = 1;
m_pBuf = new BYTE[m_bufLen];
if( m_pBuf==NULL )
{
m_bufLen = 0;
m_curLen = 0;
return;
}
*m_pBuf = ch;
}
CByteString::CByteString(BYTE *pBuf, int nLen)
{
if( pBuf==NULL || nLen<=0 )return;
m_bufLen = (((nLen>>8)+1)<<8);
m_curLen = nLen;
if( m_bufLen>0 )
{
m_pBuf = new BYTE[m_bufLen];
if( m_pBuf==NULL )
{
m_bufLen = 0;
m_curLen = 0;
return;
}
memcpy(m_pBuf,pBuf,nLen);
}
}
CByteString::CByteString(const CByteString& str)
{
BYTE *pBuf = str.m_pBuf;
if( str.m_bufLen>0 )
{
m_pBuf = new BYTE[str.m_bufLen];
if( m_pBuf==NULL )
{
m_bufLen = 0;
m_curLen = 0;
return;
}
memcpy(m_pBuf,pBuf,str.m_curLen);
}
else m_pBuf=NULL;
m_bufLen = str.m_bufLen;
m_curLen = str.m_curLen;
}
void CByteString::Empty()
{
m_curLen = 0;
}
//////////////////////////////////////////////////////////////////////////////
// concatenate in place
bool CByteString::Append(BYTE ch)
{
if( m_curLen+1>m_bufLen )
{
int bufLen = ((((m_curLen+1)>>8)+1)<<8);
BYTE* pNewBuf = new BYTE[bufLen];
if( pNewBuf==NULL )return false;
if( m_pBuf!=NULL )
{
memcpy(pNewBuf,m_pBuf,m_curLen);
delete[] m_pBuf;
}
m_pBuf = pNewBuf;
m_bufLen = bufLen;
}
m_pBuf[m_curLen]=ch;
m_curLen++;
return true;
}
bool CByteString::Append(BYTE* pBuf,int nLen)
{
// concatenating an empty string is a no-op!
if( nLen<=0 )return true;
if( m_curLen+nLen>m_bufLen )
{
int bufLen = ((((m_curLen+nLen)>>8)+1)<<8);
BYTE* pNewBuf = new BYTE[bufLen];
if( pNewBuf==NULL )return false;
if( m_pBuf!=NULL )
{
memcpy(pNewBuf,m_pBuf,m_curLen);
delete[] m_pBuf;
}
m_pBuf = pNewBuf;
m_bufLen = bufLen;
}
memcpy(m_pBuf+m_curLen,pBuf,nLen);
m_curLen += nLen;
return true;
}
int CByteString::SetSize(int nLen)
{
if( nLen<=0 )nLen = 0;
int newLen = (((nLen>>8)+1)<<8);
BYTE *pNewBuf = new BYTE[newLen];
if( pNewBuf==NULL )return m_bufLen;
if( m_curLen>nLen )m_curLen = nLen;
m_bufLen = newLen;
if( m_pBuf!=NULL )
{
memcpy( pNewBuf, m_pBuf, m_curLen);
delete[] m_pBuf;
}
m_pBuf = pNewBuf;
return nLen;
}
///////////////////////////////////////////////////////////////////////////////
// Advanced direct buffer access
BYTE* CByteString::GetBuffer()
{
// return a pointer to the character storage for this string
return m_pBuf;
}
int CByteString::GetLength()
{
return m_curLen;
}
///////////////////////////////////////////////////////////////////////////////
// Commonly used routines (rarely used routines in STREX.CPP)
int CByteString::Find(BYTE ch) const
{
BYTE* pBuf=m_pBuf, *pMax=m_pBuf+m_curLen;
while(pBuf<pMax)
{
if( *pBuf==ch )return (pBuf-m_pBuf);
pBuf++;
}
return -1;
}
void CByteString::SetAt(int nIdx, BYTE ch)
{
if( nIdx<m_curLen && nIdx>=0 )m_pBuf[nIdx] = ch;
}
//////////////////////////////////////////////////////////////////////////////
// Assignment operators
const CByteString& CByteString::operator=(BYTE ch)
{
if( m_bufLen>=1 )
{
m_curLen = 1;
*m_pBuf = ch;
}
else
{
int newLen = 256;
BYTE* pBuf = new BYTE[newLen];
if( pBuf!=NULL )
{
*pBuf = ch;
if( m_pBuf!=NULL )delete[] m_pBuf;
m_pBuf = pBuf;
m_bufLen = newLen;
m_curLen = 1;
}
}
return *this;
}
const CByteString& CByteString::operator=(const CByteString& str)
{
if( str.m_curLen>0 )
{
if( m_bufLen<str.m_curLen )
{
BYTE* pNewBuf = new BYTE[str.m_bufLen];
if( pNewBuf==NULL )return *this;
if( str.m_curLen>0 )memcpy(pNewBuf,str.m_pBuf,str.m_curLen);
if( m_pBuf!=NULL )delete[] m_pBuf;
m_pBuf = pNewBuf;
m_bufLen = str.m_bufLen;
m_curLen = str.m_curLen;
}
else
{
memcpy(m_pBuf,str.m_pBuf,str.m_curLen);
m_curLen = str.m_curLen;
}
}
else
{
Empty();
}
return *this;
}
BYTE& CByteString::operator[](int nIdx)
{
if( nIdx<m_curLen && nIdx>=0 )return m_pBuf[nIdx];
#pragma warning(disable:4172)
BYTE tmp;
return tmp;
#pragma warning(default:4172)
}
//////////////////////////////////////////////////////////////////////////////
// less common string expressions
CByteString operator+(const CByteString& str1,const CByteString& str2)
{
CByteString s(str1);
s.Append(str2.m_pBuf, str2.m_curLen );
return s;
}
CByteString operator+(const CByteString& str, BYTE ch)
{
CByteString s(str);
s.Append(ch);
return s;
}
CByteString operator+(BYTE ch, const CByteString& str)
{
CByteString s(ch);
s = s+str;
return s;
}
//////////////////////////////////////////////////////////////////////////////
// Advanced manipulation
int CByteString::Delete(int nIdx, int nCount /* = 1 */)
{
if( nIdx<0 || nIdx>=m_curLen )return -1;
if( nIdx+nCount<m_bufLen )
{
memmove(m_pBuf+nIdx, m_pBuf+nIdx+nCount,m_bufLen-(nIdx+nCount) );
}
m_curLen -= nCount;
return m_curLen;
}
int CByteString::Insert(int nIdx, BYTE ch)
{
return Insert(nIdx, &ch, 1);
}
int CByteString::Insert(int nIdx, BYTE*pBuf, int nLen)
{
if (nIdx < 0)nIdx = 0;
if( m_curLen+nLen>m_bufLen )
{
int bufLen = ((((m_curLen+nLen)>>8)+1)<<8);
BYTE* pNewBuf = new BYTE[bufLen];
if( pNewBuf==NULL )return -1;
memcpy(pNewBuf,m_pBuf,nIdx);
memcpy(pNewBuf+nIdx,pBuf,nLen);
memcpy(pNewBuf+nIdx+nLen,m_pBuf+nIdx,m_curLen-nIdx);
m_bufLen = bufLen;
}
else
{
memmove(m_pBuf+nIdx+nLen,m_pBuf+nIdx,m_curLen-nIdx);
memcpy(m_pBuf+nIdx,pBuf,nLen);
}
m_curLen += nLen;
return m_bufLen;
}
void CByteString::Decrease()
{
if( m_curLen>0 )m_curLen--;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -