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>
Jeff King committed
Jun 18, 2014 at 15:48 UTC
95b567c7c3cf6b85d74b79424cdfbd40a7dee7c9
10 files changed
+44
-54
builtin/checkout.c
+2
-2
@@ -776,8 +776,8 @@ static int switch_branches(const struct checkout_opts *opts,
776
if (!(flag & REF_ISSYMREF))
777
old.path = NULL;
778
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);
781
782
if (!new->name) {
783
new->name = "HEAD";
builtin/fmt-merge-msg.c
+3
-3
@@ -100,7 +100,8 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
100
{
101
int i, len = strlen(line);
102
struct origin_data *origin_data;
103
- char *src, *origin;
103
+ char *src;
104
+ const char *origin;
105
struct src_data *src_data;
106
struct string_list_item *item;
107
int pulling_head = 0;
@@ -164,8 +165,7 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
165
origin = line;
166
string_list_append(&src_data->tag, origin + 4);
167
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)) {
169
string_list_append(&src_data->r_branch, origin);
170
src_data->head_status |= 2;
171
} else {
builtin/log.c
+6
-6
@@ -872,7 +872,7 @@ static char *find_branch_name(struct rev_info *rev)
872
int i, positive = -1;
873
unsigned char branch_sha1[20];
874
const unsigned char *tip_sha1;
875
- const char *ref;
875
+ const char *ref, *v;
876
char *full_ref, *branch = NULL;
877
878
for (i = 0; i < rev->cmdline.nr; i++) {
@@ -888,9 +888,9 @@ static char *find_branch_name(struct rev_info *rev)
888
ref = rev->cmdline.rev[positive].name;
889
tip_sha1 = rev->cmdline.rev[positive].item->sha1;
890
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) &&
892
!hashcmp(tip_sha1, branch_sha1))
893
- branch = xstrdup(full_ref + strlen("refs/heads/"));
893
+ branch = xstrdup(v);
894
free(full_ref);
895
return branch;
896
}
@@ -1394,10 +1394,10 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1394
1395
if (check_head) {
1396
unsigned char sha1[20];
1397
- const char *ref;
1397
+ const char *ref, *v;
1398
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);
1401
else
1402
branch_name = xstrdup(""); /* no branch */
1403
}
diff.c
+9
-18
@@ -3395,12 +3395,10 @@ int parse_long_opt(const char *opt, const char **argv,
3395
const char **optarg)
3396
{
3397
const char *arg = argv[0];
3398
- if (arg[0] != '-' || arg[1] != '-')
3398
+ if (!skip_prefix(arg, "--", &arg))
3399
return 0;
3400
- arg += strlen("--");
3401
- if (!starts_with(arg, opt))
3400
+ if (!skip_prefix(arg, opt, &arg))
3401
return 0;
3403
- arg += strlen(opt);
3402
if (*arg == '=') { /* stuck form: --option=value */
3403
*optarg = arg + 1;
3404
return 1;
@@ -3429,8 +3427,7 @@ static int stat_opt(struct diff_options *options, const char **av)
3427
3428
switch (*arg) {
3429
case '-':
3432
- if (starts_with(arg, "-width")) {
3433
- arg += strlen("-width");
3430
+ if (skip_prefix(arg, "-width", &arg)) {
3431
if (*arg == '=')
3432
width = strtoul(arg + 1, &end, 10);
3433
else if (!*arg && !av[1])
@@ -3439,8 +3436,7 @@ static int stat_opt(struct diff_options *options, const char **av)
3436
width = strtoul(av[1], &end, 10);
3437
argcount = 2;
3438
}
3442
- } else if (starts_with(arg, "-name-width")) {
3443
- arg += strlen("-name-width");
3439
+ } else if (skip_prefix(arg, "-name-width", &arg)) {
3440
if (*arg == '=')
3441
name_width = strtoul(arg + 1, &end, 10);
3442
else if (!*arg && !av[1])
@@ -3449,8 +3445,7 @@ static int stat_opt(struct diff_options *options, const char **av)
3445
name_width = strtoul(av[1], &end, 10);
3446
argcount = 2;
3447
}
3452
- } else if (starts_with(arg, "-graph-width")) {
3453
- arg += strlen("-graph-width");
3448
+ } else if (skip_prefix(arg, "-graph-width", &arg)) {
3449
if (*arg == '=')
3450
graph_width = strtoul(arg + 1, &end, 10);
3451
else if (!*arg && !av[1])
@@ -3459,8 +3454,7 @@ static int stat_opt(struct diff_options *options, const char **av)
3454
graph_width = strtoul(av[1], &end, 10);
3455
argcount = 2;
3456
}
3462
- } else if (starts_with(arg, "-count")) {
3463
- arg += strlen("-count");
3457
+ } else if (skip_prefix(arg, "-count", &arg)) {
3458
if (*arg == '=')
3459
count = strtoul(arg + 1, &end, 10);
3460
else if (!*arg && !av[1])
@@ -3905,16 +3899,13 @@ static int diff_scoreopt_parse(const char *opt)
3899
cmd = *opt++;
3900
if (cmd == '-') {
3901
/* 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)) {
3903
if (*opt == 0 || *opt++ == '=')
3904
cmd = 'B';
3912
- } else if (starts_with(opt, "find-copies")) {
3913
- opt += strlen("find-copies");
3905
+ } else if (skip_prefix(opt, "find-copies", &opt)) {
3906
if (*opt == 0 || *opt++ == '=')
3907
cmd = 'C';
3916
- } else if (starts_with(opt, "find-renames")) {
3917
- opt += strlen("find-renames");
3908
+ } else if (skip_prefix(opt, "find-renames", &opt)) {
3909
if (*opt == 0 || *opt++ == '=')
3910
cmd = 'M';
3911
}
help.c
+4
-3
@@ -414,11 +414,12 @@ static int append_similar_ref(const char *refname, const unsigned char *sha1,
414
{
415
struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
416
char *branch = strrchr(refname, '/') + 1;
417
+ const char *remote;
418
+
419
/* 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) &&
421
!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);
423
return 0;
424
}
425
merge-recursive.c
+8
-7
@@ -2063,6 +2063,8 @@ void init_merge_options(struct merge_options *o)
2063
2064
int parse_merge_opt(struct merge_options *o, const char *s)
2065
{
2066
+ const char *arg;
2067
+
2068
if (!s || !*s)
2069
return -1;
2070
if (!strcmp(s, "ours"))
@@ -2071,14 +2073,14 @@ int parse_merge_opt(struct merge_options *o, const char *s)
2073
o->recursive_variant = MERGE_RECURSIVE_THEIRS;
2074
else if (!strcmp(s, "subtree"))
2075
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;
2078
else if (!strcmp(s, "patience"))
2079
o->xdl_opts = DIFF_WITH_ALG(o, PATIENCE_DIFF);
2080
else if (!strcmp(s, "histogram"))
2081
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);
2084
if (value < 0)
2085
return -1;
2086
/* clear out previous settings */
@@ -2096,9 +2098,8 @@ int parse_merge_opt(struct merge_options *o, const char *s)
2098
o->renormalize = 1;
2099
else if (!strcmp(s, "no-renormalize"))
2100
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)
2103
return -1;
2104
}
2105
else
pretty.c
+1
-2
@@ -40,10 +40,9 @@ static int git_pretty_formats_config(const char *var, const char *value, void *c
40
const char *fmt;
41
int i;
42
43
- if (!starts_with(var, "pretty."))
43
+ if (!skip_prefix(var, "pretty.", &name))
44
return 0;
45
46
- name = var + strlen("pretty.");
46
for (i = 0; i < builtin_formats_len; i++) {
47
if (!strcmp(commit_formats[i].name, name))
48
return 0;
remote-curl.c
+8
-7
@@ -791,9 +791,9 @@ static void parse_fetch(struct strbuf *buf)
791
int alloc_heads = 0, nr_heads = 0;
792
793
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;
797
struct ref *ref;
798
unsigned char old_sha1[20];
799
@@ -968,6 +968,8 @@ int main(int argc, const char **argv)
968
http_init(remote, url.buf, 0);
969
970
do {
971
+ const char *arg;
972
+
973
if (strbuf_getline(&buf, stdin, '\n') == EOF) {
974
if (ferror(stdin))
975
fprintf(stderr, "Error reading command stream\n");
@@ -989,9 +991,8 @@ int main(int argc, const char **argv)
991
} else if (starts_with(buf.buf, "push ")) {
992
parse_push(&buf);
993
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, ' ');
996
int result;
997
998
if (value)
@@ -999,7 +1000,7 @@ int main(int argc, const char **argv)
1000
else
1001
value = "true";
1002
1002
- result = set_option(name, value);
1003
+ result = set_option(arg, value);
1004
if (!result)
1005
printf("ok\n");
1006
else if (result < 0)
remote.c
+2
-3
@@ -488,9 +488,8 @@ static void read_config(void)
488
current_branch = NULL;
489
head_ref = resolve_ref_unsafe("HEAD", sha1, 0, &flag);
490
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);
493
}
494
git_config(handle_config, NULL);
495
if (branch_pushremote_name) {
sha1_name.c
+1
-3
@@ -911,10 +911,8 @@ static int grab_nth_branch_switch(unsigned char *osha1, unsigned char *nsha1,
911
const char *match = NULL, *target = NULL;
912
size_t len;
913
914
- if (starts_with(message, "checkout: moving from ")) {
915
- match = message + strlen("checkout: moving from ");
914
+ if (skip_prefix(message, "checkout: moving from ", &match))
915
target = strstr(match, " to ");
917
- }
916
917
if (!match || !target)
918
return 0;