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 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 10 | Py_LOCAL_INLINE(Py_ssize_t) |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 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 | |
Amaury Forgeot d'Arc | fc5ea39 | 2008-09-26 22:34:08 +0000 | [diff] [blame] | 17 | if (str_len < 0) |
| 18 | return -1; |
| 19 | if (sub_len == 0) |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 20 | return offset; |
| 21 | |
| 22 | pos = fastsearch(str, str_len, sub, sub_len, FAST_SEARCH); |
| 23 | |
| 24 | if (pos >= 0) |
| 25 | pos += offset; |
| 26 | |
| 27 | return pos; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 30 | Py_LOCAL_INLINE(Py_ssize_t) |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 31 | stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 32 | const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, |
| 33 | Py_ssize_t offset) |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 34 | { |
Antoine Pitrou | 5b7139a | 2010-01-02 21:12:58 +0000 | [diff] [blame] | 35 | Py_ssize_t pos; |
| 36 | |
| 37 | if (str_len < 0) |
| 38 | return -1; |
| 39 | if (sub_len == 0) |
| 40 | return str_len + offset; |
| 41 | |
| 42 | pos = fastsearch(str, str_len, sub, sub_len, FAST_RSEARCH); |
| 43 | |
| 44 | if (pos >= 0) |
| 45 | pos += offset; |
| 46 | |
| 47 | return pos; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 50 | Py_LOCAL_INLINE(Py_ssize_t) |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 51 | stringlib_find_slice(const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
| 52 | const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, |
| 53 | Py_ssize_t start, Py_ssize_t end) |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 54 | { |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 55 | if (start < 0) |
| 56 | start += str_len; |
| 57 | if (start < 0) |
| 58 | start = 0; |
| 59 | if (end > str_len) |
| 60 | end = str_len; |
| 61 | if (end < 0) |
| 62 | end += str_len; |
| 63 | if (end < 0) |
| 64 | end = 0; |
| 65 | |
Antoine Pitrou | 5b7139a | 2010-01-02 21:12:58 +0000 | [diff] [blame] | 66 | return stringlib_find(str + start, end - start, sub, sub_len, start); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 69 | Py_LOCAL_INLINE(Py_ssize_t) |
| 70 | stringlib_rfind_slice(const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
| 71 | const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, |
| 72 | Py_ssize_t start, Py_ssize_t end) |
| 73 | { |
| 74 | if (start < 0) |
| 75 | start += str_len; |
| 76 | if (start < 0) |
| 77 | start = 0; |
| 78 | if (end > str_len) |
| 79 | end = str_len; |
| 80 | if (end < 0) |
| 81 | end += str_len; |
| 82 | if (end < 0) |
| 83 | end = 0; |
| 84 | |
| 85 | return stringlib_rfind(str + start, end - start, sub, sub_len, start); |
| 86 | } |
| 87 | |
Christian Heimes | 7d4c317 | 2008-08-22 19:47:25 +0000 | [diff] [blame] | 88 | #if defined(STRINGLIB_STR) && !defined(FROM_BYTEARRAY) |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 89 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 90 | Py_LOCAL_INLINE(int) |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 91 | stringlib_contains_obj(PyObject* str, PyObject* sub) |
| 92 | { |
| 93 | return stringlib_find( |
| 94 | STRINGLIB_STR(str), STRINGLIB_LEN(str), |
| 95 | STRINGLIB_STR(sub), STRINGLIB_LEN(sub), 0 |
| 96 | ) != -1; |
| 97 | } |
| 98 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 99 | #endif /* STRINGLIB_STR */ |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 100 | |
Facundo Batista | 6f7e6fb | 2007-11-16 19:16:15 +0000 | [diff] [blame] | 101 | #ifdef FROM_UNICODE |
| 102 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 103 | /* |
| 104 | This function is a helper for the "find" family (find, rfind, index, |
| 105 | rindex) of unicodeobject.c file, because they all have the same |
| 106 | behaviour for the arguments. |
| 107 | |
| 108 | It does not touch the variables received until it knows everything |
| 109 | is ok. |
| 110 | |
| 111 | Note that we receive a pointer to the pointer of the substring object, |
| 112 | so when we create that object in this function we don't DECREF it, |
| 113 | because it continues living in the caller functions (those functions, |
| 114 | after finishing using the substring, must DECREF it). |
| 115 | */ |
| 116 | |
Facundo Batista | 292a069 | 2007-11-16 18:41:24 +0000 | [diff] [blame] | 117 | Py_LOCAL_INLINE(int) |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 118 | _ParseTupleFinds (PyObject *args, PyObject **substring, |
| 119 | Py_ssize_t *start, Py_ssize_t *end) { |
| 120 | PyObject *tmp_substring; |
| 121 | Py_ssize_t tmp_start = 0; |
| 122 | Py_ssize_t tmp_end = PY_SSIZE_T_MAX; |
| 123 | PyObject *obj_start=Py_None, *obj_end=Py_None; |
| 124 | |
| 125 | if (!PyArg_ParseTuple(args, "O|OO:find", &tmp_substring, |
| 126 | &obj_start, &obj_end)) |
| 127 | return 0; |
| 128 | |
| 129 | /* To support None in "start" and "end" arguments, meaning |
| 130 | the same as if they were not passed. |
| 131 | */ |
| 132 | if (obj_start != Py_None) |
| 133 | if (!_PyEval_SliceIndex(obj_start, &tmp_start)) |
| 134 | return 0; |
| 135 | if (obj_end != Py_None) |
| 136 | if (!_PyEval_SliceIndex(obj_end, &tmp_end)) |
| 137 | return 0; |
| 138 | |
| 139 | tmp_substring = PyUnicode_FromObject(tmp_substring); |
| 140 | if (!tmp_substring) |
| 141 | return 0; |
| 142 | |
| 143 | *start = tmp_start; |
| 144 | *end = tmp_end; |
| 145 | *substring = tmp_substring; |
| 146 | return 1; |
| 147 | } |
| 148 | |
Facundo Batista | 6f7e6fb | 2007-11-16 19:16:15 +0000 | [diff] [blame] | 149 | #endif /* FROM_UNICODE */ |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 150 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 151 | #endif /* STRINGLIB_FIND_H */ |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | Local variables: |
| 155 | c-basic-offset: 4 |
| 156 | indent-tabs-mode: nil |
| 157 | End: |
| 158 | */ |