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, |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 12 | const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, |
| 13 | Py_ssize_t offset) |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 14 | { |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 15 | Py_ssize_t pos; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 16 | |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 17 | if (sub_len == 0) |
| 18 | return offset; |
| 19 | |
| 20 | pos = fastsearch(str, str_len, sub, sub_len, FAST_SEARCH); |
| 21 | |
| 22 | if (pos >= 0) |
| 23 | pos += offset; |
| 24 | |
| 25 | return pos; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | Py_LOCAL(Py_ssize_t) |
| 29 | stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 30 | const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, |
| 31 | Py_ssize_t offset) |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 32 | { |
| 33 | Py_ssize_t pos; |
| 34 | |
| 35 | /* XXX - create reversefastsearch helper! */ |
| 36 | if (sub_len == 0) |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 37 | pos = str_len + offset; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 38 | else { |
| 39 | Py_ssize_t j; |
| 40 | pos = -1; |
| 41 | for (j = str_len - sub_len; j >= 0; --j) |
| 42 | if (STRINGLIB_CMP(str+j, sub, sub_len) == 0) { |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 43 | pos = j + offset; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 44 | break; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return pos; |
| 49 | } |
| 50 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame^] | 51 | #ifdef STRINGLIB_STR |
| 52 | |
| 53 | Py_LOCAL(Py_ssize_t) |
| 54 | stringlib_find_obj(PyObject* str, PyObject* sub, |
| 55 | Py_ssize_t start, Py_ssize_t end) |
| 56 | { |
| 57 | return stringlib_find( |
| 58 | STRINGLIB_STR(str) + start, end - start, |
| 59 | STRINGLIB_STR(sub), STRINGLIB_LEN(sub), start |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | Py_LOCAL(int) |
| 64 | stringlib_contains_obj(PyObject* str, PyObject* sub) |
| 65 | { |
| 66 | return stringlib_find( |
| 67 | STRINGLIB_STR(str), STRINGLIB_LEN(str), |
| 68 | STRINGLIB_STR(sub), STRINGLIB_LEN(sub), 0 |
| 69 | ) != -1; |
| 70 | } |
| 71 | |
| 72 | Py_LOCAL(Py_ssize_t) |
| 73 | stringlib_rfind_obj(PyObject* str, PyObject* sub, |
| 74 | Py_ssize_t start, Py_ssize_t end) |
| 75 | { |
| 76 | return stringlib_rfind( |
| 77 | STRINGLIB_STR(str) + start, end - start, |
| 78 | STRINGLIB_STR(sub), STRINGLIB_LEN(sub), start |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | #endif |
| 83 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 84 | #endif |
| 85 | |
| 86 | /* |
| 87 | Local variables: |
| 88 | c-basic-offset: 4 |
| 89 | indent-tabs-mode: nil |
| 90 | End: |
| 91 | */ |