blob: 797da5a0fbf865bfd069f131d39188aa35eaf499 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
#include "slang-string-slice-pool.h"
namespace Slang {
/* static */ const StringSlicePool::Handle StringSlicePool::kNullHandle;
/* static */ const StringSlicePool::Handle StringSlicePool::kEmptyHandle;
/* static */const int StringSlicePool::kNumDefaultHandles;
StringSlicePool::StringSlicePool() :
m_arena(1024)
{
clear();
}
void StringSlicePool::clear()
{
m_slices.SetSize(2);
m_slices[0] = UnownedStringSlice((const char*)nullptr, (const char*)nullptr);
m_slices[1] = UnownedStringSlice::fromLiteral("");
// Add the empty entry
m_map.Add(m_slices[1], kEmptyHandle);
m_map.Clear();
}
StringSlicePool::Handle StringSlicePool::add(const Slice& slice)
{
const Handle* handlePtr = m_map.TryGetValue(slice);
if (handlePtr)
{
return *handlePtr;
}
// Create a scoped copy
UnownedStringSlice scopePath(m_arena.allocateString(slice.begin(), slice.size()), slice.size());
const int index = int(m_slices.Count());
m_slices.Add(scopePath);
m_map.Add(scopePath, Handle(index));
return Handle(index);
}
StringSlicePool::Handle StringSlicePool::add(StringRepresentation* stringRep)
{
if (stringRep == nullptr)
{
return kNullHandle;
}
return add(StringRepresentation::asSlice(stringRep));
}
StringSlicePool::Handle StringSlicePool::add(const char* chars)
{
if (!chars)
{
return kNullHandle;
}
if (chars[0] == 0)
{
return kEmptyHandle;
}
return add(UnownedStringSlice(chars));
}
int StringSlicePool::findIndex(const Slice& slice) const
{
const Handle* handlePtr = m_map.TryGetValue(slice);
return handlePtr ? int(*handlePtr) : -1;
}
} // namespace Slang
|