summaryrefslogtreecommitdiffstats
path: root/source/compiler-core/slang-name-convention-util.cpp
blob: 930e00e1855054f7dae74513a9a90baa0b173c13 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "slang-name-convention-util.h"

#include "../core/slang-char-util.h"
#include "../core/slang-string-util.h"

namespace Slang
{

/* static */NameConvention NameConventionUtil::getConvention(const UnownedStringSlice& slice)
{
    for (const char c : slice)
    {
        switch (c)
        {
            case '-':   return NameConvention::Kabab;
            case '_':   return NameConvention::Snake;
            default: break;
        }
    }
    return NameConvention::Camel;
}

/* static */void NameConventionUtil::split(NameConvention convention, const UnownedStringSlice& slice, List<UnownedStringSlice>& out)
{
    switch (convention)
    {
        case NameConvention::Kabab:
        {
            StringUtil::split(slice, '-', out);
            break;
        }
        case NameConvention::Snake:
        {
            StringUtil::split(slice, '_', out);
            break;
        }
        case NameConvention::Camel:
        {
            typedef CharUtil::Flags CharFlags;
            typedef CharUtil::Flag CharFlag;

            CharFlags prevFlags = 0;
            const char*const end = slice.end();

            const char* start = slice.begin();
            for (const char* cur = start; cur < end; ++cur)
            {
                const char c = *cur;
                const CharUtil::Flags flags = CharUtil::getFlags(c);

                if (flags & CharFlag::Upper)
                {
                    if (prevFlags & CharFlag::Lower)
                    {
                        // If we go from lower to upper, we have a transition
                        out.add(UnownedStringSlice(start, cur));
                        start = cur;
                    }
                    else if ((prevFlags & CharFlag::Upper) && cur + 1 < end)
                    {
                        // This works with capital or uncapitalized acronyms, but if we have two capitalized acronyms following each other - it can't split.
                        // 
                        // For example 
                        // "IAABBSystem" -> "IAABB", "System"
                        // 
                        // If it only accepted lower case acronyms the logic could be changed such that the following could be produced
                        // "IAabbSystem" -> "I", "Aabb", "System" 
                        //
                        // Since Slang source largely goes with upper case acronyms, we work with the heuristic here..

                        if (CharUtil::isLower(cur[1]))
                        {
                            out.add(UnownedStringSlice(start, cur));
                            start = cur;
                        }
                    }
                }
                
                prevFlags = flags;
            }

            // Add any end section
            if (start < end)
            {
                out.add(UnownedStringSlice(start, end));
            }
            break;
        }
    }
}

void NameConventionUtil::split(const UnownedStringSlice& slice, List<UnownedStringSlice>& out)
{
    split(getConvention(slice), slice, out);
}

/* static */void NameConventionUtil::join(const UnownedStringSlice* slices, Index slicesCount, CharCase charCase, char joinChar, StringBuilder& out)
{
    if (slicesCount <= 0)
    {
        return;
    }

    Index totalSize = slicesCount - 1;
    for (Index i = 0; i < slicesCount; ++i)
    {
        totalSize += slices[i].getLength();
    }

    char*const dstStart = out.prepareForAppend(totalSize);
    char* dst = dstStart;

    for (Index i = 0; i < slicesCount; ++i)
    {
        const UnownedStringSlice& slice = slices[i];
        const Index count = slice.getLength();
        const char*const src = slice.begin();

        if (i > 0)
        {
            *dst++ = joinChar;
        }

        switch (charCase)
        {
            case CharCase::Upper:
            {
                for (Index j = 0; j < count; ++j)
                {
                    dst[j] = CharUtil::toUpper(src[j]);
                }
                break;
            }
            case CharCase::Lower:
            {
                for (Index j = 0; j < count; ++j)
                {
                    dst[j] = CharUtil::toLower(src[j]);
                }
                break;
            }
        }

        dst += count;
    }

    SLANG_ASSERT(dstStart + totalSize == dst);
    out.appendInPlace(dstStart, totalSize);
}

/* static */void NameConventionUtil::join(const UnownedStringSlice* slices, Index slicesCount, CharCase charCase, NameConvention convention, StringBuilder& out)
{
    switch (convention)
    {
        case NameConvention::Kabab:        return join(slices, slicesCount, charCase, '-', out);
        case NameConvention::Snake:        return join(slices, slicesCount, charCase, '_', out);
        case NameConvention::Camel:
        {
            Index totalSize = 0;

            for (Index i = 0; i < slicesCount; ++i)
            {
                totalSize += slices[i].getLength();
            }

            char*const dstStart = out.prepareForAppend(totalSize);
            char* dst = dstStart;

            for (Index i = 0; i < slicesCount; ++i)
            {
                const UnownedStringSlice& slice = slices[i];
                Index count = slice.getLength();
                const char* src = slice.begin();

                Int j = 0;

                if (count > 0 && !(i == 0 && charCase == CharCase::Lower))
                {
                    // Capitalize first letter of each word, unless on first word and 'lower'
                    dst[j] = CharUtil::toUpper(src[j]);
                    j++;
                }

                for (; j < count; ++j)
                {
                    dst[j] = CharUtil::toLower(src[j]);
                }

                dst += count;
            }
            break;
        }
    }
}

/* static */void NameConventionUtil::convert(NameConvention fromConvention, const UnownedStringSlice& slice, CharCase charCase, NameConvention toConvention, StringBuilder& out)
{
    // Split into slices
    List<UnownedStringSlice> slices;
    split(fromConvention, slice, slices);

    // Join the slices in the toConvention
    join(slices.getBuffer(), slices.getCount(), charCase, toConvention, out);
}

/* static */void NameConventionUtil::convert(const UnownedStringSlice& slice, CharCase charCase, NameConvention toConvention, StringBuilder& out)
{
    convert(getConvention(slice), slice, charCase, toConvention, out);
}

}