[PATCH] Add strchrnul

Corinna Vinschen vinschen@redhat.com
Tue Mar 29 21:52:00 GMT 2011

On Mar 28 05:12, Yaakov (Cygwin/X) wrote:
> On Mon, 2011-03-28 at 10:10 +0200, Corinna Vinschen wrote:
> > Isn't this implementation a bit simple?  It's as fast as the underlying
> > strchr function in case of a hit, but it suffers quite a performance hit
> > in case of a miss, because the string has to be scanned twice.
> > 
> > So I'm wondering, shouldn't we better adapt the strchr implementation
> > and just return a pointer to s where strchr returns NULL?
> There's always a tradeoff, simplicity vs. code duplication.  I presume
> you mean something like the attached.  I'm just not sure about the !
> PREFER_SIZE_OVER_SPEED block of the code.

It looks ok to me.  On second thought, I'm opting for your original
solution for two reaason.

- We have explicit target-specific strchr and strlen implementations
  in newlib.

- We have implicit target-specific strchr and strlen implementations
  in the form of GCC builtin functions.

Both types of target-specific optimized code would never be used if we
implement strchrnul always as a generically optimized C function.

So I applied your first implementation.  Maybe, in the long run, we
should provide target-specific optimized strchnul implementations and
implement strchr generically like this?

  strchr(s, c)
    char *se = strchrnul();
    return *se || !c ? se : NULL;


Corinna Vinschen
Cygwin Project Co-Leader
Red Hat

More information about the Newlib mailing list