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

View File

@@ -791,9 +791,9 @@ static void parse_fetch(struct strbuf *buf)
int alloc_heads = 0, nr_heads = 0;
do {
if (starts_with(buf->buf, "fetch ")) {
char *p = buf->buf + strlen("fetch ");
char *name;
const char *p;
if (skip_prefix(buf->buf, "fetch ", &p)) {
const char *name;
struct ref *ref;
unsigned char old_sha1[20];
@@ -968,6 +968,8 @@ int main(int argc, const char **argv)
http_init(remote, url.buf, 0);
do {
const char *arg;
if (strbuf_getline(&buf, stdin, '\n') == EOF) {
if (ferror(stdin))
fprintf(stderr, "Error reading command stream\n");
@@ -989,9 +991,8 @@ int main(int argc, const char **argv)
} else if (starts_with(buf.buf, "push ")) {
parse_push(&buf);
} else if (starts_with(buf.buf, "option ")) {
char *name = buf.buf + strlen("option ");
char *value = strchr(name, ' ');
} else if (skip_prefix(buf.buf, "option ", &arg)) {
char *value = strchr(arg, ' ');
int result;
if (value)
@@ -999,7 +1000,7 @@ int main(int argc, const char **argv)
else
value = "true";
result = set_option(name, value);
result = set_option(arg, value);
if (!result)
printf("ok\n");
else if (result < 0)