git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] use skip_prefix() to avoid more magic numbers

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 7, 2014, 18:23 UTC
Message-ID
<xmqqd2a3g2mf.fsf@gitster.dls.corp.google.com>
In-Reply-To
<5430427A.5080800@web.de>
René Scharfe <l.s.r@web.de> writes:
Show 24 quoted lines
> @@ -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.
Previous: Jeff KingNext: René Scharfe
Message 11 of 13 in “use skip_prefix() to avoid more magic numbers”
  1. use skip_prefix() to avoid more magic numbersRené Scharfe, Oct 4, 2014
  2. Jonathan NiederOct 5, 2014
  3. Jeff KingOct 6, 2014
  4. Junio C HamanoOct 7, 2014
  5. Jeff KingOct 7, 2014
  6. Jeff KingOct 7, 2014
  7. Junio C HamanoOct 7, 2014
  8. Junio C HamanoOct 7, 2014
  9. Jeff KingOct 7, 2014
  10. Jeff KingOct 6, 2014
  11. Junio C HamanoOct 7, 2014
  12. René ScharfeOct 9, 2014
  13. Junio C HamanoOct 9, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.