Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1 | /* stringlib: fastsearch implementation */ |
| 2 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3 | #define STRINGLIB_FASTSEARCH_H |
| 4 | |
| 5 | /* fast search/count implementation, based on a mix between boyer- |
| 6 | 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] | 7 | for some more background, see: http://effbot.org/zone/stringlib.htm */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8 | |
| 9 | /* note: fastsearch may access s[n], which isn't a problem when using |
| 10 | Python's ordinary string types, but may cause problems if you're |
| 11 | using this code in other contexts. also, the count mode returns -1 |
| 12 | if there cannot possible be a match in the target string, and 0 if |
| 13 | it has actually checked for matches, but didn't find any. callers |
| 14 | beware! */ |
| 15 | |
| 16 | #define FAST_COUNT 0 |
| 17 | #define FAST_SEARCH 1 |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 18 | #define FAST_RSEARCH 2 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 19 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 20 | #if LONG_BIT >= 128 |
| 21 | #define STRINGLIB_BLOOM_WIDTH 128 |
| 22 | #elif LONG_BIT >= 64 |
| 23 | #define STRINGLIB_BLOOM_WIDTH 64 |
| 24 | #elif LONG_BIT >= 32 |
| 25 | #define STRINGLIB_BLOOM_WIDTH 32 |
| 26 | #else |
| 27 | #error "LONG_BIT is smaller than 32" |
| 28 | #endif |
| 29 | |
| 30 | #define STRINGLIB_BLOOM_ADD(mask, ch) \ |
| 31 | ((mask |= (1UL << ((ch) & (STRINGLIB_BLOOM_WIDTH -1))))) |
| 32 | #define STRINGLIB_BLOOM(mask, ch) \ |
| 33 | ((mask & (1UL << ((ch) & (STRINGLIB_BLOOM_WIDTH -1))))) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 34 | |
Antoine Pitrou | 2c3b230 | 2011-10-11 20:29:21 +0200 | [diff] [blame] | 35 | |
| 36 | Py_LOCAL_INLINE(Py_ssize_t) |
| 37 | STRINGLIB(fastsearch_memchr_1char)(const STRINGLIB_CHAR* s, Py_ssize_t n, |
| 38 | STRINGLIB_CHAR ch, unsigned char needle, |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 39 | int mode) |
Antoine Pitrou | 2c3b230 | 2011-10-11 20:29:21 +0200 | [diff] [blame] | 40 | { |
Antoine Pitrou | 2c3b230 | 2011-10-11 20:29:21 +0200 | [diff] [blame] | 41 | if (mode == FAST_SEARCH) { |
Victor Stinner | b3f5501 | 2012-08-02 23:05:01 +0200 | [diff] [blame] | 42 | const STRINGLIB_CHAR *ptr = s; |
Antoine Pitrou | 2c3b230 | 2011-10-11 20:29:21 +0200 | [diff] [blame] | 43 | const STRINGLIB_CHAR *e = s + n; |
Victor Stinner | b3f5501 | 2012-08-02 23:05:01 +0200 | [diff] [blame] | 44 | while (ptr < e) { |
Serhiy Storchaka | 18ba40b | 2013-01-15 13:27:28 +0200 | [diff] [blame] | 45 | void *candidate = memchr((const void *) ptr, needle, (e - ptr) * sizeof(STRINGLIB_CHAR)); |
| 46 | if (candidate == NULL) |
Antoine Pitrou | 2c3b230 | 2011-10-11 20:29:21 +0200 | [diff] [blame] | 47 | return -1; |
Serhiy Storchaka | 18ba40b | 2013-01-15 13:27:28 +0200 | [diff] [blame] | 48 | ptr = (const STRINGLIB_CHAR *) _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR)); |
| 49 | if (sizeof(STRINGLIB_CHAR) == 1 || *ptr == ch) |
| 50 | return (ptr - s); |
Antoine Pitrou | 2c3b230 | 2011-10-11 20:29:21 +0200 | [diff] [blame] | 51 | /* False positive */ |
Serhiy Storchaka | 18ba40b | 2013-01-15 13:27:28 +0200 | [diff] [blame] | 52 | ptr++; |
Antoine Pitrou | 2c3b230 | 2011-10-11 20:29:21 +0200 | [diff] [blame] | 53 | } |
| 54 | return -1; |
| 55 | } |
| 56 | #ifdef HAVE_MEMRCHR |
| 57 | /* memrchr() is a GNU extension, available since glibc 2.1.91. |
| 58 | it doesn't seem as optimized as memchr(), but is still quite |
| 59 | faster than our hand-written loop in FASTSEARCH below */ |
| 60 | else if (mode == FAST_RSEARCH) { |
| 61 | while (n > 0) { |
Serhiy Storchaka | 18ba40b | 2013-01-15 13:27:28 +0200 | [diff] [blame] | 62 | const STRINGLIB_CHAR *found; |
| 63 | void *candidate = memrchr((const void *) s, needle, n * sizeof(STRINGLIB_CHAR)); |
| 64 | if (candidate == NULL) |
Antoine Pitrou | 2c3b230 | 2011-10-11 20:29:21 +0200 | [diff] [blame] | 65 | return -1; |
Serhiy Storchaka | 18ba40b | 2013-01-15 13:27:28 +0200 | [diff] [blame] | 66 | found = (const STRINGLIB_CHAR *) _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR)); |
Antoine Pitrou | 2c3b230 | 2011-10-11 20:29:21 +0200 | [diff] [blame] | 67 | n = found - s; |
| 68 | if (sizeof(STRINGLIB_CHAR) == 1 || *found == ch) |
| 69 | return n; |
| 70 | /* False positive */ |
| 71 | } |
| 72 | return -1; |
| 73 | } |
| 74 | #endif |
| 75 | else { |
| 76 | assert(0); /* Should never get here */ |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | #undef DO_MEMCHR |
| 81 | } |
| 82 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 83 | Py_LOCAL_INLINE(Py_ssize_t) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 84 | FASTSEARCH(const STRINGLIB_CHAR* s, Py_ssize_t n, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 85 | const STRINGLIB_CHAR* p, Py_ssize_t m, |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 86 | Py_ssize_t maxcount, int mode) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 87 | { |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 88 | unsigned long mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 89 | Py_ssize_t skip, count = 0; |
| 90 | Py_ssize_t i, j, mlast, w; |
| 91 | |
| 92 | w = n - m; |
| 93 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 94 | if (w < 0 || (mode == FAST_COUNT && maxcount == 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 95 | return -1; |
| 96 | |
| 97 | /* look for special cases */ |
| 98 | if (m <= 1) { |
| 99 | if (m <= 0) |
| 100 | return -1; |
| 101 | /* use special case for 1-character strings */ |
Antoine Pitrou | 2c3b230 | 2011-10-11 20:29:21 +0200 | [diff] [blame] | 102 | if (n > 10 && (mode == FAST_SEARCH |
| 103 | #ifdef HAVE_MEMRCHR |
| 104 | || mode == FAST_RSEARCH |
| 105 | #endif |
| 106 | )) { |
| 107 | /* use memchr if we can choose a needle without two many likely |
| 108 | false positives */ |
| 109 | unsigned char needle; |
Antoine Pitrou | 2c3b230 | 2011-10-11 20:29:21 +0200 | [diff] [blame] | 110 | needle = p[0] & 0xff; |
Victor Stinner | 8cc70dc | 2011-10-11 23:22:22 +0200 | [diff] [blame] | 111 | #if STRINGLIB_SIZEOF_CHAR > 1 |
Antoine Pitrou | 5b9f4c1 | 2011-10-17 19:21:04 +0200 | [diff] [blame] | 112 | /* If looking for a multiple of 256, we'd have too |
Antoine Pitrou | c198d05 | 2011-10-13 18:07:37 +0200 | [diff] [blame] | 113 | many false positives looking for the '\0' byte in UCS2 |
| 114 | and UCS4 representations. */ |
Antoine Pitrou | dda339e | 2011-10-13 17:58:11 +0200 | [diff] [blame] | 115 | if (needle != 0) |
Victor Stinner | 8cc70dc | 2011-10-11 23:22:22 +0200 | [diff] [blame] | 116 | #endif |
Antoine Pitrou | 2c3b230 | 2011-10-11 20:29:21 +0200 | [diff] [blame] | 117 | return STRINGLIB(fastsearch_memchr_1char) |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 118 | (s, n, p[0], needle, mode); |
Antoine Pitrou | 2c3b230 | 2011-10-11 20:29:21 +0200 | [diff] [blame] | 119 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 120 | if (mode == FAST_COUNT) { |
| 121 | for (i = 0; i < n; i++) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 122 | if (s[i] == p[0]) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 123 | count++; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 124 | if (count == maxcount) |
| 125 | return maxcount; |
| 126 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 127 | return count; |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 128 | } else if (mode == FAST_SEARCH) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 129 | for (i = 0; i < n; i++) |
| 130 | if (s[i] == p[0]) |
| 131 | return i; |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 132 | } else { /* FAST_RSEARCH */ |
| 133 | for (i = n - 1; i > -1; i--) |
| 134 | if (s[i] == p[0]) |
| 135 | return i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 136 | } |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | mlast = m - 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 141 | skip = mlast - 1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 142 | mask = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 143 | |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 144 | if (mode != FAST_RSEARCH) { |
Victor Stinner | 7efa3b8 | 2013-04-08 00:26:43 +0200 | [diff] [blame] | 145 | const STRINGLIB_CHAR *ss = s + m - 1; |
| 146 | const STRINGLIB_CHAR *pp = p + m - 1; |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 147 | |
| 148 | /* create compressed boyer-moore delta 1 table */ |
| 149 | |
| 150 | /* process pattern[:-1] */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 151 | for (i = 0; i < mlast; i++) { |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 152 | STRINGLIB_BLOOM_ADD(mask, p[i]); |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 153 | if (p[i] == p[mlast]) |
| 154 | skip = mlast - i - 1; |
| 155 | } |
| 156 | /* process pattern[-1] outside the loop */ |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 157 | STRINGLIB_BLOOM_ADD(mask, p[mlast]); |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 158 | |
| 159 | for (i = 0; i <= w; i++) { |
| 160 | /* note: using mlast in the skip path slows things down on x86 */ |
Victor Stinner | 7efa3b8 | 2013-04-08 00:26:43 +0200 | [diff] [blame] | 161 | if (ss[i] == pp[0]) { |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 162 | /* candidate match */ |
| 163 | for (j = 0; j < mlast; j++) |
| 164 | if (s[i+j] != p[j]) |
| 165 | break; |
| 166 | if (j == mlast) { |
| 167 | /* got a match! */ |
| 168 | if (mode != FAST_COUNT) |
| 169 | return i; |
| 170 | count++; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 171 | if (count == maxcount) |
| 172 | return maxcount; |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 173 | i = i + mlast; |
| 174 | continue; |
| 175 | } |
| 176 | /* miss: check if next character is part of pattern */ |
Victor Stinner | 7efa3b8 | 2013-04-08 00:26:43 +0200 | [diff] [blame] | 177 | if (!STRINGLIB_BLOOM(mask, ss[i+1])) |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 178 | i = i + m; |
| 179 | else |
| 180 | i = i + skip; |
| 181 | } else { |
| 182 | /* skip: check if next character is part of pattern */ |
Victor Stinner | 7efa3b8 | 2013-04-08 00:26:43 +0200 | [diff] [blame] | 183 | if (!STRINGLIB_BLOOM(mask, ss[i+1])) |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 184 | i = i + m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 185 | } |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 186 | } |
| 187 | } else { /* FAST_RSEARCH */ |
| 188 | |
| 189 | /* create compressed boyer-moore delta 1 table */ |
| 190 | |
| 191 | /* process pattern[0] outside the loop */ |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 192 | STRINGLIB_BLOOM_ADD(mask, p[0]); |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 193 | /* process pattern[:0:-1] */ |
| 194 | for (i = mlast; i > 0; i--) { |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 195 | STRINGLIB_BLOOM_ADD(mask, p[i]); |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 196 | if (p[i] == p[0]) |
| 197 | skip = i - 1; |
| 198 | } |
| 199 | |
| 200 | for (i = w; i >= 0; i--) { |
| 201 | if (s[i] == p[0]) { |
| 202 | /* candidate match */ |
| 203 | for (j = mlast; j > 0; j--) |
| 204 | if (s[i+j] != p[j]) |
| 205 | break; |
| 206 | if (j == 0) |
| 207 | /* got a match! */ |
| 208 | return i; |
| 209 | /* miss: check if previous character is part of pattern */ |
Florent Xicluna | eb6f3ea | 2010-08-08 22:07:16 +0000 | [diff] [blame] | 210 | if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1])) |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 211 | i = i - m; |
| 212 | else |
| 213 | i = i - skip; |
| 214 | } else { |
| 215 | /* skip: check if previous character is part of pattern */ |
Florent Xicluna | eb6f3ea | 2010-08-08 22:07:16 +0000 | [diff] [blame] | 216 | if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1])) |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 217 | i = i - m; |
| 218 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | if (mode != FAST_COUNT) |
| 223 | return -1; |
| 224 | return count; |
| 225 | } |
| 226 | |