Re: [PATCH v2 08/12] skip_prefix(): check const match between in and out params
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Apr 2, 2026, 05:11 UTC
- Message-ID
- <xmqqeckyt08e.fsf@gitster.g>
- In-Reply-To
- <20260402041507.GH3501239@coredump.intra.peff.net>
Jeff King <peff@peff.net> writes:
> +/* > + * Check that an out-parameter that is "at least as const as" a matching > + * in-parameter. For example, skip_prefix() will return "out" that is a subset > + * of "str". So:
Sorry for not mentioning earlier, but I couldn't quite parse the above with "that" immediately after "out-parameter". I am guessing that you wanted to say an equivalent of
Check that an out-parameter "out" is at least as const as a
matching in-parameter "in".Show 27 quoted lines
> + *
> + * const str, const out: ok
> + * non-const str, const out: ok
> + * non-const str, non-const out: ok
> + * const str, non-const out: compile error
> + *
> + * See the skip_prefix macro below for an example of use.
> + */
> +#define CONST_OUTPARAM(in, out) \
> + ((const char **)(0 ? ((*(out) = (in)),(out)) : (out)))
> +
> /*
> * If the string "str" begins with the string found in "prefix", return true.
> * The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in
> @@ -479,8 +494,10 @@ void set_die_is_recursing_routine(int (*routine)(void));
> * [skip prefix if present, otherwise use whole string]
> * skip_prefix(name, "refs/heads/", &name);
> */
> -static inline bool skip_prefix(const char *str, const char *prefix,
> - const char **out)
> +#define skip_prefix(str, prefix, out) \
> + skip_prefix_impl((str), (prefix), CONST_OUTPARAM((str), (out)))
> +static inline bool skip_prefix_impl(const char *str, const char *prefix,
> + const char **out)
> {
> do {
> if (!*prefix) {