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

[PATCH 02/10] diffcore-pickaxe.c: refactor regex compilation

From
DLDavid A. Dalrymple (and Bhushan G. Lodha) <dad-bgl@mit.edu>
Date
Mar 27, 2014, 18:50 UTC
Message-ID
<36cbd6934df8cf17a52a4478aa4e2da436feaf0b.1395942768.git.davidad@alum.mit.edu>
In-Reply-To
<1395946256-67124-1-git-send-email-dad-bgl@mit.edu>
From: "Bhushan G. Lodha & David A. Dalrymple" <dad-bgl@mit.edu>

In this file, two functions use identical blocks of code to call the POSIX regex compiling function and handle a possible error. Here we factor that block into its own function, in anticipation of using the same code a third time.

Signed-off-by: David Dalrymple (on zayin) <davidad@alum.mit.edu>
---
 diffcore-pickaxe.c | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)
diff --git a/diffcore-pickaxe.c b/diffcore-pickaxe.c
index 401eb72..0d36a3c 100644
--- a/diffcore-pickaxe.c
+++ b/diffcore-pickaxe.c
@@ -12,6 +12,8 @@ typedef int (*pickaxe_fn)(mmfile_t *one, mmfile_t *two,
 			  struct diff_options *o,
 			  regex_t *regexp, kwset_t kws);
 
+static void compile_regex(regex_t *r, const char *s, int cflags);
+
 static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
 			 regex_t *regexp, kwset_t kws, pickaxe_fn fn);
 
@@ -110,20 +112,13 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 
 static void diffcore_pickaxe_grep(struct diff_options *o)
 {
-	int err;
 	regex_t regex;
 	int cflags = REG_EXTENDED | REG_NEWLINE;
 
 	if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE))
 		cflags |= REG_ICASE;
 
-	err = regcomp(&regex, o->pickaxe, cflags);
-	if (err) {
-		char errbuf[1024];
-		regerror(err, &regex, errbuf, 1024);
-		regfree(&regex);
-		die("invalid regex: %s", errbuf);
-	}
+	compile_regex(&regex, o->pickaxe, cflags);
 
 	pickaxe(&diff_queued_diff, o, &regex, NULL, diff_grep);
 
@@ -180,6 +175,18 @@ static int has_changes(mmfile_t *one, mmfile_t *two,
 	return one_contains != two_contains;
 }
 
+static void compile_regex(regex_t *r, const char *s, int cflags)
+{
+	int err;
+	err = regcomp(r, s, cflags);
+	if (err) {
+		char errbuf[1024];
+		regerror(err, r, errbuf, 1024);
+		regfree(r);
+		die("invalid regex: %s", errbuf);
+	}
+}
+
 static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
 			 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
 {
@@ -236,15 +243,7 @@ static void diffcore_pickaxe_count(struct diff_options *o)
 	kwset_t kws = NULL;
 
 	if (opts & DIFF_PICKAXE_REGEX) {
-		int err;
-		err = regcomp(&regex, needle, REG_EXTENDED | REG_NEWLINE);
-		if (err) {
-			/* The POSIX.2 people are surely sick */
-			char errbuf[1024];
-			regerror(err, &regex, errbuf, 1024);
-			regfree(&regex);
-			die("invalid regex: %s", errbuf);
-		}
+		compile_regex(&regex, needle, REG_EXTENDED | REG_NEWLINE);
 		regexp = &regex;
 	} else {
 		kws = kwsalloc(DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE)
-- 
1.7.12.4 (Apple Git-37)
Previous: David A. Dalrymple (and Bhushan G. Lodha)Next: David A. Dalrymple (and Bhushan G. Lodha)
Message 3 of 23 in “[RFC] pickaxe for function names”
  1. 00/10 [RFC] pickaxe for function namesDavid A. Dalrymple (and Bhushan G. Lodha), Mar 27, 2014
  2. 01/10 .gitattributes: specify the language usedDavid A. Dalrymple (and Bhushan G. Lodha), Mar 27, 2014
  3. 02/10 diffcore-pickaxe.c: refactor regex compilationDavid A. Dalrymple (and Bhushan G. Lodha), Mar 27, 2014
  4. 03/10 diffcore-pickaxe.c: Refactor pickaxe_fn signatureDavid A. Dalrymple (and Bhushan G. Lodha), Mar 27, 2014
  5. Jakub NarębskiApr 4, 2014
  6. 04/10 diff.c/diff.h: expose userdiff_funcnameDavid A. Dalrymple (and Bhushan G. Lodha), Mar 27, 2014
  7. 05/10 diffcore-pickaxe.c: set up funcname patternDavid A. Dalrymple (and Bhushan G. Lodha), Mar 27, 2014
  8. 06/10 log: --function-name pickaxeDavid A. Dalrymple (and Bhushan G. Lodha), Mar 27, 2014
  9. René ScharfeApr 3, 2014
  10. Junio C HamanoApr 3, 2014
  11. Jakub NarębskiApr 4, 2014
  12. Junio C HamanoApr 4, 2014
  13. Bhushan LodhaApr 28, 2014
  14. 07/10 xdiff: add XDL_EMIT_MOREFUNCNAMESDavid A. Dalrymple (and Bhushan G. Lodha), Mar 27, 2014
  15. 08/10 xdiff: add XDL_EMIT_MOREHUNKHEADSDavid A. Dalrymple (and Bhushan G. Lodha), Mar 27, 2014
  16. 09/10 t4213: test --function-name optionDavid A. Dalrymple (and Bhushan G. Lodha), Mar 27, 2014
  17. Johannes SixtMar 28, 2014
  18. Eric SunshineMar 28, 2014
  19. Johannes SixtMar 28, 2014
  20. Jakub NarębskiApr 4, 2014
  21. 10/10 Documentation: Document --function-name usageDavid A. Dalrymple (and Bhushan G. Lodha), Mar 27, 2014
  22. Eric SunshineMar 28, 2014
  23. Jeff KingMar 27, 2014

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.