This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
Re: FW: FW: strcasestr() patch proposal
- From: Eric Blake <ebb9 at byu dot net>
- To: newlib at sources dot redhat dot com
- Date: Wed, 20 Jun 2007 19:28:48 +0000 (UTC)
- Subject: Re: FW: FW: strcasestr() patch proposal
- References: <46697BC0.40006@redhat.com> <44919.6557667621$1181318622@news.gmane.org>
Olivier Langlois <olivier.langlois <at> streamtheworld.com> writes:
> Nothing is missing in the freebsd implementation. In fact, it might even be
> better than glibc version by being simpler. I took the first implementation
> that Google code returned without knowing all implications.
It is certainly simpler. But it is O(n*m), whereas O(n+m) algorithms exist.
We can't use this implementation because it is LGPL, but see gnulib's approach
[1] that uses a hueristic that decides when searching is taking too long (so as
to avoid malloc for trivial cases), at which point it mallocs memory,
lowercases the haystack and needle once, then uses Knuth-Morris-Pratt to find
the needle with a linear traversal (falling back on the quadratic traversal if
the malloc fails). Or, better yet, don't look at the implementation, but use
this description as the guide on how to implement it yourself with the same
properties, so that you aren't tainted by having looked at the LGPL source for
inspiration. Obviously, this becomes a size vs. speed issue, as using gnulib's
approach consumes more code size, but with the payoff of a huge speedup on
large strings.
[1] http://cvs.savannah.gnu.org/viewvc/gnulib/lib/strcasestr.c?
revision=1.9&root=gnulib&view=markup
--
Eric Blake