1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
/* 
 * NCHAR = Needle character 
 * HCHAR = Haystack character 
 */ 
 
static INLINE HCHAR *NameNH(MEMCHR)(HCHAR *p, NCHAR c, ptrdiff_t e) 
{ 
#if NSHIFT > HSHIFT 
  if(c > (1<<(8*HSHIFT))) return 0; 
#endif 
  return NameH(MEMCHR)(p,c,e); 
} 
 
 
static INLINE int NameNH(MEMCMP)(NCHAR *a, HCHAR *b, ptrdiff_t e) 
{ 
#if NSHIFT == HSHIFT 
  return MEMCMP(a, b, sizeof(HCHAR)*e); 
#else 
  for(;e;e--,b++,a++) 
  { 
    if(*b!=*a) 
    { 
      if(*b<*a) return -1; 
      if(*b>*a) return 1; 
    } 
  } 
  return 0; 
   
#endif 
} 
 
INLINE HCHAR *NameNH(memchr_search)(void *data, 
                                    HCHAR *haystack, 
                                    ptrdiff_t haystacklen) 
{ 
  return NameNH(MEMCHR)(haystack, DO_NOT_WARN((NCHAR)(ptrdiff_t)data), haystacklen); 
} 
 
 
INLINE HCHAR *NameNH(memchr_memcmp)(NCHAR *needle, 
                                    ptrdiff_t needlelen, 
                                    HCHAR *haystack, 
                                    ptrdiff_t haystacklen) 
{ 
  NCHAR c; 
  HCHAR *end; 
 
  if(needlelen > haystacklen) return 0; 
 
  end=haystack + haystacklen - needlelen+1; 
  c=needle[0]; 
  needle++; 
  needlelen--; 
  while((haystack=NameNH(MEMCHR)(haystack,c,end-haystack))) 
    if(!NameNH(MEMCMP)(needle,++haystack,needlelen)) 
      return haystack-1; 
 
  return 0; 
} 
 
 
#define make_memchr_memcmpX(X)                                        \ 
HCHAR *PxC4(memchr_memcmp,X,NSHIFT,HSHIFT)(void *n,            \ 
                                           HCHAR *haystack,     \ 
                                           ptrdiff_t haystacklen)       \ 
{                                                               \ 
  return NameNH(memchr_memcmp)((NCHAR *)n,                      \ 
                               X,                               \ 
                               haystack,                        \ 
                               haystacklen);                    \ 
} 
 
make_memchr_memcmpX(2) 
make_memchr_memcmpX(3) 
make_memchr_memcmpX(4) 
make_memchr_memcmpX(5) 
make_memchr_memcmpX(6) 
 
 
HCHAR *NameNH(boyer_moore_hubbe)(struct boyer_moore_hubbe_searcher *s, 
                                 HCHAR *haystack, 
                                 ptrdiff_t haystacklen) 
{ 
  ptrdiff_t i=s->plen-1; 
  ptrdiff_t hlen=haystacklen; 
  if(NEEDLELEN > s->plen) 
    hlen -= NEEDLELEN-s->plen; 
   
 restart: 
  while(i<hlen) 
  { 
    ptrdiff_t k,j; 
     
    if((k=s->d1[ NameNH(BMHASH)( haystack[i] ) ])) 
      i+=k; 
    else 
    { 
#if NSHIFT == 0 
      j=s->plen-1; 
       
#ifdef PIKE_DEBUG 
      if(NEEDLE[j] != haystack[i]) 
        fatal("T2BM failed!\n"); 
#endif 
 
#else 
      i++; 
      j=s->plen; 
#endif 
       
      while(NEEDLE[--j] == haystack[--i]) 
      { 
        if(!j) 
        { 
          if(NEEDLELEN > s->plen) 
          { 
            if(!NameNH(MEMCMP)(NEEDLE+s->plen, 
                               haystack+i+s->plen, NEEDLELEN-s->plen)) 
            { 
              return haystack+i; 
            }else{ 
              /* this can be optimized... */ 
              i+=s->plen; 
              goto restart; 
            } 
          }else{ 
            return haystack+i; 
          } 
        } 
      } 
       
      i+= 
        (s->d1[ NameNH(BMHASH)(haystack[i]) ] >= s->d2[j]) ? 
        (s->d1[ NameNH(BMHASH)(haystack[i]) ]): 
        (s->d2[j]); 
    } 
  } 
  return 0; 
} 
 
 
HCHAR *NameNH(hubbe_search)(struct hubbe_searcher *s, 
                            HCHAR *haystack, 
                            ptrdiff_t haystacklen) 
{ 
  INT32 tmp, h; 
  HCHAR *q, *end; 
  register struct hubbe_search_link *ptr; 
   
  end=haystack+haystacklen; 
  q=haystack + s->max - 4; 
 
  NameH(HUBBE_ALIGN)(q); 
 
  for(;q<=end-sizeof(INT32);q+=s->max) 
  { 
    h=tmp=NameH(GET_4_ALIGNED_CHARS)(q); 
     
    h+=h>>7; 
    h+=h>>17; 
    h&=s->hsize; 
     
    for(ptr=s->set[h];ptr;ptr=ptr->next) 
    { 
      HCHAR *where; 
       
      if(ptr->key != tmp) continue; 
       
      where=q-ptr->offset; 
 
      /* FIXME: This if statement is not required 
       * HUBBE_ALIGN is a noop 
       */ 
      if(where<haystack) continue; 
 
      if(where+NEEDLELEN>end) return 0; 
       
      if(!NameNH(MEMCMP)(NEEDLE,where,NEEDLELEN)) 
        return where; 
    } 
  } 
  return 0; 
}