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
,
40 return str_len
+ offset
;
42 pos
= fastsearch(str
, str_len
, sub
, sub_len
, FAST_RSEARCH
);
50 Py_LOCAL_INLINE(Py_ssize_t
)
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
)
66 return stringlib_find(str
+ start
, end
- start
, sub
, sub_len
, start
);
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
)
85 return stringlib_rfind(str
+ start
, end
- start
, sub
, sub_len
, start
);
88 #if defined(STRINGLIB_STR) && !defined(FROM_BYTEARRAY)
91 stringlib_contains_obj(PyObject
* str
, PyObject
* sub
)
93 return stringlib_find(
94 STRINGLIB_STR(str
), STRINGLIB_LEN(str
),
95 STRINGLIB_STR(sub
), STRINGLIB_LEN(sub
), 0
99 #endif /* STRINGLIB_STR */
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.
108 It does not touch the variables received until it knows everything
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).
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
;
125 if (!PyArg_ParseTuple(args
, "O|OO:find", &tmp_substring
,
126 &obj_start
, &obj_end
))
129 /* To support None in "start" and "end" arguments, meaning
130 the same as if they were not passed.
132 if (obj_start
!= Py_None
)
133 if (!_PyEval_SliceIndex(obj_start
, &tmp_start
))
135 if (obj_end
!= Py_None
)
136 if (!_PyEval_SliceIndex(obj_end
, &tmp_end
))
139 tmp_substring
= PyUnicode_FromObject(tmp_substring
);
145 *substring
= tmp_substring
;
149 #endif /* FROM_UNICODE */
151 #endif /* STRINGLIB_FIND_H */
156 indent-tabs-mode: nil