Debian Codegraph @

scintilla/lexilla/lexlib/CharacterSet.h

raw
1 // Scintilla source code edit control
2 /** @file CharacterSet.h
3 ** Encapsulates a set of characters. Used to test if a character is within a set.
4 **/
5 // Copyright 2007 by Neil Hodgson <[email protected]>
6 // The License.txt file describes the conditions under which this software may be distributed.
7
8 #ifndef CHARACTERSET_H
9 #define CHARACTERSET_H
10
11 namespace Lexilla {
12
13 template<int N>
14 class CharacterSetArray {
15 unsigned char bset[(N-1)/8 + 1] = {};
16 bool valueAfter = false;
17 public:
18 enum setBase {
19 setNone=0,
20 setLower=1,
21 setUpper=2,
22 setDigits=4,
23 setAlpha=setLower|setUpper,
24 setAlphaNum=setAlpha|setDigits
25 };
26 CharacterSetArray(setBase base=setNone, const char *initialSet="", bool valueAfter_=false) noexcept {
27 valueAfter = valueAfter_;
28 AddString(initialSet);
29 if (base & setLower)
30 AddString("abcdefghijklmnopqrstuvwxyz");
31 if (base & setUpper)
32 AddString("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
33 if (base & setDigits)
34 AddString("0123456789");
35 }
36 CharacterSetArray(const char *initialSet, bool valueAfter_=false) noexcept :
37 CharacterSetArray(setNone, initialSet, valueAfter_) {
38 }
39 // For compatibility with previous version but should not be used in new code.
40 CharacterSetArray(setBase base, const char *initialSet, [[maybe_unused]]int size_, bool valueAfter_=false) noexcept :
41 CharacterSetArray(base, initialSet, valueAfter_) {
42 assert(size_ == N);
43 }
44 void Add(int val) noexcept {
45 assert(val >= 0);
46 assert(val < N);
47 bset[val >> 3] |= 1 << (val & 7);
48 }
49 void AddString(const char *setToAdd) noexcept {
50 for (const char *cp=setToAdd; *cp; cp++) {
51 const unsigned char uch = *cp;
52 assert(uch < N);
53 Add(uch);
54 }
55 }
56 bool Contains(int val) const noexcept {
57 assert(val >= 0);
58 if (val < 0) return false;
59 if (val >= N) return valueAfter;
60 return bset[val >> 3] & (1 << (val & 7));
61 }
62 bool Contains(char ch) const noexcept {
63 // Overload char as char may be signed
64 const unsigned char uch = ch;
65 return Contains(uch);
66 }
67 };
68
69 using CharacterSet = CharacterSetArray<0x80>;
70
71 // Functions for classifying characters
72
73 template <typename T, typename... Args>
74 constexpr bool AnyOf(T t, Args... args) noexcept {
75 #if defined(__clang__)
76 static_assert(__is_integral(T) || __is_enum(T));
77 #endif
78 return ((t == args) || ...);
79 }
80
81 // prevent pointer without <type_traits>
82 template <typename T, typename... Args>
83 constexpr void AnyOf([[maybe_unused]] T *t, [[maybe_unused]] Args... args) noexcept {}
84 template <typename T, typename... Args>
85 constexpr void AnyOf([[maybe_unused]] const T *t, [[maybe_unused]] Args... args) noexcept {}
86
87 constexpr bool IsASpace(int ch) noexcept {
88 return (ch == ' ') || ((ch >= 0x09) && (ch <= 0x0d));
89 }
90
91 constexpr bool IsASpaceOrTab(int ch) noexcept {
92 return (ch == ' ') || (ch == '\t');
93 }
94
95 constexpr bool IsADigit(int ch) noexcept {
96 return (ch >= '0') && (ch <= '9');
97 }
98
99 constexpr bool IsAHeXDigit(int ch) noexcept {
100 return (ch >= '0' && ch <= '9')
101 || (ch >= 'A' && ch <= 'F')
102 || (ch >= 'a' && ch <= 'f');
103 }
104
105 constexpr bool IsAnOctalDigit(int ch) noexcept {
106 return ch >= '0' && ch <= '7';
107 }
108
109 constexpr bool IsADigit(int ch, int base) noexcept {
110 if (base <= 10) {
111 return (ch >= '0') && (ch < '0' + base);
112 } else {
113 return ((ch >= '0') && (ch <= '9')) ||
114 ((ch >= 'A') && (ch < 'A' + base - 10)) ||
115 ((ch >= 'a') && (ch < 'a' + base - 10));
116 }
117 }
118
119 constexpr bool IsASCII(int ch) noexcept {
120 return (ch >= 0) && (ch < 0x80);
121 }
122
123 constexpr bool IsLowerCase(int ch) noexcept {
124 return (ch >= 'a') && (ch <= 'z');
125 }
126
127 constexpr bool IsUpperCase(int ch) noexcept {
128 return (ch >= 'A') && (ch <= 'Z');
129 }
130
131 constexpr bool IsUpperOrLowerCase(int ch) noexcept {
132 return IsUpperCase(ch) || IsLowerCase(ch);
133 }
134
135 constexpr bool IsAlphaNumeric(int ch) noexcept {
136 return
137 ((ch >= '0') && (ch <= '9')) ||
138 ((ch >= 'a') && (ch <= 'z')) ||
139 ((ch >= 'A') && (ch <= 'Z'));
140 }
141
142 /**
143 * Check if a character is a space.
144 * This is ASCII specific but is safe with chars >= 0x80.
145 */
146 constexpr bool isspacechar(int ch) noexcept {
147 return (ch == ' ') || ((ch >= 0x09) && (ch <= 0x0d));
148 }
149
150 constexpr bool iswordchar(int ch) noexcept {
151 return IsAlphaNumeric(ch) || ch == '.' || ch == '_';
152 }
153
154 constexpr bool iswordstart(int ch) noexcept {
155 return IsAlphaNumeric(ch) || ch == '_';
156 }
157
158 constexpr bool isoperator(int ch) noexcept {
159 if (IsAlphaNumeric(ch))
160 return false;
161 if (ch == '%' || ch == '^' || ch == '&' || ch == '*' ||
162 ch == '(' || ch == ')' || ch == '-' || ch == '+' ||
163 ch == '=' || ch == '|' || ch == '{' || ch == '}' ||
164 ch == '[' || ch == ']' || ch == ':' || ch == ';' ||
165 ch == '<' || ch == '>' || ch == ',' || ch == '/' ||
166 ch == '?' || ch == '!' || ch == '.' || ch == '~')
167 return true;
168 return false;
169 }
170
171 // Simple case functions for ASCII supersets.
172
173 template <typename T>
174 constexpr T MakeUpperCase(T ch) noexcept {
175 if (ch < 'a' || ch > 'z')
176 return ch;
177 else
178 return ch - 'a' + 'A';
179 }
180
181 template <typename T>
182 constexpr T MakeLowerCase(T ch) noexcept {
183 if (ch < 'A' || ch > 'Z')
184 return ch;
185 else
186 return ch - 'A' + 'a';
187 }
188
189 int CompareCaseInsensitive(const char *a, const char *b) noexcept;
190 bool EqualCaseInsensitive(std::string_view a, std::string_view b) noexcept;
191 int CompareNCaseInsensitive(const char *a, const char *b, size_t len) noexcept;
192
193 }
194
195 #endif