summaryrefslogtreecommitdiffstats
path: root/source/compiler-core/slang-name-convention-util.cpp
blob: 3c57f8bc4e5f9708c360230facafab92ccf0e548 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "slang-name-convention-util.h"

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

namespace Slang
{

/* static */ NameConvention NameConventionUtil::inferConventionFromText(
    const UnownedStringSlice& slice)
{
    // If no chars, or first char isn't alpha we don't know what it is
    if (slice.getLength() <= 0 || !CharUtil::isAlpha(slice[0]))
    {
        return NameConvention::Invalid;
    }

    typedef int Flags;
    struct Flag
    {
        enum Enum : Flags
        {
            Underscore = 0x1,
            Dash = 0x2,
            Upper = 0x4,
            Lower = 0x8,
        };
    };

    Flags flags = 0;

    for (const char c : slice)
    {
        switch (c)
        {
        case '-':
            flags |= Flag::Dash;
            break;
        case '_':
            flags |= Flag::Underscore;
            break;
        default:
            {
                if (CharUtil::isLower(c))
                {
                    flags |= Flag::Lower;
                }
                else if (CharUtil::isUpper(c))
                {
                    flags |= Flag::Upper;
                }
                else if (CharUtil::isDigit(c))
                {
                    // Is okay as long as not first char (which we already tested is alpha)
                }
                else
                {
                    // Don't know what this style is
                    return NameConvention::Invalid;
                }
            }
        }
    }

    // Use flags to determine what convention is used
    switch (flags)
    {
    // We'll assume it's lower camel.
    case Flag::Lower:
        return NameConvention::LowerCamel;
    // We'll assume it's upper snake. It almost certainly isn't camel, and snake is more usual
    // than kabab.
    case Flag::Upper:
        return NameConvention::UpperSnake;
    case Flag::Upper | Flag::Lower:
        {
            // Looks like camel, choose the right case based on first char
            return CharUtil::isUpper(slice[0]) ? NameConvention::UpperCamel
                                               : NameConvention::LowerCamel;
        }
    case Flag::Lower | Flag::Dash:
        return NameConvention::LowerKabab;
    case Flag::Upper | Flag::Dash:
        return NameConvention::UpperKabab;
    case Flag::Lower | Flag::Underscore:
        return NameConvention::LowerSnake;
    case Flag::Upper | Flag::Underscore:
        return NameConvention::UpperSnake;
    default:
        break;
    }

    // Don't know what this style is
    return NameConvention::Invalid;
}

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

/* static */ void NameConventionUtil::split(
    NameStyle style,
    const UnownedStringSlice& slice,
    List<UnownedStringSlice>& out)
{
    switch (style)
    {
    case NameStyle::Kabab:
        {
            StringUtil::split(slice, '-', out);
            break;
        }
    case NameStyle::Snake:
        {
            StringUtil::split(slice, '_', out);
            break;
        }
    case NameStyle::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;
        }
    case NameStyle::Unknown:
        {
            out.add(slice);
            break;
        }
    }
}

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

/* static */ void NameConventionUtil::join(
    const UnownedStringSlice* slices,
    Index slicesCount,
    NameConvention convention,
    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;

    const bool upper = isUpper(convention);

    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;
        }

        if (upper)
        {
            for (Index j = 0; j < count; ++j)
            {
                dst[j] = CharUtil::toUpper(src[j]);
            }
        }
        else
        {
            for (Index j = 0; j < count; ++j)
            {
                dst[j] = CharUtil::toLower(src[j]);
            }
        }

        dst += count;
    }

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

/* static */ void NameConventionUtil::join(
    const UnownedStringSlice* slices,
    Index slicesCount,
    NameConvention convention,
    StringBuilder& out)
{
    const auto style = getNameStyle(convention);

    switch (style)
    {
    case NameStyle::Kabab:
        return join(slices, slicesCount, convention, '-', out);
    case NameStyle::Snake:
        return join(slices, slicesCount, convention, '_', out);
    case NameStyle::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 && isLower(convention)))
                {
                    // 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;
            }

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

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

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

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

} // namespace Slang