Line | Branch | Exec | Source |
---|---|---|---|
1 | #ifndef SYNTAX_BITSET_H | ||
2 | #define SYNTAX_BITSET_H | ||
3 | |||
4 | #include <limits.h> | ||
5 | #include <stdbool.h> | ||
6 | #include <stddef.h> | ||
7 | #include "util/macros.h" | ||
8 | |||
9 | #define BITSET_WORD_BITS (sizeof(BitSetWord) * CHAR_BIT) | ||
10 | #define BITSET_BIT_MASK (BITSET_WORD_BITS - 1) | ||
11 | #define BITSET_NR_WORDS(bits) (((bits) + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS) | ||
12 | #define BITSET_INVERT(set) bitset_invert(set, ARRAYLEN(set)) | ||
13 | |||
14 | typedef unsigned long BitSetWord; | ||
15 | |||
16 | 360 | static inline bool bitset_contains(const BitSetWord *set, unsigned char ch) | |
17 | { | ||
18 | 360 | unsigned int word = ch / BITSET_WORD_BITS; | |
19 | 360 | unsigned int bit = ch & BITSET_BIT_MASK; | |
20 | 360 | return set[word] & ((BitSetWord)1) << bit; | |
21 | } | ||
22 | |||
23 | 16540 | static inline void bitset_add(BitSetWord *set, unsigned char ch) | |
24 | { | ||
25 | 16540 | unsigned int word = ch / BITSET_WORD_BITS; | |
26 | 16540 | unsigned int bit = ch & BITSET_BIT_MASK; | |
27 | 16540 | set[word] |= ((BitSetWord)1) << bit; | |
28 | 16540 | } | |
29 | |||
30 | 782 | static inline void bitset_add_char_range(BitSetWord *set, const unsigned char *r) | |
31 | { | ||
32 |
2/2✓ Branch 0 taken 2105 times.
✓ Branch 1 taken 782 times.
|
2887 | for (size_t i = 0; r[i]; i++) { |
33 | 2105 | unsigned int ch = r[i]; | |
34 | 2105 | bitset_add(set, ch); | |
35 |
4/4✓ Branch 0 taken 947 times.
✓ Branch 1 taken 1158 times.
✓ Branch 2 taken 898 times.
✓ Branch 3 taken 49 times.
|
2105 | if (r[i + 1] == '-' && r[i + 2]) { |
36 | // Add char range | ||
37 |
2/2✓ Branch 0 taken 14435 times.
✓ Branch 1 taken 898 times.
|
15333 | for (ch = ch + 1; ch <= r[i + 2]; ch++) { |
38 | 14435 | bitset_add(set, ch); | |
39 | } | ||
40 | i += 2; | ||
41 | } | ||
42 | } | ||
43 | 782 | } | |
44 | |||
45 | 44 | static inline void bitset_invert(BitSetWord *set, size_t nr_words) | |
46 | { | ||
47 |
2/2✓ Branch 0 taken 176 times.
✓ Branch 1 taken 44 times.
|
220 | for (size_t i = 0; i < nr_words; i++) { |
48 | 176 | set[i] = ~set[i]; | |
49 | } | ||
50 | 44 | } | |
51 | |||
52 | #endif | ||
53 |