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
3
alias.c
3
alias.c
@ -5,7 +5,8 @@ static char *alias_val;
|
||||
|
||||
static int alias_lookup_cb(const char *k, const char *v, void *cb)
|
||||
{
|
||||
if (starts_with(k, "alias.") && !strcmp(k + 6, alias_key)) {
|
||||
const char *name;
|
||||
if (skip_prefix(k, "alias.", &name) && !strcmp(name, alias_key)) {
|
||||
if (!v)
|
||||
return config_error_nonbool(k);
|
||||
alias_val = xstrdup(v);
|
||||
|
||||
Reference in New Issue
Block a user