-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_util.h
More file actions
46 lines (39 loc) · 1.2 KB
/
string_util.h
File metadata and controls
46 lines (39 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef __STRING_UTIL__
#define __STRING_UTIL__
// Our keyboard nowadays we use, we could type these chars for input.
// 95 chars with blackspace the first char.
// !"#$%&'()*+,-./0123456789:;<=>?
// ABCDEFGHIJKLMNOPQRSTUVWXYZ
// [\]^_`
// abcdefghijklmnopqrstuvwxyz
// {|}~
#define MAKE_LETTER_BITMAP(NAME, N) \
unsigned int NAME[N / 32 + 1]; \
int i = sizeof(NAME) / sizeof(unsigned int); \
while (i-- > 0) { NAME[i] = 0xFFFFFFFF; }
bool contains_char(const char* src, const char* sub) {
// letter map
MAKE_LETTER_BITMAP(index, 95);
unsigned int src_i[3] = {0, 0, 0};
unsigned int des_i[3] = {0, 0, 0};
while (*src) {
int i = ((*src) - ' ') / 32;
int j = ((*src) - ' ') % 32; // offset
unsigned int r = (index[i] & (1 << j));
src_i[i] |= r;
++src;
}
while (*sub) {
int i = ((*sub) - ' ') / 32;
int j = ((*sub) - ' ') % 32; // offset
unsigned int r = (index[i] & (1 << j));
des_i[i] |= r;
++sub;
}
// We could differ them from the sequence, only could give the result that
// chars in sub are all in src.
return (src_i[0] | des_i[0]) == src_i[0] &&
(src_i[1] | des_i[1]) == src_i[1] &&
(src_i[2] | des_i[2]) == src_i[2];
}
#endif