summaryrefslogtreecommitdiff
path: root/source/fud_string.cpp
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2024-10-30 09:51:54 -0500
committerDominick Allen <djallen@librehumanitas.org>2024-10-30 09:51:54 -0500
commit6c7fd1db481ff10a16ecab958c6542784fa60b9c (patch)
treee0162f930ec44a7c53b07061311d52910c36e481 /source/fud_string.cpp
parent8dcb1de91e15ff7fc66279cd9cd9ad8a70f624e0 (diff)
Use std::byte* instead of void* for allocators.
Diffstat (limited to 'source/fud_string.cpp')
-rw-r--r--source/fud_string.cpp95
1 files changed, 41 insertions, 54 deletions
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<size_t>(lenResult);
+ auto outputLength = static_cast<size_t>(lenResult);
if constexpr (static_cast<size_t>(std::numeric_limits<ssize_t>::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<uintptr_t>(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<utf8*>(dataResult.getOkay());
- data = output.m_repr.large.data;
- output.setLarge();
} else {
- capacity = SsoBufSize;
- static_assert(SsoBufSize < std::numeric_limits<int8_t>::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<utf8*>(
- 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<utf8*>(allocResult.takeOkay());
- capacity = m_repr.large.capacity;
- length = m_repr.large.length;
- data = m_repr.large.data;
+ m_repr.large.data = reinterpret_cast<utf8*>(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<std::byte*>(m_repr.large.data), m_repr.large.capacity);
static_cast<void>(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<std::byte*>(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<utf8*>(allocResult.takeOkay());
+ auto* newData = reinterpret_cast<utf8*>(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<std::byte*>(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<utf8*>(dataResult.getOkay());
+ m_repr.large.data = reinterpret_cast<utf8*>(dataResult.getOkay());
outputData = m_repr.large.data;
setLarge();
return FudStatus::Success;