@@ -7,17 +7,18 @@ struct cstring {
7
7
8
8
#define CSTRING_VALUE (self ) (((struct cstring *)self)->value)
9
9
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
+
10
16
static PyObject * cstring_new (PyTypeObject * type , PyObject * args , PyObject * * kwargs ) {
11
17
char * value = NULL ;
12
-
13
18
if (!PyArg_ParseTuple (args , "s" , & value ))
14
19
return NULL ;
15
-
16
20
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 );
21
22
}
22
23
23
24
static void cstring_dealloc (PyObject * self ) {
@@ -58,9 +59,25 @@ static PyObject *cstring_concat(PyObject *left, PyObject *right) {
58
59
return (PyObject * )new ;
59
60
}
60
61
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
+
61
77
static PySequenceMethods cstring_as_sequence = {
62
78
.sq_length = cstring_len ,
63
79
.sq_concat = cstring_concat ,
80
+ .sq_repeat = cstring_repeat ,
64
81
};
65
82
66
83
static PyTypeObject cstring_type = {
0 commit comments