Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame^] | 1 | /* stringlib: find/index implementation */ |
| 2 | |
| 3 | #ifndef STRINGLIB_FIND_H |
| 4 | #define STRINGLIB_FIND_H |
| 5 | |
| 6 | #ifndef STRINGLIB_FASTSEARCH_H |
| 7 | #error must include "stringlib/fastsearch.h" before including this module |
| 8 | #endif |
| 9 | |
| 10 | Py_LOCAL(Py_ssize_t) |
| 11 | stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
| 12 | const STRINGLIB_CHAR* sub, Py_ssize_t sub_len) |
| 13 | { |
| 14 | if (sub_len == 0) |
| 15 | return 0; |
| 16 | |
| 17 | return fastsearch(str, str_len, sub, sub_len, FAST_SEARCH); |
| 18 | } |
| 19 | |
| 20 | Py_LOCAL(Py_ssize_t) |
| 21 | stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
| 22 | const STRINGLIB_CHAR* sub, Py_ssize_t sub_len) |
| 23 | { |
| 24 | Py_ssize_t pos; |
| 25 | |
| 26 | /* XXX - create reversefastsearch helper! */ |
| 27 | if (sub_len == 0) |
| 28 | pos = str_len; |
| 29 | else { |
| 30 | Py_ssize_t j; |
| 31 | pos = -1; |
| 32 | for (j = str_len - sub_len; j >= 0; --j) |
| 33 | if (STRINGLIB_CMP(str+j, sub, sub_len) == 0) { |
| 34 | pos = j; |
| 35 | break; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | return pos; |
| 40 | } |
| 41 | |
| 42 | #endif |
| 43 | |
| 44 | /* |
| 45 | Local variables: |
| 46 | c-basic-offset: 4 |
| 47 | indent-tabs-mode: nil |
| 48 | End: |
| 49 | */ |