home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!paladin.american.edu!darwin.sura.net!mips!swrinde!cs.utexas.edu!torn!utzoo!telly!druid!darcy
- From: darcy@druid.uucp (D'Arcy J.M. Cain)
- Newsgroups: comp.lang.c
- Subject: Re: HIgher problems at hand. HELP!
- Message-ID: <1992Jul28.180817.9388@druid.uucp>
- Date: 28 Jul 92 18:08:17 GMT
- References: <Brv6oD.Dq5@usenet.ucs.indiana.edu> <1992Jul25.142216.5636@druid.uucp> <24902@dog.ee.lbl.gov>
- Organization: D'Arcy Cain Consulting
- Lines: 55
-
- torek@horse.ee.lbl.gov (Chris Torek) writes:
- >In article <1992Jul25.142216.5636@druid.uucp> darcy@druid.uucp
- >(D'Arcy J.M. Cain) writes:
- >> * strstr - find first occurrence of wanted in s
- >> *
- >> * based on code by Henry Spencer
- >> * modified for ANSI by D'Arcy J.M. Cain
- >> */
- >The given strstr() fails to meet the second sentence under `Returns'
- >in the ANSI C standard description of strstr() (line 25, p. 168):
- >
- > If |s2| points to a string with zero length, the function
- > returns |s1|.
-
- Oops. That's right. The culprit is the optimization attempt in the
- while statement:
-
- while (*s != firstc || strncmp(s, wanted, len) != 0)
-
- Should be:
-
- while (strncmp(s, wanted, len) != 0)
-
- Or the simpler:
-
- while (strncmp(s, wanted, len))
-
- This depends on strncmp returning equal on zero length strings. I am
- pretty sure that the standard requires this. It certainly strongly
- implies it. Perhaps this needs a clarification.
-
- So the new version, without the "peculiar" define, is:
-
- -------------------------------------------------------
- #include <string.h>
-
- char *strstr(const char *s, const char *wanted)
- {
- size_t len = strlen(wanted);
-
- while (strncmp(s, wanted, len))
- if (*s++ == '\0')
- return(NULL);
-
- return(s);
- }
- --------------------------------------------------------
-
- Kluge to taste for warning suppression.
-
- --
- D'Arcy J.M. Cain (darcy@druid.com) |
- D'Arcy Cain Consulting | There's no government
- Toronto, Ontario, Canada | like no government!
- +1 416 424 2871 DoD#0082 |
-