Re: [PATCH] Add simplistic reimplementation of strcasestr to compat library

Subject: Re: [PATCH] Add simplistic reimplementation of strcasestr to compat library

Date: Tue, 13 Apr 2010 15:59:24 +1000

To: Dirk Hohndel

Cc: notmuch@notmuchmail.org

From: Anthony Towns


On Tue, Apr 13, 2010 at 14:10, Dirk Hohndel <hohndel@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@erisian.com.au>

Thread: