diff options
Diffstat (limited to 'src/regex/regex_test_lib.c')
-rw-r--r-- | src/regex/regex_test_lib.c | 518 |
1 files changed, 260 insertions, 258 deletions
diff --git a/src/regex/regex_test_lib.c b/src/regex/regex_test_lib.c index d8eb22370..15d6c653a 100644 --- a/src/regex/regex_test_lib.c +++ b/src/regex/regex_test_lib.c | |||
@@ -32,7 +32,8 @@ | |||
32 | /** | 32 | /** |
33 | * Struct to hold the tree formed by prefix-combining the regexes. | 33 | * Struct to hold the tree formed by prefix-combining the regexes. |
34 | */ | 34 | */ |
35 | struct RegexCombineCtx { | 35 | struct RegexCombineCtx |
36 | { | ||
36 | /** | 37 | /** |
37 | * Child nodes with same prefix and token. | 38 | * Child nodes with same prefix and token. |
38 | */ | 39 | */ |
@@ -61,34 +62,34 @@ struct RegexCombineCtx { | |||
61 | * @return Int in range [0, (base-1)] | 62 | * @return Int in range [0, (base-1)] |
62 | */ | 63 | */ |
63 | static int | 64 | static int |
64 | c2i(char c, int size) | 65 | c2i (char c, int size) |
65 | { | 66 | { |
66 | switch (size) | 67 | switch (size) |
67 | { | 68 | { |
68 | case 2: | 69 | case 2: |
69 | case 8: | 70 | case 8: |
71 | return c - '0'; | ||
72 | break; | ||
73 | |||
74 | case 16: | ||
75 | if ((c >= '0') &&(c <= '9') ) | ||
70 | return c - '0'; | 76 | return c - '0'; |
71 | break; | 77 | else if ((c >= 'A') &&(c <= 'F') ) |
72 | 78 | return c - 'A' + 10; | |
73 | case 16: | 79 | else if ((c >= 'a') &&(c <= 'f') ) |
74 | if (c >= '0' && c <= '9') | 80 | return c - 'a' + 10; |
75 | return c - '0'; | 81 | else |
76 | else if (c >= 'A' && c <= 'F') | 82 | { |
77 | return c - 'A' + 10; | 83 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
78 | else if (c >= 'a' && c <= 'f') | 84 | "Cannot convert char %c in base %u\n", |
79 | return c - 'a' + 10; | 85 | c, size); |
80 | else | 86 | GNUNET_assert (0); |
81 | { | ||
82 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, | ||
83 | "Cannot convert char %c in base %u\n", | ||
84 | c, size); | ||
85 | GNUNET_assert(0); | ||
86 | } | ||
87 | break; | ||
88 | |||
89 | default: | ||
90 | GNUNET_assert(0); | ||
91 | } | 87 | } |
88 | break; | ||
89 | |||
90 | default: | ||
91 | GNUNET_assert (0); | ||
92 | } | ||
92 | } | 93 | } |
93 | 94 | ||
94 | 95 | ||
@@ -98,10 +99,10 @@ c2i(char c, int size) | |||
98 | * @param n Indentation level | 99 | * @param n Indentation level |
99 | */ | 100 | */ |
100 | static void | 101 | static void |
101 | space(int n) | 102 | space (int n) |
102 | { | 103 | { |
103 | for (int i = 0; i < n; i++) | 104 | for (int i = 0; i < n; i++) |
104 | fprintf(stderr, "| "); | 105 | fprintf (stderr, "| "); |
105 | } | 106 | } |
106 | 107 | ||
107 | 108 | ||
@@ -112,25 +113,25 @@ space(int n) | |||
112 | * @param level Indentation level to start with | 113 | * @param level Indentation level to start with |
113 | */ | 114 | */ |
114 | static void | 115 | static void |
115 | debugctx(struct RegexCombineCtx *ctx, int level) | 116 | debugctx (struct RegexCombineCtx *ctx, int level) |
116 | { | 117 | { |
117 | #if DEBUG_REGEX | 118 | #if DEBUG_REGEX |
118 | if (NULL != ctx->s) | 119 | if (NULL != ctx->s) |
119 | { | 120 | { |
120 | space(level - 1); | 121 | space (level - 1); |
121 | fprintf(stderr, "%u:'%s'\n", c2i(ctx->s[0], ctx->size), ctx->s); | 122 | fprintf (stderr, "%u:'%s'\n", c2i (ctx->s[0], ctx->size), ctx->s); |
122 | } | 123 | } |
123 | else | 124 | else |
124 | fprintf(stderr, "ROOT (base %u)\n", ctx->size); | 125 | fprintf (stderr, "ROOT (base %u)\n", ctx->size); |
125 | for (unsigned int i = 0; i < ctx->size; i++) | 126 | for (unsigned int i = 0; i < ctx->size; i++) |
127 | { | ||
128 | if (NULL != ctx->children[i]) | ||
126 | { | 129 | { |
127 | if (NULL != ctx->children[i]) | 130 | space (level); |
128 | { | 131 | debugctx (ctx->children[i], level + 1); |
129 | space(level); | ||
130 | debugctx(ctx->children[i], level + 1); | ||
131 | } | ||
132 | } | 132 | } |
133 | fflush(stderr); | 133 | } |
134 | fflush (stderr); | ||
134 | #endif | 135 | #endif |
135 | } | 136 | } |
136 | 137 | ||
@@ -142,8 +143,8 @@ debugctx(struct RegexCombineCtx *ctx, int level) | |||
142 | * @param regex Regex to add. | 143 | * @param regex Regex to add. |
143 | */ | 144 | */ |
144 | static void | 145 | static void |
145 | regex_add(struct RegexCombineCtx *ctx, | 146 | regex_add (struct RegexCombineCtx *ctx, |
146 | const char *regex); | 147 | const char *regex); |
147 | 148 | ||
148 | 149 | ||
149 | /** | 150 | /** |
@@ -152,14 +153,14 @@ regex_add(struct RegexCombineCtx *ctx, | |||
152 | * @param alphabet_size Size of the alphabet (and the Trie array) | 153 | * @param alphabet_size Size of the alphabet (and the Trie array) |
153 | */ | 154 | */ |
154 | static struct RegexCombineCtx * | 155 | static struct RegexCombineCtx * |
155 | new_regex_ctx(unsigned int alphabet_size) | 156 | new_regex_ctx (unsigned int alphabet_size) |
156 | { | 157 | { |
157 | struct RegexCombineCtx *ctx; | 158 | struct RegexCombineCtx *ctx; |
158 | size_t array_size; | 159 | size_t array_size; |
159 | 160 | ||
160 | array_size = sizeof(struct RegexCombineCtx *) * alphabet_size; | 161 | array_size = sizeof(struct RegexCombineCtx *) * alphabet_size; |
161 | ctx = GNUNET_new(struct RegexCombineCtx); | 162 | ctx = GNUNET_new (struct RegexCombineCtx); |
162 | ctx->children = GNUNET_malloc(array_size); | 163 | ctx->children = GNUNET_malloc (array_size); |
163 | ctx->size = alphabet_size; | 164 | ctx->size = alphabet_size; |
164 | 165 | ||
165 | return ctx; | 166 | return ctx; |
@@ -167,19 +168,19 @@ new_regex_ctx(unsigned int alphabet_size) | |||
167 | 168 | ||
168 | 169 | ||
169 | static void | 170 | static void |
170 | move_children(struct RegexCombineCtx *dst, | 171 | move_children (struct RegexCombineCtx *dst, |
171 | const struct RegexCombineCtx *src) | 172 | const struct RegexCombineCtx *src) |
172 | { | 173 | { |
173 | size_t array_size; | 174 | size_t array_size; |
174 | 175 | ||
175 | array_size = sizeof(struct RegexCombineCtx *) * src->size; | 176 | array_size = sizeof(struct RegexCombineCtx *) * src->size; |
176 | GNUNET_memcpy(dst->children, | 177 | GNUNET_memcpy (dst->children, |
177 | src->children, | 178 | src->children, |
178 | array_size); | 179 | array_size); |
179 | for (unsigned int i = 0; i < src->size; i++) | 180 | for (unsigned int i = 0; i < src->size; i++) |
180 | { | 181 | { |
181 | src->children[i] = NULL; | 182 | src->children[i] = NULL; |
182 | } | 183 | } |
183 | } | 184 | } |
184 | 185 | ||
185 | 186 | ||
@@ -191,7 +192,7 @@ move_children(struct RegexCombineCtx *dst, | |||
191 | * @return Regex that matches any of the added regexes. | 192 | * @return Regex that matches any of the added regexes. |
192 | */ | 193 | */ |
193 | static char * | 194 | static char * |
194 | regex_combine(struct RegexCombineCtx *ctx) | 195 | regex_combine (struct RegexCombineCtx *ctx) |
195 | { | 196 | { |
196 | struct RegexCombineCtx *p; | 197 | struct RegexCombineCtx *p; |
197 | unsigned int i; | 198 | unsigned int i; |
@@ -201,56 +202,57 @@ regex_combine(struct RegexCombineCtx *ctx) | |||
201 | char *s; | 202 | char *s; |
202 | int opt; | 203 | int opt; |
203 | 204 | ||
204 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "new combine %s\n", ctx->s); | 205 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new combine %s\n", ctx->s); |
205 | regex = GNUNET_strdup(""); | 206 | regex = GNUNET_strdup (""); |
206 | opt = GNUNET_NO; | 207 | opt = GNUNET_NO; |
207 | for (i = 0; i < ctx->size; i++) | 208 | for (i = 0; i < ctx->size; i++) |
209 | { | ||
210 | p = ctx->children[i]; | ||
211 | if (NULL == p) | ||
212 | continue; | ||
213 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
214 | "adding '%s' to innner %s\n", | ||
215 | p->s, ctx->s); | ||
216 | s = regex_combine (p); | ||
217 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " total '%s'\n", s); | ||
218 | if (strlen (s) == 0) | ||
208 | { | 219 | { |
209 | p = ctx->children[i]; | 220 | opt = GNUNET_YES; |
210 | if (NULL == p) | ||
211 | continue; | ||
212 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | ||
213 | "adding '%s' to innner %s\n", | ||
214 | p->s, ctx->s); | ||
215 | s = regex_combine(p); | ||
216 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " total '%s'\n", s); | ||
217 | if (strlen(s) == 0) | ||
218 | { | ||
219 | opt = GNUNET_YES; | ||
220 | } | ||
221 | else | ||
222 | { | ||
223 | GNUNET_asprintf(&tmp, "%s%s|", regex, s); | ||
224 | GNUNET_free_non_null(regex); | ||
225 | regex = tmp; | ||
226 | } | ||
227 | GNUNET_free_non_null(s); | ||
228 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " so far '%s' for inner %s\n", regex, ctx->s); | ||
229 | } | 221 | } |
230 | 222 | else | |
231 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "opt: %d, innner: '%s'\n", opt, regex); | ||
232 | len = strlen(regex); | ||
233 | if (0 == len) | ||
234 | { | 223 | { |
235 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "empty, returning ''\n"); | 224 | GNUNET_asprintf (&tmp, "%s%s|", regex, s); |
236 | GNUNET_free(regex); | 225 | GNUNET_free_non_null (regex); |
237 | return NULL == ctx->s ? NULL : GNUNET_strdup(ctx->s); | 226 | regex = tmp; |
238 | } | 227 | } |
228 | GNUNET_free_non_null (s); | ||
229 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " so far '%s' for inner %s\n", regex, | ||
230 | ctx->s); | ||
231 | } | ||
232 | |||
233 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "opt: %d, innner: '%s'\n", opt, regex); | ||
234 | len = strlen (regex); | ||
235 | if (0 == len) | ||
236 | { | ||
237 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "empty, returning ''\n"); | ||
238 | GNUNET_free (regex); | ||
239 | return NULL == ctx->s ? NULL : GNUNET_strdup (ctx->s); | ||
240 | } | ||
239 | 241 | ||
240 | if ('|' == regex[len - 1]) | 242 | if ('|' == regex[len - 1]) |
241 | regex[len - 1] = '\0'; | 243 | regex[len - 1] = '\0'; |
242 | 244 | ||
243 | if (NULL != ctx->s) | 245 | if (NULL != ctx->s) |
244 | { | 246 | { |
245 | if (opt) | 247 | if (opt) |
246 | GNUNET_asprintf(&s, "%s(%s)?", ctx->s, regex); | 248 | GNUNET_asprintf (&s, "%s(%s)?", ctx->s, regex); |
247 | else | 249 | else |
248 | GNUNET_asprintf(&s, "%s(%s)", ctx->s, regex); | 250 | GNUNET_asprintf (&s, "%s(%s)", ctx->s, regex); |
249 | GNUNET_free(regex); | 251 | GNUNET_free (regex); |
250 | regex = s; | 252 | regex = s; |
251 | } | 253 | } |
252 | 254 | ||
253 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "partial: %s\n", regex); | 255 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "partial: %s\n", regex); |
254 | return regex; | 256 | return regex; |
255 | } | 257 | } |
256 | 258 | ||
@@ -264,22 +266,22 @@ regex_combine(struct RegexCombineCtx *ctx) | |||
264 | * @return Number of characters of matching prefix. | 266 | * @return Number of characters of matching prefix. |
265 | */ | 267 | */ |
266 | static unsigned int | 268 | static unsigned int |
267 | get_prefix_length(const char *s1, const char *s2) | 269 | get_prefix_length (const char *s1, const char *s2) |
268 | { | 270 | { |
269 | unsigned int l1; | 271 | unsigned int l1; |
270 | unsigned int l2; | 272 | unsigned int l2; |
271 | unsigned int limit; | 273 | unsigned int limit; |
272 | unsigned int i; | 274 | unsigned int i; |
273 | 275 | ||
274 | l1 = strlen(s1); | 276 | l1 = strlen (s1); |
275 | l2 = strlen(s2); | 277 | l2 = strlen (s2); |
276 | limit = l1 > l2 ? l2 : l1; | 278 | limit = l1 > l2 ? l2 : l1; |
277 | 279 | ||
278 | for (i = 0; i < limit; i++) | 280 | for (i = 0; i < limit; i++) |
279 | { | 281 | { |
280 | if (s1[i] != s2[i]) | 282 | if (s1[i] != s2[i]) |
281 | return i; | 283 | return i; |
282 | } | 284 | } |
283 | return limit; | 285 | return limit; |
284 | } | 286 | } |
285 | 287 | ||
@@ -294,7 +296,7 @@ get_prefix_length(const char *s1, const char *s2) | |||
294 | * @return Child with the longest prefix, NULL if no child matches. | 296 | * @return Child with the longest prefix, NULL if no child matches. |
295 | */ | 297 | */ |
296 | static struct RegexCombineCtx * | 298 | static struct RegexCombineCtx * |
297 | get_longest_prefix(struct RegexCombineCtx *ctx, const char *regex) | 299 | get_longest_prefix (struct RegexCombineCtx *ctx, const char *regex) |
298 | { | 300 | { |
299 | struct RegexCombineCtx *p; | 301 | struct RegexCombineCtx *p; |
300 | struct RegexCombineCtx *best; | 302 | struct RegexCombineCtx *best; |
@@ -306,26 +308,26 @@ get_longest_prefix(struct RegexCombineCtx *ctx, const char *regex) | |||
306 | best = NULL; | 308 | best = NULL; |
307 | 309 | ||
308 | for (i = 0; i < ctx->size; i++) | 310 | for (i = 0; i < ctx->size; i++) |
311 | { | ||
312 | p = ctx->children[i]; | ||
313 | if (NULL == p) | ||
314 | continue; | ||
315 | |||
316 | l = get_prefix_length (p->s, regex); | ||
317 | if (l > best_l) | ||
309 | { | 318 | { |
310 | p = ctx->children[i]; | 319 | GNUNET_break (0 == best_l); |
311 | if (NULL == p) | 320 | best = p; |
312 | continue; | 321 | best_l = l; |
313 | |||
314 | l = get_prefix_length(p->s, regex); | ||
315 | if (l > best_l) | ||
316 | { | ||
317 | GNUNET_break(0 == best_l); | ||
318 | best = p; | ||
319 | best_l = l; | ||
320 | } | ||
321 | } | 322 | } |
323 | } | ||
322 | return best; | 324 | return best; |
323 | } | 325 | } |
324 | 326 | ||
325 | static void | 327 | static void |
326 | regex_add_multiple(struct RegexCombineCtx *ctx, | 328 | regex_add_multiple (struct RegexCombineCtx *ctx, |
327 | const char *regex, | 329 | const char *regex, |
328 | struct RegexCombineCtx **children) | 330 | struct RegexCombineCtx **children) |
329 | { | 331 | { |
330 | char tmp[2]; | 332 | char tmp[2]; |
331 | long unsigned int i; | 333 | long unsigned int i; |
@@ -334,43 +336,43 @@ regex_add_multiple(struct RegexCombineCtx *ctx, | |||
334 | unsigned int count; | 336 | unsigned int count; |
335 | 337 | ||
336 | if ('(' != regex[0]) | 338 | if ('(' != regex[0]) |
337 | { | 339 | { |
338 | GNUNET_assert(0); | 340 | GNUNET_assert (0); |
339 | } | 341 | } |
340 | 342 | ||
341 | /* Does the regex cover *all* possible children? Then don't add any, | 343 | /* Does the regex cover *all* possible children? Then don't add any, |
342 | * as it will be covered by the post-regex "(a-z)*" | 344 | * as it will be covered by the post-regex "(a-z)*" |
343 | */ | 345 | */ |
344 | l = strlen(regex); | 346 | l = strlen (regex); |
345 | count = 0; | 347 | count = 0; |
346 | for (i = 1UL; i < l; i++) | 348 | for (i = 1UL; i < l; i++) |
349 | { | ||
350 | if ((regex[i] != '|') &&(regex[i] != ')') ) | ||
347 | { | 351 | { |
348 | if (regex[i] != '|' && regex[i] != ')') | 352 | count++; |
349 | { | ||
350 | count++; | ||
351 | } | ||
352 | } | 353 | } |
354 | } | ||
353 | if (count == ctx->size) | 355 | if (count == ctx->size) |
354 | { | 356 | { |
355 | return; | 357 | return; |
356 | } | 358 | } |
357 | 359 | ||
358 | /* Add every component as a child node */ | 360 | /* Add every component as a child node */ |
359 | tmp[1] = '\0'; | 361 | tmp[1] = '\0'; |
360 | for (i = 1UL; i < l; i++) | 362 | for (i = 1UL; i < l; i++) |
363 | { | ||
364 | if ((regex[i] != '|') &&(regex[i] != ')') ) | ||
361 | { | 365 | { |
362 | if (regex[i] != '|' && regex[i] != ')') | 366 | tmp[0] = regex[i]; |
363 | { | 367 | newctx = new_regex_ctx (ctx->size); |
364 | tmp[0] = regex[i]; | 368 | newctx->s = GNUNET_strdup (tmp); |
365 | newctx = new_regex_ctx(ctx->size); | 369 | if (children != NULL) |
366 | newctx->s = GNUNET_strdup(tmp); | 370 | GNUNET_memcpy (newctx->children, |
367 | if (children != NULL) | 371 | children, |
368 | GNUNET_memcpy(newctx->children, | 372 | sizeof(*children) * ctx->size); |
369 | children, | 373 | ctx->children[c2i (tmp[0], ctx->size)] = newctx; |
370 | sizeof(*children) * ctx->size); | ||
371 | ctx->children[c2i(tmp[0], ctx->size)] = newctx; | ||
372 | } | ||
373 | } | 374 | } |
375 | } | ||
374 | } | 376 | } |
375 | 377 | ||
376 | /** | 378 | /** |
@@ -384,19 +386,19 @@ regex_add_multiple(struct RegexCombineCtx *ctx, | |||
384 | * @param prefix_l Lenght of common prefix of the new regex and @a ctx->s | 386 | * @param prefix_l Lenght of common prefix of the new regex and @a ctx->s |
385 | */ | 387 | */ |
386 | static void | 388 | static void |
387 | regex_split(struct RegexCombineCtx *ctx, | 389 | regex_split (struct RegexCombineCtx *ctx, |
388 | unsigned int len, | 390 | unsigned int len, |
389 | unsigned int prefix_l) | 391 | unsigned int prefix_l) |
390 | { | 392 | { |
391 | struct RegexCombineCtx *newctx; | 393 | struct RegexCombineCtx *newctx; |
392 | unsigned int idx; | 394 | unsigned int idx; |
393 | char *suffix; | 395 | char *suffix; |
394 | 396 | ||
395 | suffix = GNUNET_malloc(len - prefix_l + 1); | 397 | suffix = GNUNET_malloc (len - prefix_l + 1); |
396 | /* | 398 | /* |
397 | * We can use GNUNET_strlcpy because ctx->s is null-terminated | 399 | * We can use GNUNET_strlcpy because ctx->s is null-terminated |
398 | */ | 400 | */ |
399 | GNUNET_strlcpy(suffix, &ctx->s[prefix_l], len - prefix_l + 1); | 401 | GNUNET_strlcpy (suffix, &ctx->s[prefix_l], len - prefix_l + 1); |
400 | 402 | ||
401 | /* Suffix saved, truncate current node so it only contains the prefix, | 403 | /* Suffix saved, truncate current node so it only contains the prefix, |
402 | * copy any children nodes to put as grandchildren and initialize new empty | 404 | * copy any children nodes to put as grandchildren and initialize new empty |
@@ -406,22 +408,22 @@ regex_split(struct RegexCombineCtx *ctx, | |||
406 | 408 | ||
407 | /* If the suffix is an OR expression, add multiple children */ | 409 | /* If the suffix is an OR expression, add multiple children */ |
408 | if ('(' == suffix[0]) | 410 | if ('(' == suffix[0]) |
409 | { | 411 | { |
410 | struct RegexCombineCtx **tmp; | 412 | struct RegexCombineCtx **tmp; |
411 | 413 | ||
412 | tmp = ctx->children; | 414 | tmp = ctx->children; |
413 | ctx->children = GNUNET_malloc(sizeof(*tmp) * ctx->size); | 415 | ctx->children = GNUNET_malloc (sizeof(*tmp) * ctx->size); |
414 | regex_add_multiple(ctx, suffix, tmp); | 416 | regex_add_multiple (ctx, suffix, tmp); |
415 | GNUNET_free(suffix); | 417 | GNUNET_free (suffix); |
416 | GNUNET_free(tmp); | 418 | GNUNET_free (tmp); |
417 | return; | 419 | return; |
418 | } | 420 | } |
419 | 421 | ||
420 | /* The suffix is a normal string, add as one node */ | 422 | /* The suffix is a normal string, add as one node */ |
421 | newctx = new_regex_ctx(ctx->size); | 423 | newctx = new_regex_ctx (ctx->size); |
422 | newctx->s = suffix; | 424 | newctx->s = suffix; |
423 | move_children(newctx, ctx); | 425 | move_children (newctx, ctx); |
424 | idx = c2i(suffix[0], ctx->size); | 426 | idx = c2i (suffix[0], ctx->size); |
425 | ctx->children[idx] = newctx; | 427 | ctx->children[idx] = newctx; |
426 | } | 428 | } |
427 | 429 | ||
@@ -433,7 +435,7 @@ regex_split(struct RegexCombineCtx *ctx, | |||
433 | * @param regex Regex to add. | 435 | * @param regex Regex to add. |
434 | */ | 436 | */ |
435 | static void | 437 | static void |
436 | regex_add(struct RegexCombineCtx *ctx, const char *regex) | 438 | regex_add (struct RegexCombineCtx *ctx, const char *regex) |
437 | { | 439 | { |
438 | struct RegexCombineCtx *p; | 440 | struct RegexCombineCtx *p; |
439 | struct RegexCombineCtx *newctx; | 441 | struct RegexCombineCtx *newctx; |
@@ -444,54 +446,54 @@ regex_add(struct RegexCombineCtx *ctx, const char *regex) | |||
444 | size_t len; | 446 | size_t len; |
445 | int idx; | 447 | int idx; |
446 | 448 | ||
447 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | 449 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
448 | "regex_add '%s' into '%s'\n", | 450 | "regex_add '%s' into '%s'\n", |
449 | regex, ctx->s); | 451 | regex, ctx->s); |
450 | l = strlen(regex); | 452 | l = strlen (regex); |
451 | if (0UL == l) | 453 | if (0UL == l) |
452 | return; | 454 | return; |
453 | 455 | ||
454 | /* If the regex is in the form of (a|b|c), add every character separately */ | 456 | /* If the regex is in the form of (a|b|c), add every character separately */ |
455 | if ('(' == regex[0]) | 457 | if ('(' == regex[0]) |
456 | { | 458 | { |
457 | regex_add_multiple(ctx, regex, NULL); | 459 | regex_add_multiple (ctx, regex, NULL); |
458 | return; | 460 | return; |
459 | } | 461 | } |
460 | 462 | ||
461 | p = get_longest_prefix(ctx, regex); | 463 | p = get_longest_prefix (ctx, regex); |
462 | if (NULL != p) | 464 | if (NULL != p) |
465 | { | ||
466 | /* There is some prefix match, reduce regex and try again */ | ||
467 | prefix_l = get_prefix_length (p->s, regex); | ||
468 | rest_s = &p->s[prefix_l]; | ||
469 | rest_r = ®ex[prefix_l]; | ||
470 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "chosen '%s' [%u]\n", p->s, prefix_l); | ||
471 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "prefix r '%.*s'\n", prefix_l, p->s); | ||
472 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "rest r '%s'\n", rest_r); | ||
473 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "rest s '%s'\n", rest_s); | ||
474 | len = strlen (p->s); | ||
475 | if (prefix_l < len) | ||
463 | { | 476 | { |
464 | /* There is some prefix match, reduce regex and try again */ | 477 | regex_split (p, len, prefix_l); |
465 | prefix_l = get_prefix_length(p->s, regex); | ||
466 | rest_s = &p->s[prefix_l]; | ||
467 | rest_r = ®ex[prefix_l]; | ||
468 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "chosen '%s' [%u]\n", p->s, prefix_l); | ||
469 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "prefix r '%.*s'\n", prefix_l, p->s); | ||
470 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "rest r '%s'\n", rest_r); | ||
471 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "rest s '%s'\n", rest_s); | ||
472 | len = strlen(p->s); | ||
473 | if (prefix_l < len) | ||
474 | { | ||
475 | regex_split(p, len, prefix_l); | ||
476 | } | ||
477 | regex_add(p, rest_r); | ||
478 | return; | ||
479 | } | 478 | } |
479 | regex_add (p, rest_r); | ||
480 | return; | ||
481 | } | ||
480 | 482 | ||
481 | /* There is no prefix match, add new */ | 483 | /* There is no prefix match, add new */ |
482 | idx = c2i(regex[0], ctx->size); | 484 | idx = c2i (regex[0], ctx->size); |
483 | if (NULL == ctx->children[idx] && NULL != ctx->s) | 485 | if ((NULL == ctx->children[idx])&&(NULL != ctx->s)) |
484 | { | 486 | { |
485 | /* this was the end before, add empty string */ | 487 | /* this was the end before, add empty string */ |
486 | newctx = new_regex_ctx(ctx->size); | 488 | newctx = new_regex_ctx (ctx->size); |
487 | newctx->s = GNUNET_strdup(""); | 489 | newctx->s = GNUNET_strdup (""); |
488 | ctx->children[idx] = newctx; | 490 | ctx->children[idx] = newctx; |
489 | } | 491 | } |
490 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " no match\n"); | 492 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no match\n"); |
491 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " new state %s\n", regex); | 493 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " new state %s\n", regex); |
492 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " under %s\n", ctx->s); | 494 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " under %s\n", ctx->s); |
493 | newctx = new_regex_ctx(ctx->size); | 495 | newctx = new_regex_ctx (ctx->size); |
494 | newctx->s = GNUNET_strdup(regex); | 496 | newctx->s = GNUNET_strdup (regex); |
495 | ctx->children[idx] = newctx; | 497 | ctx->children[idx] = newctx; |
496 | } | 498 | } |
497 | 499 | ||
@@ -502,7 +504,7 @@ regex_add(struct RegexCombineCtx *ctx, const char *regex) | |||
502 | * @param ctx Context to free. | 504 | * @param ctx Context to free. |
503 | */ | 505 | */ |
504 | static void | 506 | static void |
505 | regex_ctx_destroy(struct RegexCombineCtx *ctx) | 507 | regex_ctx_destroy (struct RegexCombineCtx *ctx) |
506 | { | 508 | { |
507 | unsigned int i; | 509 | unsigned int i; |
508 | 510 | ||
@@ -510,12 +512,12 @@ regex_ctx_destroy(struct RegexCombineCtx *ctx) | |||
510 | return; | 512 | return; |
511 | 513 | ||
512 | for (i = 0; i < ctx->size; i++) | 514 | for (i = 0; i < ctx->size; i++) |
513 | { | 515 | { |
514 | regex_ctx_destroy(ctx->children[i]); | 516 | regex_ctx_destroy (ctx->children[i]); |
515 | } | 517 | } |
516 | GNUNET_free_non_null(ctx->s); /* 's' on root node is null */ | 518 | GNUNET_free_non_null (ctx->s); /* 's' on root node is null */ |
517 | GNUNET_free(ctx->children); | 519 | GNUNET_free (ctx->children); |
518 | GNUNET_free(ctx); | 520 | GNUNET_free (ctx); |
519 | } | 521 | } |
520 | 522 | ||
521 | 523 | ||
@@ -534,27 +536,27 @@ regex_ctx_destroy(struct RegexCombineCtx *ctx) | |||
534 | * @return A string with a single regex that matches any of the original regexes | 536 | * @return A string with a single regex that matches any of the original regexes |
535 | */ | 537 | */ |
536 | char * | 538 | char * |
537 | REGEX_TEST_combine(char * const regexes[], unsigned int alphabet_size) | 539 | REGEX_TEST_combine (char *const regexes[], unsigned int alphabet_size) |
538 | { | 540 | { |
539 | unsigned int i; | 541 | unsigned int i; |
540 | char *combined; | 542 | char *combined; |
541 | const char *current; | 543 | const char *current; |
542 | struct RegexCombineCtx *ctx; | 544 | struct RegexCombineCtx *ctx; |
543 | 545 | ||
544 | ctx = new_regex_ctx(alphabet_size); | 546 | ctx = new_regex_ctx (alphabet_size); |
545 | for (i = 0; regexes[i]; i++) | 547 | for (i = 0; regexes[i]; i++) |
546 | { | 548 | { |
547 | current = regexes[i]; | 549 | current = regexes[i]; |
548 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Regex %u: %s\n", i, current); | 550 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex %u: %s\n", i, current); |
549 | regex_add(ctx, current); | 551 | regex_add (ctx, current); |
550 | debugctx(ctx, 0); | 552 | debugctx (ctx, 0); |
551 | } | 553 | } |
552 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n"); | 554 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n"); |
553 | debugctx(ctx, 0); | 555 | debugctx (ctx, 0); |
554 | 556 | ||
555 | combined = regex_combine(ctx); | 557 | combined = regex_combine (ctx); |
556 | 558 | ||
557 | regex_ctx_destroy(ctx); | 559 | regex_ctx_destroy (ctx); |
558 | 560 | ||
559 | return combined; | 561 | return combined; |
560 | } | 562 | } |
@@ -570,7 +572,7 @@ REGEX_TEST_combine(char * const regexes[], unsigned int alphabet_size) | |||
570 | * @return A newly allocated, NULL terminated array of regexes. | 572 | * @return A newly allocated, NULL terminated array of regexes. |
571 | */ | 573 | */ |
572 | char ** | 574 | char ** |
573 | REGEX_TEST_read_from_file(const char *filename) | 575 | REGEX_TEST_read_from_file (const char *filename) |
574 | { | 576 | { |
575 | struct GNUNET_DISK_FileHandle *f; | 577 | struct GNUNET_DISK_FileHandle *f; |
576 | unsigned int nr; | 578 | unsigned int nr; |
@@ -581,55 +583,55 @@ REGEX_TEST_read_from_file(const char *filename) | |||
581 | char *regex; | 583 | char *regex; |
582 | char **regexes; | 584 | char **regexes; |
583 | 585 | ||
584 | f = GNUNET_DISK_file_open(filename, | 586 | f = GNUNET_DISK_file_open (filename, |
585 | GNUNET_DISK_OPEN_READ, | 587 | GNUNET_DISK_OPEN_READ, |
586 | GNUNET_DISK_PERM_NONE); | 588 | GNUNET_DISK_PERM_NONE); |
587 | if (NULL == f) | 589 | if (NULL == f) |
588 | { | 590 | { |
589 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, | 591 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
590 | "Can't open file %s for reading\n", filename); | 592 | "Can't open file %s for reading\n", filename); |
591 | return NULL; | 593 | return NULL; |
592 | } | 594 | } |
593 | if (GNUNET_OK != GNUNET_DISK_file_handle_size(f, &size)) | 595 | if (GNUNET_OK != GNUNET_DISK_file_handle_size (f, &size)) |
594 | { | 596 | { |
595 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, | 597 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
596 | "Can't get size of file %s\n", filename); | 598 | "Can't get size of file %s\n", filename); |
597 | GNUNET_DISK_file_close(f); | 599 | GNUNET_DISK_file_close (f); |
598 | return NULL; | 600 | return NULL; |
599 | } | 601 | } |
600 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | 602 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
601 | "using file %s, size %llu\n", | 603 | "using file %s, size %llu\n", |
602 | filename, (unsigned long long)size); | 604 | filename, (unsigned long long) size); |
603 | 605 | ||
604 | buffer = GNUNET_malloc(size + 1); | 606 | buffer = GNUNET_malloc (size + 1); |
605 | GNUNET_DISK_file_read(f, buffer, size); | 607 | GNUNET_DISK_file_read (f, buffer, size); |
606 | GNUNET_DISK_file_close(f); | 608 | GNUNET_DISK_file_close (f); |
607 | regexes = GNUNET_malloc(sizeof(char *)); | 609 | regexes = GNUNET_malloc (sizeof(char *)); |
608 | nr = 1; | 610 | nr = 1; |
609 | offset = 0; | 611 | offset = 0; |
610 | regex = NULL; | 612 | regex = NULL; |
611 | do | 613 | do |
612 | { | 614 | { |
613 | if (NULL == regex) | 615 | if (NULL == regex) |
614 | regex = GNUNET_malloc(size + 1); | 616 | regex = GNUNET_malloc (size + 1); |
615 | len = (size_t)sscanf(&buffer[offset], "%s", regex); | 617 | len = (size_t) sscanf (&buffer[offset], "%s", regex); |
616 | if (0 == len) | 618 | if (0 == len) |
617 | break; | 619 | break; |
618 | len = strlen(regex); | 620 | len = strlen (regex); |
619 | offset += len + 1; | 621 | offset += len + 1; |
620 | if (len < 1) | 622 | if (len < 1) |
621 | continue; | 623 | continue; |
622 | regex[len] = '\0'; | 624 | regex[len] = '\0'; |
623 | regex = GNUNET_realloc(regex, len + 1); | 625 | regex = GNUNET_realloc (regex, len + 1); |
624 | GNUNET_array_grow(regexes, nr, nr + 1); | 626 | GNUNET_array_grow (regexes, nr, nr + 1); |
625 | GNUNET_assert(NULL == regexes[nr - 2]); | 627 | GNUNET_assert (NULL == regexes[nr - 2]); |
626 | regexes[nr - 2] = regex; | 628 | regexes[nr - 2] = regex; |
627 | regexes[nr - 1] = NULL; | 629 | regexes[nr - 1] = NULL; |
628 | regex = NULL; | 630 | regex = NULL; |
629 | } | 631 | } |
630 | while (offset < size); | 632 | while (offset < size); |
631 | GNUNET_free_non_null(regex); | 633 | GNUNET_free_non_null (regex); |
632 | GNUNET_free(buffer); | 634 | GNUNET_free (buffer); |
633 | 635 | ||
634 | return regexes; | 636 | return regexes; |
635 | } | 637 | } |
@@ -641,13 +643,13 @@ REGEX_TEST_read_from_file(const char *filename) | |||
641 | * @param regexes NULL-terminated array of regexes. | 643 | * @param regexes NULL-terminated array of regexes. |
642 | */ | 644 | */ |
643 | void | 645 | void |
644 | REGEX_TEST_free_from_file(char **regexes) | 646 | REGEX_TEST_free_from_file (char **regexes) |
645 | { | 647 | { |
646 | unsigned int i; | 648 | unsigned int i; |
647 | 649 | ||
648 | for (i = 0; regexes[i]; i++) | 650 | for (i = 0; regexes[i]; i++) |
649 | GNUNET_free(regexes[i]); | 651 | GNUNET_free (regexes[i]); |
650 | GNUNET_free(regexes); | 652 | GNUNET_free (regexes); |
651 | } | 653 | } |
652 | 654 | ||
653 | /* end of regex_test_lib.c */ | 655 | /* end of regex_test_lib.c */ |