diff options
Diffstat (limited to 'source/fud_string.cpp')
-rw-r--r-- | source/fud_string.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/source/fud_string.cpp b/source/fud_string.cpp index 048cc94..c444a74 100644 --- a/source/fud_string.cpp +++ b/source/fud_string.cpp @@ -84,6 +84,32 @@ StringResult String::from(const String& rhs) return StringResult::okay(std::move(output)); } +StringResult String::from(StringView view, Allocator* allocator) +{ + if (allocator == nullptr || view.m_data == nullptr) { + return StringResult::error(FudStatus::NullPointer); + } + + String output{}; + output.m_allocator = allocator; + output.m_length = view.m_length; + + if (output.m_length >= output.m_capacity) { + output.m_capacity = output.m_length + 1; + + output.m_data = static_cast<utf8*>(M_TakeOrReturn(output.m_allocator->allocate(output.m_capacity))); + fudAssert(output.m_data != nullptr); + } + + auto copyStatus = copyMem(output.data(), output.m_capacity, view.m_data, output.m_length); + fudAssert(copyStatus == FudStatus::Success); + + auto terminateStatus = output.nullTerminate(); + fudAssert(terminateStatus == FudStatus::Success); + + return StringResult::okay(std::move(output)); +} + String::String(String&& rhs) noexcept : m_length{rhs.m_length}, m_capacity{rhs.m_capacity}, m_allocator{rhs.m_allocator} { if (rhs.isLarge()) { @@ -131,6 +157,10 @@ FudStatus String::copy(const String& rhs) String& String::operator=(String&& rhs) noexcept { + if (&rhs == this) { + return *this; + } + cleanup(); m_length = rhs.m_length; |