1
/* stringlib: find/index implementation */
3
#ifndef STRINGLIB_FIND_H
4
#define STRINGLIB_FIND_H
6
#ifndef STRINGLIB_FASTSEARCH_H
7
#error must include "stringlib/fastsearch.h" before including this module
10
Py_LOCAL_INLINE(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,
22
pos = fastsearch(str, str_len, sub, sub_len, FAST_SEARCH);
30
Py_LOCAL_INLINE(Py_ssize_t)
31
stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
32
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
35
/* XXX - create reversefastsearch helper! */
39
return str_len + offset;
41
Py_ssize_t j, pos = -1;
42
for (j = str_len - sub_len; j >= 0; --j)
43
if (STRINGLIB_CMP(str+j, sub, sub_len) == 0) {
51
Py_LOCAL_INLINE(Py_ssize_t)
52
stringlib_find_slice(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
53
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
54
Py_ssize_t start, Py_ssize_t end)
67
return stringlib_find(
68
str + start, end - start,
73
Py_LOCAL_INLINE(Py_ssize_t)
74
stringlib_rfind_slice(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
75
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
76
Py_ssize_t start, Py_ssize_t end)
89
return stringlib_rfind(str + start, end - start, sub, sub_len, start);
92
#ifdef STRINGLIB_WANT_CONTAINS_OBJ
95
stringlib_contains_obj(PyObject* str, PyObject* sub)
97
return stringlib_find(
98
STRINGLIB_STR(str), STRINGLIB_LEN(str),
99
STRINGLIB_STR(sub), STRINGLIB_LEN(sub), 0
103
#endif /* STRINGLIB_STR */
108
This function is a helper for the "find" family (find, rfind, index,
109
rindex) of unicodeobject.c file, because they all have the same
110
behaviour for the arguments.
112
It does not touch the variables received until it knows everything
115
Note that we receive a pointer to the pointer of the substring object,
116
so when we create that object in this function we don't DECREF it,
117
because it continues living in the caller functions (those functions,
118
after finishing using the substring, must DECREF it).
122
_ParseTupleFinds (PyObject *args, PyObject **substring,
123
Py_ssize_t *start, Py_ssize_t *end) {
124
PyObject *tmp_substring;
125
Py_ssize_t tmp_start = 0;
126
Py_ssize_t tmp_end = PY_SSIZE_T_MAX;
127
PyObject *obj_start=Py_None, *obj_end=Py_None;
129
if (!PyArg_ParseTuple(args, "O|OO:find", &tmp_substring,
130
&obj_start, &obj_end))
133
/* To support None in "start" and "end" arguments, meaning
134
the same as if they were not passed.
136
if (obj_start != Py_None)
137
if (!_PyEval_SliceIndex(obj_start, &tmp_start))
139
if (obj_end != Py_None)
140
if (!_PyEval_SliceIndex(obj_end, &tmp_end))
143
tmp_substring = PyUnicode_FromObject(tmp_substring);
149
*substring = tmp_substring;
153
#endif /* FROM_UNICODE */
155
#endif /* STRINGLIB_FIND_H */
160
indent-tabs-mode: nil