[PATCH] Add simplistic reimplementation of strcasestr to compat library

Dirk Hohndel hohndel at infradead.org
Tue Apr 13 05:42:39 PDT 2010


On Tue, 13 Apr 2010 15:59:24 +1000, Anthony Towns <aj at erisian.com.au> wrote:
> On Tue, Apr 13, 2010 at 14:10, Dirk Hohndel <hohndel at infradead.org> wrote:
> > +/* the semantic here actually puzzles me:
> > +   how can haystack be const char * - yet the return value is char *
> > +   after all, it points to a sub-string of haystack... */
> 
> Dunno if this is a question from the original source, but the answer

No, that was me being puzzled :-)

> if anyone's interested is probably because C doesn't have templates --
> you'd ideally like to have it treated as:
> 
>     char *strcasestr(char *haystack, const char *needle);
> 
> for when you're doing a search and replace on the needle (say), and:
> 
>     const char *strcasestr(const char *haystack, const char *needle);
> 
> for when you're doing a search for the needle in something you can't
> modify. But C isn't clever enough to let you say that with just one
> function (and no fancy #defines), so you have to drop some of the
> typechecking with the (char*) cast on the return value if you want to
> handle both use cases, without the compiler complaining about
> const->non-const conversions in otherwise correct code in one case or
> the other.

That makes sense. Thanks

/D

-- 
Dirk Hohndel
Intel Open Source Technology Center


More information about the notmuch mailing list