yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
1#ifndef SLANG_CORE_CHAR_UTIL_H 2#define SLANG_CORE_CHAR_UTIL_H 3 4#include "slang-string.h" 5 6namespace Slang 7{ 8 9struct CharUtil 10{ 11typedef uint8_t Flags ; 12struct Flag 13 { 14enum Enum :Flags 15 { 16Upper = 0x01 ,///< A-Z 17Lower = 0x02 ,///< a-z 18Digit = 0x04 ,///< 0-9 19HorizontalWhitespace = 200x08 ,///< Whitespace that can appear horizontally (ie excluding CR/LF) 21HexDigit = 0x10 ,///< 0-9, a-f, A-F 22VerticalWhitespace = 0x20 ,///< \n \r 23 }; 24 }; 25 26SLANG_FORCE_INLINE static bool isDigit (char c ) {return c >= '0 '&& c <= '9 '; } 27SLANG_FORCE_INLINE static bool isLower (char c ) {return c >='a' && c <='z' ; } 28SLANG_FORCE_INLINE static bool isUpper (char c ) {return c >='A' && c <='Z' ; } 29SLANG_FORCE_INLINE static bool isHorizontalWhitespace (char c ) {return c == ' ' || c == '\t' ; } 30SLANG_FORCE_INLINE static bool isVerticalWhitespace (char c ) {return c == '\n' || c == '\r' ; } 31SLANG_FORCE_INLINE static bool isWhitespace (char c ) 32 { 33return (getFlags (c )& (Flag ::HorizontalWhitespace |Flag ::VerticalWhitespace ))!= 0 ; 34 } 35 36/// True if it's alpha 37SLANG_FORCE_INLINE static bool isAlpha (char c ) 38 { 39return (getFlags (c )& (Flag ::Upper |Flag ::Lower ))!= 0 ; 40 } 41/// True if it's alpha or a digit 42SLANG_FORCE_INLINE static bool isAlphaOrDigit (char c ) 43 { 44return (getFlags (c )& (Flag ::Upper |Flag ::Lower |Flag ::Digit ))!= 0 ; 45 } 46 47/// True if the character is a valid hex character 48SLANG_FORCE_INLINE static bool isHexDigit (char c ) 49 { 50return (getFlags (c )& Flag ::HexDigit )!= 0 ; 51 } 52 53/// True if the character is an octal digit 54SLANG_FORCE_INLINE static bool isOctalDigit (char c ) {return c >='0' && c <='7' ; } 55 56/// For a given character get the associated flags 57SLANG_FORCE_INLINE static Flags getFlags (char c ) {return g_charFlagMap .flags [size_t (c )]; } 58 59/// Given a character return the lower case equivalent 60SLANG_FORCE_INLINE static char toLower (char c ) 61 { 62return (c >='A' && c <='Z' ) ? (c - 'A' + 'a' ) :c ; 63 } 64/// Given a character return the upper case equivalent 65SLANG_FORCE_INLINE static char toUpper (char c ) 66 { 67return (c >='a' && c <='z' ) ? (c - 'a' + 'A' ) :c ; 68 } 69 70/// Given a value between 0-15 inclusive returns the hex digit. Uses lower case hex. 71SLANG_FORCE_INLINE static char getHexChar (Index i ) 72 { 73SLANG_ASSERT ((i & ~Index (0xf ))== 0 ); 74return char (i >=10 ? (i - 10 + 'a' ) : (i + '0' )); 75 } 76 77/// Returns the value if c interpretted as a decimal digit 78/// If c is not a valid digit returns -1 79inline static int getDecimalDigitValue (char c ) {return isDigit (c ) ? (c - '0' ) :-1 ; } 80 81/// Returns the value if c interpretted as a hex digit 82/// If c is not a valid hex returns -1 83inline static int getHexDigitValue (char c ); 84 85/// Returns the value if c interpretted as a octal digit 86/// If c is not a valid octal returns -1 87inline static int getOctalDigitValue (char c ) {return isOctalDigit (c ) ? (c - '0' ) :-1 ; } 88 89struct CharFlagMap 90 { 91Flags flags [0x100 ]; 92 }; 93 94static CharFlagMap makeCharFlagMap (); 95 96// HACK! 97// JS: Many of the inlined functions of CharUtil just access a global map. That referencing this 98// global is *NOT* enough to link correctly with CharUtil on linux for a shared library. Caling 99// this function can force linkage. 100static int _ensureLink (); 101 102static const CharFlagMap g_charFlagMap ; 103}; 104 105// ------------------------------------------------------------------------------------ 106inline /* static */ int CharUtil ::getHexDigitValue (char c ) 107{ 108if (c >='0' && c <='9' ) 109 { 110return c - '0' ; 111 } 112else if (c >='a' && c <='f' ) 113 { 114return c - 'a' + 10 ; 115 } 116else if (c >='A' && c <='F' ) 117 { 118return c - 'A' + 10 ; 119 } 120return -1 ; 121} 122 123}// namespace Slang 124 125#endif // SLANG_CHAR_UTIL_H