Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1 | /* PyByteArray (bytearray) implementation */ |
| 2 | |
| 3 | #define PY_SSIZE_T_CLEAN |
| 4 | #include "Python.h" |
| 5 | #include "structmember.h" |
| 6 | #include "bytes_methods.h" |
| 7 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 8 | /*[clinic input] |
| 9 | class bytearray "PyByteArrayObject *" "&PyByteArray_Type" |
| 10 | [clinic start generated code]*/ |
| 11 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/ |
| 12 | |
Antoine Pitrou | fc8d6f4 | 2010-01-17 12:38:54 +0000 | [diff] [blame] | 13 | char _PyByteArray_empty_string[] = ""; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 14 | |
| 15 | void |
| 16 | PyByteArray_Fini(void) |
| 17 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | int |
| 21 | PyByteArray_Init(void) |
| 22 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 23 | return 1; |
| 24 | } |
| 25 | |
| 26 | /* end nullbytes support */ |
| 27 | |
| 28 | /* Helpers */ |
| 29 | |
| 30 | static int |
| 31 | _getbytevalue(PyObject* arg, int *value) |
| 32 | { |
| 33 | long face_value; |
| 34 | |
| 35 | if (PyLong_Check(arg)) { |
| 36 | face_value = PyLong_AsLong(arg); |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 37 | } else { |
| 38 | PyObject *index = PyNumber_Index(arg); |
| 39 | if (index == NULL) { |
| 40 | PyErr_Format(PyExc_TypeError, "an integer is required"); |
Mark Dickinson | 10de93a | 2010-07-09 19:25:48 +0000 | [diff] [blame] | 41 | *value = -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 42 | return 0; |
| 43 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 44 | face_value = PyLong_AsLong(index); |
| 45 | Py_DECREF(index); |
| 46 | } |
| 47 | |
| 48 | if (face_value < 0 || face_value >= 256) { |
| 49 | /* this includes the OverflowError in case the long is too large */ |
| 50 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
Mark Dickinson | 10de93a | 2010-07-09 19:25:48 +0000 | [diff] [blame] | 51 | *value = -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | *value = face_value; |
| 56 | return 1; |
| 57 | } |
| 58 | |
| 59 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 60 | bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 61 | { |
| 62 | int ret; |
| 63 | void *ptr; |
| 64 | if (view == NULL) { |
| 65 | obj->ob_exports++; |
| 66 | return 0; |
| 67 | } |
Antoine Pitrou | fc8d6f4 | 2010-01-17 12:38:54 +0000 | [diff] [blame] | 68 | ptr = (void *) PyByteArray_AS_STRING(obj); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 69 | ret = PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 70 | if (ret >= 0) { |
| 71 | obj->ob_exports++; |
| 72 | } |
| 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 77 | bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 78 | { |
| 79 | obj->ob_exports--; |
| 80 | } |
| 81 | |
| 82 | static Py_ssize_t |
| 83 | _getbuffer(PyObject *obj, Py_buffer *view) |
| 84 | { |
| 85 | PyBufferProcs *buffer = Py_TYPE(obj)->tp_as_buffer; |
| 86 | |
| 87 | if (buffer == NULL || buffer->bf_getbuffer == NULL) |
| 88 | { |
| 89 | PyErr_Format(PyExc_TypeError, |
R David Murray | 861470c | 2014-10-05 11:47:01 -0400 | [diff] [blame] | 90 | "a bytes-like object is required, not '%.100s'", |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 91 | Py_TYPE(obj)->tp_name); |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0) |
| 96 | return -1; |
| 97 | return view->len; |
| 98 | } |
| 99 | |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 100 | static int |
| 101 | _canresize(PyByteArrayObject *self) |
| 102 | { |
| 103 | if (self->ob_exports > 0) { |
| 104 | PyErr_SetString(PyExc_BufferError, |
| 105 | "Existing exports of data: object cannot be re-sized"); |
| 106 | return 0; |
| 107 | } |
| 108 | return 1; |
| 109 | } |
| 110 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 111 | /* Direct API functions */ |
| 112 | |
| 113 | PyObject * |
| 114 | PyByteArray_FromObject(PyObject *input) |
| 115 | { |
| 116 | return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type, |
| 117 | input, NULL); |
| 118 | } |
| 119 | |
| 120 | PyObject * |
| 121 | PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size) |
| 122 | { |
| 123 | PyByteArrayObject *new; |
| 124 | Py_ssize_t alloc; |
| 125 | |
| 126 | if (size < 0) { |
| 127 | PyErr_SetString(PyExc_SystemError, |
| 128 | "Negative size passed to PyByteArray_FromStringAndSize"); |
| 129 | return NULL; |
| 130 | } |
| 131 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 132 | /* Prevent buffer overflow when setting alloc to size+1. */ |
| 133 | if (size == PY_SSIZE_T_MAX) { |
| 134 | return PyErr_NoMemory(); |
| 135 | } |
| 136 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 137 | new = PyObject_New(PyByteArrayObject, &PyByteArray_Type); |
| 138 | if (new == NULL) |
| 139 | return NULL; |
| 140 | |
| 141 | if (size == 0) { |
| 142 | new->ob_bytes = NULL; |
| 143 | alloc = 0; |
| 144 | } |
| 145 | else { |
| 146 | alloc = size + 1; |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 147 | new->ob_bytes = PyObject_Malloc(alloc); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 148 | if (new->ob_bytes == NULL) { |
| 149 | Py_DECREF(new); |
| 150 | return PyErr_NoMemory(); |
| 151 | } |
Antoine Pitrou | fc8d6f4 | 2010-01-17 12:38:54 +0000 | [diff] [blame] | 152 | if (bytes != NULL && size > 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 153 | memcpy(new->ob_bytes, bytes, size); |
| 154 | new->ob_bytes[size] = '\0'; /* Trailing null byte */ |
| 155 | } |
| 156 | Py_SIZE(new) = size; |
| 157 | new->ob_alloc = alloc; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 158 | new->ob_start = new->ob_bytes; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 159 | new->ob_exports = 0; |
| 160 | |
| 161 | return (PyObject *)new; |
| 162 | } |
| 163 | |
| 164 | Py_ssize_t |
| 165 | PyByteArray_Size(PyObject *self) |
| 166 | { |
| 167 | assert(self != NULL); |
| 168 | assert(PyByteArray_Check(self)); |
| 169 | |
| 170 | return PyByteArray_GET_SIZE(self); |
| 171 | } |
| 172 | |
| 173 | char * |
| 174 | PyByteArray_AsString(PyObject *self) |
| 175 | { |
| 176 | assert(self != NULL); |
| 177 | assert(PyByteArray_Check(self)); |
| 178 | |
| 179 | return PyByteArray_AS_STRING(self); |
| 180 | } |
| 181 | |
| 182 | int |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 183 | PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 184 | { |
| 185 | void *sval; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 186 | PyByteArrayObject *obj = ((PyByteArrayObject *)self); |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 187 | /* All computations are done unsigned to avoid integer overflows |
| 188 | (see issue #22335). */ |
| 189 | size_t alloc = (size_t) obj->ob_alloc; |
| 190 | size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes); |
| 191 | size_t size = (size_t) requested_size; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 192 | |
| 193 | assert(self != NULL); |
| 194 | assert(PyByteArray_Check(self)); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 195 | assert(logical_offset <= alloc); |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 196 | assert(requested_size >= 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 197 | |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 198 | if (requested_size == Py_SIZE(self)) { |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 199 | return 0; |
| 200 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 201 | if (!_canresize(obj)) { |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 202 | return -1; |
| 203 | } |
| 204 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 205 | if (size + logical_offset + 1 < alloc) { |
| 206 | /* Current buffer is large enough to host the requested size, |
| 207 | decide on a strategy. */ |
| 208 | if (size < alloc / 2) { |
| 209 | /* Major downsize; resize down to exact size */ |
| 210 | alloc = size + 1; |
| 211 | } |
| 212 | else { |
| 213 | /* Minor downsize; quick exit */ |
| 214 | Py_SIZE(self) = size; |
| 215 | PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */ |
| 216 | return 0; |
| 217 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 218 | } |
| 219 | else { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 220 | /* Need growing, decide on a strategy */ |
| 221 | if (size <= alloc * 1.125) { |
| 222 | /* Moderate upsize; overallocate similar to list_resize() */ |
| 223 | alloc = size + (size >> 3) + (size < 9 ? 3 : 6); |
| 224 | } |
| 225 | else { |
| 226 | /* Major upsize; resize up to exact size */ |
| 227 | alloc = size + 1; |
| 228 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 229 | } |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 230 | if (alloc > PY_SSIZE_T_MAX) { |
| 231 | PyErr_NoMemory(); |
| 232 | return -1; |
| 233 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 234 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 235 | if (logical_offset > 0) { |
| 236 | sval = PyObject_Malloc(alloc); |
| 237 | if (sval == NULL) { |
| 238 | PyErr_NoMemory(); |
| 239 | return -1; |
| 240 | } |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 241 | memcpy(sval, PyByteArray_AS_STRING(self), |
| 242 | Py_MIN(requested_size, Py_SIZE(self))); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 243 | PyObject_Free(obj->ob_bytes); |
| 244 | } |
| 245 | else { |
| 246 | sval = PyObject_Realloc(obj->ob_bytes, alloc); |
| 247 | if (sval == NULL) { |
| 248 | PyErr_NoMemory(); |
| 249 | return -1; |
| 250 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 253 | obj->ob_bytes = obj->ob_start = sval; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 254 | Py_SIZE(self) = size; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 255 | obj->ob_alloc = alloc; |
| 256 | obj->ob_bytes[size] = '\0'; /* Trailing null byte */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | PyObject * |
| 262 | PyByteArray_Concat(PyObject *a, PyObject *b) |
| 263 | { |
| 264 | Py_ssize_t size; |
| 265 | Py_buffer va, vb; |
| 266 | PyByteArrayObject *result = NULL; |
| 267 | |
| 268 | va.len = -1; |
| 269 | vb.len = -1; |
| 270 | if (_getbuffer(a, &va) < 0 || |
| 271 | _getbuffer(b, &vb) < 0) { |
| 272 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 273 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 274 | goto done; |
| 275 | } |
| 276 | |
| 277 | size = va.len + vb.len; |
| 278 | if (size < 0) { |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 279 | PyErr_NoMemory(); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 280 | goto done; |
| 281 | } |
| 282 | |
| 283 | result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, size); |
| 284 | if (result != NULL) { |
| 285 | memcpy(result->ob_bytes, va.buf, va.len); |
| 286 | memcpy(result->ob_bytes + va.len, vb.buf, vb.len); |
| 287 | } |
| 288 | |
| 289 | done: |
| 290 | if (va.len != -1) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 291 | PyBuffer_Release(&va); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 292 | if (vb.len != -1) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 293 | PyBuffer_Release(&vb); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 294 | return (PyObject *)result; |
| 295 | } |
| 296 | |
| 297 | /* Functions stuffed into the type object */ |
| 298 | |
| 299 | static Py_ssize_t |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 300 | bytearray_length(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 301 | { |
| 302 | return Py_SIZE(self); |
| 303 | } |
| 304 | |
| 305 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 306 | bytearray_iconcat(PyByteArrayObject *self, PyObject *other) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 307 | { |
| 308 | Py_ssize_t mysize; |
| 309 | Py_ssize_t size; |
| 310 | Py_buffer vo; |
| 311 | |
| 312 | if (_getbuffer(other, &vo) < 0) { |
| 313 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 314 | Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name); |
| 315 | return NULL; |
| 316 | } |
| 317 | |
| 318 | mysize = Py_SIZE(self); |
| 319 | size = mysize + vo.len; |
| 320 | if (size < 0) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 321 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 322 | return PyErr_NoMemory(); |
| 323 | } |
| 324 | if (size < self->ob_alloc) { |
| 325 | Py_SIZE(self) = size; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 326 | PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0'; /* Trailing null byte */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 327 | } |
| 328 | else if (PyByteArray_Resize((PyObject *)self, size) < 0) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 329 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 330 | return NULL; |
| 331 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 332 | memcpy(PyByteArray_AS_STRING(self) + mysize, vo.buf, vo.len); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 333 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 334 | Py_INCREF(self); |
| 335 | return (PyObject *)self; |
| 336 | } |
| 337 | |
| 338 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 339 | bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 340 | { |
| 341 | PyByteArrayObject *result; |
| 342 | Py_ssize_t mysize; |
| 343 | Py_ssize_t size; |
| 344 | |
| 345 | if (count < 0) |
| 346 | count = 0; |
| 347 | mysize = Py_SIZE(self); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 348 | if (count > 0 && mysize > PY_SSIZE_T_MAX / count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 349 | return PyErr_NoMemory(); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 350 | size = mysize * count; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 351 | result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size); |
| 352 | if (result != NULL && size != 0) { |
| 353 | if (mysize == 1) |
| 354 | memset(result->ob_bytes, self->ob_bytes[0], size); |
| 355 | else { |
| 356 | Py_ssize_t i; |
| 357 | for (i = 0; i < count; i++) |
| 358 | memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize); |
| 359 | } |
| 360 | } |
| 361 | return (PyObject *)result; |
| 362 | } |
| 363 | |
| 364 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 365 | bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 366 | { |
| 367 | Py_ssize_t mysize; |
| 368 | Py_ssize_t size; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 369 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 370 | |
| 371 | if (count < 0) |
| 372 | count = 0; |
| 373 | mysize = Py_SIZE(self); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 374 | if (count > 0 && mysize > PY_SSIZE_T_MAX / count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 375 | return PyErr_NoMemory(); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 376 | size = mysize * count; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 377 | if (PyByteArray_Resize((PyObject *)self, size) < 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 378 | return NULL; |
| 379 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 380 | buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 381 | if (mysize == 1) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 382 | memset(buf, buf[0], size); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 383 | else { |
| 384 | Py_ssize_t i; |
| 385 | for (i = 1; i < count; i++) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 386 | memcpy(buf + i*mysize, buf, mysize); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | Py_INCREF(self); |
| 390 | return (PyObject *)self; |
| 391 | } |
| 392 | |
| 393 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 394 | bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 395 | { |
| 396 | if (i < 0) |
| 397 | i += Py_SIZE(self); |
| 398 | if (i < 0 || i >= Py_SIZE(self)) { |
| 399 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 400 | return NULL; |
| 401 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 402 | return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i])); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 406 | bytearray_subscript(PyByteArrayObject *self, PyObject *index) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 407 | { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 408 | if (PyIndex_Check(index)) { |
| 409 | Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 410 | |
| 411 | if (i == -1 && PyErr_Occurred()) |
| 412 | return NULL; |
| 413 | |
| 414 | if (i < 0) |
| 415 | i += PyByteArray_GET_SIZE(self); |
| 416 | |
| 417 | if (i < 0 || i >= Py_SIZE(self)) { |
| 418 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 419 | return NULL; |
| 420 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 421 | return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i])); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 422 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 423 | else if (PySlice_Check(index)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 424 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 425 | if (PySlice_GetIndicesEx(index, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 426 | PyByteArray_GET_SIZE(self), |
| 427 | &start, &stop, &step, &slicelength) < 0) { |
| 428 | return NULL; |
| 429 | } |
| 430 | |
| 431 | if (slicelength <= 0) |
| 432 | return PyByteArray_FromStringAndSize("", 0); |
| 433 | else if (step == 1) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 434 | return PyByteArray_FromStringAndSize( |
| 435 | PyByteArray_AS_STRING(self) + start, slicelength); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 436 | } |
| 437 | else { |
| 438 | char *source_buf = PyByteArray_AS_STRING(self); |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 439 | char *result_buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 440 | PyObject *result; |
| 441 | |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 442 | result = PyByteArray_FromStringAndSize(NULL, slicelength); |
| 443 | if (result == NULL) |
| 444 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 445 | |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 446 | result_buf = PyByteArray_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 447 | for (cur = start, i = 0; i < slicelength; |
| 448 | cur += step, i++) { |
| 449 | result_buf[i] = source_buf[cur]; |
| 450 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 451 | return result; |
| 452 | } |
| 453 | } |
| 454 | else { |
Terry Jan Reedy | ffff144 | 2014-08-02 01:30:37 -0400 | [diff] [blame] | 455 | PyErr_Format(PyExc_TypeError, |
| 456 | "bytearray indices must be integers or slices, not %.200s", |
| 457 | Py_TYPE(index)->tp_name); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 458 | return NULL; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | static int |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 463 | bytearray_setslice_linear(PyByteArrayObject *self, |
| 464 | Py_ssize_t lo, Py_ssize_t hi, |
| 465 | char *bytes, Py_ssize_t bytes_len) |
| 466 | { |
| 467 | Py_ssize_t avail = hi - lo; |
| 468 | char *buf = PyByteArray_AS_STRING(self); |
| 469 | Py_ssize_t growth = bytes_len - avail; |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 470 | int res = 0; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 471 | assert(avail >= 0); |
| 472 | |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 473 | if (growth < 0) { |
| 474 | if (!_canresize(self)) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 475 | return -1; |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 476 | |
| 477 | if (lo == 0) { |
| 478 | /* Shrink the buffer by advancing its logical start */ |
| 479 | self->ob_start -= growth; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 480 | /* |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 481 | 0 lo hi old_size |
| 482 | | |<----avail----->|<-----tail------>| |
| 483 | | |<-bytes_len->|<-----tail------>| |
| 484 | 0 new_lo new_hi new_size |
| 485 | */ |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 486 | } |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 487 | else { |
| 488 | /* |
| 489 | 0 lo hi old_size |
| 490 | | |<----avail----->|<-----tomove------>| |
| 491 | | |<-bytes_len->|<-----tomove------>| |
| 492 | 0 lo new_hi new_size |
| 493 | */ |
| 494 | memmove(buf + lo + bytes_len, buf + hi, |
| 495 | Py_SIZE(self) - hi); |
| 496 | } |
| 497 | if (PyByteArray_Resize((PyObject *)self, |
| 498 | Py_SIZE(self) + growth) < 0) { |
| 499 | /* Issue #19578: Handling the memory allocation failure here is |
| 500 | tricky here because the bytearray object has already been |
| 501 | modified. Depending on growth and lo, the behaviour is |
| 502 | different. |
| 503 | |
| 504 | If growth < 0 and lo != 0, the operation is completed, but a |
| 505 | MemoryError is still raised and the memory block is not |
| 506 | shrinked. Otherwise, the bytearray is restored in its previous |
| 507 | state and a MemoryError is raised. */ |
| 508 | if (lo == 0) { |
| 509 | self->ob_start += growth; |
| 510 | return -1; |
| 511 | } |
| 512 | /* memmove() removed bytes, the bytearray object cannot be |
| 513 | restored in its previous state. */ |
| 514 | Py_SIZE(self) += growth; |
| 515 | res = -1; |
| 516 | } |
| 517 | buf = PyByteArray_AS_STRING(self); |
| 518 | } |
| 519 | else if (growth > 0) { |
| 520 | if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) { |
| 521 | PyErr_NoMemory(); |
| 522 | return -1; |
| 523 | } |
| 524 | |
| 525 | if (PyByteArray_Resize((PyObject *)self, |
| 526 | Py_SIZE(self) + growth) < 0) { |
| 527 | return -1; |
| 528 | } |
| 529 | buf = PyByteArray_AS_STRING(self); |
| 530 | /* Make the place for the additional bytes */ |
| 531 | /* |
| 532 | 0 lo hi old_size |
| 533 | | |<-avail->|<-----tomove------>| |
| 534 | | |<---bytes_len-->|<-----tomove------>| |
| 535 | 0 lo new_hi new_size |
| 536 | */ |
| 537 | memmove(buf + lo + bytes_len, buf + hi, |
| 538 | Py_SIZE(self) - lo - bytes_len); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | if (bytes_len > 0) |
| 542 | memcpy(buf + lo, bytes, bytes_len); |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 543 | return res; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 547 | bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 548 | PyObject *values) |
| 549 | { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 550 | Py_ssize_t needed; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 551 | void *bytes; |
| 552 | Py_buffer vbytes; |
| 553 | int res = 0; |
| 554 | |
| 555 | vbytes.len = -1; |
| 556 | if (values == (PyObject *)self) { |
| 557 | /* Make a copy and call this function recursively */ |
| 558 | int err; |
| 559 | values = PyByteArray_FromObject(values); |
| 560 | if (values == NULL) |
| 561 | return -1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 562 | err = bytearray_setslice(self, lo, hi, values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 563 | Py_DECREF(values); |
| 564 | return err; |
| 565 | } |
| 566 | if (values == NULL) { |
| 567 | /* del b[lo:hi] */ |
| 568 | bytes = NULL; |
| 569 | needed = 0; |
| 570 | } |
| 571 | else { |
| 572 | if (_getbuffer(values, &vbytes) < 0) { |
| 573 | PyErr_Format(PyExc_TypeError, |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 574 | "can't set bytearray slice from %.100s", |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 575 | Py_TYPE(values)->tp_name); |
| 576 | return -1; |
| 577 | } |
| 578 | needed = vbytes.len; |
| 579 | bytes = vbytes.buf; |
| 580 | } |
| 581 | |
| 582 | if (lo < 0) |
| 583 | lo = 0; |
| 584 | if (hi < lo) |
| 585 | hi = lo; |
| 586 | if (hi > Py_SIZE(self)) |
| 587 | hi = Py_SIZE(self); |
| 588 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 589 | res = bytearray_setslice_linear(self, lo, hi, bytes, needed); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 590 | if (vbytes.len != -1) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 591 | PyBuffer_Release(&vbytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 592 | return res; |
| 593 | } |
| 594 | |
| 595 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 596 | bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 597 | { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 598 | int ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 599 | |
| 600 | if (i < 0) |
| 601 | i += Py_SIZE(self); |
| 602 | |
| 603 | if (i < 0 || i >= Py_SIZE(self)) { |
| 604 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 605 | return -1; |
| 606 | } |
| 607 | |
| 608 | if (value == NULL) |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 609 | return bytearray_setslice(self, i, i+1, NULL); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 610 | |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 611 | if (!_getbytevalue(value, &ival)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 612 | return -1; |
| 613 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 614 | PyByteArray_AS_STRING(self)[i] = ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 619 | bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 620 | { |
| 621 | Py_ssize_t start, stop, step, slicelen, needed; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 622 | char *buf, *bytes; |
| 623 | buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 624 | |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 625 | if (PyIndex_Check(index)) { |
| 626 | Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 627 | |
| 628 | if (i == -1 && PyErr_Occurred()) |
| 629 | return -1; |
| 630 | |
| 631 | if (i < 0) |
| 632 | i += PyByteArray_GET_SIZE(self); |
| 633 | |
| 634 | if (i < 0 || i >= Py_SIZE(self)) { |
| 635 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 636 | return -1; |
| 637 | } |
| 638 | |
| 639 | if (values == NULL) { |
| 640 | /* Fall through to slice assignment */ |
| 641 | start = i; |
| 642 | stop = i + 1; |
| 643 | step = 1; |
| 644 | slicelen = 1; |
| 645 | } |
| 646 | else { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 647 | int ival; |
| 648 | if (!_getbytevalue(values, &ival)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 649 | return -1; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 650 | buf[i] = (char)ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 651 | return 0; |
| 652 | } |
| 653 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 654 | else if (PySlice_Check(index)) { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 655 | if (PySlice_GetIndicesEx(index, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 656 | PyByteArray_GET_SIZE(self), |
| 657 | &start, &stop, &step, &slicelen) < 0) { |
| 658 | return -1; |
| 659 | } |
| 660 | } |
| 661 | else { |
Terry Jan Reedy | ffff144 | 2014-08-02 01:30:37 -0400 | [diff] [blame] | 662 | PyErr_Format(PyExc_TypeError, |
| 663 | "bytearray indices must be integers or slices, not %.200s", |
| 664 | Py_TYPE(index)->tp_name); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 665 | return -1; |
| 666 | } |
| 667 | |
| 668 | if (values == NULL) { |
| 669 | bytes = NULL; |
| 670 | needed = 0; |
| 671 | } |
| 672 | else if (values == (PyObject *)self || !PyByteArray_Check(values)) { |
Christian Heimes | 6d26ade | 2012-11-03 23:07:59 +0100 | [diff] [blame] | 673 | int err; |
Ezio Melotti | c64bcbe | 2012-11-03 21:19:06 +0200 | [diff] [blame] | 674 | if (PyNumber_Check(values) || PyUnicode_Check(values)) { |
| 675 | PyErr_SetString(PyExc_TypeError, |
| 676 | "can assign only bytes, buffers, or iterables " |
| 677 | "of ints in range(0, 256)"); |
| 678 | return -1; |
| 679 | } |
Georg Brandl | f3fa568 | 2010-12-04 17:09:30 +0000 | [diff] [blame] | 680 | /* Make a copy and call this function recursively */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 681 | values = PyByteArray_FromObject(values); |
| 682 | if (values == NULL) |
| 683 | return -1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 684 | err = bytearray_ass_subscript(self, index, values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 685 | Py_DECREF(values); |
| 686 | return err; |
| 687 | } |
| 688 | else { |
| 689 | assert(PyByteArray_Check(values)); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 690 | bytes = PyByteArray_AS_STRING(values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 691 | needed = Py_SIZE(values); |
| 692 | } |
| 693 | /* Make sure b[5:2] = ... inserts before 5, not before 2. */ |
| 694 | if ((step < 0 && start < stop) || |
| 695 | (step > 0 && start > stop)) |
| 696 | stop = start; |
| 697 | if (step == 1) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 698 | return bytearray_setslice_linear(self, start, stop, bytes, needed); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 699 | } |
| 700 | else { |
| 701 | if (needed == 0) { |
| 702 | /* Delete slice */ |
Mark Dickinson | bc09964 | 2010-01-29 17:27:24 +0000 | [diff] [blame] | 703 | size_t cur; |
| 704 | Py_ssize_t i; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 705 | |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 706 | if (!_canresize(self)) |
| 707 | return -1; |
Mark Dickinson | 7e3b948 | 2010-08-06 21:33:18 +0000 | [diff] [blame] | 708 | |
| 709 | if (slicelen == 0) |
| 710 | /* Nothing to do here. */ |
| 711 | return 0; |
| 712 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 713 | if (step < 0) { |
| 714 | stop = start + 1; |
| 715 | start = stop + step * (slicelen - 1) - 1; |
| 716 | step = -step; |
| 717 | } |
| 718 | for (cur = start, i = 0; |
| 719 | i < slicelen; cur += step, i++) { |
| 720 | Py_ssize_t lim = step - 1; |
| 721 | |
Mark Dickinson | 66f575b | 2010-02-14 12:53:32 +0000 | [diff] [blame] | 722 | if (cur + step >= (size_t)PyByteArray_GET_SIZE(self)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 723 | lim = PyByteArray_GET_SIZE(self) - cur - 1; |
| 724 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 725 | memmove(buf + cur - i, |
| 726 | buf + cur + 1, lim); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 727 | } |
| 728 | /* Move the tail of the bytes, in one chunk */ |
Mark Dickinson | 7e3b948 | 2010-08-06 21:33:18 +0000 | [diff] [blame] | 729 | cur = start + (size_t)slicelen*step; |
Mark Dickinson | 66f575b | 2010-02-14 12:53:32 +0000 | [diff] [blame] | 730 | if (cur < (size_t)PyByteArray_GET_SIZE(self)) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 731 | memmove(buf + cur - slicelen, |
| 732 | buf + cur, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 733 | PyByteArray_GET_SIZE(self) - cur); |
| 734 | } |
| 735 | if (PyByteArray_Resize((PyObject *)self, |
| 736 | PyByteArray_GET_SIZE(self) - slicelen) < 0) |
| 737 | return -1; |
| 738 | |
| 739 | return 0; |
| 740 | } |
| 741 | else { |
| 742 | /* Assign slice */ |
Mark Dickinson | 7e3b948 | 2010-08-06 21:33:18 +0000 | [diff] [blame] | 743 | Py_ssize_t i; |
| 744 | size_t cur; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 745 | |
| 746 | if (needed != slicelen) { |
| 747 | PyErr_Format(PyExc_ValueError, |
| 748 | "attempt to assign bytes of size %zd " |
| 749 | "to extended slice of size %zd", |
| 750 | needed, slicelen); |
| 751 | return -1; |
| 752 | } |
| 753 | for (cur = start, i = 0; i < slicelen; cur += step, i++) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 754 | buf[cur] = bytes[i]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 755 | return 0; |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 761 | bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 762 | { |
| 763 | static char *kwlist[] = {"source", "encoding", "errors", 0}; |
| 764 | PyObject *arg = NULL; |
| 765 | const char *encoding = NULL; |
| 766 | const char *errors = NULL; |
| 767 | Py_ssize_t count; |
| 768 | PyObject *it; |
| 769 | PyObject *(*iternext)(PyObject *); |
| 770 | |
| 771 | if (Py_SIZE(self) != 0) { |
| 772 | /* Empty previous contents (yes, do this first of all!) */ |
| 773 | if (PyByteArray_Resize((PyObject *)self, 0) < 0) |
| 774 | return -1; |
| 775 | } |
| 776 | |
| 777 | /* Parse arguments */ |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 778 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 779 | &arg, &encoding, &errors)) |
| 780 | return -1; |
| 781 | |
| 782 | /* Make a quick exit if no first argument */ |
| 783 | if (arg == NULL) { |
| 784 | if (encoding != NULL || errors != NULL) { |
| 785 | PyErr_SetString(PyExc_TypeError, |
| 786 | "encoding or errors without sequence argument"); |
| 787 | return -1; |
| 788 | } |
| 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | if (PyUnicode_Check(arg)) { |
| 793 | /* Encode via the codec registry */ |
| 794 | PyObject *encoded, *new; |
| 795 | if (encoding == NULL) { |
| 796 | PyErr_SetString(PyExc_TypeError, |
| 797 | "string argument without an encoding"); |
| 798 | return -1; |
| 799 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 800 | encoded = PyUnicode_AsEncodedString(arg, encoding, errors); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 801 | if (encoded == NULL) |
| 802 | return -1; |
| 803 | assert(PyBytes_Check(encoded)); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 804 | new = bytearray_iconcat(self, encoded); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 805 | Py_DECREF(encoded); |
| 806 | if (new == NULL) |
| 807 | return -1; |
| 808 | Py_DECREF(new); |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | /* If it's not unicode, there can't be encoding or errors */ |
| 813 | if (encoding != NULL || errors != NULL) { |
| 814 | PyErr_SetString(PyExc_TypeError, |
| 815 | "encoding or errors without a string argument"); |
| 816 | return -1; |
| 817 | } |
| 818 | |
| 819 | /* Is it an int? */ |
Benjamin Peterson | 8380dd5 | 2010-04-16 22:51:37 +0000 | [diff] [blame] | 820 | count = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
| 821 | if (count == -1 && PyErr_Occurred()) { |
| 822 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 823 | return -1; |
Benjamin Peterson | 9c0e94f | 2010-04-16 23:00:53 +0000 | [diff] [blame] | 824 | PyErr_Clear(); |
Benjamin Peterson | 8380dd5 | 2010-04-16 22:51:37 +0000 | [diff] [blame] | 825 | } |
| 826 | else if (count < 0) { |
| 827 | PyErr_SetString(PyExc_ValueError, "negative count"); |
| 828 | return -1; |
| 829 | } |
| 830 | else { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 831 | if (count > 0) { |
Victor Stinner | 2bc4d95 | 2014-06-02 22:22:42 +0200 | [diff] [blame] | 832 | if (PyByteArray_Resize((PyObject *)self, count)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 833 | return -1; |
Victor Stinner | 2bc4d95 | 2014-06-02 22:22:42 +0200 | [diff] [blame] | 834 | memset(PyByteArray_AS_STRING(self), 0, count); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 835 | } |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | /* Use the buffer API */ |
| 840 | if (PyObject_CheckBuffer(arg)) { |
| 841 | Py_ssize_t size; |
| 842 | Py_buffer view; |
| 843 | if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0) |
| 844 | return -1; |
| 845 | size = view.len; |
| 846 | if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 847 | if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self), |
| 848 | &view, size, 'C') < 0) |
Stefan Krah | 7d12d9d | 2012-07-28 12:25:55 +0200 | [diff] [blame] | 849 | goto fail; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 850 | PyBuffer_Release(&view); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 851 | return 0; |
| 852 | fail: |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 853 | PyBuffer_Release(&view); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 854 | return -1; |
| 855 | } |
| 856 | |
| 857 | /* XXX Optimize this if the arguments is a list, tuple */ |
| 858 | |
| 859 | /* Get the iterator */ |
| 860 | it = PyObject_GetIter(arg); |
| 861 | if (it == NULL) |
| 862 | return -1; |
| 863 | iternext = *Py_TYPE(it)->tp_iternext; |
| 864 | |
| 865 | /* Run the iterator to exhaustion */ |
| 866 | for (;;) { |
| 867 | PyObject *item; |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 868 | int rc, value; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 869 | |
| 870 | /* Get the next item */ |
| 871 | item = iternext(it); |
| 872 | if (item == NULL) { |
| 873 | if (PyErr_Occurred()) { |
| 874 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 875 | goto error; |
| 876 | PyErr_Clear(); |
| 877 | } |
| 878 | break; |
| 879 | } |
| 880 | |
| 881 | /* Interpret it as an int (__index__) */ |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 882 | rc = _getbytevalue(item, &value); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 883 | Py_DECREF(item); |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 884 | if (!rc) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 885 | goto error; |
| 886 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 887 | /* Append the byte */ |
| 888 | if (Py_SIZE(self) < self->ob_alloc) |
| 889 | Py_SIZE(self)++; |
| 890 | else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0) |
| 891 | goto error; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 892 | PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | /* Clean up and return success */ |
| 896 | Py_DECREF(it); |
| 897 | return 0; |
| 898 | |
| 899 | error: |
| 900 | /* Error handling when it != NULL */ |
| 901 | Py_DECREF(it); |
| 902 | return -1; |
| 903 | } |
| 904 | |
| 905 | /* Mostly copied from string_repr, but without the |
| 906 | "smart quote" functionality. */ |
| 907 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 908 | bytearray_repr(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 909 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 910 | const char *quote_prefix = "bytearray(b"; |
| 911 | const char *quote_postfix = ")"; |
| 912 | Py_ssize_t length = Py_SIZE(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 913 | /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */ |
Mark Dickinson | 66f575b | 2010-02-14 12:53:32 +0000 | [diff] [blame] | 914 | size_t newsize; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 915 | PyObject *v; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 916 | Py_ssize_t i; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 917 | char *bytes; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 918 | char c; |
| 919 | char *p; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 920 | int quote; |
| 921 | char *test, *start; |
| 922 | char *buffer; |
| 923 | |
| 924 | if (length > (PY_SSIZE_T_MAX - 15) / 4) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 925 | PyErr_SetString(PyExc_OverflowError, |
| 926 | "bytearray object is too large to make repr"); |
| 927 | return NULL; |
| 928 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 929 | |
| 930 | newsize = 15 + length * 4; |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 931 | buffer = PyObject_Malloc(newsize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 932 | if (buffer == NULL) { |
| 933 | PyErr_NoMemory(); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 934 | return NULL; |
| 935 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 936 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 937 | /* Figure out which quote to use; single is preferred */ |
| 938 | quote = '\''; |
| 939 | start = PyByteArray_AS_STRING(self); |
| 940 | for (test = start; test < start+length; ++test) { |
| 941 | if (*test == '"') { |
| 942 | quote = '\''; /* back to single */ |
| 943 | break; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 944 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 945 | else if (*test == '\'') |
| 946 | quote = '"'; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 947 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 948 | |
| 949 | p = buffer; |
| 950 | while (*quote_prefix) |
| 951 | *p++ = *quote_prefix++; |
| 952 | *p++ = quote; |
| 953 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 954 | bytes = PyByteArray_AS_STRING(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 955 | for (i = 0; i < length; i++) { |
| 956 | /* There's at least enough room for a hex escape |
| 957 | and a closing quote. */ |
| 958 | assert(newsize - (p - buffer) >= 5); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 959 | c = bytes[i]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 960 | if (c == '\'' || c == '\\') |
| 961 | *p++ = '\\', *p++ = c; |
| 962 | else if (c == '\t') |
| 963 | *p++ = '\\', *p++ = 't'; |
| 964 | else if (c == '\n') |
| 965 | *p++ = '\\', *p++ = 'n'; |
| 966 | else if (c == '\r') |
| 967 | *p++ = '\\', *p++ = 'r'; |
| 968 | else if (c == 0) |
| 969 | *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0'; |
| 970 | else if (c < ' ' || c >= 0x7f) { |
| 971 | *p++ = '\\'; |
| 972 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 973 | *p++ = Py_hexdigits[(c & 0xf0) >> 4]; |
| 974 | *p++ = Py_hexdigits[c & 0xf]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 975 | } |
| 976 | else |
| 977 | *p++ = c; |
| 978 | } |
| 979 | assert(newsize - (p - buffer) >= 1); |
| 980 | *p++ = quote; |
| 981 | while (*quote_postfix) { |
| 982 | *p++ = *quote_postfix++; |
| 983 | } |
| 984 | |
| 985 | v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL); |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 986 | PyObject_Free(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 987 | return v; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 991 | bytearray_str(PyObject *op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 992 | { |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 993 | if (Py_BytesWarningFlag) { |
| 994 | if (PyErr_WarnEx(PyExc_BytesWarning, |
| 995 | "str() on a bytearray instance", 1)) |
| 996 | return NULL; |
| 997 | } |
| 998 | return bytearray_repr((PyByteArrayObject*)op); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1002 | bytearray_richcompare(PyObject *self, PyObject *other, int op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1003 | { |
| 1004 | Py_ssize_t self_size, other_size; |
| 1005 | Py_buffer self_bytes, other_bytes; |
| 1006 | PyObject *res; |
| 1007 | Py_ssize_t minsize; |
| 1008 | int cmp; |
| 1009 | |
| 1010 | /* Bytes can be compared to anything that supports the (binary) |
| 1011 | buffer API. Except that a comparison with Unicode is always an |
| 1012 | error, even if the comparison is for equality. */ |
| 1013 | if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) || |
| 1014 | PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) { |
Barry Warsaw | 9e9dcd6 | 2008-10-17 01:50:37 +0000 | [diff] [blame] | 1015 | if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1016 | if (PyErr_WarnEx(PyExc_BytesWarning, |
Georg Brandl | e5d68ac | 2008-06-04 11:30:26 +0000 | [diff] [blame] | 1017 | "Comparison between bytearray and string", 1)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1018 | return NULL; |
| 1019 | } |
| 1020 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1021 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | self_size = _getbuffer(self, &self_bytes); |
| 1025 | if (self_size < 0) { |
| 1026 | PyErr_Clear(); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1027 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | other_size = _getbuffer(other, &other_bytes); |
| 1031 | if (other_size < 0) { |
| 1032 | PyErr_Clear(); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1033 | PyBuffer_Release(&self_bytes); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1034 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | if (self_size != other_size && (op == Py_EQ || op == Py_NE)) { |
| 1038 | /* Shortcut: if the lengths differ, the objects differ */ |
| 1039 | cmp = (op == Py_NE); |
| 1040 | } |
| 1041 | else { |
| 1042 | minsize = self_size; |
| 1043 | if (other_size < minsize) |
| 1044 | minsize = other_size; |
| 1045 | |
| 1046 | cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize); |
| 1047 | /* In ISO C, memcmp() guarantees to use unsigned bytes! */ |
| 1048 | |
| 1049 | if (cmp == 0) { |
| 1050 | if (self_size < other_size) |
| 1051 | cmp = -1; |
| 1052 | else if (self_size > other_size) |
| 1053 | cmp = 1; |
| 1054 | } |
| 1055 | |
| 1056 | switch (op) { |
| 1057 | case Py_LT: cmp = cmp < 0; break; |
| 1058 | case Py_LE: cmp = cmp <= 0; break; |
| 1059 | case Py_EQ: cmp = cmp == 0; break; |
| 1060 | case Py_NE: cmp = cmp != 0; break; |
| 1061 | case Py_GT: cmp = cmp > 0; break; |
| 1062 | case Py_GE: cmp = cmp >= 0; break; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | res = cmp ? Py_True : Py_False; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1067 | PyBuffer_Release(&self_bytes); |
| 1068 | PyBuffer_Release(&other_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1069 | Py_INCREF(res); |
| 1070 | return res; |
| 1071 | } |
| 1072 | |
| 1073 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1074 | bytearray_dealloc(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1075 | { |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 1076 | if (self->ob_exports > 0) { |
| 1077 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1078 | "deallocated bytearray object has exported buffers"); |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 1079 | PyErr_Print(); |
| 1080 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1081 | if (self->ob_bytes != 0) { |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 1082 | PyObject_Free(self->ob_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1083 | } |
| 1084 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 1085 | } |
| 1086 | |
| 1087 | |
| 1088 | /* -------------------------------------------------------------------- */ |
| 1089 | /* Methods */ |
| 1090 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1091 | #define FASTSEARCH fastsearch |
| 1092 | #define STRINGLIB(F) stringlib_##F |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1093 | #define STRINGLIB_CHAR char |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 1094 | #define STRINGLIB_SIZEOF_CHAR 1 |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1095 | #define STRINGLIB_LEN PyByteArray_GET_SIZE |
| 1096 | #define STRINGLIB_STR PyByteArray_AS_STRING |
| 1097 | #define STRINGLIB_NEW PyByteArray_FromStringAndSize |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1098 | #define STRINGLIB_ISSPACE Py_ISSPACE |
| 1099 | #define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r')) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1100 | #define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact |
| 1101 | #define STRINGLIB_MUTABLE 1 |
| 1102 | |
| 1103 | #include "stringlib/fastsearch.h" |
| 1104 | #include "stringlib/count.h" |
| 1105 | #include "stringlib/find.h" |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 1106 | #include "stringlib/join.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1107 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1108 | #include "stringlib/split.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1109 | #include "stringlib/ctype.h" |
| 1110 | #include "stringlib/transmogrify.h" |
| 1111 | |
| 1112 | |
| 1113 | /* The following Py_LOCAL_INLINE and Py_LOCAL functions |
| 1114 | were copied from the old char* style string object. */ |
| 1115 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1116 | /* helper macro to fixup start/end slice values */ |
| 1117 | #define ADJUST_INDICES(start, end, len) \ |
| 1118 | if (end > len) \ |
| 1119 | end = len; \ |
| 1120 | else if (end < 0) { \ |
| 1121 | end += len; \ |
| 1122 | if (end < 0) \ |
| 1123 | end = 0; \ |
| 1124 | } \ |
| 1125 | if (start < 0) { \ |
| 1126 | start += len; \ |
| 1127 | if (start < 0) \ |
| 1128 | start = 0; \ |
| 1129 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1130 | |
| 1131 | Py_LOCAL_INLINE(Py_ssize_t) |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1132 | bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1133 | { |
| 1134 | PyObject *subobj; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1135 | char byte; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1136 | Py_buffer subbuf; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1137 | const char *sub; |
| 1138 | Py_ssize_t sub_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1139 | Py_ssize_t start=0, end=PY_SSIZE_T_MAX; |
| 1140 | Py_ssize_t res; |
| 1141 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1142 | if (!stringlib_parse_args_finds_byte("find/rfind/index/rindex", |
| 1143 | args, &subobj, &byte, &start, &end)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1144 | return -2; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1145 | |
| 1146 | if (subobj) { |
| 1147 | if (_getbuffer(subobj, &subbuf) < 0) |
| 1148 | return -2; |
| 1149 | |
| 1150 | sub = subbuf.buf; |
| 1151 | sub_len = subbuf.len; |
| 1152 | } |
| 1153 | else { |
| 1154 | sub = &byte; |
| 1155 | sub_len = 1; |
| 1156 | } |
| 1157 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1158 | if (dir > 0) |
| 1159 | res = stringlib_find_slice( |
| 1160 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1161 | sub, sub_len, start, end); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1162 | else |
| 1163 | res = stringlib_rfind_slice( |
| 1164 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1165 | sub, sub_len, start, end); |
| 1166 | |
| 1167 | if (subobj) |
| 1168 | PyBuffer_Release(&subbuf); |
| 1169 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1170 | return res; |
| 1171 | } |
| 1172 | |
| 1173 | PyDoc_STRVAR(find__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1174 | "B.find(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1175 | \n\ |
| 1176 | Return the lowest index in B where subsection sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 1177 | such that sub is contained within B[start,end]. Optional\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1178 | arguments start and end are interpreted as in slice notation.\n\ |
| 1179 | \n\ |
| 1180 | Return -1 on failure."); |
| 1181 | |
| 1182 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1183 | bytearray_find(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1184 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1185 | Py_ssize_t result = bytearray_find_internal(self, args, +1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1186 | if (result == -2) |
| 1187 | return NULL; |
| 1188 | return PyLong_FromSsize_t(result); |
| 1189 | } |
| 1190 | |
| 1191 | PyDoc_STRVAR(count__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1192 | "B.count(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1193 | \n\ |
| 1194 | Return the number of non-overlapping occurrences of subsection sub in\n\ |
| 1195 | bytes B[start:end]. Optional arguments start and end are interpreted\n\ |
| 1196 | as in slice notation."); |
| 1197 | |
| 1198 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1199 | bytearray_count(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1200 | { |
| 1201 | PyObject *sub_obj; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1202 | const char *str = PyByteArray_AS_STRING(self), *sub; |
| 1203 | Py_ssize_t sub_len; |
| 1204 | char byte; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1205 | Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1206 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1207 | Py_buffer vsub; |
| 1208 | PyObject *count_obj; |
| 1209 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1210 | if (!stringlib_parse_args_finds_byte("count", args, &sub_obj, &byte, |
| 1211 | &start, &end)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1212 | return NULL; |
| 1213 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1214 | if (sub_obj) { |
| 1215 | if (_getbuffer(sub_obj, &vsub) < 0) |
| 1216 | return NULL; |
| 1217 | |
| 1218 | sub = vsub.buf; |
| 1219 | sub_len = vsub.len; |
| 1220 | } |
| 1221 | else { |
| 1222 | sub = &byte; |
| 1223 | sub_len = 1; |
| 1224 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1225 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1226 | ADJUST_INDICES(start, end, PyByteArray_GET_SIZE(self)); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1227 | |
| 1228 | count_obj = PyLong_FromSsize_t( |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1229 | stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1230 | ); |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1231 | |
| 1232 | if (sub_obj) |
| 1233 | PyBuffer_Release(&vsub); |
| 1234 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1235 | return count_obj; |
| 1236 | } |
| 1237 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1238 | /*[clinic input] |
| 1239 | bytearray.clear |
| 1240 | |
| 1241 | self: self(type="PyByteArrayObject *") |
| 1242 | |
| 1243 | Remove all items from the bytearray. |
| 1244 | [clinic start generated code]*/ |
| 1245 | |
| 1246 | PyDoc_STRVAR(bytearray_clear__doc__, |
| 1247 | "clear($self, /)\n" |
| 1248 | "--\n" |
| 1249 | "\n" |
| 1250 | "Remove all items from the bytearray."); |
| 1251 | |
| 1252 | #define BYTEARRAY_CLEAR_METHODDEF \ |
| 1253 | {"clear", (PyCFunction)bytearray_clear, METH_NOARGS, bytearray_clear__doc__}, |
Eli Bendersky | 4db28d3 | 2011-03-03 18:21:02 +0000 | [diff] [blame] | 1254 | |
Victor Stinner | 6430fd5 | 2011-09-29 04:02:13 +0200 | [diff] [blame] | 1255 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1256 | bytearray_clear_impl(PyByteArrayObject *self); |
| 1257 | |
| 1258 | static PyObject * |
| 1259 | bytearray_clear(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored)) |
| 1260 | { |
| 1261 | return bytearray_clear_impl(self); |
| 1262 | } |
| 1263 | |
| 1264 | static PyObject * |
| 1265 | bytearray_clear_impl(PyByteArrayObject *self) |
| 1266 | /*[clinic end generated code: output=5344093031e2f36c input=e524fd330abcdc18]*/ |
Eli Bendersky | 4db28d3 | 2011-03-03 18:21:02 +0000 | [diff] [blame] | 1267 | { |
| 1268 | if (PyByteArray_Resize((PyObject *)self, 0) < 0) |
| 1269 | return NULL; |
| 1270 | Py_RETURN_NONE; |
| 1271 | } |
| 1272 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1273 | /*[clinic input] |
| 1274 | bytearray.copy |
| 1275 | |
| 1276 | self: self(type="PyByteArrayObject *") |
| 1277 | |
| 1278 | Return a copy of B. |
| 1279 | [clinic start generated code]*/ |
| 1280 | |
| 1281 | PyDoc_STRVAR(bytearray_copy__doc__, |
| 1282 | "copy($self, /)\n" |
| 1283 | "--\n" |
| 1284 | "\n" |
| 1285 | "Return a copy of B."); |
| 1286 | |
| 1287 | #define BYTEARRAY_COPY_METHODDEF \ |
| 1288 | {"copy", (PyCFunction)bytearray_copy, METH_NOARGS, bytearray_copy__doc__}, |
Eli Bendersky | 4db28d3 | 2011-03-03 18:21:02 +0000 | [diff] [blame] | 1289 | |
| 1290 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1291 | bytearray_copy_impl(PyByteArrayObject *self); |
| 1292 | |
| 1293 | static PyObject * |
| 1294 | bytearray_copy(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored)) |
| 1295 | { |
| 1296 | return bytearray_copy_impl(self); |
| 1297 | } |
| 1298 | |
| 1299 | static PyObject * |
| 1300 | bytearray_copy_impl(PyByteArrayObject *self) |
| 1301 | /*[clinic end generated code: output=8788ed299f7f2214 input=6d5d2975aa0f33f3]*/ |
Eli Bendersky | 4db28d3 | 2011-03-03 18:21:02 +0000 | [diff] [blame] | 1302 | { |
| 1303 | return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self), |
| 1304 | PyByteArray_GET_SIZE(self)); |
| 1305 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1306 | |
| 1307 | PyDoc_STRVAR(index__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1308 | "B.index(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1309 | \n\ |
| 1310 | Like B.find() but raise ValueError when the subsection is not found."); |
| 1311 | |
| 1312 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1313 | bytearray_index(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1314 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1315 | Py_ssize_t result = bytearray_find_internal(self, args, +1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1316 | if (result == -2) |
| 1317 | return NULL; |
| 1318 | if (result == -1) { |
| 1319 | PyErr_SetString(PyExc_ValueError, |
| 1320 | "subsection not found"); |
| 1321 | return NULL; |
| 1322 | } |
| 1323 | return PyLong_FromSsize_t(result); |
| 1324 | } |
| 1325 | |
| 1326 | |
| 1327 | PyDoc_STRVAR(rfind__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1328 | "B.rfind(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1329 | \n\ |
| 1330 | Return the highest index in B where subsection sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 1331 | such that sub is contained within B[start,end]. Optional\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1332 | arguments start and end are interpreted as in slice notation.\n\ |
| 1333 | \n\ |
| 1334 | Return -1 on failure."); |
| 1335 | |
| 1336 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1337 | bytearray_rfind(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1338 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1339 | Py_ssize_t result = bytearray_find_internal(self, args, -1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1340 | if (result == -2) |
| 1341 | return NULL; |
| 1342 | return PyLong_FromSsize_t(result); |
| 1343 | } |
| 1344 | |
| 1345 | |
| 1346 | PyDoc_STRVAR(rindex__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1347 | "B.rindex(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1348 | \n\ |
| 1349 | Like B.rfind() but raise ValueError when the subsection is not found."); |
| 1350 | |
| 1351 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1352 | bytearray_rindex(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1353 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1354 | Py_ssize_t result = bytearray_find_internal(self, args, -1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1355 | if (result == -2) |
| 1356 | return NULL; |
| 1357 | if (result == -1) { |
| 1358 | PyErr_SetString(PyExc_ValueError, |
| 1359 | "subsection not found"); |
| 1360 | return NULL; |
| 1361 | } |
| 1362 | return PyLong_FromSsize_t(result); |
| 1363 | } |
| 1364 | |
| 1365 | |
| 1366 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1367 | bytearray_contains(PyObject *self, PyObject *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1368 | { |
| 1369 | Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError); |
| 1370 | if (ival == -1 && PyErr_Occurred()) { |
| 1371 | Py_buffer varg; |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 1372 | Py_ssize_t pos; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1373 | PyErr_Clear(); |
| 1374 | if (_getbuffer(arg, &varg) < 0) |
| 1375 | return -1; |
| 1376 | pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self), |
| 1377 | varg.buf, varg.len, 0); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1378 | PyBuffer_Release(&varg); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1379 | return pos >= 0; |
| 1380 | } |
| 1381 | if (ival < 0 || ival >= 256) { |
| 1382 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
| 1383 | return -1; |
| 1384 | } |
| 1385 | |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 1386 | return memchr(PyByteArray_AS_STRING(self), (int) ival, Py_SIZE(self)) != NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | |
| 1390 | /* Matches the end (direction >= 0) or start (direction < 0) of self |
| 1391 | * against substr, using the start and end arguments. Returns |
| 1392 | * -1 on error, 0 if not found and 1 if found. |
| 1393 | */ |
| 1394 | Py_LOCAL(int) |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1395 | _bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1396 | Py_ssize_t end, int direction) |
| 1397 | { |
| 1398 | Py_ssize_t len = PyByteArray_GET_SIZE(self); |
| 1399 | const char* str; |
| 1400 | Py_buffer vsubstr; |
| 1401 | int rv = 0; |
| 1402 | |
| 1403 | str = PyByteArray_AS_STRING(self); |
| 1404 | |
| 1405 | if (_getbuffer(substr, &vsubstr) < 0) |
| 1406 | return -1; |
| 1407 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1408 | ADJUST_INDICES(start, end, len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1409 | |
| 1410 | if (direction < 0) { |
| 1411 | /* startswith */ |
| 1412 | if (start+vsubstr.len > len) { |
| 1413 | goto done; |
| 1414 | } |
| 1415 | } else { |
| 1416 | /* endswith */ |
| 1417 | if (end-start < vsubstr.len || start > len) { |
| 1418 | goto done; |
| 1419 | } |
| 1420 | |
| 1421 | if (end-vsubstr.len > start) |
| 1422 | start = end - vsubstr.len; |
| 1423 | } |
| 1424 | if (end-start >= vsubstr.len) |
| 1425 | rv = ! memcmp(str+start, vsubstr.buf, vsubstr.len); |
| 1426 | |
| 1427 | done: |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1428 | PyBuffer_Release(&vsubstr); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1429 | return rv; |
| 1430 | } |
| 1431 | |
| 1432 | |
| 1433 | PyDoc_STRVAR(startswith__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1434 | "B.startswith(prefix[, start[, end]]) -> bool\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1435 | \n\ |
| 1436 | Return True if B starts with the specified prefix, False otherwise.\n\ |
| 1437 | With optional start, test B beginning at that position.\n\ |
| 1438 | With optional end, stop comparing B at that position.\n\ |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1439 | prefix can also be a tuple of bytes to try."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1440 | |
| 1441 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1442 | bytearray_startswith(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1443 | { |
| 1444 | Py_ssize_t start = 0; |
| 1445 | Py_ssize_t end = PY_SSIZE_T_MAX; |
| 1446 | PyObject *subobj; |
| 1447 | int result; |
| 1448 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 1449 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1450 | return NULL; |
| 1451 | if (PyTuple_Check(subobj)) { |
| 1452 | Py_ssize_t i; |
| 1453 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1454 | result = _bytearray_tailmatch(self, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1455 | PyTuple_GET_ITEM(subobj, i), |
| 1456 | start, end, -1); |
| 1457 | if (result == -1) |
| 1458 | return NULL; |
| 1459 | else if (result) { |
| 1460 | Py_RETURN_TRUE; |
| 1461 | } |
| 1462 | } |
| 1463 | Py_RETURN_FALSE; |
| 1464 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1465 | result = _bytearray_tailmatch(self, subobj, start, end, -1); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1466 | if (result == -1) { |
| 1467 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1468 | PyErr_Format(PyExc_TypeError, "startswith first arg must be bytes " |
| 1469 | "or a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1470 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1471 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1472 | else |
| 1473 | return PyBool_FromLong(result); |
| 1474 | } |
| 1475 | |
| 1476 | PyDoc_STRVAR(endswith__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1477 | "B.endswith(suffix[, start[, end]]) -> bool\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1478 | \n\ |
| 1479 | Return True if B ends with the specified suffix, False otherwise.\n\ |
| 1480 | With optional start, test B beginning at that position.\n\ |
| 1481 | With optional end, stop comparing B at that position.\n\ |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1482 | suffix can also be a tuple of bytes to try."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1483 | |
| 1484 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1485 | bytearray_endswith(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1486 | { |
| 1487 | Py_ssize_t start = 0; |
| 1488 | Py_ssize_t end = PY_SSIZE_T_MAX; |
| 1489 | PyObject *subobj; |
| 1490 | int result; |
| 1491 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 1492 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1493 | return NULL; |
| 1494 | if (PyTuple_Check(subobj)) { |
| 1495 | Py_ssize_t i; |
| 1496 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1497 | result = _bytearray_tailmatch(self, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1498 | PyTuple_GET_ITEM(subobj, i), |
| 1499 | start, end, +1); |
| 1500 | if (result == -1) |
| 1501 | return NULL; |
| 1502 | else if (result) { |
| 1503 | Py_RETURN_TRUE; |
| 1504 | } |
| 1505 | } |
| 1506 | Py_RETURN_FALSE; |
| 1507 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1508 | result = _bytearray_tailmatch(self, subobj, start, end, +1); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1509 | if (result == -1) { |
| 1510 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1511 | PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or " |
| 1512 | "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1513 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1514 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1515 | else |
| 1516 | return PyBool_FromLong(result); |
| 1517 | } |
| 1518 | |
| 1519 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1520 | /*[clinic input] |
| 1521 | bytearray.translate |
| 1522 | |
| 1523 | self: self(type="PyByteArrayObject *") |
| 1524 | table: object |
| 1525 | Translation table, which must be a bytes object of length 256. |
| 1526 | [ |
| 1527 | deletechars: object |
| 1528 | ] |
| 1529 | / |
| 1530 | |
| 1531 | Return a copy with each character mapped by the given translation table. |
| 1532 | |
| 1533 | All characters occurring in the optional argument deletechars are removed. |
| 1534 | The remaining characters are mapped through the given translation table. |
| 1535 | [clinic start generated code]*/ |
| 1536 | |
| 1537 | PyDoc_STRVAR(bytearray_translate__doc__, |
| 1538 | "translate(table, [deletechars])\n" |
| 1539 | "Return a copy with each character mapped by the given translation table.\n" |
| 1540 | "\n" |
| 1541 | " table\n" |
| 1542 | " Translation table, which must be a bytes object of length 256.\n" |
| 1543 | "\n" |
| 1544 | "All characters occurring in the optional argument deletechars are removed.\n" |
| 1545 | "The remaining characters are mapped through the given translation table."); |
| 1546 | |
| 1547 | #define BYTEARRAY_TRANSLATE_METHODDEF \ |
| 1548 | {"translate", (PyCFunction)bytearray_translate, METH_VARARGS, bytearray_translate__doc__}, |
| 1549 | |
| 1550 | static PyObject * |
| 1551 | bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, int group_right_1, PyObject *deletechars); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1552 | |
| 1553 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1554 | bytearray_translate(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1555 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1556 | PyObject *return_value = NULL; |
| 1557 | PyObject *table; |
| 1558 | int group_right_1 = 0; |
| 1559 | PyObject *deletechars = NULL; |
| 1560 | |
| 1561 | switch (PyTuple_GET_SIZE(args)) { |
| 1562 | case 1: |
| 1563 | if (!PyArg_ParseTuple(args, "O:translate", &table)) |
| 1564 | goto exit; |
| 1565 | break; |
| 1566 | case 2: |
| 1567 | if (!PyArg_ParseTuple(args, "OO:translate", &table, &deletechars)) |
| 1568 | goto exit; |
| 1569 | group_right_1 = 1; |
| 1570 | break; |
| 1571 | default: |
| 1572 | PyErr_SetString(PyExc_TypeError, "bytearray.translate requires 1 to 2 arguments"); |
| 1573 | goto exit; |
| 1574 | } |
| 1575 | return_value = bytearray_translate_impl(self, table, group_right_1, deletechars); |
| 1576 | |
| 1577 | exit: |
| 1578 | return return_value; |
| 1579 | } |
| 1580 | |
| 1581 | static PyObject * |
| 1582 | bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, int group_right_1, PyObject *deletechars) |
| 1583 | /*[clinic end generated code: output=a709df81d41db4b7 input=b749ad85f4860824]*/ |
| 1584 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1585 | char *input, *output; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1586 | const char *table_chars; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1587 | Py_ssize_t i, c; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1588 | PyObject *input_obj = (PyObject*)self; |
| 1589 | const char *output_start; |
| 1590 | Py_ssize_t inlen; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1591 | PyObject *result = NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1592 | int trans_table[256]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1593 | Py_buffer vtable, vdel; |
| 1594 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1595 | if (table == Py_None) { |
| 1596 | table_chars = NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1597 | table = NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1598 | } else if (_getbuffer(table, &vtable) < 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1599 | return NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1600 | } else { |
| 1601 | if (vtable.len != 256) { |
| 1602 | PyErr_SetString(PyExc_ValueError, |
| 1603 | "translation table must be 256 characters long"); |
Georg Brandl | 953152f | 2009-07-22 12:03:59 +0000 | [diff] [blame] | 1604 | PyBuffer_Release(&vtable); |
| 1605 | return NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1606 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1607 | table_chars = (const char*)vtable.buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1610 | if (deletechars != NULL) { |
| 1611 | if (_getbuffer(deletechars, &vdel) < 0) { |
| 1612 | if (table != NULL) |
Georg Brandl | 953152f | 2009-07-22 12:03:59 +0000 | [diff] [blame] | 1613 | PyBuffer_Release(&vtable); |
| 1614 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1615 | } |
| 1616 | } |
| 1617 | else { |
| 1618 | vdel.buf = NULL; |
| 1619 | vdel.len = 0; |
| 1620 | } |
| 1621 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1622 | inlen = PyByteArray_GET_SIZE(input_obj); |
| 1623 | result = PyByteArray_FromStringAndSize((char *)NULL, inlen); |
| 1624 | if (result == NULL) |
| 1625 | goto done; |
| 1626 | output_start = output = PyByteArray_AsString(result); |
| 1627 | input = PyByteArray_AS_STRING(input_obj); |
| 1628 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1629 | if (vdel.len == 0 && table_chars != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1630 | /* If no deletions are required, use faster code */ |
| 1631 | for (i = inlen; --i >= 0; ) { |
| 1632 | c = Py_CHARMASK(*input++); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1633 | *output++ = table_chars[c]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1634 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1635 | goto done; |
| 1636 | } |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1637 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1638 | if (table_chars == NULL) { |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1639 | for (i = 0; i < 256; i++) |
| 1640 | trans_table[i] = Py_CHARMASK(i); |
| 1641 | } else { |
| 1642 | for (i = 0; i < 256; i++) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1643 | trans_table[i] = Py_CHARMASK(table_chars[i]); |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1644 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1645 | |
| 1646 | for (i = 0; i < vdel.len; i++) |
| 1647 | trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1; |
| 1648 | |
| 1649 | for (i = inlen; --i >= 0; ) { |
| 1650 | c = Py_CHARMASK(*input++); |
| 1651 | if (trans_table[c] != -1) |
| 1652 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 1653 | continue; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1654 | } |
| 1655 | /* Fix the size of the resulting string */ |
| 1656 | if (inlen > 0) |
Christian Heimes | c731bbe | 2013-07-21 02:04:35 +0200 | [diff] [blame] | 1657 | if (PyByteArray_Resize(result, output - output_start) < 0) { |
| 1658 | Py_CLEAR(result); |
| 1659 | goto done; |
| 1660 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1661 | |
| 1662 | done: |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1663 | if (table != NULL) |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1664 | PyBuffer_Release(&vtable); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1665 | if (deletechars != NULL) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1666 | PyBuffer_Release(&vdel); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1667 | return result; |
| 1668 | } |
| 1669 | |
| 1670 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1671 | /*[clinic input] |
| 1672 | |
| 1673 | @staticmethod |
| 1674 | bytearray.maketrans |
| 1675 | |
| 1676 | frm: object |
| 1677 | to: object |
| 1678 | / |
| 1679 | |
| 1680 | Return a translation table useable for the bytes or bytearray translate method. |
| 1681 | |
| 1682 | The returned table will be one where each byte in frm is mapped to the byte at |
| 1683 | the same position in to. |
| 1684 | |
| 1685 | The bytes objects frm and to must be of the same length. |
| 1686 | [clinic start generated code]*/ |
| 1687 | |
| 1688 | PyDoc_STRVAR(bytearray_maketrans__doc__, |
| 1689 | "maketrans(frm, to, /)\n" |
| 1690 | "--\n" |
| 1691 | "\n" |
| 1692 | "Return a translation table useable for the bytes or bytearray translate method.\n" |
| 1693 | "\n" |
| 1694 | "The returned table will be one where each byte in frm is mapped to the byte at\n" |
| 1695 | "the same position in to.\n" |
| 1696 | "\n" |
| 1697 | "The bytes objects frm and to must be of the same length."); |
| 1698 | |
| 1699 | #define BYTEARRAY_MAKETRANS_METHODDEF \ |
| 1700 | {"maketrans", (PyCFunction)bytearray_maketrans, METH_VARARGS|METH_STATIC, bytearray_maketrans__doc__}, |
| 1701 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1702 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1703 | bytearray_maketrans_impl(PyObject *frm, PyObject *to); |
| 1704 | |
| 1705 | static PyObject * |
| 1706 | bytearray_maketrans(void *null, PyObject *args) |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1707 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1708 | PyObject *return_value = NULL; |
| 1709 | PyObject *frm; |
| 1710 | PyObject *to; |
| 1711 | |
| 1712 | if (!PyArg_UnpackTuple(args, "maketrans", |
| 1713 | 2, 2, |
| 1714 | &frm, &to)) |
| 1715 | goto exit; |
| 1716 | return_value = bytearray_maketrans_impl(frm, to); |
| 1717 | |
| 1718 | exit: |
| 1719 | return return_value; |
| 1720 | } |
| 1721 | |
| 1722 | static PyObject * |
| 1723 | bytearray_maketrans_impl(PyObject *frm, PyObject *to) |
| 1724 | /*[clinic end generated code: output=307752019d9b25b5 input=ea9bdc6b328c15e2]*/ |
| 1725 | { |
| 1726 | return _Py_bytes_maketrans(frm, to); |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1730 | /* find and count characters and substrings */ |
| 1731 | |
| 1732 | #define findchar(target, target_len, c) \ |
| 1733 | ((char *)memchr((const void *)(target), c, target_len)) |
| 1734 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1735 | |
Benjamin Peterson | 0f3641c | 2008-11-19 22:05:52 +0000 | [diff] [blame] | 1736 | /* Bytes ops must return a string, create a copy */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1737 | Py_LOCAL(PyByteArrayObject *) |
| 1738 | return_self(PyByteArrayObject *self) |
| 1739 | { |
Georg Brandl | 1e7217d | 2008-05-30 12:02:38 +0000 | [diff] [blame] | 1740 | /* always return a new bytearray */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1741 | return (PyByteArrayObject *)PyByteArray_FromStringAndSize( |
| 1742 | PyByteArray_AS_STRING(self), |
| 1743 | PyByteArray_GET_SIZE(self)); |
| 1744 | } |
| 1745 | |
| 1746 | Py_LOCAL_INLINE(Py_ssize_t) |
| 1747 | countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount) |
| 1748 | { |
| 1749 | Py_ssize_t count=0; |
| 1750 | const char *start=target; |
| 1751 | const char *end=target+target_len; |
| 1752 | |
| 1753 | while ( (start=findchar(start, end-start, c)) != NULL ) { |
| 1754 | count++; |
| 1755 | if (count >= maxcount) |
| 1756 | break; |
| 1757 | start += 1; |
| 1758 | } |
| 1759 | return count; |
| 1760 | } |
| 1761 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1762 | |
| 1763 | /* Algorithms for different cases of string replacement */ |
| 1764 | |
| 1765 | /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
| 1766 | Py_LOCAL(PyByteArrayObject *) |
| 1767 | replace_interleave(PyByteArrayObject *self, |
| 1768 | const char *to_s, Py_ssize_t to_len, |
| 1769 | Py_ssize_t maxcount) |
| 1770 | { |
| 1771 | char *self_s, *result_s; |
| 1772 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1773 | Py_ssize_t count, i; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1774 | PyByteArrayObject *result; |
| 1775 | |
| 1776 | self_len = PyByteArray_GET_SIZE(self); |
| 1777 | |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1778 | /* 1 at the end plus 1 after every character; |
| 1779 | count = min(maxcount, self_len + 1) */ |
| 1780 | if (maxcount <= self_len) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1781 | count = maxcount; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1782 | else |
| 1783 | /* Can't overflow: self_len + 1 <= maxcount <= PY_SSIZE_T_MAX. */ |
| 1784 | count = self_len + 1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1785 | |
| 1786 | /* Check for overflow */ |
| 1787 | /* result_len = count * to_len + self_len; */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1788 | assert(count > 0); |
| 1789 | if (to_len > (PY_SSIZE_T_MAX - self_len) / count) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1790 | PyErr_SetString(PyExc_OverflowError, |
| 1791 | "replace string is too long"); |
| 1792 | return NULL; |
| 1793 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1794 | result_len = count * to_len + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1795 | |
| 1796 | if (! (result = (PyByteArrayObject *) |
| 1797 | PyByteArray_FromStringAndSize(NULL, result_len)) ) |
| 1798 | return NULL; |
| 1799 | |
| 1800 | self_s = PyByteArray_AS_STRING(self); |
| 1801 | result_s = PyByteArray_AS_STRING(result); |
| 1802 | |
| 1803 | /* TODO: special case single character, which doesn't need memcpy */ |
| 1804 | |
| 1805 | /* Lay the first one down (guaranteed this will occur) */ |
| 1806 | Py_MEMCPY(result_s, to_s, to_len); |
| 1807 | result_s += to_len; |
| 1808 | count -= 1; |
| 1809 | |
| 1810 | for (i=0; i<count; i++) { |
| 1811 | *result_s++ = *self_s++; |
| 1812 | Py_MEMCPY(result_s, to_s, to_len); |
| 1813 | result_s += to_len; |
| 1814 | } |
| 1815 | |
| 1816 | /* Copy the rest of the original string */ |
| 1817 | Py_MEMCPY(result_s, self_s, self_len-i); |
| 1818 | |
| 1819 | return result; |
| 1820 | } |
| 1821 | |
| 1822 | /* Special case for deleting a single character */ |
| 1823 | /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
| 1824 | Py_LOCAL(PyByteArrayObject *) |
| 1825 | replace_delete_single_character(PyByteArrayObject *self, |
| 1826 | char from_c, Py_ssize_t maxcount) |
| 1827 | { |
| 1828 | char *self_s, *result_s; |
| 1829 | char *start, *next, *end; |
| 1830 | Py_ssize_t self_len, result_len; |
| 1831 | Py_ssize_t count; |
| 1832 | PyByteArrayObject *result; |
| 1833 | |
| 1834 | self_len = PyByteArray_GET_SIZE(self); |
| 1835 | self_s = PyByteArray_AS_STRING(self); |
| 1836 | |
| 1837 | count = countchar(self_s, self_len, from_c, maxcount); |
| 1838 | if (count == 0) { |
| 1839 | return return_self(self); |
| 1840 | } |
| 1841 | |
| 1842 | result_len = self_len - count; /* from_len == 1 */ |
| 1843 | assert(result_len>=0); |
| 1844 | |
| 1845 | if ( (result = (PyByteArrayObject *) |
| 1846 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL) |
| 1847 | return NULL; |
| 1848 | result_s = PyByteArray_AS_STRING(result); |
| 1849 | |
| 1850 | start = self_s; |
| 1851 | end = self_s + self_len; |
| 1852 | while (count-- > 0) { |
| 1853 | next = findchar(start, end-start, from_c); |
| 1854 | if (next == NULL) |
| 1855 | break; |
| 1856 | Py_MEMCPY(result_s, start, next-start); |
| 1857 | result_s += (next-start); |
| 1858 | start = next+1; |
| 1859 | } |
| 1860 | Py_MEMCPY(result_s, start, end-start); |
| 1861 | |
| 1862 | return result; |
| 1863 | } |
| 1864 | |
| 1865 | /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
| 1866 | |
| 1867 | Py_LOCAL(PyByteArrayObject *) |
| 1868 | replace_delete_substring(PyByteArrayObject *self, |
| 1869 | const char *from_s, Py_ssize_t from_len, |
| 1870 | Py_ssize_t maxcount) |
| 1871 | { |
| 1872 | char *self_s, *result_s; |
| 1873 | char *start, *next, *end; |
| 1874 | Py_ssize_t self_len, result_len; |
| 1875 | Py_ssize_t count, offset; |
| 1876 | PyByteArrayObject *result; |
| 1877 | |
| 1878 | self_len = PyByteArray_GET_SIZE(self); |
| 1879 | self_s = PyByteArray_AS_STRING(self); |
| 1880 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1881 | count = stringlib_count(self_s, self_len, |
| 1882 | from_s, from_len, |
| 1883 | maxcount); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1884 | |
| 1885 | if (count == 0) { |
| 1886 | /* no matches */ |
| 1887 | return return_self(self); |
| 1888 | } |
| 1889 | |
| 1890 | result_len = self_len - (count * from_len); |
| 1891 | assert (result_len>=0); |
| 1892 | |
| 1893 | if ( (result = (PyByteArrayObject *) |
| 1894 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL ) |
| 1895 | return NULL; |
| 1896 | |
| 1897 | result_s = PyByteArray_AS_STRING(result); |
| 1898 | |
| 1899 | start = self_s; |
| 1900 | end = self_s + self_len; |
| 1901 | while (count-- > 0) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1902 | offset = stringlib_find(start, end-start, |
| 1903 | from_s, from_len, |
| 1904 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1905 | if (offset == -1) |
| 1906 | break; |
| 1907 | next = start + offset; |
| 1908 | |
| 1909 | Py_MEMCPY(result_s, start, next-start); |
| 1910 | |
| 1911 | result_s += (next-start); |
| 1912 | start = next+from_len; |
| 1913 | } |
| 1914 | Py_MEMCPY(result_s, start, end-start); |
| 1915 | return result; |
| 1916 | } |
| 1917 | |
| 1918 | /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
| 1919 | Py_LOCAL(PyByteArrayObject *) |
| 1920 | replace_single_character_in_place(PyByteArrayObject *self, |
| 1921 | char from_c, char to_c, |
| 1922 | Py_ssize_t maxcount) |
| 1923 | { |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1924 | char *self_s, *result_s, *start, *end, *next; |
| 1925 | Py_ssize_t self_len; |
| 1926 | PyByteArrayObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1927 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1928 | /* The result string will be the same size */ |
| 1929 | self_s = PyByteArray_AS_STRING(self); |
| 1930 | self_len = PyByteArray_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1931 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1932 | next = findchar(self_s, self_len, from_c); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1933 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1934 | if (next == NULL) { |
| 1935 | /* No matches; return the original bytes */ |
| 1936 | return return_self(self); |
| 1937 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1938 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1939 | /* Need to make a new bytes */ |
| 1940 | result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len); |
| 1941 | if (result == NULL) |
| 1942 | return NULL; |
| 1943 | result_s = PyByteArray_AS_STRING(result); |
| 1944 | Py_MEMCPY(result_s, self_s, self_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1945 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1946 | /* change everything in-place, starting with this one */ |
| 1947 | start = result_s + (next-self_s); |
| 1948 | *start = to_c; |
| 1949 | start++; |
| 1950 | end = result_s + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1951 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1952 | while (--maxcount > 0) { |
| 1953 | next = findchar(start, end-start, from_c); |
| 1954 | if (next == NULL) |
| 1955 | break; |
| 1956 | *next = to_c; |
| 1957 | start = next+1; |
| 1958 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1959 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1960 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
| 1964 | Py_LOCAL(PyByteArrayObject *) |
| 1965 | replace_substring_in_place(PyByteArrayObject *self, |
| 1966 | const char *from_s, Py_ssize_t from_len, |
| 1967 | const char *to_s, Py_ssize_t to_len, |
| 1968 | Py_ssize_t maxcount) |
| 1969 | { |
| 1970 | char *result_s, *start, *end; |
| 1971 | char *self_s; |
| 1972 | Py_ssize_t self_len, offset; |
| 1973 | PyByteArrayObject *result; |
| 1974 | |
| 1975 | /* The result bytes will be the same size */ |
| 1976 | |
| 1977 | self_s = PyByteArray_AS_STRING(self); |
| 1978 | self_len = PyByteArray_GET_SIZE(self); |
| 1979 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1980 | offset = stringlib_find(self_s, self_len, |
| 1981 | from_s, from_len, |
| 1982 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1983 | if (offset == -1) { |
| 1984 | /* No matches; return the original bytes */ |
| 1985 | return return_self(self); |
| 1986 | } |
| 1987 | |
| 1988 | /* Need to make a new bytes */ |
| 1989 | result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len); |
| 1990 | if (result == NULL) |
| 1991 | return NULL; |
| 1992 | result_s = PyByteArray_AS_STRING(result); |
| 1993 | Py_MEMCPY(result_s, self_s, self_len); |
| 1994 | |
| 1995 | /* change everything in-place, starting with this one */ |
| 1996 | start = result_s + offset; |
| 1997 | Py_MEMCPY(start, to_s, from_len); |
| 1998 | start += from_len; |
| 1999 | end = result_s + self_len; |
| 2000 | |
| 2001 | while ( --maxcount > 0) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2002 | offset = stringlib_find(start, end-start, |
| 2003 | from_s, from_len, |
| 2004 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2005 | if (offset==-1) |
| 2006 | break; |
| 2007 | Py_MEMCPY(start+offset, to_s, from_len); |
| 2008 | start += offset+from_len; |
| 2009 | } |
| 2010 | |
| 2011 | return result; |
| 2012 | } |
| 2013 | |
| 2014 | /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
| 2015 | Py_LOCAL(PyByteArrayObject *) |
| 2016 | replace_single_character(PyByteArrayObject *self, |
| 2017 | char from_c, |
| 2018 | const char *to_s, Py_ssize_t to_len, |
| 2019 | Py_ssize_t maxcount) |
| 2020 | { |
| 2021 | char *self_s, *result_s; |
| 2022 | char *start, *next, *end; |
| 2023 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2024 | Py_ssize_t count; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2025 | PyByteArrayObject *result; |
| 2026 | |
| 2027 | self_s = PyByteArray_AS_STRING(self); |
| 2028 | self_len = PyByteArray_GET_SIZE(self); |
| 2029 | |
| 2030 | count = countchar(self_s, self_len, from_c, maxcount); |
| 2031 | if (count == 0) { |
| 2032 | /* no matches, return unchanged */ |
| 2033 | return return_self(self); |
| 2034 | } |
| 2035 | |
| 2036 | /* use the difference between current and new, hence the "-1" */ |
| 2037 | /* result_len = self_len + count * (to_len-1) */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2038 | assert(count > 0); |
| 2039 | if (to_len - 1 > (PY_SSIZE_T_MAX - self_len) / count) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2040 | PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
| 2041 | return NULL; |
| 2042 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2043 | result_len = self_len + count * (to_len - 1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2044 | |
| 2045 | if ( (result = (PyByteArrayObject *) |
| 2046 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL) |
| 2047 | return NULL; |
| 2048 | result_s = PyByteArray_AS_STRING(result); |
| 2049 | |
| 2050 | start = self_s; |
| 2051 | end = self_s + self_len; |
| 2052 | while (count-- > 0) { |
| 2053 | next = findchar(start, end-start, from_c); |
| 2054 | if (next == NULL) |
| 2055 | break; |
| 2056 | |
| 2057 | if (next == start) { |
| 2058 | /* replace with the 'to' */ |
| 2059 | Py_MEMCPY(result_s, to_s, to_len); |
| 2060 | result_s += to_len; |
| 2061 | start += 1; |
| 2062 | } else { |
| 2063 | /* copy the unchanged old then the 'to' */ |
| 2064 | Py_MEMCPY(result_s, start, next-start); |
| 2065 | result_s += (next-start); |
| 2066 | Py_MEMCPY(result_s, to_s, to_len); |
| 2067 | result_s += to_len; |
| 2068 | start = next+1; |
| 2069 | } |
| 2070 | } |
| 2071 | /* Copy the remainder of the remaining bytes */ |
| 2072 | Py_MEMCPY(result_s, start, end-start); |
| 2073 | |
| 2074 | return result; |
| 2075 | } |
| 2076 | |
| 2077 | /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
| 2078 | Py_LOCAL(PyByteArrayObject *) |
| 2079 | replace_substring(PyByteArrayObject *self, |
| 2080 | const char *from_s, Py_ssize_t from_len, |
| 2081 | const char *to_s, Py_ssize_t to_len, |
| 2082 | Py_ssize_t maxcount) |
| 2083 | { |
| 2084 | char *self_s, *result_s; |
| 2085 | char *start, *next, *end; |
| 2086 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2087 | Py_ssize_t count, offset; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2088 | PyByteArrayObject *result; |
| 2089 | |
| 2090 | self_s = PyByteArray_AS_STRING(self); |
| 2091 | self_len = PyByteArray_GET_SIZE(self); |
| 2092 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2093 | count = stringlib_count(self_s, self_len, |
| 2094 | from_s, from_len, |
| 2095 | maxcount); |
| 2096 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2097 | if (count == 0) { |
| 2098 | /* no matches, return unchanged */ |
| 2099 | return return_self(self); |
| 2100 | } |
| 2101 | |
| 2102 | /* Check for overflow */ |
| 2103 | /* result_len = self_len + count * (to_len-from_len) */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2104 | assert(count > 0); |
| 2105 | if (to_len - from_len > (PY_SSIZE_T_MAX - self_len) / count) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2106 | PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
| 2107 | return NULL; |
| 2108 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2109 | result_len = self_len + count * (to_len - from_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2110 | |
| 2111 | if ( (result = (PyByteArrayObject *) |
| 2112 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL) |
| 2113 | return NULL; |
| 2114 | result_s = PyByteArray_AS_STRING(result); |
| 2115 | |
| 2116 | start = self_s; |
| 2117 | end = self_s + self_len; |
| 2118 | while (count-- > 0) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2119 | offset = stringlib_find(start, end-start, |
| 2120 | from_s, from_len, |
| 2121 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2122 | if (offset == -1) |
| 2123 | break; |
| 2124 | next = start+offset; |
| 2125 | if (next == start) { |
| 2126 | /* replace with the 'to' */ |
| 2127 | Py_MEMCPY(result_s, to_s, to_len); |
| 2128 | result_s += to_len; |
| 2129 | start += from_len; |
| 2130 | } else { |
| 2131 | /* copy the unchanged old then the 'to' */ |
| 2132 | Py_MEMCPY(result_s, start, next-start); |
| 2133 | result_s += (next-start); |
| 2134 | Py_MEMCPY(result_s, to_s, to_len); |
| 2135 | result_s += to_len; |
| 2136 | start = next+from_len; |
| 2137 | } |
| 2138 | } |
| 2139 | /* Copy the remainder of the remaining bytes */ |
| 2140 | Py_MEMCPY(result_s, start, end-start); |
| 2141 | |
| 2142 | return result; |
| 2143 | } |
| 2144 | |
| 2145 | |
| 2146 | Py_LOCAL(PyByteArrayObject *) |
| 2147 | replace(PyByteArrayObject *self, |
| 2148 | const char *from_s, Py_ssize_t from_len, |
| 2149 | const char *to_s, Py_ssize_t to_len, |
| 2150 | Py_ssize_t maxcount) |
| 2151 | { |
| 2152 | if (maxcount < 0) { |
| 2153 | maxcount = PY_SSIZE_T_MAX; |
| 2154 | } else if (maxcount == 0 || PyByteArray_GET_SIZE(self) == 0) { |
| 2155 | /* nothing to do; return the original bytes */ |
| 2156 | return return_self(self); |
| 2157 | } |
| 2158 | |
| 2159 | if (maxcount == 0 || |
| 2160 | (from_len == 0 && to_len == 0)) { |
| 2161 | /* nothing to do; return the original bytes */ |
| 2162 | return return_self(self); |
| 2163 | } |
| 2164 | |
| 2165 | /* Handle zero-length special cases */ |
| 2166 | |
| 2167 | if (from_len == 0) { |
| 2168 | /* insert the 'to' bytes everywhere. */ |
| 2169 | /* >>> "Python".replace("", ".") */ |
| 2170 | /* '.P.y.t.h.o.n.' */ |
| 2171 | return replace_interleave(self, to_s, to_len, maxcount); |
| 2172 | } |
| 2173 | |
| 2174 | /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
| 2175 | /* point for an empty self bytes to generate a non-empty bytes */ |
| 2176 | /* Special case so the remaining code always gets a non-empty bytes */ |
| 2177 | if (PyByteArray_GET_SIZE(self) == 0) { |
| 2178 | return return_self(self); |
| 2179 | } |
| 2180 | |
| 2181 | if (to_len == 0) { |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2182 | /* delete all occurrences of 'from' bytes */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2183 | if (from_len == 1) { |
| 2184 | return replace_delete_single_character( |
| 2185 | self, from_s[0], maxcount); |
| 2186 | } else { |
| 2187 | return replace_delete_substring(self, from_s, from_len, maxcount); |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | /* Handle special case where both bytes have the same length */ |
| 2192 | |
| 2193 | if (from_len == to_len) { |
| 2194 | if (from_len == 1) { |
| 2195 | return replace_single_character_in_place( |
| 2196 | self, |
| 2197 | from_s[0], |
| 2198 | to_s[0], |
| 2199 | maxcount); |
| 2200 | } else { |
| 2201 | return replace_substring_in_place( |
| 2202 | self, from_s, from_len, to_s, to_len, maxcount); |
| 2203 | } |
| 2204 | } |
| 2205 | |
| 2206 | /* Otherwise use the more generic algorithms */ |
| 2207 | if (from_len == 1) { |
| 2208 | return replace_single_character(self, from_s[0], |
| 2209 | to_s, to_len, maxcount); |
| 2210 | } else { |
| 2211 | /* len('from')>=2, len('to')>=1 */ |
| 2212 | return replace_substring(self, from_s, from_len, to_s, to_len, maxcount); |
| 2213 | } |
| 2214 | } |
| 2215 | |
| 2216 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2217 | /*[clinic input] |
| 2218 | bytearray.replace |
| 2219 | |
| 2220 | old: object |
| 2221 | new: object |
| 2222 | count: Py_ssize_t = -1 |
| 2223 | Maximum number of occurrences to replace. |
| 2224 | -1 (the default value) means replace all occurrences. |
| 2225 | / |
| 2226 | |
| 2227 | Return a copy with all occurrences of substring old replaced by new. |
| 2228 | |
| 2229 | If the optional argument count is given, only the first count occurrences are |
| 2230 | replaced. |
| 2231 | [clinic start generated code]*/ |
| 2232 | |
| 2233 | PyDoc_STRVAR(bytearray_replace__doc__, |
| 2234 | "replace($self, old, new, count=-1, /)\n" |
| 2235 | "--\n" |
| 2236 | "\n" |
| 2237 | "Return a copy with all occurrences of substring old replaced by new.\n" |
| 2238 | "\n" |
| 2239 | " count\n" |
| 2240 | " Maximum number of occurrences to replace.\n" |
| 2241 | " -1 (the default value) means replace all occurrences.\n" |
| 2242 | "\n" |
| 2243 | "If the optional argument count is given, only the first count occurrences are\n" |
| 2244 | "replaced."); |
| 2245 | |
| 2246 | #define BYTEARRAY_REPLACE_METHODDEF \ |
| 2247 | {"replace", (PyCFunction)bytearray_replace, METH_VARARGS, bytearray_replace__doc__}, |
| 2248 | |
| 2249 | static PyObject * |
| 2250 | bytearray_replace_impl(PyByteArrayObject *self, PyObject *old, PyObject *new, Py_ssize_t count); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2251 | |
| 2252 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2253 | bytearray_replace(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2254 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2255 | PyObject *return_value = NULL; |
| 2256 | PyObject *old; |
| 2257 | PyObject *new; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2258 | Py_ssize_t count = -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2259 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2260 | if (!PyArg_ParseTuple(args, |
| 2261 | "OO|n:replace", |
| 2262 | &old, &new, &count)) |
| 2263 | goto exit; |
| 2264 | return_value = bytearray_replace_impl(self, old, new, count); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2265 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2266 | exit: |
| 2267 | return return_value; |
| 2268 | } |
| 2269 | |
| 2270 | static PyObject * |
| 2271 | bytearray_replace_impl(PyByteArrayObject *self, PyObject *old, PyObject *new, Py_ssize_t count) |
| 2272 | /*[clinic end generated code: output=4d2e3c9130da0f96 input=9aaaa123608dfc1f]*/ |
| 2273 | { |
| 2274 | PyObject *res; |
| 2275 | Py_buffer vold, vnew; |
| 2276 | |
| 2277 | if (_getbuffer(old, &vold) < 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2278 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2279 | if (_getbuffer(new, &vnew) < 0) { |
| 2280 | PyBuffer_Release(&vold); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2281 | return NULL; |
| 2282 | } |
| 2283 | |
| 2284 | res = (PyObject *)replace((PyByteArrayObject *) self, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2285 | vold.buf, vold.len, |
| 2286 | vnew.buf, vnew.len, count); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2287 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2288 | PyBuffer_Release(&vold); |
| 2289 | PyBuffer_Release(&vnew); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2290 | return res; |
| 2291 | } |
| 2292 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2293 | /*[clinic input] |
| 2294 | bytearray.split |
| 2295 | |
| 2296 | sep: object = None |
| 2297 | The delimiter according which to split the bytearray. |
| 2298 | None (the default value) means split on ASCII whitespace characters |
| 2299 | (space, tab, return, newline, formfeed, vertical tab). |
| 2300 | maxsplit: Py_ssize_t = -1 |
| 2301 | Maximum number of splits to do. |
| 2302 | -1 (the default value) means no limit. |
| 2303 | |
| 2304 | Return a list of the sections in the bytearray, using sep as the delimiter. |
| 2305 | [clinic start generated code]*/ |
| 2306 | |
| 2307 | PyDoc_STRVAR(bytearray_split__doc__, |
| 2308 | "split($self, /, sep=None, maxsplit=-1)\n" |
| 2309 | "--\n" |
| 2310 | "\n" |
| 2311 | "Return a list of the sections in the bytearray, using sep as the delimiter.\n" |
| 2312 | "\n" |
| 2313 | " sep\n" |
| 2314 | " The delimiter according which to split the bytearray.\n" |
| 2315 | " None (the default value) means split on ASCII whitespace characters\n" |
| 2316 | " (space, tab, return, newline, formfeed, vertical tab).\n" |
| 2317 | " maxsplit\n" |
| 2318 | " Maximum number of splits to do.\n" |
| 2319 | " -1 (the default value) means no limit."); |
| 2320 | |
| 2321 | #define BYTEARRAY_SPLIT_METHODDEF \ |
| 2322 | {"split", (PyCFunction)bytearray_split, METH_VARARGS|METH_KEYWORDS, bytearray_split__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2323 | |
| 2324 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2325 | bytearray_split_impl(PyByteArrayObject *self, PyObject *sep, Py_ssize_t maxsplit); |
| 2326 | |
| 2327 | static PyObject * |
| 2328 | bytearray_split(PyByteArrayObject *self, PyObject *args, PyObject *kwargs) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2329 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2330 | PyObject *return_value = NULL; |
| 2331 | static char *_keywords[] = {"sep", "maxsplit", NULL}; |
| 2332 | PyObject *sep = Py_None; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2333 | Py_ssize_t maxsplit = -1; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2334 | |
| 2335 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2336 | "|On:split", _keywords, |
| 2337 | &sep, &maxsplit)) |
| 2338 | goto exit; |
| 2339 | return_value = bytearray_split_impl(self, sep, maxsplit); |
| 2340 | |
| 2341 | exit: |
| 2342 | return return_value; |
| 2343 | } |
| 2344 | |
| 2345 | static PyObject * |
| 2346 | bytearray_split_impl(PyByteArrayObject *self, PyObject *sep, Py_ssize_t maxsplit) |
| 2347 | /*[clinic end generated code: output=062a3d87d6f918fa input=24f82669f41bf523]*/ |
| 2348 | { |
| 2349 | Py_ssize_t len = PyByteArray_GET_SIZE(self), n; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2350 | const char *s = PyByteArray_AS_STRING(self), *sub; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2351 | PyObject *list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2352 | Py_buffer vsub; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2353 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2354 | if (maxsplit < 0) |
| 2355 | maxsplit = PY_SSIZE_T_MAX; |
| 2356 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2357 | if (sep == Py_None) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2358 | return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2359 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2360 | if (_getbuffer(sep, &vsub) < 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2361 | return NULL; |
| 2362 | sub = vsub.buf; |
| 2363 | n = vsub.len; |
| 2364 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2365 | list = stringlib_split( |
| 2366 | (PyObject*) self, s, len, sub, n, maxsplit |
| 2367 | ); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2368 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2369 | return list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2372 | /*[clinic input] |
| 2373 | bytearray.partition |
| 2374 | |
| 2375 | self: self(type="PyByteArrayObject *") |
| 2376 | sep: object |
| 2377 | / |
| 2378 | |
| 2379 | Partition the bytearray into three parts using the given separator. |
| 2380 | |
| 2381 | This will search for the separator sep in the bytearray. If the separator is |
| 2382 | found, returns a 3-tuple containing the part before the separator, the |
| 2383 | separator itself, and the part after it. |
| 2384 | |
| 2385 | If the separator is not found, returns a 3-tuple containing the original |
| 2386 | bytearray object and two empty bytearray objects. |
| 2387 | [clinic start generated code]*/ |
| 2388 | |
| 2389 | PyDoc_STRVAR(bytearray_partition__doc__, |
| 2390 | "partition($self, sep, /)\n" |
| 2391 | "--\n" |
| 2392 | "\n" |
| 2393 | "Partition the bytearray into three parts using the given separator.\n" |
| 2394 | "\n" |
| 2395 | "This will search for the separator sep in the bytearray. If the separator is\n" |
| 2396 | "found, returns a 3-tuple containing the part before the separator, the\n" |
| 2397 | "separator itself, and the part after it.\n" |
| 2398 | "\n" |
| 2399 | "If the separator is not found, returns a 3-tuple containing the original\n" |
| 2400 | "bytearray object and two empty bytearray objects."); |
| 2401 | |
| 2402 | #define BYTEARRAY_PARTITION_METHODDEF \ |
| 2403 | {"partition", (PyCFunction)bytearray_partition, METH_O, bytearray_partition__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2404 | |
| 2405 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2406 | bytearray_partition(PyByteArrayObject *self, PyObject *sep) |
| 2407 | /*[clinic end generated code: output=2645138221fe6f4d input=7d7fe37b1696d506]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2408 | { |
| 2409 | PyObject *bytesep, *result; |
| 2410 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2411 | bytesep = PyByteArray_FromObject(sep); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2412 | if (! bytesep) |
| 2413 | return NULL; |
| 2414 | |
| 2415 | result = stringlib_partition( |
| 2416 | (PyObject*) self, |
| 2417 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 2418 | bytesep, |
| 2419 | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
| 2420 | ); |
| 2421 | |
| 2422 | Py_DECREF(bytesep); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2423 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2426 | /*[clinic input] |
| 2427 | bytearray.rpartition |
| 2428 | |
| 2429 | self: self(type="PyByteArrayObject *") |
| 2430 | sep: object |
| 2431 | / |
| 2432 | |
| 2433 | Partition the bytes into three parts using the given separator. |
| 2434 | |
| 2435 | This will search for the separator sep in the bytearray, starting and the end. |
| 2436 | If the separator is found, returns a 3-tuple containing the part before the |
| 2437 | separator, the separator itself, and the part after it. |
| 2438 | |
| 2439 | If the separator is not found, returns a 3-tuple containing two empty bytearray |
| 2440 | objects and the original bytearray object. |
| 2441 | [clinic start generated code]*/ |
| 2442 | |
| 2443 | PyDoc_STRVAR(bytearray_rpartition__doc__, |
| 2444 | "rpartition($self, sep, /)\n" |
| 2445 | "--\n" |
| 2446 | "\n" |
| 2447 | "Partition the bytes into three parts using the given separator.\n" |
| 2448 | "\n" |
| 2449 | "This will search for the separator sep in the bytearray, starting and the end.\n" |
| 2450 | "If the separator is found, returns a 3-tuple containing the part before the\n" |
| 2451 | "separator, the separator itself, and the part after it.\n" |
| 2452 | "\n" |
| 2453 | "If the separator is not found, returns a 3-tuple containing two empty bytearray\n" |
| 2454 | "objects and the original bytearray object."); |
| 2455 | |
| 2456 | #define BYTEARRAY_RPARTITION_METHODDEF \ |
| 2457 | {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, bytearray_rpartition__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2458 | |
| 2459 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2460 | bytearray_rpartition(PyByteArrayObject *self, PyObject *sep) |
| 2461 | /*[clinic end generated code: output=ed13e54605d007de input=9b8cd540c1b75853]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2462 | { |
| 2463 | PyObject *bytesep, *result; |
| 2464 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2465 | bytesep = PyByteArray_FromObject(sep); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2466 | if (! bytesep) |
| 2467 | return NULL; |
| 2468 | |
| 2469 | result = stringlib_rpartition( |
| 2470 | (PyObject*) self, |
| 2471 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 2472 | bytesep, |
| 2473 | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
| 2474 | ); |
| 2475 | |
| 2476 | Py_DECREF(bytesep); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2477 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2480 | /*[clinic input] |
| 2481 | bytearray.rsplit = bytearray.split |
| 2482 | |
| 2483 | Return a list of the sections in the bytearray, using sep as the delimiter. |
| 2484 | |
| 2485 | Splitting is done starting at the end of the bytearray and working to the front. |
| 2486 | [clinic start generated code]*/ |
| 2487 | |
| 2488 | PyDoc_STRVAR(bytearray_rsplit__doc__, |
| 2489 | "rsplit($self, /, sep=None, maxsplit=-1)\n" |
| 2490 | "--\n" |
| 2491 | "\n" |
| 2492 | "Return a list of the sections in the bytearray, using sep as the delimiter.\n" |
| 2493 | "\n" |
| 2494 | " sep\n" |
| 2495 | " The delimiter according which to split the bytearray.\n" |
| 2496 | " None (the default value) means split on ASCII whitespace characters\n" |
| 2497 | " (space, tab, return, newline, formfeed, vertical tab).\n" |
| 2498 | " maxsplit\n" |
| 2499 | " Maximum number of splits to do.\n" |
| 2500 | " -1 (the default value) means no limit.\n" |
| 2501 | "\n" |
| 2502 | "Splitting is done starting at the end of the bytearray and working to the front."); |
| 2503 | |
| 2504 | #define BYTEARRAY_RSPLIT_METHODDEF \ |
| 2505 | {"rsplit", (PyCFunction)bytearray_rsplit, METH_VARARGS|METH_KEYWORDS, bytearray_rsplit__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2506 | |
| 2507 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2508 | bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep, Py_ssize_t maxsplit); |
| 2509 | |
| 2510 | static PyObject * |
| 2511 | bytearray_rsplit(PyByteArrayObject *self, PyObject *args, PyObject *kwargs) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2512 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2513 | PyObject *return_value = NULL; |
| 2514 | static char *_keywords[] = {"sep", "maxsplit", NULL}; |
| 2515 | PyObject *sep = Py_None; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2516 | Py_ssize_t maxsplit = -1; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2517 | |
| 2518 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2519 | "|On:rsplit", _keywords, |
| 2520 | &sep, &maxsplit)) |
| 2521 | goto exit; |
| 2522 | return_value = bytearray_rsplit_impl(self, sep, maxsplit); |
| 2523 | |
| 2524 | exit: |
| 2525 | return return_value; |
| 2526 | } |
| 2527 | |
| 2528 | static PyObject * |
| 2529 | bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep, Py_ssize_t maxsplit) |
| 2530 | /*[clinic end generated code: output=affaf9fc2aae8d41 input=a68286e4dd692ffe]*/ |
| 2531 | { |
| 2532 | Py_ssize_t len = PyByteArray_GET_SIZE(self), n; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2533 | const char *s = PyByteArray_AS_STRING(self), *sub; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2534 | PyObject *list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2535 | Py_buffer vsub; |
| 2536 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2537 | if (maxsplit < 0) |
| 2538 | maxsplit = PY_SSIZE_T_MAX; |
| 2539 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2540 | if (sep == Py_None) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2541 | return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2542 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2543 | if (_getbuffer(sep, &vsub) < 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2544 | return NULL; |
| 2545 | sub = vsub.buf; |
| 2546 | n = vsub.len; |
| 2547 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2548 | list = stringlib_rsplit( |
| 2549 | (PyObject*) self, s, len, sub, n, maxsplit |
| 2550 | ); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2551 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2552 | return list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2555 | /*[clinic input] |
| 2556 | bytearray.reverse |
| 2557 | |
| 2558 | self: self(type="PyByteArrayObject *") |
| 2559 | |
| 2560 | Reverse the order of the values in B in place. |
| 2561 | [clinic start generated code]*/ |
| 2562 | |
| 2563 | PyDoc_STRVAR(bytearray_reverse__doc__, |
| 2564 | "reverse($self, /)\n" |
| 2565 | "--\n" |
| 2566 | "\n" |
| 2567 | "Reverse the order of the values in B in place."); |
| 2568 | |
| 2569 | #define BYTEARRAY_REVERSE_METHODDEF \ |
| 2570 | {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, bytearray_reverse__doc__}, |
| 2571 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2572 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2573 | bytearray_reverse_impl(PyByteArrayObject *self); |
| 2574 | |
| 2575 | static PyObject * |
| 2576 | bytearray_reverse(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored)) |
| 2577 | { |
| 2578 | return bytearray_reverse_impl(self); |
| 2579 | } |
| 2580 | |
| 2581 | static PyObject * |
| 2582 | bytearray_reverse_impl(PyByteArrayObject *self) |
| 2583 | /*[clinic end generated code: output=5d5e5f0bfc67f476 input=7933a499b8597bd1]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2584 | { |
| 2585 | char swap, *head, *tail; |
| 2586 | Py_ssize_t i, j, n = Py_SIZE(self); |
| 2587 | |
| 2588 | j = n / 2; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2589 | head = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2590 | tail = head + n - 1; |
| 2591 | for (i = 0; i < j; i++) { |
| 2592 | swap = *head; |
| 2593 | *head++ = *tail; |
| 2594 | *tail-- = swap; |
| 2595 | } |
| 2596 | |
| 2597 | Py_RETURN_NONE; |
| 2598 | } |
| 2599 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2600 | |
| 2601 | /*[python input] |
| 2602 | class bytesvalue_converter(CConverter): |
| 2603 | type = 'int' |
| 2604 | converter = '_getbytevalue' |
| 2605 | [python start generated code]*/ |
| 2606 | /*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/ |
| 2607 | |
| 2608 | |
| 2609 | /*[clinic input] |
| 2610 | bytearray.insert |
| 2611 | |
| 2612 | self: self(type="PyByteArrayObject *") |
| 2613 | index: Py_ssize_t |
| 2614 | The index where the value is to be inserted. |
| 2615 | item: bytesvalue |
| 2616 | The item to be inserted. |
| 2617 | / |
| 2618 | |
| 2619 | Insert a single item into the bytearray before the given index. |
| 2620 | [clinic start generated code]*/ |
| 2621 | |
| 2622 | PyDoc_STRVAR(bytearray_insert__doc__, |
| 2623 | "insert($self, index, item, /)\n" |
| 2624 | "--\n" |
| 2625 | "\n" |
| 2626 | "Insert a single item into the bytearray before the given index.\n" |
| 2627 | "\n" |
| 2628 | " index\n" |
| 2629 | " The index where the value is to be inserted.\n" |
| 2630 | " item\n" |
| 2631 | " The item to be inserted."); |
| 2632 | |
| 2633 | #define BYTEARRAY_INSERT_METHODDEF \ |
| 2634 | {"insert", (PyCFunction)bytearray_insert, METH_VARARGS, bytearray_insert__doc__}, |
| 2635 | |
| 2636 | static PyObject * |
| 2637 | bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item); |
| 2638 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2639 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2640 | bytearray_insert(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2641 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2642 | PyObject *return_value = NULL; |
| 2643 | Py_ssize_t index; |
| 2644 | int item; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2645 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2646 | if (!PyArg_ParseTuple(args, |
| 2647 | "nO&:insert", |
| 2648 | &index, _getbytevalue, &item)) |
| 2649 | goto exit; |
| 2650 | return_value = bytearray_insert_impl(self, index, item); |
| 2651 | |
| 2652 | exit: |
| 2653 | return return_value; |
| 2654 | } |
| 2655 | |
| 2656 | static PyObject * |
| 2657 | bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item) |
| 2658 | /*[clinic end generated code: output=5ec9340d4ad19080 input=833766836ba30e1e]*/ |
| 2659 | { |
| 2660 | Py_ssize_t n = Py_SIZE(self); |
| 2661 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2662 | |
| 2663 | if (n == PY_SSIZE_T_MAX) { |
| 2664 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 2665 | "cannot add more objects to bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2666 | return NULL; |
| 2667 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2668 | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
| 2669 | return NULL; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2670 | buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2671 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2672 | if (index < 0) { |
| 2673 | index += n; |
| 2674 | if (index < 0) |
| 2675 | index = 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2676 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2677 | if (index > n) |
| 2678 | index = n; |
| 2679 | memmove(buf + index + 1, buf + index, n - index); |
| 2680 | buf[index] = item; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2681 | |
| 2682 | Py_RETURN_NONE; |
| 2683 | } |
| 2684 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2685 | /*[clinic input] |
| 2686 | bytearray.append |
| 2687 | |
| 2688 | self: self(type="PyByteArrayObject *") |
| 2689 | item: bytesvalue |
| 2690 | The item to be appended. |
| 2691 | / |
| 2692 | |
| 2693 | Append a single item to the end of the bytearray. |
| 2694 | [clinic start generated code]*/ |
| 2695 | |
| 2696 | PyDoc_STRVAR(bytearray_append__doc__, |
| 2697 | "append($self, item, /)\n" |
| 2698 | "--\n" |
| 2699 | "\n" |
| 2700 | "Append a single item to the end of the bytearray.\n" |
| 2701 | "\n" |
| 2702 | " item\n" |
| 2703 | " The item to be appended."); |
| 2704 | |
| 2705 | #define BYTEARRAY_APPEND_METHODDEF \ |
| 2706 | {"append", (PyCFunction)bytearray_append, METH_VARARGS, bytearray_append__doc__}, |
| 2707 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2708 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2709 | bytearray_append_impl(PyByteArrayObject *self, int item); |
| 2710 | |
| 2711 | static PyObject * |
| 2712 | bytearray_append(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2713 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2714 | PyObject *return_value = NULL; |
| 2715 | int item; |
| 2716 | |
| 2717 | if (!PyArg_ParseTuple(args, |
| 2718 | "O&:append", |
| 2719 | _getbytevalue, &item)) |
| 2720 | goto exit; |
| 2721 | return_value = bytearray_append_impl(self, item); |
| 2722 | |
| 2723 | exit: |
| 2724 | return return_value; |
| 2725 | } |
| 2726 | |
| 2727 | static PyObject * |
| 2728 | bytearray_append_impl(PyByteArrayObject *self, int item) |
| 2729 | /*[clinic end generated code: output=b5b3325bb3bbaf85 input=ae56ea87380407cc]*/ |
| 2730 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2731 | Py_ssize_t n = Py_SIZE(self); |
| 2732 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2733 | if (n == PY_SSIZE_T_MAX) { |
| 2734 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 2735 | "cannot add more objects to bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2736 | return NULL; |
| 2737 | } |
| 2738 | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
| 2739 | return NULL; |
| 2740 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2741 | PyByteArray_AS_STRING(self)[n] = item; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2742 | |
| 2743 | Py_RETURN_NONE; |
| 2744 | } |
| 2745 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2746 | /*[clinic input] |
| 2747 | bytearray.extend |
| 2748 | |
| 2749 | self: self(type="PyByteArrayObject *") |
| 2750 | iterable_of_ints: object |
| 2751 | The iterable of items to append. |
| 2752 | / |
| 2753 | |
| 2754 | Append all the items from the iterator or sequence to the end of the bytearray. |
| 2755 | [clinic start generated code]*/ |
| 2756 | |
| 2757 | PyDoc_STRVAR(bytearray_extend__doc__, |
| 2758 | "extend($self, iterable_of_ints, /)\n" |
| 2759 | "--\n" |
| 2760 | "\n" |
| 2761 | "Append all the items from the iterator or sequence to the end of the bytearray.\n" |
| 2762 | "\n" |
| 2763 | " iterable_of_ints\n" |
| 2764 | " The iterable of items to append."); |
| 2765 | |
| 2766 | #define BYTEARRAY_EXTEND_METHODDEF \ |
| 2767 | {"extend", (PyCFunction)bytearray_extend, METH_O, bytearray_extend__doc__}, |
| 2768 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2769 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2770 | bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints) |
| 2771 | /*[clinic end generated code: output=13b0c13ad5110dfb input=ce83a5d75b70d850]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2772 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2773 | PyObject *it, *item, *bytearray_obj; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2774 | Py_ssize_t buf_size = 0, len = 0; |
| 2775 | int value; |
| 2776 | char *buf; |
| 2777 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2778 | /* bytearray_setslice code only accepts something supporting PEP 3118. */ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2779 | if (PyObject_CheckBuffer(iterable_of_ints)) { |
| 2780 | if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2781 | return NULL; |
| 2782 | |
| 2783 | Py_RETURN_NONE; |
| 2784 | } |
| 2785 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2786 | it = PyObject_GetIter(iterable_of_ints); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2787 | if (it == NULL) |
| 2788 | return NULL; |
| 2789 | |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 2790 | /* Try to determine the length of the argument. 32 is arbitrary. */ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2791 | buf_size = PyObject_LengthHint(iterable_of_ints, 32); |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2792 | if (buf_size == -1) { |
| 2793 | Py_DECREF(it); |
| 2794 | return NULL; |
| 2795 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2796 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2797 | bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size); |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 2798 | if (bytearray_obj == NULL) { |
| 2799 | Py_DECREF(it); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2800 | return NULL; |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 2801 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2802 | buf = PyByteArray_AS_STRING(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2803 | |
| 2804 | while ((item = PyIter_Next(it)) != NULL) { |
| 2805 | if (! _getbytevalue(item, &value)) { |
| 2806 | Py_DECREF(item); |
| 2807 | Py_DECREF(it); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2808 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2809 | return NULL; |
| 2810 | } |
| 2811 | buf[len++] = value; |
| 2812 | Py_DECREF(item); |
| 2813 | |
| 2814 | if (len >= buf_size) { |
| 2815 | buf_size = len + (len >> 1) + 1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2816 | if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2817 | Py_DECREF(it); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2818 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2819 | return NULL; |
| 2820 | } |
| 2821 | /* Recompute the `buf' pointer, since the resizing operation may |
| 2822 | have invalidated it. */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2823 | buf = PyByteArray_AS_STRING(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2824 | } |
| 2825 | } |
| 2826 | Py_DECREF(it); |
| 2827 | |
| 2828 | /* Resize down to exact size. */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2829 | if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) { |
| 2830 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2831 | return NULL; |
| 2832 | } |
| 2833 | |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 2834 | if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) { |
| 2835 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2836 | return NULL; |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 2837 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2838 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2839 | |
| 2840 | Py_RETURN_NONE; |
| 2841 | } |
| 2842 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2843 | /*[clinic input] |
| 2844 | bytearray.pop |
| 2845 | |
| 2846 | self: self(type="PyByteArrayObject *") |
| 2847 | index: Py_ssize_t = -1 |
| 2848 | The index from where to remove the item. |
| 2849 | -1 (the default value) means remove the last item. |
| 2850 | / |
| 2851 | |
| 2852 | Remove and return a single item from B. |
| 2853 | |
| 2854 | If no index argument is given, will pop the last item. |
| 2855 | [clinic start generated code]*/ |
| 2856 | |
| 2857 | PyDoc_STRVAR(bytearray_pop__doc__, |
| 2858 | "pop($self, index=-1, /)\n" |
| 2859 | "--\n" |
| 2860 | "\n" |
| 2861 | "Remove and return a single item from B.\n" |
| 2862 | "\n" |
| 2863 | " index\n" |
| 2864 | " The index from where to remove the item.\n" |
| 2865 | " -1 (the default value) means remove the last item.\n" |
| 2866 | "\n" |
| 2867 | "If no index argument is given, will pop the last item."); |
| 2868 | |
| 2869 | #define BYTEARRAY_POP_METHODDEF \ |
| 2870 | {"pop", (PyCFunction)bytearray_pop, METH_VARARGS, bytearray_pop__doc__}, |
| 2871 | |
| 2872 | static PyObject * |
| 2873 | bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index); |
| 2874 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2875 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2876 | bytearray_pop(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2877 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2878 | PyObject *return_value = NULL; |
| 2879 | Py_ssize_t index = -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2880 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2881 | if (!PyArg_ParseTuple(args, |
| 2882 | "|n:pop", |
| 2883 | &index)) |
| 2884 | goto exit; |
| 2885 | return_value = bytearray_pop_impl(self, index); |
| 2886 | |
| 2887 | exit: |
| 2888 | return return_value; |
| 2889 | } |
| 2890 | |
| 2891 | static PyObject * |
| 2892 | bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index) |
| 2893 | /*[clinic end generated code: output=3b763e548e79af96 input=0797e6c0ca9d5a85]*/ |
| 2894 | { |
| 2895 | int value; |
| 2896 | Py_ssize_t n = Py_SIZE(self); |
| 2897 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2898 | |
| 2899 | if (n == 0) { |
Eli Bendersky | 1bc4f19 | 2011-03-04 04:55:25 +0000 | [diff] [blame] | 2900 | PyErr_SetString(PyExc_IndexError, |
| 2901 | "pop from empty bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2902 | return NULL; |
| 2903 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2904 | if (index < 0) |
| 2905 | index += Py_SIZE(self); |
| 2906 | if (index < 0 || index >= Py_SIZE(self)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2907 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 2908 | return NULL; |
| 2909 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 2910 | if (!_canresize(self)) |
| 2911 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2912 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2913 | buf = PyByteArray_AS_STRING(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2914 | value = buf[index]; |
| 2915 | memmove(buf + index, buf + index + 1, n - index); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2916 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 2917 | return NULL; |
| 2918 | |
Mark Dickinson | 54a3db9 | 2009-09-06 10:19:23 +0000 | [diff] [blame] | 2919 | return PyLong_FromLong((unsigned char)value); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2922 | /*[clinic input] |
| 2923 | bytearray.remove |
| 2924 | |
| 2925 | self: self(type="PyByteArrayObject *") |
| 2926 | value: bytesvalue |
| 2927 | The value to remove. |
| 2928 | / |
| 2929 | |
| 2930 | Remove the first occurrence of a value in the bytearray. |
| 2931 | [clinic start generated code]*/ |
| 2932 | |
| 2933 | PyDoc_STRVAR(bytearray_remove__doc__, |
| 2934 | "remove($self, value, /)\n" |
| 2935 | "--\n" |
| 2936 | "\n" |
| 2937 | "Remove the first occurrence of a value in the bytearray.\n" |
| 2938 | "\n" |
| 2939 | " value\n" |
| 2940 | " The value to remove."); |
| 2941 | |
| 2942 | #define BYTEARRAY_REMOVE_METHODDEF \ |
| 2943 | {"remove", (PyCFunction)bytearray_remove, METH_VARARGS, bytearray_remove__doc__}, |
| 2944 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2945 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2946 | bytearray_remove_impl(PyByteArrayObject *self, int value); |
| 2947 | |
| 2948 | static PyObject * |
| 2949 | bytearray_remove(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2950 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2951 | PyObject *return_value = NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2952 | int value; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2953 | |
| 2954 | if (!PyArg_ParseTuple(args, |
| 2955 | "O&:remove", |
| 2956 | _getbytevalue, &value)) |
| 2957 | goto exit; |
| 2958 | return_value = bytearray_remove_impl(self, value); |
| 2959 | |
| 2960 | exit: |
| 2961 | return return_value; |
| 2962 | } |
| 2963 | |
| 2964 | static PyObject * |
| 2965 | bytearray_remove_impl(PyByteArrayObject *self, int value) |
| 2966 | /*[clinic end generated code: output=c71c8bcf4703abfc input=47560b11fd856c24]*/ |
| 2967 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2968 | Py_ssize_t where, n = Py_SIZE(self); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2969 | char *buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2970 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2971 | for (where = 0; where < n; where++) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2972 | if (buf[where] == value) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2973 | break; |
| 2974 | } |
| 2975 | if (where == n) { |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 2976 | PyErr_SetString(PyExc_ValueError, "value not found in bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2977 | return NULL; |
| 2978 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 2979 | if (!_canresize(self)) |
| 2980 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2981 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2982 | memmove(buf + where, buf + where + 1, n - where); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2983 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 2984 | return NULL; |
| 2985 | |
| 2986 | Py_RETURN_NONE; |
| 2987 | } |
| 2988 | |
| 2989 | /* XXX These two helpers could be optimized if argsize == 1 */ |
| 2990 | |
| 2991 | static Py_ssize_t |
Antoine Pitrou | 5b72075 | 2013-10-05 21:24:10 +0200 | [diff] [blame] | 2992 | lstrip_helper(char *myptr, Py_ssize_t mysize, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2993 | void *argptr, Py_ssize_t argsize) |
| 2994 | { |
| 2995 | Py_ssize_t i = 0; |
Antoine Pitrou | 5b72075 | 2013-10-05 21:24:10 +0200 | [diff] [blame] | 2996 | while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2997 | i++; |
| 2998 | return i; |
| 2999 | } |
| 3000 | |
| 3001 | static Py_ssize_t |
Antoine Pitrou | 5b72075 | 2013-10-05 21:24:10 +0200 | [diff] [blame] | 3002 | rstrip_helper(char *myptr, Py_ssize_t mysize, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3003 | void *argptr, Py_ssize_t argsize) |
| 3004 | { |
| 3005 | Py_ssize_t i = mysize - 1; |
Antoine Pitrou | 5b72075 | 2013-10-05 21:24:10 +0200 | [diff] [blame] | 3006 | while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3007 | i--; |
| 3008 | return i + 1; |
| 3009 | } |
| 3010 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3011 | /*[clinic input] |
| 3012 | bytearray.strip |
| 3013 | |
| 3014 | bytes: object = None |
| 3015 | / |
| 3016 | |
| 3017 | Strip leading and trailing bytes contained in the argument. |
| 3018 | |
| 3019 | If the argument is omitted or None, strip leading and trailing ASCII whitespace. |
| 3020 | [clinic start generated code]*/ |
| 3021 | |
| 3022 | PyDoc_STRVAR(bytearray_strip__doc__, |
| 3023 | "strip($self, bytes=None, /)\n" |
| 3024 | "--\n" |
| 3025 | "\n" |
| 3026 | "Strip leading and trailing bytes contained in the argument.\n" |
| 3027 | "\n" |
| 3028 | "If the argument is omitted or None, strip leading and trailing ASCII whitespace."); |
| 3029 | |
| 3030 | #define BYTEARRAY_STRIP_METHODDEF \ |
| 3031 | {"strip", (PyCFunction)bytearray_strip, METH_VARARGS, bytearray_strip__doc__}, |
| 3032 | |
| 3033 | static PyObject * |
| 3034 | bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes); |
| 3035 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3036 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3037 | bytearray_strip(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3038 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3039 | PyObject *return_value = NULL; |
| 3040 | PyObject *bytes = Py_None; |
| 3041 | |
| 3042 | if (!PyArg_UnpackTuple(args, "strip", |
| 3043 | 0, 1, |
| 3044 | &bytes)) |
| 3045 | goto exit; |
| 3046 | return_value = bytearray_strip_impl(self, bytes); |
| 3047 | |
| 3048 | exit: |
| 3049 | return return_value; |
| 3050 | } |
| 3051 | |
| 3052 | static PyObject * |
| 3053 | bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes) |
| 3054 | /*[clinic end generated code: output=2e3d3358acc4c235 input=ef7bb59b09c21d62]*/ |
| 3055 | { |
| 3056 | Py_ssize_t left, right, mysize, byteslen; |
| 3057 | char *myptr, *bytesptr; |
| 3058 | Py_buffer vbytes; |
| 3059 | |
| 3060 | if (bytes == Py_None) { |
| 3061 | bytesptr = "\t\n\r\f\v "; |
| 3062 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3063 | } |
| 3064 | else { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3065 | if (_getbuffer(bytes, &vbytes) < 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3066 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3067 | bytesptr = (char *) vbytes.buf; |
| 3068 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3069 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 3070 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3071 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3072 | left = lstrip_helper(myptr, mysize, bytesptr, byteslen); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3073 | if (left == mysize) |
| 3074 | right = left; |
| 3075 | else |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3076 | right = rstrip_helper(myptr, mysize, bytesptr, byteslen); |
| 3077 | if (bytes != Py_None) |
| 3078 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 3079 | return PyByteArray_FromStringAndSize(myptr + left, right - left); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3082 | /*[clinic input] |
| 3083 | bytearray.lstrip |
| 3084 | |
| 3085 | bytes: object = None |
| 3086 | / |
| 3087 | |
| 3088 | Strip leading bytes contained in the argument. |
| 3089 | |
| 3090 | If the argument is omitted or None, strip leading ASCII whitespace. |
| 3091 | [clinic start generated code]*/ |
| 3092 | |
| 3093 | PyDoc_STRVAR(bytearray_lstrip__doc__, |
| 3094 | "lstrip($self, bytes=None, /)\n" |
| 3095 | "--\n" |
| 3096 | "\n" |
| 3097 | "Strip leading bytes contained in the argument.\n" |
| 3098 | "\n" |
| 3099 | "If the argument is omitted or None, strip leading ASCII whitespace."); |
| 3100 | |
| 3101 | #define BYTEARRAY_LSTRIP_METHODDEF \ |
| 3102 | {"lstrip", (PyCFunction)bytearray_lstrip, METH_VARARGS, bytearray_lstrip__doc__}, |
| 3103 | |
| 3104 | static PyObject * |
| 3105 | bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes); |
| 3106 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3107 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3108 | bytearray_lstrip(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3109 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3110 | PyObject *return_value = NULL; |
| 3111 | PyObject *bytes = Py_None; |
| 3112 | |
| 3113 | if (!PyArg_UnpackTuple(args, "lstrip", |
| 3114 | 0, 1, |
| 3115 | &bytes)) |
| 3116 | goto exit; |
| 3117 | return_value = bytearray_lstrip_impl(self, bytes); |
| 3118 | |
| 3119 | exit: |
| 3120 | return return_value; |
| 3121 | } |
| 3122 | |
| 3123 | static PyObject * |
| 3124 | bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes) |
| 3125 | /*[clinic end generated code: output=2599309808a9ec02 input=80843f975dd7c480]*/ |
| 3126 | { |
| 3127 | Py_ssize_t left, right, mysize, byteslen; |
| 3128 | char *myptr, *bytesptr; |
| 3129 | Py_buffer vbytes; |
| 3130 | |
| 3131 | if (bytes == Py_None) { |
| 3132 | bytesptr = "\t\n\r\f\v "; |
| 3133 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3134 | } |
| 3135 | else { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3136 | if (_getbuffer(bytes, &vbytes) < 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3137 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3138 | bytesptr = (char *) vbytes.buf; |
| 3139 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3140 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 3141 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3142 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3143 | left = lstrip_helper(myptr, mysize, bytesptr, byteslen); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3144 | right = mysize; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3145 | if (bytes != Py_None) |
| 3146 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 3147 | return PyByteArray_FromStringAndSize(myptr + left, right - left); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3148 | } |
| 3149 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3150 | /*[clinic input] |
| 3151 | bytearray.rstrip |
| 3152 | |
| 3153 | bytes: object = None |
| 3154 | / |
| 3155 | |
| 3156 | Strip trailing bytes contained in the argument. |
| 3157 | |
| 3158 | If the argument is omitted or None, strip trailing ASCII whitespace. |
| 3159 | [clinic start generated code]*/ |
| 3160 | |
| 3161 | PyDoc_STRVAR(bytearray_rstrip__doc__, |
| 3162 | "rstrip($self, bytes=None, /)\n" |
| 3163 | "--\n" |
| 3164 | "\n" |
| 3165 | "Strip trailing bytes contained in the argument.\n" |
| 3166 | "\n" |
| 3167 | "If the argument is omitted or None, strip trailing ASCII whitespace."); |
| 3168 | |
| 3169 | #define BYTEARRAY_RSTRIP_METHODDEF \ |
| 3170 | {"rstrip", (PyCFunction)bytearray_rstrip, METH_VARARGS, bytearray_rstrip__doc__}, |
| 3171 | |
| 3172 | static PyObject * |
| 3173 | bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes); |
| 3174 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3175 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3176 | bytearray_rstrip(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3177 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3178 | PyObject *return_value = NULL; |
| 3179 | PyObject *bytes = Py_None; |
| 3180 | |
| 3181 | if (!PyArg_UnpackTuple(args, "rstrip", |
| 3182 | 0, 1, |
| 3183 | &bytes)) |
| 3184 | goto exit; |
| 3185 | return_value = bytearray_rstrip_impl(self, bytes); |
| 3186 | |
| 3187 | exit: |
| 3188 | return return_value; |
| 3189 | } |
| 3190 | |
| 3191 | static PyObject * |
| 3192 | bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes) |
| 3193 | /*[clinic end generated code: output=b5ca6259f4f4f2a3 input=e728b994954cfd91]*/ |
| 3194 | { |
| 3195 | Py_ssize_t right, mysize, byteslen; |
| 3196 | char *myptr, *bytesptr; |
| 3197 | Py_buffer vbytes; |
| 3198 | |
| 3199 | if (bytes == Py_None) { |
| 3200 | bytesptr = "\t\n\r\f\v "; |
| 3201 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3202 | } |
| 3203 | else { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3204 | if (_getbuffer(bytes, &vbytes) < 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3205 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3206 | bytesptr = (char *) vbytes.buf; |
| 3207 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3208 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 3209 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3210 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3211 | right = rstrip_helper(myptr, mysize, bytesptr, byteslen); |
| 3212 | if (bytes != Py_None) |
| 3213 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 3214 | return PyByteArray_FromStringAndSize(myptr, right); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3217 | /*[clinic input] |
| 3218 | bytearray.decode |
| 3219 | |
| 3220 | encoding: str(c_default="NULL") = 'utf-8' |
| 3221 | The encoding with which to decode the bytearray. |
| 3222 | errors: str(c_default="NULL") = 'strict' |
| 3223 | The error handling scheme to use for the handling of decoding errors. |
| 3224 | The default is 'strict' meaning that decoding errors raise a |
| 3225 | UnicodeDecodeError. Other possible values are 'ignore' and 'replace' |
| 3226 | as well as any other name registered with codecs.register_error that |
| 3227 | can handle UnicodeDecodeErrors. |
| 3228 | |
| 3229 | Decode the bytearray using the codec registered for encoding. |
| 3230 | [clinic start generated code]*/ |
| 3231 | |
| 3232 | PyDoc_STRVAR(bytearray_decode__doc__, |
| 3233 | "decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n" |
| 3234 | "--\n" |
| 3235 | "\n" |
| 3236 | "Decode the bytearray using the codec registered for encoding.\n" |
| 3237 | "\n" |
| 3238 | " encoding\n" |
| 3239 | " The encoding with which to decode the bytearray.\n" |
| 3240 | " errors\n" |
| 3241 | " The error handling scheme to use for the handling of decoding errors.\n" |
| 3242 | " The default is \'strict\' meaning that decoding errors raise a\n" |
| 3243 | " UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n" |
| 3244 | " as well as any other name registered with codecs.register_error that\n" |
| 3245 | " can handle UnicodeDecodeErrors."); |
| 3246 | |
| 3247 | #define BYTEARRAY_DECODE_METHODDEF \ |
| 3248 | {"decode", (PyCFunction)bytearray_decode, METH_VARARGS|METH_KEYWORDS, bytearray_decode__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3249 | |
| 3250 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3251 | bytearray_decode_impl(PyByteArrayObject *self, const char *encoding, const char *errors); |
| 3252 | |
| 3253 | static PyObject * |
| 3254 | bytearray_decode(PyByteArrayObject *self, PyObject *args, PyObject *kwargs) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3255 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3256 | PyObject *return_value = NULL; |
| 3257 | static char *_keywords[] = {"encoding", "errors", NULL}; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3258 | const char *encoding = NULL; |
| 3259 | const char *errors = NULL; |
| 3260 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3261 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3262 | "|ss:decode", _keywords, |
| 3263 | &encoding, &errors)) |
| 3264 | goto exit; |
| 3265 | return_value = bytearray_decode_impl(self, encoding, errors); |
| 3266 | |
| 3267 | exit: |
| 3268 | return return_value; |
| 3269 | } |
| 3270 | |
| 3271 | static PyObject * |
| 3272 | bytearray_decode_impl(PyByteArrayObject *self, const char *encoding, const char *errors) |
| 3273 | /*[clinic end generated code: output=38b83681f1e38a6c input=f28d8f903020257b]*/ |
| 3274 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3275 | if (encoding == NULL) |
| 3276 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3277 | return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | PyDoc_STRVAR(alloc_doc, |
| 3281 | "B.__alloc__() -> int\n\ |
| 3282 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 3283 | Return the number of bytes actually allocated."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3284 | |
| 3285 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3286 | bytearray_alloc(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3287 | { |
| 3288 | return PyLong_FromSsize_t(self->ob_alloc); |
| 3289 | } |
| 3290 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3291 | /*[clinic input] |
| 3292 | bytearray.join |
| 3293 | |
| 3294 | iterable_of_bytes: object |
| 3295 | / |
| 3296 | |
| 3297 | Concatenate any number of bytes/bytearray objects. |
| 3298 | |
| 3299 | The bytearray whose method is called is inserted in between each pair. |
| 3300 | |
| 3301 | The result is returned as a new bytearray object. |
| 3302 | [clinic start generated code]*/ |
| 3303 | |
| 3304 | PyDoc_STRVAR(bytearray_join__doc__, |
| 3305 | "join($self, iterable_of_bytes, /)\n" |
| 3306 | "--\n" |
| 3307 | "\n" |
| 3308 | "Concatenate any number of bytes/bytearray objects.\n" |
| 3309 | "\n" |
| 3310 | "The bytearray whose method is called is inserted in between each pair.\n" |
| 3311 | "\n" |
| 3312 | "The result is returned as a new bytearray object."); |
| 3313 | |
| 3314 | #define BYTEARRAY_JOIN_METHODDEF \ |
| 3315 | {"join", (PyCFunction)bytearray_join, METH_O, bytearray_join__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3316 | |
| 3317 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3318 | bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes) |
| 3319 | /*[clinic end generated code: output=544e7430032dfdf4 input=aba6b1f9b30fcb8e]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3320 | { |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3321 | return stringlib_bytes_join((PyObject*)self, iterable_of_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3322 | } |
| 3323 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3324 | /*[clinic input] |
| 3325 | bytearray.splitlines |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 3326 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3327 | keepends: int(py_default="False") = 0 |
| 3328 | |
| 3329 | Return a list of the lines in the bytearray, breaking at line boundaries. |
| 3330 | |
| 3331 | Line breaks are not included in the resulting list unless keepends is given and |
| 3332 | true. |
| 3333 | [clinic start generated code]*/ |
| 3334 | |
| 3335 | PyDoc_STRVAR(bytearray_splitlines__doc__, |
| 3336 | "splitlines($self, /, keepends=False)\n" |
| 3337 | "--\n" |
| 3338 | "\n" |
| 3339 | "Return a list of the lines in the bytearray, breaking at line boundaries.\n" |
| 3340 | "\n" |
| 3341 | "Line breaks are not included in the resulting list unless keepends is given and\n" |
| 3342 | "true."); |
| 3343 | |
| 3344 | #define BYTEARRAY_SPLITLINES_METHODDEF \ |
| 3345 | {"splitlines", (PyCFunction)bytearray_splitlines, METH_VARARGS|METH_KEYWORDS, bytearray_splitlines__doc__}, |
| 3346 | |
| 3347 | static PyObject * |
| 3348 | bytearray_splitlines_impl(PyByteArrayObject *self, int keepends); |
| 3349 | |
| 3350 | static PyObject * |
| 3351 | bytearray_splitlines(PyByteArrayObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 3352 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3353 | PyObject *return_value = NULL; |
| 3354 | static char *_keywords[] = {"keepends", NULL}; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 3355 | int keepends = 0; |
| 3356 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3357 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3358 | "|i:splitlines", _keywords, |
| 3359 | &keepends)) |
| 3360 | goto exit; |
| 3361 | return_value = bytearray_splitlines_impl(self, keepends); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 3362 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3363 | exit: |
| 3364 | return return_value; |
| 3365 | } |
| 3366 | |
| 3367 | static PyObject * |
| 3368 | bytearray_splitlines_impl(PyByteArrayObject *self, int keepends) |
| 3369 | /*[clinic end generated code: output=a837fd0512ad46ff input=36f0b25bc792f6c0]*/ |
| 3370 | { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 3371 | return stringlib_splitlines( |
| 3372 | (PyObject*) self, PyByteArray_AS_STRING(self), |
| 3373 | PyByteArray_GET_SIZE(self), keepends |
| 3374 | ); |
| 3375 | } |
| 3376 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3377 | static int |
Victor Stinner | 6430fd5 | 2011-09-29 04:02:13 +0200 | [diff] [blame] | 3378 | hex_digit_to_int(Py_UCS4 c) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3379 | { |
| 3380 | if (c >= 128) |
| 3381 | return -1; |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 3382 | if (Py_ISDIGIT(c)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3383 | return c - '0'; |
| 3384 | else { |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 3385 | if (Py_ISUPPER(c)) |
| 3386 | c = Py_TOLOWER(c); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3387 | if (c >= 'a' && c <= 'f') |
| 3388 | return c - 'a' + 10; |
| 3389 | } |
| 3390 | return -1; |
| 3391 | } |
| 3392 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3393 | /*[clinic input] |
| 3394 | @classmethod |
| 3395 | bytearray.fromhex |
| 3396 | |
| 3397 | cls: self(type="PyObject*") |
| 3398 | string: unicode |
| 3399 | / |
| 3400 | |
| 3401 | Create a bytearray object from a string of hexadecimal numbers. |
| 3402 | |
| 3403 | Spaces between two numbers are accepted. |
| 3404 | Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef') |
| 3405 | [clinic start generated code]*/ |
| 3406 | |
| 3407 | PyDoc_STRVAR(bytearray_fromhex__doc__, |
| 3408 | "fromhex($type, string, /)\n" |
| 3409 | "--\n" |
| 3410 | "\n" |
| 3411 | "Create a bytearray object from a string of hexadecimal numbers.\n" |
| 3412 | "\n" |
| 3413 | "Spaces between two numbers are accepted.\n" |
| 3414 | "Example: bytearray.fromhex(\'B9 01EF\') -> bytearray(b\'\\\\xb9\\\\x01\\\\xef\')"); |
| 3415 | |
| 3416 | #define BYTEARRAY_FROMHEX_METHODDEF \ |
| 3417 | {"fromhex", (PyCFunction)bytearray_fromhex, METH_VARARGS|METH_CLASS, bytearray_fromhex__doc__}, |
| 3418 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3419 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3420 | bytearray_fromhex_impl(PyObject*cls, PyObject *string); |
| 3421 | |
| 3422 | static PyObject * |
| 3423 | bytearray_fromhex(PyTypeObject *cls, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3424 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3425 | PyObject *return_value = NULL; |
| 3426 | PyObject *string; |
| 3427 | |
| 3428 | if (!PyArg_ParseTuple(args, |
| 3429 | "U:fromhex", |
| 3430 | &string)) |
| 3431 | goto exit; |
| 3432 | return_value = bytearray_fromhex_impl((PyObject*)cls, string); |
| 3433 | |
| 3434 | exit: |
| 3435 | return return_value; |
| 3436 | } |
| 3437 | |
| 3438 | static PyObject * |
| 3439 | bytearray_fromhex_impl(PyObject*cls, PyObject *string) |
| 3440 | /*[clinic end generated code: output=adc3c804a74e56d4 input=907bbd2d34d9367a]*/ |
| 3441 | { |
| 3442 | PyObject *newbytes; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3443 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3444 | Py_ssize_t hexlen, byteslen, i, j; |
| 3445 | int top, bot; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3446 | void *data; |
| 3447 | unsigned int kind; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3448 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3449 | assert(PyUnicode_Check(string)); |
| 3450 | if (PyUnicode_READY(string)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3451 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3452 | kind = PyUnicode_KIND(string); |
| 3453 | data = PyUnicode_DATA(string); |
| 3454 | hexlen = PyUnicode_GET_LENGTH(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3455 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3456 | byteslen = hexlen/2; /* This overestimates if there are spaces */ |
| 3457 | newbytes = PyByteArray_FromStringAndSize(NULL, byteslen); |
| 3458 | if (!newbytes) |
| 3459 | return NULL; |
| 3460 | buf = PyByteArray_AS_STRING(newbytes); |
| 3461 | for (i = j = 0; i < hexlen; i += 2) { |
| 3462 | /* skip over spaces in the input */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3463 | while (PyUnicode_READ(kind, data, i) == ' ') |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3464 | i++; |
| 3465 | if (i >= hexlen) |
| 3466 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3467 | top = hex_digit_to_int(PyUnicode_READ(kind, data, i)); |
| 3468 | bot = hex_digit_to_int(PyUnicode_READ(kind, data, i+1)); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3469 | if (top == -1 || bot == -1) { |
| 3470 | PyErr_Format(PyExc_ValueError, |
| 3471 | "non-hexadecimal number found in " |
| 3472 | "fromhex() arg at position %zd", i); |
| 3473 | goto error; |
| 3474 | } |
| 3475 | buf[j++] = (top << 4) + bot; |
| 3476 | } |
| 3477 | if (PyByteArray_Resize(newbytes, j) < 0) |
| 3478 | goto error; |
| 3479 | return newbytes; |
| 3480 | |
| 3481 | error: |
| 3482 | Py_DECREF(newbytes); |
| 3483 | return NULL; |
| 3484 | } |
| 3485 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3486 | |
| 3487 | static PyObject * |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 3488 | _common_reduce(PyByteArrayObject *self, int proto) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3489 | { |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 3490 | PyObject *dict; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3491 | _Py_IDENTIFIER(__dict__); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 3492 | char *buf; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3493 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3494 | dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3495 | if (dict == NULL) { |
| 3496 | PyErr_Clear(); |
| 3497 | dict = Py_None; |
| 3498 | Py_INCREF(dict); |
| 3499 | } |
| 3500 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 3501 | buf = PyByteArray_AS_STRING(self); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 3502 | if (proto < 3) { |
| 3503 | /* use str based reduction for backwards compatibility with Python 2.x */ |
| 3504 | PyObject *latin1; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 3505 | if (Py_SIZE(self)) |
| 3506 | latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 3507 | else |
| 3508 | latin1 = PyUnicode_FromString(""); |
| 3509 | return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict); |
| 3510 | } |
| 3511 | else { |
| 3512 | /* use more efficient byte based reduction */ |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 3513 | if (Py_SIZE(self)) { |
| 3514 | return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 3515 | } |
| 3516 | else { |
| 3517 | return Py_BuildValue("(O()N)", Py_TYPE(self), dict); |
| 3518 | } |
| 3519 | } |
| 3520 | } |
| 3521 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3522 | /*[clinic input] |
| 3523 | bytearray.__reduce__ as bytearray_reduce |
| 3524 | |
| 3525 | self: self(type="PyByteArrayObject *") |
| 3526 | |
| 3527 | Return state information for pickling. |
| 3528 | [clinic start generated code]*/ |
| 3529 | |
| 3530 | PyDoc_STRVAR(bytearray_reduce__doc__, |
| 3531 | "__reduce__($self, /)\n" |
| 3532 | "--\n" |
| 3533 | "\n" |
| 3534 | "Return state information for pickling."); |
| 3535 | |
| 3536 | #define BYTEARRAY_REDUCE_METHODDEF \ |
| 3537 | {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, bytearray_reduce__doc__}, |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 3538 | |
| 3539 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3540 | bytearray_reduce_impl(PyByteArrayObject *self); |
| 3541 | |
| 3542 | static PyObject * |
| 3543 | bytearray_reduce(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored)) |
| 3544 | { |
| 3545 | return bytearray_reduce_impl(self); |
| 3546 | } |
| 3547 | |
| 3548 | static PyObject * |
| 3549 | bytearray_reduce_impl(PyByteArrayObject *self) |
| 3550 | /*[clinic end generated code: output=b1b56fe87bf30fb0 input=fbb07de4d102a03a]*/ |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 3551 | { |
| 3552 | return _common_reduce(self, 2); |
| 3553 | } |
| 3554 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3555 | /*[clinic input] |
| 3556 | bytearray.__reduce_ex__ as bytearray_reduce_ex |
| 3557 | |
| 3558 | self: self(type="PyByteArrayObject *") |
| 3559 | proto: int = 0 |
| 3560 | / |
| 3561 | |
| 3562 | Return state information for pickling. |
| 3563 | [clinic start generated code]*/ |
| 3564 | |
| 3565 | PyDoc_STRVAR(bytearray_reduce_ex__doc__, |
| 3566 | "__reduce_ex__($self, proto=0, /)\n" |
| 3567 | "--\n" |
| 3568 | "\n" |
| 3569 | "Return state information for pickling."); |
| 3570 | |
| 3571 | #define BYTEARRAY_REDUCE_EX_METHODDEF \ |
| 3572 | {"__reduce_ex__", (PyCFunction)bytearray_reduce_ex, METH_VARARGS, bytearray_reduce_ex__doc__}, |
| 3573 | |
| 3574 | static PyObject * |
| 3575 | bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 3576 | |
| 3577 | static PyObject * |
| 3578 | bytearray_reduce_ex(PyByteArrayObject *self, PyObject *args) |
| 3579 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3580 | PyObject *return_value = NULL; |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 3581 | int proto = 0; |
| 3582 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3583 | if (!PyArg_ParseTuple(args, |
| 3584 | "|i:__reduce_ex__", |
| 3585 | &proto)) |
| 3586 | goto exit; |
| 3587 | return_value = bytearray_reduce_ex_impl(self, proto); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 3588 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3589 | exit: |
| 3590 | return return_value; |
| 3591 | } |
| 3592 | |
| 3593 | static PyObject * |
| 3594 | bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto) |
| 3595 | /*[clinic end generated code: output=bbd9afb2f5953dc1 input=0e091a42ca6dbd91]*/ |
| 3596 | { |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 3597 | return _common_reduce(self, proto); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3598 | } |
| 3599 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3600 | /*[clinic input] |
| 3601 | bytearray.__sizeof__ as bytearray_sizeof |
| 3602 | |
| 3603 | self: self(type="PyByteArrayObject *") |
| 3604 | |
| 3605 | Returns the size of the bytearray object in memory, in bytes. |
| 3606 | [clinic start generated code]*/ |
| 3607 | |
| 3608 | PyDoc_STRVAR(bytearray_sizeof__doc__, |
| 3609 | "__sizeof__($self, /)\n" |
| 3610 | "--\n" |
| 3611 | "\n" |
| 3612 | "Returns the size of the bytearray object in memory, in bytes."); |
| 3613 | |
| 3614 | #define BYTEARRAY_SIZEOF_METHODDEF \ |
| 3615 | {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, bytearray_sizeof__doc__}, |
| 3616 | |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 3617 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3618 | bytearray_sizeof_impl(PyByteArrayObject *self); |
| 3619 | |
| 3620 | static PyObject * |
| 3621 | bytearray_sizeof(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored)) |
| 3622 | { |
| 3623 | return bytearray_sizeof_impl(self); |
| 3624 | } |
| 3625 | |
| 3626 | static PyObject * |
| 3627 | bytearray_sizeof_impl(PyByteArrayObject *self) |
| 3628 | /*[clinic end generated code: output=4a2254b0a85630c6 input=6b23d305362b462b]*/ |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 3629 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 3630 | Py_ssize_t res; |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 3631 | |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 3632 | res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char); |
| 3633 | return PyLong_FromSsize_t(res); |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 3634 | } |
| 3635 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3636 | static PySequenceMethods bytearray_as_sequence = { |
| 3637 | (lenfunc)bytearray_length, /* sq_length */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3638 | (binaryfunc)PyByteArray_Concat, /* sq_concat */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3639 | (ssizeargfunc)bytearray_repeat, /* sq_repeat */ |
| 3640 | (ssizeargfunc)bytearray_getitem, /* sq_item */ |
| 3641 | 0, /* sq_slice */ |
| 3642 | (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */ |
| 3643 | 0, /* sq_ass_slice */ |
| 3644 | (objobjproc)bytearray_contains, /* sq_contains */ |
| 3645 | (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */ |
| 3646 | (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3647 | }; |
| 3648 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3649 | static PyMappingMethods bytearray_as_mapping = { |
| 3650 | (lenfunc)bytearray_length, |
| 3651 | (binaryfunc)bytearray_subscript, |
| 3652 | (objobjargproc)bytearray_ass_subscript, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3653 | }; |
| 3654 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3655 | static PyBufferProcs bytearray_as_buffer = { |
| 3656 | (getbufferproc)bytearray_getbuffer, |
| 3657 | (releasebufferproc)bytearray_releasebuffer, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3658 | }; |
| 3659 | |
| 3660 | static PyMethodDef |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3661 | bytearray_methods[] = { |
| 3662 | {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3663 | BYTEARRAY_REDUCE_METHODDEF |
| 3664 | BYTEARRAY_REDUCE_EX_METHODDEF |
| 3665 | BYTEARRAY_SIZEOF_METHODDEF |
| 3666 | BYTEARRAY_APPEND_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3667 | {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, |
| 3668 | _Py_capitalize__doc__}, |
| 3669 | {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3670 | BYTEARRAY_CLEAR_METHODDEF |
| 3671 | BYTEARRAY_COPY_METHODDEF |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3672 | {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3673 | BYTEARRAY_DECODE_METHODDEF |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3674 | {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 3675 | {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3676 | expandtabs__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3677 | BYTEARRAY_EXTEND_METHODDEF |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3678 | {"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3679 | BYTEARRAY_FROMHEX_METHODDEF |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3680 | {"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3681 | BYTEARRAY_INSERT_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3682 | {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, |
| 3683 | _Py_isalnum__doc__}, |
| 3684 | {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, |
| 3685 | _Py_isalpha__doc__}, |
| 3686 | {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, |
| 3687 | _Py_isdigit__doc__}, |
| 3688 | {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, |
| 3689 | _Py_islower__doc__}, |
| 3690 | {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, |
| 3691 | _Py_isspace__doc__}, |
| 3692 | {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, |
| 3693 | _Py_istitle__doc__}, |
| 3694 | {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, |
| 3695 | _Py_isupper__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3696 | BYTEARRAY_JOIN_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3697 | {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__}, |
| 3698 | {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3699 | BYTEARRAY_LSTRIP_METHODDEF |
| 3700 | BYTEARRAY_MAKETRANS_METHODDEF |
| 3701 | BYTEARRAY_PARTITION_METHODDEF |
| 3702 | BYTEARRAY_POP_METHODDEF |
| 3703 | BYTEARRAY_REMOVE_METHODDEF |
| 3704 | BYTEARRAY_REPLACE_METHODDEF |
| 3705 | BYTEARRAY_REVERSE_METHODDEF |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3706 | {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__}, |
| 3707 | {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3708 | {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3709 | BYTEARRAY_RPARTITION_METHODDEF |
| 3710 | BYTEARRAY_RSPLIT_METHODDEF |
| 3711 | BYTEARRAY_RSTRIP_METHODDEF |
| 3712 | BYTEARRAY_SPLIT_METHODDEF |
| 3713 | BYTEARRAY_SPLITLINES_METHODDEF |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3714 | {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS , |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3715 | startswith__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3716 | BYTEARRAY_STRIP_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3717 | {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, |
| 3718 | _Py_swapcase__doc__}, |
| 3719 | {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3720 | BYTEARRAY_TRANSLATE_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3721 | {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
| 3722 | {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__}, |
| 3723 | {NULL} |
| 3724 | }; |
| 3725 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3726 | PyDoc_STRVAR(bytearray_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 3727 | "bytearray(iterable_of_ints) -> bytearray\n\ |
| 3728 | bytearray(string, encoding[, errors]) -> bytearray\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 3729 | bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\ |
| 3730 | bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\ |
| 3731 | bytearray() -> empty bytes array\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3732 | \n\ |
| 3733 | Construct an mutable bytearray object from:\n\ |
| 3734 | - an iterable yielding integers in range(256)\n\ |
| 3735 | - a text string encoded using the specified encoding\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 3736 | - a bytes or a buffer object\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3737 | - any object implementing the buffer API.\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 3738 | - an integer"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3739 | |
| 3740 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3741 | static PyObject *bytearray_iter(PyObject *seq); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3742 | |
| 3743 | PyTypeObject PyByteArray_Type = { |
| 3744 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3745 | "bytearray", |
| 3746 | sizeof(PyByteArrayObject), |
| 3747 | 0, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3748 | (destructor)bytearray_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3749 | 0, /* tp_print */ |
| 3750 | 0, /* tp_getattr */ |
| 3751 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 3752 | 0, /* tp_reserved */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3753 | (reprfunc)bytearray_repr, /* tp_repr */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3754 | 0, /* tp_as_number */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3755 | &bytearray_as_sequence, /* tp_as_sequence */ |
| 3756 | &bytearray_as_mapping, /* tp_as_mapping */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3757 | 0, /* tp_hash */ |
| 3758 | 0, /* tp_call */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3759 | bytearray_str, /* tp_str */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3760 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3761 | 0, /* tp_setattro */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3762 | &bytearray_as_buffer, /* tp_as_buffer */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3763 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3764 | bytearray_doc, /* tp_doc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3765 | 0, /* tp_traverse */ |
| 3766 | 0, /* tp_clear */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3767 | (richcmpfunc)bytearray_richcompare, /* tp_richcompare */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3768 | 0, /* tp_weaklistoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3769 | bytearray_iter, /* tp_iter */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3770 | 0, /* tp_iternext */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3771 | bytearray_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3772 | 0, /* tp_members */ |
| 3773 | 0, /* tp_getset */ |
| 3774 | 0, /* tp_base */ |
| 3775 | 0, /* tp_dict */ |
| 3776 | 0, /* tp_descr_get */ |
| 3777 | 0, /* tp_descr_set */ |
| 3778 | 0, /* tp_dictoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3779 | (initproc)bytearray_init, /* tp_init */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3780 | PyType_GenericAlloc, /* tp_alloc */ |
| 3781 | PyType_GenericNew, /* tp_new */ |
| 3782 | PyObject_Del, /* tp_free */ |
| 3783 | }; |
| 3784 | |
| 3785 | /*********************** Bytes Iterator ****************************/ |
| 3786 | |
| 3787 | typedef struct { |
| 3788 | PyObject_HEAD |
| 3789 | Py_ssize_t it_index; |
| 3790 | PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 3791 | } bytesiterobject; |
| 3792 | |
| 3793 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3794 | bytearrayiter_dealloc(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3795 | { |
| 3796 | _PyObject_GC_UNTRACK(it); |
| 3797 | Py_XDECREF(it->it_seq); |
| 3798 | PyObject_GC_Del(it); |
| 3799 | } |
| 3800 | |
| 3801 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3802 | bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3803 | { |
| 3804 | Py_VISIT(it->it_seq); |
| 3805 | return 0; |
| 3806 | } |
| 3807 | |
| 3808 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3809 | bytearrayiter_next(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3810 | { |
| 3811 | PyByteArrayObject *seq; |
| 3812 | PyObject *item; |
| 3813 | |
| 3814 | assert(it != NULL); |
| 3815 | seq = it->it_seq; |
| 3816 | if (seq == NULL) |
| 3817 | return NULL; |
| 3818 | assert(PyByteArray_Check(seq)); |
| 3819 | |
| 3820 | if (it->it_index < PyByteArray_GET_SIZE(seq)) { |
| 3821 | item = PyLong_FromLong( |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 3822 | (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3823 | if (item != NULL) |
| 3824 | ++it->it_index; |
| 3825 | return item; |
| 3826 | } |
| 3827 | |
| 3828 | Py_DECREF(seq); |
| 3829 | it->it_seq = NULL; |
| 3830 | return NULL; |
| 3831 | } |
| 3832 | |
| 3833 | static PyObject * |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3834 | bytearrayiter_length_hint(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3835 | { |
| 3836 | Py_ssize_t len = 0; |
| 3837 | if (it->it_seq) |
| 3838 | len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index; |
| 3839 | return PyLong_FromSsize_t(len); |
| 3840 | } |
| 3841 | |
| 3842 | PyDoc_STRVAR(length_hint_doc, |
| 3843 | "Private method returning an estimate of len(list(it))."); |
| 3844 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3845 | static PyObject * |
| 3846 | bytearrayiter_reduce(bytesiterobject *it) |
| 3847 | { |
| 3848 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 3849 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3850 | it->it_seq, it->it_index); |
| 3851 | } else { |
| 3852 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 3853 | if (u == NULL) |
| 3854 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 3855 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3856 | } |
| 3857 | } |
| 3858 | |
| 3859 | static PyObject * |
| 3860 | bytearrayiter_setstate(bytesiterobject *it, PyObject *state) |
| 3861 | { |
| 3862 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 3863 | if (index == -1 && PyErr_Occurred()) |
| 3864 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 3865 | if (it->it_seq != NULL) { |
| 3866 | if (index < 0) |
| 3867 | index = 0; |
| 3868 | else if (index > PyByteArray_GET_SIZE(it->it_seq)) |
| 3869 | index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */ |
| 3870 | it->it_index = index; |
| 3871 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3872 | Py_RETURN_NONE; |
| 3873 | } |
| 3874 | |
| 3875 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 3876 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3877 | static PyMethodDef bytearrayiter_methods[] = { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3878 | {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3879 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3880 | {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3881 | bytearray_reduce__doc__}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3882 | {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O, |
| 3883 | setstate_doc}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3884 | {NULL, NULL} /* sentinel */ |
| 3885 | }; |
| 3886 | |
| 3887 | PyTypeObject PyByteArrayIter_Type = { |
| 3888 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3889 | "bytearray_iterator", /* tp_name */ |
| 3890 | sizeof(bytesiterobject), /* tp_basicsize */ |
| 3891 | 0, /* tp_itemsize */ |
| 3892 | /* methods */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3893 | (destructor)bytearrayiter_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3894 | 0, /* tp_print */ |
| 3895 | 0, /* tp_getattr */ |
| 3896 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 3897 | 0, /* tp_reserved */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3898 | 0, /* tp_repr */ |
| 3899 | 0, /* tp_as_number */ |
| 3900 | 0, /* tp_as_sequence */ |
| 3901 | 0, /* tp_as_mapping */ |
| 3902 | 0, /* tp_hash */ |
| 3903 | 0, /* tp_call */ |
| 3904 | 0, /* tp_str */ |
| 3905 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3906 | 0, /* tp_setattro */ |
| 3907 | 0, /* tp_as_buffer */ |
| 3908 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 3909 | 0, /* tp_doc */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3910 | (traverseproc)bytearrayiter_traverse, /* tp_traverse */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3911 | 0, /* tp_clear */ |
| 3912 | 0, /* tp_richcompare */ |
| 3913 | 0, /* tp_weaklistoffset */ |
| 3914 | PyObject_SelfIter, /* tp_iter */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3915 | (iternextfunc)bytearrayiter_next, /* tp_iternext */ |
| 3916 | bytearrayiter_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3917 | 0, |
| 3918 | }; |
| 3919 | |
| 3920 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3921 | bytearray_iter(PyObject *seq) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3922 | { |
| 3923 | bytesiterobject *it; |
| 3924 | |
| 3925 | if (!PyByteArray_Check(seq)) { |
| 3926 | PyErr_BadInternalCall(); |
| 3927 | return NULL; |
| 3928 | } |
| 3929 | it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type); |
| 3930 | if (it == NULL) |
| 3931 | return NULL; |
| 3932 | it->it_index = 0; |
| 3933 | Py_INCREF(seq); |
| 3934 | it->it_seq = (PyByteArrayObject *)seq; |
| 3935 | _PyObject_GC_TRACK(it); |
| 3936 | return (PyObject *)it; |
| 3937 | } |