Test
This commit is contained in:
parent
6dd93155cc
commit
6e22717a81
8 changed files with 576 additions and 0 deletions
229
LSString/LSString.cpp
Normal file
229
LSString/LSString.cpp
Normal file
|
@ -0,0 +1,229 @@
|
|||
#include "LSString.h"
|
||||
#include <memory>
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
LSString::LSString(const char* pData)
|
||||
: m_uiLength(StringLength(pData))
|
||||
, m_uiCapacity(StringLength(pData))
|
||||
{
|
||||
if (!pData) return;
|
||||
|
||||
m_pData = new char[m_uiLength + 1];
|
||||
memset(m_pData, 0, m_uiLength + 1);
|
||||
memcpy(m_pData, pData, m_uiLength);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
LSString::LSString(const std::string& sRhs)
|
||||
: m_pData(new char[sRhs.length()])
|
||||
, m_uiLength(sRhs.length())
|
||||
, m_uiCapacity(sRhs.length())
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
LSString::LSString(const LSString& sRhs)
|
||||
: m_uiLength(sRhs.m_uiLength)
|
||||
, m_pData(new char[sRhs.m_uiLength + 1])
|
||||
, m_uiCapacity(sRhs.m_uiCapacity)
|
||||
{
|
||||
memset(m_pData + m_uiLength, 0, 1);
|
||||
memcpy(m_pData, sRhs.m_pData, m_uiLength);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
LSString::~LSString()
|
||||
{
|
||||
DELETE_ARRAY(m_pData);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
void LSString::Insert(const LSString& sRhs, size_t uiPos)
|
||||
{
|
||||
Insert(sRhs.m_pData, uiPos);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
void LSString::Insert(const std::string& sRhs, size_t uiPos)
|
||||
{
|
||||
Insert(sRhs.c_str(), uiPos);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
void LSString::Insert(const char* pData, size_t uiPos)
|
||||
{
|
||||
if (!pData) return;
|
||||
|
||||
size_t uilength = StringLength(pData);
|
||||
size_t uiNewLength = m_uiLength + uilength;
|
||||
|
||||
while (uiNewLength > m_uiCapacity)
|
||||
{
|
||||
IncreaseCapacity(uiNewLength);
|
||||
}
|
||||
|
||||
char* pLeft = m_pData + uiPos;
|
||||
char* pRight = m_pData + uiPos + uilength;
|
||||
|
||||
memcpy(pRight, m_pData + uiPos, m_uiLength - uiPos);
|
||||
memcpy(pLeft, pData, uilength);
|
||||
|
||||
m_uiLength = uiNewLength;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
void LSString::Append(const LSString& sRhs)
|
||||
{
|
||||
Insert(sRhs, sRhs.Length());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
void LSString::Append(const std::string& sRhs)
|
||||
{
|
||||
Insert(sRhs, sRhs.length());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
void LSString::Append(const char* pData)
|
||||
{
|
||||
Insert(pData, StringLength(pData));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
bool LSString::Remove(const LSString& sRhs)
|
||||
{
|
||||
return RemoveAt(Find(sRhs), sRhs.m_uiLength);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
bool LSString::Remove(const std::string& sRhs)
|
||||
{
|
||||
return RemoveAt(Find(sRhs), sRhs.length());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
bool LSString::Remove(const char* pData)
|
||||
{
|
||||
return RemoveAt(Find(pData), StringLength(pData));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
bool LSString::RemoveAt(size_t uiPos, size_t uiSize)
|
||||
{
|
||||
if (uiPos > m_uiLength) return false;
|
||||
|
||||
m_uiLength = m_uiLength - uiSize;
|
||||
char* pLast = m_pData + uiPos + uiSize;
|
||||
memcpy(m_pData + uiPos, pLast, uiSize);
|
||||
memset(pLast, 0, uiSize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
size_t LSString::StringLength(const char* pChar) const
|
||||
{
|
||||
if (!pChar) return 0;
|
||||
int iRet = 0;
|
||||
while (*pChar != 0x0)
|
||||
{
|
||||
pChar++;
|
||||
iRet++;
|
||||
}
|
||||
return iRet;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
bool LSString::Contains(const LSString& sRhs) const
|
||||
{
|
||||
return Contains(sRhs.m_pData);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
bool LSString::Contains(const std::string& sRhs) const
|
||||
{
|
||||
return Contains(sRhs.c_str());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
bool LSString::Contains(const char* pData) const
|
||||
{
|
||||
int iRet = 1;
|
||||
size_t uiLength = StringLength(pData);
|
||||
size_t uiOffset = 0;
|
||||
|
||||
while (iRet != 0)
|
||||
{
|
||||
if (uiLength > m_uiLength || uiOffset > m_uiLength) break;
|
||||
iRet = memcmp(m_pData + uiOffset, pData, uiLength);
|
||||
uiOffset++;
|
||||
}
|
||||
|
||||
return iRet == 0;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
size_t LSString::Find(const LSString& sRhs, size_t uiOffset) const
|
||||
{
|
||||
return Find(sRhs.m_pData, uiOffset);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
size_t LSString::Find(const std::string& sRhs, size_t uiOffset) const
|
||||
{
|
||||
return Find(sRhs.c_str(), uiOffset);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
size_t LSString::Find(const char* pData, size_t uiOffset) const
|
||||
{
|
||||
size_t uiLength = StringLength(pData);
|
||||
size_t uiCurrentOffset = uiOffset;
|
||||
|
||||
for (int iRet = 1; iRet != 0; uiCurrentOffset++)
|
||||
{
|
||||
if (uiLength > m_uiLength || uiCurrentOffset > m_uiLength)
|
||||
{
|
||||
uiCurrentOffset = -1;
|
||||
break;
|
||||
}
|
||||
iRet = memcmp(m_pData + uiCurrentOffset, pData, uiLength);
|
||||
}
|
||||
|
||||
return uiCurrentOffset;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
char LSString::At(size_t uiPos) const
|
||||
{
|
||||
return *(m_pData + uiPos);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
size_t LSString::Length() const
|
||||
{
|
||||
return m_uiLength;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
std::string LSString::ToStdString() const
|
||||
{
|
||||
return std::string(m_pData);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
const char* LSString::ToCChar() const
|
||||
{
|
||||
return m_pData;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------
|
||||
void LSString::IncreaseCapacity(size_t uiNewLength)
|
||||
{
|
||||
m_uiCapacity = (m_uiCapacity + uiNewLength) * c_uiCapacityIncreaseFactor;
|
||||
char* pNew = new char[m_uiCapacity + 1];
|
||||
memset(pNew, 0, m_uiCapacity + 1);
|
||||
memcpy(pNew, m_pData, m_uiLength);
|
||||
DELETE_ARRAY(m_pData);
|
||||
m_pData = pNew;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue