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 | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame^] | 21 | #define BLOOM_ADD(mask, ch) ((mask |= (1 << ((ch) & (LONG_BIT - 1))))) |
| 22 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & (LONG_BIT - 1))))) |
| 23 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 24 | Py_LOCAL_INLINE(Py_ssize_t) |
| 25 | fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n, |
| 26 | const STRINGLIB_CHAR* p, Py_ssize_t m, |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame^] | 27 | Py_ssize_t maxcount, int mode) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 28 | { |
| 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 Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame^] | 35 | if (w < 0 || (mode == FAST_COUNT && maxcount == 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 36 | 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 Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame^] | 45 | if (s[i] == p[0]) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 46 | count++; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame^] | 47 | if (count == maxcount) |
| 48 | return maxcount; |
| 49 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 50 | return count; |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 51 | } else if (mode == FAST_SEARCH) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 52 | for (i = 0; i < n; i++) |
| 53 | if (s[i] == p[0]) |
| 54 | return i; |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 55 | } else { /* FAST_RSEARCH */ |
| 56 | for (i = n - 1; i > -1; i--) |
| 57 | if (s[i] == p[0]) |
| 58 | return i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 59 | } |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | mlast = m - 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 64 | skip = mlast - 1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame^] | 65 | mask = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 66 | |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 67 | if (mode != FAST_RSEARCH) { |
| 68 | |
| 69 | /* create compressed boyer-moore delta 1 table */ |
| 70 | |
| 71 | /* process pattern[:-1] */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame^] | 72 | for (i = 0; i < mlast; i++) { |
| 73 | BLOOM_ADD(mask, p[i]); |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 74 | if (p[i] == p[mlast]) |
| 75 | skip = mlast - i - 1; |
| 76 | } |
| 77 | /* process pattern[-1] outside the loop */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame^] | 78 | BLOOM_ADD(mask, p[mlast]); |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 79 | |
| 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 Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame^] | 92 | if (count == maxcount) |
| 93 | return maxcount; |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 94 | i = i + mlast; |
| 95 | continue; |
| 96 | } |
| 97 | /* miss: check if next character is part of pattern */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame^] | 98 | if (!BLOOM(mask, s[i+m])) |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 99 | i = i + m; |
| 100 | else |
| 101 | i = i + skip; |
| 102 | } else { |
| 103 | /* skip: check if next character is part of pattern */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame^] | 104 | if (!BLOOM(mask, s[i+m])) |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 105 | i = i + m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 106 | } |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 107 | } |
| 108 | } else { /* FAST_RSEARCH */ |
| 109 | |
| 110 | /* create compressed boyer-moore delta 1 table */ |
| 111 | |
| 112 | /* process pattern[0] outside the loop */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame^] | 113 | BLOOM_ADD(mask, p[0]); |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 114 | /* process pattern[:0:-1] */ |
| 115 | for (i = mlast; i > 0; i--) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame^] | 116 | BLOOM_ADD(mask, p[i]); |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 117 | 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 Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame^] | 131 | if (!BLOOM(mask, s[i-1])) |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 132 | i = i - m; |
| 133 | else |
| 134 | i = i - skip; |
| 135 | } else { |
| 136 | /* skip: check if previous character is part of pattern */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame^] | 137 | if (!BLOOM(mask, s[i-1])) |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 138 | i = i - m; |
| 139 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | if (mode != FAST_COUNT) |
| 144 | return -1; |
| 145 | return count; |
| 146 | } |
| 147 | |
| 148 | #endif |