Skip to content

Commit 572dd54

Browse files
committed
Avoid some warnings in headers.
1 parent 38e92a9 commit 572dd54

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

lexlib/CharacterSet.h

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ namespace Lexilla {
1212

1313
template<int N>
1414
class 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;
1719
public:
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
8487
template <typename T, typename... Args>
8588
constexpr void AnyOf([[maybe_unused]] const T *t, [[maybe_unused]] Args... args) noexcept {}
8689

90+
constexpr int charTab = 0x09;
91+
constexpr int charCarriageReturn = 0x0D;
92+
8793
constexpr bool IsASpace(int ch) noexcept {
88-
return (ch == ' ') || ((ch >= 0x09) && (ch <= 0x0d));
94+
return (ch == ' ') || ((ch >= charTab) && (ch <= charCarriageReturn));
8995
}
9096

9197
constexpr bool IsASpaceOrTab(int ch) noexcept {
@@ -107,17 +113,18 @@ constexpr bool IsAnOctalDigit(int ch) noexcept {
107113
}
108114

109115
constexpr 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

119125
constexpr bool IsASCII(int ch) noexcept {
120-
return (ch >= 0) && (ch < 0x80);
126+
constexpr int lastASCII = 0x7F;
127+
return (ch >= 0) && (ch <= lastASCII);
121128
}
122129

123130
constexpr 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
*/
146153
constexpr bool isspacechar(int ch) noexcept {
147-
return (ch == ' ') || ((ch >= 0x09) && (ch <= 0x0d));
154+
return (ch == ' ') || ((ch >= charTab) && (ch <= charCarriageReturn));
148155
}
149156

150157
constexpr bool iswordchar(int ch) noexcept {
@@ -174,16 +181,14 @@ template <typename T>
174181
constexpr 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

181187
template <typename T>
182188
constexpr 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

189194
int CompareCaseInsensitive(const char *a, const char *b) noexcept;

lexlib/SparseState.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class SparseState {
6262
}
6363
}
6464
bool Delete(Sci_Position position) {
65-
typename stateVector::iterator low = Find(position);
65+
const typename stateVector::iterator low = Find(position);
6666
if (low != states.end()) {
6767
states.erase(low, states.end());
6868
return true;
@@ -80,7 +80,7 @@ class SparseState {
8080

8181
bool different = true;
8282
bool changed = false;
83-
typename stateVector::iterator low = Find(other.positionFirst);
83+
const typename stateVector::iterator low = Find(other.positionFirst);
8484
if (static_cast<size_t>(states.end() - low) == other.states.size()) {
8585
// Same number in other as after positionFirst in this
8686
different = !std::equal(low, states.end(), other.states.begin());

0 commit comments

Comments
 (0)