From 8249b28bea29e8ce17eac12776a60ec3f9e47176 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Thu, 17 Oct 2024 19:42:29 -0500 Subject: Rename InvalidInput to ArgumentInvalid. --- source/fud_string.cpp | 131 ++++++++++++++++++++++++-------------------------- 1 file changed, 62 insertions(+), 69 deletions(-) (limited to 'source/fud_string.cpp') diff --git a/source/fud_string.cpp b/source/fud_string.cpp index 31ba5ba..d354fe7 100644 --- a/source/fud_string.cpp +++ b/source/fud_string.cpp @@ -19,6 +19,8 @@ #include "fud_assert.hpp" +#include + namespace fud { ssize_t cStringLength(const char* str) @@ -59,31 +61,23 @@ StringResult String::makeFromCString(const char* cString, Allocator* allocator) auto lenResult = cStringLength(cString); if (lenResult < 0 || lenResult >= SSIZE_MAX) { - return StringResult::error(FudStatus::InvalidInput); + return StringResult::error(FudStatus::ArgumentInvalid); } String output{}; output.m_allocator = allocator; - auto* data = output.m_buffer.data(); output.m_length = static_cast(lenResult); if (output.m_length >= output.m_capacity) { output.m_capacity = output.m_length + 1; - auto dataResult{output.m_allocator->allocate(output.m_capacity)}; - if (dataResult.isError()) { - return StringResult::error(dataResult); - } - - data = static_cast(dataResult.takeOkay()); - if (data == nullptr) { - return StringResult::error(FudStatus::AllocFailure); - } - output.m_data = data; + output.m_data = static_cast(M_TakeOrReturn(output.m_allocator->allocate(output.m_capacity))); + fudAssert(output.m_data != nullptr); } - auto copyStatus = copyMem(data, output.m_capacity, cString, output.m_length); + auto copyStatus = copyMem(output.data(), output.m_capacity, cString, output.m_length); fudAssert(copyStatus == FudStatus::Success); + auto terminateStatus = output.nullTerminate(); fudAssert(terminateStatus == FudStatus::Success); @@ -93,7 +87,7 @@ StringResult String::makeFromCString(const char* cString, Allocator* allocator) StringResult String::from(const String& rhs) { if (!rhs.valid()) { - return StringResult::error(FudStatus::InvalidInput); + return StringResult::error(FudStatus::ArgumentInvalid); } String output{}; @@ -102,18 +96,16 @@ StringResult String::from(const String& rhs) output.m_allocator = rhs.m_allocator; if (rhs.isLarge()) { - auto dataResult = output.m_allocator->allocate(output.m_capacity); - if (dataResult.isError()) { - return StringResult::error(dataResult.getError()); - } - output.m_data = static_cast(dataResult.getOkay()); + output.m_data = static_cast(M_TakeOrReturn(output.m_allocator->allocate(output.m_capacity))); fudAssert(output.m_data != nullptr); } auto copyResult = copyMem(output.data(), output.m_capacity, rhs.data(), output.m_length); fudAssert(copyResult == FudStatus::Success); + auto nullTerminateStatus = output.nullTerminate(); fudAssert(nullTerminateStatus == FudStatus::Success); + return StringResult::okay(std::move(output)); } @@ -141,7 +133,7 @@ FudStatus String::copy(const String& rhs) } if (!rhs.valid()) { - return FudStatus::InvalidInput; + return FudStatus::ArgumentInvalid; } cleanup(); @@ -151,12 +143,7 @@ FudStatus String::copy(const String& rhs) m_allocator = rhs.m_allocator; if (isLarge()) { - auto dataResult = m_allocator->allocate(m_capacity); - if (dataResult.isError()) { - return dataResult.getError(); - } - m_data = static_cast(dataResult.getOkay()); - fudAssert(m_data != nullptr); + m_data = static_cast(M_TakeOrReturn(m_allocator->allocate(m_capacity))); } auto copyResult = copyMem(data(), m_capacity, rhs.data(), m_length); @@ -187,7 +174,8 @@ String& String::operator=(String&& rhs) noexcept void String::cleanup() { if (isLarge() && m_data != nullptr && m_allocator != nullptr) { - m_allocator->deallocate(m_data, m_capacity); + auto deallocStatus = m_allocator->deallocate(m_data, m_capacity); + static_cast(deallocStatus); m_data = nullptr; } } @@ -209,37 +197,36 @@ FudStatus String::resize(size_t newCapacity) if (newCapacity <= SSO_BUF_SIZE) { BufType temp{BufType::constFill(0)}; - static_cast(copyMem(data(), temp.size(), temp.data(), length())); - m_allocator->deallocate(m_data, m_capacity); + auto copyResult = copyMem(data(), temp.size(), temp.data(), length()); + fudAssert(copyResult == FudStatus::Success); + + auto deallocStatus = m_allocator->deallocate(m_data, m_capacity); m_capacity = SSO_BUF_SIZE; m_data = nullptr; + copyMem(m_buffer, temp); data()[m_length] = '\0'; - return FudStatus::Success; - } - utf8* newData = nullptr; - auto newDataResult = m_allocator->allocate(newCapacity); - if (newDataResult.isError()) { - return newDataResult.getError(); - } - newData = static_cast(newDataResult.getOkay()); - if (newData == nullptr) { - return FudStatus::AllocFailure; + return deallocStatus != FudStatus::Success ? FudStatus::DeallocFailure : FudStatus::Success; } + auto* newData = static_cast(M_TakeOrReturn(m_allocator->allocate(newCapacity))); + fudAssert(newData != nullptr); + auto copyResult = copyMem(newData, newCapacity, data(), length()); fudAssert(copyResult == FudStatus::Success); + auto deallocStatus = FudStatus::Success; if (isLarge()) { - m_allocator->deallocate(data(), m_capacity); + deallocStatus = m_allocator->deallocate(data(), m_capacity); } m_capacity = newCapacity; m_data = newData; data()[m_length] = '\0'; fudAssert(valid()); - return FudStatus::Success; + + return deallocStatus != FudStatus::Success ? FudStatus::DeallocFailure : FudStatus::Success; } bool String::nullTerminated() const @@ -348,12 +335,12 @@ FudStatus String::pushBack(const FudUtf8& letter) } if (!letter.valid()) { - return FudStatus::InvalidInput; + return FudStatus::ArgumentInvalid; } const auto* letterData = letter.data(); if (letterData == nullptr) { - return FudStatus::InvalidInput; + return FudStatus::ArgumentInvalid; } auto letterSize = letter.size(); @@ -385,7 +372,7 @@ FudStatus String::append(const char* source) { auto lenResult = cStringLength(source); if (lenResult < 0 || lenResult >= SSIZE_MAX) { - return FudStatus::InvalidInput; + return FudStatus::ArgumentInvalid; } return this->append(StringView{static_cast(lenResult), source}); @@ -436,41 +423,48 @@ FudStatus String::append(StringView source) StringResult String::catenate(const char* rhs) const { if (!valid()) { - return StringResult::error(FudStatus::InvalidInput); + return StringResult::error(FudStatus::ArgumentInvalid); } auto lenResult = cStringLength(rhs); if (lenResult < 0 || lenResult >= SSIZE_MAX) { - return StringResult::error(FudStatus::InvalidInput); + return StringResult::error(FudStatus::ArgumentInvalid); } size_t rhsLength = static_cast(lenResult); String output{}; - if (SIZE_MAX - m_length < rhsLength) - { + if (SIZE_MAX - m_length < rhsLength) { return StringResult::error(FudStatus::Failure); } + output.m_allocator = m_allocator; + output.m_length = m_length + rhsLength; - auto* destPtr = output.m_buffer.data(); - if (output.m_length >= output.m_capacity) { + if (output.m_length > output.m_capacity) { output.m_capacity = output.m_length + 1; - auto ptrResult = m_allocator->allocate(output.m_capacity); - if (ptrResult.isError()) { - return StringResult::error(ptrResult.getError()); - } - destPtr = static_cast(ptrResult.getOkay()); - if (destPtr == nullptr) { - return StringResult::error(FudStatus::AllocFailure); - } - output.m_data = destPtr; } - auto status = copyMem(destPtr, m_capacity, data(), length()); + if (!output.isLarge()) { + auto status = copyMem(output.m_buffer.data(), output.m_capacity, data(), length()); + fudAssert(status == FudStatus::Success); + + status = copyMem(output.m_buffer.data() + length(), output.m_capacity - length(), rhs, rhsLength); + fudAssert(status == FudStatus::Success); + + auto terminateStatus = output.nullTerminate(); + fudAssert(terminateStatus == FudStatus::Success); + + return StringResult::okay(std::move(output)); + } + + output.m_data = static_cast(M_TakeOrReturn(output.m_allocator->allocate(output.m_capacity))); + fudAssert(output.m_data != nullptr); + + auto status = copyMem(output.m_data, m_capacity, data(), length()); fudAssert(status == FudStatus::Success); - status = copyMem(destPtr + length(), output.m_capacity - length(), rhs, rhsLength); + status = copyMem(output.m_data + length(), output.m_capacity - length(), rhs, rhsLength); fudAssert(status == FudStatus::Success); auto terminateStatus = output.nullTerminate(); @@ -482,26 +476,25 @@ StringResult String::catenate(const char* rhs) const StringResult String::catenate(const String& rhs) const { if (!valid() || !rhs.valid()) { - return StringResult::error(FudStatus::InvalidInput); + return StringResult::error(FudStatus::ArgumentInvalid); } - if (SIZE_MAX - m_length < rhs.length()) - { + if (SIZE_MAX - m_length < rhs.length()) { return StringResult::error(FudStatus::Failure); } String output{}; + output.m_allocator = m_allocator; output.m_length = m_length + rhs.length(); output.m_capacity = output.m_length + 1; + if (output.m_capacity < SSO_BUF_SIZE) { output.m_capacity = SSO_BUF_SIZE; } + if (output.isLarge()) { - auto allocResult{output.m_allocator->allocate(output.m_capacity)}; - if (allocResult.isError()) { - return StringResult::error(allocResult.getError()); - } - output.m_data = static_cast(allocResult.takeOkay()); + output.m_data = static_cast(M_TakeOrReturn(output.m_allocator->allocate(output.m_capacity))); + fudAssert(output.m_data != nullptr); } auto* destPtr = output.data(); -- cgit v1.2.3