@@ -7,17 +7,18 @@ struct cstring {
77
88#define CSTRING_VALUE (self ) (((struct cstring *)self)->value)
99
10+ static PyObject * _cstring_new (PyTypeObject * type , const char * value , int size ) {
11+ struct cstring * new = type -> tp_alloc (type , size );
12+ memcpy (new -> value , value , size );
13+ return (PyObject * )new ;
14+ }
15+
1016static PyObject * cstring_new (PyTypeObject * type , PyObject * args , PyObject * * kwargs ) {
1117 char * value = NULL ;
12-
1318 if (!PyArg_ParseTuple (args , "s" , & value ))
1419 return NULL ;
15-
1620 int size = strlen (value ) + 1 ;
17-
18- struct cstring * new = type -> tp_alloc (type , size );
19- memcpy (new -> value , value , size );
20- return (PyObject * )new ;
21+ return _cstring_new (type , value , size );
2122}
2223
2324static void cstring_dealloc (PyObject * self ) {
@@ -58,9 +59,25 @@ static PyObject *cstring_concat(PyObject *left, PyObject *right) {
5859 return (PyObject * )new ;
5960}
6061
62+ static PyObject * cstring_repeat (PyObject * self , Py_ssize_t count ) {
63+ if (!_ensure_cstring (self ))
64+ return NULL ;
65+ if (count <= 0 )
66+ return _cstring_new (Py_TYPE (self ), "" , 1 );
67+
68+ Py_ssize_t size = (cstring_len (self ) * count ) + 1 ;
69+
70+ struct cstring * new = Py_TYPE (self )-> tp_alloc (Py_TYPE (self ), size );
71+ for (Py_ssize_t i = 0 ; i < size - 1 ; i += cstring_len (self )) {
72+ memcpy (& new -> value [i ], CSTRING_VALUE (self ), Py_SIZE (self ));
73+ }
74+ return (PyObject * )new ;
75+ }
76+
6177static PySequenceMethods cstring_as_sequence = {
6278 .sq_length = cstring_len ,
6379 .sq_concat = cstring_concat ,
80+ .sq_repeat = cstring_repeat ,
6481};
6582
6683static PyTypeObject cstring_type = {
0 commit comments