summaryrefslogtreecommitdiffstats
path: root/source/core
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2023-04-20 13:42:20 -0400
committerGitHub <noreply@github.com>2023-04-20 13:42:20 -0400
commit088644c21c015eb60a41a59f417990023f1f4ef8 (patch)
tree596d224f99e35dcd3d66507cfe297bc25407336d /source/core
parent4e67cdedbef8f643c90b48172d5419d3dd1839db (diff)
Improve SourceMap coverage/testing (#2818)
* #include an absolute path didn't work - because paths were taken to always be relative. * WIP around more value like behavior for SourceMap/StringPool. * Use default impls on SourceMap ctor/assignment. * Handle comparison of SourceMaps. Some small fixes. Expand unit tests.
Diffstat (limited to 'source/core')
-rw-r--r--source/core/slang-string-slice-pool.cpp94
-rw-r--r--source/core/slang-string-slice-pool.h12
2 files changed, 103 insertions, 3 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)
{
diff --git a/source/core/slang-string-slice-pool.h b/source/core/slang-string-slice-pool.h
index 90586a527..843556296 100644
--- a/source/core/slang-string-slice-pool.h
+++ b/source/core/slang-string-slice-pool.h
@@ -102,13 +102,19 @@ public:
/// Swap this with rhs
void swapWith(ThisType& rhs);
+ /// True if the pools are identical. Same style, same slices in the same order.
+ bool operator==(const ThisType& rhs) const;
+
+ /// Copy ctor
+ StringSlicePool(const ThisType& rhs);
+ /// Assignment
+ void operator=(const ThisType& rhs);
+
/// Ctor
explicit StringSlicePool(Style style);
protected:
- // Disable copy ctor and assignment
- StringSlicePool(const ThisType& rhs) = delete;
- void operator=(const ThisType& rhs) = delete;
+ void _set(const ThisType& rhs);
Style m_style;
List<UnownedStringSlice> m_slices;