{"thread":{"id":"37451","subject":"[PATCH] connect: simplify check_ref() using skip_prefix() and starts_with()","startedAt":"2014-08-30T09:46:54Z","lastAt":"2014-08-30T09:46:54Z","messageCount":1,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"248641","messageId":"54019D8E.8020309@web.de","threadId":"37451","inReplyTo":null,"subject":"[PATCH] connect: simplify check_ref() using skip_prefix() and starts_with()","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2014-08-30T09:46:54Z","receivedAt":"2014-08-30T09:46:54Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Both callers of check_ref() pass in NUL-terminated strings for name.\nRemove the len parameter and then use skip_prefix() and starts_with()\ninstead of memcmp() to check if it starts with certain strings.  This\ngets rid of several magic string length constants and a strlen() call.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n connect.c | 16 ++++++----------\n 1 file changed, 6 insertions(+), 10 deletions(-)\n\ndiff --git a/connect.c b/connect.c\nindex 5047402..34193e5 100644\n--- a/connect.c\n+++ b/connect.c\n@@ -13,28 +13,24 @@\n static char *server_capabilities;\n static const char *parse_feature_value(const char *, const char *, int *);\n \n-static int check_ref(const char *name, int len, unsigned int flags)\n+static int check_ref(const char *name, unsigned int flags)\n {\n \tif (!flags)\n \t\treturn 1;\n \n-\tif (len < 5 || memcmp(name, \"refs/\", 5))\n+\tif (!skip_prefix(name, \"refs/\", &name))\n \t\treturn 0;\n \n-\t/* Skip the \"refs/\" part */\n-\tname += 5;\n-\tlen -= 5;\n-\n \t/* REF_NORMAL means that we don't want the magic fake tag refs */\n \tif ((flags & REF_NORMAL) && check_refname_format(name, 0))\n \t\treturn 0;\n \n \t/* REF_HEADS means that we want regular branch heads */\n-\tif ((flags & REF_HEADS) && !memcmp(name, \"heads/\", 6))\n+\tif ((flags & REF_HEADS) && starts_with(name, \"heads/\"))\n \t\treturn 1;\n \n \t/* REF_TAGS means that we want tags */\n-\tif ((flags & REF_TAGS) && !memcmp(name, \"tags/\", 5))\n+\tif ((flags & REF_TAGS) && starts_with(name, \"tags/\"))\n \t\treturn 1;\n \n \t/* All type bits clear means that we are ok with anything */\n@@ -43,7 +39,7 @@ static int check_ref(const char *name, int len, unsigned int flags)\n \n int check_ref_type(const struct ref *ref, int flags)\n {\n-\treturn check_ref(ref->name, strlen(ref->name), flags);\n+\treturn check_ref(ref->name, flags);\n }\n \n static void die_initial_contact(int got_at_least_one_head)\n@@ -167,7 +163,7 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,\n \t\t\tcontinue;\n \t\t}\n \n-\t\tif (!check_ref(name, name_len, flags))\n+\t\tif (!check_ref(name, flags))\n \t\t\tcontinue;\n \t\tref = alloc_ref(buffer + 41);\n \t\thashcpy(ref->old_sha1, old_sha1);\n-- \n2.1.0\n"}]}