git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Josh Steadmon <steadmon@google.com>
To: "René Scharfe" <l.s.r@web.de>
Cc: git@vger.kernel.org
Subject: Re: [PATCH v2 5/6] parse-options: normalize arg and long_name before comparison
Date: Tue, 12 Mar 2024 14:43:59 -0700	[thread overview]
Message-ID: <ZfDMn-3dKDQwYx_v@google.com> (raw)
In-Reply-To: <20240303121944.20627-6-l.s.r@web.de>

On 2024.03.03 13:19, René Scharfe wrote:
> Strip "no-" from arg and long_name before comparing them.  This way we
> no longer have to repeat the comparison with an offset of 3 for negated
> arguments.
> 
> Note that we must not modify the "flags" value, which tracks whether arg
> is negated, inside the loop.  When registering "--n", "--no" or "--no-"
> as abbreviation for any negative option, we used to OR it with OPT_UNSET
> and end the loop.  We can simply hard-code OPT_UNSET and leave flags
> unchanged instead.
> 
> Signed-off-by: René Scharfe <l.s.r@web.de>
> ---
>  parse-options.c | 44 ++++++++++++++++++++++----------------------
>  1 file changed, 22 insertions(+), 22 deletions(-)
> 
> diff --git a/parse-options.c b/parse-options.c
> index 008c0f32cf..d45efa4e5c 100644
> --- a/parse-options.c
> +++ b/parse-options.c
> @@ -382,28 +382,42 @@ static enum parse_opt_result parse_long_opt(
>  	const struct option *options)
>  {
>  	const char *arg_end = strchrnul(arg, '=');
> +	const char *arg_start = arg;
> +	enum opt_parsed flags = OPT_LONG;
> +	int arg_starts_with_no_no = 0;
>  	struct parsed_option abbrev = { .option = NULL, .flags = OPT_LONG };
>  	struct parsed_option ambiguous = { .option = NULL, .flags = OPT_LONG };
> 
> +	if (skip_prefix(arg_start, "no-", &arg_start)) {
> +		if (skip_prefix(arg_start, "no-", &arg_start))
> +			arg_starts_with_no_no = 1;
> +		else
> +			flags |= OPT_UNSET;
> +	}
> +
>  	for (; options->type != OPTION_END; options++) {
>  		const char *rest, *long_name = options->long_name;
> -		enum opt_parsed flags = OPT_LONG, opt_flags = OPT_LONG;
> +		enum opt_parsed opt_flags = OPT_LONG;
> +		int allow_unset = !(options->flags & PARSE_OPT_NONEG);
> 
>  		if (options->type == OPTION_SUBCOMMAND)
>  			continue;
>  		if (!long_name)
>  			continue;
> 
> -		if (!starts_with(arg, "no-") &&
> -		    !(options->flags & PARSE_OPT_NONEG) &&
> -		    skip_prefix(long_name, "no-", &long_name))
> +		if (skip_prefix(long_name, "no-", &long_name))
>  			opt_flags |= OPT_UNSET;
> +		else if (arg_starts_with_no_no)
> +			continue;

We only allow double negation ("--no-no-foo") if the canonical form
starts with "--no-". Makes sense.


> +		if (((flags ^ opt_flags) & OPT_UNSET) && !allow_unset)
> +			continue;

I don't think the "& OPT_UNSET" is required here, as we never set any
flags other than OPT_UNSET, but I think it's fine to keep it for
clarity.


> -		if (!skip_prefix(arg, long_name, &rest))
> +		if (!skip_prefix(arg_start, long_name, &rest))
>  			rest = NULL;
>  		if (!rest) {
>  			/* abbreviated? */
> -			if (!strncmp(long_name, arg, arg_end - arg)) {
> +			if (!strncmp(long_name, arg_start, arg_end - arg_start)) {
>  				register_abbrev(p, options, flags ^ opt_flags,
>  						&abbrev, &ambiguous);
>  			}
> @@ -412,24 +426,10 @@ static enum parse_opt_result parse_long_opt(
>  				continue;
>  			/* negated and abbreviated very much? */
>  			if (starts_with("no-", arg)) {
> -				flags |= OPT_UNSET;
> -				register_abbrev(p, options, flags ^ opt_flags,
> +				register_abbrev(p, options, OPT_UNSET ^ opt_flags,
>  						&abbrev, &ambiguous);
> -				continue;
> -			}
> -			/* negated? */
> -			if (!starts_with(arg, "no-"))
> -				continue;
> -			flags |= OPT_UNSET;
> -			if (!skip_prefix(arg + 3, long_name, &rest)) {
> -				/* abbreviated and negated? */
> -				if (!strncmp(long_name, arg + 3,
> -					     arg_end - arg - 3))
> -					register_abbrev(p, options,
> -							flags ^ opt_flags,
> -							&abbrev, &ambiguous);
> -				continue;
>  			}
> +			continue;
>  		}
>  		if (*rest) {
>  			if (*rest != '=')
> --
> 2.44.0
> 
> 

  reply	other threads:[~2024-03-12 21:44 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-24 21:29 [PATCH 0/6] parse-options: long option parsing fixes and cleanup René Scharfe
2024-02-24 21:29 ` [PATCH 1/6] parse-options: recognize abbreviated negated option with arg René Scharfe
2024-02-24 21:29 ` [PATCH 2/6] parse-options: set arg of abbreviated option lazily René Scharfe
2024-02-24 21:29 ` [PATCH 3/6] parse-options: factor out register_abbrev() and struct parsed_option René Scharfe
2024-02-24 23:13   ` Junio C Hamano
2024-02-24 21:29 ` [PATCH 4/6] parse-options: detect ambiguous self-negation René Scharfe
2024-02-24 21:29 ` [PATCH 5/6] parse-options: normalize arg and long_name before comparison René Scharfe
2024-02-24 21:29 ` [PATCH 6/6] parse-options: rearrange long_name matching code René Scharfe
2024-02-24 21:42 ` [PATCH 0/6] parse-options: long option parsing fixes and cleanup René Scharfe
2024-03-03 12:19 ` [PATCH v2 " René Scharfe
2024-03-03 12:19   ` [PATCH v2 1/6] parse-options: recognize abbreviated negated option with arg René Scharfe
2024-03-12 21:42     ` Josh Steadmon
2024-03-03 12:19   ` [PATCH v2 2/6] parse-options: set arg of abbreviated option lazily René Scharfe
2024-03-03 12:19   ` [PATCH v2 3/6] parse-options: factor out register_abbrev() and struct parsed_option René Scharfe
2024-03-12 21:43     ` Josh Steadmon
2024-03-13 17:48       ` Junio C Hamano
2024-03-13 18:47       ` René Scharfe
2024-03-03 12:19   ` [PATCH v2 4/6] parse-options: detect ambiguous self-negation René Scharfe
2024-03-03 12:19   ` [PATCH v2 5/6] parse-options: normalize arg and long_name before comparison René Scharfe
2024-03-12 21:43     ` Josh Steadmon [this message]
2024-03-03 12:19   ` [PATCH v2 6/6] parse-options: rearrange long_name matching code René Scharfe
2024-03-12 21:44     ` Josh Steadmon
2024-03-12 21:45   ` [PATCH v2 0/6] parse-options: long option parsing fixes and cleanup Josh Steadmon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZfDMn-3dKDQwYx_v@google.com \
    --to=steadmon@google.com \
    --cc=git@vger.kernel.org \
    --cc=l.s.r@web.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).