diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-10-29 23:16:46 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-10-29 23:16:46 -0500 |
commit | 8dcb1de91e15ff7fc66279cd9cd9ad8a70f624e0 (patch) | |
tree | c73840b15a074cf37f59bc3b2eff56cde982d74f /include/fud_string.hpp | |
parent | 8ce397e8c0a83e49e390de9deb73d588e4931ecf (diff) |
u8 string literals
Diffstat (limited to 'include/fud_string.hpp')
-rw-r--r-- | include/fud_string.hpp | 56 |
1 files changed, 19 insertions, 37 deletions
diff --git a/include/fud_string.hpp b/include/fud_string.hpp index cdc6b91..226bb89 100644 --- a/include/fud_string.hpp +++ b/include/fud_string.hpp @@ -60,6 +60,7 @@ class String { * \returns FudStatus::AllocFailure if the allocator fails. */ static StringResult makeFromCString(const char* cString); + static StringResult makeFromCString(const char8_t* cString); /** \brief Create a string from a C String, specifying the allocator. * @@ -73,6 +74,7 @@ class String { * \returns FudStatus::AllocFailure if the allocator fails. */ static StringResult makeFromCString(const char* cString, Allocator* allocator); + static StringResult makeFromCString(const char8_t* cString, Allocator* allocator); /** \brief Create a string from concatenating multiple C Strings. * @@ -151,24 +153,15 @@ class String { String output{}; output.m_allocator = reinterpret_cast<uintptr_t>(allocator); utf8* data{nullptr}; - size_t capacity = totalLength + 1; - bool isLarge = capacity > SsoBufSize; + size_t outputCapacity = totalLength + 1; + bool isLarge = outputCapacity > SsoBufSize; if (isLarge) { - output.m_repr.large.capacity = capacity; - output.m_repr.large.length = totalLength; - auto dataResult = output.allocator()->allocate(output.m_repr.large.capacity); - if (dataResult.isError()) { - return StringResult::error(dataResult.getError()); + auto status = output.makeLarge(outputCapacity, totalLength, data); + 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 = static_cast<uint8_t>(totalLength) & smallStringLengthMask; - data = output.m_repr.small.buffer.data(); - output.setSmall(); + output.makeSmall(outputCapacity, totalLength, data); } fudAssert(data != nullptr); @@ -176,7 +169,11 @@ class String { size_t cumulativeLength = 0; for (size_t idx = 0; idx < strPointers.size(); ++idx) { const auto* cString = strPointers[idx]; - auto copyStatus = copyMem(data + cumulativeLength, capacity - cumulativeLength, cString, lengths[idx]); + auto copyStatus = copyMem( + data + cumulativeLength, + outputCapacity - cumulativeLength, + cString, + lengths[idx]); fudAssert(copyStatus == FudStatus::Success); cumulativeLength += lengths[idx]; } @@ -368,28 +365,13 @@ class String { m_allocator &= allocatorMask; } - void addToLength(size_t augend) - { - if (isLarge()) { - fudAssert(m_repr.large.length + augend < maxStringLength); - m_repr.large.length += augend; - } else { - fudAssert(m_repr.small.length + augend < maxSmallStringLength); - m_repr.small.length = static_cast<decltype(m_repr.small.length)>((m_repr.small.length + augend)) & - smallStringLengthMask; - } - } + void addToLength(size_t augend); - void setLength(size_t newLength) - { - if (isLarge()) { - fudAssert(newLength < maxStringLength); - m_repr.large.length = newLength; - } else { - fudAssert(newLength < maxSmallStringLength); - m_repr.small.length = static_cast<uint8_t>(newLength) & smallStringLengthMask; - } - } + void setLength(size_t newLength); + + FudStatus makeLarge(size_t cap, size_t len, utf8*& outputData); + + void makeSmall(size_t& cap, size_t len, utf8*& outputData); }; } // namespace fud |