Re: str_str() for those who need it

From: Akuma/Chris Baggett/DOOMer (doomer@BAYOU.COM)
Date: 01/20/98


I finally got around to trying your version of the str_str()
and I must say it worked fine, but it wasn't case insensitive :-P

I fixed it by changing one line down there.
the   'if (*h == *n)'  should be   'if (LOWER(*h) == LOWER(*n))'

A also changed the while { } loop to a for loop, but that depends on
taste.  either way, it works fine.  Of course, I could also
change it around to use Integers instead of char *'s
but again, that's a matter of taste.

Thanks d.

Code On
Akuma the Raging Coder

At 06:14 PM 1/15/98 -0500, you wrote:
>/*
> * I hacked the following, it may contain missing sanity
> * checking since I wrote it in vi instead of emacs
> */
>char *str_str (const char *haystack, const char *needle)
>{
>   register const char *h, *n;
>   const char *hm;
>
>   if (!haystack || !needle)
>      return NULL;
>
>   n = needle;
>   for (h = haystack; *h; h++) {
// Like so
       if (LOWER(*h) == LOWER(*n)) {
// >      if (*h == *n) {
>         hm = h;                /* set mark start of substring search */
>
>         while (*h && *n) {
>            if (LOWER(*h) != LOWER(*n)) /* substring does not match */
>               break;
>            h++; n++;
>         }
>         if (*n) {              /* needle not terminated? start over */
>            n = needle;         /* reset needle */
>            h = hm;             /* reset haystack */
>         } else                 /* needle found, return start of substring */
>            return (char *) hm;
>      }
>   }
>   return NULL;                 /* nothing found */
>}
>
>int main ()
>{
>   char *p;
>
>   p = str_str ("hello world", "world");
>   printf ("%s\n", p ? p : "NULL");
>
>   p = str_str ("ab", "a");
>   printf ("%s\n", p ? p : "NULL");
>
>   p = str_str ("hello", "ab");
>   printf ("%s\n", p ? p : "NULL");
>
>   p = str_str ("ab", "world");
>   printf ("%s\n", p ? p : "NULL");
>
>   p = str_str ("word world world-wonder", "world-wonder");
>   printf ("%s\n", p ? p : "NULL");
>
>   return 1;
>}
>

  +------------------------------------------------------------+
  | "The poets talk about love, but what I talk about is DOOM, |
  |      because in the end, DOOM is all that counts." -       |
  |   Alex Machine/George Stark/Stephen King, The Dark Half    |
  |        "Nothing is IMPOSSIBLE, Just IMPROBABLE"            |
  |   "Easier Said Than Done, But Better Done Than Said..."    |
  +------------------------------------------------------------+


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST