It's a common idiom to match a prefix and then skip past it
with a magic number, like:
if (starts_with(foo, "bar"))
foo += 3;
This is easy to get wrong, since you have to count the
prefix string yourself, and there's no compiler check if the
string changes. We can use skip_prefix to avoid the magic
numbers here.
Note that some of these conversions could be much shorter.
For example:
if (starts_with(arg, "--foo=")) {
bar = arg + 6;
continue;
}
could become:
if (skip_prefix(arg, "--foo=", &bar))
continue;
However, I have left it as:
if (skip_prefix(arg, "--foo=", &v)) {
bar = v;
continue;
}
to visually match nearby cases which need to actually
process the string. Like:
if (skip_prefix(arg, "--foo=", &v)) {
bar = atoi(v);
continue;
}
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
@ -54,8 +54,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
@@ -54,8 +54,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
/*
* Check remaining flags.
*/
if (starts_with(cmd, "--exec-path")) {
cmd += 11;
if (skip_prefix(cmd, "--exec-path", &cmd)) {
if (*cmd == '=')
git_set_argv_exec_path(cmd + 1);
else {
@ -92,8 +91,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
@@ -92,8 +91,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
*envchanged = 1;
(*argv)++;
(*argc)--;
} else if (starts_with(cmd, "--git-dir=")) {
setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1);
} else if (skip_prefix(cmd, "--git-dir=", &cmd)) {
setenv(GIT_DIR_ENVIRONMENT, cmd, 1);
if (envchanged)
*envchanged = 1;
} else if (!strcmp(cmd, "--namespace")) {
@ -106,8 +105,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
@@ -106,8 +105,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
*envchanged = 1;
(*argv)++;
(*argc)--;
} else if (starts_with(cmd, "--namespace=")) {
setenv(GIT_NAMESPACE_ENVIRONMENT, cmd + 12, 1);
} else if (skip_prefix(cmd, "--namespace=", &cmd)) {
setenv(GIT_NAMESPACE_ENVIRONMENT, cmd, 1);
if (envchanged)
*envchanged = 1;
} else if (!strcmp(cmd, "--work-tree")) {
@ -120,8 +119,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
@@ -120,8 +119,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
*envchanged = 1;
(*argv)++;
(*argc)--;
} else if (starts_with(cmd, "--work-tree=")) {
setenv(GIT_WORK_TREE_ENVIRONMENT, cmd + 12, 1);
} else if (skip_prefix(cmd, "--work-tree=", &cmd)) {
setenv(GIT_WORK_TREE_ENVIRONMENT, cmd, 1);
if (envchanged)
*envchanged = 1;
} else if (!strcmp(cmd, "--bare")) {
@ -578,8 +577,7 @@ int main(int argc, char **av)
@@ -578,8 +577,7 @@ int main(int argc, char **av)
* So we just directly call the builtin handler, and die if