@@ -12,7 +12,9 @@ namespace Lexilla {
1212
1313template <int N>
1414class CharacterSetArray {
15- unsigned char bset[(N-1 )/8 + 1 ] = {};
15+ static constexpr int bitsPerChar = 8 ;
16+ static constexpr int mask3Bits = 7 ;
17+ unsigned char bset[((N-1 )/bitsPerChar) + 1 ] = {};
1618 bool valueAfter = false ;
1719public:
1820 enum setBase {
@@ -23,7 +25,7 @@ class CharacterSetArray {
2325 setAlpha=setLower|setUpper,
2426 setAlphaNum=setAlpha|setDigits
2527 };
26- CharacterSetArray (setBase base=setNone, const char *initialSet=" " , bool valueAfter_=false ) noexcept {
28+ explicit CharacterSetArray (setBase base=setNone, const char *initialSet=" " , bool valueAfter_=false ) noexcept {
2729 valueAfter = valueAfter_;
2830 AddString (initialSet);
2931 if (base & setLower)
@@ -33,7 +35,7 @@ class CharacterSetArray {
3335 if (base & setDigits)
3436 AddString (" 0123456789" );
3537 }
36- CharacterSetArray (const char *initialSet, bool valueAfter_=false ) noexcept :
38+ explicit CharacterSetArray (const char *initialSet, bool valueAfter_=false ) noexcept :
3739 CharacterSetArray(setNone, initialSet, valueAfter_) {
3840 }
3941 // For compatibility with previous version but should not be used in new code.
@@ -44,7 +46,7 @@ class CharacterSetArray {
4446 void Add (int val) noexcept {
4547 assert (val >= 0 );
4648 assert (val < N);
47- bset[val >> 3 ] |= 1 << (val & 7 );
49+ bset[val >> 3 ] |= 1 << (val & mask3Bits );
4850 }
4951 void AddString (const char *setToAdd) noexcept {
5052 for (const char *cp=setToAdd; *cp; cp++) {
@@ -53,20 +55,21 @@ class CharacterSetArray {
5355 Add (uch);
5456 }
5557 }
56- bool Contains (int val) const noexcept {
58+ [[nodiscard]] bool Contains (int val) const noexcept {
5759 assert (val >= 0 );
5860 if (val < 0 ) return false ;
5961 if (val >= N) return valueAfter;
60- return bset[val >> 3 ] & (1 << (val & 7 ));
62+ return bset[val >> 3 ] & (1 << (val & mask3Bits ));
6163 }
62- bool Contains (char ch) const noexcept {
64+ [[nodiscard]] bool Contains (char ch) const noexcept {
6365 // Overload char as char may be signed
6466 const unsigned char uch = ch;
6567 return Contains (uch);
6668 }
6769};
6870
69- using CharacterSet = CharacterSetArray<0x80 >;
71+ constexpr int countASCII = 0x80 ;
72+ using CharacterSet = CharacterSetArray<countASCII>;
7073
7174// Functions for classifying characters
7275
@@ -84,8 +87,11 @@ constexpr void AnyOf([[maybe_unused]] T *t, [[maybe_unused]] Args... args) noexc
8487template <typename T, typename ... Args>
8588constexpr void AnyOf ([[maybe_unused]] const T *t, [[maybe_unused]] Args... args) noexcept {}
8689
90+ constexpr int charTab = 0x09 ;
91+ constexpr int charCarriageReturn = 0x0D ;
92+
8793constexpr bool IsASpace (int ch) noexcept {
88- return (ch == ' ' ) || ((ch >= 0x09 ) && (ch <= 0x0d ));
94+ return (ch == ' ' ) || ((ch >= charTab ) && (ch <= charCarriageReturn ));
8995}
9096
9197constexpr bool IsASpaceOrTab (int ch) noexcept {
@@ -107,17 +113,18 @@ constexpr bool IsAnOctalDigit(int ch) noexcept {
107113}
108114
109115constexpr bool IsADigit (int ch, int base) noexcept {
110- if (base <= 10 ) {
116+ constexpr int digits = 10 ;
117+ if (base <= digits) {
111118 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 ));
116119 }
120+ return ((ch >= ' 0' ) && (ch <= ' 9' )) ||
121+ ((ch >= ' A' ) && (ch < ' A' + base - digits)) ||
122+ ((ch >= ' a' ) && (ch < ' a' + base - digits));
117123}
118124
119125constexpr bool IsASCII (int ch) noexcept {
120- return (ch >= 0 ) && (ch < 0x80 );
126+ constexpr int lastASCII = 0x7F ;
127+ return (ch >= 0 ) && (ch <= lastASCII);
121128}
122129
123130constexpr bool IsLowerCase (int ch) noexcept {
@@ -144,7 +151,7 @@ constexpr bool IsAlphaNumeric(int ch) noexcept {
144151 * This is ASCII specific but is safe with chars >= 0x80.
145152 */
146153constexpr bool isspacechar (int ch) noexcept {
147- return (ch == ' ' ) || ((ch >= 0x09 ) && (ch <= 0x0d ));
154+ return (ch == ' ' ) || ((ch >= charTab ) && (ch <= charCarriageReturn ));
148155}
149156
150157constexpr bool iswordchar (int ch) noexcept {
@@ -174,16 +181,14 @@ template <typename T>
174181constexpr T MakeUpperCase (T ch) noexcept {
175182 if (ch < ' a' || ch > ' z' )
176183 return ch;
177- else
178- return ch - ' a' + ' A' ;
184+ return ch - ' a' + ' A' ;
179185}
180186
181187template <typename T>
182188constexpr T MakeLowerCase (T ch) noexcept {
183189 if (ch < ' A' || ch > ' Z' )
184190 return ch;
185- else
186- return ch - ' A' + ' a' ;
191+ return ch - ' A' + ' a' ;
187192}
188193
189194int CompareCaseInsensitive (const char *a, const char *b) noexcept ;
0 commit comments