@@ -580,6 +580,33 @@ PyObject *cstring_startswith(PyObject *self, PyObject *args) {
580580 return PyBool_FromLong (cmp == 0 );
581581}
582582
583+ PyDoc_STRVAR (strip__doc__ , "" );
584+ PyObject * cstring_strip (PyObject * self , PyObject * args ) {
585+ PyObject * charsobj = NULL ;
586+ if (!PyArg_ParseTuple (args , "|O" , & charsobj ))
587+ return NULL ;
588+
589+ const char * chars = " \t\n\v\f\r" ;
590+
591+ if (charsobj && charsobj != Py_None ) {
592+ if (!PyUnicode_Check (charsobj ))
593+ return _bad_argument_type (charsobj );
594+ chars = PyUnicode_AsUTF8 (charsobj );
595+ }
596+
597+ const char * start = CSTRING_VALUE (self );
598+ while (strchr (chars , * start ))
599+ ++ start ;
600+
601+ const char * end = & CSTRING_LAST_BYTE (self ) - 1 ;
602+ while (strchr (chars , * end ))
603+ -- end ;
604+
605+ Py_ssize_t newsize = end - start + 1 ;
606+
607+ return _cstring_new (Py_TYPE (self ), start , newsize );
608+ }
609+
583610PyDoc_STRVAR (endswith__doc__ , "" );
584611PyObject * cstring_endswith (PyObject * self , PyObject * args ) {
585612 struct _substr_params params ;
@@ -680,7 +707,7 @@ static PyMethodDef cstring_methods[] = {
680707 /* TODO: split */
681708 /* TODO: splitlines */
682709 {"startswith" , cstring_startswith , METH_VARARGS , startswith__doc__ },
683- /* TODO: strip */
710+ { "strip" , cstring_strip , METH_VARARGS , strip__doc__ },
684711 {"swapcase" , cstring_swapcase , METH_NOARGS , swapcase__doc__ },
685712 /* TODO: title */
686713 /* TODO: translate */
0 commit comments