yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
4.6 KiB135 linesraw
1#ifndef SLANG_COMPILER_CORE_NAME_CONVENTION_UTIL_H
2#define SLANG_COMPILER_CORE_NAME_CONVENTION_UTIL_H
3
4#include "../core/slang-list.h"
5#include "../core/slang-string.h"
6
7namespace Slang
8{
9
10typedef uint8_t NameConventionBackingType;
11
12enum class NameStyle : NameConventionBackingType
13{
14    Unknown, /// Unknown style
15    Kabab,   /// Words are separated with -. WORDS-ARE-SEPARATED, words-are-separted
16    Snake,   /// Words are separated with _. WORDS_ARE_SEPARATED, words_are_separated
17    Camel,   /// Words start with a capital. (Upper will make first words character capitalized, aka
18             /// PascalCase)
19};
20
21struct NameConventionFlag
22{
23    enum Enum : NameConventionBackingType
24    {
25        UpperCase = 0x80,
26    };
27};
28
29struct NameConventionMask
30{
31    enum Enum : NameConventionBackingType
32    {
33        Style = 0x7,
34    };
35};
36
37enum class NameConvention : NameConventionBackingType
38{
39    Invalid = NameConventionBackingType(NameStyle::Unknown),
40
41    LowerKabab = NameConventionBackingType(NameStyle::Kabab),
42    LowerSnake = NameConventionBackingType(NameStyle::Snake),
43    LowerCamel = NameConventionBackingType(NameStyle::Camel),
44
45    UpperKabab = NameConventionBackingType(NameStyle::Kabab) | NameConventionFlag::UpperCase,
46    UpperSnake = NameConventionBackingType(NameStyle::Snake) | NameConventionFlag::UpperCase,
47    UpperCamel = NameConventionBackingType(NameStyle::Camel) | NameConventionFlag::UpperCase,
48};
49
50SLANG_FORCE_INLINE NameConvention makeUpper(NameStyle style)
51{
52    return NameConvention(NameConventionBackingType(style) | NameConventionFlag::UpperCase);
53}
54SLANG_FORCE_INLINE NameConvention makeLower(NameStyle style)
55{
56    return NameConvention(style);
57}
58
59SLANG_FORCE_INLINE bool isUpper(NameConvention convention)
60{
61    return (NameConventionBackingType(convention) & NameConventionFlag::UpperCase) != 0;
62}
63SLANG_FORCE_INLINE bool isLower(NameConvention convention)
64{
65    return (NameConventionBackingType(convention) & NameConventionFlag::UpperCase) == 0;
66}
67SLANG_FORCE_INLINE NameStyle getNameStyle(NameConvention convention)
68{
69    return NameStyle(NameConventionBackingType(convention) & NameConventionMask::Style);
70}
71
72/* This utility is to enable easy conversion and interpretation of names that use standard
73conventions, typically in programming languages. The conventions are largely how to represent
74multiple words together.
75
76Split is used to split up a name into it's constituent 'words' based on a convention.
77Join is used to combine words based on a convention/character case
78
79Convert uses split and join to allow easy conversion between conventions.
80*/
81struct NameConventionUtil
82{
83    /// Given a slice tries to determine the convention used.
84    /// If no separators are found, will assume Camel
85    /// Doesn't exhaustively test the string slice, or determine invalid scenarios
86    /// Use 'getConvention' to get error checking
87    static NameStyle inferStyleFromText(const UnownedStringSlice& slice);
88
89    /// Gets the naming convention based on the slice.
90    /// Will return invalid convention if cannot be determined.
91    ///
92    /// TODO(JS):
93    /// Does not handle leading `_` styles: "_a" and "_1" will be invalid.
94    /// We may want to make it do so, but requires changes in infer, split and join.
95    static NameConvention inferConventionFromText(const UnownedStringSlice& slice);
96
97    /// Given a slice and a naming convention, split into it's constituent parts. If convention
98    /// isn't specified, will infer from slice using getConvention.
99    static void split(
100        NameStyle nameStyle,
101        const UnownedStringSlice& slice,
102        List<UnownedStringSlice>& out);
103    static void split(const UnownedStringSlice& slice, List<UnownedStringSlice>& out);
104
105    /// Given slices, join together with the specified convention into out
106    static void join(
107        const UnownedStringSlice* slices,
108        Index slicesCount,
109        NameConvention convention,
110        StringBuilder& out);
111
112    /// Join with a join char, and potentially changing case of input slices
113    static void join(
114        const UnownedStringSlice* slices,
115        Index slicesCount,
116        NameConvention convention,
117        char joinChar,
118        StringBuilder& out);
119
120    /// Convert from one convention to another. If fromConvention isn't specified, will infer from
121    /// slice using getConvention.
122    static void convert(
123        NameStyle fromStyle,
124        const UnownedStringSlice& slice,
125        NameConvention toConvention,
126        StringBuilder& out);
127    static void convert(
128        const UnownedStringSlice& slice,
129        NameConvention toConvention,
130        StringBuilder& out);
131};
132
133} // namespace Slang
134
135#endif // SLANG_COMPILER_CORE_NAME_CONVENTION_UTIL_H