git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Christian Couder <christian.couder@gmail.com>
Cc: git@vger.kernel.org, Lukas Fleischer <lfleischer@lfos.de>,
	Jeff King <peff@peff.net>,
	Christian Couder <chriscool@tuxfamily.org>
Subject: Re: [PATCH] refs: use skip_prefix() in ref_is_hidden()
Date: Fri, 21 Jul 2017 09:55:52 -0700	[thread overview]
Message-ID: <xmqqk231hgpz.fsf@gitster.mtv.corp.google.com> (raw)
In-Reply-To: <20170721095757.28373-1-chriscool@tuxfamily.org> (Christian Couder's message of "Fri, 21 Jul 2017 11:57:57 +0200")

Christian Couder <christian.couder@gmail.com> writes:

> This saves one line, makes the code a bit easier to understand
> and perhaps a bit faster too.
>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  refs.c | 7 +++----
>  1 file changed, 3 insertions(+), 4 deletions(-)
>
> diff --git a/refs.c b/refs.c
> index ba22f4acef..15cb36d426 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -1160,7 +1160,7 @@ int ref_is_hidden(const char *refname, const char *refname_full)
>  		const char *match = hide_refs->items[i].string;
>  		const char *subject;
>  		int neg = 0;
> -		int len;
> +		const char *p;
>  
>  		if (*match == '!') {
>  			neg = 1;
> @@ -1175,10 +1175,9 @@ int ref_is_hidden(const char *refname, const char *refname_full)
>  		}
>  
>  		/* refname can be NULL when namespaces are used. */
> -		if (!subject || !starts_with(subject, match))
> +		if (!subject || !skip_prefix(subject, match, &p))
>  			continue;
> -		len = strlen(match);
> -		if (!subject[len] || subject[len] == '/')
> +		if (!*p || *p == '/')
>  			return !neg;
>  	}
>  	return 0;

If we were to rewrite this using skip_prefix(), I would imagine we
would rather write it this way:

	if (subject &&
	    skip_prefix(subject, match, &p) &&
	    (*p || *p != '/'))
		return !neg;

because it would be shorter and make the logic easier to follow: 

    We make the final decision only when "subject" is there, its
    early part matches "match", and the match is at the slash
    boundary (or the whole thing).

The original wanted to say the same thing, but it had to split that
logic into two only because it needed an assignment to "len" in the
middle, and because doing an assignment inside a conditional part of
if() statement is frowned upon.




      reply	other threads:[~2017-07-21 16:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-21  9:57 [PATCH] refs: use skip_prefix() in ref_is_hidden() Christian Couder
2017-07-21 16:55 ` Junio C Hamano [this message]

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=xmqqk231hgpz.fsf@gitster.mtv.corp.google.com \
    --to=gitster@pobox.com \
    --cc=chriscool@tuxfamily.org \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=lfleischer@lfos.de \
    --cc=peff@peff.net \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).