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