feladat6 v1 (no test)
This commit is contained in:
parent
6ca06962d9
commit
49d964d8f1
7 changed files with 291 additions and 19 deletions
|
|
@ -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 <CMyString.h>
|
||||
|
|
@ -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++)
|
||||
|
|
|
|||
|
|
@ -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 <iostream>
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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 <ostream>
|
||||
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;
|
||||
};
|
||||
#ifdef _MYDEBUG
|
||||
public:
|
||||
static void clearobjcount();
|
||||
static unsigned objcount();
|
||||
private:
|
||||
static unsigned m_iCounter;
|
||||
#endif // _MYDEBUG
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, CMyString& s);
|
||||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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 <memory>
|
||||
#include "CMyString.h"
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue