@@ -63,6 +63,7 @@ static PyObject *_cstring_copy(PyObject *self) {
63
63
}
64
64
65
65
static PyObject * cstring_new_empty (void ) {
66
+ /* TODO: empty cstring should be a singleton */
66
67
return _cstring_new (& cstring_type , "" , 0 );
67
68
}
68
69
@@ -538,6 +539,51 @@ PyObject *cstring_lower(PyObject *self, PyObject *args) {
538
539
return (PyObject * )new ;
539
540
}
540
541
542
+ static PyObject * _tuple_steal_refs (Py_ssize_t count , ...) {
543
+ PyObject * result = PyTuple_New (count );
544
+ if (!result )
545
+ return NULL ;
546
+
547
+ va_list va ;
548
+ va_start (va , count );
549
+ for (int i = 0 ; i < count ; ++ i ) {
550
+ PyObject * o = va_arg (va , PyObject * );
551
+ if (!o )
552
+ goto fail ;
553
+ PyTuple_SET_ITEM (result , i , o );
554
+ }
555
+ va_end (va );
556
+
557
+ return result ;
558
+
559
+ fail :
560
+ Py_DECREF (result );
561
+ return NULL ;
562
+ }
563
+
564
+ PyDoc_STRVAR (partition__doc__ , "" );
565
+ PyObject * cstring_partition (PyObject * self , PyObject * arg ) {
566
+ if (!_ensure_cstring (arg ))
567
+ return NULL ;
568
+
569
+ const char * search = CSTRING_VALUE (arg );
570
+
571
+ const char * left = CSTRING_VALUE (self );
572
+ const char * mid = strstr (left , search );
573
+ if (!mid ) {
574
+ return _tuple_steal_refs (3 ,
575
+ (Py_INCREF (self ), self ),
576
+ cstring_new_empty (),
577
+ cstring_new_empty ());
578
+ }
579
+ const char * right = mid + strlen (search );
580
+
581
+ return _tuple_steal_refs (3 ,
582
+ _cstring_new (Py_TYPE (self ), left , mid - left ),
583
+ _cstring_new (Py_TYPE (self ), mid , right - mid ),
584
+ _cstring_new (Py_TYPE (self ), right , & CSTRING_LAST_BYTE (self ) - right ));
585
+ }
586
+
541
587
PyDoc_STRVAR (rfind__doc__ , "" );
542
588
PyObject * cstring_rfind (PyObject * self , PyObject * args ) {
543
589
struct _substr_params params ;
@@ -726,7 +772,7 @@ static PyMethodDef cstring_methods[] = {
726
772
{"lower" , cstring_lower , METH_NOARGS , lower__doc__ },
727
773
{"lstrip" , cstring_lstrip , METH_VARARGS , lstrip__doc__ },
728
774
/* TODO: maketrans */
729
- /* TODO: partition */
775
+ { "partition" , cstring_partition , METH_O , partition__doc__ },
730
776
/* TODO: removeprefix */
731
777
/* TODO: replace */
732
778
{"rfind" , cstring_rfind , METH_VARARGS , rfind__doc__ },
0 commit comments