Skip to content

Commit 95b567c

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

File tree

10 files changed

+44
-54
lines changed

10 files changed

+44
-54
lines changed

builtin/checkout.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,8 +776,8 @@ static int switch_branches(const struct checkout_opts *opts,
776776
if (!(flag & REF_ISSYMREF))
777777
old.path = NULL;
778778

779-
if (old.path && starts_with(old.path, "refs/heads/"))
780-
old.name = old.path + strlen("refs/heads/");
779+
if (old.path)
780+
skip_prefix(old.path, "refs/heads/", &old.name);
781781

782782
if (!new->name) {
783783
new->name = "HEAD";

builtin/fmt-merge-msg.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
100100
{
101101
int i, len = strlen(line);
102102
struct origin_data *origin_data;
103-
char *src, *origin;
103+
char *src;
104+
const char *origin;
104105
struct src_data *src_data;
105106
struct string_list_item *item;
106107
int pulling_head = 0;
@@ -164,8 +165,7 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
164165
origin = line;
165166
string_list_append(&src_data->tag, origin + 4);
166167
src_data->head_status |= 2;
167-
} else if (starts_with(line, "remote-tracking branch ")) {
168-
origin = line + strlen("remote-tracking branch ");
168+
} else if (skip_prefix(line, "remote-tracking branch ", &origin)) {
169169
string_list_append(&src_data->r_branch, origin);
170170
src_data->head_status |= 2;
171171
} else {

builtin/log.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ static char *find_branch_name(struct rev_info *rev)
872872
int i, positive = -1;
873873
unsigned char branch_sha1[20];
874874
const unsigned char *tip_sha1;
875-
const char *ref;
875+
const char *ref, *v;
876876
char *full_ref, *branch = NULL;
877877

878878
for (i = 0; i < rev->cmdline.nr; i++) {
@@ -888,9 +888,9 @@ static char *find_branch_name(struct rev_info *rev)
888888
ref = rev->cmdline.rev[positive].name;
889889
tip_sha1 = rev->cmdline.rev[positive].item->sha1;
890890
if (dwim_ref(ref, strlen(ref), branch_sha1, &full_ref) &&
891-
starts_with(full_ref, "refs/heads/") &&
891+
skip_prefix(full_ref, "refs/heads/", &v) &&
892892
!hashcmp(tip_sha1, branch_sha1))
893-
branch = xstrdup(full_ref + strlen("refs/heads/"));
893+
branch = xstrdup(v);
894894
free(full_ref);
895895
return branch;
896896
}
@@ -1394,10 +1394,10 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
13941394

13951395
if (check_head) {
13961396
unsigned char sha1[20];
1397-
const char *ref;
1397+
const char *ref, *v;
13981398
ref = resolve_ref_unsafe("HEAD", sha1, 1, NULL);
1399-
if (ref && starts_with(ref, "refs/heads/"))
1400-
branch_name = xstrdup(ref + strlen("refs/heads/"));
1399+
if (ref && skip_prefix(ref, "refs/heads/", &v))
1400+
branch_name = xstrdup(v);
14011401
else
14021402
branch_name = xstrdup(""); /* no branch */
14031403
}

diff.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,12 +3395,10 @@ int parse_long_opt(const char *opt, const char **argv,
33953395
const char **optarg)
33963396
{
33973397
const char *arg = argv[0];
3398-
if (arg[0] != '-' || arg[1] != '-')
3398+
if (!skip_prefix(arg, "--", &arg))
33993399
return 0;
3400-
arg += strlen("--");
3401-
if (!starts_with(arg, opt))
3400+
if (!skip_prefix(arg, opt, &arg))
34023401
return 0;
3403-
arg += strlen(opt);
34043402
if (*arg == '=') { /* stuck form: --option=value */
34053403
*optarg = arg + 1;
34063404
return 1;
@@ -3429,8 +3427,7 @@ static int stat_opt(struct diff_options *options, const char **av)
34293427

34303428
switch (*arg) {
34313429
case '-':
3432-
if (starts_with(arg, "-width")) {
3433-
arg += strlen("-width");
3430+
if (skip_prefix(arg, "-width", &arg)) {
34343431
if (*arg == '=')
34353432
width = strtoul(arg + 1, &end, 10);
34363433
else if (!*arg && !av[1])
@@ -3439,8 +3436,7 @@ static int stat_opt(struct diff_options *options, const char **av)
34393436
width = strtoul(av[1], &end, 10);
34403437
argcount = 2;
34413438
}
3442-
} else if (starts_with(arg, "-name-width")) {
3443-
arg += strlen("-name-width");
3439+
} else if (skip_prefix(arg, "-name-width", &arg)) {
34443440
if (*arg == '=')
34453441
name_width = strtoul(arg + 1, &end, 10);
34463442
else if (!*arg && !av[1])
@@ -3449,8 +3445,7 @@ static int stat_opt(struct diff_options *options, const char **av)
34493445
name_width = strtoul(av[1], &end, 10);
34503446
argcount = 2;
34513447
}
3452-
} else if (starts_with(arg, "-graph-width")) {
3453-
arg += strlen("-graph-width");
3448+
} else if (skip_prefix(arg, "-graph-width", &arg)) {
34543449
if (*arg == '=')
34553450
graph_width = strtoul(arg + 1, &end, 10);
34563451
else if (!*arg && !av[1])
@@ -3459,8 +3454,7 @@ static int stat_opt(struct diff_options *options, const char **av)
34593454
graph_width = strtoul(av[1], &end, 10);
34603455
argcount = 2;
34613456
}
3462-
} else if (starts_with(arg, "-count")) {
3463-
arg += strlen("-count");
3457+
} else if (skip_prefix(arg, "-count", &arg)) {
34643458
if (*arg == '=')
34653459
count = strtoul(arg + 1, &end, 10);
34663460
else if (!*arg && !av[1])
@@ -3905,16 +3899,13 @@ static int diff_scoreopt_parse(const char *opt)
39053899
cmd = *opt++;
39063900
if (cmd == '-') {
39073901
/* convert the long-form arguments into short-form versions */
3908-
if (starts_with(opt, "break-rewrites")) {
3909-
opt += strlen("break-rewrites");
3902+
if (skip_prefix(opt, "break-rewrites", &opt)) {
39103903
if (*opt == 0 || *opt++ == '=')
39113904
cmd = 'B';
3912-
} else if (starts_with(opt, "find-copies")) {
3913-
opt += strlen("find-copies");
3905+
} else if (skip_prefix(opt, "find-copies", &opt)) {
39143906
if (*opt == 0 || *opt++ == '=')
39153907
cmd = 'C';
3916-
} else if (starts_with(opt, "find-renames")) {
3917-
opt += strlen("find-renames");
3908+
} else if (skip_prefix(opt, "find-renames", &opt)) {
39183909
if (*opt == 0 || *opt++ == '=')
39193910
cmd = 'M';
39203911
}

help.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,12 @@ static int append_similar_ref(const char *refname, const unsigned char *sha1,
414414
{
415415
struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
416416
char *branch = strrchr(refname, '/') + 1;
417+
const char *remote;
418+
417419
/* A remote branch of the same name is deemed similar */
418-
if (starts_with(refname, "refs/remotes/") &&
420+
if (skip_prefix(refname, "refs/remotes/", &remote) &&
419421
!strcmp(branch, cb->base_ref))
420-
string_list_append(cb->similar_refs,
421-
refname + strlen("refs/remotes/"));
422+
string_list_append(cb->similar_refs, remote);
422423
return 0;
423424
}
424425

merge-recursive.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,6 +2063,8 @@ void init_merge_options(struct merge_options *o)
20632063

20642064
int parse_merge_opt(struct merge_options *o, const char *s)
20652065
{
2066+
const char *arg;
2067+
20662068
if (!s || !*s)
20672069
return -1;
20682070
if (!strcmp(s, "ours"))
@@ -2071,14 +2073,14 @@ int parse_merge_opt(struct merge_options *o, const char *s)
20712073
o->recursive_variant = MERGE_RECURSIVE_THEIRS;
20722074
else if (!strcmp(s, "subtree"))
20732075
o->subtree_shift = "";
2074-
else if (starts_with(s, "subtree="))
2075-
o->subtree_shift = s + strlen("subtree=");
2076+
else if (skip_prefix(s, "subtree=", &arg))
2077+
o->subtree_shift = arg;
20762078
else if (!strcmp(s, "patience"))
20772079
o->xdl_opts = DIFF_WITH_ALG(o, PATIENCE_DIFF);
20782080
else if (!strcmp(s, "histogram"))
20792081
o->xdl_opts = DIFF_WITH_ALG(o, HISTOGRAM_DIFF);
2080-
else if (starts_with(s, "diff-algorithm=")) {
2081-
long value = parse_algorithm_value(s + strlen("diff-algorithm="));
2082+
else if (skip_prefix(s, "diff-algorithm=", &arg)) {
2083+
long value = parse_algorithm_value(arg);
20822084
if (value < 0)
20832085
return -1;
20842086
/* clear out previous settings */
@@ -2096,9 +2098,8 @@ int parse_merge_opt(struct merge_options *o, const char *s)
20962098
o->renormalize = 1;
20972099
else if (!strcmp(s, "no-renormalize"))
20982100
o->renormalize = 0;
2099-
else if (starts_with(s, "rename-threshold=")) {
2100-
const char *score = s + strlen("rename-threshold=");
2101-
if ((o->rename_score = parse_rename_score(&score)) == -1 || *score != 0)
2101+
else if (skip_prefix(s, "rename-threshold=", &arg)) {
2102+
if ((o->rename_score = parse_rename_score(&arg)) == -1 || *arg != 0)
21022103
return -1;
21032104
}
21042105
else

pretty.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ static int git_pretty_formats_config(const char *var, const char *value, void *c
4040
const char *fmt;
4141
int i;
4242

43-
if (!starts_with(var, "pretty."))
43+
if (!skip_prefix(var, "pretty.", &name))
4444
return 0;
4545

46-
name = var + strlen("pretty.");
4746
for (i = 0; i < builtin_formats_len; i++) {
4847
if (!strcmp(commit_formats[i].name, name))
4948
return 0;

remote-curl.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,9 @@ static void parse_fetch(struct strbuf *buf)
791791
int alloc_heads = 0, nr_heads = 0;
792792

793793
do {
794-
if (starts_with(buf->buf, "fetch ")) {
795-
char *p = buf->buf + strlen("fetch ");
796-
char *name;
794+
const char *p;
795+
if (skip_prefix(buf->buf, "fetch ", &p)) {
796+
const char *name;
797797
struct ref *ref;
798798
unsigned char old_sha1[20];
799799

@@ -968,6 +968,8 @@ int main(int argc, const char **argv)
968968
http_init(remote, url.buf, 0);
969969

970970
do {
971+
const char *arg;
972+
971973
if (strbuf_getline(&buf, stdin, '\n') == EOF) {
972974
if (ferror(stdin))
973975
fprintf(stderr, "Error reading command stream\n");
@@ -989,17 +991,16 @@ int main(int argc, const char **argv)
989991
} else if (starts_with(buf.buf, "push ")) {
990992
parse_push(&buf);
991993

992-
} else if (starts_with(buf.buf, "option ")) {
993-
char *name = buf.buf + strlen("option ");
994-
char *value = strchr(name, ' ');
994+
} else if (skip_prefix(buf.buf, "option ", &arg)) {
995+
char *value = strchr(arg, ' ');
995996
int result;
996997

997998
if (value)
998999
*value++ = '\0';
9991000
else
10001001
value = "true";
10011002

1002-
result = set_option(name, value);
1003+
result = set_option(arg, value);
10031004
if (!result)
10041005
printf("ok\n");
10051006
else if (result < 0)

remote.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,8 @@ static void read_config(void)
488488
current_branch = NULL;
489489
head_ref = resolve_ref_unsafe("HEAD", sha1, 0, &flag);
490490
if (head_ref && (flag & REF_ISSYMREF) &&
491-
starts_with(head_ref, "refs/heads/")) {
492-
current_branch =
493-
make_branch(head_ref + strlen("refs/heads/"), 0);
491+
skip_prefix(head_ref, "refs/heads/", &head_ref)) {
492+
current_branch = make_branch(head_ref, 0);
494493
}
495494
git_config(handle_config, NULL);
496495
if (branch_pushremote_name) {

sha1_name.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -911,10 +911,8 @@ static int grab_nth_branch_switch(unsigned char *osha1, unsigned char *nsha1,
911911
const char *match = NULL, *target = NULL;
912912
size_t len;
913913

914-
if (starts_with(message, "checkout: moving from ")) {
915-
match = message + strlen("checkout: moving from ");
914+
if (skip_prefix(message, "checkout: moving from ", &match))
916915
target = strstr(match, " to ");
917-
}
918916

919917
if (!match || !target)
920918
return 0;

0 commit comments

Comments
 (0)