home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / kpathsea / strstr.c < prev    next >
C/C++ Source or Header  |  2000-01-15  |  3KB  |  124 lines

  1. /* Copyright (C) 1994, 95 Free Software Foundation, Inc.
  2. This file was part of the GNU C Library. Modified by kb@mail.tug.org.
  3.  
  4. This file 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. This file 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 this file; see the file COPYING.LIB.  If not, write
  16. to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. Boston, MA 02111-1307, USA.  */
  18.  
  19.  
  20. /*
  21.  * My personal strstr() implementation that beats most other algorithms.
  22.  * Until someone tells me otherwise, I assume that this is the
  23.  * fastest implementation of strstr() in C.
  24.  * I deliberately chose not to comment it.  You should have at least
  25.  * as much fun trying to understand it, as I had to write it :-).
  26.  *
  27.  * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de    */
  28.  
  29. #if !defined (__STDC__) || !__STDC__
  30. /* This is a separate conditional since some stdc systems
  31.    reject `defined (const)'.  */
  32. #ifndef const
  33. #define const
  34. #endif
  35. #endif
  36.  
  37. typedef unsigned chartype;
  38.  
  39. char *
  40. strstr (phaystack, pneedle)
  41.      const char *phaystack;
  42.      const char *pneedle;
  43. {
  44.   register const unsigned char *haystack, *needle;
  45.   register chartype b, c;
  46.  
  47.   haystack = (const unsigned char *) phaystack;
  48.   needle = (const unsigned char *) pneedle;
  49.  
  50.   b = *needle;
  51.   if (b != '\0')
  52.     {
  53.       haystack--;                /* possible ANSI violation */
  54.       do
  55.     {
  56.       c = *++haystack;
  57.       if (c == '\0')
  58.         goto ret0;
  59.     }
  60.       while (c != b);
  61.  
  62.       c = *++needle;
  63.       if (c == '\0')
  64.     goto foundneedle;
  65.       ++needle;
  66.       goto jin;
  67.  
  68.       for (;;)
  69.         { 
  70.           register chartype a;
  71.       register const unsigned char *rhaystack, *rneedle;
  72.  
  73.       do
  74.         {
  75.           a = *++haystack;
  76.           if (a == '\0')
  77.         goto ret0;
  78.           if (a == b)
  79.         break;
  80.           a = *++haystack;
  81.           if (a == '\0')
  82.         goto ret0;
  83. shloop:        }
  84.           while (a != b);
  85.  
  86. jin:      a = *++haystack;
  87.       if (a == '\0')
  88.         goto ret0;
  89.  
  90.       if (a != c)
  91.         goto shloop;
  92.  
  93.       rhaystack = haystack-- + 1;
  94.       rneedle = needle;
  95.       a = *rneedle;
  96.  
  97.       if (*rhaystack == a)
  98.         do
  99.           {
  100.         if (a == '\0')
  101.           goto foundneedle;
  102.         ++rhaystack;
  103.         a = *++needle;
  104.         if (*rhaystack != a)
  105.           break;
  106.         if (a == '\0')
  107.           goto foundneedle;
  108.         ++rhaystack;
  109.         a = *++needle;
  110.           }
  111.         while (*rhaystack == a);
  112.  
  113.       needle = rneedle;           /* took the register-poor aproach */
  114.  
  115.       if (a == '\0')
  116.         break;
  117.         }
  118.     }
  119. foundneedle:
  120.   return (char*) haystack;
  121. ret0:
  122.   return 0;
  123. }
  124.