git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: "Jonathan Nieder" <jrnieder@gmail.com>,
	"Joachim Schmitz" <jojo@schmitz-digital.de>,
	"René Scharfe" <rene.scharfe@lsrfire.ath.cx>,
	git@vger.kernel.org
Subject: [PATCHv2 3/8] convert some config callbacks to parse_config_key
Date: Wed, 23 Jan 2013 01:24:23 -0500	[thread overview]
Message-ID: <20130123062422.GC5036@sigill.intra.peff.net> (raw)
In-Reply-To: <20130123062132.GA2038@sigill.intra.peff.net>

These callers can drop some inline pointer arithmetic and
magic offset constants, making them more readable and less
error-prone (those constants had to match the lengths of
strings, but there is no automatic verification of that
fact).

The "ep" pointer (presumably for "end pointer"), which
points to the final key segment of the config variable, is
given the more standard name "key" to describe its function
rather than its derivation.

Signed-off-by: Jeff King <peff@peff.net>
---
 convert.c  | 14 +++++---------
 ll-merge.c | 14 +++++---------
 userdiff.c | 13 +++----------
 3 files changed, 13 insertions(+), 28 deletions(-)

diff --git a/convert.c b/convert.c
index 6602155..3520252 100644
--- a/convert.c
+++ b/convert.c
@@ -457,7 +457,7 @@ static int read_convert_config(const char *var, const char *value, void *cb)
 
 static int read_convert_config(const char *var, const char *value, void *cb)
 {
-	const char *ep, *name;
+	const char *key, *name;
 	int namelen;
 	struct convert_driver *drv;
 
@@ -465,10 +465,8 @@ static int read_convert_config(const char *var, const char *value, void *cb)
 	 * External conversion drivers are configured using
 	 * "filter.<name>.variable".
 	 */
-	if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6)
+	if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name)
 		return 0;
-	name = var + 7;
-	namelen = ep - name;
 	for (drv = user_convert; drv; drv = drv->next)
 		if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
 			break;
@@ -479,8 +477,6 @@ static int read_convert_config(const char *var, const char *value, void *cb)
 		user_convert_tail = &(drv->next);
 	}
 
-	ep++;
-
 	/*
 	 * filter.<name>.smudge and filter.<name>.clean specifies
 	 * the command line:
@@ -490,13 +486,13 @@ static int read_convert_config(const char *var, const char *value, void *cb)
 	 * The command-line will not be interpolated in any way.
 	 */
 
-	if (!strcmp("smudge", ep))
+	if (!strcmp("smudge", key))
 		return git_config_string(&drv->smudge, var, value);
 
-	if (!strcmp("clean", ep))
+	if (!strcmp("clean", key))
 		return git_config_string(&drv->clean, var, value);
 
