Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1 | /* 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 Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 8 | for some more background, see: http://effbot.org/zone/stringlib.htm */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9 | |
| 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 Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 19 | #define FAST_RSEARCH 2 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 20 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 21 | #if LONG_BIT >= 128 |
| 22 | #define STRINGLIB_BLOOM_WIDTH 128 |
| 23 | #elif LONG_BIT >= 64 |
| 24 | #define STRINGLIB_BLOOM_WIDTH 64 |
| 25 | #elif LONG_BIT >= 32 |
| 26 | #define STRINGLIB_BLOOM_WIDTH 32 |
| 27 | #else |
| 28 | #error "LONG_BIT is smaller than 32" |
| 29 | #endif |
| 30 | |
| 31 | #define STRINGLIB_BLOOM_ADD(mask, ch) \ |
| 32 | ((mask |= (1UL << ((ch) & (STRINGLIB_BLOOM_WIDTH -1))))) |
| 33 | #define STRINGLIB_BLOOM(mask, ch) \ |
| 34 | ((mask & (1UL << ((ch) & (STRINGLIB_BLOOM_WIDTH -1))))) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 35 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 36 | Py_LOCAL_INLINE(Py_ssize_t) |
| 37 | fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n, |
| 38 | const STRINGLIB_CHAR* p, Py_ssize_t m, |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 39 | Py_ssize_t maxcount, int mode) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 40 | { |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 41 | unsigned long mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 42 | Py_ssize_t skip, count = 0; |
| 43 | Py_ssize_t i, j, mlast, w; |
| 44 | |
| 45 | w = n - m; |
| 46 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 47 | if (w < 0 || (mode == FAST_COUNT && maxcount == 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 48 | return -1; |
| 49 | |
| 50 | /* look for special cases */ |
| 51 | if (m <= 1) { |
| 52 | if (m <= 0) |
| 53 | return -1; |
| 54 | /* use special case for 1-character strings */ |
| 55 | if (mode == FAST_COUNT) { |
| 56 | for (i = 0; i < n; i++) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 57 | if (s[i] == p[0]) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 58 | count++; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 59 | if (count == maxcount) |
| 60 | return maxcount; |
| 61 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 62 | return count; |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 63 | } else if (mode == FAST_SEARCH) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 64 | for (i = 0; i < n; i++) |
| 65 | if (s[i] == p[0]) |
| 66 | return i; |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 67 | } else { /* FAST_RSEARCH */ |
| 68 | for (i = n - 1; i > -1; i--) |
| 69 | if (s[i] == p[0]) |
| 70 | return i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 71 | } |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | mlast = m - 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 76 | skip = mlast - 1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 77 | mask = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 78 | |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 79 | if (mode != FAST_RSEARCH) { |
| 80 | |
| 81 | /* create compressed boyer-moore delta 1 table */ |
| 82 | |
| 83 | /* process pattern[:-1] */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 84 | for (i = 0; i < mlast; i++) { |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 85 | STRINGLIB_BLOOM_ADD(mask, p[i]); |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 86 | if (p[i] == p[mlast]) |
| 87 | skip = mlast - i - 1; |
| 88 | } |
| 89 | /* process pattern[-1] outside the loop */ |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 90 | STRINGLIB_BLOOM_ADD(mask, p[mlast]); |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 91 | |
| 92 | for (i = 0; i <= w; i++) { |
| 93 | /* note: using mlast in the skip path slows things down on x86 */ |
| 94 | if (s[i+m-1] == p[m-1]) { |
| 95 | /* candidate match */ |
| 96 | for (j = 0; j < mlast; j++) |
| 97 | if (s[i+j] != p[j]) |
| 98 | break; |
| 99 | if (j == mlast) { |
| 100 | /* got a match! */ |
| 101 | if (mode != FAST_COUNT) |
| 102 | return i; |
| 103 | count++; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 104 | if (count == maxcount) |
| 105 | return maxcount; |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 106 | i = i + mlast; |
| 107 | continue; |
| 108 | } |
| 109 | /* miss: check if next character is part of pattern */ |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 110 | if (!STRINGLIB_BLOOM(mask, s[i+m])) |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 111 | i = i + m; |
| 112 | else |
| 113 | i = i + skip; |
| 114 | } else { |
| 115 | /* skip: check if next character is part of pattern */ |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 116 | if (!STRINGLIB_BLOOM(mask, s[i+m])) |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 117 | i = i + m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 118 | } |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 119 | } |
| 120 | } else { /* FAST_RSEARCH */ |
| 121 | |
| 122 | /* create compressed boyer-moore delta 1 table */ |
| 123 | |
| 124 | /* process pattern[0] outside the loop */ |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 125 | STRINGLIB_BLOOM_ADD(mask, p[0]); |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 126 | /* process pattern[:0:-1] */ |
| 127 | for (i = mlast; i > 0; i--) { |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 128 | STRINGLIB_BLOOM_ADD(mask, p[i]); |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 129 | if (p[i] == p[0]) |
| 130 | skip = i - 1; |
| 131 | } |
| 132 | |
| 133 | for (i = w; i >= 0; i--) { |
| 134 | if (s[i] == p[0]) { |
| 135 | /* candidate match */ |
| 136 | for (j = mlast; j > 0; j--) |
| 137 | if (s[i+j] != p[j]) |
| 138 | break; |
| 139 | if (j == 0) |
| 140 | /* got a match! */ |
| 141 | return i; |
| 142 | /* miss: check if previous character is part of pattern */ |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 143 | if (!STRINGLIB_BLOOM(mask, s[i-1])) |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 144 | i = i - m; |
| 145 | else |
| 146 | i = i - skip; |
| 147 | } else { |
| 148 | /* skip: check if previous character is part of pattern */ |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 149 | if (!STRINGLIB_BLOOM(mask, s[i-1])) |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 150 | i = i - m; |
| 151 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | if (mode != FAST_COUNT) |
| 156 | return -1; |
| 157 | return count; |
| 158 | } |
| 159 | |
| 160 | #endif |