diff options
author | Bart Polot <bart@net.in.tum.de> | 2013-02-15 19:04:45 +0000 |
---|---|---|
committer | Bart Polot <bart@net.in.tum.de> | 2013-02-15 19:04:45 +0000 |
commit | c5f70c7b4b2af1131bdcec66fe9761ca3c6e4490 (patch) | |
tree | a6e1b986923f0827cf031e47e784661960a8639c /src/regex | |
parent | 559448d179102e1da16784ab978c47bc7c40b5be (diff) | |
download | gnunet-c5f70c7b4b2af1131bdcec66fe9761ca3c6e4490.tar.gz gnunet-c5f70c7b4b2af1131bdcec66fe9761ca3c6e4490.zip |
- fix 2699
Diffstat (limited to 'src/regex')
-rw-r--r-- | src/regex/regex_test_lib.c | 78 |
1 files changed, 59 insertions, 19 deletions
diff --git a/src/regex/regex_test_lib.c b/src/regex/regex_test_lib.c index d344603a1..57c6b13e8 100644 --- a/src/regex/regex_test_lib.c +++ b/src/regex/regex_test_lib.c | |||
@@ -60,6 +60,29 @@ struct RegexCombineCtx { | |||
60 | char *s; | 60 | char *s; |
61 | }; | 61 | }; |
62 | 62 | ||
63 | // static void | ||
64 | // space (int n) | ||
65 | // { | ||
66 | // int i; | ||
67 | // for (i = 0; i < n; i++) | ||
68 | // printf (" "); | ||
69 | // } | ||
70 | // | ||
71 | // static void | ||
72 | // debugctx (struct RegexCombineCtx *ctx, int level) | ||
73 | // { | ||
74 | // struct RegexCombineCtx *p; | ||
75 | // space (level); | ||
76 | // if (NULL != ctx->s) | ||
77 | // printf ("'%s'\n", ctx->s); | ||
78 | // else | ||
79 | // printf ("NULL\n"); | ||
80 | // for (p = ctx->head; NULL != p; p = p->next) | ||
81 | // { | ||
82 | // debugctx (p, level + 1); | ||
83 | // } | ||
84 | // } | ||
85 | |||
63 | 86 | ||
64 | /** | 87 | /** |
65 | * Extract a string from all prefix-combined regexes. | 88 | * Extract a string from all prefix-combined regexes. |
@@ -76,34 +99,50 @@ regex_combine (struct RegexCombineCtx *ctx) | |||
76 | char *regex; | 99 | char *regex; |
77 | char *tmp; | 100 | char *tmp; |
78 | char *s; | 101 | char *s; |
102 | int opt; | ||
79 | 103 | ||
80 | if (NULL != ctx->s) | 104 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new combine %s\n", ctx->s); |
81 | GNUNET_asprintf (®ex, "%s(", ctx->s); | 105 | regex = GNUNET_strdup (""); |
82 | else | 106 | opt = GNUNET_NO; |
83 | regex = GNUNET_strdup ("("); | ||
84 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "prefix: %s\n", regex); | ||
85 | |||
86 | for (p = ctx->head; NULL != p; p = p->next) | 107 | for (p = ctx->head; NULL != p; p = p->next) |
87 | { | 108 | { |
109 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding '%s' to innner %s\n", p->s, ctx->s); | ||
88 | s = regex_combine (p); | 110 | s = regex_combine (p); |
89 | GNUNET_asprintf (&tmp, "%s%s|", regex, s); | 111 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " total '%s'\n", s); |
112 | if (strlen(s) == 0) | ||
113 | opt = GNUNET_YES; | ||
114 | else | ||
115 | { | ||
116 | GNUNET_asprintf (&tmp, "%s%s|", regex, s); | ||
117 | GNUNET_free_non_null (regex); | ||
118 | regex = tmp; | ||
119 | } | ||
90 | GNUNET_free_non_null (s); | 120 | GNUNET_free_non_null (s); |
91 | GNUNET_free_non_null (regex); | 121 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " so far '%s' for inner %s\n", regex, ctx->s); |
92 | regex = tmp; | ||
93 | } | 122 | } |
123 | |||
124 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "opt: %d, innner: '%s'\n", opt, regex); | ||
94 | len = strlen (regex); | 125 | len = strlen (regex); |
95 | if (1 == len) | 126 | if (0 == len) |
96 | { | 127 | { |
128 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "empty, returning ''\n"); | ||
97 | GNUNET_free (regex); | 129 | GNUNET_free (regex); |
98 | return GNUNET_strdup (""); | 130 | return GNUNET_strdup (ctx->s); |
99 | } | 131 | } |
100 | 132 | ||
101 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pre-partial: %s\n", regex); | ||
102 | if ('|' == regex[len - 1]) | 133 | if ('|' == regex[len - 1]) |
103 | regex[len - 1] = ')'; | ||
104 | if ('(' == regex[len - 1]) | ||
105 | regex[len - 1] = '\0'; | 134 | regex[len - 1] = '\0'; |
106 | 135 | ||
136 | if (NULL != ctx->s) | ||
137 | { | ||
138 | if (opt) | ||
139 | GNUNET_asprintf (&s, "%s[%s]", ctx->s, regex); | ||
140 | else | ||
141 | GNUNET_asprintf (&s, "%s(%s)", ctx->s, regex); | ||
142 | GNUNET_free (regex); | ||
143 | regex = s; | ||
144 | } | ||
145 | |||
107 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "partial: %s\n", regex); | 146 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "partial: %s\n", regex); |
108 | return regex; | 147 | return regex; |
109 | } | 148 | } |
@@ -134,16 +173,16 @@ regex_add (struct RegexCombineCtx *ctx, const char *regex) | |||
134 | } | 173 | } |
135 | else | 174 | else |
136 | { | 175 | { |
137 | struct RegexCombineCtx *new; | 176 | struct RegexCombineCtx *newctx; |
138 | new = GNUNET_malloc (sizeof (struct RegexCombineCtx)); | 177 | newctx = GNUNET_malloc (sizeof (struct RegexCombineCtx)); |
139 | new->s = GNUNET_strdup (&p->s[1]); | 178 | newctx->s = GNUNET_strdup (&p->s[1]); |
140 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " p has now %s\n", p->s); | 179 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " p has now %s\n", p->s); |
141 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " p will have %.1s\n", p->s); | 180 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " p will have %.1s\n", p->s); |
142 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex is %s\n", regex); | 181 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex is %s\n", regex); |
143 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " new has now %s\n", new->s); | 182 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " new has now %s\n", newctx->s); |
144 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " rest is now %s\n", rest); | 183 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " rest is now %s\n", rest); |
145 | p->s[1] = '\0'; /* dont realloc */ | 184 | p->s[1] = '\0'; /* dont realloc */ |
146 | GNUNET_CONTAINER_DLL_insert (p->head, p->tail, new); | 185 | GNUNET_CONTAINER_DLL_insert (p->head, p->tail, newctx); |
147 | regex_add (p, rest); | 186 | regex_add (p, rest); |
148 | } | 187 | } |
149 | return; | 188 | return; |
@@ -203,6 +242,7 @@ GNUNET_REGEX_combine (char * const regexes[]) | |||
203 | regex_add (ctx, current); | 242 | regex_add (ctx, current); |
204 | } | 243 | } |
205 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n"); | 244 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n"); |
245 | //debugctx (ctx, 0); | ||
206 | 246 | ||
207 | combined = regex_combine (ctx); | 247 | combined = regex_combine (ctx); |
208 | 248 | ||