use skip_prefix to avoid repeated calculations

In some cases, we use starts_with to check for a prefix, and then use an already-calculated prefix length to advance a pointer past the prefix. There are no magic numbers or duplicated strings here, but we can still make the code simpler and more obvious by using skip_prefix. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 18, 2014 at 15:57 UTC de8118e153c5e527263086605e437ccca5d4f1ef
2 files changed +6 -8
help.c
+5 -6
@@ -129,7 +129,6 @@ static void list_commands_in_dir(struct cmdnames *cmds,
129 const char *path,
130 const char *prefix)
131 {
132 - int prefix_len;
132 DIR *dir = opendir(path);
133 struct dirent *de;
134 struct strbuf buf = STRBUF_INIT;
@@ -139,15 +138,15 @@ static void list_commands_in_dir(struct cmdnames *cmds,
138 return;
139 if (!prefix)
140 prefix = "git-";
142 - prefix_len = strlen(prefix);
141
142 strbuf_addf(&buf, "%s/", path);
143 len = buf.len;
144
145 while ((de = readdir(dir)) != NULL) {
146 + const char *ent;
147 int entlen;
148
150 - if (!starts_with(de->d_name, prefix))
149 + if (!skip_prefix(de->d_name, prefix, &ent))
150 continue;
151
152 strbuf_setlen(&buf, len);
@@ -155,11 +154,11 @@ static void list_commands_in_dir(struct cmdnames *cmds,
154 if (!is_executable(buf.buf))
155 continue;
156
158 - entlen = strlen(de->d_name) - prefix_len;
159 - if (has_extension(de->d_name, ".exe"))
157 + entlen = strlen(ent);
158 + if (has_extension(ent, ".exe"))
159 entlen -= 4;
160
162 - add_cmdname(cmds, de->d_name + prefix_len, entlen);
161 + add_cmdname(cmds, ent, entlen);
162 }
163 closedir(dir);
164 strbuf_release(&buf);
http.c
+1 -2
@@ -1087,11 +1087,10 @@ static int update_url_from_redirect(struct strbuf *base,
1087 if (!strcmp(asked, got->buf))
1088 return 0;
1089
1090 - if (!starts_with(asked, base->buf))
1090 + if (!skip_prefix(asked, base->buf, &tail))
1091 die("BUG: update_url_from_redirect: %s is not a superset of %s",
1092 asked, base->buf);
1093
1094 - tail = asked + base->len;
1094 tail_len = strlen(tail);
1095
1096 if (got->len < tail_len ||