diff options
Diffstat (limited to 'source/fud_string.cpp')
-rw-r--r-- | source/fud_string.cpp | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/source/fud_string.cpp b/source/fud_string.cpp index 97acb52..b10f6ee 100644 --- a/source/fud_string.cpp +++ b/source/fud_string.cpp @@ -95,14 +95,17 @@ String::String(String&& rhs) : m_length{rhs.m_length}, m_capacity{rhs.m_capacity String::~String() { - if (isLarge() && m_data != nullptr) { - fudFree(m_data); - m_data = nullptr; - } + cleanup(); } String& String::operator=(const String& rhs) { + if (this == &rhs) { + return *this; + } + + cleanup(); + m_length = rhs.m_length; m_capacity = rhs.m_capacity; if (rhs.valid()) { @@ -118,6 +121,8 @@ String& String::operator=(const String& rhs) String& String::operator=(String&& rhs) { + cleanup(); + m_length = rhs.m_length; m_capacity = rhs.m_capacity; if (rhs.isLarge()) { @@ -130,6 +135,14 @@ String& String::operator=(String&& rhs) return *this; } +void String::cleanup() +{ + if (isLarge() && m_data != nullptr) { + fudFree(m_data); + m_data = nullptr; + } +} + bool String::nullTerminated() const { return data() != nullptr && m_length < m_capacity && data()[m_length] == '\0'; |