use skip_prefix to avoid magic numbers
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>
This commit is contained in:
committed by
Junio C Hamano
parent
21a2d4ada5
commit
ae021d8791
@ -221,17 +221,19 @@ static void get_idx_file(char *name)
|
||||
|
||||
static int http_config(const char *var, const char *value, void *cb)
|
||||
{
|
||||
const char *p;
|
||||
|
||||
if (!strcmp(var, "http.getanyfile")) {
|
||||
getanyfile = git_config_bool(var, value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (starts_with(var, "http.")) {
|
||||
if (skip_prefix(var, "http.", &p)) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
|
||||
struct rpc_service *svc = &rpc_service[i];
|
||||
if (!strcmp(var + 5, svc->config_name)) {
|
||||
if (!strcmp(p, svc->config_name)) {
|
||||
svc->enabled = git_config_bool(var, value);
|
||||
return 0;
|
||||
}
|
||||
@ -244,15 +246,16 @@ static int http_config(const char *var, const char *value, void *cb)
|
||||
|
||||
static struct rpc_service *select_service(const char *name)
|
||||
{
|
||||
const char *svc_name;
|
||||
struct rpc_service *svc = NULL;
|
||||
int i;
|
||||
|
||||
if (!starts_with(name, "git-"))
|
||||
if (!skip_prefix(name, "git-", &svc_name))
|
||||
forbidden("Unsupported service: '%s'", name);
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
|
||||
struct rpc_service *s = &rpc_service[i];
|
||||
if (!strcmp(s->name, name + 4)) {
|
||||
if (!strcmp(s->name, svc_name)) {
|
||||
svc = s;
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user