+static void
+regex_add_multiple (struct RegexCombineCtx *ctx,
+ const char *regex,
+ struct RegexCombineCtx **children)
+{
+ char tmp[2];
+ long unsigned int i;
+ size_t l;
+ struct RegexCombineCtx *newctx;
+ unsigned int count;
+
+ if ('(' != regex[0])
+ {
+ GNUNET_assert (0);
+ }
+
+ /* Does the regex cover *all* possible children? Then don't add any,
+ * as it will be covered by the post-regex "(a-z)*"
+ */
+ l = strlen (regex);
+ count = 0;
+ for (i = 1UL; i < l; i++)
+ {
+ if (regex[i] != '|' && regex[i] != ')')
+ {
+ count++;
+ }
+ }
+ if (count == ctx->size)
+ {
+ return;
+ }
+
+ /* Add every component as a child node */
+ tmp[1] = '\0';
+ for (i = 1UL; i < l; i++)
+ {
+ if (regex[i] != '|' && regex[i] != ')')
+ {
+ tmp[0] = regex[i];
+ newctx = new_regex_ctx(ctx->size);
+ newctx->s = GNUNET_strdup (tmp);
+ if (children != NULL)
+ GNUNET_memcpy (newctx->children,
+ children,
+ sizeof (*children) * ctx->size);
+ ctx->children[c2i(tmp[0], ctx->size)] = newctx;
+ }
+ }
+}
+
+/**
+ * Add a single regex to a context, splitting the exisiting state.
+ *
+ * We only had a partial match, split existing state, truncate the current node
+ * so it only contains the prefix, add suffix(es) as children.
+ *
+ * @param ctx Context to split.
+ * @param len Lenght of ctx->s
+ * @param prefix_l Lenght of common prefix of the new regex and @a ctx->s
+ */
+static void
+regex_split (struct RegexCombineCtx *ctx,
+ unsigned int len,
+ unsigned int prefix_l)
+{
+ struct RegexCombineCtx *newctx;
+ unsigned int idx;
+ char *suffix;
+
+ suffix = GNUNET_malloc (len - prefix_l + 1);
+ strncpy (suffix, &ctx->s[prefix_l], len - prefix_l + 1);
+
+ /* Suffix saved, truncate current node so it only contains the prefix,
+ * copy any children nodes to put as grandchildren and initialize new empty
+ * children array.
+ */
+ ctx->s[prefix_l] = '\0';
+
+ /* If the suffix is an OR expression, add multiple children */
+ if ('(' == suffix[0])
+ {
+ struct RegexCombineCtx **tmp;
+
+ tmp = ctx->children;
+ ctx->children = GNUNET_malloc (sizeof(*tmp) * ctx->size);
+ regex_add_multiple (ctx, suffix, tmp);
+ GNUNET_free (suffix);
+ GNUNET_free (tmp);
+ return;
+ }
+
+ /* The suffix is a normal string, add as one node */
+ newctx = new_regex_ctx (ctx->size);
+ newctx->s = suffix;
+ move_children (newctx, ctx);
+ idx = c2i(suffix[0], ctx->size);
+ ctx->children[idx] = newctx;
+}
+