[PATCH 02/10] diffcore-pickaxe.c: refactor regex compilation
From: David A. Dalrymple (and Bhushan G. Lodha) <hidden>
Date: 2016-06-15 23:00:33
Subsystem:
the rest · Maintainer:
Linus Torvalds
From: "Bhushan G. Lodha & David A. Dalrymple" <redacted> 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) <redacted> --- 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(®ex, o->pickaxe, cflags); - if (err) { - char errbuf[1024]; - regerror(err, ®ex, errbuf, 1024); - regfree(®ex); - die("invalid regex: %s", errbuf); - } + compile_regex(®ex, o->pickaxe, cflags); pickaxe(&diff_queued_diff, o, ®ex, 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(®ex, needle, REG_EXTENDED | REG_NEWLINE); - if (err) { - /* The POSIX.2 people are surely sick */ - char errbuf[1024]; - regerror(err, ®ex, errbuf, 1024); - regfree(®ex); - die("invalid regex: %s", errbuf); - } + compile_regex(®ex, needle, REG_EXTENDED | REG_NEWLINE); regexp = ®ex; } else { kws = kwsalloc(DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE)
--
1.7.12.4 (Apple Git-37)