public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: James <tirtajames45@gmail.com>
To: newlib@sourceware.org, James Tirta Halim <tirtajames45@gmail.com>
Cc: "vinschen@redhat.com" <vinschen@redhat.com>
Subject: Re: [PATCH] strchr, strchrnul: implement strchr() as strchrnul()
Date: Tue, 5 Dec 2023 22:52:41 +0700	[thread overview]
Message-ID: <CANDqPp1imFkduSUJsCc8oS2UXK83jbsqNRYEdDHce5i6U24hbg@mail.gmail.com> (raw)
In-Reply-To: <ZW9AZIEEMWoGQTqA@calimero.vinschen.de>

[-- Attachment #1: Type: text/plain, Size: 943 bytes --]

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.

  reply	other threads:[~2023-12-05 15:56 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 [this message]
2023-12-05 16:39     ` Richard Earnshaw

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=CANDqPp1imFkduSUJsCc8oS2UXK83jbsqNRYEdDHce5i6U24hbg@mail.gmail.com \
    --to=tirtajames45@gmail.com \
    --cc=newlib@sourceware.org \
    --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).