@@ -580,6 +580,33 @@ PyObject *cstring_startswith(PyObject *self, PyObject *args) {
580
580
return PyBool_FromLong (cmp == 0 );
581
581
}
582
582
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
+
583
610
PyDoc_STRVAR (endswith__doc__ , "" );
584
611
PyObject * cstring_endswith (PyObject * self , PyObject * args ) {
585
612
struct _substr_params params ;
@@ -680,7 +707,7 @@ static PyMethodDef cstring_methods[] = {
680
707
/* TODO: split */
681
708
/* TODO: splitlines */
682
709
{"startswith" , cstring_startswith , METH_VARARGS , startswith__doc__ },
683
- /* TODO: strip */
710
+ { "strip" , cstring_strip , METH_VARARGS , strip__doc__ },
684
711
{"swapcase" , cstring_swapcase , METH_NOARGS , swapcase__doc__ },
685
712
/* TODO: title */
686
713
/* TODO: translate */
0 commit comments