From 49d964d8f14ef470e0d311b1f6f744fc7712c79c Mon Sep 17 00:00:00 2001 From: Damimani <41192030+Damimani@users.noreply.github.com> Date: Sun, 15 Nov 2026 14:31:05 +0100 Subject: [PATCH] feladat6 v1 (no test) --- .github/copilot-instructions.md | 4 + feladat5/UnitTest1/UnitTest1.cpp | 6 +- feladat5/feladat5/CMyString.cpp | 267 +++++++++++++++++++++-- feladat5/feladat5/CMyString.h | 27 ++- feladat5/feladat5/CMyStringException.cpp | 2 +- feladat5/feladat5/CMyStringException.h | 2 +- feladat5/feladat5/main.cpp | 2 +- 7 files changed, 291 insertions(+), 19 deletions(-) create mode 100644 .github/copilot-instructions.md diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md new file mode 100644 index 0000000..1ca2674 --- /dev/null +++ b/.github/copilot-instructions.md @@ -0,0 +1,4 @@ +# Copilot Instructions + +## Project Guidelines +- The user requires CMyString.h to remain unchanged when fixing the implementation. \ No newline at end of file diff --git a/feladat5/UnitTest1/UnitTest1.cpp b/feladat5/UnitTest1/UnitTest1.cpp index 4ce6dcc..16646c7 100644 --- a/feladat5/UnitTest1/UnitTest1.cpp +++ b/feladat5/UnitTest1/UnitTest1.cpp @@ -1,4 +1,4 @@ -// Tóth Dániel Mátyás | IFPQYY | v3 +// Tóth Dániel Mátyás | IFPQYY | v1 #include "pch.h" #include "CppUnitTest.h" #include @@ -385,6 +385,7 @@ namespace UnitTest1 CMyString('a', 10), CMyString("alma"), CMyString("test"), + CMyString("alma"), }; int pos[] = { 0, @@ -395,6 +396,7 @@ namespace UnitTest1 3, 4, 0, + 1, }; int cnt[] = { 1, @@ -405,6 +407,7 @@ namespace UnitTest1 1, 100, 0, + -1, }; char* text[] = { "", @@ -415,6 +418,7 @@ namespace UnitTest1 "aaaaaaaaa", "alma", "test", + "a" }; int testcount = sizeof(pos) / sizeof(int); for (size_t i = 0; i < testcount; i++) diff --git a/feladat5/feladat5/CMyString.cpp b/feladat5/feladat5/CMyString.cpp index 92aa8e0..372134b 100644 --- a/feladat5/feladat5/CMyString.cpp +++ b/feladat5/feladat5/CMyString.cpp @@ -1,4 +1,4 @@ -// Tóth Dániel Mátyás | IFPQYY | v3 +// Tóth Dániel Mátyás | IFPQYY | v1 #include "CMyString.h" #include "CMyStringException.h" #include @@ -32,7 +32,16 @@ static void CopyStr(char* dest, const char* src, size_t length) { static void MakeEmptyStr(size_t* datalngth, size_t* alloclngth, char** ptr) { *datalngth = 0; *alloclngth = 1; + char* cpy = nullptr; + if (ptr != nullptr) + { + cpy = *ptr; + } *ptr = new char[1]; + if (cpy != nullptr) + { + delete[] cpy; + } (*ptr)[0] = '\0'; } @@ -41,11 +50,53 @@ static void reserveNoCheck(size_t* alloc, size_t* size, char** data, size_t news *alloc = newsize; *data = new char[*alloc]; CopyStr(*data, tmp, *size); - delete tmp; + delete[] tmp; +} +static void insertStrAtIndex(const char* src, int pos, int delcnt, size_t* alloc, size_t* size, char** data) { + size_t dlngth = *size; + size_t alngth = *alloc; + char* old = new char[*alloc]; + size_t srclen = 0; + if (src != "") + { + srclen = strLength(src); + } + CopyStr(old, *data, *size); + try + { + char* cpy = new char[int(*size + srclen + 1)]; + CopyStr(cpy, *data, pos); + if (srclen != 0) + { + CopyStr(cpy + pos, src, srclen); + } + CopyStr(cpy + pos + srclen, *data + pos + delcnt, *size - pos); + *size = strLength(cpy); + if (*alloc <= *size) + { + reserveNoCheck(alloc, size, data, ((*size + 1) * 2)); + } + *data = cpy; + delete[] old; + } + catch (const bad_alloc e) + { + *size = dlngth; + *alloc = alngth; + *data = old; + throw e; + } + catch (...) { + throw CMyStringException(CMyStringException::ErrUnexpected); + } } CMyString::CMyString() { MakeEmptyStr(&m_nDataLength, &m_nAllocLength, &m_pchData); +#ifdef _MYDEBUG + m_iCounter++; +#endif // _MYDEBUG + } CMyString::CMyString(const char* psz) { if (psz == nullptr) @@ -63,6 +114,9 @@ CMyString::CMyString(const char* psz) { m_pchData = new char[m_nAllocLength]; CopyStr(m_pchData, psz, m_nDataLength); } +#ifdef _MYDEBUG + m_iCounter++; +#endif // _MYDEBUG } CMyString::CMyString(char ch, size_t repeat) { if (ch == '\0') @@ -73,17 +127,17 @@ CMyString::CMyString(char ch, size_t repeat) { { throw CMyStringException(CMyStringException::ErrCount); } - else + m_nDataLength = 0; + m_nAllocLength = repeat + 1; + m_pchData = new char[m_nAllocLength]; + while (m_nDataLength < repeat) { - 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'; + m_pchData[m_nDataLength++] = ch; } + m_pchData[m_nAllocLength-1] = '\0'; +#ifdef _MYDEBUG + m_iCounter++; +#endif // _MYDEBUG } CMyString::CMyString(const CMyString& str) { if (str.empty()) @@ -104,9 +158,15 @@ CMyString::CMyString(const CMyString& str) { } m_pchData[m_nDataLength] = '\0'; } +#ifdef _MYDEBUG + m_iCounter++; +#endif // _MYDEBUG } CMyString::~CMyString() { - delete m_pchData; + delete[] m_pchData; +#ifdef _MYDEBUG + m_iCounter--; +#endif // _MYDEBUG } size_t CMyString::size() const { return m_nDataLength; @@ -155,6 +215,10 @@ void CMyString::erase(size_t pos, size_t count) { { return; } + if (count > m_nDataLength) + { + count = m_nDataLength; + } if (pos + count > m_nDataLength) { count = m_nDataLength - pos; @@ -253,4 +317,181 @@ CMyString& CMyString::operator=(const CMyString& str) { } } return *this; -} \ No newline at end of file +} + +CMyString::CMyString(CMyString&& str){ + m_nDataLength = str.m_nDataLength; + m_nAllocLength = str.m_nAllocLength; + m_pchData = str.m_pchData; + str = CMyString(); +} +CMyString& CMyString::operator=(CMyString&& str){ + m_nDataLength = str.m_nDataLength; + m_nAllocLength = str.m_nAllocLength; + char* old = m_pchData; + m_pchData = str.m_pchData; + delete[] old; + str = CMyString(); + return *this; +} +char& CMyString::operator[](size_t nIndex){ + if (nIndex >= m_nDataLength) + { + throw CMyStringException(CMyStringException::ErrOutOfRange); + } + return m_pchData[nIndex]; +} +bool CMyString::operator==(const CMyString& s1){ + if (s1.c_str() != c_str()) + { + return false; + } + if (s1.capacity() != capacity()) + { + return false; + } + if (s1.size() != size()) + { + return false; + } + return true; +} +CMyString& CMyString::operator+=(const CMyString& str){ + append(str.c_str()); + return *this; +} +CMyString& CMyString::operator+=(const char* p){ + append(p); + return *this; +} +CMyString& CMyString::operator+(const CMyString& str1){ + static CMyString res = *this; + res.append(str1.c_str()); + return res; +} +CMyString::operator const char* (){ + return m_pchData; +} +CMyString CMyString::substring(size_t pos, size_t len){ + if (pos > m_nDataLength) + { + throw CMyStringException(CMyStringException::ErrOutOfRange); + } + if (pos == m_nDataLength || len == 0) + { + return ""; + } + if (len > m_nDataLength) + { + len = m_nDataLength; + } + if ((len + pos) > m_nDataLength) + { + len = m_nDataLength - pos; + } + char* cpy = new char[len + 1]; + CopyStr(cpy, c_str() + pos, len); + CMyString substr = CMyString(cpy); + delete[] cpy; + return substr; +} +void CMyString::swap(CMyString& str1){ + char* tempData = m_pchData; + size_t tempLength = m_nDataLength; + size_t tempAllocLength = m_nAllocLength; + + m_pchData = str1.m_pchData; + m_nDataLength = str1.m_nDataLength; + m_nAllocLength = str1.m_nAllocLength; + + str1.m_pchData = tempData; + str1.m_nDataLength = tempLength; + str1.m_nAllocLength = tempAllocLength; +} +int CMyString::find(const char* psz) const{ + if (psz == nullptr || psz == "") + { + return -1; + } + int pszlen = strLength(psz); + if (pszlen > m_nDataLength) + { + return -1; + } + size_t i = 0; + size_t j = 0; + size_t ret = -1; + while (pszlen + i <= m_nDataLength && j < pszlen) + { + if (getat(i) == psz[j]) + { + ret = ret == -1 ? i : ret; + j++; + } + else + { + ret = -1; + j = 0; + } + i++; + } + return ret; +} +void CMyString::insert(size_t pos, const char* psz){ + if (pos > m_nDataLength) + { + throw CMyStringException(CMyStringException::ErrOutOfRange); + } + if (psz == nullptr || psz == "") + { + return; + } + if (pos == m_nDataLength) + { + append(psz); + } + else + { + insertStrAtIndex(psz, pos, 0, &m_nAllocLength, &m_nDataLength, &m_pchData); + } +} +void CMyString::replace(size_t pos, size_t count, const char* psz){ + if (pos > m_nDataLength) + { + throw CMyStringException(CMyStringException::ErrOutOfRange); + } + if (count > m_nDataLength) + { + count = m_nDataLength; + } + if ((count + pos) > m_nDataLength) + { + count = m_nDataLength - pos; + } + if (psz == nullptr) + { + psz = ""; + } + if (pos == m_nDataLength) + { + append(psz); + } + else + { + insertStrAtIndex(psz, pos, count, &m_nAllocLength, &m_nDataLength, &m_pchData); + } +} + +std::ostream& operator<<(std::ostream& os, CMyString& s) { + os << s.c_str(); + return os; +} + +#ifdef _MYDEBUG +void CMyString::clearobjcount() { + m_iCounter = 0; +} +unsigned CMyString::objcount() { + return m_iCounter; +} +#endif // _MYDEBUG diff --git a/feladat5/feladat5/CMyString.h b/feladat5/feladat5/CMyString.h index 11ceac5..a9e0d57 100644 --- a/feladat5/feladat5/CMyString.h +++ b/feladat5/feladat5/CMyString.h @@ -1,5 +1,6 @@ -// Tóth Dániel Mátyás | IFPQYY | v3 +// Tóth Dániel Mátyás | IFPQYY | v1 #pragma once +#include class CMyString { public: CMyString(); @@ -21,8 +22,30 @@ public: void reverse(); void append(const char* psz, unsigned offset = 0, unsigned count = ~0); CMyString& operator=(const CMyString& str); + CMyString(CMyString&& str); + CMyString& operator=(CMyString&& str); + char& operator[](size_t nIndex); + bool operator==(const CMyString& s1); + CMyString& operator+=(const CMyString& str); + CMyString& operator+=(const char* p); + CMyString& operator+(const CMyString& str1); + operator const char* (); + CMyString substring(size_t pos, size_t len = (~0)); + void swap(CMyString& str1); + int find(const char* psz) const; + void insert(size_t pos, const char* psz); + void replace(size_t pos, size_t count, const char* psz); private: char* m_pchData; size_t m_nDataLength; size_t m_nAllocLength; -}; \ No newline at end of file +#ifdef _MYDEBUG +public: + static void clearobjcount(); + static unsigned objcount(); +private: + static unsigned m_iCounter; +#endif // _MYDEBUG +}; + +std::ostream& operator<<(std::ostream& os, CMyString& s); \ No newline at end of file diff --git a/feladat5/feladat5/CMyStringException.cpp b/feladat5/feladat5/CMyStringException.cpp index 222c3d2..7f0d4a2 100644 --- a/feladat5/feladat5/CMyStringException.cpp +++ b/feladat5/feladat5/CMyStringException.cpp @@ -1,4 +1,4 @@ -// Tóth Dániel Mátyás | IFPQYY | v3 +// Tóth Dániel Mátyás | IFPQYY | v1 #include "CMyStringException.h" #include "CMyString.h" diff --git a/feladat5/feladat5/CMyStringException.h b/feladat5/feladat5/CMyStringException.h index fcb0761..77db8df 100644 --- a/feladat5/feladat5/CMyStringException.h +++ b/feladat5/feladat5/CMyStringException.h @@ -1,4 +1,4 @@ -// Tóth Dániel Mátyás | IFPQYY | v3 +// Tóth Dániel Mátyás | IFPQYY | v1 #pragma once #include #include "CMyString.h" diff --git a/feladat5/feladat5/main.cpp b/feladat5/feladat5/main.cpp index f68ceaa..f93f701 100644 --- a/feladat5/feladat5/main.cpp +++ b/feladat5/feladat5/main.cpp @@ -1,4 +1,4 @@ -// Tóth Dániel Mátyás | IFPQYY | v3 +// Tóth Dániel Mátyás | IFPQYY | v1 #include "CMyString.h" int main() {