Skip to content

Commit de8118e

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6d87780 commit de8118e

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

help.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ static void list_commands_in_dir(struct cmdnames *cmds,
129129
const char *path,
130130
const char *prefix)
131131
{
132-
int prefix_len;
133132
DIR *dir = opendir(path);
134133
struct dirent *de;
135134
struct strbuf buf = STRBUF_INIT;
@@ -139,27 +138,27 @@ static void list_commands_in_dir(struct cmdnames *cmds,
139138
return;
140139
if (!prefix)
141140
prefix = "git-";
142-
prefix_len = strlen(prefix);
143141

144142
strbuf_addf(&buf, "%s/", path);
145143
len = buf.len;
146144

147145
while ((de = readdir(dir)) != NULL) {
146+
const char *ent;
148147
int entlen;
149148

150-
if (!starts_with(de->d_name, prefix))
149+
if (!skip_prefix(de->d_name, prefix, &ent))
151150
continue;
152151

153152
strbuf_setlen(&buf, len);
154153
strbuf_addstr(&buf, de->d_name);
155154
if (!is_executable(buf.buf))
156155
continue;
157156

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"))
160159
entlen -= 4;
161160

162-
add_cmdname(cmds, de->d_name + prefix_len, entlen);
161+
add_cmdname(cmds, ent, entlen);
163162
}
164163
closedir(dir);
165164
strbuf_release(&buf);

http.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,11 +1087,10 @@ static int update_url_from_redirect(struct strbuf *base,
10871087
if (!strcmp(asked, got->buf))
10881088
return 0;
10891089

1090-
if (!starts_with(asked, base->buf))
1090+
if (!skip_prefix(asked, base->buf, &tail))
10911091
die("BUG: update_url_from_redirect: %s is not a superset of %s",
10921092
asked, base->buf);
10931093

1094-
tail = asked + base->len;
10951094
tail_len = strlen(tail);
10961095

10971096
if (got->len < tail_len ||

0 commit comments

Comments
 (0)