@@ -231,6 +231,34 @@ static PyObject *cstring_count(PyObject *self, PyObject *args) {
231231 return PyLong_FromLong (result );
232232}
233233
234+ PyDoc_STRVAR (find__doc__ , "" );
235+ PyObject * cstring_find (PyObject * self , PyObject * args ) {
236+ PyObject * substr_obj ;
237+ Py_ssize_t start = 0 ;
238+ Py_ssize_t end = PY_SSIZE_T_MAX ;
239+
240+ if (!PyArg_ParseTuple (args , "O|nn" , & substr_obj , & start , & end ))
241+ return NULL ;
242+
243+ Py_ssize_t substr_len ;
244+ const char * substr = _obj_to_utf8 (substr_obj , & substr_len );
245+ if (!substr )
246+ return NULL ;
247+
248+ start = _fix_index (start , cstring_len (self ));
249+ end = _fix_index (end , cstring_len (self ));
250+
251+ char * p = strstr (& CSTRING_VALUE (self )[start ], substr );
252+ if (!p )
253+ return PyLong_FromLong (-1 );
254+
255+ Py_ssize_t result = p - CSTRING_VALUE (self );
256+ if (result + substr_len > end )
257+ return PyLong_FromLong (-1 );
258+
259+ return PyLong_FromSsize_t (result );
260+ }
261+
234262static PySequenceMethods cstring_as_sequence = {
235263 .sq_length = cstring_len ,
236264 .sq_concat = cstring_concat ,
@@ -246,6 +274,7 @@ static PyMappingMethods cstring_as_mapping = {
246274
247275static PyMethodDef cstring_methods [] = {
248276 {"count" , cstring_count , METH_VARARGS , count__doc__ },
277+ {"find" , cstring_find , METH_VARARGS , find__doc__ },
249278 {0 },
250279};
251280
0 commit comments