blob: 21cf3a2ab7e52eb8b5c8a5a061a7a72b6edc87c0 [file] [log] [blame]
Thomas Wouters477c8d52006-05-27 19:21:47 +00001/* stringlib: fastsearch implementation */
2
3#ifndef STRINGLIB_FASTSEARCH_H
4#define STRINGLIB_FASTSEARCH_H
5
6/* fast search/count implementation, based on a mix between boyer-
7 moore and horspool, with a few more bells and whistles on the top.
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +00008 for some more background, see: http://effbot.org/zone/stringlib.htm */
Thomas Wouters477c8d52006-05-27 19:21:47 +00009
10/* note: fastsearch may access s[n], which isn't a problem when using
11 Python's ordinary string types, but may cause problems if you're
12 using this code in other contexts. also, the count mode returns -1
13 if there cannot possible be a match in the target string, and 0 if
14 it has actually checked for matches, but didn't find any. callers
15 beware! */
16
17#define FAST_COUNT 0
18#define FAST_SEARCH 1
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +000019#define FAST_RSEARCH 2
Thomas Wouters477c8d52006-05-27 19:21:47 +000020
Antoine Pitrouf2c54842010-01-13 08:07:53 +000021#define BLOOM_ADD(mask, ch) ((mask |= (1 << ((ch) & (LONG_BIT - 1)))))
22#define BLOOM(mask, ch) ((mask & (1 << ((ch) & (LONG_BIT - 1)))))
23
Thomas Wouters477c8d52006-05-27 19:21:47 +000024Py_LOCAL_INLINE(Py_ssize_t)
25fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n,
26 const STRINGLIB_CHAR* p, Py_ssize_t m,
Antoine Pitrouf2c54842010-01-13 08:07:53 +000027 Py_ssize_t maxcount, int mode)
Thomas Wouters477c8d52006-05-27 19:21:47 +000028{
29 long mask;
30 Py_ssize_t skip, count = 0;
31 Py_ssize_t i, j, mlast, w;
32
33 w = n - m;
34
Antoine Pitrouf2c54842010-01-13 08:07:53 +000035 if (w < 0 || (mode == FAST_COUNT && maxcount == 0))
Thomas Wouters477c8d52006-05-27 19:21:47 +000036 return -1;
37
38 /* look for special cases */
39 if (m <= 1) {
40 if (m <= 0)
41 return -1;
42 /* use special case for 1-character strings */
43 if (mode == FAST_COUNT) {
44 for (i = 0; i < n; i++)
Antoine Pitrouf2c54842010-01-13 08:07:53 +000045 if (s[i] == p[0]) {
Thomas Wouters477c8d52006-05-27 19:21:47 +000046 count++;
Antoine Pitrouf2c54842010-01-13 08:07:53 +000047 if (count == maxcount)
48 return maxcount;
49 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000050 return count;
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +000051 } else if (mode == FAST_SEARCH) {
Thomas Wouters477c8d52006-05-27 19:21:47 +000052 for (i = 0; i < n; i++)
53 if (s[i] == p[0])
54 return i;
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +000055 } else { /* FAST_RSEARCH */
56 for (i = n - 1; i > -1; i--)
57 if (s[i] == p[0])
58 return i;
Thomas Wouters477c8d52006-05-27 19:21:47 +000059 }
60 return -1;
61 }
62
63 mlast = m - 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000064 skip = mlast - 1;
Antoine Pitrouf2c54842010-01-13 08:07:53 +000065 mask = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +000066
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +000067 if (mode != FAST_RSEARCH) {
68
69 /* create compressed boyer-moore delta 1 table */
70
71 /* process pattern[:-1] */
Antoine Pitrouf2c54842010-01-13 08:07:53 +000072 for (i = 0; i < mlast; i++) {
73 BLOOM_ADD(mask, p[i]);
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +000074 if (p[i] == p[mlast])
75 skip = mlast - i - 1;
76 }
77 /* process pattern[-1] outside the loop */
Antoine Pitrouf2c54842010-01-13 08:07:53 +000078 BLOOM_ADD(mask, p[mlast]);
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +000079
80 for (i = 0; i <= w; i++) {
81 /* note: using mlast in the skip path slows things down on x86 */
82 if (s[i+m-1] == p[m-1]) {
83 /* candidate match */
84 for (j = 0; j < mlast; j++)
85 if (s[i+j] != p[j])
86 break;
87 if (j == mlast) {
88 /* got a match! */
89 if (mode != FAST_COUNT)
90 return i;
91 count++;
Antoine Pitrouf2c54842010-01-13 08:07:53 +000092 if (count == maxcount)
93 return maxcount;
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +000094 i = i + mlast;
95 continue;
96 }
97 /* miss: check if next character is part of pattern */
Antoine Pitrouf2c54842010-01-13 08:07:53 +000098 if (!BLOOM(mask, s[i+m]))
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +000099 i = i + m;
100 else
101 i = i + skip;
102 } else {
103 /* skip: check if next character is part of pattern */
Antoine Pitrouf2c54842010-01-13 08:07:53 +0000104 if (!BLOOM(mask, s[i+m]))
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +0000105 i = i + m;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000106 }
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +0000107 }
108 } else { /* FAST_RSEARCH */
109
110 /* create compressed boyer-moore delta 1 table */
111
112 /* process pattern[0] outside the loop */
Antoine Pitrouf2c54842010-01-13 08:07:53 +0000113 BLOOM_ADD(mask, p[0]);
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +0000114 /* process pattern[:0:-1] */
115 for (i = mlast; i > 0; i--) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +0000116 BLOOM_ADD(mask, p[i]);
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +0000117 if (p[i] == p[0])
118 skip = i - 1;
119 }
120
121 for (i = w; i >= 0; i--) {
122 if (s[i] == p[0]) {
123 /* candidate match */
124 for (j = mlast; j > 0; j--)
125 if (s[i+j] != p[j])
126 break;
127 if (j == 0)
128 /* got a match! */
129 return i;
130 /* miss: check if previous character is part of pattern */
Antoine Pitrouf2c54842010-01-13 08:07:53 +0000131 if (!BLOOM(mask, s[i-1]))
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +0000132 i = i - m;
133 else
134 i = i - skip;
135 } else {
136 /* skip: check if previous character is part of pattern */
Antoine Pitrouf2c54842010-01-13 08:07:53 +0000137 if (!BLOOM(mask, s[i-1]))
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +0000138 i = i - m;
139 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000140 }
141 }
142
143 if (mode != FAST_COUNT)
144 return -1;
145 return count;
146}
147
148#endif