[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r25472 - gnunet/src/regex
From: |
gnunet |
Subject: |
[GNUnet-SVN] r25472 - gnunet/src/regex |
Date: |
Thu, 13 Dec 2012 21:21:45 +0100 |
Author: bartpolot
Date: 2012-12-13 21:21:45 +0100 (Thu, 13 Dec 2012)
New Revision: 25472
Modified:
gnunet/src/regex/perf-regex.c
gnunet/src/regex/regex.c
Log:
- new perf
Modified: gnunet/src/regex/perf-regex.c
===================================================================
--- gnunet/src/regex/perf-regex.c 2012-12-13 20:21:30 UTC (rev 25471)
+++ gnunet/src/regex/perf-regex.c 2012-12-13 20:21:45 UTC (rev 25472)
@@ -46,12 +46,17 @@
int
main (int argc, char *const *argv)
{
- FILE *f;
struct GNUNET_REGEX_Automaton* dfa;
- long size;
+ char **regexes;
+ char *buffer;
char *regex;
+ unsigned int nr;
int compression;
+ long size;
+ size_t len;
+ FILE *f;
+ GNUNET_log_setup ("perf-regex", "DEBUG", NULL);
exe = argv[0];
if (3 != argc)
{
@@ -67,18 +72,52 @@
}
fseek (f, 0, SEEK_END);
size = ftell (f);
+ fprintf (stderr, "using file %s, size %ld\n", argv[1], size);
fseek (f, 0, SEEK_SET);
- regex = GNUNET_malloc (size);
- if (fread (regex, sizeof(char), size, f) != size)
+ buffer = GNUNET_malloc (size + 1);
+ regexes = GNUNET_malloc (sizeof (char *));
+ nr = 1;
+ do
{
- fprintf (stderr, "Can't read file %s\n", argv[1]);
- usage();
- return 3;
- }
+ if (NULL == fgets (buffer, size + 1, f))
+ {
+ fprintf (stderr, "Can't read file %s\n", argv[1]);
+ usage();
+ return 3;
+ }
+ len = strlen (buffer);
+ if (len < 1)
+ continue;
+ if ('\n' == buffer[len - 1])
+ {
+ len--;
+ buffer[len] = '\0';
+ }
+ if (len < 6 || strncmp (&buffer[len - 6], "(0|1)*", 6) != 0)
+ {
+ fprintf (stderr, "\nWARNING:\n");
+ fprintf (stderr, "%s (line %u) does not end in (0|1)*\n", buffer, nr);
+ }
+ else
+ {
+ buffer[len - 6] = '\0';
+ }
+ GNUNET_array_grow (regexes, nr, nr+1);
+ regexes[nr - 2] = GNUNET_strdup (buffer);
+ regexes[nr - 1] = NULL;
+ } while (ftell(f) < size);
+ GNUNET_free (buffer);
+
+ buffer = GNUNET_REGEX_combine (regexes);
+
+ GNUNET_asprintf (®ex, "GNVPN-0001-PAD(%s)(0|1)*", buffer);
+
+// fprintf (stderr, "Combined regex:\n%s\n", regex);
+// return 0;
+
compression = atoi (argv[2]);
dfa = GNUNET_REGEX_construct_dfa (regex, size, compression);
GNUNET_REGEX_automaton_destroy (dfa);
- GNUNET_free (regex);
return 0;
}
Modified: gnunet/src/regex/regex.c
===================================================================
--- gnunet/src/regex/regex.c 2012-12-13 20:21:30 UTC (rev 25471)
+++ gnunet/src/regex/regex.c 2012-12-13 20:21:45 UTC (rev 25472)
@@ -3052,3 +3052,131 @@
if (prefixlen < 128)
strcat (rxstr, "(0|1)+");
}
+
+
+struct RegexCombineCtx {
+ struct RegexCombineCtx *next;
+ struct RegexCombineCtx *prev;
+
+ struct RegexCombineCtx *head;
+ struct RegexCombineCtx *tail;
+
+ char *s;
+};
+
+
+static char *
+regex_combine (struct RegexCombineCtx *ctx)
+{
+ struct RegexCombineCtx *p;
+ size_t len;
+ char *regex;
+ char *tmp;
+ char *s;
+
+ if (NULL != ctx->s)
+ GNUNET_asprintf (®ex, "%s(", ctx->s);
+ else
+ regex = GNUNET_strdup ("(");
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "prefix: %s\n", regex);
+
+ for (p = ctx->head; NULL != p; p = p->next)
+ {
+ s = regex_combine (p);
+ GNUNET_asprintf (&tmp, "%s%s|", regex, s);
+ GNUNET_free_non_null (s);
+ GNUNET_free_non_null (regex);
+ regex = tmp;
+ }
+ len = strlen (regex);
+ if (1 == len)
+ return GNUNET_strdup ("");
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pre-partial: %s\n", regex);
+ if ('|' == regex[len - 1])
+ regex[len - 1] = ')';
+ if ('(' == regex[len - 1])
+ regex[len - 1] = '\0';
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "partial: %s\n", regex);
+ return regex;
+}
+
+static void
+regex_add (struct RegexCombineCtx *ctx, const char *regex)
+{
+ struct RegexCombineCtx *p;
+ const char *rest;
+
+ rest = ®ex[1];
+ for (p = ctx->head; NULL != p; p = p->next)
+ {
+ if (p->s[0] == regex[0])
+ {
+ if (1 == strlen(p->s))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "common char %s\n", p->s);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding %s\n", rest);
+ regex_add (p, rest);
+ }
+ else
+ {
+ struct RegexCombineCtx *new;
+ new = GNUNET_malloc (sizeof (struct RegexCombineCtx));
+ new->s = GNUNET_strdup (&p->s[1]);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " p has now %s\n", p->s);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " p will have %.1s\n", p->s);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex is %s\n", regex);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " new has now %s\n", new->s);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " rest is now %s\n", rest);
+ p->s[1] = '\0'; /* dont realloc */
+ GNUNET_CONTAINER_DLL_insert (p->head, p->tail, new);
+ regex_add (p, rest);
+ }
+ return;
+ }
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no match\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " new state %s\n", regex);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " under %s\n", ctx->s);
+ p = GNUNET_malloc (sizeof (struct RegexCombineCtx));
+ p->s = GNUNET_strdup (regex);
+ GNUNET_CONTAINER_DLL_insert (ctx->head, ctx->tail, p);
+}
+/*
+static void
+debug (struct RegexCombineCtx *ctx, int lvl)
+{
+ struct RegexCombineCtx *p;
+ unsigned int i;
+
+ for (i = 0; i < lvl; i++) fprintf (stderr, " ");
+ fprintf (stderr, "%s\n", ctx->s);
+
+ for (p = ctx->head; NULL != p; p = p->next)
+ {
+ debug (p, lvl + 2);
+ }
+}*/
+
+char *
+GNUNET_REGEX_combine (char * const regexes[])
+{
+ unsigned int i;
+ char *combined;
+ const char *current;
+ struct RegexCombineCtx *ctx;
+
+ ctx = GNUNET_malloc (sizeof (struct RegexCombineCtx));
+ for (i = 0; regexes[i]; i++)
+ {
+ current = regexes[i];
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex %u: %s\n", i, current);
+ regex_add (ctx, current);
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n");
+
+ combined = regex_combine (ctx);
+
+ return combined;
+}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r25472 - gnunet/src/regex,
gnunet <=