Path: utzoo!utgpu!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!think.com!mintaka!spdcc!dirtydog.ima.isc.com!karl From: karl@ima.isc.com (Karl Heuer) Newsgroups: alt.hackers Subject: Re: Useful string function Message-ID: <1991Jun27.151946.25284@ima.isc.com> Date: 27 Jun 91 15:19:46 GMT References: <27187@ttidca.TTI.COM> <27212@ttidca.TTI.COM> Sender: usenet@ima.isc.com (news) Organization: Interactive Systems, Cambridge, MA 02138-5302 Lines: 38 Approved: karl@ima.isc.com In <27212@ttidca.TTI.COM> hollombe@ttidca.TTI.COM (The Polymath) writes: >In <27187@ttidca.TTI.COM> hollombe@ttidca.TTI.COM (The Polymath) writes: >}... a function that finds a substring within a string and returns its >}starting location or -1 if the substring isn't there. ... > >My thanks to all who wrote to tell me about the strstr(3), or equivalent, >function in their libraries. [We don't have it here.] >To all others in my position I say again, enjoy. To you and all others in your position, I would recommend that you *not* use Strloc(), because it's better to create your own implementation of strstr() that agrees with the ANSI specs. Then when strstr() does become ubiquitous, you won't have future generations scratching their heads wondering why your programs use an incompatible variant. >(And to those who think they can improve on my code -- go for it. I make >no claim perfection). I rather like this implementation, myself; it's small and contains no function calls. Speed improvements are possible, but I wouldn't bother with minor tweaks--if the speed is necessary, just go straight to Boyer-Moore. Karl W. Z. Heuer (karl@ima.isc.com or uunet!ima!karl), The Walking Lint ________ #if defined(NEED_STRSTR) /* deassert if you already have an ANSI library */ /* Public Domain strstr() implementation by Karl Heuer */ #include /* for NULL */ char *strstr(register char const *s, register char const *t) { do { register char const *ss = s; register char const *tt = t; do { if (*tt == '\0') return ((char *)s); } while (*ss++ == *tt++); } while (*s++ != '\0'); return (NULL); } #endif