summaryrefslogtreecommitdiff
path: root/source/fud_string.cpp
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2024-10-27 09:04:05 -0500
committerDominick Allen <djallen@librehumanitas.org>2024-10-27 09:04:05 -0500
commitb8345246dcc2121bcb6d1515a9341789de20199f (patch)
tree4a25857512a90ff38e8a40166c54694b74920216 /source/fud_string.cpp
parentf84b8259f6e980fed647d8e1ec0634f89ee59c06 (diff)
First crack at file objects.
Diffstat (limited to 'source/fud_string.cpp')
-rw-r--r--source/fud_string.cpp30
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;