git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] grep: use grep_or_expr() in compile_pattern_or()

From
René Scharfe <l.s.r@web.de>
Date
Jan 6, 2022, 09:51 UTC
Message-ID
<ea01f9b6-2ce3-57e8-533f-4cc29937f700@web.de>

Move the definition of grep_or_expr() up and use this function in compile_pattern_or() to reduce code duplication.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 grep.c | 26 +++++++++++---------------
 1 file changed, 11 insertions(+), 15 deletions(-)
diff --git a/grep.c b/grep.c
index 47c75ab7fb..f1bbe80ccb 100644
--- a/grep.c
+++ b/grep.c
@@ -595,6 +595,15 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
 	}
 }

+static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
+{
+	struct grep_expr *z = xcalloc(1, sizeof(*z));
+	z->node = GREP_NODE_OR;
+	z->u.binary.left = left;
+	z->u.binary.right = right;
+	return z;
+}
+
 static struct grep_expr *compile_pattern_or(struct grep_pat **);
 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
 {
@@ -677,7 +686,7 @@ static struct grep_expr *compile_pattern_and(struct grep_pat **list)
 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
 {
 	struct grep_pat *p;
-	struct grep_expr *x, *y, *z;
+	struct grep_expr *x, *y;

 	x = compile_pattern_and(list);
 	p = *list;
@@ -685,11 +694,7 @@ static struct grep_expr *compile_pattern_or(struct grep_pat **list)
 		y = compile_pattern_or(list);
 		if (!y)
 			die("not a pattern expression %s", p->pattern);
-		CALLOC_ARRAY(z, 1);
-		z->node = GREP_NODE_OR;
-		z->u.binary.left = x;
-		z->u.binary.right = y;
-		return z;
+		return grep_or_expr(x, y);
 	}
 	return x;
 }
@@ -714,15 +719,6 @@ static struct grep_expr *grep_true_expr(void)
 	return z;
 }

-static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
-{
-	struct grep_expr *z = xcalloc(1, sizeof(*z));
-	z->node = GREP_NODE_OR;
-	z->u.binary.left = left;
-	z->u.binary.right = right;
-	return z;
-}
-
 static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
 {
 	struct grep_pat *p;
--
2.34.1
Next: René Scharfe
Message 1 of 13 in “grep: use grep_or_expr() in compile_pattern_or()”
  1. 1/2 grep: use grep_or_expr() in compile_pattern_or()René Scharfe, Jan 6, 2022
  2. 2/2 grep: use grep_not_expr() in compile_pattern_not()René Scharfe, Jan 6, 2022
  3. 0/2 grep: introduce and use grep_and_expr()Taylor Blau, Jan 6, 2022
  4. 1/2 grep: extract grep_binexp() from grep_or_expr()Taylor Blau, Jan 6, 2022
  5. 2/2 grep: use grep_and_expr() in compile_pattern_and()Taylor Blau, Jan 6, 2022
  6. Junio C HamanoJan 6, 2022
  7. Taylor BlauJan 6, 2022
  8. 0/2 grep: introduce and use grep_and_expr()Taylor Blau, Jan 6, 2022
  9. 1/2 grep: extract grep_binexp() from grep_or_expr()Taylor Blau, Jan 6, 2022
  10. 2/2 grep: use grep_and_expr() in compile_pattern_and()Taylor Blau, Jan 6, 2022
  11. René ScharfeJan 7, 2022
  12. Taylor BlauJan 7, 2022
  13. René ScharfeJan 7, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.