summaryrefslogtreecommitdiff
path: root/source/fud_string.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/fud_string.cpp')
-rw-r--r--source/fud_string.cpp54
1 files changed, 31 insertions, 23 deletions
diff --git a/source/fud_string.cpp b/source/fud_string.cpp
index 4943dac..edb25da 100644
--- a/source/fud_string.cpp
+++ b/source/fud_string.cpp
@@ -51,27 +51,27 @@ StringResult String::makeFromCString(const char* cString, Allocator* allocator)
}
String output{};
- output.m_allocator = allocator;
+ output.m_allocator = reinterpret_cast<uintptr_t>(allocator);
utf8* data{nullptr};
size_t capacity = length + 1;
bool isLarge = capacity > SsoBufSize;
if (isLarge) {
- output.m_repr.large.capacity = capacity & largeStringCapacitymask;
+ output.m_repr.large.capacity = capacity;
output.m_repr.large.length = length;
auto dataResult = output.allocator()->allocate(output.m_repr.large.capacity);
if (dataResult.isError()) {
return StringResult::error(dataResult.getError());
}
output.m_repr.large.data = static_cast<utf8*>(dataResult.getOkay());
- output.m_repr.large.isLarge = 1;
data = output.m_repr.large.data;
+ output.setLarge();
} else {
capacity = SsoBufSize;
static_assert(SsoBufSize < std::numeric_limits<int8_t>::max());
- output.m_repr.small.isLarge = 0;
output.m_repr.small.length = static_cast<uint8_t>(length) & smallStringLengthMask;
data = output.m_repr.small.buffer.data();
+ output.setSmall();
}
fudAssert(data != nullptr);
@@ -99,7 +99,7 @@ StringResult String::from(const String& rhs)
if (rhs.isLarge()) {
output.m_repr.large = rhs.m_repr.large;
output.m_repr.large.data = static_cast<utf8*>(
- M_TakeOrReturn(output.allocator()->allocate(output.m_repr.large.capacity)));
+ 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;
@@ -135,22 +135,22 @@ StringResult String::from(StringView view, Allocator* allocator)
}
String output{};
- output.m_allocator = allocator;
+ output.m_allocator = reinterpret_cast<uintptr_t>(allocator);
size_t capacity = view.length() + 1U;
bool isLarge = capacity > SsoBufSize;
utf8* data{nullptr};
if (isLarge) {
- output.m_repr.large.capacity = capacity & largeStringCapacitymask;
+ output.m_repr.large.capacity = capacity;
output.m_repr.large.length = view.length();
- output.m_repr.large.data = static_cast<utf8*>(M_TakeOrReturn(output.allocator()->allocate(capacity)));
- output.m_repr.large.isLarge = 1;
+ output.m_repr.large.data = static_cast<utf8*>(M_TakeOrReturn(StringResult, output.allocator()->allocate(capacity)));
data = output.m_repr.large.data;
+ output.setLarge();
} else {
capacity = SsoBufSize;
static_assert(SsoBufSize < std::numeric_limits<int8_t>::max());
- output.m_repr.small.isLarge = 0;
output.m_repr.small.length = static_cast<uint8_t>(view.length()) & smallStringLengthMask;
data = output.m_repr.small.buffer.data();
+ output.setSmall();
}
fudAssert(data != nullptr);
auto copyStatus = copyMem(data, capacity, view.m_data, view.length());
@@ -194,7 +194,11 @@ FudStatus String::copy(const String& rhs)
size_t length{};
if (isLarge()) {
- m_repr.large.data = static_cast<utf8*>(M_TakeOrReturn(allocator()->allocate(m_repr.large.capacity)));
+ 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;
@@ -251,11 +255,11 @@ FudStatus String::resize(size_t newCapacity)
return FudStatus::OperationInvalid;
}
- if (!isLarge() && newCapacity <= SSO_BUF_SIZE) {
+ if (!isLarge() && newCapacity <= SsoBufSize) {
return FudStatus::Success;
}
- if (newCapacity <= SSO_BUF_SIZE) {
+ if (newCapacity <= SsoBufSize) {
fudAssert(isLarge());
auto len = static_cast<uint8_t>(length());
@@ -264,7 +268,7 @@ FudStatus String::resize(size_t newCapacity)
fudAssert(copyResult == FudStatus::Success);
auto deallocStatus = allocator()->deallocate(m_repr.large.data, m_repr.large.capacity);
- m_repr.small.isLarge = 0;
+ setSmall();
m_repr.small.length = len & smallStringLengthMask;
copyMem(m_repr.small.buffer, temp);
m_repr.small.buffer[len] = '\0';
@@ -272,7 +276,11 @@ FudStatus String::resize(size_t newCapacity)
return deallocStatus != FudStatus::Success ? FudStatus::DeallocFailure : FudStatus::Success;
}
- auto* newData = static_cast<utf8*>(M_TakeOrReturn(allocator()->allocate(newCapacity)));
+ auto allocResult = allocator()->allocate(newCapacity);
+ if (allocResult.isError()) {
+ return allocResult.takeError();
+ }
+ auto* newData = static_cast<utf8*>(allocResult.takeOkay());
fudAssert(newData != nullptr);
auto copyResult = copyMem(newData, newCapacity, data(), length());
@@ -285,8 +293,8 @@ FudStatus String::resize(size_t newCapacity)
size_t len = length();
- m_repr.large.isLarge = 1;
- m_repr.large.capacity = newCapacity & largeStringCapacitymask;
+ setLarge();
+ m_repr.large.capacity = newCapacity;
m_repr.large.data = newData;
m_repr.large.length = len;
m_repr.large.data[m_repr.large.length] = '\0';
@@ -622,19 +630,19 @@ StringResult String::catenate(const char* rhs) const
size_t outputCapacity = outputLength + 1;
utf8* outputData{nullptr};
if (outputCapacity > SsoBufSize) {
- output.m_repr.large.capacity = outputCapacity & largeStringCapacitymask;
+ output.m_repr.large.capacity = outputCapacity;
output.m_repr.large.length = outputLength;
auto dataResult = output.allocator()->allocate(output.m_repr.large.capacity);
if (dataResult.isError()) {
return StringResult::error(dataResult.getError());
}
output.m_repr.large.data = static_cast<utf8*>(dataResult.getOkay());
- output.m_repr.large.isLarge = 1;
+ output.setLarge();
outputData = output.m_repr.large.data;
} else {
outputCapacity = SsoBufSize;
static_assert(SsoBufSize < std::numeric_limits<int8_t>::max());
- output.m_repr.small.isLarge = 0;
+ output.setSmall();
output.m_repr.small.length = static_cast<uint8_t>(outputLength) & smallStringLengthMask;
outputData = output.m_repr.small.buffer.data();
}
@@ -669,19 +677,19 @@ StringResult String::catenate(const String& rhs) const
size_t outputCapacity = outputLength + 1;
utf8* outputData{nullptr};
if (outputCapacity > SsoBufSize) {
- output.m_repr.large.capacity = outputCapacity & largeStringCapacitymask;
+ output.m_repr.large.capacity = outputCapacity;
output.m_repr.large.length = outputLength;
auto dataResult = output.allocator()->allocate(output.m_repr.large.capacity);
if (dataResult.isError()) {
return StringResult::error(dataResult.getError());
}
output.m_repr.large.data = static_cast<utf8*>(dataResult.getOkay());
- output.m_repr.large.isLarge = 1;
+ output.setLarge();
outputData = output.m_repr.large.data;
} else {
outputCapacity = SsoBufSize;
static_assert(SsoBufSize < std::numeric_limits<int8_t>::max());
- output.m_repr.small.isLarge = 0;
+ output.setSmall();
output.m_repr.small.length = static_cast<uint8_t>(outputLength) & smallStringLengthMask;
outputData = output.m_repr.small.buffer.data();
}