public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Richard Earnshaw <Richard.Earnshaw@foss.arm.com>
To: James <tirtajames45@gmail.com>, newlib@sourceware.org
Cc: "vinschen@redhat.com" <vinschen@redhat.com>
Subject: Re: [PATCH] strchr, strchrnul: implement strchr() as strchrnul()
Date: Tue, 5 Dec 2023 16:39:47 +0000	[thread overview]
Message-ID: <6172a3a7-724d-42d8-b464-cabd3deacf8a@foss.arm.com> (raw)
In-Reply-To: <CANDqPp1imFkduSUJsCc8oS2UXK83jbsqNRYEdDHce5i6U24hbg@mail.gmail.com>



On 05/12/2023 15:52, James wrote:
> Currently, strchrnul() is implemented as strchr() and falls back to a 
> strlen() to find the null-terminator if the character is not 
> found, scanning the string twice. However, strchr() is going to scan the 
> whole string anyway and discard the pointer to the null-terminator if 
> the character is not found, returning NULL.
> 
> Instead, we can just implement strchr() with discarding strchrnul()'s 
> pointer to null-terminator returning NULL and by that avoid calling 
> strlen() in strchrnul() if a character is not found.
> 
> I made a typo in the strchr(), it should be:
>    return s1 && *s1 ? (char *) s1 : NULL;
> which should be:
>    return *s1 ? (char *) s1 : NULL;
> since strchrnul will never return NULL.
> 
> We can avoid the strchrnul() function call in strchr() by implementing 
> it in a separate header like str-two-way.h and including them in both 
> files. The same could be done with the strlen() part in strchr() since 
> the implementations look to be the same.

strchrnul is not standard, it's a GNU extension.  So technically, it 
should be possible to redefine strchrnul to something else and strchr 
should still work as expected.

The other issue is that many ports implement strchr in assembly for 
performance and rely on strchrnul calling that to avoid having too many 
functions implemented in assembly.

So I'm not convinced this is a good idea.

R.

      reply	other threads:[~2023-12-05 16:39 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-05 14:58 James Tirta Halim
2023-12-05 15:23 ` Corinna Vinschen
2023-12-05 15:52   ` James
2023-12-05 16:39     ` Richard Earnshaw [this message]

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=6172a3a7-724d-42d8-b464-cabd3deacf8a@foss.arm.com \
    --to=richard.earnshaw@foss.arm.com \
    --cc=newlib@sourceware.org \
    --cc=tirtajames45@gmail.com \
    --cc=vinschen@redhat.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 read-only IMAP folder(s) and NNTP newsgroup(s).