C: use skip_prefix() to avoid hardcoded string length

We often skip an optional prefix in a string with a hardcoded
constant, e.g.

	if (starts_with(string, "prefix"))
		string += 6;

which is less error prone when written

	skip_prefix(string, "prefix", &string);

Note that this changes a few error messages from "git reflog expire
--expire=nonsense.timestamp", which used to complain by saying

    '--expire=nonsense.timestamp' is not a valid timestamp

but with this change, we say

    'nonsense.timestamp' is not a valid timestamp

which is more technically correct (the string with --expire= as
a prefix obviously cannot be a valid timestamp, but the error is
about the part of the input without that prefix).

Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Junio C Hamano
2020-01-30 11:35:46 -08:00
parent d0654dc308
commit 145136a95a
8 changed files with 18 additions and 25 deletions

View File

@ -357,8 +357,7 @@ is_abbreviated:
}
/* negated? */
if (!starts_with(arg, "no-")) {
if (starts_with(long_name, "no-")) {
long_name += 3;
if (skip_prefix(long_name, "no-", &long_name)) {
opt_flags |= OPT_UNSET;
goto again;
}