>From cebe2057d2970043db7e5efd839d9e663eaf0309 Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy Date: Sun, 27 Sep 2026 15:12:37 +0000 Subject: [PATCH 19/25] regex: refactor tag list merging factor out "concatenate tags with duplicate removal" into merge_nodup to optimize it later and apply elsewhere. --- src/regex/regcomp.c | 43 +++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/src/regex/regcomp.c b/src/regex/regcomp.c index be5fa595..ed35ac72 100644 --- a/src/regex/regcomp.c +++ b/src/regex/regcomp.c @@ -2013,6 +2013,20 @@ tre_expand_ast(tre_mem_t mem, tre_stack_t *stack, tre_ast_node_t *ast, return status; } +/* merge s1 and s2 with no dups into out with no dups, returns length of out. */ +static int merge_nodup(int *out, int *s1, int n1, int *s2, int n2) +{ + int i=0, j=0, n=n1, v; + for (i=0; iposition >= 0) @@ -2577,31 +2591,8 @@ tre_make_trans(tre_pos_and_tags_t *p1, tre_pos_and_tags_t *p2, trans->tags = xmalloc(sizeof(*trans->tags) * (i + j + 1)); if (!trans->tags) return REG_ESPACE; - i = 0; - if (p1->tags != NULL) - while(p1->tags[i] >= 0) - { - trans->tags[i] = p1->tags[i]; - i++; - } - l = i; - j = 0; - if (p2->tags != NULL) - while (p2->tags[j] >= 0) - { - /* Don't add duplicates. */ - dup = 0; - for (k = 0; k < i; k++) - if (trans->tags[k] == p2->tags[j]) - { - dup = 1; - break; - } - if (!dup) - trans->tags[l++] = p2->tags[j]; - j++; - } - trans->tags[l] = -1; + i = merge_nodup(trans->tags, p1->tags, i, p2->tags, j); + trans->tags[i] = -1; } p2++; -- 2.52.0