From 0f678f845c39d849c710991f713fcd1b65ddd483 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 7 Apr 2023 12:14:21 +0200 Subject: [PATCH] String: Add operator= --- libluna/include/luna/String.h | 3 +++ libluna/src/String.cpp | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/libluna/include/luna/String.h b/libluna/include/luna/String.h index 7b22c4ea..de8dab1b 100644 --- a/libluna/include/luna/String.h +++ b/libluna/include/luna/String.h @@ -17,6 +17,9 @@ class String String(String&&); String(const String&) = delete; + String& operator=(String&&); + String& operator=(const String&) = delete; + ~String(); Result clone() const; diff --git a/libluna/src/String.cpp b/libluna/src/String.cpp index 3d0fc465..a9be76ca 100644 --- a/libluna/src/String.cpp +++ b/libluna/src/String.cpp @@ -23,6 +23,24 @@ String::String(String&& other) other.m_string = nullptr; } +String& String::operator=(String&& other) +{ + if (&other == this) return *this; + + if (!m_inline) free_impl(m_string); + + m_inline = other.m_inline; + + m_string = other.m_string; + m_length = other.m_length; + + if (m_inline) memcpy(m_inline_storage, other.m_inline_storage, sizeof(m_inline_storage)); + + other.m_string = nullptr; + + return *this; +} + String::String(char* c_str) { check(c_str);