-	if (!strcmp("required", ep)) {
+	if (!strcmp("required", key)) {
 		drv->required = git_config_bool(var, value);
 		return 0;
 	}
diff --git a/ll-merge.c b/ll-merge.c
index acea33b..fb61ea6 100644
--- a/ll-merge.c
+++ b/ll-merge.c
@@ -222,7 +222,7 @@ static int read_merge_config(const char *var, const char *value, void *cb)
 static int read_merge_config(const char *var, const char *value, void *cb)
 {
 	struct ll_merge_driver *fn;
-	const char *ep, *name;
+	const char *key, *name;
 	int namelen;
 
 	if (!strcmp(var, "merge.default")) {
@@ -236,15 +236,13 @@ static int read_merge_config(const char *var, const char *value, void *cb)
 	 * especially, we do not want to look at variables such as
 	 * "merge.summary", "merge.tool", and "merge.verbosity".
 	 */
-	if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5)
+	if (parse_config_key(var, "merge", &name, &namelen, &key) < 0 || !name)
 		return 0;
 
 	/*
 	 * Find existing one as we might be processing merge.<name>.var2
 	 * after seeing merge.<name>.var1.
 	 */
-	name = var + 6;
-	namelen = ep - name;
 	for (fn = ll_user_merge; fn; fn = fn->next)
 		if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
 			break;
@@ -256,16 +254,14 @@ static int read_merge_config(const char *var, const char *value, void *cb)
 		ll_user_merge_tail = &(fn->next);
 	}
 
-	ep++;
-
-	if (!strcmp("name", ep)) {
+	if (!strcmp("name", key)) {
 		if (!value)
 			return error("%s: lacks value", var);
 		fn->description = xstrdup(value);
 		return 0;
 	}
 
-	if (!strcmp("driver", ep)) {
+	if (!strcmp("driver", key)) {
 		if (!value)
 			return error("%s: lacks value", var);
 		/*
@@ -289,7 +285,7 @@ static int read_merge_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	if (!strcmp("recursive", ep)) {
+	if (!strcmp("recursive", key)) {
 		if (!value)
 			return error("%s: lacks value", var);
 		fn->recursive = xstrdup(value);
diff --git a/userdiff.c b/userdiff.c
index ed958ef..a4ea1e9 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -188,20 +188,13 @@ static struct userdiff_driver *parse_driver(const char *var,
 		const char *value, const char *type)
 {
 	struct userdiff_driver *drv;
-	const char *dot;
-	const char *name;
+	const char *name, *key;
 	int namelen;
 
-	if (prefixcmp(var, "diff."))
-		return NULL;
-	dot = strrchr(var, '.');
-	if (dot == var + 4)
-		return NULL;
-	if (strcmp(type, dot+1))
+	if (parse_config_key(var, "diff", &name, &namelen, &key) < 0 ||
+	    !name || strcmp(type, key))
 		return NULL;
 
-	name = var + 5;
-	namelen = dot - name;
 	drv = userdiff_find_by_namelen(name, namelen);
 	if (!drv) {
 		ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
-- 
1.8.0.2.15.g815dc66

  parent reply	other threads:[~2013-01-23  6:24 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-13 17:42 [PATCH] archive-tar: fix sanity check in config parsing René Scharfe
2013-01-13 20:00 ` Jeff King
2013-01-14  8:17   ` Joachim Schmitz
2013-01-14 12:44     ` Jeff King
2013-01-14 14:58       ` Jeff King
2013-01-14 15:00         ` [PATCH 1/6] config: add helper function for parsing key names Jeff King
2013-01-14 18:08           ` Junio C Hamano
2013-01-15 16:04             ` Jeff King
2013-01-15 17:07               ` Junio C Hamano
2013-01-18 20:53                 ` Junio C Hamano
2013-01-23  6:21                   ` [PATCHv2 0/8] config key-parsing cleanups Jeff King
2013-01-23  6:23                     ` [PATCHv2 1/8] config: add helper function for parsing key names Jeff King
2013-01-23  6:23                     ` [PATCHv2 2/8] archive-tar: use parse_config_key when parsing config Jeff King
2013-01-23  6:24                     ` Jeff King [this message]
2013-01-23  6:25                     ` [PATCHv2 4/8] userdiff: drop parse_driver function Jeff King
2013-01-23  6:25                     ` [PATCHv2 5/8] submodule: use parse_config_key when parsing config Jeff King
2013-01-23 20:45                       ` Jens Lehmann
2013-01-23  6:26                     ` [PATCHv2 6/8] submodule: simplify memory handling in config parsing Jeff King
2013-01-23 20:51                       ` Jens Lehmann
2013-01-23  6:27                     ` [PATCHv2 7/8] help: use parse_config_key for man config Jeff King
2013-01-23  6:27                     ` [PATCHv2 8/8] reflog: use parse_config_key in config callback Jeff King
2013-01-23  7:04                       ` Junio C Hamano
2013-01-23  7:27                     ` [PATCHv2 0/8] config key-parsing cleanups Jonathan Nieder
2013-01-14 15:02         ` [PATCH 2/6] archive-tar: use match_config_key when parsing config Jeff King
2013-01-14 15:03         ` [PATCH 3/6] convert some config callbacks to match_config_key Jeff King
2013-01-14 16:55           ` Jonathan Nieder
2013-01-14 17:06             ` Jeff King
2013-01-14 18:05               ` Jeff King
2013-01-14 15:04         ` [PATCH 4/6] userdiff: drop parse_driver function Jeff King
2013-01-14 15:04         ` [PATCH 5/6] submodule: use match_config_key when parsing config Jeff King
2013-01-14 15:07         ` [PATCH 6/6] submodule: simplify memory handling in config parsing Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130123062422.GC5036@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jojo@schmitz-digital.de \
    --cc=jrnieder@gmail.com \
    --cc=rene.scharfe@lsrfire.ath.cx \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).