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 p...@peff.net
---
alias.c| 3 ++-
connect.c | 11 +
convert.c | 4 ++--
daemon.c | 73 ++
diff.c | 65 ++-
fast-import.c | 69 +-
fetch-pack.c | 9
git.c | 18 +++
help.c | 6 +++--
http-backend.c | 11 +
http-push.c| 11 +
11 files changed, 149 insertions(+), 131 deletions(-)
diff --git a/alias.c b/alias.c
index 5efc3d6..758c867 100644
--- a/alias.c
+++ b/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);
diff --git a/connect.c b/connect.c
index 94a6650..37ff018 100644
--- a/connect.c
+++ b/connect.c
@@ -129,6 +129,7 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t
src_len,
char *name;
int len, name_len;
char *buffer = packet_buffer;
+ const char *arg;
len = packet_read(in, src_buf, src_len,
packet_buffer, sizeof(packet_buffer),
@@ -140,12 +141,12 @@ struct ref **get_remote_heads(int in, char *src_buf,
size_t src_len,
if (!len)
break;
- if (len 4 starts_with(buffer, ERR ))
- die(remote error: %s, buffer + 4);
+ if (len 4 skip_prefix(buffer, ERR , arg))
+ die(remote error: %s, arg);
- if (len == 48 starts_with(buffer, shallow )) {
- if (get_sha1_hex(buffer + 8, old_sha1))
- die(protocol error: expected shallow sha-1,
got '%s', buffer + 8);
+ if (len == 48 skip_prefix(buffer, shallow , arg)) {
+ if (get_sha1_hex(arg, old_sha1))
+ die(protocol error: expected shallow sha-1,
got '%s', arg);
if (!shallow_points)
die(repository on the other end cannot be
shallow);
sha1_array_append(shallow_points, old_sha1);
diff --git a/convert.c b/convert.c
index ab80b72..cb5fbb4 100644
--- a/convert.c
+++ b/convert.c
@@ -1121,9 +1121,9 @@ static int is_foreign_ident(const char *str)
{
int i;
- if (!starts_with(str, $Id: ))
+ if (!skip_prefix(str, $Id: , str))
return 0;
- for (i = 5; str[i]; i++) {
+ for (i = 0; str[i]; i++) {
if (isspace(str[i]) str[i+1] != '$')
return 1;
}
diff --git a/daemon.c b/daemon.c
index 18818c3..6d25828 100644
--- a/daemon.c
+++ b/daemon.c
@@ -235,8 +235,10 @@ static int service_enabled;
static int git_daemon_config(const char *var, const char *value, void *cb)
{
- if (starts_with(var, daemon.)
- !strcmp(var + 7, service_looking_at-config_name)) {
+ const char *service;
+
+ if (skip_prefix(var, daemon., service)
+ !strcmp(service, service_looking_at-config_name)) {
service_enabled = git_config_bool(var, value);
return 0;
}
@@ -1133,16 +1135,17 @@ int main(int argc, char **argv)
for (i = 1; i argc; i++) {
char *arg = argv[i];
+ const char *v;
- if (starts_with(arg, --listen=)) {
- string_list_append(listen_addr, xstrdup_tolower(arg +
9));
+ if (skip_prefix(arg, --listen=, v)) {
+ string_list_append(listen_addr, xstrdup_tolower(v));
continue;
}
- if (starts_with(arg, --port=)) {
+ if (skip_prefix(arg, --port=, v)) {