yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
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{ 14Unknown ,/// Unknown style 15Kabab ,/// Words are separated with -. WORDS-ARE-SEPARATED, words-are-separted 16Snake ,/// Words are separated with _. WORDS_ARE_SEPARATED, words_are_separated 17Camel ,/// Words start with a capital. (Upper will make first words character capitalized, aka 18/// PascalCase) 19}; 20 21struct NameConventionFlag 22{ 23enum Enum :NameConventionBackingType 24 { 25UpperCase = 0x80 , 26 }; 27}; 28 29struct NameConventionMask 30{ 31enum Enum :NameConventionBackingType 32 { 33Style = 0x7 , 34 }; 35}; 36 37enum class NameConvention :NameConventionBackingType 38{ 39Invalid = NameConventionBackingType (NameStyle ::Unknown ), 40 41LowerKabab = NameConventionBackingType (NameStyle ::Kabab ), 42LowerSnake = NameConventionBackingType (NameStyle ::Snake ), 43LowerCamel = NameConventionBackingType (NameStyle ::Camel ), 44 45UpperKabab = NameConventionBackingType (NameStyle ::Kabab ) |NameConventionFlag ::UpperCase , 46UpperSnake = NameConventionBackingType (NameStyle ::Snake ) |NameConventionFlag ::UpperCase , 47UpperCamel = NameConventionBackingType (NameStyle ::Camel ) |NameConventionFlag ::UpperCase , 48}; 49 50SLANG_FORCE_INLINE NameConvention makeUpper (NameStyle style ) 51{ 52return NameConvention (NameConventionBackingType (style ) |NameConventionFlag ::UpperCase ); 53} 54SLANG_FORCE_INLINE NameConvention makeLower (NameStyle style ) 55{ 56return NameConvention (style ); 57} 58 59SLANG_FORCE_INLINE bool isUpper (NameConvention convention ) 60{ 61return (NameConventionBackingType (convention )& NameConventionFlag ::UpperCase )!= 0 ; 62} 63SLANG_FORCE_INLINE bool isLower (NameConvention convention ) 64{ 65return (NameConventionBackingType (convention )& NameConventionFlag ::UpperCase )== 0 ; 66} 67SLANG_FORCE_INLINE NameStyle getNameStyle (NameConvention convention ) 68{ 69return 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 87static 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. 95static 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. 99static void split ( 100NameStyle nameStyle , 101const UnownedStringSlice & slice , 102List < UnownedStringSlice >& out ); 103static void split (const UnownedStringSlice & slice ,List < UnownedStringSlice >& out ); 104 105/// Given slices, join together with the specified convention into out 106static void join ( 107const UnownedStringSlice * slices , 108Index slicesCount , 109NameConvention convention , 110StringBuilder & out ); 111 112/// Join with a join char, and potentially changing case of input slices 113static void join ( 114const UnownedStringSlice * slices , 115Index slicesCount , 116NameConvention convention , 117char joinChar , 118StringBuilder & out ); 119 120/// Convert from one convention to another. If fromConvention isn't specified, will infer from 121/// slice using getConvention. 122static void convert ( 123NameStyle fromStyle , 124const UnownedStringSlice & slice , 125NameConvention toConvention , 126StringBuilder & out ); 127static void convert ( 128const UnownedStringSlice & slice , 129NameConvention toConvention , 130StringBuilder & out ); 131}; 132 133}// namespace Slang 134 135#endif // SLANG_COMPILER_CORE_NAME_CONVENTION_UTIL_H