From 6c7fd1db481ff10a16ecab958c6542784fa60b9c Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Wed, 30 Oct 2024 09:51:54 -0500 Subject: Use std::byte* instead of void* for allocators. --- source/fud_string.cpp | 95 ++++++++++++++++++++++----------------------------- 1 file changed, 41 insertions(+), 54 deletions(-) (limited to 'source/fud_string.cpp') diff --git a/source/fud_string.cpp b/source/fud_string.cpp index 058c813..4a20630 100644 --- a/source/fud_string.cpp +++ b/source/fud_string.cpp @@ -52,9 +52,9 @@ StringResult String::makeFromCString(const char* cString, Allocator* allocator) return StringResult::error(FudStatus::ArgumentInvalid); } - auto length = static_cast(lenResult); + auto outputLength = static_cast(lenResult); if constexpr (static_cast(std::numeric_limits::max()) > maxStringLength) { - if (length > maxStringLength) { + if (outputLength > maxStringLength) { return StringResult::error(FudStatus::ArgumentInvalid); } } @@ -62,30 +62,21 @@ StringResult String::makeFromCString(const char* cString, Allocator* allocator) String output{}; output.m_allocator = reinterpret_cast(allocator); - utf8* data{nullptr}; - size_t capacity = length + 1; - bool isLarge = capacity > SsoBufSize; + utf8* outputData{nullptr}; + size_t outputCapacity = outputLength + 1; + bool isLarge = outputCapacity > SsoBufSize; if (isLarge) { - output.m_repr.large.capacity = capacity; - output.m_repr.large.length = length; - auto dataResult = output.allocator()->allocate(capacity); - if (dataResult.isError()) { - return StringResult::error(dataResult.getError()); + auto status = output.makeLarge(outputCapacity, outputLength, outputData); + if (status != FudStatus::Success) { + return StringResult::error(status); } - output.m_repr.large.data = static_cast(dataResult.getOkay()); - data = output.m_repr.large.data; - output.setLarge(); } else { - capacity = SsoBufSize; - static_assert(SsoBufSize < std::numeric_limits::max()); - output.m_repr.small.length = length & smallStringLengthMask; - data = output.m_repr.small.buffer.data(); - output.setSmall(); + output.makeSmall(outputCapacity, outputLength, outputData); } - fudAssert(data != nullptr); + fudAssert(outputData != nullptr); - auto copyStatus = copyMem(data, capacity, cString, length); + auto copyStatus = copyMem(outputData, outputCapacity, cString, outputLength); fudAssert(copyStatus == FudStatus::Success); auto terminateStatus = output.nullTerminate(); @@ -102,25 +93,20 @@ StringResult String::from(const String& rhs) String output{}; output.m_allocator = rhs.m_allocator; - utf8* data{nullptr}; - size_t capacity{0}; - size_t length{0}; + utf8* outputData{nullptr}; + size_t outputCapacity{0}; + size_t outputLength{0}; if (rhs.isLarge()) { - output.m_repr.large = rhs.m_repr.large; - output.m_repr.large.data = static_cast( - M_TakeOrReturn(StringResult, output.allocator()->allocate(output.m_repr.large.capacity))); - data = output.m_repr.large.data; - capacity = output.m_repr.large.capacity; - length = output.m_repr.large.length; + auto status = output.makeLarge(outputCapacity, outputLength, outputData); + if (status != FudStatus::Success) { + return StringResult::error(status); + } } else { - output.m_repr.small = rhs.m_repr.small; - data = output.m_repr.small.buffer.data(); - capacity = SsoBufSize; - length = output.m_repr.small.length; + output.makeSmall(outputCapacity, outputLength, outputData); } - fudAssert(data != nullptr); + fudAssert(outputData != nullptr); - auto copyResult = copyMem(output.dataMut(), capacity, rhs.data(), length); + auto copyResult = copyMem(output.dataMut(), outputCapacity, rhs.data(), outputLength); fudAssert(copyResult == FudStatus::Success); auto nullTerminateStatus = output.nullTerminate(); @@ -193,28 +179,29 @@ FudStatus String::copy(const String& rhs) m_allocator = rhs.m_allocator; m_repr = rhs.m_repr; - utf8* data{nullptr}; - size_t capacity{}; - size_t length{}; + utf8* outputData{nullptr}; + size_t outputCapacity{}; + size_t outputLength{}; if (isLarge()) { auto allocResult = allocator()->allocate(m_repr.large.capacity); if (allocResult.isError()) { return allocResult.takeError(); } - m_repr.large.data = static_cast(allocResult.takeOkay()); - capacity = m_repr.large.capacity; - length = m_repr.large.length; - data = m_repr.large.data; + m_repr.large.data = reinterpret_cast(allocResult.takeOkay()); + outputCapacity = m_repr.large.capacity; + outputLength = m_repr.large.length; + outputData = m_repr.large.data; + } else { - capacity = SsoBufSize; - length = m_repr.small.length; - data = m_repr.small.buffer.data(); + outputCapacity = SsoBufSize; + outputLength = m_repr.small.length; + outputData = m_repr.small.buffer.data(); } - fudAssert(data != nullptr); + fudAssert(outputData != nullptr); - auto copyResult = copyMem(dataMut(), capacity, rhs.data(), length); + auto copyResult = copyMem(dataMut(), outputCapacity, rhs.data(), outputLength); fudAssert(copyResult == FudStatus::Success); auto nullTerminateStatus = nullTerminate(); fudAssert(nullTerminateStatus == FudStatus::Success); @@ -243,7 +230,7 @@ void String::cleanup() { const auto* allocPtr = allocator(); if (isLarge() && m_repr.large.data != nullptr && allocPtr != nullptr) { - auto deallocStatus = allocator()->deallocate(m_repr.large.data, m_repr.large.capacity); + auto deallocStatus = allocator()->deallocate(reinterpret_cast(m_repr.large.data), m_repr.large.capacity); static_cast(deallocStatus); m_repr.large.data = nullptr; } @@ -271,7 +258,7 @@ FudStatus String::resize(size_t newCapacity) auto copyResult = copyMem(dataMut(), temp.size(), temp.data(), len); fudAssert(copyResult == FudStatus::Success); - auto deallocStatus = allocator()->deallocate(m_repr.large.data, m_repr.large.capacity); + auto deallocStatus = allocator()->deallocate(reinterpret_cast(m_repr.large.data), m_repr.large.capacity); setSmall(); m_repr.small.length = len & smallStringLengthMask; copyMem(m_repr.small.buffer, temp); @@ -284,7 +271,7 @@ FudStatus String::resize(size_t newCapacity) if (allocResult.isError()) { return allocResult.takeError(); } - auto* newData = static_cast(allocResult.takeOkay()); + auto* newData = reinterpret_cast(allocResult.takeOkay()); fudAssert(newData != nullptr); auto copyResult = copyMem(newData, newCapacity, data(), length()); @@ -292,7 +279,7 @@ FudStatus String::resize(size_t newCapacity) auto deallocStatus = FudStatus::Success; if (isLarge()) { - deallocStatus = allocator()->deallocate(dataMut(), m_repr.large.capacity); + deallocStatus = allocator()->deallocate(reinterpret_cast(m_repr.large.data), m_repr.large.capacity); } size_t len = length(); @@ -569,7 +556,7 @@ DrainResult String::drain(StringView source) auto status = copyMem(destPtr, remainingLength(), firstPart.m_data, firstPart.m_length); fudAssert(status == FudStatus::Success); addToLength(firstPart.m_length); - result.bytesWritten += firstPart.m_length; + result.bytesDrained += firstPart.m_length; status = nullTerminate(); source.advanceUnsafe(firstPart.m_length); fudAssert(status == FudStatus::Success); @@ -603,7 +590,7 @@ DrainResult String::drain(StringView source) auto* destPtr = dataMut() + length(); result.status = copyMem(destPtr, remainingLength(), source.data(), source.length()); fudAssert(result.status == FudStatus::Success); - result.bytesWritten += source.length(); + result.bytesDrained += source.length(); setLength(newLength); result.status = nullTerminate(); @@ -767,7 +754,7 @@ FudStatus String::makeLarge(size_t cap, size_t len, utf8*& outputData) if (dataResult.isError()) { return dataResult.getError(); } - m_repr.large.data = static_cast(dataResult.getOkay()); + m_repr.large.data = reinterpret_cast(dataResult.getOkay()); outputData = m_repr.large.data; setLarge(); return FudStatus::Success; -- cgit v1.2.3