Website Summary Files Commits Branches @commit @diff @stats
path: root / src / String
diff options
context:
space:
mode:
2 files changed, 47 insertions, 3 deletions
-rw-r--r--src/String/String.cpp49
-rw-r--r--src/String/String.h1
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;
}
diff --git a/src/String/String.h b/src/String/String.h
index acf3650..408e193 100644
--- a/src/String/String.h
+++ b/src/String/String.h
@@ -3,7 +3,6 @@
class String {
char* str;
- unsigned count;
unsigned length;
void free();