@@ -262,11 +262,34 @@ PyObject *cstring_find(PyObject *self, PyObject *args) {
262262
263263 char * p = strstr (params .start , params .substr );
264264 if (!p )
265- return PyLong_FromLong ( -1 ) ;
265+ goto err ;
266266 if (p + params .substr_len > params .end )
267- return PyLong_FromLong ( -1 ) ;
267+ goto err ;
268268
269269 return PyLong_FromSsize_t (p - CSTRING_VALUE (self ));
270+
271+ err :
272+ return PyLong_FromLong (-1 );
273+ }
274+
275+ PyDoc_STRVAR (index__doc__ , "" );
276+ PyObject * cstring_index (PyObject * self , PyObject * args ) {
277+ struct _substr_params params ;
278+
279+ if (!_parse_substr_args (self , args , & params ))
280+ return NULL ;
281+
282+ char * p = strstr (params .start , params .substr );
283+ if (!p )
284+ goto err ;
285+ if (p + params .substr_len > params .end )
286+ goto err ;
287+
288+ return PyLong_FromSsize_t (p - CSTRING_VALUE (self ));
289+
290+ err :
291+ PyErr_SetString (PyExc_ValueError , "substring not found" );
292+ return NULL ;
270293}
271294
272295static PySequenceMethods cstring_as_sequence = {
@@ -285,6 +308,7 @@ static PyMappingMethods cstring_as_mapping = {
285308static PyMethodDef cstring_methods [] = {
286309 {"count" , cstring_count , METH_VARARGS , count__doc__ },
287310 {"find" , cstring_find , METH_VARARGS , find__doc__ },
311+ {"index" , cstring_index , METH_VARARGS , index__doc__ },
288312 {0 },
289313};
290314
0 commit comments