Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 09 / StringPool / MyString / MyString.cpp
diff options
context:
space:
mode:
Commit7da5cae134b3f5dc0e3bc6641f2ae5364fa5f512 (patch)
Author
 angeld55 <angel_dimitriev@abv.bg>
2022-05-24 00:13:26 +0300
Committer
 angeld55 <angel_dimitriev@abv.bg>
2022-05-24 00:13:26 +0300
Files1d2b86be84b35fd6bb2bc8bde340633b6e6997aa /Sem. 09/StringPool/MyString/MyString.cpp
Object-oriented_programming_FMI-7da5cae134b3f5dc0e3bc6641f2ae5364fa5f512.tar
Object-oriented_programming_FMI-7da5cae134b3f5dc0e3bc6641f2ae5364fa5f512.tar.gz
Object-oriented_programming_FMI-7da5cae134b3f5dc0e3bc6641f2ae5364fa5f512.zip
Parent2b5e881d9b4e6f421521ef0a6334fc107d29bda0 (diff)
added check to MyString for length when input comes for a stream
1 files changed, 34 insertions, 11 deletions
-rw-r--r--Sem. 09/StringPool/MyString/MyString.cpp45
diff --git a/Sem. 09/StringPool/MyString/MyString.cpp b/Sem. 09/StringPool/MyString/MyString.cpp
index cad878a..ca5f738 100644
--- a/Sem. 09/StringPool/MyString/MyString.cpp
+++ b/Sem. 09/StringPool/MyString/MyString.cpp
@@ -1,6 +1,7 @@
#include "MyString.h"
#include <cstring>
#include <iostream>
+#include <iomanip>
void MyString::copyFrom(const MyString& other)
{
@@ -13,13 +14,6 @@ void MyString::free()
delete[] str;
}
-MyString::MyString()
-{
- str = new char[1];
- str[0] = '\0';
- size = 0;
-}
-
MyString::MyString(const char* data)
{
if(data == nullptr)
@@ -36,6 +30,35 @@ MyString::MyString(const char* data)
}
}
+size_t getNumSize(size_t num)
+{
+ size_t res = 0;
+ while (num)
+ {
+ res++;
+ num /= 10;
+ }
+ return res;
+}
+MyString::MyString(size_t n)
+{
+ int numSize = getNumSize(n);
+ str = new char[numSize + 1];
+
+ str[numSize] = '\0';
+
+ for (size_t i = 0; i < numSize; i++, n /= 10)
+ str[numSize - 1 - i] = (n % 10) + '0';
+ size = numSize;
+}
+
+MyString::MyString()
+{
+ str = new char[1];
+ str[0] = '\0';
+ size = 1;
+}
+
MyString::MyString(const MyString& other)
{
copyFrom(other);
@@ -59,7 +82,7 @@ size_t MyString::getSize() const
{
return size;
}
-void MyString::concat(const MyString& other)
+void MyString::concatFrom(const MyString& other)
{
char* temp = new char[getSize() + other.getSize() + 1];
strcpy(temp, str);
@@ -77,7 +100,7 @@ const char* MyString::c_str() const
MyString& MyString::operator+=(const MyString& other)
{
- concat(other);
+ concatFrom(other);
return *this;
}
@@ -98,7 +121,7 @@ std::istream& operator>>(std::istream& stream, MyString& str)
{
delete[] str.str;
char buff[1024];
- stream >> buff;
+ stream >> std::setw(1024) >> buff;
str.size = strlen(buff);
str.str = new char[str.size + 1];
@@ -127,11 +150,11 @@ MyString::MyString(MyString&& otherString)
str = otherString.str;
size = otherString.size;
otherString.str = nullptr;
- std::cout << "move constr" << std::endl;
}
MyString& MyString::operator=(MyString&& otherString)
{
+
if (this != &otherString)
{
free();