summaryrefslogtreecommitdiff
path: root/source/fud_string.cpp
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2024-10-17 19:42:29 -0500
committerDominick Allen <djallen@librehumanitas.org>2024-10-17 19:42:29 -0500
commit8249b28bea29e8ce17eac12776a60ec3f9e47176 (patch)
tree98318d7564b5f618cfb59e23cc6b918fcab88ee8 /source/fud_string.cpp
parentb32e83ece42cec5aa9dee370bcdf349d23dbc8ba (diff)
Rename InvalidInput to ArgumentInvalid.
Diffstat (limited to 'source/fud_string.cpp')
-rw-r--r--source/fud_string.cpp131
1 files changed, 62 insertions, 69 deletions
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 <type_traits>
+
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<size_t>(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<utf8*>(dataResult.takeOkay());
- if (data == nullptr) {
- return StringResult::error(FudStatus::AllocFailure);
- }
- output.m_data = data;
+ output.m_data = static_cast<utf8*>(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<utf8*>(dataResult.getOkay());
+ output.m_data = static_cast<utf8*>(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<utf8*>(dataResult.getOkay());
- fudAssert(m_data != nullptr);
+ m_data = static_cast<utf8*>(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<void>(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<void>(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<utf8*>(newDataResult.getOkay());
- if (newData == nullptr) {
- return FudStatus::AllocFailure;
+ return deallocStatus != FudStatus::Success ? FudStatus::DeallocFailure : FudStatus::Success;
}
+ auto* newData = static_cast<utf8*>(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<size_t>(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<size_t>(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<utf8*>(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<utf8*>(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<utf8*>(allocResult.takeOkay());
+ output.m_data = static_cast<utf8*>(M_TakeOrReturn(output.m_allocator->allocate(output.m_capacity)));
+ fudAssert(output.m_data != nullptr);
}
auto* destPtr = output.data();