home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / stringsearch / bmsource / sfC.fwd.inc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-06  |  1.9 KB  |  103 lines  |  [TEXT/MPS ]

  1. /*
  2.     search routine generated by gen.
  3.     skip=sfC, match=fwd (using fwdl), shift=inc
  4. */
  5. /*
  6.  * The authors of this software are Andrew Hume and Daniel Sunday.
  7.  * 
  8.  * Copyright (c) 1991 by AT&T and Daniel Sunday.
  9.  * 
  10.  * Permission to use, copy, modify, and distribute this software for any
  11.  * purpose without fee is hereby granted, provided that this entire notice
  12.  * is included in all copies of any software which is or includes a copy
  13.  * or modification of this software and in all copies of the supporting
  14.  * documentation for such software.
  15.  * 
  16.  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  17.  * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR AT&T MAKE ANY
  18.  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  19.  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  20.  */
  21.  
  22. #ifndef    CHARTYPE
  23. #define    CHARTYPE    unsigned char
  24. #endif
  25. #define    MAXPAT    256
  26.  
  27. #include    "stats.h"
  28.  
  29. #ifndef    TABTYPE
  30. #define    TABTYPE    long
  31. #endif
  32. typedef TABTYPE Tab;
  33.  
  34. static struct
  35. {
  36.     int patlen;
  37.     CHARTYPE pat[MAXPAT];
  38. } pat;
  39.  
  40. prep(base, m)
  41.     CHARTYPE *base;
  42.     register m;
  43. {
  44.     CHARTYPE *skipc;
  45.  
  46.     pat.patlen = m;
  47.     if(m > MAXPAT)
  48.         abort();
  49.     memcpy(pat.pat, base, m);
  50.     skipc = 0;
  51.     stats.len = m;
  52.     skipc = &pat.pat[0];
  53. }
  54.  
  55. exec(base, n)
  56.     CHARTYPE *base;
  57. {
  58.     int nmatch = 0;
  59.     register CHARTYPE *e, *s;
  60.     register CHARTYPE ch;
  61.     register np1;
  62.     extern char *memchr();
  63.     register CHARTYPE *p, *q;
  64.     register CHARTYPE *ep;
  65.  
  66.     s = base;
  67.     e = base + n - pat.patlen;
  68.     np1 = n+1;
  69.     ch = pat.pat[0];
  70.     base[n] = ch;
  71.     ep = pat.pat + pat.patlen;
  72.     while(s < e){
  73. #ifdef    STATS
  74.         while(ch != *s++)
  75.             stats.jump++;
  76.         stats.jump++;
  77.         if(--s > e)
  78.             break;
  79. #else
  80.         s = (CHARTYPE *)memchr(s, ch, np1);    /* must find it */
  81.         if(s > e)
  82.             break;
  83. #endif
  84. #ifdef    STATS
  85.         stats.slow++;
  86. #endif
  87.         for(p = pat.pat+1, q = s+1; p < ep; ){
  88. #ifdef    STATS
  89.             stats.cmp++;
  90. #endif
  91.             if(*q++ != *p++)
  92.                 goto mismatch;
  93.         }
  94.         nmatch++;
  95.     mismatch:
  96.         s++;
  97. #ifdef    STATS
  98.         stats.step[1]++;
  99. #endif
  100.     }
  101.     return(nmatch);
  102. }
  103.