use skip_prefix to avoid repeating strings

It's a common idiom to match a prefix and then skip past it
with strlen, like:

  if (starts_with(foo, "bar"))
	  foo += strlen("bar");

This avoids magic numbers, but means we have to repeat the
string (and there is no compiler check that we didn't make a
typo in one of the strings).

We can use skip_prefix to handle this case without repeating
ourselves.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King
2014-06-18 15:48:29 -04:00
committed by Junio C Hamano
parent ae021d8791
commit 95b567c7c3
10 changed files with 44 additions and 54 deletions

7
help.c
View File

@ -414,11 +414,12 @@ static int append_similar_ref(const char *refname, const unsigned char *sha1,
{
struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
char *branch = strrchr(refname, '/') + 1;
const char *remote;
/* A remote branch of the same name is deemed similar */
if (starts_with(refname, "refs/remotes/") &&
if (skip_prefix(refname, "refs/remotes/", &remote) &&
!strcmp(branch, cb->base_ref))
string_list_append(cb->similar_refs,
refname + strlen("refs/remotes/"));
string_list_append(cb->similar_refs, remote);
return 0;
}