From a119e6ca70ffab14f0a70908fa3eeb83b41bb5ab Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 14 Mar 2022 10:24:36 +0100 Subject: refactor: rename std to common --- src/common/string.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/common/string.cpp (limited to 'src/common/string.cpp') diff --git a/src/common/string.cpp b/src/common/string.cpp new file mode 100644 index 0000000..27b65d4 --- /dev/null +++ b/src/common/string.cpp @@ -0,0 +1,70 @@ +#include "string.hpp" + +#include "common/memory.hpp" + +#include + +namespace common +{ + +String::String(char *c_string) : c_str(c_string) +{ +} + +String::String(unsigned int size) : c_str(malloc_s(size + 1)) +{ +} + +String::String(const String &smart_str) + : c_str(malloc_s(strlen(smart_str.c_str) + 1)) +{ + memcpy(c_str, smart_str.c_str, strlen(smart_str.c_str) + 1); +} + +String::String(String &&smart_str) noexcept : c_str(smart_str.c_str) +{ + smart_str.c_str = nullptr; +} + +String &String::operator=(const String &smart_str) +{ + if (&smart_str != this) + { + free(c_str); + c_str = nullptr; + + auto str_size = strlen(smart_str.c_str) + 1; + + c_str = malloc_s(str_size); + memcpy(c_str, smart_str.c_str, str_size); + } + + return *this; +} + +String &String::operator=(String &&smart_str) noexcept +{ + if (&smart_str != this) + { + free(c_str); + c_str = smart_str.c_str; + smart_str.c_str = nullptr; + } + + return *this; +} + +String::~String() +{ + if (c_str != nullptr) + { + free(c_str); + } +} + +String::operator char *() const +{ + return c_str; +} + +} // namespace common -- cgit v1.2.3-18-g5258