From 90ce8dc2317c7afc6bf6c7e842f92a31678a4994 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Mon, 25 Apr 2022 17:07:06 +0300 Subject: Autocommit at 25.04.2022 @ 17:07 --- src/String/String.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) (limited to 'src/String/String.cpp') diff --git a/src/String/String.cpp b/src/String/String.cpp index f94dce2..ee9c9df 100644 --- a/src/String/String.cpp +++ b/src/String/String.cpp @@ -8,6 +8,51 @@ void String::free() { } void String::copyFrom(const String& other) { - this->str = new char[other.length]; - strcpy(this->str, other.str); + str = new char[other.length]; + strcpy(str, other.str); + length = other.length; +} + +void String::moveFrom(String&& other) { + str = other.str; + other.str = nullptr; + length = other.length; +} + +/* Public */ + +String::String(const char* str) { + length = strlen(str) + 1; // capture terminating zero + this->str = new char[length]; + strcpy(this->str, str); +} + +String::String() : String("") {} + +String::String(const String& other) { + copyFrom(other); +} + +String& String::operator=(const String& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +String::~String() { + free(); +} + +String::String(String&& other) { + moveFrom(other); +} + +String& String::operator=(const String& other) { + if (this != &other) { + free(); + moveFrom(other); + } + return *this; } -- cgit v1.3.1-23-gd82d