summaryrefslogtreecommitdiffstats
path: root/source/compiler-core/slang-slice-allocator.cpp
blob: 9a0620cedb88b3aaf6a94ff432ca10cc7798aa4b (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// slang-slice-allocator.cpp
#include "slang-slice-allocator.h"

#include "../core/slang-blob.h"

namespace Slang {

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SliceUtil !!!!!!!!!!!!!!!!!!!!!!!!!!! */

/* static */ List<String> SliceUtil::toList(const Slice<TerminatedCharSlice>& in)
{
    List<String> list;
    const auto count = in.count;

    list.setCount(count);
    for (Index i = 0; i < count; ++i)
    {
        list[i] = asStringSlice(in[i]);
    }
    return list;
}

/* static */const char* SliceUtil::getTerminated(ISlangBlob* blob, TerminatedCharSlice& outSlice)
{
    const auto size = blob->getBufferSize();
    if (size == 0)
    {
        outSlice = TerminatedCharSlice();
        return outSlice.begin();
    }

    // If there is a 0 at the end byte, we are zero terminated 
    const char* chars = (const char*)blob->getBufferPointer();
    if (chars[size - 1] == 0)
    {
        outSlice = TerminatedCharSlice(chars, Count(size - 1));
        return chars;
    }

    // See if it has a castable interface
    ComPtr<ICastable> castable;
    if (SLANG_SUCCEEDED(blob->queryInterface(ICastable::getTypeGuid(), (void**)castable.writeRef())))
    {
        if (castable->castAs(SlangTerminatedChars::getTypeGuid()))
        {
            outSlice = TerminatedCharSlice(chars, Count(size));
            return chars;
        }
    }

    return nullptr;
}

/* static */TerminatedCharSlice SliceUtil::toTerminatedCharSlice(SliceAllocator& allocator, ISlangBlob* blob)
{
    TerminatedCharSlice slice;
    if (SliceUtil::getTerminated(blob, slice))
    {
        return slice;
    }
    const auto size = blob->getBufferSize();
    // We are out of options, we just have to allocate with zero termination which allocateString does
    auto dst = allocator.getArena().allocateString((const char*)blob->getBufferPointer(), Count(size));
    return TerminatedCharSlice(dst, Count(size));
}

/* static */TerminatedCharSlice SliceUtil::toTerminatedCharSlice(StringBuilder& storage, ISlangBlob* blob)
{
    TerminatedCharSlice slice;
    if (SliceUtil::getTerminated(blob, slice))
    {
        return slice;
    }
    
    const auto size = blob->getBufferSize();
    auto chars = (const char*)blob->getBufferPointer();

    storage.Clear();
    storage.append(UnownedStringSlice(chars, size));
   
    return TerminatedCharSlice(storage.getBuffer(), Count(size));
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SliceAllocator !!!!!!!!!!!!!!!!!!!!!!!!!!! */

TerminatedCharSlice SliceAllocator::allocate(const char* in)
{
    const size_t length = ::strlen(in);
    auto dst = m_arena.allocateString(in, length);
    return TerminatedCharSlice(dst, length);
}

TerminatedCharSlice SliceAllocator::allocate(const UnownedStringSlice& slice)
{
    const auto length = slice.getLength();
    auto dst = m_arena.allocateString(slice.begin(), length);
    return TerminatedCharSlice(dst, length);
}

TerminatedCharSlice SliceAllocator::allocate(const Slice<char>& slice)
{
    const auto count = slice.count;
    auto dst = m_arena.allocateString(slice.begin(), count);
    return TerminatedCharSlice(dst, count);
}

Slice<TerminatedCharSlice> SliceAllocator::allocate(const List<String>& in)
{
    const auto count = in.getCount();
    if (count == 0)
    {
        return Slice<TerminatedCharSlice>(nullptr, 0);
    }

    auto dst = m_arena.allocateArray<TerminatedCharSlice>(count);
    for (Index i = 0; i < count; ++i)
    {
        dst[i] = allocate(in[i]);
    }

    return Slice<TerminatedCharSlice>(dst, count);
}

} // namespace Slang