git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "René Scharfe" <l.s.r@web.de>
To: Junio C Hamano <gitster@pobox.com>
Cc: Git Mailing List <git@vger.kernel.org>,
	Markus Elfring <Markus.Elfring@web.de>
Subject: Re: [PATCH] use strpbrk(3) to search for characters from a given set
Date: Mon, 24 Feb 2020 20:01:36 +0100	[thread overview]
Message-ID: <21e6eaac-318b-3cb3-bb63-4f65abfae670@web.de> (raw)
In-Reply-To: <xmqqy2srrjx1.fsf@gitster-ct.c.googlers.com>

Am 24.02.20 um 18:10 schrieb Junio C Hamano:
> René Scharfe <l.s.r@web.de> writes:
>
>> 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?

That would be nice.  I briefly considered it, but I only can think of a
silly way to convert char literals to strings (by using one rule for
each possible character value), I don't know how to concatenate strings
in Coccinelle (simply putting them next to each other as in C doesn't
seem to work), and I don't know how to apply a rule recursively to allow
transforming an arbitrarily long chain of strchr() calls. :-/

René

  reply	other threads:[~2020-02-24 19:02 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-22 18:51 [PATCH] use strpbrk(3) to search for characters from a given set René Scharfe
2020-02-23  9:01 ` Martin Ågren
2020-02-24  6:38 ` Jeff King
2020-02-24 17:10 ` Junio C Hamano
2020-02-24 19:01   ` René Scharfe [this message]
2020-02-24 19:10     ` Jeff King

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=21e6eaac-318b-3cb3-bb63-4f65abfae670@web.de \
    --to=l.s.r@web.de \
    --cc=Markus.Elfring@web.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /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).