diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-10-25 01:09:10 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-10-25 01:09:10 -0500 |
commit | 11968f674a7de34fb7de744598a8086330cd88a3 (patch) | |
tree | be5b885ef47c8ceab50f558c8aef6632fefc94d1 /source/fud_string.cpp | |
parent | 512d026de016f2720060d264adec02e56123851d (diff) |
Get working string formatting for unsigned numbers.
Diffstat (limited to 'source/fud_string.cpp')
-rw-r--r-- | source/fud_string.cpp | 99 |
1 files changed, 96 insertions, 3 deletions
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<size_t>(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(); |