[augeas-devel] [PATCH 8/8] Allow NULLS in regexp_(union|concat)_n

David Lutterkort lutter at redhat.com
Mon Aug 31 21:44:34 UTC 2009


It's sometimes useful to union/concat N regexps even if some of them are
NULL. regexp_(union|concat)_n will ignore them.
---
 src/regexp.c |   18 +++++++++++++++---
 1 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/src/regexp.c b/src/regexp.c
index 886875f..df99656 100644
--- a/src/regexp.c
+++ b/src/regexp.c
@@ -139,13 +139,19 @@ regexp_union_n(struct info *info, int n, struct regexp **r) {
     char *pat, *p;
 
     for (int i=0; i < n; i++)
-        len += strlen(r[i]->pattern->str) + 2;
+        if (r[i] != NULL)
+            len += strlen(r[i]->pattern->str) + strlen("()|");
 
-    if (ALLOC_N(pat, len + (n-1) + 1) < 0)
+    if (len == 0)
+        return NULL;
+
+    if (ALLOC_N(pat, len) < 0)
         return NULL;
 
     p = pat;
     for (int i=0; i < n; i++) {
+        if (r[i] == NULL)
+            continue;
         if (i > 0)
             *p++ = '|';
         *p++ = '(';
@@ -172,13 +178,19 @@ regexp_concat_n(struct info *info, int n, struct regexp **r) {
     char *pat, *p;
 
     for (int i=0; i < n; i++)
-        len += strlen(r[i]->pattern->str) + 2;
+        if (r[i] != NULL)
+            len += strlen(r[i]->pattern->str) + strlen("()");
+
+    if (len == 0)
+        return NULL;
 
     if (ALLOC_N(pat, len+1) < 0)
         return NULL;
 
     p = pat;
     for (int i=0; i < n; i++) {
+        if (r[i] == NULL)
+            continue;
         *p++ = '(';
         p = stpcpy(p, r[i]->pattern->str);
         *p++ = ')';
-- 
1.6.2.5




More information about the augeas-devel mailing list