Repository navigation
Expand file tree
/
Copy pathtinyrulechecker.h
More file actions
232 lines (199 loc) · 6.96 KB
/
Copy pathtinyrulechecker.h
File metadata and controls
232 lines (199 loc) · 6.96 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// -----------------------------------------------------------------------------
// Copyright (C) 2024 Pau Sanchez
// MIT License
// -----------------------------------------------------------------------------
#ifndef __TinyRuleChecker_h__
#define __TinyRuleChecker_h__
#include <string>
#include <cstring>
#include <stdint.h>
#include <map>
#include <vector>
// -----------------------------------------------------------------------------
// FastStringLookup
//
// The main idea is to have a fast lookup table for strings, where we can just
// look for a hash lookup, if we don't find we know is not there, if we find
// and the index is less than the size of the lookup table, we can compare with
// the original string, if it is the same, then we have the index to the value;
// in any other case we'll need to use the lookup map.
//
// Note that this is not a normal hash table, because here in case of collissions
// we don't try to insert the new element anywhere, we just mark it as a collission
// which will get resolved by using the lookup map.
// -----------------------------------------------------------------------------
template<typename T>
class FastStringLookup {
public:
FastStringLookup() {
// up to X fast elements (still works with more, but slower)
_lookup.resize(1021, 0);
_lookupNames.resize(_lookup.size());
}
~FastStringLookup() {
clear();
}
void clear();
void set(const std::string &key, T value);
const T *get(const std::string &key) const;
const T *get(const std::string_view &key) const;
private:
static uint32_t _fnvHash32v(const uint8_t *data, size_t n);
std::map<std::string, uint32_t, std::less<>> _lookupMap;
std::vector<uint32_t> _lookup;
std::vector<std::string> _lookupNames;
std::vector<T> _values;
};
// -----------------------------------------------------------------------------
// TinyRuleChecker
// -----------------------------------------------------------------------------
class TinyRuleChecker {
public:
typedef enum {
V_TYPE_INT = 'i',
V_TYPE_FLOAT = 'f',
V_TYPE_STRING = 's',
V_TYPE_ARRAY = 'a'
} VarType;
typedef struct _VarValue {
VarType type;
int32_t intval;
float floatval;
std::string strval;
std::vector<_VarValue> array;
} VarValue;
typedef struct {
bool result;
std::string error;
} EvalResult;
typedef bool (*MethodOperator)(
const VarValue &v1,
const VarValue &v2,
EvalResult &result
);
TinyRuleChecker(bool defaultMethods = true);
~TinyRuleChecker();
void clearVars();
void setVarInt(const char *name, int value);
void setVarFloat(const char *name, float value);
void setVarString(const char *name, const char *value);
void clearMethods();
void initMethods();
void setMethod(const char *name, MethodOperator method);
EvalResult eval(const char *expr);
private:
typedef enum {
TK_UNKNOWN = 'u',
TK_ID = 'i',
TK_INT = 'n',
TK_FLOAT = 'f',
TK_RAW_STRING = 's',
TK_RAW_STRING_NO_ESCAPE = 'S', // no escape chars inside
TK_UNTERMINATED_STRING = 't',
TK_AND = '&',
TK_OR = '|',
TK_NOT = '!',
TK_DOT = '.',
TK_COMMA = ',',
TK_LPAR = '(',
TK_RPAR = ')',
TK_LBRACE = '[',
TK_RBRACE = ']',
TK_SPACE = ' ',
TK_EOF = 'e'
} TokenType;
public: static void __generateLookupTable();
typedef struct Token {
TokenType type;
std::string_view value;
union {
int32_t intval;
float floatval;
};
} Token;
typedef struct {
const char *next;
Token token;
bool result;
std::string error;
} ParseState;
FastStringLookup<VarValue> _variables;
FastStringLookup<MethodOperator> _methods;
std::string _stringifyToken(const Token &t);
const char *_nextToken(const char *expr, Token &t);
bool _peekToken(const char *expr, Token &t);
bool _parseExpr(ParseState &ps);
bool _parseStatement(ParseState &ps);
bool _parseValue(ParseState &ps, VarValue &v);
bool _evalStatement(ParseState &ps, const VarValue &v1, const std::string_view &method, const VarValue &v2);
};
// -----------------------------------------------------------------------------
// FastStringLookup<T>::_fnvHash32v
//
// FNV-1a hash function over a string/binary data
// -----------------------------------------------------------------------------
template<typename T>
uint32_t FastStringLookup<T>::_fnvHash32v(const uint8_t *data, size_t n) {
const uint32_t PRIME = 16777619;
uint32_t result = 0;
// continue iterating through the rest of the string
for(size_t i = 0; i < n; i++) {
result ^= data[i];
result *= PRIME;
}
return result;
}
// -----------------------------------------------------------------------------
// FastStringLookup<T>::clear
// -----------------------------------------------------------------------------
template<typename T>
void FastStringLookup<T>::clear() {
_lookupMap.clear();
_values.clear();
std::fill(_lookup.begin(), _lookup.end(), 0);
}
// -----------------------------------------------------------------------------
// FastStringLookup<T>::set
// -----------------------------------------------------------------------------
template<typename T>
void FastStringLookup<T>::set(const std::string &key, T value) {
uint32_t index = _values.size();
uint32_t qkey = _fnvHash32v((const uint8_t*)key.c_str(), key.size()) % _lookup.size();
_values.push_back(value);
_lookupMap[key] = index;
_lookup[qkey] = (_lookup[qkey] == 0 ? index + 1 : _lookup.size());
_lookupNames[qkey] = key;
}
// -----------------------------------------------------------------------------
// FastStringLookup<T>::get
// -----------------------------------------------------------------------------
template<typename T>
inline const T *FastStringLookup<T>::get(const std::string_view &key) const {
uint32_t qkey = _fnvHash32v((const uint8_t*)key.data(), key.size()) % _lookup.size();
uint32_t index = _lookup[qkey];
if (index == 0) {
return NULL;
}
else if (index < _lookup.size()) {
// validate we are talking about same name
if (_lookupNames[qkey].size() != key.size()) {
return NULL;
}
return (memcmp(_lookupNames[qkey].data(), key.data(), key.size()) == 0)
? &_values[index - 1] : NULL;
}
// index = _lookup.size() means duplicates, thus, we need to use lookup map
auto it = _lookupMap.find(key);
if (it == _lookupMap.end()) {
return NULL;
}
return &_values[it->second];
}
// -----------------------------------------------------------------------------
// FastStringLookup<T>::get
// -----------------------------------------------------------------------------
template<typename T>
const T *FastStringLookup<T>::get(const std::string &key) const {
return get(std::string_view(key));
}
#endif