Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1 | /* stringlib: find/index implementation */ |
| 2 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3 | #ifndef STRINGLIB_FASTSEARCH_H |
| 4 | #error must include "stringlib/fastsearch.h" before including this module |
| 5 | #endif |
| 6 | |
| 7 | Py_LOCAL_INLINE(Py_ssize_t) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8 | STRINGLIB(find)(const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9 | const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, |
| 10 | Py_ssize_t offset) |
| 11 | { |
| 12 | Py_ssize_t pos; |
| 13 | |
Amaury Forgeot d'Arc | f2e9368 | 2008-09-26 22:48:41 +0000 | [diff] [blame] | 14 | if (str_len < 0) |
| 15 | return -1; |
| 16 | if (sub_len == 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 17 | return offset; |
| 18 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 19 | pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_SEARCH); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 20 | |
| 21 | if (pos >= 0) |
| 22 | pos += offset; |
| 23 | |
| 24 | return pos; |
| 25 | } |
| 26 | |
| 27 | Py_LOCAL_INLINE(Py_ssize_t) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 28 | STRINGLIB(rfind)(const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 29 | const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, |
| 30 | Py_ssize_t offset) |
| 31 | { |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 32 | Py_ssize_t pos; |
| 33 | |
| 34 | if (str_len < 0) |
| 35 | return -1; |
| 36 | if (sub_len == 0) |
| 37 | return str_len + offset; |
| 38 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 39 | pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_RSEARCH); |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 40 | |
| 41 | if (pos >= 0) |
| 42 | pos += offset; |
| 43 | |
| 44 | return pos; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 47 | /* helper macro to fixup start/end slice values */ |
| 48 | #define ADJUST_INDICES(start, end, len) \ |
| 49 | if (end > len) \ |
| 50 | end = len; \ |
| 51 | else if (end < 0) { \ |
| 52 | end += len; \ |
| 53 | if (end < 0) \ |
| 54 | end = 0; \ |
| 55 | } \ |
| 56 | if (start < 0) { \ |
| 57 | start += len; \ |
| 58 | if (start < 0) \ |
| 59 | start = 0; \ |
| 60 | } |
| 61 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 62 | Py_LOCAL_INLINE(Py_ssize_t) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 63 | STRINGLIB(find_slice)(const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 64 | const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, |
| 65 | Py_ssize_t start, Py_ssize_t end) |
| 66 | { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 67 | ADJUST_INDICES(start, end, str_len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 68 | return STRINGLIB(find)(str + start, end - start, sub, sub_len, start); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | Py_LOCAL_INLINE(Py_ssize_t) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 72 | STRINGLIB(rfind_slice)(const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 73 | const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, |
| 74 | Py_ssize_t start, Py_ssize_t end) |
| 75 | { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 76 | ADJUST_INDICES(start, end, str_len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 77 | return STRINGLIB(rfind)(str + start, end - start, sub, sub_len, start); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Christian Heimes | 4f3c561 | 2008-08-28 14:55:10 +0000 | [diff] [blame] | 80 | #ifdef STRINGLIB_WANT_CONTAINS_OBJ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 81 | |
| 82 | Py_LOCAL_INLINE(int) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 83 | STRINGLIB(contains_obj)(PyObject* str, PyObject* sub) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 84 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 85 | return STRINGLIB(find)( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 86 | STRINGLIB_STR(str), STRINGLIB_LEN(str), |
| 87 | STRINGLIB_STR(sub), STRINGLIB_LEN(sub), 0 |
| 88 | ) != -1; |
| 89 | } |
| 90 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 91 | #endif /* STRINGLIB_WANT_CONTAINS_OBJ */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 92 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 93 | /* |
| 94 | This function is a helper for the "find" family (find, rfind, index, |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 95 | rindex) and for count, startswith and endswith, because they all have |
| 96 | the same behaviour for the arguments. |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 97 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 98 | It does not touch the variables received until it knows everything |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 99 | is ok. |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 100 | */ |
| 101 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 102 | #define FORMAT_BUFFER_SIZE 50 |
| 103 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 104 | Py_LOCAL_INLINE(int) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 105 | STRINGLIB(parse_args_finds)(const char * function_name, PyObject *args, |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 106 | PyObject **subobj, |
| 107 | Py_ssize_t *start, Py_ssize_t *end) |
| 108 | { |
| 109 | PyObject *tmp_subobj; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 110 | Py_ssize_t tmp_start = 0; |
| 111 | Py_ssize_t tmp_end = PY_SSIZE_T_MAX; |
| 112 | PyObject *obj_start=Py_None, *obj_end=Py_None; |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 113 | char format[FORMAT_BUFFER_SIZE] = "O|OO:"; |
| 114 | size_t len = strlen(format); |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 115 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 116 | strncpy(format + len, function_name, FORMAT_BUFFER_SIZE - len - 1); |
| 117 | format[FORMAT_BUFFER_SIZE - 1] = '\0'; |
| 118 | |
| 119 | if (!PyArg_ParseTuple(args, format, &tmp_subobj, &obj_start, &obj_end)) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 120 | return 0; |
| 121 | |
| 122 | /* To support None in "start" and "end" arguments, meaning |
| 123 | the same as if they were not passed. |
| 124 | */ |
| 125 | if (obj_start != Py_None) |
| 126 | if (!_PyEval_SliceIndex(obj_start, &tmp_start)) |
| 127 | return 0; |
| 128 | if (obj_end != Py_None) |
| 129 | if (!_PyEval_SliceIndex(obj_end, &tmp_end)) |
| 130 | return 0; |
| 131 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 132 | *start = tmp_start; |
| 133 | *end = tmp_end; |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 134 | *subobj = tmp_subobj; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 135 | return 1; |
| 136 | } |
| 137 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 138 | #undef FORMAT_BUFFER_SIZE |
| 139 | |
Jesus Cea | 6159ee3 | 2011-04-20 17:42:50 +0200 | [diff] [blame] | 140 | #if STRINGLIB_IS_UNICODE |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | Wraps stringlib_parse_args_finds() and additionally ensures that the |
| 144 | first argument is a unicode object. |
| 145 | |
| 146 | Note that we receive a pointer to the pointer of the substring object, |
| 147 | so when we create that object in this function we don't DECREF it, |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 148 | because it continues living in the caller functions (those functions, |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 149 | after finishing using the substring, must DECREF it). |
| 150 | */ |
| 151 | |
| 152 | Py_LOCAL_INLINE(int) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 153 | STRINGLIB(parse_args_finds_unicode)(const char * function_name, PyObject *args, |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 154 | PyObject **substring, |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 155 | Py_ssize_t *start, Py_ssize_t *end) |
| 156 | { |
| 157 | PyObject *tmp_substring; |
| 158 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 159 | if(STRINGLIB(parse_args_finds)(function_name, args, &tmp_substring, |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 160 | start, end)) { |
| 161 | tmp_substring = PyUnicode_FromObject(tmp_substring); |
| 162 | if (!tmp_substring) |
| 163 | return 0; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 164 | *substring = tmp_substring; |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 165 | return 1; |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 170 | #else /* !STRINGLIB_IS_UNICODE */ |
| 171 | |
| 172 | /* |
| 173 | Wraps stringlib_parse_args_finds() and additionally checks whether the |
| 174 | first argument is an integer in range(0, 256). |
| 175 | |
| 176 | If this is the case, writes the integer value to the byte parameter |
| 177 | and sets subobj to NULL. Otherwise, sets the first argument to subobj |
| 178 | and doesn't touch byte. The other parameters are similar to those of |
| 179 | stringlib_parse_args_finds(). |
| 180 | */ |
| 181 | |
| 182 | Py_LOCAL_INLINE(int) |
| 183 | STRINGLIB(parse_args_finds_byte)(const char *function_name, PyObject *args, |
| 184 | PyObject **subobj, char *byte, |
| 185 | Py_ssize_t *start, Py_ssize_t *end) |
| 186 | { |
| 187 | PyObject *tmp_subobj; |
| 188 | Py_ssize_t ival; |
Victor Stinner | f8eac00 | 2011-12-18 01:17:41 +0100 | [diff] [blame] | 189 | PyObject *err; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 190 | |
| 191 | if(!STRINGLIB(parse_args_finds)(function_name, args, &tmp_subobj, |
| 192 | start, end)) |
| 193 | return 0; |
| 194 | |
Victor Stinner | f8eac00 | 2011-12-18 01:17:41 +0100 | [diff] [blame] | 195 | if (!PyNumber_Check(tmp_subobj)) { |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 196 | *subobj = tmp_subobj; |
Victor Stinner | f8eac00 | 2011-12-18 01:17:41 +0100 | [diff] [blame] | 197 | return 1; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 198 | } |
Victor Stinner | f8eac00 | 2011-12-18 01:17:41 +0100 | [diff] [blame] | 199 | |
| 200 | ival = PyNumber_AsSsize_t(tmp_subobj, PyExc_OverflowError); |
| 201 | if (ival == -1) { |
| 202 | err = PyErr_Occurred(); |
| 203 | if (err && !PyErr_GivenExceptionMatches(err, PyExc_OverflowError)) { |
| 204 | PyErr_Clear(); |
| 205 | *subobj = tmp_subobj; |
| 206 | return 1; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 207 | } |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Victor Stinner | f8eac00 | 2011-12-18 01:17:41 +0100 | [diff] [blame] | 210 | if (ival < 0 || ival > 255) { |
| 211 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | *subobj = NULL; |
| 216 | *byte = (char)ival; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 217 | return 1; |
| 218 | } |
| 219 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 220 | #endif /* STRINGLIB_IS_UNICODE */ |