yum-mirror/slang

Making it easier to work with shaders

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

Ellie Hermaszewskaformatf65d756bf

master
3.9 KiB125 linesraw
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{
11    typedef uint8_t Flags;
12    struct Flag
13    {
14        enum Enum : Flags
15        {
16            Upper = 0x01, ///< A-Z
17            Lower = 0x02, ///< a-z
18            Digit = 0x04, ///< 0-9
19            HorizontalWhitespace =
20                0x08,        ///< Whitespace that can appear horizontally (ie excluding CR/LF)
21            HexDigit = 0x10, ///< 0-9, a-f, A-F
22            VerticalWhitespace = 0x20, ///< \n \r
23        };
24    };
25
26    SLANG_FORCE_INLINE static bool isDigit(char c) { return c >= '0' && c <= '9'; }
27    SLANG_FORCE_INLINE static bool isLower(char c) { return c >= 'a' && c <= 'z'; }
28    SLANG_FORCE_INLINE static bool isUpper(char c) { return c >= 'A' && c <= 'Z'; }
29    SLANG_FORCE_INLINE static bool isHorizontalWhitespace(char c) { return c == ' ' || c == '\t'; }
30    SLANG_FORCE_INLINE static bool isVerticalWhitespace(char c) { return c == '\n' || c == '\r'; }
31    SLANG_FORCE_INLINE static bool isWhitespace(char c)
32    {
33        return (getFlags(c) & (Flag::HorizontalWhitespace | Flag::VerticalWhitespace)) != 0;
34    }
35
36    /// True if it's alpha
37    SLANG_FORCE_INLINE static bool isAlpha(char c)
38    {
39        return (getFlags(c) & (Flag::Upper | Flag::Lower)) != 0;
40    }
41    /// True if it's alpha or a digit
42    SLANG_FORCE_INLINE static bool isAlphaOrDigit(char c)
43    {
44        return (getFlags(c) & (Flag::Upper | Flag::Lower | Flag::Digit)) != 0;
45    }
46
47    /// True if the character is a valid hex character
48    SLANG_FORCE_INLINE static bool isHexDigit(char c)
49    {
50        return (getFlags(c) & Flag::HexDigit) != 0;
51    }
52
53    /// True if the character is an octal digit
54    SLANG_FORCE_INLINE static bool isOctalDigit(char c) { return c >= '0' && c <= '7'; }
55
56    /// For a given character get the associated flags
57    SLANG_FORCE_INLINE static Flags getFlags(char c) { return g_charFlagMap.flags[size_t(c)]; }
58
59    /// Given a character return the lower case equivalent
60    SLANG_FORCE_INLINE static char toLower(char c)
61    {
62        return (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
63    }
64    /// Given a character return the upper case equivalent
65    SLANG_FORCE_INLINE static char toUpper(char c)
66    {
67        return (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.
71    SLANG_FORCE_INLINE static char getHexChar(Index i)
72    {
73        SLANG_ASSERT((i & ~Index(0xf)) == 0);
74        return 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
79    inline 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
83    inline 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
87    inline static int getOctalDigitValue(char c) { return isOctalDigit(c) ? (c - '0') : -1; }
88
89    struct CharFlagMap
90    {
91        Flags flags[0x100];
92    };
93
94    static 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.
100    static int _ensureLink();
101
102    static const CharFlagMap g_charFlagMap;
103};
104
105// ------------------------------------------------------------------------------------
106inline /* static */ int CharUtil::getHexDigitValue(char c)
107{
108    if (c >= '0' && c <= '9')
109    {
110        return c - '0';
111    }
112    else if (c >= 'a' && c <= 'f')
113    {
114        return c - 'a' + 10;
115    }
116    else if (c >= 'A' && c <= 'F')
117    {
118        return c - 'A' + 10;
119    }
120    return -1;
121}
122
123} // namespace Slang
124
125#endif // SLANG_CHAR_UTIL_H