summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/fud_string.hpp42
-rw-r--r--source/fud_string.cpp6
2 files changed, 3 insertions, 45 deletions
diff --git a/include/fud_string.hpp b/include/fud_string.hpp
index 29b887a..55b1e86 100644
--- a/include/fud_string.hpp
+++ b/include/fud_string.hpp
@@ -326,57 +326,15 @@ class String {
FudStatus resize(size_t newCapacity);
- [[nodiscard]] bool optIsLarge() const
- {
- return (reinterpret_cast<uintptr_t>(m_allocator) & capacityMask) != 0;
- }
-
- public:
- static constexpr size_t OptBufSize = 2 * sizeof(size_t) + sizeof(utf8*) - 1;
- using OptBufType = Array<utf8, OptBufSize>;
- size_t optLength() const
- {
- if (optIsLarge()) {
- return m_large.optLength;
- }
- return m_small.optLength;
- }
-
- size_t optCapacity() const
- {
- if (optIsLarge()) {
- return m_large.optCapacity;
- }
- return OptBufSize - 1U;
- }
-
- private:
void setLength(size_t newLength)
{
m_length = newLength;
- if (optIsLarge()) {
- m_large.optLength = newLength;
- } else {
- m_small.optLength = static_cast<uint8_t>(newLength);
- }
}
/** \brief The allocator used to get storage for characters when the string
* is large. */
Allocator* m_allocator{&globalFudAllocator};
- union {
- struct {
- size_t optLength;
- size_t optCapacity;
- utf8* optData;
- } m_large;
- struct {
- uint8_t optLength;
- OptBufType optBuffer;
- } m_small;
- };
-
using BufType = Array<utf8, SSO_BUF_SIZE>;
union {
/** \brief The storage for string characters when using SSO. */
diff --git a/source/fud_string.cpp b/source/fud_string.cpp
index 58b6bd1..994689e 100644
--- a/source/fud_string.cpp
+++ b/source/fud_string.cpp
@@ -100,14 +100,14 @@ StringResult String::from(StringView view, Allocator* allocator)
String output{};
output.m_allocator = allocator;
- output.m_length = view.m_length;
- if (output.m_length >= output.m_capacity) {
- output.m_capacity = output.m_length + 1;
+ if (view.m_length >= output.capacity() + 1U) {
+ output.m_capacity = view.m_length + 1;
output.m_data = static_cast<utf8*>(M_TakeOrReturn(output.allocator()->allocate(output.m_capacity)));
fudAssert(output.m_data != nullptr);
}
+ output.setLength(view.m_length);
auto copyStatus = copyMem(output.dataMut(), output.m_capacity, view.m_data, output.m_length);
fudAssert(copyStatus == FudStatus::Success);