diff options
Diffstat (limited to 'source/core/slang-string-slice-pool.cpp')
| -rw-r--r-- | source/core/slang-string-slice-pool.cpp | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/source/core/slang-string-slice-pool.cpp b/source/core/slang-string-slice-pool.cpp index 45cc811a3..5c4c7f61f 100644 --- a/source/core/slang-string-slice-pool.cpp +++ b/source/core/slang-string-slice-pool.cpp @@ -14,9 +14,103 @@ StringSlicePool::StringSlicePool(Style style) : clear(); } +StringSlicePool::StringSlicePool(const ThisType& rhs): + m_style(rhs.m_style), + m_arena(1024) +{ + // Set with rhs + _set(rhs); +} + +void StringSlicePool::operator=(const ThisType& rhs) +{ + if (&rhs != this) + { + _set(rhs); + } +} + +void StringSlicePool::_set(const ThisType& rhs) +{ + SLANG_ASSERT(this != &rhs); + m_style = rhs.m_style; + + clear(); + + const Index startIndex = rhs.getFirstAddedIndex(); + const Count count = rhs.m_slices.getCount(); + + // We need the same amount of slices + m_slices.setCount(count); + + // Work out the total size to store all slices including terminating 0 + // (which *isn't* part of the slice size) + size_t totalSize = 0; + + for (Index i = startIndex; i < count; ++i) + { + const auto slice = rhs.m_slices[i]; + totalSize += slice.getLength() + 1; + } + + char* dst = (char*)m_arena.allocate(totalSize); + + for (Index i = startIndex; i < count; ++i) + { + const auto srcSlice = rhs.m_slices[i]; + const auto sliceSize = srcSlice.getLength(); + + // Copy over the src slices contents + ::memcpy(dst, srcSlice.begin(), sliceSize); + // Zero terminate + dst[sliceSize] = 0; + + const UnownedStringSlice dstSlice(dst, sliceSize); + // Set the slice + m_slices[i] = dstSlice; + + // Add to the map + m_map.Add(dstSlice, Handle(i)); + + // Skip to next slices storage + dst += sliceSize + 1; + } +} + +bool StringSlicePool::operator==(const ThisType& rhs) const +{ + if (this == &rhs) + { + return true; + } + + if (m_style != rhs.m_style) + { + return false; + } + + const auto count = m_slices.getCount(); + + if (count != rhs.m_slices.getCount()) + { + return false; + } + + for (Index i = 0; i < count; ++i) + { + if (m_slices[i] != rhs.m_slices[i]) + { + return false; + } + } + + return true; +} + void StringSlicePool::clear() { m_map.Clear(); + m_arena.deallocateAll(); switch (m_style) { |
