home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / string / strstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-12  |  2.5 KB  |  101 lines

  1. /* Copyright (C) 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. /*
  20.  * My personal strstr() implementation that beats most other algorithms.
  21.  * Until someone tells me otherwise, I assume that this is the
  22.  * fastest implementation of strstr() in C.
  23.  * I deliberately chose not to comment it.  You should have at least
  24.  * as much fun trying to understand it, as I had to write it :-).
  25.  *
  26.  * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de    */
  27.  
  28. #include <ansidecl.h>
  29. #include <string.h>
  30. #include <sys/types.h>
  31.  
  32. typedef unsigned chartype;
  33.  
  34. char *
  35. DEFUN(strstr, (phaystack, pneedle),
  36.     const char *phaystack AND const char *pneedle)
  37. {
  38.   register const unchar *haystack, *needle;
  39.   register chartype b, c;
  40.  
  41.   haystack = (const unchar *)phaystack;
  42.  
  43.   if ((b= *(needle=(const unchar*)pneedle)))
  44.     {
  45.       haystack--;                /* possible ANSI violation */
  46.       do
  47.     if (!(c= *++haystack))
  48.       goto ret0;
  49.       while (c!=b);
  50.  
  51.       if (!(c= *++needle))
  52.     goto foundneedle;
  53.       ++needle;
  54.       goto jin;
  55.  
  56.       for (;;)
  57.         { 
  58.           register chartype a;
  59.       register const unchar *rhaystack, *rneedle;
  60.  
  61.       do
  62.         {
  63.           if (!(a= *++haystack))
  64.         goto ret0;
  65.           if (a==b)
  66.         break;
  67.           if (!(a= *++haystack))
  68.         goto ret0;
  69. shloop:     }
  70.           while (a!=b);
  71.  
  72. jin:      if (!(a= *++haystack))
  73.         goto ret0;
  74.  
  75.       if (a!=c)
  76.         goto shloop;
  77.  
  78.       if (*(rhaystack=haystack--+1) == (a= *(rneedle=needle)))
  79.         do
  80.           {
  81.         if (!a)
  82.           goto foundneedle;
  83.         if (*++rhaystack!=(a= *++needle))
  84.           break;
  85.         if (!a)
  86.           goto foundneedle;
  87.           }
  88.         while (*++rhaystack == (a= *++needle));
  89.  
  90.       needle=rneedle;           /* took the register-poor aproach */
  91.  
  92.       if (!a)
  93.         break;
  94.         }
  95.     }
  96. foundneedle:
  97.   return (char*)haystack;
  98. ret0:
  99.   return 0;
  100. }
  101.