From e3f1da982e4f14e7146964cb25a5011a3f41e84a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 4 Oct 2014 20:54:50 +0200 Subject: use skip_prefix() to avoid more magic numbers Continue where ae021d87 (use skip_prefix to avoid magic numbers) left off and use skip_prefix() in more places for determining the lengths of prefix strings to avoid using dependent constants and other indirect methods. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- pretty.c | 56 ++++++++++++++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 30 deletions(-) (limited to 'pretty.c') diff --git a/pretty.c b/pretty.c index 5fd9de6ce..a181ac668 100644 --- a/pretty.c +++ b/pretty.c @@ -73,10 +73,9 @@ static int git_pretty_formats_config(const char *var, const char *value, void *c if (git_config_string(&fmt, var, value)) return -1; - if (starts_with(fmt, "format:") || starts_with(fmt, "tformat:")) { - commit_format->is_tformat = fmt[0] == 't'; - fmt = strchr(fmt, ':') + 1; - } else if (strchr(fmt, '%')) + if (skip_prefix(fmt, "format:", &fmt)) + commit_format->is_tformat = 0; + else if (skip_prefix(fmt, "tformat:", &fmt) || strchr(fmt, '%')) commit_format->is_tformat = 1; else commit_format->is_alias = 1; @@ -157,12 +156,12 @@ void get_commit_format(const char *arg, struct rev_info *rev) rev->commit_format = CMIT_FMT_DEFAULT; return; } - if (starts_with(arg, "format:") || starts_with(arg, "tformat:")) { - save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't'); + if (skip_prefix(arg, "format:", &arg)) { + save_user_format(rev, arg, 0); return; } - if (!*arg || strchr(arg, '%')) { + if (!*arg || skip_prefix(arg, "tformat:", &arg) || strchr(arg, '%')) { save_user_format(rev, arg, 1); return; } @@ -809,18 +808,19 @@ static void parse_commit_header(struct format_commit_context *context) int i; for (i = 0; msg[i]; i++) { + const char *name; int eol; for (eol = i; msg[eol] && msg[eol] != '\n'; eol++) ; /* do nothing */ if (i == eol) { break; - } else if (starts_with(msg + i, "author ")) { - context->author.off = i + 7; - context->author.len = eol - i - 7; - } else if (starts_with(msg + i, "committer ")) { - context->committer.off = i + 10; - context->committer.len = eol - i - 10; + } else if (skip_prefix(msg + i, "author ", &name)) { + context->author.off = name - msg; + context->author.len = msg + eol - name; + } else if (skip_prefix(msg + i, "committer ", &name)) { + context->committer.off = name - msg; + context->committer.len = msg + eol - name; } i = eol; } @@ -951,6 +951,8 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */ const char *placeholder, struct format_commit_context *c) { + const char *rest = placeholder; + if (placeholder[1] == '(') { const char *begin = placeholder + 2; const char *end = strchr(begin, ')'); @@ -958,10 +960,9 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */ if (!end) return 0; - if (starts_with(begin, "auto,")) { + if (skip_prefix(begin, "auto,", &begin)) { if (!want_color(c->pretty_ctx->color)) return end - placeholder + 1; - begin += 5; } color_parse_mem(begin, end - begin, @@ -969,20 +970,15 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */ strbuf_addstr(sb, color); return end - placeholder + 1; } - if (starts_with(placeholder + 1, "red")) { + if (skip_prefix(placeholder + 1, "red", &rest)) strbuf_addstr(sb, GIT_COLOR_RED); - return 4; - } else if (starts_with(placeholder + 1, "green")) { + else if (skip_prefix(placeholder + 1, "green", &rest)) strbuf_addstr(sb, GIT_COLOR_GREEN); - return 6; - } else if (starts_with(placeholder + 1, "blue")) { + else if (skip_prefix(placeholder + 1, "blue", &rest)) strbuf_addstr(sb, GIT_COLOR_BLUE); - return 5; - } else if (starts_with(placeholder + 1, "reset")) { + else if (skip_prefix(placeholder + 1, "reset", &rest)) strbuf_addstr(sb, GIT_COLOR_RESET); - return 6; - } else - return 0; + return rest - placeholder; } static size_t parse_padding_placeholder(struct strbuf *sb, @@ -1522,7 +1518,7 @@ static void pp_header(struct pretty_print_context *pp, int parents_shown = 0; for (;;) { - const char *line = *msg_p; + const char *name, *line = *msg_p; int linelen = get_one_line(*msg_p); if (!linelen) @@ -1557,14 +1553,14 @@ static void pp_header(struct pretty_print_context *pp, * FULL shows both authors but not dates. * FULLER shows both authors and dates. */ - if (starts_with(line, "author ")) { + if (skip_prefix(line, "author ", &name)) { strbuf_grow(sb, linelen + 80); - pp_user_info(pp, "Author", sb, line + 7, encoding); + pp_user_info(pp, "Author", sb, name, encoding); } - if (starts_with(line, "committer ") && + if (skip_prefix(line, "committer ", &name) && (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) { strbuf_grow(sb, linelen + 80); - pp_user_info(pp, "Commit", sb, line + 10, encoding); + pp_user_info(pp, "Commit", sb, name, encoding); } } } -- cgit v1.2.1