From 11968f674a7de34fb7de744598a8086330cd88a3 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Fri, 25 Oct 2024 01:09:10 -0500 Subject: Get working string formatting for unsigned numbers. --- source/fud_allocator.cpp | 1 - source/fud_string.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 96 insertions(+), 4 deletions(-) (limited to 'source') diff --git a/source/fud_allocator.cpp b/source/fud_allocator.cpp index 1e4117a..8daf969 100644 --- a/source/fud_allocator.cpp +++ b/source/fud_allocator.cpp @@ -35,7 +35,6 @@ FudStatus FudAllocator::deallocate(void* pointer, size_t bytes) if (pointer == nullptr || bytes == 0) { return FudStatus::ArgumentInvalid; } - static_cast(bytes); fudFree(pointer); return FudStatus::Success; } diff --git a/source/fud_string.cpp b/source/fud_string.cpp index 6e16741..048cc94 100644 --- a/source/fud_string.cpp +++ b/source/fud_string.cpp @@ -364,14 +364,15 @@ FudStatus String::append(StringView source) return FudStatus::StringInvalid; } - if (source.data() == m_data) { - return FudStatus::Aliased; + if (source.data() == nullptr) { + return FudStatus::NullPointer; } const size_t newLength = length() + source.length(); if (newLength < length()) { return FudStatus::OperationInvalid; } + const size_t newSize = newLength + 1; // cppcheck-suppress knownConditionTrueFalse if (newSize < newLength) { // cppcheck-suppress knownConditionTrueFalse return FudStatus::OperationInvalid; @@ -386,7 +387,7 @@ FudStatus String::append(StringView source) } auto* destPtr = data() + length(); - auto status = copyMem(destPtr, m_capacity, source.data(), source.length()); + auto status = copyMem(destPtr, remainingLength(), source.data(), source.length()); fudAssert(status == FudStatus::Success); m_length += source.length(); @@ -395,6 +396,87 @@ FudStatus String::append(StringView source) return status; } +DrainResult String::drain(const char* source) +{ + auto lenResult = cStringLength(source); + if (lenResult < 0 || lenResult >= SSIZE_MAX) { + return {0, FudStatus::ArgumentInvalid}; + } + + return drain(StringView{static_cast(lenResult), source}); +} + +DrainResult String::drain(const String& source) +{ + return drain(source.asView()); +} + +DrainResult String::drain(StringView source) +{ + DrainResult result{0, FudStatus::Success}; + if (!valid()) { + result.status = FudStatus::StringInvalid; + return result; + } + + if (source.data() == nullptr) { + result.status = FudStatus::NullPointer; + return result; + } + + if (source.length() == 0) { + return result; + } + + if (remainingLength() > 0) { + StringView firstPart{source}; + if (source.length() > remainingLength()) { + firstPart.m_length = remainingLength(); + } + auto* destPtr = data() + length(); + auto status = copyMem(destPtr, remainingLength(), firstPart.m_data, firstPart.m_length); + fudAssert(status == FudStatus::Success); + m_length += firstPart.m_length; + result.bytesWritten += firstPart.m_length; + status = nullTerminate(); + source.advanceUnsafe(firstPart.m_length); + fudAssert(status == FudStatus::Success); + } + + if (source.length() == 0) { + return result; + } + + const size_t newLength = length() + source.length(); + if (newLength < length()) { + result.status = FudStatus::OperationInvalid; + return result; + } + const size_t newSize = newLength + 1; // cppcheck-suppress knownConditionTrueFalse + if (newSize < newLength) { // cppcheck-suppress knownConditionTrueFalse + result.status = FudStatus::OperationInvalid; + return result; + } + if (newSize >= m_capacity) { + auto newCapacity = newSize < SIZE_MAX / 2 ? newSize * 2 : SIZE_MAX; + const auto resizeStatus = resize(newCapacity); + if (resizeStatus != FudStatus::Success) { + result.status = resizeStatus; + return result; + } + fudAssert(m_capacity == newCapacity); + } + + auto* destPtr = data() + length(); + auto status = copyMem(destPtr, remainingLength(), source.data(), source.length()); + fudAssert(status == FudStatus::Success); + + m_length += source.length(); + status = nullTerminate(); + + return result; +} + StringResult String::catenate(const char* rhs) const { if (!valid()) { @@ -507,6 +589,17 @@ bool String::compare(const String& rhs) const return diffResult.getOkay() == 0; } +FudStatus String::clear() { + if (!valid()) { + return FudStatus::StringInvalid; + } + + data()[0] = '\0'; + m_length = 0; + + return FudStatus::Success; +} + const utf8* String::begin() const { return data(); -- cgit v1.2.3