threads / patch / 35269

patchbuiltin/remote: remove postfixcmp() and use suffixcmp() instead

Subject: [PATCH] builtin/remote: remove postfixcmp() and use suffixcmp() instead

## tl;dr

7 messages between Nov 3, 2013 and Nov 4, 2013. Diffs are folded; open one to read it.

replies: 6people: 4as markdown or json

Christian Couder· Nov 3, 2013, 20:13 UTC · lore

Commit 8cc5b290 (git merge -X<option>, 25 Nov 2009) introduced suffixcmp() with nearly the same implementation as postfixcmp() that already existed since commit 211c8968 (Make git-remote a builtin, 29 Feb 2008).

As postfixcmp() has always been static in builtin/remote.c and is used nowhere else, it makes more sense to remove it and use suffixcmp() instead in builtin/remote.c, rather than to remove suffixcmp().

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/remote.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
Show changes to builtin/remote.c +3 −11
diff --git a/builtin/remote.c b/builtin/remote.c
index 4e14891..9b3a98e 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -80,14 +80,6 @@ static int verbose;
 static int show_all(void);
 static int prune_remote(const char *remote, int dry_run);
 
-static inline int postfixcmp(const char *string, const char *postfix)
-{
-	int len1 = strlen(string), len2 = strlen(postfix);
-	if (len1 < len2)
-		return 1;
-	return strcmp(string + len1 - len2, postfix);
-}
-
 static int fetch_remote(const char *name)
 {
 	const char *argv[] = { "fetch", name, NULL, NULL };
@@ -277,13 +269,13 @@ static int config_read_branches(const char *key, const char *value, void *cb)
 		enum { REMOTE, MERGE, REBASE } type;
 
 		key += 7;
-		if (!postfixcmp(key, ".remote")) {
+		if (!suffixcmp(key, ".remote")) {
 			name = xstrndup(key, strlen(key) - 7);
 			type = REMOTE;
-		} else if (!postfixcmp(key, ".merge")) {
+		} else if (!suffixcmp(key, ".merge")) {
 			name = xstrndup(key, strlen(key) - 6);
 			type = MERGE;
-		} else if (!postfixcmp(key, ".rebase")) {
+		} else if (!suffixcmp(key, ".rebase")) {
 			name = xstrndup(key, strlen(key) - 7);
 			type = REBASE;
 		} else
-- 
1.8.4.1.576.g36ba827.dirty
Jonathan Nieder· Nov 4, 2013, 17:56 UTC · re: Christian Couder · lore

Re: [PATCH] builtin/remote: remove postfixcmp() and use suffixcmp() instead

Christian Couder wrote:
> Commit 8cc5b290 (git merge -X<option>, 25 Nov 2009) introduced
> suffixcmp() with nearly the same implementation as postfixcmp()
> that already existed since commit 211c8968 (Make git-remote a
> builtin, 29 Feb 2008).
[...]
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  builtin/remote.c | 14 +++-----------
>  1 file changed, 3 insertions(+), 11 deletions(-)
Makes sense.  Thanks for noticing.
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Junio C Hamano· Nov 4, 2013, 19:19 UTC · re: Christian Couder · lore

Re: [PATCH] builtin/remote: remove postfixcmp() and use suffixcmp() instead

Christian Couder <chriscool@tuxfamily.org> writes:
> Commit 8cc5b290 (git merge -X<option>, 25 Nov 2009) introduced
> suffixcmp() with nearly the same implementation as postfixcmp()
> that already existed since commit 211c8968 (Make git-remote a
> builtin, 29 Feb 2008).
This "nearly the same" piqued my curiosity ;-)

The postfixcmp() you are removing will say "f" > ".txt" while suffixcmp() will say "f" < ".txt".

As postfixcmp() is only used to compare for equality, the distinction does not matter and does not affect the correctness of this patch, but I am not sure which answer is more correct.

I do not think anybody sane uses prefixcmp() or suffixcmp() for anything but checking with zero; in other words, I suspect that all uses of Xcmp() can be replaced with !!Xcmp(), so as a separate clean-up patch, we may at least want to make it clear that the callers should not expect anything but "does str have sfx as its suffix, yes or no?" by doing something like this:

 int suffixcmp(const char *str, const char *suffix)
 {
 	int len = strlen(str), suflen = strlen(suffix);
 	if (len < suflen)
 		return -1;
 	else
-		return strcmp(str + len - suflen, suffix);
+		return !!strcmp(str + len - suflen, suffix);
 }

I am not absolutely sure about doing the same to prefixcmp(), though. It could be used for ordering, even though no existing code seems to do so.

Show 46 quoted lines
> As postfixcmp() has always been static in builtin/remote.c
> and is used nowhere else, it makes more sense to remove it
> and use suffixcmp() instead in builtin/remote.c, rather than
> to remove suffixcmp().
>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  builtin/remote.c | 14 +++-----------
>  1 file changed, 3 insertions(+), 11 deletions(-)
>
> diff --git a/builtin/remote.c b/builtin/remote.c
> index 4e14891..9b3a98e 100644
> --- a/builtin/remote.c
> +++ b/builtin/remote.c
> @@ -80,14 +80,6 @@ static int verbose;
>  static int show_all(void);
>  static int prune_remote(const char *remote, int dry_run);
>  
> -static inline int postfixcmp(const char *string, const char *postfix)
> -{
> -	int len1 = strlen(string), len2 = strlen(postfix);
> -	if (len1 < len2)
> -		return 1;
> -	return strcmp(string + len1 - len2, postfix);
> -}
> -
>  static int fetch_remote(const char *name)
>  {
>  	const char *argv[] = { "fetch", name, NULL, NULL };
> @@ -277,13 +269,13 @@ static int config_read_branches(const char *key, const char *value, void *cb)
>  		enum { REMOTE, MERGE, REBASE } type;
>  
>  		key += 7;
> -		if (!postfixcmp(key, ".remote")) {
> +		if (!suffixcmp(key, ".remote")) {
>  			name = xstrndup(key, strlen(key) - 7);
>  			type = REMOTE;
> -		} else if (!postfixcmp(key, ".merge")) {
> +		} else if (!suffixcmp(key, ".merge")) {
>  			name = xstrndup(key, strlen(key) - 6);
>  			type = MERGE;
> -		} else if (!postfixcmp(key, ".rebase")) {
> +		} else if (!suffixcmp(key, ".rebase")) {
>  			name = xstrndup(key, strlen(key) - 7);
>  			type = REBASE;
>  		} else
Christian Couder· Nov 4, 2013, 20:16 UTC · re: Junio C Hamano · lore

Re: [PATCH] builtin/remote: remove postfixcmp() and use suffixcmp() instead

From: Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH] builtin/remote: remove postfixcmp() and use suffixcmp() instead
Date: Mon, 04 Nov 2013 11:19:43 -0800
Show 8 quoted lines
> Christian Couder <chriscool@tuxfamily.org> writes:
> 
>> Commit 8cc5b290 (git merge -X<option>, 25 Nov 2009) introduced
>> suffixcmp() with nearly the same implementation as postfixcmp()
>> that already existed since commit 211c8968 (Make git-remote a
>> builtin, 29 Feb 2008).
> 
> This "nearly the same" piqued my curiosity ;-)
Yeah, I realize I should have explained the differences.
 
Show 6 quoted lines
> The postfixcmp() you are removing will say "f" > ".txt" while
> suffixcmp() will say "f" < ".txt".
> 
> As postfixcmp() is only used to compare for equality, the
> distinction does not matter and does not affect the correctness of
> this patch, but I am not sure which answer is more correct.

Yeah, that's also my opinion. I am not even sure if there is one more correct answer than the other.

Show 20 quoted lines
> I do not think anybody sane uses prefixcmp() or suffixcmp() for
> anything but checking with zero; in other words, I suspect that all
> uses of Xcmp() can be replaced with !!Xcmp(), so as a separate
> clean-up patch, we may at least want to make it clear that the
> callers should not expect anything but "does str have sfx as its
> suffix, yes or no?" by doing something like this:
> 
>  int suffixcmp(const char *str, const char *suffix)
>  {
>  	int len = strlen(str), suflen = strlen(suffix);
>  	if (len < suflen)
>  		return -1;
>  	else
> -		return strcmp(str + len - suflen, suffix);
> +		return !!strcmp(str + len - suflen, suffix);
>  }
> 
> I am not absolutely sure about doing the same to prefixcmp(),
> though. It could be used for ordering, even though no existing code
> seems to do so.
I agree.

I will resend a 2 patch long patch series where the first patch will have an improved commit message and the second patch will do what you suggest above.

Thanks, Christian.

Junio C Hamano· Nov 4, 2013, 20:21 UTC · re: Junio C Hamano · lore

Re: [PATCH] builtin/remote: remove postfixcmp() and use suffixcmp() instead

Junio C Hamano <gitster@pobox.com> writes:
Show 20 quoted lines
> I do not think anybody sane uses prefixcmp() or suffixcmp() for
> anything but checking with zero; in other words, I suspect that all
> uses of Xcmp() can be replaced with !!Xcmp(), so as a separate
> clean-up patch, we may at least want to make it clear that the
> callers should not expect anything but "does str have sfx as its
> suffix, yes or no?" by doing something like this:
>
>  int suffixcmp(const char *str, const char *suffix)
>  {
>  	int len = strlen(str), suflen = strlen(suffix);
>  	if (len < suflen)
>  		return -1;
>  	else
> -		return strcmp(str + len - suflen, suffix);
> +		return !!strcmp(str + len - suflen, suffix);
>  }
>
> I am not absolutely sure about doing the same to prefixcmp(),
> though. It could be used for ordering, even though no existing code
> seems to do so.

I just realized why this suggestion is incomplete; if we were to go this route, we should rename the function to has_suffix() or something. anything-cmp() ought to be usable as an ordering comparison function, but suffixcmp() clearly isn't.

Johannes Schindelin· Nov 4, 2013, 22:17 UTC · re: Junio C Hamano · lore

Re: [PATCH] builtin/remote: remove postfixcmp() and use suffixcmp() instead

Hi Junio,
On Mon, 4 Nov 2013, Junio C Hamano wrote:
Show 27 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
> 
> > I do not think anybody sane uses prefixcmp() or suffixcmp() for
> > anything but checking with zero; in other words, I suspect that all
> > uses of Xcmp() can be replaced with !!Xcmp(), so as a separate
> > clean-up patch, we may at least want to make it clear that the
> > callers should not expect anything but "does str have sfx as its
> > suffix, yes or no?" by doing something like this:
> >
> >  int suffixcmp(const char *str, const char *suffix)
> >  {
> >  	int len = strlen(str), suflen = strlen(suffix);
> >  	if (len < suflen)
> >  		return -1;
> >  	else
> > -		return strcmp(str + len - suflen, suffix);
> > +		return !!strcmp(str + len - suflen, suffix);
> >  }
> >
> > I am not absolutely sure about doing the same to prefixcmp(),
> > though. It could be used for ordering, even though no existing code
> > seems to do so.
> 
> I just realized why this suggestion is incomplete; if we were to go
> this route, we should rename the function to has_suffix() or
> something. anything-cmp() ought to be usable as an ordering
> comparison function, but suffixcmp() clearly isn't.

I have to admit that I do not understand why a change in suffixcmp()'s behavior is needed.

Ciao, Johannes

Junio C Hamano· Nov 4, 2013, 22:37 UTC · re: Johannes Schindelin · lore

Re: [PATCH] builtin/remote: remove postfixcmp() and use suffixcmp() instead

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
Show 34 quoted lines
> Hi Junio,
>
> On Mon, 4 Nov 2013, Junio C Hamano wrote:
>
>> Junio C Hamano <gitster@pobox.com> writes:
>> 
>> > I do not think anybody sane uses prefixcmp() or suffixcmp() for
>> > anything but checking with zero; in other words, I suspect that all
>> > uses of Xcmp() can be replaced with !!Xcmp(), so as a separate
>> > clean-up patch, we may at least want to make it clear that the
>> > callers should not expect anything but "does str have sfx as its
>> > suffix, yes or no?" by doing something like this:
>> >
>> >  int suffixcmp(const char *str, const char *suffix)
>> >  {
>> >  	int len = strlen(str), suflen = strlen(suffix);
>> >  	if (len < suflen)
>> >  		return -1;
>> >  	else
>> > -		return strcmp(str + len - suflen, suffix);
>> > +		return !!strcmp(str + len - suflen, suffix);
>> >  }
>> >
>> > I am not absolutely sure about doing the same to prefixcmp(),
>> > though. It could be used for ordering, even though no existing code
>> > seems to do so.
>> 
>> I just realized why this suggestion is incomplete; if we were to go
>> this route, we should rename the function to has_suffix() or
>> something. anything-cmp() ought to be usable as an ordering
>> comparison function, but suffixcmp() clearly isn't.
>
> I have to admit that I do not understand why a change in suffixcmp()'s
> behavior is needed.

I made the suggestion only because I do not understand why the function should order "f" and ".txt" in the

	"f" < ".txt"

order. Even worse, the other function postfixcmp() orders them the other way around.

If -1 returned from the function were an indication of error "The string does not even have that suffix", then I would have been a bit more sympathetic, and its current behaviour in that case could be argued as a special case of the broader return value "non-zero (from the ordinary strcmp() return codeflow) means the string does not have that suffix and zero means the string ends with the suffix".

But then, a function that pretends to be for ordering comparison, with a name that ends with cmp(), and then declaring that "no, this is not for ordering; the sign of the result does not matter--what only matters is if it returns zero or non-zero", feels quite schizophrenic, at least to me.

And my earlier suggestion to change the return value *is* not a right change. It still keeps the pretense of comparison for ordering (i.e. ...cmp() name), while returning a value that cannot possibly be used for ordering.

So I think the right patch should make the function like this:
	int has_suffix(const char *str, const char *suffix)
        {
        	int len = strlen(str);
                int suffix_len = strlen(suffix);
                if (len < suffix_len)
                	return 0;
		return !strcmp(str + len - suffix_len, suffix);
	}

← back to recent threads