All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "René Scharfe" <l.s.r@web.de>
Cc: Git Mailing List <git@vger.kernel.org>, Jeff King <peff@peff.net>
Subject: Re: [PATCH] use skip_prefix() to avoid more magic numbers
Date: Tue, 07 Oct 2014 11:23:52 -0700	[thread overview]
Message-ID: <xmqqd2a3g2mf.fsf@gitster.dls.corp.google.com> (raw)
In-Reply-To: <5430427A.5080800@web.de> ("René Scharfe"'s message of "Sat, 04 Oct 2014 20:54:50 +0200")

René Scharfe <l.s.r@web.de> writes:

> @@ -335,20 +337,18 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
>  	static struct {
>  		int kind;
>  		const char *prefix;
> -		int pfxlen;
>  	} ref_kind[] = {
> -		{ REF_LOCAL_BRANCH, "refs/heads/", 11 },
> -		{ REF_REMOTE_BRANCH, "refs/remotes/", 13 },
> +		{ REF_LOCAL_BRANCH, "refs/heads/" },
> +		{ REF_REMOTE_BRANCH, "refs/remotes/" },
>  	};
>  
>  	/* Detect kind */
>  	for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
>  		prefix = ref_kind[i].prefix;
> -		if (strncmp(refname, prefix, ref_kind[i].pfxlen))
> -			continue;
> -		kind = ref_kind[i].kind;
> -		refname += ref_kind[i].pfxlen;
> -		break;
> +		if (skip_prefix(refname, prefix, &refname)) {
> +			kind = ref_kind[i].kind;
> +			break;
> +		}

This certainly makes it easier to read.

I suspect that the original was done as a (possibly premature)
optimization to avoid having to do strlen(3) on a variable that
points at constant strings for each and every ref we iterate with
for_each_rawref(), and it is somewhat sad to see it lost because
skip_prefix() assumes that the caller never knows the length of the
prefix, though.

Thanks.

  parent reply	other threads:[~2014-10-07 18:24 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-04 18:54 [PATCH] use skip_prefix() to avoid more magic numbers René Scharfe
2014-10-05 22:49 ` Jonathan Nieder
2014-10-06  1:18   ` Jeff King
2014-10-07 18:14     ` Junio C Hamano
2014-10-07 19:16       ` Jeff King
2014-10-07 19:33         ` Jeff King
2014-10-07 19:47           ` Junio C Hamano
2014-10-07 18:21     ` Junio C Hamano
2014-10-07 18:31       ` Jeff King
2014-10-06  1:35 ` Jeff King
2014-10-07 18:23 ` Junio C Hamano [this message]
2014-10-09 20:06   ` René Scharfe
2014-10-09 20:12     ` Junio C Hamano

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=xmqqd2a3g2mf.fsf@gitster.dls.corp.google.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=l.s.r@web.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.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.