[PATCH] Add simplistic reimplementation of strcasestr to compat library

Anthony Towns aj at erisian.com.au
Mon Apr 12 22:59:24 PDT 2010


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
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.

Cheers,
aj

-- 
Anthony Towns <aj at erisian.com.au>


More information about the notmuch mailing list