cleanup and optimization
Costa Tsaousis (ktsaou) committed
Dec 9, 2017 at 15:41 UTC
7249dcb37d7ddb2e20f7f2d9730a28e15d5d7053
2 files changed
+13
-18
src/simple_pattern.c
+10
-15
@@ -144,10 +144,10 @@ static inline char *add_wildcarded(const char *matched, size_t matched_size, cha
144
// fprintf(stderr, "ADD WILDCARDED '%s' of length %zu\n", buf, matched_size);
145
//}
146
147
- if(matched && *matched && matched_size && wildcarded && *wildcarded_size) {
147
+ if(unlikely(wildcarded && *wildcarded_size && matched && *matched && matched_size)) {
148
size_t wss = *wildcarded_size - 1;
149
size_t len = (matched_size < wss)?matched_size:wss;
150
- if(len) {
150
+ if(likely(len)) {
151
strncpyz(wildcarded, matched, len);
152
153
*wildcarded_size -= len;
@@ -158,37 +158,36 @@ static inline char *add_wildcarded(const char *matched, size_t matched_size, cha
158
return wildcarded;
159
}
160
161
-static inline int match_pattern(struct simple_pattern *m, const char *str, size_t len, char *wildcarded, const size_t *wildcarded_size) {
162
- char *s, *ws = wildcarded;
163
- size_t wss = *wildcarded_size;
161
+static inline int match_pattern(struct simple_pattern *m, const char *str, size_t len, char *wildcarded, size_t *wildcarded_size) {
162
+ char *s;
163
164
if(m->len <= len) {
165
switch(m->mode) {
166
case SIMPLE_PATTERN_SUBSTRING:
167
if(!m->len) return 1;
168
if((s = strstr(str, m->match))) {
170
- ws = add_wildcarded(str, s - str, ws, &wss);
169
+ wildcarded = add_wildcarded(str, s - str, wildcarded, wildcarded_size);
170
if(!m->child) {
172
- ws = add_wildcarded(&s[m->len], len - (&s[m->len] - str), ws, &wss);
171
+ wildcarded = add_wildcarded(&s[m->len], len - (&s[m->len] - str), wildcarded, wildcarded_size);
172
return 1;
173
}
175
- return match_pattern(m->child, &s[m->len], len - (s - str) - m->len, ws, &wss);
174
+ return match_pattern(m->child, &s[m->len], len - (s - str) - m->len, wildcarded, wildcarded_size);
175
}
176
break;
177
178
case SIMPLE_PATTERN_PREFIX:
179
if(unlikely(strncmp(str, m->match, m->len) == 0)) {
180
if(!m->child) {
182
- ws = add_wildcarded(&str[m->len], len - m->len, ws, &wss);
181
+ wildcarded = add_wildcarded(&str[m->len], len - m->len, wildcarded, wildcarded_size);
182
return 1;
183
}
185
- return match_pattern(m->child, &str[m->len], len - m->len, ws, &wss);
184
+ return match_pattern(m->child, &str[m->len], len - m->len, wildcarded, wildcarded_size);
185
}
186
break;
187
188
case SIMPLE_PATTERN_SUFFIX:
189
if(unlikely(strcmp(&str[len - m->len], m->match) == 0)) {
191
- ws = add_wildcarded(str, len - m->len, ws, &wss);
190
+ wildcarded = add_wildcarded(str, len - m->len, wildcarded, wildcarded_size);
191
if(!m->child) return 1;
192
return 0;
193
}
@@ -231,10 +230,6 @@ int simple_pattern_matches_extract(SIMPLE_PATTERN *list, const char *str, char *
230
return 0;
231
}
232
234
-int simple_pattern_matches(SIMPLE_PATTERN *list, const char *str) {
235
- return simple_pattern_matches_extract(list, str, NULL, 0);
236
-}
237
-
233
static inline void free_pattern(struct simple_pattern *m) {
234
if(!m) return;
235
src/simple_pattern.h
+3
-3
@@ -15,12 +15,12 @@ typedef void SIMPLE_PATTERN;
15
// should be considered PREFIX matches.
16
extern SIMPLE_PATTERN *simple_pattern_create(const char *list, SIMPLE_PREFIX_MODE default_mode);
17
18
-// test if string str is matched from the pattern
19
-extern int simple_pattern_matches(SIMPLE_PATTERN *list, const char *str);
20
-
18
// test if string str is matched from the pattern and fill 'wildcarded' with the parts matched by '*'
19
extern int simple_pattern_matches_extract(SIMPLE_PATTERN *list, const char *str, char *wildcarded, size_t wildcarded_size);
20
21
+// test if string str is matched from the pattern
22
+#define simple_pattern_matches(list, str) simple_pattern_matches_extract(list, str, NULL, 0)
23
+
24
// free a simple_pattern that was created with simple_pattern_create()
25
// list can be NULL, in which case, this does nothing.
26
extern void simple_pattern_free(SIMPLE_PATTERN *list);