git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] use strpbrk(3) to search for characters from a given set

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 24, 2020, 17:10 UTC
Message-ID
<xmqqy2srrjx1.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<4140dade-d999-a74a-1f8e-06eedb84ed20@web.de>
René Scharfe <l.s.r@web.de> writes:
Show 23 quoted lines
> We can check if certain characters are present in a string by calling
> strchr(3) on each of them, or we can pass them all to a single
> strpbrk(3) call.  The latter is shorter, less repetitive and slightly
> more efficient, so let's do that instead.
>
> Signed-off-by: René Scharfe <l.s.r@web.de>
> ---
>  builtin/show-branch.c              | 2 +-
>  compat/mingw.c                     | 2 +-
>  mailinfo.c                         | 3 +--
>  t/helper/test-windows-named-pipe.c | 2 +-
>  4 files changed, 4 insertions(+), 5 deletions(-)
>
> diff --git a/builtin/show-branch.c b/builtin/show-branch.c
> index 35d7f51c23..8c90cbb18f 100644
> --- a/builtin/show-branch.c
> +++ b/builtin/show-branch.c
> @@ -536,7 +536,7 @@ static void append_one_rev(const char *av)
>  		append_ref(av, &revkey, 0);
>  		return;
>  	}
> -	if (strchr(av, '*') || strchr(av, '?') || strchr(av, '[')) {
> +	if (strpbrk(av, "*?[")) {
The changes in the patch obviously look all correct.

I wonder how we can exploit Coccinelle to do this kind of transformations, though. Would it be possible to say

 * if we see "strchr(S, C1) || strchr(S, C2)", transform it to
   "strpbrk(S, concat(stringify(C1),stringify(C2)))"; and
 * if we see "strpbrk(S, N) || strchr(S, C)", transform it to
   "strpbrk(S, concat(N, stringify(C))";

and let the tool apply these two rules repeatedly, to catch the pattern to find any number of needle character in the same haystack?

Previous: Jeff KingNext: René Scharfe
Message 4 of 6 in “use strpbrk(3) to search for characters from a given set”
  1. use strpbrk(3) to search for characters from a given setRené Scharfe, Feb 22, 2020
  2. Martin ÅgrenFeb 23, 2020
  3. Jeff KingFeb 24, 2020
  4. Junio C HamanoFeb 24, 2020
  5. René ScharfeFeb 24, 2020
  6. Jeff KingFeb 24, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.