2026-27-cpp-homework/feladat5/feladat5/CMyString.cpp
Damimani 6ca06962d9 v3
2026-11-14 16:07:04 +01:00

256 lines
No EOL
4.9 KiB
C++

// Tóth Dániel Mátyás | IFPQYY | v3
#include "CMyString.h"
#include "CMyStringException.h"
#include <iostream>
static int strLength(const char* str) {
if (str == nullptr)
{
return -1;
}
int cnt = 0;
while (str[cnt])
{
cnt++;
}
return cnt;
}
static void CopyStr(char* dest, const char* src, size_t length) {
if (dest == nullptr || src == nullptr) {
return;
}
size_t i;
for (i = 0; i < length; i++)
{
dest[i] = src[i];
}
dest[i] = '\0';
}
static void MakeEmptyStr(size_t* datalngth, size_t* alloclngth, char** ptr) {
*datalngth = 0;
*alloclngth = 1;
*ptr = new char[1];
(*ptr)[0] = '\0';
}
static void reserveNoCheck(size_t* alloc, size_t* size, char** data, size_t newsize) {
char* tmp = *data;
*alloc = newsize;
*data = new char[*alloc];
CopyStr(*data, tmp, *size);
delete tmp;
}
CMyString::CMyString() {
MakeEmptyStr(&m_nDataLength, &m_nAllocLength, &m_pchData);
}
CMyString::CMyString(const char* psz) {
if (psz == nullptr)
{
MakeEmptyStr(&m_nDataLength, &m_nAllocLength, &m_pchData);
}
else
{
m_nDataLength = 0;
while (psz[m_nDataLength])
{
m_nDataLength++;
}
m_nAllocLength = m_nDataLength + 1;
m_pchData = new char[m_nAllocLength];
CopyStr(m_pchData, psz, m_nDataLength);
}
}
CMyString::CMyString(char ch, size_t repeat) {
if (ch == '\0')
{
throw CMyStringException(CMyStringException::ErrInvalidChar);
}
if (repeat == 0)
{
throw CMyStringException(CMyStringException::ErrCount);
}
else
{
m_nDataLength = 0;
m_nAllocLength = repeat + 1;
m_pchData = new char[m_nAllocLength];
while (m_nDataLength < repeat)
{
m_pchData[m_nDataLength++] = ch;
}
m_pchData[m_nAllocLength-1] = '\0';
}
}
CMyString::CMyString(const CMyString& str) {
if (str.empty())
{
MakeEmptyStr(&m_nDataLength, &m_nAllocLength, &m_pchData);
}
else
{
m_nDataLength = str.size();
m_nAllocLength = m_nDataLength + 1;
m_pchData = new char[m_nAllocLength];
int i = 0;
const char* cont = str.c_str();
while (cont[i])
{
m_pchData[i] = cont[i];
i++;
}
m_pchData[m_nDataLength] = '\0';
}
}
CMyString::~CMyString() {
delete m_pchData;
}
size_t CMyString::size() const {
return m_nDataLength;
}
size_t CMyString::capacity() const {
return m_nAllocLength;
}
const char* CMyString::c_str() const {
return m_pchData;
}
void CMyString::clear() {
m_nDataLength = 0;
m_pchData[0] = '\0';
}
char CMyString::getat(size_t index) const {
if (index >= m_nDataLength)
{
throw CMyStringException(CMyStringException::ErrOutOfRange);
}
return m_pchData[index];
}
void CMyString::setat(size_t index, char ch) {
if (index >= m_nDataLength)
{
throw CMyStringException(CMyStringException::ErrOutOfRange);
}
if (ch == '\0')
{
throw CMyStringException(CMyStringException::ErrInvalidChar);
}
m_pchData[index] = ch;
}
void CMyString::reserve(size_t newcapacity) {
if (newcapacity <= m_nAllocLength)
{
return;
}
reserveNoCheck(&m_nAllocLength, &m_nDataLength, &m_pchData, newcapacity);
}
void CMyString::erase(size_t pos, size_t count) {
if (pos > m_nDataLength)
{
throw CMyStringException(CMyStringException::ErrOutOfRange);
}
if (empty())
{
return;
}
if (pos + count > m_nDataLength)
{
count = m_nDataLength - pos;
}
size_t i;
for (i = 0; i < count; i++)
{
m_pchData[pos + i] = ' ';
}
for (size_t j = pos; j < m_nDataLength; j++)
{
if (!m_pchData[j + i])
{
m_pchData[j] = '\0';
m_nDataLength = j;
break;
}
m_pchData[j] = m_pchData[j + i];
}
}
bool CMyString::empty() const {
return m_nDataLength == 0;
}
void CMyString::display() const {
if (!empty())
{
cout << m_pchData;
}
}
void CMyString::shrink_to_fit() {
reserveNoCheck(&m_nAllocLength, &m_nDataLength, &m_pchData, m_nDataLength+1);
}
void CMyString::reverse() {
if (!empty())
{
for (size_t i = 0; i < m_nDataLength / 2; i++)
{
size_t j = m_nDataLength - i - 1;
char tmp = m_pchData[i];
m_pchData[i] = m_pchData[j];
m_pchData[j] = tmp;
}
}
}
void CMyString::append(const char* psz, unsigned offset, unsigned count) {
if (psz == nullptr || psz == m_pchData)
{
return;
}
if (count == 0)
{
throw CMyStringException(CMyStringException::ErrCount);
}
size_t len = strLength(psz);
if (offset == len)
{
return;
}
if (count == ~0)
{
count = len - offset;
}
int range = count + offset;
if (len < count || len < offset || len < range)
{
throw CMyStringException(CMyStringException::ErrOutOfRange);
}
if (count + m_nDataLength >= m_nAllocLength)
{
this->reserve((m_nDataLength + count + 1)*2);
}
size_t i = m_nDataLength;
size_t j = offset;
while (j < range)
{
m_pchData[i++] = psz[j++];
}
m_nDataLength = i;
m_pchData[i] = '\0';
}
CMyString& CMyString::operator=(const CMyString& str) {
if (this != &str)
{
if (!str.empty())
{
if (str.size() >= m_nAllocLength)
{
this->reserve((str.size() + 1));
}
m_nDataLength = str.size();
CopyStr(m_pchData, str.c_str(), m_nDataLength);
}
else
{
MakeEmptyStr(&m_nDataLength, &m_nAllocLength, &m_pchData);
}
}
return *this;
}