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
|
#ifndef SLANG_COMPILER_CORE_NAME_CONVENTION_UTIL_H
#define SLANG_COMPILER_CORE_NAME_CONVENTION_UTIL_H
#include "../core/slang-list.h"
#include "../core/slang-string.h"
namespace Slang
{
typedef uint8_t NameConventionBackingType;
enum class NameStyle : NameConventionBackingType
{
Unknown, /// Unknown style
Kabab, /// Words are separated with -. WORDS-ARE-SEPARATED, words-are-separted
Snake, /// Words are separated with _. WORDS_ARE_SEPARATED, words_are_separated
Camel, /// Words start with a capital. (Upper will make first words character capitalized, aka
/// PascalCase)
};
struct NameConventionFlag
{
enum Enum : NameConventionBackingType
{
UpperCase = 0x80,
};
};
struct NameConventionMask
{
enum Enum : NameConventionBackingType
{
Style = 0x7,
};
};
enum class NameConvention : NameConventionBackingType
{
Invalid = NameConventionBackingType(NameStyle::Unknown),
LowerKabab = NameConventionBackingType(NameStyle::Kabab),
LowerSnake = NameConventionBackingType(NameStyle::Snake),
LowerCamel = NameConventionBackingType(NameStyle::Camel),
UpperKabab = NameConventionBackingType(NameStyle::Kabab) | NameConventionFlag::UpperCase,
UpperSnake = NameConventionBackingType(NameStyle::Snake) | NameConventionFlag::UpperCase,
UpperCamel = NameConventionBackingType(NameStyle::Camel) | NameConventionFlag::UpperCase,
};
SLANG_FORCE_INLINE NameConvention makeUpper(NameStyle style)
{
return NameConvention(NameConventionBackingType(style) | NameConventionFlag::UpperCase);
}
SLANG_FORCE_INLINE NameConvention makeLower(NameStyle style)
{
return NameConvention(style);
}
SLANG_FORCE_INLINE bool isUpper(NameConvention convention)
{
return (NameConventionBackingType(convention) & NameConventionFlag::UpperCase) != 0;
}
SLANG_FORCE_INLINE bool isLower(NameConvention convention)
{
return (NameConventionBackingType(convention) & NameConventionFlag::UpperCase) == 0;
}
SLANG_FORCE_INLINE NameStyle getNameStyle(NameConvention convention)
{
return NameStyle(NameConventionBackingType(convention) & NameConventionMask::Style);
}
/* This utility is to enable easy conversion and interpretation of names that use standard
conventions, typically in programming languages. The conventions are largely how to represent
multiple words together.
Split is used to split up a name into it's constituent 'words' based on a convention.
Join is used to combine words based on a convention/character case
Convert uses split and join to allow easy conversion between conventions.
*/
struct NameConventionUtil
{
/// Given a slice tries to determine the convention used.
/// If no separators are found, will assume Camel
/// Doesn't exhaustively test the string slice, or determine invalid scenarios
/// Use 'getConvention' to get error checking
static NameStyle inferStyleFromText(const UnownedStringSlice& slice);
/// Gets the naming convention based on the slice.
/// Will return invalid convention if cannot be determined.
///
/// TODO(JS):
/// Does not handle leading `_` styles: "_a" and "_1" will be invalid.
/// We may want to make it do so, but requires changes in infer, split and join.
static NameConvention inferConventionFromText(const UnownedStringSlice& slice);
/// Given a slice and a naming convention, split into it's constituent parts. If convention
/// isn't specified, will infer from slice using getConvention.
static void split(
NameStyle nameStyle,
const UnownedStringSlice& slice,
List<UnownedStringSlice>& out);
static void split(const UnownedStringSlice& slice, List<UnownedStringSlice>& out);
/// Given slices, join together with the specified convention into out
static void join(
const UnownedStringSlice* slices,
Index slicesCount,
NameConvention convention,
StringBuilder& out);
/// Join with a join char, and potentially changing case of input slices
static void join(
const UnownedStringSlice* slices,
Index slicesCount,
NameConvention convention,
char joinChar,
StringBuilder& out);
/// Convert from one convention to another. If fromConvention isn't specified, will infer from
/// slice using getConvention.
static void convert(
NameStyle fromStyle,
const UnownedStringSlice& slice,
NameConvention toConvention,
StringBuilder& out);
static void convert(
const UnownedStringSlice& slice,
NameConvention toConvention,
StringBuilder& out);
};
} // namespace Slang
#endif // SLANG_COMPILER_CORE_NAME_CONVENTION_UTIL_H
|