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 | |
| 8 | static PyByteArrayObject *nullbytes = NULL; |
Antoine Pitrou | 20d6c15 | 2010-01-17 12:43:00 +0000 | [diff] [blame] | 9 | char _PyByteArray_empty_string[] = ""; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 10 | |
| 11 | void |
| 12 | PyByteArray_Fini(void) |
| 13 | { |
| 14 | Py_CLEAR(nullbytes); |
| 15 | } |
| 16 | |
| 17 | int |
| 18 | PyByteArray_Init(void) |
| 19 | { |
| 20 | nullbytes = PyObject_New(PyByteArrayObject, &PyByteArray_Type); |
| 21 | if (nullbytes == NULL) |
| 22 | return 0; |
| 23 | nullbytes->ob_bytes = NULL; |
| 24 | Py_SIZE(nullbytes) = nullbytes->ob_alloc = 0; |
| 25 | nullbytes->ob_exports = 0; |
| 26 | return 1; |
| 27 | } |
| 28 | |
| 29 | /* end nullbytes support */ |
| 30 | |
| 31 | /* Helpers */ |
| 32 | |
| 33 | static int |
| 34 | _getbytevalue(PyObject* arg, int *value) |
| 35 | { |
| 36 | long face_value; |
| 37 | |
| 38 | if (PyLong_Check(arg)) { |
| 39 | face_value = PyLong_AsLong(arg); |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 40 | } else { |
| 41 | PyObject *index = PyNumber_Index(arg); |
| 42 | if (index == NULL) { |
| 43 | PyErr_Format(PyExc_TypeError, "an integer is required"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 44 | return 0; |
| 45 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 46 | face_value = PyLong_AsLong(index); |
| 47 | Py_DECREF(index); |
| 48 | } |
| 49 | |
| 50 | if (face_value < 0 || face_value >= 256) { |
| 51 | /* this includes the OverflowError in case the long is too large */ |
| 52 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | *value = face_value; |
| 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 61 | bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 62 | { |
| 63 | int ret; |
| 64 | void *ptr; |
| 65 | if (view == NULL) { |
| 66 | obj->ob_exports++; |
| 67 | return 0; |
| 68 | } |
Antoine Pitrou | 20d6c15 | 2010-01-17 12:43:00 +0000 | [diff] [blame] | 69 | ptr = (void *) PyByteArray_AS_STRING(obj); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 70 | ret = PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 71 | if (ret >= 0) { |
| 72 | obj->ob_exports++; |
| 73 | } |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 78 | bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 79 | { |
| 80 | obj->ob_exports--; |
| 81 | } |
| 82 | |
| 83 | static Py_ssize_t |
| 84 | _getbuffer(PyObject *obj, Py_buffer *view) |
| 85 | { |
| 86 | PyBufferProcs *buffer = Py_TYPE(obj)->tp_as_buffer; |
| 87 | |
| 88 | if (buffer == NULL || buffer->bf_getbuffer == NULL) |
| 89 | { |
| 90 | PyErr_Format(PyExc_TypeError, |
| 91 | "Type %.100s doesn't support the buffer API", |
| 92 | Py_TYPE(obj)->tp_name); |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0) |
| 97 | return -1; |
| 98 | return view->len; |
| 99 | } |
| 100 | |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 101 | static int |
| 102 | _canresize(PyByteArrayObject *self) |
| 103 | { |
| 104 | if (self->ob_exports > 0) { |
| 105 | PyErr_SetString(PyExc_BufferError, |
| 106 | "Existing exports of data: object cannot be re-sized"); |
| 107 | return 0; |
| 108 | } |
| 109 | return 1; |
| 110 | } |
| 111 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 112 | /* Direct API functions */ |
| 113 | |
| 114 | PyObject * |
| 115 | PyByteArray_FromObject(PyObject *input) |
| 116 | { |
| 117 | return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type, |
| 118 | input, NULL); |
| 119 | } |
| 120 | |
| 121 | PyObject * |
| 122 | PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size) |
| 123 | { |
| 124 | PyByteArrayObject *new; |
| 125 | Py_ssize_t alloc; |
| 126 | |
| 127 | if (size < 0) { |
| 128 | PyErr_SetString(PyExc_SystemError, |
| 129 | "Negative size passed to PyByteArray_FromStringAndSize"); |
| 130 | return NULL; |
| 131 | } |
| 132 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 133 | /* Prevent buffer overflow when setting alloc to size+1. */ |
| 134 | if (size == PY_SSIZE_T_MAX) { |
| 135 | return PyErr_NoMemory(); |
| 136 | } |
| 137 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 138 | new = PyObject_New(PyByteArrayObject, &PyByteArray_Type); |
| 139 | if (new == NULL) |
| 140 | return NULL; |
| 141 | |
| 142 | if (size == 0) { |
| 143 | new->ob_bytes = NULL; |
| 144 | alloc = 0; |
| 145 | } |
| 146 | else { |
| 147 | alloc = size + 1; |
| 148 | new->ob_bytes = PyMem_Malloc(alloc); |
| 149 | if (new->ob_bytes == NULL) { |
| 150 | Py_DECREF(new); |
| 151 | return PyErr_NoMemory(); |
| 152 | } |
Antoine Pitrou | 20d6c15 | 2010-01-17 12:43:00 +0000 | [diff] [blame] | 153 | if (bytes != NULL && size > 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 154 | memcpy(new->ob_bytes, bytes, size); |
| 155 | new->ob_bytes[size] = '\0'; /* Trailing null byte */ |
| 156 | } |
| 157 | Py_SIZE(new) = size; |
| 158 | new->ob_alloc = alloc; |
| 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 |
| 183 | PyByteArray_Resize(PyObject *self, Py_ssize_t size) |
| 184 | { |
| 185 | void *sval; |
| 186 | Py_ssize_t alloc = ((PyByteArrayObject *)self)->ob_alloc; |
| 187 | |
| 188 | assert(self != NULL); |
| 189 | assert(PyByteArray_Check(self)); |
| 190 | assert(size >= 0); |
| 191 | |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 192 | if (size == Py_SIZE(self)) { |
| 193 | return 0; |
| 194 | } |
| 195 | if (!_canresize((PyByteArrayObject *)self)) { |
| 196 | return -1; |
| 197 | } |
| 198 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 199 | if (size < alloc / 2) { |
| 200 | /* Major downsize; resize down to exact size */ |
| 201 | alloc = size + 1; |
| 202 | } |
| 203 | else if (size < alloc) { |
| 204 | /* Within allocated size; quick exit */ |
| 205 | Py_SIZE(self) = size; |
| 206 | ((PyByteArrayObject *)self)->ob_bytes[size] = '\0'; /* Trailing null */ |
| 207 | return 0; |
| 208 | } |
| 209 | else if (size <= alloc * 1.125) { |
| 210 | /* Moderate upsize; overallocate similar to list_resize() */ |
| 211 | alloc = size + (size >> 3) + (size < 9 ? 3 : 6); |
| 212 | } |
| 213 | else { |
| 214 | /* Major upsize; resize up to exact size */ |
| 215 | alloc = size + 1; |
| 216 | } |
| 217 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 218 | sval = PyMem_Realloc(((PyByteArrayObject *)self)->ob_bytes, alloc); |
| 219 | if (sval == NULL) { |
| 220 | PyErr_NoMemory(); |
| 221 | return -1; |
| 222 | } |
| 223 | |
| 224 | ((PyByteArrayObject *)self)->ob_bytes = sval; |
| 225 | Py_SIZE(self) = size; |
| 226 | ((PyByteArrayObject *)self)->ob_alloc = alloc; |
| 227 | ((PyByteArrayObject *)self)->ob_bytes[size] = '\0'; /* Trailing null byte */ |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | PyObject * |
| 233 | PyByteArray_Concat(PyObject *a, PyObject *b) |
| 234 | { |
| 235 | Py_ssize_t size; |
| 236 | Py_buffer va, vb; |
| 237 | PyByteArrayObject *result = NULL; |
| 238 | |
| 239 | va.len = -1; |
| 240 | vb.len = -1; |
| 241 | if (_getbuffer(a, &va) < 0 || |
| 242 | _getbuffer(b, &vb) < 0) { |
| 243 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 244 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 245 | goto done; |
| 246 | } |
| 247 | |
| 248 | size = va.len + vb.len; |
| 249 | if (size < 0) { |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 250 | PyErr_NoMemory(); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 251 | goto done; |
| 252 | } |
| 253 | |
| 254 | result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, size); |
| 255 | if (result != NULL) { |
| 256 | memcpy(result->ob_bytes, va.buf, va.len); |
| 257 | memcpy(result->ob_bytes + va.len, vb.buf, vb.len); |
| 258 | } |
| 259 | |
| 260 | done: |
| 261 | if (va.len != -1) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 262 | PyBuffer_Release(&va); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 263 | if (vb.len != -1) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 264 | PyBuffer_Release(&vb); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 265 | return (PyObject *)result; |
| 266 | } |
| 267 | |
| 268 | /* Functions stuffed into the type object */ |
| 269 | |
| 270 | static Py_ssize_t |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 271 | bytearray_length(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 272 | { |
| 273 | return Py_SIZE(self); |
| 274 | } |
| 275 | |
| 276 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 277 | bytearray_iconcat(PyByteArrayObject *self, PyObject *other) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 278 | { |
| 279 | Py_ssize_t mysize; |
| 280 | Py_ssize_t size; |
| 281 | Py_buffer vo; |
| 282 | |
| 283 | if (_getbuffer(other, &vo) < 0) { |
| 284 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 285 | Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name); |
| 286 | return NULL; |
| 287 | } |
| 288 | |
| 289 | mysize = Py_SIZE(self); |
| 290 | size = mysize + vo.len; |
| 291 | if (size < 0) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 292 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 293 | return PyErr_NoMemory(); |
| 294 | } |
| 295 | if (size < self->ob_alloc) { |
| 296 | Py_SIZE(self) = size; |
| 297 | self->ob_bytes[Py_SIZE(self)] = '\0'; /* Trailing null byte */ |
| 298 | } |
| 299 | else if (PyByteArray_Resize((PyObject *)self, size) < 0) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 300 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 301 | return NULL; |
| 302 | } |
| 303 | memcpy(self->ob_bytes + mysize, vo.buf, vo.len); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 304 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 305 | Py_INCREF(self); |
| 306 | return (PyObject *)self; |
| 307 | } |
| 308 | |
| 309 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 310 | bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 311 | { |
| 312 | PyByteArrayObject *result; |
| 313 | Py_ssize_t mysize; |
| 314 | Py_ssize_t size; |
| 315 | |
| 316 | if (count < 0) |
| 317 | count = 0; |
| 318 | mysize = Py_SIZE(self); |
| 319 | size = mysize * count; |
| 320 | if (count != 0 && size / count != mysize) |
| 321 | return PyErr_NoMemory(); |
| 322 | result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size); |
| 323 | if (result != NULL && size != 0) { |
| 324 | if (mysize == 1) |
| 325 | memset(result->ob_bytes, self->ob_bytes[0], size); |
| 326 | else { |
| 327 | Py_ssize_t i; |
| 328 | for (i = 0; i < count; i++) |
| 329 | memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize); |
| 330 | } |
| 331 | } |
| 332 | return (PyObject *)result; |
| 333 | } |
| 334 | |
| 335 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 336 | bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 337 | { |
| 338 | Py_ssize_t mysize; |
| 339 | Py_ssize_t size; |
| 340 | |
| 341 | if (count < 0) |
| 342 | count = 0; |
| 343 | mysize = Py_SIZE(self); |
| 344 | size = mysize * count; |
| 345 | if (count != 0 && size / count != mysize) |
| 346 | return PyErr_NoMemory(); |
| 347 | if (size < self->ob_alloc) { |
| 348 | Py_SIZE(self) = size; |
| 349 | self->ob_bytes[Py_SIZE(self)] = '\0'; /* Trailing null byte */ |
| 350 | } |
| 351 | else if (PyByteArray_Resize((PyObject *)self, size) < 0) |
| 352 | return NULL; |
| 353 | |
| 354 | if (mysize == 1) |
| 355 | memset(self->ob_bytes, self->ob_bytes[0], size); |
| 356 | else { |
| 357 | Py_ssize_t i; |
| 358 | for (i = 1; i < count; i++) |
| 359 | memcpy(self->ob_bytes + i*mysize, self->ob_bytes, mysize); |
| 360 | } |
| 361 | |
| 362 | Py_INCREF(self); |
| 363 | return (PyObject *)self; |
| 364 | } |
| 365 | |
| 366 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 367 | bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 368 | { |
| 369 | if (i < 0) |
| 370 | i += Py_SIZE(self); |
| 371 | if (i < 0 || i >= Py_SIZE(self)) { |
| 372 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 373 | return NULL; |
| 374 | } |
| 375 | return PyLong_FromLong((unsigned char)(self->ob_bytes[i])); |
| 376 | } |
| 377 | |
| 378 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 379 | bytearray_subscript(PyByteArrayObject *self, PyObject *index) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 380 | { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 381 | if (PyIndex_Check(index)) { |
| 382 | Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 383 | |
| 384 | if (i == -1 && PyErr_Occurred()) |
| 385 | return NULL; |
| 386 | |
| 387 | if (i < 0) |
| 388 | i += PyByteArray_GET_SIZE(self); |
| 389 | |
| 390 | if (i < 0 || i >= Py_SIZE(self)) { |
| 391 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 392 | return NULL; |
| 393 | } |
| 394 | return PyLong_FromLong((unsigned char)(self->ob_bytes[i])); |
| 395 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 396 | else if (PySlice_Check(index)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 397 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 398 | if (PySlice_GetIndicesEx((PySliceObject *)index, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 399 | PyByteArray_GET_SIZE(self), |
| 400 | &start, &stop, &step, &slicelength) < 0) { |
| 401 | return NULL; |
| 402 | } |
| 403 | |
| 404 | if (slicelength <= 0) |
| 405 | return PyByteArray_FromStringAndSize("", 0); |
| 406 | else if (step == 1) { |
| 407 | return PyByteArray_FromStringAndSize(self->ob_bytes + start, |
| 408 | slicelength); |
| 409 | } |
| 410 | else { |
| 411 | char *source_buf = PyByteArray_AS_STRING(self); |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 412 | char *result_buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 413 | PyObject *result; |
| 414 | |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 415 | result = PyByteArray_FromStringAndSize(NULL, slicelength); |
| 416 | if (result == NULL) |
| 417 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 418 | |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 419 | result_buf = PyByteArray_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 420 | for (cur = start, i = 0; i < slicelength; |
| 421 | cur += step, i++) { |
| 422 | result_buf[i] = source_buf[cur]; |
| 423 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 424 | return result; |
| 425 | } |
| 426 | } |
| 427 | else { |
| 428 | PyErr_SetString(PyExc_TypeError, "bytearray indices must be integers"); |
| 429 | return NULL; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 434 | bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 435 | PyObject *values) |
| 436 | { |
| 437 | Py_ssize_t avail, needed; |
| 438 | void *bytes; |
| 439 | Py_buffer vbytes; |
| 440 | int res = 0; |
| 441 | |
| 442 | vbytes.len = -1; |
| 443 | if (values == (PyObject *)self) { |
| 444 | /* Make a copy and call this function recursively */ |
| 445 | int err; |
| 446 | values = PyByteArray_FromObject(values); |
| 447 | if (values == NULL) |
| 448 | return -1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 449 | err = bytearray_setslice(self, lo, hi, values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 450 | Py_DECREF(values); |
| 451 | return err; |
| 452 | } |
| 453 | if (values == NULL) { |
| 454 | /* del b[lo:hi] */ |
| 455 | bytes = NULL; |
| 456 | needed = 0; |
| 457 | } |
| 458 | else { |
| 459 | if (_getbuffer(values, &vbytes) < 0) { |
| 460 | PyErr_Format(PyExc_TypeError, |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 461 | "can't set bytearray slice from %.100s", |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 462 | Py_TYPE(values)->tp_name); |
| 463 | return -1; |
| 464 | } |
| 465 | needed = vbytes.len; |
| 466 | bytes = vbytes.buf; |
| 467 | } |
| 468 | |
| 469 | if (lo < 0) |
| 470 | lo = 0; |
| 471 | if (hi < lo) |
| 472 | hi = lo; |
| 473 | if (hi > Py_SIZE(self)) |
| 474 | hi = Py_SIZE(self); |
| 475 | |
| 476 | avail = hi - lo; |
| 477 | if (avail < 0) |
| 478 | lo = hi = avail = 0; |
| 479 | |
| 480 | if (avail != needed) { |
| 481 | if (avail > needed) { |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 482 | if (!_canresize(self)) { |
| 483 | res = -1; |
| 484 | goto finish; |
| 485 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 486 | /* |
| 487 | 0 lo hi old_size |
| 488 | | |<----avail----->|<-----tomove------>| |
| 489 | | |<-needed->|<-----tomove------>| |
| 490 | 0 lo new_hi new_size |
| 491 | */ |
| 492 | memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi, |
| 493 | Py_SIZE(self) - hi); |
| 494 | } |
| 495 | /* XXX(nnorwitz): need to verify this can't overflow! */ |
| 496 | if (PyByteArray_Resize((PyObject *)self, |
| 497 | Py_SIZE(self) + needed - avail) < 0) { |
| 498 | res = -1; |
| 499 | goto finish; |
| 500 | } |
| 501 | if (avail < needed) { |
| 502 | /* |
| 503 | 0 lo hi old_size |
| 504 | | |<-avail->|<-----tomove------>| |
| 505 | | |<----needed---->|<-----tomove------>| |
| 506 | 0 lo new_hi new_size |
| 507 | */ |
| 508 | memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi, |
| 509 | Py_SIZE(self) - lo - needed); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | if (needed > 0) |
| 514 | memcpy(self->ob_bytes + lo, bytes, needed); |
| 515 | |
| 516 | |
| 517 | finish: |
| 518 | if (vbytes.len != -1) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 519 | PyBuffer_Release(&vbytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 520 | return res; |
| 521 | } |
| 522 | |
| 523 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 524 | bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 525 | { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 526 | int ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 527 | |
| 528 | if (i < 0) |
| 529 | i += Py_SIZE(self); |
| 530 | |
| 531 | if (i < 0 || i >= Py_SIZE(self)) { |
| 532 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 533 | return -1; |
| 534 | } |
| 535 | |
| 536 | if (value == NULL) |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 537 | return bytearray_setslice(self, i, i+1, NULL); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 538 | |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 539 | if (!_getbytevalue(value, &ival)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 540 | return -1; |
| 541 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 542 | self->ob_bytes[i] = ival; |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 547 | bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 548 | { |
| 549 | Py_ssize_t start, stop, step, slicelen, needed; |
| 550 | char *bytes; |
| 551 | |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 552 | if (PyIndex_Check(index)) { |
| 553 | Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 554 | |
| 555 | if (i == -1 && PyErr_Occurred()) |
| 556 | return -1; |
| 557 | |
| 558 | if (i < 0) |
| 559 | i += PyByteArray_GET_SIZE(self); |
| 560 | |
| 561 | if (i < 0 || i >= Py_SIZE(self)) { |
| 562 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 563 | return -1; |
| 564 | } |
| 565 | |
| 566 | if (values == NULL) { |
| 567 | /* Fall through to slice assignment */ |
| 568 | start = i; |
| 569 | stop = i + 1; |
| 570 | step = 1; |
| 571 | slicelen = 1; |
| 572 | } |
| 573 | else { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 574 | int ival; |
| 575 | if (!_getbytevalue(values, &ival)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 576 | return -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 577 | self->ob_bytes[i] = (char)ival; |
| 578 | return 0; |
| 579 | } |
| 580 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 581 | else if (PySlice_Check(index)) { |
| 582 | if (PySlice_GetIndicesEx((PySliceObject *)index, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 583 | PyByteArray_GET_SIZE(self), |
| 584 | &start, &stop, &step, &slicelen) < 0) { |
| 585 | return -1; |
| 586 | } |
| 587 | } |
| 588 | else { |
| 589 | PyErr_SetString(PyExc_TypeError, "bytearray indices must be integer"); |
| 590 | return -1; |
| 591 | } |
| 592 | |
| 593 | if (values == NULL) { |
| 594 | bytes = NULL; |
| 595 | needed = 0; |
| 596 | } |
| 597 | else if (values == (PyObject *)self || !PyByteArray_Check(values)) { |
| 598 | /* Make a copy an call this function recursively */ |
| 599 | int err; |
| 600 | values = PyByteArray_FromObject(values); |
| 601 | if (values == NULL) |
| 602 | return -1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 603 | err = bytearray_ass_subscript(self, index, values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 604 | Py_DECREF(values); |
| 605 | return err; |
| 606 | } |
| 607 | else { |
| 608 | assert(PyByteArray_Check(values)); |
| 609 | bytes = ((PyByteArrayObject *)values)->ob_bytes; |
| 610 | needed = Py_SIZE(values); |
| 611 | } |
| 612 | /* Make sure b[5:2] = ... inserts before 5, not before 2. */ |
| 613 | if ((step < 0 && start < stop) || |
| 614 | (step > 0 && start > stop)) |
| 615 | stop = start; |
| 616 | if (step == 1) { |
| 617 | if (slicelen != needed) { |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 618 | if (!_canresize(self)) |
| 619 | return -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 620 | if (slicelen > needed) { |
| 621 | /* |
| 622 | 0 start stop old_size |
| 623 | | |<---slicelen--->|<-----tomove------>| |
| 624 | | |<-needed->|<-----tomove------>| |
| 625 | 0 lo new_hi new_size |
| 626 | */ |
| 627 | memmove(self->ob_bytes + start + needed, self->ob_bytes + stop, |
| 628 | Py_SIZE(self) - stop); |
| 629 | } |
| 630 | if (PyByteArray_Resize((PyObject *)self, |
| 631 | Py_SIZE(self) + needed - slicelen) < 0) |
| 632 | return -1; |
| 633 | if (slicelen < needed) { |
| 634 | /* |
| 635 | 0 lo hi old_size |
| 636 | | |<-avail->|<-----tomove------>| |
| 637 | | |<----needed---->|<-----tomove------>| |
| 638 | 0 lo new_hi new_size |
| 639 | */ |
| 640 | memmove(self->ob_bytes + start + needed, self->ob_bytes + stop, |
| 641 | Py_SIZE(self) - start - needed); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | if (needed > 0) |
| 646 | memcpy(self->ob_bytes + start, bytes, needed); |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | else { |
| 651 | if (needed == 0) { |
| 652 | /* Delete slice */ |
Mark Dickinson | a53f2c9 | 2010-01-29 17:29:21 +0000 | [diff] [blame] | 653 | size_t cur; |
| 654 | Py_ssize_t i; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 655 | |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 656 | if (!_canresize(self)) |
| 657 | return -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 658 | if (step < 0) { |
| 659 | stop = start + 1; |
| 660 | start = stop + step * (slicelen - 1) - 1; |
| 661 | step = -step; |
| 662 | } |
| 663 | for (cur = start, i = 0; |
| 664 | i < slicelen; cur += step, i++) { |
| 665 | Py_ssize_t lim = step - 1; |
| 666 | |
Mark Dickinson | 5a41a4c | 2010-02-14 13:09:30 +0000 | [diff] [blame] | 667 | if (cur + step >= (size_t)PyByteArray_GET_SIZE(self)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 668 | lim = PyByteArray_GET_SIZE(self) - cur - 1; |
| 669 | |
| 670 | memmove(self->ob_bytes + cur - i, |
| 671 | self->ob_bytes + cur + 1, lim); |
| 672 | } |
| 673 | /* Move the tail of the bytes, in one chunk */ |
| 674 | cur = start + slicelen*step; |
Mark Dickinson | 5a41a4c | 2010-02-14 13:09:30 +0000 | [diff] [blame] | 675 | if (cur < (size_t)PyByteArray_GET_SIZE(self)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 676 | memmove(self->ob_bytes + cur - slicelen, |
| 677 | self->ob_bytes + cur, |
| 678 | PyByteArray_GET_SIZE(self) - cur); |
| 679 | } |
| 680 | if (PyByteArray_Resize((PyObject *)self, |
| 681 | PyByteArray_GET_SIZE(self) - slicelen) < 0) |
| 682 | return -1; |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | else { |
| 687 | /* Assign slice */ |
| 688 | Py_ssize_t cur, i; |
| 689 | |
| 690 | if (needed != slicelen) { |
| 691 | PyErr_Format(PyExc_ValueError, |
| 692 | "attempt to assign bytes of size %zd " |
| 693 | "to extended slice of size %zd", |
| 694 | needed, slicelen); |
| 695 | return -1; |
| 696 | } |
| 697 | for (cur = start, i = 0; i < slicelen; cur += step, i++) |
| 698 | self->ob_bytes[cur] = bytes[i]; |
| 699 | return 0; |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 705 | bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 706 | { |
| 707 | static char *kwlist[] = {"source", "encoding", "errors", 0}; |
| 708 | PyObject *arg = NULL; |
| 709 | const char *encoding = NULL; |
| 710 | const char *errors = NULL; |
| 711 | Py_ssize_t count; |
| 712 | PyObject *it; |
| 713 | PyObject *(*iternext)(PyObject *); |
| 714 | |
| 715 | if (Py_SIZE(self) != 0) { |
| 716 | /* Empty previous contents (yes, do this first of all!) */ |
| 717 | if (PyByteArray_Resize((PyObject *)self, 0) < 0) |
| 718 | return -1; |
| 719 | } |
| 720 | |
| 721 | /* Parse arguments */ |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 722 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 723 | &arg, &encoding, &errors)) |
| 724 | return -1; |
| 725 | |
| 726 | /* Make a quick exit if no first argument */ |
| 727 | if (arg == NULL) { |
| 728 | if (encoding != NULL || errors != NULL) { |
| 729 | PyErr_SetString(PyExc_TypeError, |
| 730 | "encoding or errors without sequence argument"); |
| 731 | return -1; |
| 732 | } |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | if (PyUnicode_Check(arg)) { |
| 737 | /* Encode via the codec registry */ |
| 738 | PyObject *encoded, *new; |
| 739 | if (encoding == NULL) { |
| 740 | PyErr_SetString(PyExc_TypeError, |
| 741 | "string argument without an encoding"); |
| 742 | return -1; |
| 743 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 744 | encoded = PyUnicode_AsEncodedString(arg, encoding, errors); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 745 | if (encoded == NULL) |
| 746 | return -1; |
| 747 | assert(PyBytes_Check(encoded)); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 748 | new = bytearray_iconcat(self, encoded); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 749 | Py_DECREF(encoded); |
| 750 | if (new == NULL) |
| 751 | return -1; |
| 752 | Py_DECREF(new); |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | /* If it's not unicode, there can't be encoding or errors */ |
| 757 | if (encoding != NULL || errors != NULL) { |
| 758 | PyErr_SetString(PyExc_TypeError, |
| 759 | "encoding or errors without a string argument"); |
| 760 | return -1; |
| 761 | } |
| 762 | |
| 763 | /* Is it an int? */ |
Benjamin Peterson | 0ff8a50 | 2010-04-16 23:19:11 +0000 | [diff] [blame] | 764 | count = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
| 765 | if (count == -1 && PyErr_Occurred()) { |
| 766 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 767 | return -1; |
Benjamin Peterson | 0ff8a50 | 2010-04-16 23:19:11 +0000 | [diff] [blame] | 768 | PyErr_Clear(); |
| 769 | } |
| 770 | else if (count < 0) { |
| 771 | PyErr_SetString(PyExc_ValueError, "negative count"); |
| 772 | return -1; |
| 773 | } |
| 774 | else { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 775 | if (count > 0) { |
| 776 | if (PyByteArray_Resize((PyObject *)self, count)) |
| 777 | return -1; |
| 778 | memset(self->ob_bytes, 0, count); |
| 779 | } |
| 780 | return 0; |
| 781 | } |
| 782 | |
| 783 | /* Use the buffer API */ |
| 784 | if (PyObject_CheckBuffer(arg)) { |
| 785 | Py_ssize_t size; |
| 786 | Py_buffer view; |
| 787 | if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0) |
| 788 | return -1; |
| 789 | size = view.len; |
| 790 | if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail; |
| 791 | if (PyBuffer_ToContiguous(self->ob_bytes, &view, size, 'C') < 0) |
| 792 | goto fail; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 793 | PyBuffer_Release(&view); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 794 | return 0; |
| 795 | fail: |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 796 | PyBuffer_Release(&view); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 797 | return -1; |
| 798 | } |
| 799 | |
| 800 | /* XXX Optimize this if the arguments is a list, tuple */ |
| 801 | |
| 802 | /* Get the iterator */ |
| 803 | it = PyObject_GetIter(arg); |
| 804 | if (it == NULL) |
| 805 | return -1; |
| 806 | iternext = *Py_TYPE(it)->tp_iternext; |
| 807 | |
| 808 | /* Run the iterator to exhaustion */ |
| 809 | for (;;) { |
| 810 | PyObject *item; |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 811 | int rc, value; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 812 | |
| 813 | /* Get the next item */ |
| 814 | item = iternext(it); |
| 815 | if (item == NULL) { |
| 816 | if (PyErr_Occurred()) { |
| 817 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 818 | goto error; |
| 819 | PyErr_Clear(); |
| 820 | } |
| 821 | break; |
| 822 | } |
| 823 | |
| 824 | /* Interpret it as an int (__index__) */ |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 825 | rc = _getbytevalue(item, &value); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 826 | Py_DECREF(item); |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 827 | if (!rc) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 828 | goto error; |
| 829 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 830 | /* Append the byte */ |
| 831 | if (Py_SIZE(self) < self->ob_alloc) |
| 832 | Py_SIZE(self)++; |
| 833 | else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0) |
| 834 | goto error; |
| 835 | self->ob_bytes[Py_SIZE(self)-1] = value; |
| 836 | } |
| 837 | |
| 838 | /* Clean up and return success */ |
| 839 | Py_DECREF(it); |
| 840 | return 0; |
| 841 | |
| 842 | error: |
| 843 | /* Error handling when it != NULL */ |
| 844 | Py_DECREF(it); |
| 845 | return -1; |
| 846 | } |
| 847 | |
| 848 | /* Mostly copied from string_repr, but without the |
| 849 | "smart quote" functionality. */ |
| 850 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 851 | bytearray_repr(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 852 | { |
| 853 | static const char *hexdigits = "0123456789abcdef"; |
| 854 | const char *quote_prefix = "bytearray(b"; |
| 855 | const char *quote_postfix = ")"; |
| 856 | Py_ssize_t length = Py_SIZE(self); |
| 857 | /* 14 == strlen(quote_prefix) + 2 + strlen(quote_postfix) */ |
Mark Dickinson | 5a41a4c | 2010-02-14 13:09:30 +0000 | [diff] [blame] | 858 | size_t newsize; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 859 | PyObject *v; |
Mark Dickinson | 5a41a4c | 2010-02-14 13:09:30 +0000 | [diff] [blame] | 860 | if (length > (PY_SSIZE_T_MAX - 14) / 4) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 861 | PyErr_SetString(PyExc_OverflowError, |
| 862 | "bytearray object is too large to make repr"); |
| 863 | return NULL; |
| 864 | } |
Mark Dickinson | 5a41a4c | 2010-02-14 13:09:30 +0000 | [diff] [blame] | 865 | newsize = 14 + 4 * length; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 866 | v = PyUnicode_FromUnicode(NULL, newsize); |
| 867 | if (v == NULL) { |
| 868 | return NULL; |
| 869 | } |
| 870 | else { |
| 871 | register Py_ssize_t i; |
| 872 | register Py_UNICODE c; |
| 873 | register Py_UNICODE *p; |
| 874 | int quote; |
| 875 | |
| 876 | /* Figure out which quote to use; single is preferred */ |
| 877 | quote = '\''; |
| 878 | { |
| 879 | char *test, *start; |
| 880 | start = PyByteArray_AS_STRING(self); |
| 881 | for (test = start; test < start+length; ++test) { |
| 882 | if (*test == '"') { |
| 883 | quote = '\''; /* back to single */ |
| 884 | goto decided; |
| 885 | } |
| 886 | else if (*test == '\'') |
| 887 | quote = '"'; |
| 888 | } |
| 889 | decided: |
| 890 | ; |
| 891 | } |
| 892 | |
| 893 | p = PyUnicode_AS_UNICODE(v); |
| 894 | while (*quote_prefix) |
| 895 | *p++ = *quote_prefix++; |
| 896 | *p++ = quote; |
| 897 | |
| 898 | for (i = 0; i < length; i++) { |
| 899 | /* There's at least enough room for a hex escape |
| 900 | and a closing quote. */ |
| 901 | assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5); |
| 902 | c = self->ob_bytes[i]; |
| 903 | if (c == '\'' || c == '\\') |
| 904 | *p++ = '\\', *p++ = c; |
| 905 | else if (c == '\t') |
| 906 | *p++ = '\\', *p++ = 't'; |
| 907 | else if (c == '\n') |
| 908 | *p++ = '\\', *p++ = 'n'; |
| 909 | else if (c == '\r') |
| 910 | *p++ = '\\', *p++ = 'r'; |
| 911 | else if (c == 0) |
| 912 | *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0'; |
| 913 | else if (c < ' ' || c >= 0x7f) { |
| 914 | *p++ = '\\'; |
| 915 | *p++ = 'x'; |
| 916 | *p++ = hexdigits[(c & 0xf0) >> 4]; |
| 917 | *p++ = hexdigits[c & 0xf]; |
| 918 | } |
| 919 | else |
| 920 | *p++ = c; |
| 921 | } |
| 922 | assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1); |
| 923 | *p++ = quote; |
| 924 | while (*quote_postfix) { |
| 925 | *p++ = *quote_postfix++; |
| 926 | } |
| 927 | *p = '\0'; |
| 928 | if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) { |
| 929 | Py_DECREF(v); |
| 930 | return NULL; |
| 931 | } |
| 932 | return v; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 937 | bytearray_str(PyObject *op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 938 | { |
| 939 | if (Py_BytesWarningFlag) { |
| 940 | if (PyErr_WarnEx(PyExc_BytesWarning, |
| 941 | "str() on a bytearray instance", 1)) |
| 942 | return NULL; |
| 943 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 944 | return bytearray_repr((PyByteArrayObject*)op); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 948 | bytearray_richcompare(PyObject *self, PyObject *other, int op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 949 | { |
| 950 | Py_ssize_t self_size, other_size; |
| 951 | Py_buffer self_bytes, other_bytes; |
| 952 | PyObject *res; |
| 953 | Py_ssize_t minsize; |
| 954 | int cmp; |
| 955 | |
| 956 | /* Bytes can be compared to anything that supports the (binary) |
| 957 | buffer API. Except that a comparison with Unicode is always an |
| 958 | error, even if the comparison is for equality. */ |
| 959 | if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) || |
| 960 | PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) { |
Barry Warsaw | 9e9dcd6 | 2008-10-17 01:50:37 +0000 | [diff] [blame] | 961 | if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 962 | if (PyErr_WarnEx(PyExc_BytesWarning, |
Georg Brandl | e5d68ac | 2008-06-04 11:30:26 +0000 | [diff] [blame] | 963 | "Comparison between bytearray and string", 1)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 964 | return NULL; |
| 965 | } |
| 966 | |
| 967 | Py_INCREF(Py_NotImplemented); |
| 968 | return Py_NotImplemented; |
| 969 | } |
| 970 | |
| 971 | self_size = _getbuffer(self, &self_bytes); |
| 972 | if (self_size < 0) { |
| 973 | PyErr_Clear(); |
| 974 | Py_INCREF(Py_NotImplemented); |
| 975 | return Py_NotImplemented; |
| 976 | } |
| 977 | |
| 978 | other_size = _getbuffer(other, &other_bytes); |
| 979 | if (other_size < 0) { |
| 980 | PyErr_Clear(); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 981 | PyBuffer_Release(&self_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 982 | Py_INCREF(Py_NotImplemented); |
| 983 | return Py_NotImplemented; |
| 984 | } |
| 985 | |
| 986 | if (self_size != other_size && (op == Py_EQ || op == Py_NE)) { |
| 987 | /* Shortcut: if the lengths differ, the objects differ */ |
| 988 | cmp = (op == Py_NE); |
| 989 | } |
| 990 | else { |
| 991 | minsize = self_size; |
| 992 | if (other_size < minsize) |
| 993 | minsize = other_size; |
| 994 | |
| 995 | cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize); |
| 996 | /* In ISO C, memcmp() guarantees to use unsigned bytes! */ |
| 997 | |
| 998 | if (cmp == 0) { |
| 999 | if (self_size < other_size) |
| 1000 | cmp = -1; |
| 1001 | else if (self_size > other_size) |
| 1002 | cmp = 1; |
| 1003 | } |
| 1004 | |
| 1005 | switch (op) { |
| 1006 | case Py_LT: cmp = cmp < 0; break; |
| 1007 | case Py_LE: cmp = cmp <= 0; break; |
| 1008 | case Py_EQ: cmp = cmp == 0; break; |
| 1009 | case Py_NE: cmp = cmp != 0; break; |
| 1010 | case Py_GT: cmp = cmp > 0; break; |
| 1011 | case Py_GE: cmp = cmp >= 0; break; |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | res = cmp ? Py_True : Py_False; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1016 | PyBuffer_Release(&self_bytes); |
| 1017 | PyBuffer_Release(&other_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1018 | Py_INCREF(res); |
| 1019 | return res; |
| 1020 | } |
| 1021 | |
| 1022 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1023 | bytearray_dealloc(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1024 | { |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 1025 | if (self->ob_exports > 0) { |
| 1026 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1027 | "deallocated bytearray object has exported buffers"); |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 1028 | PyErr_Print(); |
| 1029 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1030 | if (self->ob_bytes != 0) { |
| 1031 | PyMem_Free(self->ob_bytes); |
| 1032 | } |
| 1033 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 1034 | } |
| 1035 | |
| 1036 | |
| 1037 | /* -------------------------------------------------------------------- */ |
| 1038 | /* Methods */ |
| 1039 | |
| 1040 | #define STRINGLIB_CHAR char |
| 1041 | #define STRINGLIB_CMP memcmp |
| 1042 | #define STRINGLIB_LEN PyByteArray_GET_SIZE |
| 1043 | #define STRINGLIB_STR PyByteArray_AS_STRING |
| 1044 | #define STRINGLIB_NEW PyByteArray_FromStringAndSize |
| 1045 | #define STRINGLIB_EMPTY nullbytes |
| 1046 | #define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact |
| 1047 | #define STRINGLIB_MUTABLE 1 |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1048 | #define FROM_BYTEARRAY 1 |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1049 | |
| 1050 | #include "stringlib/fastsearch.h" |
| 1051 | #include "stringlib/count.h" |
| 1052 | #include "stringlib/find.h" |
| 1053 | #include "stringlib/partition.h" |
| 1054 | #include "stringlib/ctype.h" |
| 1055 | #include "stringlib/transmogrify.h" |
| 1056 | |
| 1057 | |
| 1058 | /* The following Py_LOCAL_INLINE and Py_LOCAL functions |
| 1059 | were copied from the old char* style string object. */ |
| 1060 | |
| 1061 | Py_LOCAL_INLINE(void) |
| 1062 | _adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len) |
| 1063 | { |
| 1064 | if (*end > len) |
| 1065 | *end = len; |
| 1066 | else if (*end < 0) |
| 1067 | *end += len; |
| 1068 | if (*end < 0) |
| 1069 | *end = 0; |
| 1070 | if (*start < 0) |
| 1071 | *start += len; |
| 1072 | if (*start < 0) |
| 1073 | *start = 0; |
| 1074 | } |
| 1075 | |
| 1076 | |
| 1077 | Py_LOCAL_INLINE(Py_ssize_t) |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1078 | bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1079 | { |
| 1080 | PyObject *subobj; |
| 1081 | Py_buffer subbuf; |
| 1082 | Py_ssize_t start=0, end=PY_SSIZE_T_MAX; |
| 1083 | Py_ssize_t res; |
| 1084 | |
| 1085 | if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", &subobj, |
| 1086 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 1087 | return -2; |
| 1088 | if (_getbuffer(subobj, &subbuf) < 0) |
| 1089 | return -2; |
| 1090 | if (dir > 0) |
| 1091 | res = stringlib_find_slice( |
| 1092 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 1093 | subbuf.buf, subbuf.len, start, end); |
| 1094 | else |
| 1095 | res = stringlib_rfind_slice( |
| 1096 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 1097 | subbuf.buf, subbuf.len, start, end); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1098 | PyBuffer_Release(&subbuf); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1099 | return res; |
| 1100 | } |
| 1101 | |
| 1102 | PyDoc_STRVAR(find__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1103 | "B.find(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1104 | \n\ |
| 1105 | Return the lowest index in B where subsection sub is found,\n\ |
| 1106 | such that sub is contained within s[start,end]. Optional\n\ |
| 1107 | arguments start and end are interpreted as in slice notation.\n\ |
| 1108 | \n\ |
| 1109 | Return -1 on failure."); |
| 1110 | |
| 1111 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1112 | bytearray_find(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1113 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1114 | Py_ssize_t result = bytearray_find_internal(self, args, +1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1115 | if (result == -2) |
| 1116 | return NULL; |
| 1117 | return PyLong_FromSsize_t(result); |
| 1118 | } |
| 1119 | |
| 1120 | PyDoc_STRVAR(count__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1121 | "B.count(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1122 | \n\ |
| 1123 | Return the number of non-overlapping occurrences of subsection sub in\n\ |
| 1124 | bytes B[start:end]. Optional arguments start and end are interpreted\n\ |
| 1125 | as in slice notation."); |
| 1126 | |
| 1127 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1128 | bytearray_count(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1129 | { |
| 1130 | PyObject *sub_obj; |
| 1131 | const char *str = PyByteArray_AS_STRING(self); |
| 1132 | Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
| 1133 | Py_buffer vsub; |
| 1134 | PyObject *count_obj; |
| 1135 | |
| 1136 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj, |
| 1137 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 1138 | return NULL; |
| 1139 | |
| 1140 | if (_getbuffer(sub_obj, &vsub) < 0) |
| 1141 | return NULL; |
| 1142 | |
| 1143 | _adjust_indices(&start, &end, PyByteArray_GET_SIZE(self)); |
| 1144 | |
| 1145 | count_obj = PyLong_FromSsize_t( |
| 1146 | stringlib_count(str + start, end - start, vsub.buf, vsub.len) |
| 1147 | ); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1148 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1149 | return count_obj; |
| 1150 | } |
| 1151 | |
| 1152 | |
| 1153 | PyDoc_STRVAR(index__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1154 | "B.index(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1155 | \n\ |
| 1156 | Like B.find() but raise ValueError when the subsection is not found."); |
| 1157 | |
| 1158 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1159 | bytearray_index(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1160 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1161 | Py_ssize_t result = bytearray_find_internal(self, args, +1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1162 | if (result == -2) |
| 1163 | return NULL; |
| 1164 | if (result == -1) { |
| 1165 | PyErr_SetString(PyExc_ValueError, |
| 1166 | "subsection not found"); |
| 1167 | return NULL; |
| 1168 | } |
| 1169 | return PyLong_FromSsize_t(result); |
| 1170 | } |
| 1171 | |
| 1172 | |
| 1173 | PyDoc_STRVAR(rfind__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1174 | "B.rfind(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1175 | \n\ |
| 1176 | Return the highest index in B where subsection sub is found,\n\ |
| 1177 | such that sub is contained within s[start,end]. Optional\n\ |
| 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_rfind(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 | |
| 1192 | PyDoc_STRVAR(rindex__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1193 | "B.rindex(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1194 | \n\ |
| 1195 | Like B.rfind() but raise ValueError when the subsection is not found."); |
| 1196 | |
| 1197 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1198 | bytearray_rindex(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1199 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1200 | Py_ssize_t result = bytearray_find_internal(self, args, -1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1201 | if (result == -2) |
| 1202 | return NULL; |
| 1203 | if (result == -1) { |
| 1204 | PyErr_SetString(PyExc_ValueError, |
| 1205 | "subsection not found"); |
| 1206 | return NULL; |
| 1207 | } |
| 1208 | return PyLong_FromSsize_t(result); |
| 1209 | } |
| 1210 | |
| 1211 | |
| 1212 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1213 | bytearray_contains(PyObject *self, PyObject *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1214 | { |
| 1215 | Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError); |
| 1216 | if (ival == -1 && PyErr_Occurred()) { |
| 1217 | Py_buffer varg; |
| 1218 | int pos; |
| 1219 | PyErr_Clear(); |
| 1220 | if (_getbuffer(arg, &varg) < 0) |
| 1221 | return -1; |
| 1222 | pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self), |
| 1223 | varg.buf, varg.len, 0); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1224 | PyBuffer_Release(&varg); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1225 | return pos >= 0; |
| 1226 | } |
| 1227 | if (ival < 0 || ival >= 256) { |
| 1228 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
| 1229 | return -1; |
| 1230 | } |
| 1231 | |
| 1232 | return memchr(PyByteArray_AS_STRING(self), ival, Py_SIZE(self)) != NULL; |
| 1233 | } |
| 1234 | |
| 1235 | |
| 1236 | /* Matches the end (direction >= 0) or start (direction < 0) of self |
| 1237 | * against substr, using the start and end arguments. Returns |
| 1238 | * -1 on error, 0 if not found and 1 if found. |
| 1239 | */ |
| 1240 | Py_LOCAL(int) |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1241 | _bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1242 | Py_ssize_t end, int direction) |
| 1243 | { |
| 1244 | Py_ssize_t len = PyByteArray_GET_SIZE(self); |
| 1245 | const char* str; |
| 1246 | Py_buffer vsubstr; |
| 1247 | int rv = 0; |
| 1248 | |
| 1249 | str = PyByteArray_AS_STRING(self); |
| 1250 | |
| 1251 | if (_getbuffer(substr, &vsubstr) < 0) |
| 1252 | return -1; |
| 1253 | |
| 1254 | _adjust_indices(&start, &end, len); |
| 1255 | |
| 1256 | if (direction < 0) { |
| 1257 | /* startswith */ |
| 1258 | if (start+vsubstr.len > len) { |
| 1259 | goto done; |
| 1260 | } |
| 1261 | } else { |
| 1262 | /* endswith */ |
| 1263 | if (end-start < vsubstr.len || start > len) { |
| 1264 | goto done; |
| 1265 | } |
| 1266 | |
| 1267 | if (end-vsubstr.len > start) |
| 1268 | start = end - vsubstr.len; |
| 1269 | } |
| 1270 | if (end-start >= vsubstr.len) |
| 1271 | rv = ! memcmp(str+start, vsubstr.buf, vsubstr.len); |
| 1272 | |
| 1273 | done: |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1274 | PyBuffer_Release(&vsubstr); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1275 | return rv; |
| 1276 | } |
| 1277 | |
| 1278 | |
| 1279 | PyDoc_STRVAR(startswith__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1280 | "B.startswith(prefix[, start[, end]]) -> bool\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1281 | \n\ |
| 1282 | Return True if B starts with the specified prefix, False otherwise.\n\ |
| 1283 | With optional start, test B beginning at that position.\n\ |
| 1284 | With optional end, stop comparing B at that position.\n\ |
| 1285 | prefix can also be a tuple of strings to try."); |
| 1286 | |
| 1287 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1288 | bytearray_startswith(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1289 | { |
| 1290 | Py_ssize_t start = 0; |
| 1291 | Py_ssize_t end = PY_SSIZE_T_MAX; |
| 1292 | PyObject *subobj; |
| 1293 | int result; |
| 1294 | |
| 1295 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
| 1296 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 1297 | return NULL; |
| 1298 | if (PyTuple_Check(subobj)) { |
| 1299 | Py_ssize_t i; |
| 1300 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1301 | result = _bytearray_tailmatch(self, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1302 | PyTuple_GET_ITEM(subobj, i), |
| 1303 | start, end, -1); |
| 1304 | if (result == -1) |
| 1305 | return NULL; |
| 1306 | else if (result) { |
| 1307 | Py_RETURN_TRUE; |
| 1308 | } |
| 1309 | } |
| 1310 | Py_RETURN_FALSE; |
| 1311 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1312 | result = _bytearray_tailmatch(self, subobj, start, end, -1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1313 | if (result == -1) |
| 1314 | return NULL; |
| 1315 | else |
| 1316 | return PyBool_FromLong(result); |
| 1317 | } |
| 1318 | |
| 1319 | PyDoc_STRVAR(endswith__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1320 | "B.endswith(suffix[, start[, end]]) -> bool\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1321 | \n\ |
| 1322 | Return True if B ends with the specified suffix, False otherwise.\n\ |
| 1323 | With optional start, test B beginning at that position.\n\ |
| 1324 | With optional end, stop comparing B at that position.\n\ |
| 1325 | suffix can also be a tuple of strings to try."); |
| 1326 | |
| 1327 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1328 | bytearray_endswith(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1329 | { |
| 1330 | Py_ssize_t start = 0; |
| 1331 | Py_ssize_t end = PY_SSIZE_T_MAX; |
| 1332 | PyObject *subobj; |
| 1333 | int result; |
| 1334 | |
| 1335 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 1336 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 1337 | return NULL; |
| 1338 | if (PyTuple_Check(subobj)) { |
| 1339 | Py_ssize_t i; |
| 1340 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1341 | result = _bytearray_tailmatch(self, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1342 | PyTuple_GET_ITEM(subobj, i), |
| 1343 | start, end, +1); |
| 1344 | if (result == -1) |
| 1345 | return NULL; |
| 1346 | else if (result) { |
| 1347 | Py_RETURN_TRUE; |
| 1348 | } |
| 1349 | } |
| 1350 | Py_RETURN_FALSE; |
| 1351 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1352 | result = _bytearray_tailmatch(self, subobj, start, end, +1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1353 | if (result == -1) |
| 1354 | return NULL; |
| 1355 | else |
| 1356 | return PyBool_FromLong(result); |
| 1357 | } |
| 1358 | |
| 1359 | |
| 1360 | PyDoc_STRVAR(translate__doc__, |
| 1361 | "B.translate(table[, deletechars]) -> bytearray\n\ |
| 1362 | \n\ |
| 1363 | Return a copy of B, where all characters occurring in the\n\ |
| 1364 | optional argument deletechars are removed, and the remaining\n\ |
| 1365 | characters have been mapped through the given translation\n\ |
| 1366 | table, which must be a bytes object of length 256."); |
| 1367 | |
| 1368 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1369 | bytearray_translate(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1370 | { |
| 1371 | register char *input, *output; |
| 1372 | register const char *table; |
Benjamin Peterson | 0f3641c | 2008-11-19 22:05:52 +0000 | [diff] [blame] | 1373 | register Py_ssize_t i, c; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1374 | PyObject *input_obj = (PyObject*)self; |
| 1375 | const char *output_start; |
| 1376 | Py_ssize_t inlen; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1377 | PyObject *result = NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1378 | int trans_table[256]; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1379 | PyObject *tableobj = NULL, *delobj = NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1380 | Py_buffer vtable, vdel; |
| 1381 | |
| 1382 | if (!PyArg_UnpackTuple(args, "translate", 1, 2, |
| 1383 | &tableobj, &delobj)) |
| 1384 | return NULL; |
| 1385 | |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1386 | if (tableobj == Py_None) { |
| 1387 | table = NULL; |
| 1388 | tableobj = NULL; |
| 1389 | } else if (_getbuffer(tableobj, &vtable) < 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1390 | return NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1391 | } else { |
| 1392 | if (vtable.len != 256) { |
| 1393 | PyErr_SetString(PyExc_ValueError, |
| 1394 | "translation table must be 256 characters long"); |
Georg Brandl | 069bcc3 | 2009-07-22 12:06:11 +0000 | [diff] [blame] | 1395 | PyBuffer_Release(&vtable); |
| 1396 | return NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1397 | } |
| 1398 | table = (const char*)vtable.buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | if (delobj != NULL) { |
| 1402 | if (_getbuffer(delobj, &vdel) < 0) { |
Georg Brandl | 069bcc3 | 2009-07-22 12:06:11 +0000 | [diff] [blame] | 1403 | if (tableobj != NULL) |
| 1404 | PyBuffer_Release(&vtable); |
| 1405 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1406 | } |
| 1407 | } |
| 1408 | else { |
| 1409 | vdel.buf = NULL; |
| 1410 | vdel.len = 0; |
| 1411 | } |
| 1412 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1413 | inlen = PyByteArray_GET_SIZE(input_obj); |
| 1414 | result = PyByteArray_FromStringAndSize((char *)NULL, inlen); |
| 1415 | if (result == NULL) |
| 1416 | goto done; |
| 1417 | output_start = output = PyByteArray_AsString(result); |
| 1418 | input = PyByteArray_AS_STRING(input_obj); |
| 1419 | |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1420 | if (vdel.len == 0 && table != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1421 | /* If no deletions are required, use faster code */ |
| 1422 | for (i = inlen; --i >= 0; ) { |
| 1423 | c = Py_CHARMASK(*input++); |
Benjamin Peterson | 0f3641c | 2008-11-19 22:05:52 +0000 | [diff] [blame] | 1424 | *output++ = table[c]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1425 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1426 | goto done; |
| 1427 | } |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1428 | |
| 1429 | if (table == NULL) { |
| 1430 | for (i = 0; i < 256; i++) |
| 1431 | trans_table[i] = Py_CHARMASK(i); |
| 1432 | } else { |
| 1433 | for (i = 0; i < 256; i++) |
| 1434 | trans_table[i] = Py_CHARMASK(table[i]); |
| 1435 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1436 | |
| 1437 | for (i = 0; i < vdel.len; i++) |
| 1438 | trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1; |
| 1439 | |
| 1440 | for (i = inlen; --i >= 0; ) { |
| 1441 | c = Py_CHARMASK(*input++); |
| 1442 | if (trans_table[c] != -1) |
| 1443 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 1444 | continue; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1445 | } |
| 1446 | /* Fix the size of the resulting string */ |
| 1447 | if (inlen > 0) |
| 1448 | PyByteArray_Resize(result, output - output_start); |
| 1449 | |
| 1450 | done: |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1451 | if (tableobj != NULL) |
| 1452 | PyBuffer_Release(&vtable); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1453 | if (delobj != NULL) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1454 | PyBuffer_Release(&vdel); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1455 | return result; |
| 1456 | } |
| 1457 | |
| 1458 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1459 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1460 | bytearray_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1461 | { |
| 1462 | return _Py_bytes_maketrans(args); |
| 1463 | } |
| 1464 | |
| 1465 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1466 | #define FORWARD 1 |
| 1467 | #define REVERSE -1 |
| 1468 | |
| 1469 | /* find and count characters and substrings */ |
| 1470 | |
| 1471 | #define findchar(target, target_len, c) \ |
| 1472 | ((char *)memchr((const void *)(target), c, target_len)) |
| 1473 | |
| 1474 | /* Don't call if length < 2 */ |
| 1475 | #define Py_STRING_MATCH(target, offset, pattern, length) \ |
| 1476 | (target[offset] == pattern[0] && \ |
| 1477 | target[offset+length-1] == pattern[length-1] && \ |
| 1478 | !memcmp(target+offset+1, pattern+1, length-2) ) |
| 1479 | |
| 1480 | |
Benjamin Peterson | 0f3641c | 2008-11-19 22:05:52 +0000 | [diff] [blame] | 1481 | /* Bytes ops must return a string, create a copy */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1482 | Py_LOCAL(PyByteArrayObject *) |
| 1483 | return_self(PyByteArrayObject *self) |
| 1484 | { |
Georg Brandl | 1e7217d | 2008-05-30 12:02:38 +0000 | [diff] [blame] | 1485 | /* always return a new bytearray */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1486 | return (PyByteArrayObject *)PyByteArray_FromStringAndSize( |
| 1487 | PyByteArray_AS_STRING(self), |
| 1488 | PyByteArray_GET_SIZE(self)); |
| 1489 | } |
| 1490 | |
| 1491 | Py_LOCAL_INLINE(Py_ssize_t) |
| 1492 | countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount) |
| 1493 | { |
| 1494 | Py_ssize_t count=0; |
| 1495 | const char *start=target; |
| 1496 | const char *end=target+target_len; |
| 1497 | |
| 1498 | while ( (start=findchar(start, end-start, c)) != NULL ) { |
| 1499 | count++; |
| 1500 | if (count >= maxcount) |
| 1501 | break; |
| 1502 | start += 1; |
| 1503 | } |
| 1504 | return count; |
| 1505 | } |
| 1506 | |
| 1507 | Py_LOCAL(Py_ssize_t) |
| 1508 | findstring(const char *target, Py_ssize_t target_len, |
| 1509 | const char *pattern, Py_ssize_t pattern_len, |
| 1510 | Py_ssize_t start, |
| 1511 | Py_ssize_t end, |
| 1512 | int direction) |
| 1513 | { |
| 1514 | if (start < 0) { |
| 1515 | start += target_len; |
| 1516 | if (start < 0) |
| 1517 | start = 0; |
| 1518 | } |
| 1519 | if (end > target_len) { |
| 1520 | end = target_len; |
| 1521 | } else if (end < 0) { |
| 1522 | end += target_len; |
| 1523 | if (end < 0) |
| 1524 | end = 0; |
| 1525 | } |
| 1526 | |
| 1527 | /* zero-length substrings always match at the first attempt */ |
| 1528 | if (pattern_len == 0) |
| 1529 | return (direction > 0) ? start : end; |
| 1530 | |
| 1531 | end -= pattern_len; |
| 1532 | |
| 1533 | if (direction < 0) { |
| 1534 | for (; end >= start; end--) |
| 1535 | if (Py_STRING_MATCH(target, end, pattern, pattern_len)) |
| 1536 | return end; |
| 1537 | } else { |
| 1538 | for (; start <= end; start++) |
| 1539 | if (Py_STRING_MATCH(target, start, pattern, pattern_len)) |
| 1540 | return start; |
| 1541 | } |
| 1542 | return -1; |
| 1543 | } |
| 1544 | |
| 1545 | Py_LOCAL_INLINE(Py_ssize_t) |
| 1546 | countstring(const char *target, Py_ssize_t target_len, |
| 1547 | const char *pattern, Py_ssize_t pattern_len, |
| 1548 | Py_ssize_t start, |
| 1549 | Py_ssize_t end, |
| 1550 | int direction, Py_ssize_t maxcount) |
| 1551 | { |
| 1552 | Py_ssize_t count=0; |
| 1553 | |
| 1554 | if (start < 0) { |
| 1555 | start += target_len; |
| 1556 | if (start < 0) |
| 1557 | start = 0; |
| 1558 | } |
| 1559 | if (end > target_len) { |
| 1560 | end = target_len; |
| 1561 | } else if (end < 0) { |
| 1562 | end += target_len; |
| 1563 | if (end < 0) |
| 1564 | end = 0; |
| 1565 | } |
| 1566 | |
| 1567 | /* zero-length substrings match everywhere */ |
| 1568 | if (pattern_len == 0 || maxcount == 0) { |
| 1569 | if (target_len+1 < maxcount) |
| 1570 | return target_len+1; |
| 1571 | return maxcount; |
| 1572 | } |
| 1573 | |
| 1574 | end -= pattern_len; |
| 1575 | if (direction < 0) { |
| 1576 | for (; (end >= start); end--) |
| 1577 | if (Py_STRING_MATCH(target, end, pattern, pattern_len)) { |
| 1578 | count++; |
| 1579 | if (--maxcount <= 0) break; |
| 1580 | end -= pattern_len-1; |
| 1581 | } |
| 1582 | } else { |
| 1583 | for (; (start <= end); start++) |
| 1584 | if (Py_STRING_MATCH(target, start, pattern, pattern_len)) { |
| 1585 | count++; |
| 1586 | if (--maxcount <= 0) |
| 1587 | break; |
| 1588 | start += pattern_len-1; |
| 1589 | } |
| 1590 | } |
| 1591 | return count; |
| 1592 | } |
| 1593 | |
| 1594 | |
| 1595 | /* Algorithms for different cases of string replacement */ |
| 1596 | |
| 1597 | /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
| 1598 | Py_LOCAL(PyByteArrayObject *) |
| 1599 | replace_interleave(PyByteArrayObject *self, |
| 1600 | const char *to_s, Py_ssize_t to_len, |
| 1601 | Py_ssize_t maxcount) |
| 1602 | { |
| 1603 | char *self_s, *result_s; |
| 1604 | Py_ssize_t self_len, result_len; |
| 1605 | Py_ssize_t count, i, product; |
| 1606 | PyByteArrayObject *result; |
| 1607 | |
| 1608 | self_len = PyByteArray_GET_SIZE(self); |
| 1609 | |
| 1610 | /* 1 at the end plus 1 after every character */ |
| 1611 | count = self_len+1; |
| 1612 | if (maxcount < count) |
| 1613 | count = maxcount; |
| 1614 | |
| 1615 | /* Check for overflow */ |
| 1616 | /* result_len = count * to_len + self_len; */ |
| 1617 | product = count * to_len; |
| 1618 | if (product / to_len != count) { |
| 1619 | PyErr_SetString(PyExc_OverflowError, |
| 1620 | "replace string is too long"); |
| 1621 | return NULL; |
| 1622 | } |
| 1623 | result_len = product + self_len; |
| 1624 | if (result_len < 0) { |
| 1625 | PyErr_SetString(PyExc_OverflowError, |
| 1626 | "replace string is too long"); |
| 1627 | return NULL; |
| 1628 | } |
| 1629 | |
| 1630 | if (! (result = (PyByteArrayObject *) |
| 1631 | PyByteArray_FromStringAndSize(NULL, result_len)) ) |
| 1632 | return NULL; |
| 1633 | |
| 1634 | self_s = PyByteArray_AS_STRING(self); |
| 1635 | result_s = PyByteArray_AS_STRING(result); |
| 1636 | |
| 1637 | /* TODO: special case single character, which doesn't need memcpy */ |
| 1638 | |
| 1639 | /* Lay the first one down (guaranteed this will occur) */ |
| 1640 | Py_MEMCPY(result_s, to_s, to_len); |
| 1641 | result_s += to_len; |
| 1642 | count -= 1; |
| 1643 | |
| 1644 | for (i=0; i<count; i++) { |
| 1645 | *result_s++ = *self_s++; |
| 1646 | Py_MEMCPY(result_s, to_s, to_len); |
| 1647 | result_s += to_len; |
| 1648 | } |
| 1649 | |
| 1650 | /* Copy the rest of the original string */ |
| 1651 | Py_MEMCPY(result_s, self_s, self_len-i); |
| 1652 | |
| 1653 | return result; |
| 1654 | } |
| 1655 | |
| 1656 | /* Special case for deleting a single character */ |
| 1657 | /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
| 1658 | Py_LOCAL(PyByteArrayObject *) |
| 1659 | replace_delete_single_character(PyByteArrayObject *self, |
| 1660 | char from_c, Py_ssize_t maxcount) |
| 1661 | { |
| 1662 | char *self_s, *result_s; |
| 1663 | char *start, *next, *end; |
| 1664 | Py_ssize_t self_len, result_len; |
| 1665 | Py_ssize_t count; |
| 1666 | PyByteArrayObject *result; |
| 1667 | |
| 1668 | self_len = PyByteArray_GET_SIZE(self); |
| 1669 | self_s = PyByteArray_AS_STRING(self); |
| 1670 | |
| 1671 | count = countchar(self_s, self_len, from_c, maxcount); |
| 1672 | if (count == 0) { |
| 1673 | return return_self(self); |
| 1674 | } |
| 1675 | |
| 1676 | result_len = self_len - count; /* from_len == 1 */ |
| 1677 | assert(result_len>=0); |
| 1678 | |
| 1679 | if ( (result = (PyByteArrayObject *) |
| 1680 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL) |
| 1681 | return NULL; |
| 1682 | result_s = PyByteArray_AS_STRING(result); |
| 1683 | |
| 1684 | start = self_s; |
| 1685 | end = self_s + self_len; |
| 1686 | while (count-- > 0) { |
| 1687 | next = findchar(start, end-start, from_c); |
| 1688 | if (next == NULL) |
| 1689 | break; |
| 1690 | Py_MEMCPY(result_s, start, next-start); |
| 1691 | result_s += (next-start); |
| 1692 | start = next+1; |
| 1693 | } |
| 1694 | Py_MEMCPY(result_s, start, end-start); |
| 1695 | |
| 1696 | return result; |
| 1697 | } |
| 1698 | |
| 1699 | /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
| 1700 | |
| 1701 | Py_LOCAL(PyByteArrayObject *) |
| 1702 | replace_delete_substring(PyByteArrayObject *self, |
| 1703 | const char *from_s, Py_ssize_t from_len, |
| 1704 | Py_ssize_t maxcount) |
| 1705 | { |
| 1706 | char *self_s, *result_s; |
| 1707 | char *start, *next, *end; |
| 1708 | Py_ssize_t self_len, result_len; |
| 1709 | Py_ssize_t count, offset; |
| 1710 | PyByteArrayObject *result; |
| 1711 | |
| 1712 | self_len = PyByteArray_GET_SIZE(self); |
| 1713 | self_s = PyByteArray_AS_STRING(self); |
| 1714 | |
| 1715 | count = countstring(self_s, self_len, |
| 1716 | from_s, from_len, |
| 1717 | 0, self_len, 1, |
| 1718 | maxcount); |
| 1719 | |
| 1720 | if (count == 0) { |
| 1721 | /* no matches */ |
| 1722 | return return_self(self); |
| 1723 | } |
| 1724 | |
| 1725 | result_len = self_len - (count * from_len); |
| 1726 | assert (result_len>=0); |
| 1727 | |
| 1728 | if ( (result = (PyByteArrayObject *) |
| 1729 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL ) |
| 1730 | return NULL; |
| 1731 | |
| 1732 | result_s = PyByteArray_AS_STRING(result); |
| 1733 | |
| 1734 | start = self_s; |
| 1735 | end = self_s + self_len; |
| 1736 | while (count-- > 0) { |
| 1737 | offset = findstring(start, end-start, |
| 1738 | from_s, from_len, |
| 1739 | 0, end-start, FORWARD); |
| 1740 | if (offset == -1) |
| 1741 | break; |
| 1742 | next = start + offset; |
| 1743 | |
| 1744 | Py_MEMCPY(result_s, start, next-start); |
| 1745 | |
| 1746 | result_s += (next-start); |
| 1747 | start = next+from_len; |
| 1748 | } |
| 1749 | Py_MEMCPY(result_s, start, end-start); |
| 1750 | return result; |
| 1751 | } |
| 1752 | |
| 1753 | /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
| 1754 | Py_LOCAL(PyByteArrayObject *) |
| 1755 | replace_single_character_in_place(PyByteArrayObject *self, |
| 1756 | char from_c, char to_c, |
| 1757 | Py_ssize_t maxcount) |
| 1758 | { |
Antoine Pitrou | a57aae7 | 2010-06-09 16:58:35 +0000 | [diff] [blame] | 1759 | char *self_s, *result_s, *start, *end, *next; |
| 1760 | Py_ssize_t self_len; |
| 1761 | PyByteArrayObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1762 | |
Antoine Pitrou | a57aae7 | 2010-06-09 16:58:35 +0000 | [diff] [blame] | 1763 | /* The result string will be the same size */ |
| 1764 | self_s = PyByteArray_AS_STRING(self); |
| 1765 | self_len = PyByteArray_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1766 | |
Antoine Pitrou | a57aae7 | 2010-06-09 16:58:35 +0000 | [diff] [blame] | 1767 | next = findchar(self_s, self_len, from_c); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1768 | |
Antoine Pitrou | a57aae7 | 2010-06-09 16:58:35 +0000 | [diff] [blame] | 1769 | if (next == NULL) { |
| 1770 | /* No matches; return the original bytes */ |
| 1771 | return return_self(self); |
| 1772 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1773 | |
Antoine Pitrou | a57aae7 | 2010-06-09 16:58:35 +0000 | [diff] [blame] | 1774 | /* Need to make a new bytes */ |
| 1775 | result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len); |
| 1776 | if (result == NULL) |
| 1777 | return NULL; |
| 1778 | result_s = PyByteArray_AS_STRING(result); |
| 1779 | Py_MEMCPY(result_s, self_s, self_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1780 | |
Antoine Pitrou | a57aae7 | 2010-06-09 16:58:35 +0000 | [diff] [blame] | 1781 | /* change everything in-place, starting with this one */ |
| 1782 | start = result_s + (next-self_s); |
| 1783 | *start = to_c; |
| 1784 | start++; |
| 1785 | end = result_s + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1786 | |
Antoine Pitrou | a57aae7 | 2010-06-09 16:58:35 +0000 | [diff] [blame] | 1787 | while (--maxcount > 0) { |
| 1788 | next = findchar(start, end-start, from_c); |
| 1789 | if (next == NULL) |
| 1790 | break; |
| 1791 | *next = to_c; |
| 1792 | start = next+1; |
| 1793 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1794 | |
Antoine Pitrou | a57aae7 | 2010-06-09 16:58:35 +0000 | [diff] [blame] | 1795 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
| 1799 | Py_LOCAL(PyByteArrayObject *) |
| 1800 | replace_substring_in_place(PyByteArrayObject *self, |
| 1801 | const char *from_s, Py_ssize_t from_len, |
| 1802 | const char *to_s, Py_ssize_t to_len, |
| 1803 | Py_ssize_t maxcount) |
| 1804 | { |
| 1805 | char *result_s, *start, *end; |
| 1806 | char *self_s; |
| 1807 | Py_ssize_t self_len, offset; |
| 1808 | PyByteArrayObject *result; |
| 1809 | |
| 1810 | /* The result bytes will be the same size */ |
| 1811 | |
| 1812 | self_s = PyByteArray_AS_STRING(self); |
| 1813 | self_len = PyByteArray_GET_SIZE(self); |
| 1814 | |
| 1815 | offset = findstring(self_s, self_len, |
| 1816 | from_s, from_len, |
| 1817 | 0, self_len, FORWARD); |
| 1818 | if (offset == -1) { |
| 1819 | /* No matches; return the original bytes */ |
| 1820 | return return_self(self); |
| 1821 | } |
| 1822 | |
| 1823 | /* Need to make a new bytes */ |
| 1824 | result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len); |
| 1825 | if (result == NULL) |
| 1826 | return NULL; |
| 1827 | result_s = PyByteArray_AS_STRING(result); |
| 1828 | Py_MEMCPY(result_s, self_s, self_len); |
| 1829 | |
| 1830 | /* change everything in-place, starting with this one */ |
| 1831 | start = result_s + offset; |
| 1832 | Py_MEMCPY(start, to_s, from_len); |
| 1833 | start += from_len; |
| 1834 | end = result_s + self_len; |
| 1835 | |
| 1836 | while ( --maxcount > 0) { |
| 1837 | offset = findstring(start, end-start, |
| 1838 | from_s, from_len, |
| 1839 | 0, end-start, FORWARD); |
| 1840 | if (offset==-1) |
| 1841 | break; |
| 1842 | Py_MEMCPY(start+offset, to_s, from_len); |
| 1843 | start += offset+from_len; |
| 1844 | } |
| 1845 | |
| 1846 | return result; |
| 1847 | } |
| 1848 | |
| 1849 | /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
| 1850 | Py_LOCAL(PyByteArrayObject *) |
| 1851 | replace_single_character(PyByteArrayObject *self, |
| 1852 | char from_c, |
| 1853 | const char *to_s, Py_ssize_t to_len, |
| 1854 | Py_ssize_t maxcount) |
| 1855 | { |
| 1856 | char *self_s, *result_s; |
| 1857 | char *start, *next, *end; |
| 1858 | Py_ssize_t self_len, result_len; |
| 1859 | Py_ssize_t count, product; |
| 1860 | PyByteArrayObject *result; |
| 1861 | |
| 1862 | self_s = PyByteArray_AS_STRING(self); |
| 1863 | self_len = PyByteArray_GET_SIZE(self); |
| 1864 | |
| 1865 | count = countchar(self_s, self_len, from_c, maxcount); |
| 1866 | if (count == 0) { |
| 1867 | /* no matches, return unchanged */ |
| 1868 | return return_self(self); |
| 1869 | } |
| 1870 | |
| 1871 | /* use the difference between current and new, hence the "-1" */ |
| 1872 | /* result_len = self_len + count * (to_len-1) */ |
| 1873 | product = count * (to_len-1); |
| 1874 | if (product / (to_len-1) != count) { |
| 1875 | PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
| 1876 | return NULL; |
| 1877 | } |
| 1878 | result_len = self_len + product; |
| 1879 | if (result_len < 0) { |
| 1880 | PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
| 1881 | return NULL; |
| 1882 | } |
| 1883 | |
| 1884 | if ( (result = (PyByteArrayObject *) |
| 1885 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL) |
| 1886 | return NULL; |
| 1887 | result_s = PyByteArray_AS_STRING(result); |
| 1888 | |
| 1889 | start = self_s; |
| 1890 | end = self_s + self_len; |
| 1891 | while (count-- > 0) { |
| 1892 | next = findchar(start, end-start, from_c); |
| 1893 | if (next == NULL) |
| 1894 | break; |
| 1895 | |
| 1896 | if (next == start) { |
| 1897 | /* replace with the 'to' */ |
| 1898 | Py_MEMCPY(result_s, to_s, to_len); |
| 1899 | result_s += to_len; |
| 1900 | start += 1; |
| 1901 | } else { |
| 1902 | /* copy the unchanged old then the 'to' */ |
| 1903 | Py_MEMCPY(result_s, start, next-start); |
| 1904 | result_s += (next-start); |
| 1905 | Py_MEMCPY(result_s, to_s, to_len); |
| 1906 | result_s += to_len; |
| 1907 | start = next+1; |
| 1908 | } |
| 1909 | } |
| 1910 | /* Copy the remainder of the remaining bytes */ |
| 1911 | Py_MEMCPY(result_s, start, end-start); |
| 1912 | |
| 1913 | return result; |
| 1914 | } |
| 1915 | |
| 1916 | /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
| 1917 | Py_LOCAL(PyByteArrayObject *) |
| 1918 | replace_substring(PyByteArrayObject *self, |
| 1919 | const char *from_s, Py_ssize_t from_len, |
| 1920 | const char *to_s, Py_ssize_t to_len, |
| 1921 | Py_ssize_t maxcount) |
| 1922 | { |
| 1923 | char *self_s, *result_s; |
| 1924 | char *start, *next, *end; |
| 1925 | Py_ssize_t self_len, result_len; |
| 1926 | Py_ssize_t count, offset, product; |
| 1927 | PyByteArrayObject *result; |
| 1928 | |
| 1929 | self_s = PyByteArray_AS_STRING(self); |
| 1930 | self_len = PyByteArray_GET_SIZE(self); |
| 1931 | |
| 1932 | count = countstring(self_s, self_len, |
| 1933 | from_s, from_len, |
| 1934 | 0, self_len, FORWARD, maxcount); |
| 1935 | if (count == 0) { |
| 1936 | /* no matches, return unchanged */ |
| 1937 | return return_self(self); |
| 1938 | } |
| 1939 | |
| 1940 | /* Check for overflow */ |
| 1941 | /* result_len = self_len + count * (to_len-from_len) */ |
| 1942 | product = count * (to_len-from_len); |
| 1943 | if (product / (to_len-from_len) != count) { |
| 1944 | PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
| 1945 | return NULL; |
| 1946 | } |
| 1947 | result_len = self_len + product; |
| 1948 | if (result_len < 0) { |
| 1949 | PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
| 1950 | return NULL; |
| 1951 | } |
| 1952 | |
| 1953 | if ( (result = (PyByteArrayObject *) |
| 1954 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL) |
| 1955 | return NULL; |
| 1956 | result_s = PyByteArray_AS_STRING(result); |
| 1957 | |
| 1958 | start = self_s; |
| 1959 | end = self_s + self_len; |
| 1960 | while (count-- > 0) { |
| 1961 | offset = findstring(start, end-start, |
| 1962 | from_s, from_len, |
| 1963 | 0, end-start, FORWARD); |
| 1964 | if (offset == -1) |
| 1965 | break; |
| 1966 | next = start+offset; |
| 1967 | if (next == start) { |
| 1968 | /* replace with the 'to' */ |
| 1969 | Py_MEMCPY(result_s, to_s, to_len); |
| 1970 | result_s += to_len; |
| 1971 | start += from_len; |
| 1972 | } else { |
| 1973 | /* copy the unchanged old then the 'to' */ |
| 1974 | Py_MEMCPY(result_s, start, next-start); |
| 1975 | result_s += (next-start); |
| 1976 | Py_MEMCPY(result_s, to_s, to_len); |
| 1977 | result_s += to_len; |
| 1978 | start = next+from_len; |
| 1979 | } |
| 1980 | } |
| 1981 | /* Copy the remainder of the remaining bytes */ |
| 1982 | Py_MEMCPY(result_s, start, end-start); |
| 1983 | |
| 1984 | return result; |
| 1985 | } |
| 1986 | |
| 1987 | |
| 1988 | Py_LOCAL(PyByteArrayObject *) |
| 1989 | replace(PyByteArrayObject *self, |
| 1990 | const char *from_s, Py_ssize_t from_len, |
| 1991 | const char *to_s, Py_ssize_t to_len, |
| 1992 | Py_ssize_t maxcount) |
| 1993 | { |
| 1994 | if (maxcount < 0) { |
| 1995 | maxcount = PY_SSIZE_T_MAX; |
| 1996 | } else if (maxcount == 0 || PyByteArray_GET_SIZE(self) == 0) { |
| 1997 | /* nothing to do; return the original bytes */ |
| 1998 | return return_self(self); |
| 1999 | } |
| 2000 | |
| 2001 | if (maxcount == 0 || |
| 2002 | (from_len == 0 && to_len == 0)) { |
| 2003 | /* nothing to do; return the original bytes */ |
| 2004 | return return_self(self); |
| 2005 | } |
| 2006 | |
| 2007 | /* Handle zero-length special cases */ |
| 2008 | |
| 2009 | if (from_len == 0) { |
| 2010 | /* insert the 'to' bytes everywhere. */ |
| 2011 | /* >>> "Python".replace("", ".") */ |
| 2012 | /* '.P.y.t.h.o.n.' */ |
| 2013 | return replace_interleave(self, to_s, to_len, maxcount); |
| 2014 | } |
| 2015 | |
| 2016 | /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
| 2017 | /* point for an empty self bytes to generate a non-empty bytes */ |
| 2018 | /* Special case so the remaining code always gets a non-empty bytes */ |
| 2019 | if (PyByteArray_GET_SIZE(self) == 0) { |
| 2020 | return return_self(self); |
| 2021 | } |
| 2022 | |
| 2023 | if (to_len == 0) { |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2024 | /* delete all occurrences of 'from' bytes */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2025 | if (from_len == 1) { |
| 2026 | return replace_delete_single_character( |
| 2027 | self, from_s[0], maxcount); |
| 2028 | } else { |
| 2029 | return replace_delete_substring(self, from_s, from_len, maxcount); |
| 2030 | } |
| 2031 | } |
| 2032 | |
| 2033 | /* Handle special case where both bytes have the same length */ |
| 2034 | |
| 2035 | if (from_len == to_len) { |
| 2036 | if (from_len == 1) { |
| 2037 | return replace_single_character_in_place( |
| 2038 | self, |
| 2039 | from_s[0], |
| 2040 | to_s[0], |
| 2041 | maxcount); |
| 2042 | } else { |
| 2043 | return replace_substring_in_place( |
| 2044 | self, from_s, from_len, to_s, to_len, maxcount); |
| 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | /* Otherwise use the more generic algorithms */ |
| 2049 | if (from_len == 1) { |
| 2050 | return replace_single_character(self, from_s[0], |
| 2051 | to_s, to_len, maxcount); |
| 2052 | } else { |
| 2053 | /* len('from')>=2, len('to')>=1 */ |
| 2054 | return replace_substring(self, from_s, from_len, to_s, to_len, maxcount); |
| 2055 | } |
| 2056 | } |
| 2057 | |
| 2058 | |
| 2059 | PyDoc_STRVAR(replace__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2060 | "B.replace(old, new[, count]) -> bytearray\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2061 | \n\ |
| 2062 | Return a copy of B with all occurrences of subsection\n\ |
| 2063 | old replaced by new. If the optional argument count is\n\ |
| 2064 | given, only the first count occurrences are replaced."); |
| 2065 | |
| 2066 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2067 | bytearray_replace(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2068 | { |
| 2069 | Py_ssize_t count = -1; |
| 2070 | PyObject *from, *to, *res; |
| 2071 | Py_buffer vfrom, vto; |
| 2072 | |
| 2073 | if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count)) |
| 2074 | return NULL; |
| 2075 | |
| 2076 | if (_getbuffer(from, &vfrom) < 0) |
| 2077 | return NULL; |
| 2078 | if (_getbuffer(to, &vto) < 0) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2079 | PyBuffer_Release(&vfrom); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2080 | return NULL; |
| 2081 | } |
| 2082 | |
| 2083 | res = (PyObject *)replace((PyByteArrayObject *) self, |
| 2084 | vfrom.buf, vfrom.len, |
| 2085 | vto.buf, vto.len, count); |
| 2086 | |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2087 | PyBuffer_Release(&vfrom); |
| 2088 | PyBuffer_Release(&vto); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2089 | return res; |
| 2090 | } |
| 2091 | |
| 2092 | |
| 2093 | /* Overallocate the initial list to reduce the number of reallocs for small |
| 2094 | split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three |
| 2095 | resizes, to sizes 4, 8, then 16. Most observed string splits are for human |
| 2096 | text (roughly 11 words per line) and field delimited data (usually 1-10 |
| 2097 | fields). For large strings the split algorithms are bandwidth limited |
| 2098 | so increasing the preallocation likely will not improve things.*/ |
| 2099 | |
| 2100 | #define MAX_PREALLOC 12 |
| 2101 | |
| 2102 | /* 5 splits gives 6 elements */ |
| 2103 | #define PREALLOC_SIZE(maxsplit) \ |
| 2104 | (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1) |
| 2105 | |
| 2106 | #define SPLIT_APPEND(data, left, right) \ |
| 2107 | str = PyByteArray_FromStringAndSize((data) + (left), \ |
| 2108 | (right) - (left)); \ |
| 2109 | if (str == NULL) \ |
| 2110 | goto onError; \ |
| 2111 | if (PyList_Append(list, str)) { \ |
| 2112 | Py_DECREF(str); \ |
| 2113 | goto onError; \ |
| 2114 | } \ |
| 2115 | else \ |
| 2116 | Py_DECREF(str); |
| 2117 | |
| 2118 | #define SPLIT_ADD(data, left, right) { \ |
| 2119 | str = PyByteArray_FromStringAndSize((data) + (left), \ |
| 2120 | (right) - (left)); \ |
| 2121 | if (str == NULL) \ |
| 2122 | goto onError; \ |
| 2123 | if (count < MAX_PREALLOC) { \ |
| 2124 | PyList_SET_ITEM(list, count, str); \ |
| 2125 | } else { \ |
| 2126 | if (PyList_Append(list, str)) { \ |
| 2127 | Py_DECREF(str); \ |
| 2128 | goto onError; \ |
| 2129 | } \ |
| 2130 | else \ |
| 2131 | Py_DECREF(str); \ |
| 2132 | } \ |
| 2133 | count++; } |
| 2134 | |
| 2135 | /* Always force the list to the expected size. */ |
| 2136 | #define FIX_PREALLOC_SIZE(list) Py_SIZE(list) = count |
| 2137 | |
| 2138 | |
| 2139 | Py_LOCAL_INLINE(PyObject *) |
| 2140 | split_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount) |
| 2141 | { |
| 2142 | register Py_ssize_t i, j, count = 0; |
| 2143 | PyObject *str; |
| 2144 | PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
| 2145 | |
| 2146 | if (list == NULL) |
| 2147 | return NULL; |
| 2148 | |
| 2149 | i = j = 0; |
| 2150 | while ((j < len) && (maxcount-- > 0)) { |
| 2151 | for(; j < len; j++) { |
| 2152 | /* I found that using memchr makes no difference */ |
| 2153 | if (s[j] == ch) { |
| 2154 | SPLIT_ADD(s, i, j); |
| 2155 | i = j = j + 1; |
| 2156 | break; |
| 2157 | } |
| 2158 | } |
| 2159 | } |
| 2160 | if (i <= len) { |
| 2161 | SPLIT_ADD(s, i, len); |
| 2162 | } |
| 2163 | FIX_PREALLOC_SIZE(list); |
| 2164 | return list; |
| 2165 | |
| 2166 | onError: |
| 2167 | Py_DECREF(list); |
| 2168 | return NULL; |
| 2169 | } |
| 2170 | |
| 2171 | |
| 2172 | Py_LOCAL_INLINE(PyObject *) |
| 2173 | split_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxcount) |
| 2174 | { |
| 2175 | register Py_ssize_t i, j, count = 0; |
| 2176 | PyObject *str; |
| 2177 | PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
| 2178 | |
| 2179 | if (list == NULL) |
| 2180 | return NULL; |
| 2181 | |
| 2182 | for (i = j = 0; i < len; ) { |
| 2183 | /* find a token */ |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 2184 | while (i < len && Py_ISSPACE(s[i])) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2185 | i++; |
| 2186 | j = i; |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 2187 | while (i < len && !Py_ISSPACE(s[i])) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2188 | i++; |
| 2189 | if (j < i) { |
| 2190 | if (maxcount-- <= 0) |
| 2191 | break; |
| 2192 | SPLIT_ADD(s, j, i); |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 2193 | while (i < len && Py_ISSPACE(s[i])) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2194 | i++; |
| 2195 | j = i; |
| 2196 | } |
| 2197 | } |
| 2198 | if (j < len) { |
| 2199 | SPLIT_ADD(s, j, len); |
| 2200 | } |
| 2201 | FIX_PREALLOC_SIZE(list); |
| 2202 | return list; |
| 2203 | |
| 2204 | onError: |
| 2205 | Py_DECREF(list); |
| 2206 | return NULL; |
| 2207 | } |
| 2208 | |
| 2209 | PyDoc_STRVAR(split__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2210 | "B.split([sep[, maxsplit]]) -> list of bytearrays\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2211 | \n\ |
| 2212 | Return a list of the sections in B, using sep as the delimiter.\n\ |
| 2213 | If sep is not given, B is split on ASCII whitespace characters\n\ |
| 2214 | (space, tab, return, newline, formfeed, vertical tab).\n\ |
| 2215 | If maxsplit is given, at most maxsplit splits are done."); |
| 2216 | |
| 2217 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2218 | bytearray_split(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2219 | { |
| 2220 | Py_ssize_t len = PyByteArray_GET_SIZE(self), n, i, j; |
| 2221 | Py_ssize_t maxsplit = -1, count = 0; |
| 2222 | const char *s = PyByteArray_AS_STRING(self), *sub; |
| 2223 | PyObject *list, *str, *subobj = Py_None; |
| 2224 | Py_buffer vsub; |
| 2225 | #ifdef USE_FAST |
| 2226 | Py_ssize_t pos; |
| 2227 | #endif |
| 2228 | |
| 2229 | if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit)) |
| 2230 | return NULL; |
| 2231 | if (maxsplit < 0) |
| 2232 | maxsplit = PY_SSIZE_T_MAX; |
| 2233 | |
| 2234 | if (subobj == Py_None) |
| 2235 | return split_whitespace(s, len, maxsplit); |
| 2236 | |
| 2237 | if (_getbuffer(subobj, &vsub) < 0) |
| 2238 | return NULL; |
| 2239 | sub = vsub.buf; |
| 2240 | n = vsub.len; |
| 2241 | |
| 2242 | if (n == 0) { |
| 2243 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2244 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2245 | return NULL; |
| 2246 | } |
Benjamin Peterson | c4fe6f3 | 2008-08-19 18:57:56 +0000 | [diff] [blame] | 2247 | if (n == 1) { |
| 2248 | list = split_char(s, len, sub[0], maxsplit); |
| 2249 | PyBuffer_Release(&vsub); |
| 2250 | return list; |
| 2251 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2252 | |
| 2253 | list = PyList_New(PREALLOC_SIZE(maxsplit)); |
| 2254 | if (list == NULL) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2255 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2256 | return NULL; |
| 2257 | } |
| 2258 | |
| 2259 | #ifdef USE_FAST |
| 2260 | i = j = 0; |
| 2261 | while (maxsplit-- > 0) { |
| 2262 | pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH); |
| 2263 | if (pos < 0) |
| 2264 | break; |
| 2265 | j = i+pos; |
| 2266 | SPLIT_ADD(s, i, j); |
| 2267 | i = j + n; |
| 2268 | } |
| 2269 | #else |
| 2270 | i = j = 0; |
| 2271 | while ((j+n <= len) && (maxsplit-- > 0)) { |
| 2272 | for (; j+n <= len; j++) { |
| 2273 | if (Py_STRING_MATCH(s, j, sub, n)) { |
| 2274 | SPLIT_ADD(s, i, j); |
| 2275 | i = j = j + n; |
| 2276 | break; |
| 2277 | } |
| 2278 | } |
| 2279 | } |
| 2280 | #endif |
| 2281 | SPLIT_ADD(s, i, len); |
| 2282 | FIX_PREALLOC_SIZE(list); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2283 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2284 | return list; |
| 2285 | |
| 2286 | onError: |
| 2287 | Py_DECREF(list); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2288 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2289 | return NULL; |
| 2290 | } |
| 2291 | |
| 2292 | /* stringlib's partition shares nullbytes in some cases. |
| 2293 | undo this, we don't want the nullbytes to be shared. */ |
| 2294 | static PyObject * |
| 2295 | make_nullbytes_unique(PyObject *result) |
| 2296 | { |
| 2297 | if (result != NULL) { |
| 2298 | int i; |
| 2299 | assert(PyTuple_Check(result)); |
| 2300 | assert(PyTuple_GET_SIZE(result) == 3); |
| 2301 | for (i = 0; i < 3; i++) { |
| 2302 | if (PyTuple_GET_ITEM(result, i) == (PyObject *)nullbytes) { |
| 2303 | PyObject *new = PyByteArray_FromStringAndSize(NULL, 0); |
| 2304 | if (new == NULL) { |
| 2305 | Py_DECREF(result); |
| 2306 | result = NULL; |
| 2307 | break; |
| 2308 | } |
| 2309 | Py_DECREF(nullbytes); |
| 2310 | PyTuple_SET_ITEM(result, i, new); |
| 2311 | } |
| 2312 | } |
| 2313 | } |
| 2314 | return result; |
| 2315 | } |
| 2316 | |
| 2317 | PyDoc_STRVAR(partition__doc__, |
| 2318 | "B.partition(sep) -> (head, sep, tail)\n\ |
| 2319 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2320 | Search for the separator sep in B, and return the part before it,\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2321 | the separator itself, and the part after it. If the separator is not\n\ |
| 2322 | found, returns B and two empty bytearray objects."); |
| 2323 | |
| 2324 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2325 | bytearray_partition(PyByteArrayObject *self, PyObject *sep_obj) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2326 | { |
| 2327 | PyObject *bytesep, *result; |
| 2328 | |
| 2329 | bytesep = PyByteArray_FromObject(sep_obj); |
| 2330 | if (! bytesep) |
| 2331 | return NULL; |
| 2332 | |
| 2333 | result = stringlib_partition( |
| 2334 | (PyObject*) self, |
| 2335 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 2336 | bytesep, |
| 2337 | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
| 2338 | ); |
| 2339 | |
| 2340 | Py_DECREF(bytesep); |
| 2341 | return make_nullbytes_unique(result); |
| 2342 | } |
| 2343 | |
| 2344 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 4c81fbb | 2010-01-25 12:02:24 +0000 | [diff] [blame] | 2345 | "B.rpartition(sep) -> (head, sep, tail)\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2346 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2347 | Search for the separator sep in B, starting at the end of B,\n\ |
| 2348 | and return the part before it, the separator itself, and the\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2349 | part after it. If the separator is not found, returns two empty\n\ |
| 2350 | bytearray objects and B."); |
| 2351 | |
| 2352 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2353 | bytearray_rpartition(PyByteArrayObject *self, PyObject *sep_obj) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2354 | { |
| 2355 | PyObject *bytesep, *result; |
| 2356 | |
| 2357 | bytesep = PyByteArray_FromObject(sep_obj); |
| 2358 | if (! bytesep) |
| 2359 | return NULL; |
| 2360 | |
| 2361 | result = stringlib_rpartition( |
| 2362 | (PyObject*) self, |
| 2363 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 2364 | bytesep, |
| 2365 | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
| 2366 | ); |
| 2367 | |
| 2368 | Py_DECREF(bytesep); |
| 2369 | return make_nullbytes_unique(result); |
| 2370 | } |
| 2371 | |
| 2372 | Py_LOCAL_INLINE(PyObject *) |
| 2373 | rsplit_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount) |
| 2374 | { |
| 2375 | register Py_ssize_t i, j, count=0; |
| 2376 | PyObject *str; |
| 2377 | PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
| 2378 | |
| 2379 | if (list == NULL) |
| 2380 | return NULL; |
| 2381 | |
| 2382 | i = j = len - 1; |
| 2383 | while ((i >= 0) && (maxcount-- > 0)) { |
| 2384 | for (; i >= 0; i--) { |
| 2385 | if (s[i] == ch) { |
| 2386 | SPLIT_ADD(s, i + 1, j + 1); |
| 2387 | j = i = i - 1; |
| 2388 | break; |
| 2389 | } |
| 2390 | } |
| 2391 | } |
| 2392 | if (j >= -1) { |
| 2393 | SPLIT_ADD(s, 0, j + 1); |
| 2394 | } |
| 2395 | FIX_PREALLOC_SIZE(list); |
| 2396 | if (PyList_Reverse(list) < 0) |
| 2397 | goto onError; |
| 2398 | |
| 2399 | return list; |
| 2400 | |
| 2401 | onError: |
| 2402 | Py_DECREF(list); |
| 2403 | return NULL; |
| 2404 | } |
| 2405 | |
| 2406 | Py_LOCAL_INLINE(PyObject *) |
| 2407 | rsplit_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxcount) |
| 2408 | { |
| 2409 | register Py_ssize_t i, j, count = 0; |
| 2410 | PyObject *str; |
| 2411 | PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
| 2412 | |
| 2413 | if (list == NULL) |
| 2414 | return NULL; |
| 2415 | |
| 2416 | for (i = j = len - 1; i >= 0; ) { |
| 2417 | /* find a token */ |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 2418 | while (i >= 0 && Py_ISSPACE(s[i])) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2419 | i--; |
| 2420 | j = i; |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 2421 | while (i >= 0 && !Py_ISSPACE(s[i])) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2422 | i--; |
| 2423 | if (j > i) { |
| 2424 | if (maxcount-- <= 0) |
| 2425 | break; |
| 2426 | SPLIT_ADD(s, i + 1, j + 1); |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 2427 | while (i >= 0 && Py_ISSPACE(s[i])) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2428 | i--; |
| 2429 | j = i; |
| 2430 | } |
| 2431 | } |
| 2432 | if (j >= 0) { |
| 2433 | SPLIT_ADD(s, 0, j + 1); |
| 2434 | } |
| 2435 | FIX_PREALLOC_SIZE(list); |
| 2436 | if (PyList_Reverse(list) < 0) |
| 2437 | goto onError; |
| 2438 | |
| 2439 | return list; |
| 2440 | |
| 2441 | onError: |
| 2442 | Py_DECREF(list); |
| 2443 | return NULL; |
| 2444 | } |
| 2445 | |
| 2446 | PyDoc_STRVAR(rsplit__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2447 | "B.rsplit(sep[, maxsplit]) -> list of bytearrays\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2448 | \n\ |
| 2449 | Return a list of the sections in B, using sep as the delimiter,\n\ |
| 2450 | starting at the end of B and working to the front.\n\ |
| 2451 | If sep is not given, B is split on ASCII whitespace characters\n\ |
| 2452 | (space, tab, return, newline, formfeed, vertical tab).\n\ |
| 2453 | If maxsplit is given, at most maxsplit splits are done."); |
| 2454 | |
| 2455 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2456 | bytearray_rsplit(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2457 | { |
| 2458 | Py_ssize_t len = PyByteArray_GET_SIZE(self), n, i, j; |
| 2459 | Py_ssize_t maxsplit = -1, count = 0; |
| 2460 | const char *s = PyByteArray_AS_STRING(self), *sub; |
| 2461 | PyObject *list, *str, *subobj = Py_None; |
| 2462 | Py_buffer vsub; |
| 2463 | |
| 2464 | if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit)) |
| 2465 | return NULL; |
| 2466 | if (maxsplit < 0) |
| 2467 | maxsplit = PY_SSIZE_T_MAX; |
| 2468 | |
| 2469 | if (subobj == Py_None) |
| 2470 | return rsplit_whitespace(s, len, maxsplit); |
| 2471 | |
| 2472 | if (_getbuffer(subobj, &vsub) < 0) |
| 2473 | return NULL; |
| 2474 | sub = vsub.buf; |
| 2475 | n = vsub.len; |
| 2476 | |
| 2477 | if (n == 0) { |
| 2478 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2479 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2480 | return NULL; |
| 2481 | } |
Benjamin Peterson | c4fe6f3 | 2008-08-19 18:57:56 +0000 | [diff] [blame] | 2482 | else if (n == 1) { |
| 2483 | list = rsplit_char(s, len, sub[0], maxsplit); |
| 2484 | PyBuffer_Release(&vsub); |
| 2485 | return list; |
| 2486 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2487 | |
| 2488 | list = PyList_New(PREALLOC_SIZE(maxsplit)); |
| 2489 | if (list == NULL) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2490 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2491 | return NULL; |
| 2492 | } |
| 2493 | |
| 2494 | j = len; |
| 2495 | i = j - n; |
| 2496 | |
| 2497 | while ( (i >= 0) && (maxsplit-- > 0) ) { |
| 2498 | for (; i>=0; i--) { |
| 2499 | if (Py_STRING_MATCH(s, i, sub, n)) { |
| 2500 | SPLIT_ADD(s, i + n, j); |
| 2501 | j = i; |
| 2502 | i -= n; |
| 2503 | break; |
| 2504 | } |
| 2505 | } |
| 2506 | } |
| 2507 | SPLIT_ADD(s, 0, j); |
| 2508 | FIX_PREALLOC_SIZE(list); |
| 2509 | if (PyList_Reverse(list) < 0) |
| 2510 | goto onError; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2511 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2512 | return list; |
| 2513 | |
| 2514 | onError: |
| 2515 | Py_DECREF(list); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2516 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2517 | return NULL; |
| 2518 | } |
| 2519 | |
| 2520 | PyDoc_STRVAR(reverse__doc__, |
| 2521 | "B.reverse() -> None\n\ |
| 2522 | \n\ |
| 2523 | Reverse the order of the values in B in place."); |
| 2524 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2525 | bytearray_reverse(PyByteArrayObject *self, PyObject *unused) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2526 | { |
| 2527 | char swap, *head, *tail; |
| 2528 | Py_ssize_t i, j, n = Py_SIZE(self); |
| 2529 | |
| 2530 | j = n / 2; |
| 2531 | head = self->ob_bytes; |
| 2532 | tail = head + n - 1; |
| 2533 | for (i = 0; i < j; i++) { |
| 2534 | swap = *head; |
| 2535 | *head++ = *tail; |
| 2536 | *tail-- = swap; |
| 2537 | } |
| 2538 | |
| 2539 | Py_RETURN_NONE; |
| 2540 | } |
| 2541 | |
| 2542 | PyDoc_STRVAR(insert__doc__, |
| 2543 | "B.insert(index, int) -> None\n\ |
| 2544 | \n\ |
| 2545 | Insert a single item into the bytearray before the given index."); |
| 2546 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2547 | bytearray_insert(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2548 | { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 2549 | PyObject *value; |
| 2550 | int ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2551 | Py_ssize_t where, n = Py_SIZE(self); |
| 2552 | |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 2553 | if (!PyArg_ParseTuple(args, "nO:insert", &where, &value)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2554 | return NULL; |
| 2555 | |
| 2556 | if (n == PY_SSIZE_T_MAX) { |
| 2557 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 099d7a0 | 2009-09-06 10:35:38 +0000 | [diff] [blame] | 2558 | "cannot add more objects to bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2559 | return NULL; |
| 2560 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 2561 | if (!_getbytevalue(value, &ival)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2562 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2563 | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
| 2564 | return NULL; |
| 2565 | |
| 2566 | if (where < 0) { |
| 2567 | where += n; |
| 2568 | if (where < 0) |
| 2569 | where = 0; |
| 2570 | } |
| 2571 | if (where > n) |
| 2572 | where = n; |
| 2573 | memmove(self->ob_bytes + where + 1, self->ob_bytes + where, n - where); |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 2574 | self->ob_bytes[where] = ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2575 | |
| 2576 | Py_RETURN_NONE; |
| 2577 | } |
| 2578 | |
| 2579 | PyDoc_STRVAR(append__doc__, |
| 2580 | "B.append(int) -> None\n\ |
| 2581 | \n\ |
| 2582 | Append a single item to the end of B."); |
| 2583 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2584 | bytearray_append(PyByteArrayObject *self, PyObject *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2585 | { |
| 2586 | int value; |
| 2587 | Py_ssize_t n = Py_SIZE(self); |
| 2588 | |
| 2589 | if (! _getbytevalue(arg, &value)) |
| 2590 | return NULL; |
| 2591 | if (n == PY_SSIZE_T_MAX) { |
| 2592 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 099d7a0 | 2009-09-06 10:35:38 +0000 | [diff] [blame] | 2593 | "cannot add more objects to bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2594 | return NULL; |
| 2595 | } |
| 2596 | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
| 2597 | return NULL; |
| 2598 | |
| 2599 | self->ob_bytes[n] = value; |
| 2600 | |
| 2601 | Py_RETURN_NONE; |
| 2602 | } |
| 2603 | |
| 2604 | PyDoc_STRVAR(extend__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2605 | "B.extend(iterable_of_ints) -> None\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2606 | \n\ |
| 2607 | Append all the elements from the iterator or sequence to the\n\ |
| 2608 | end of B."); |
| 2609 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2610 | bytearray_extend(PyByteArrayObject *self, PyObject *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2611 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2612 | PyObject *it, *item, *bytearray_obj; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2613 | Py_ssize_t buf_size = 0, len = 0; |
| 2614 | int value; |
| 2615 | char *buf; |
| 2616 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2617 | /* bytearray_setslice code only accepts something supporting PEP 3118. */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2618 | if (PyObject_CheckBuffer(arg)) { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2619 | if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), arg) == -1) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2620 | return NULL; |
| 2621 | |
| 2622 | Py_RETURN_NONE; |
| 2623 | } |
| 2624 | |
| 2625 | it = PyObject_GetIter(arg); |
| 2626 | if (it == NULL) |
| 2627 | return NULL; |
| 2628 | |
| 2629 | /* Try to determine the length of the argument. 32 is abitrary. */ |
| 2630 | buf_size = _PyObject_LengthHint(arg, 32); |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2631 | if (buf_size == -1) { |
| 2632 | Py_DECREF(it); |
| 2633 | return NULL; |
| 2634 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2635 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2636 | bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size); |
| 2637 | if (bytearray_obj == NULL) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2638 | return NULL; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2639 | buf = PyByteArray_AS_STRING(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2640 | |
| 2641 | while ((item = PyIter_Next(it)) != NULL) { |
| 2642 | if (! _getbytevalue(item, &value)) { |
| 2643 | Py_DECREF(item); |
| 2644 | Py_DECREF(it); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2645 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2646 | return NULL; |
| 2647 | } |
| 2648 | buf[len++] = value; |
| 2649 | Py_DECREF(item); |
| 2650 | |
| 2651 | if (len >= buf_size) { |
| 2652 | buf_size = len + (len >> 1) + 1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2653 | if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2654 | Py_DECREF(it); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2655 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2656 | return NULL; |
| 2657 | } |
| 2658 | /* Recompute the `buf' pointer, since the resizing operation may |
| 2659 | have invalidated it. */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2660 | buf = PyByteArray_AS_STRING(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2661 | } |
| 2662 | } |
| 2663 | Py_DECREF(it); |
| 2664 | |
| 2665 | /* Resize down to exact size. */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2666 | if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) { |
| 2667 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2668 | return NULL; |
| 2669 | } |
| 2670 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2671 | if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2672 | return NULL; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2673 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2674 | |
| 2675 | Py_RETURN_NONE; |
| 2676 | } |
| 2677 | |
| 2678 | PyDoc_STRVAR(pop__doc__, |
| 2679 | "B.pop([index]) -> int\n\ |
| 2680 | \n\ |
| 2681 | Remove and return a single item from B. If no index\n\ |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 2682 | argument is given, will pop the last value."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2683 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2684 | bytearray_pop(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2685 | { |
| 2686 | int value; |
| 2687 | Py_ssize_t where = -1, n = Py_SIZE(self); |
| 2688 | |
| 2689 | if (!PyArg_ParseTuple(args, "|n:pop", &where)) |
| 2690 | return NULL; |
| 2691 | |
| 2692 | if (n == 0) { |
| 2693 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 099d7a0 | 2009-09-06 10:35:38 +0000 | [diff] [blame] | 2694 | "cannot pop an empty bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2695 | return NULL; |
| 2696 | } |
| 2697 | if (where < 0) |
| 2698 | where += Py_SIZE(self); |
| 2699 | if (where < 0 || where >= Py_SIZE(self)) { |
| 2700 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 2701 | return NULL; |
| 2702 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 2703 | if (!_canresize(self)) |
| 2704 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2705 | |
| 2706 | value = self->ob_bytes[where]; |
| 2707 | memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where); |
| 2708 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 2709 | return NULL; |
| 2710 | |
Mark Dickinson | 424d75a | 2009-09-06 10:20:23 +0000 | [diff] [blame] | 2711 | return PyLong_FromLong((unsigned char)value); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2712 | } |
| 2713 | |
| 2714 | PyDoc_STRVAR(remove__doc__, |
| 2715 | "B.remove(int) -> None\n\ |
| 2716 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2717 | Remove the first occurrence of a value in B."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2718 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2719 | bytearray_remove(PyByteArrayObject *self, PyObject *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2720 | { |
| 2721 | int value; |
| 2722 | Py_ssize_t where, n = Py_SIZE(self); |
| 2723 | |
| 2724 | if (! _getbytevalue(arg, &value)) |
| 2725 | return NULL; |
| 2726 | |
| 2727 | for (where = 0; where < n; where++) { |
| 2728 | if (self->ob_bytes[where] == value) |
| 2729 | break; |
| 2730 | } |
| 2731 | if (where == n) { |
Mark Dickinson | 099d7a0 | 2009-09-06 10:35:38 +0000 | [diff] [blame] | 2732 | PyErr_SetString(PyExc_ValueError, "value not found in bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2733 | return NULL; |
| 2734 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 2735 | if (!_canresize(self)) |
| 2736 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2737 | |
| 2738 | memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where); |
| 2739 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 2740 | return NULL; |
| 2741 | |
| 2742 | Py_RETURN_NONE; |
| 2743 | } |
| 2744 | |
| 2745 | /* XXX These two helpers could be optimized if argsize == 1 */ |
| 2746 | |
| 2747 | static Py_ssize_t |
| 2748 | lstrip_helper(unsigned char *myptr, Py_ssize_t mysize, |
| 2749 | void *argptr, Py_ssize_t argsize) |
| 2750 | { |
| 2751 | Py_ssize_t i = 0; |
| 2752 | while (i < mysize && memchr(argptr, myptr[i], argsize)) |
| 2753 | i++; |
| 2754 | return i; |
| 2755 | } |
| 2756 | |
| 2757 | static Py_ssize_t |
| 2758 | rstrip_helper(unsigned char *myptr, Py_ssize_t mysize, |
| 2759 | void *argptr, Py_ssize_t argsize) |
| 2760 | { |
| 2761 | Py_ssize_t i = mysize - 1; |
| 2762 | while (i >= 0 && memchr(argptr, myptr[i], argsize)) |
| 2763 | i--; |
| 2764 | return i + 1; |
| 2765 | } |
| 2766 | |
| 2767 | PyDoc_STRVAR(strip__doc__, |
| 2768 | "B.strip([bytes]) -> bytearray\n\ |
| 2769 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2770 | Strip leading and trailing bytes contained in the argument\n\ |
| 2771 | and return the result as a new bytearray.\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2772 | If the argument is omitted, strip ASCII whitespace."); |
| 2773 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2774 | bytearray_strip(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2775 | { |
| 2776 | Py_ssize_t left, right, mysize, argsize; |
| 2777 | void *myptr, *argptr; |
| 2778 | PyObject *arg = Py_None; |
| 2779 | Py_buffer varg; |
| 2780 | if (!PyArg_ParseTuple(args, "|O:strip", &arg)) |
| 2781 | return NULL; |
| 2782 | if (arg == Py_None) { |
| 2783 | argptr = "\t\n\r\f\v "; |
| 2784 | argsize = 6; |
| 2785 | } |
| 2786 | else { |
| 2787 | if (_getbuffer(arg, &varg) < 0) |
| 2788 | return NULL; |
| 2789 | argptr = varg.buf; |
| 2790 | argsize = varg.len; |
| 2791 | } |
| 2792 | myptr = self->ob_bytes; |
| 2793 | mysize = Py_SIZE(self); |
| 2794 | left = lstrip_helper(myptr, mysize, argptr, argsize); |
| 2795 | if (left == mysize) |
| 2796 | right = left; |
| 2797 | else |
| 2798 | right = rstrip_helper(myptr, mysize, argptr, argsize); |
| 2799 | if (arg != Py_None) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2800 | PyBuffer_Release(&varg); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2801 | return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left); |
| 2802 | } |
| 2803 | |
| 2804 | PyDoc_STRVAR(lstrip__doc__, |
| 2805 | "B.lstrip([bytes]) -> bytearray\n\ |
| 2806 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2807 | Strip leading bytes contained in the argument\n\ |
| 2808 | and return the result as a new bytearray.\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2809 | If the argument is omitted, strip leading ASCII whitespace."); |
| 2810 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2811 | bytearray_lstrip(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2812 | { |
| 2813 | Py_ssize_t left, right, mysize, argsize; |
| 2814 | void *myptr, *argptr; |
| 2815 | PyObject *arg = Py_None; |
| 2816 | Py_buffer varg; |
| 2817 | if (!PyArg_ParseTuple(args, "|O:lstrip", &arg)) |
| 2818 | return NULL; |
| 2819 | if (arg == Py_None) { |
| 2820 | argptr = "\t\n\r\f\v "; |
| 2821 | argsize = 6; |
| 2822 | } |
| 2823 | else { |
| 2824 | if (_getbuffer(arg, &varg) < 0) |
| 2825 | return NULL; |
| 2826 | argptr = varg.buf; |
| 2827 | argsize = varg.len; |
| 2828 | } |
| 2829 | myptr = self->ob_bytes; |
| 2830 | mysize = Py_SIZE(self); |
| 2831 | left = lstrip_helper(myptr, mysize, argptr, argsize); |
| 2832 | right = mysize; |
| 2833 | if (arg != Py_None) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2834 | PyBuffer_Release(&varg); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2835 | return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left); |
| 2836 | } |
| 2837 | |
| 2838 | PyDoc_STRVAR(rstrip__doc__, |
| 2839 | "B.rstrip([bytes]) -> bytearray\n\ |
| 2840 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2841 | Strip trailing bytes contained in the argument\n\ |
| 2842 | and return the result as a new bytearray.\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2843 | If the argument is omitted, strip trailing ASCII whitespace."); |
| 2844 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2845 | bytearray_rstrip(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2846 | { |
| 2847 | Py_ssize_t left, right, mysize, argsize; |
| 2848 | void *myptr, *argptr; |
| 2849 | PyObject *arg = Py_None; |
| 2850 | Py_buffer varg; |
| 2851 | if (!PyArg_ParseTuple(args, "|O:rstrip", &arg)) |
| 2852 | return NULL; |
| 2853 | if (arg == Py_None) { |
| 2854 | argptr = "\t\n\r\f\v "; |
| 2855 | argsize = 6; |
| 2856 | } |
| 2857 | else { |
| 2858 | if (_getbuffer(arg, &varg) < 0) |
| 2859 | return NULL; |
| 2860 | argptr = varg.buf; |
| 2861 | argsize = varg.len; |
| 2862 | } |
| 2863 | myptr = self->ob_bytes; |
| 2864 | mysize = Py_SIZE(self); |
| 2865 | left = 0; |
| 2866 | right = rstrip_helper(myptr, mysize, argptr, argsize); |
| 2867 | if (arg != Py_None) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2868 | PyBuffer_Release(&varg); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2869 | return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left); |
| 2870 | } |
| 2871 | |
| 2872 | PyDoc_STRVAR(decode_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2873 | "B.decode([encoding[, errors]]) -> str\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2874 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2875 | Decode B using the codec registered for encoding. encoding defaults\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2876 | to the default encoding. errors may be given to set a different error\n\ |
| 2877 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 2878 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 2879 | as well as any other name registered with codecs.register_error that is\n\ |
| 2880 | able to handle UnicodeDecodeErrors."); |
| 2881 | |
| 2882 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2883 | bytearray_decode(PyObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2884 | { |
| 2885 | const char *encoding = NULL; |
| 2886 | const char *errors = NULL; |
| 2887 | |
| 2888 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 2889 | return NULL; |
| 2890 | if (encoding == NULL) |
| 2891 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2892 | return PyUnicode_FromEncodedObject(self, encoding, errors); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
| 2895 | PyDoc_STRVAR(alloc_doc, |
| 2896 | "B.__alloc__() -> int\n\ |
| 2897 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2898 | Return the number of bytes actually allocated."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2899 | |
| 2900 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2901 | bytearray_alloc(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2902 | { |
| 2903 | return PyLong_FromSsize_t(self->ob_alloc); |
| 2904 | } |
| 2905 | |
| 2906 | PyDoc_STRVAR(join_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2907 | "B.join(iterable_of_bytes) -> bytearray\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2908 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2909 | Concatenate any number of bytes/bytearray objects, with B\n\ |
| 2910 | in between each pair, and return the result as a new bytearray."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2911 | |
| 2912 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2913 | bytearray_join(PyByteArrayObject *self, PyObject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2914 | { |
| 2915 | PyObject *seq; |
| 2916 | Py_ssize_t mysize = Py_SIZE(self); |
| 2917 | Py_ssize_t i; |
| 2918 | Py_ssize_t n; |
| 2919 | PyObject **items; |
| 2920 | Py_ssize_t totalsize = 0; |
| 2921 | PyObject *result; |
| 2922 | char *dest; |
| 2923 | |
| 2924 | seq = PySequence_Fast(it, "can only join an iterable"); |
| 2925 | if (seq == NULL) |
| 2926 | return NULL; |
| 2927 | n = PySequence_Fast_GET_SIZE(seq); |
| 2928 | items = PySequence_Fast_ITEMS(seq); |
| 2929 | |
| 2930 | /* Compute the total size, and check that they are all bytes */ |
| 2931 | /* XXX Shouldn't we use _getbuffer() on these items instead? */ |
| 2932 | for (i = 0; i < n; i++) { |
| 2933 | PyObject *obj = items[i]; |
| 2934 | if (!PyByteArray_Check(obj) && !PyBytes_Check(obj)) { |
| 2935 | PyErr_Format(PyExc_TypeError, |
| 2936 | "can only join an iterable of bytes " |
| 2937 | "(item %ld has type '%.100s')", |
| 2938 | /* XXX %ld isn't right on Win64 */ |
| 2939 | (long)i, Py_TYPE(obj)->tp_name); |
| 2940 | goto error; |
| 2941 | } |
| 2942 | if (i > 0) |
| 2943 | totalsize += mysize; |
| 2944 | totalsize += Py_SIZE(obj); |
| 2945 | if (totalsize < 0) { |
| 2946 | PyErr_NoMemory(); |
| 2947 | goto error; |
| 2948 | } |
| 2949 | } |
| 2950 | |
| 2951 | /* Allocate the result, and copy the bytes */ |
| 2952 | result = PyByteArray_FromStringAndSize(NULL, totalsize); |
| 2953 | if (result == NULL) |
| 2954 | goto error; |
| 2955 | dest = PyByteArray_AS_STRING(result); |
| 2956 | for (i = 0; i < n; i++) { |
| 2957 | PyObject *obj = items[i]; |
| 2958 | Py_ssize_t size = Py_SIZE(obj); |
| 2959 | char *buf; |
| 2960 | if (PyByteArray_Check(obj)) |
| 2961 | buf = PyByteArray_AS_STRING(obj); |
| 2962 | else |
| 2963 | buf = PyBytes_AS_STRING(obj); |
| 2964 | if (i) { |
| 2965 | memcpy(dest, self->ob_bytes, mysize); |
| 2966 | dest += mysize; |
| 2967 | } |
| 2968 | memcpy(dest, buf, size); |
| 2969 | dest += size; |
| 2970 | } |
| 2971 | |
| 2972 | /* Done */ |
| 2973 | Py_DECREF(seq); |
| 2974 | return result; |
| 2975 | |
| 2976 | /* Error handling */ |
| 2977 | error: |
| 2978 | Py_DECREF(seq); |
| 2979 | return NULL; |
| 2980 | } |
| 2981 | |
| 2982 | PyDoc_STRVAR(fromhex_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2983 | "bytearray.fromhex(string) -> bytearray (static method)\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2984 | \n\ |
| 2985 | Create a bytearray object from a string of hexadecimal numbers.\n\ |
| 2986 | Spaces between two numbers are accepted.\n\ |
| 2987 | Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')."); |
| 2988 | |
| 2989 | static int |
| 2990 | hex_digit_to_int(Py_UNICODE c) |
| 2991 | { |
| 2992 | if (c >= 128) |
| 2993 | return -1; |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 2994 | if (Py_ISDIGIT(c)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2995 | return c - '0'; |
| 2996 | else { |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 2997 | if (Py_ISUPPER(c)) |
| 2998 | c = Py_TOLOWER(c); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2999 | if (c >= 'a' && c <= 'f') |
| 3000 | return c - 'a' + 10; |
| 3001 | } |
| 3002 | return -1; |
| 3003 | } |
| 3004 | |
| 3005 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3006 | bytearray_fromhex(PyObject *cls, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3007 | { |
| 3008 | PyObject *newbytes, *hexobj; |
| 3009 | char *buf; |
| 3010 | Py_UNICODE *hex; |
| 3011 | Py_ssize_t hexlen, byteslen, i, j; |
| 3012 | int top, bot; |
| 3013 | |
| 3014 | if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj)) |
| 3015 | return NULL; |
| 3016 | assert(PyUnicode_Check(hexobj)); |
| 3017 | hexlen = PyUnicode_GET_SIZE(hexobj); |
| 3018 | hex = PyUnicode_AS_UNICODE(hexobj); |
| 3019 | byteslen = hexlen/2; /* This overestimates if there are spaces */ |
| 3020 | newbytes = PyByteArray_FromStringAndSize(NULL, byteslen); |
| 3021 | if (!newbytes) |
| 3022 | return NULL; |
| 3023 | buf = PyByteArray_AS_STRING(newbytes); |
| 3024 | for (i = j = 0; i < hexlen; i += 2) { |
| 3025 | /* skip over spaces in the input */ |
| 3026 | while (hex[i] == ' ') |
| 3027 | i++; |
| 3028 | if (i >= hexlen) |
| 3029 | break; |
| 3030 | top = hex_digit_to_int(hex[i]); |
| 3031 | bot = hex_digit_to_int(hex[i+1]); |
| 3032 | if (top == -1 || bot == -1) { |
| 3033 | PyErr_Format(PyExc_ValueError, |
| 3034 | "non-hexadecimal number found in " |
| 3035 | "fromhex() arg at position %zd", i); |
| 3036 | goto error; |
| 3037 | } |
| 3038 | buf[j++] = (top << 4) + bot; |
| 3039 | } |
| 3040 | if (PyByteArray_Resize(newbytes, j) < 0) |
| 3041 | goto error; |
| 3042 | return newbytes; |
| 3043 | |
| 3044 | error: |
| 3045 | Py_DECREF(newbytes); |
| 3046 | return NULL; |
| 3047 | } |
| 3048 | |
| 3049 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 3050 | |
| 3051 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3052 | bytearray_reduce(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3053 | { |
| 3054 | PyObject *latin1, *dict; |
| 3055 | if (self->ob_bytes) |
| 3056 | latin1 = PyUnicode_DecodeLatin1(self->ob_bytes, |
| 3057 | Py_SIZE(self), NULL); |
| 3058 | else |
| 3059 | latin1 = PyUnicode_FromString(""); |
| 3060 | |
| 3061 | dict = PyObject_GetAttrString((PyObject *)self, "__dict__"); |
| 3062 | if (dict == NULL) { |
| 3063 | PyErr_Clear(); |
| 3064 | dict = Py_None; |
| 3065 | Py_INCREF(dict); |
| 3066 | } |
| 3067 | |
| 3068 | return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict); |
| 3069 | } |
| 3070 | |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 3071 | PyDoc_STRVAR(sizeof_doc, |
| 3072 | "B.__sizeof__() -> int\n\ |
| 3073 | \n\ |
| 3074 | Returns the size of B in memory, in bytes"); |
| 3075 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3076 | bytearray_sizeof(PyByteArrayObject *self) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 3077 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 3078 | Py_ssize_t res; |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 3079 | |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 3080 | res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char); |
| 3081 | return PyLong_FromSsize_t(res); |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 3082 | } |
| 3083 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3084 | static PySequenceMethods bytearray_as_sequence = { |
| 3085 | (lenfunc)bytearray_length, /* sq_length */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3086 | (binaryfunc)PyByteArray_Concat, /* sq_concat */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3087 | (ssizeargfunc)bytearray_repeat, /* sq_repeat */ |
| 3088 | (ssizeargfunc)bytearray_getitem, /* sq_item */ |
| 3089 | 0, /* sq_slice */ |
| 3090 | (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */ |
| 3091 | 0, /* sq_ass_slice */ |
| 3092 | (objobjproc)bytearray_contains, /* sq_contains */ |
| 3093 | (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */ |
| 3094 | (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3095 | }; |
| 3096 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3097 | static PyMappingMethods bytearray_as_mapping = { |
| 3098 | (lenfunc)bytearray_length, |
| 3099 | (binaryfunc)bytearray_subscript, |
| 3100 | (objobjargproc)bytearray_ass_subscript, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3101 | }; |
| 3102 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3103 | static PyBufferProcs bytearray_as_buffer = { |
| 3104 | (getbufferproc)bytearray_getbuffer, |
| 3105 | (releasebufferproc)bytearray_releasebuffer, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3106 | }; |
| 3107 | |
| 3108 | static PyMethodDef |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3109 | bytearray_methods[] = { |
| 3110 | {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc}, |
| 3111 | {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, reduce_doc}, |
| 3112 | {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, sizeof_doc}, |
| 3113 | {"append", (PyCFunction)bytearray_append, METH_O, append__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3114 | {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, |
| 3115 | _Py_capitalize__doc__}, |
| 3116 | {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3117 | {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__}, |
| 3118 | {"decode", (PyCFunction)bytearray_decode, METH_VARARGS, decode_doc}, |
| 3119 | {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3120 | {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS, |
| 3121 | expandtabs__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3122 | {"extend", (PyCFunction)bytearray_extend, METH_O, extend__doc__}, |
| 3123 | {"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__}, |
| 3124 | {"fromhex", (PyCFunction)bytearray_fromhex, METH_VARARGS|METH_CLASS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3125 | fromhex_doc}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3126 | {"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__}, |
| 3127 | {"insert", (PyCFunction)bytearray_insert, METH_VARARGS, insert__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3128 | {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, |
| 3129 | _Py_isalnum__doc__}, |
| 3130 | {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, |
| 3131 | _Py_isalpha__doc__}, |
| 3132 | {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, |
| 3133 | _Py_isdigit__doc__}, |
| 3134 | {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, |
| 3135 | _Py_islower__doc__}, |
| 3136 | {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, |
| 3137 | _Py_isspace__doc__}, |
| 3138 | {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, |
| 3139 | _Py_istitle__doc__}, |
| 3140 | {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, |
| 3141 | _Py_isupper__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3142 | {"join", (PyCFunction)bytearray_join, METH_O, join_doc}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3143 | {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__}, |
| 3144 | {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3145 | {"lstrip", (PyCFunction)bytearray_lstrip, METH_VARARGS, lstrip__doc__}, |
| 3146 | {"maketrans", (PyCFunction)bytearray_maketrans, METH_VARARGS|METH_STATIC, |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 3147 | _Py_maketrans__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3148 | {"partition", (PyCFunction)bytearray_partition, METH_O, partition__doc__}, |
| 3149 | {"pop", (PyCFunction)bytearray_pop, METH_VARARGS, pop__doc__}, |
| 3150 | {"remove", (PyCFunction)bytearray_remove, METH_O, remove__doc__}, |
| 3151 | {"replace", (PyCFunction)bytearray_replace, METH_VARARGS, replace__doc__}, |
| 3152 | {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, reverse__doc__}, |
| 3153 | {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__}, |
| 3154 | {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3155 | {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3156 | {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, rpartition__doc__}, |
| 3157 | {"rsplit", (PyCFunction)bytearray_rsplit, METH_VARARGS, rsplit__doc__}, |
| 3158 | {"rstrip", (PyCFunction)bytearray_rstrip, METH_VARARGS, rstrip__doc__}, |
| 3159 | {"split", (PyCFunction)bytearray_split, METH_VARARGS, split__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3160 | {"splitlines", (PyCFunction)stringlib_splitlines, METH_VARARGS, |
| 3161 | splitlines__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3162 | {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS , |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3163 | startswith__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3164 | {"strip", (PyCFunction)bytearray_strip, METH_VARARGS, strip__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3165 | {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, |
| 3166 | _Py_swapcase__doc__}, |
| 3167 | {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3168 | {"translate", (PyCFunction)bytearray_translate, METH_VARARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3169 | translate__doc__}, |
| 3170 | {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
| 3171 | {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__}, |
| 3172 | {NULL} |
| 3173 | }; |
| 3174 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3175 | PyDoc_STRVAR(bytearray_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 3176 | "bytearray(iterable_of_ints) -> bytearray\n\ |
| 3177 | bytearray(string, encoding[, errors]) -> bytearray\n\ |
| 3178 | bytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray\n\ |
| 3179 | bytearray(memory_view) -> bytearray\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3180 | \n\ |
| 3181 | Construct an mutable bytearray object from:\n\ |
| 3182 | - an iterable yielding integers in range(256)\n\ |
| 3183 | - a text string encoded using the specified encoding\n\ |
| 3184 | - a bytes or a bytearray object\n\ |
| 3185 | - any object implementing the buffer API.\n\ |
| 3186 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 3187 | bytearray(int) -> bytearray\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3188 | \n\ |
| 3189 | Construct a zero-initialized bytearray of the given length."); |
| 3190 | |
| 3191 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3192 | static PyObject *bytearray_iter(PyObject *seq); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3193 | |
| 3194 | PyTypeObject PyByteArray_Type = { |
| 3195 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3196 | "bytearray", |
| 3197 | sizeof(PyByteArrayObject), |
| 3198 | 0, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3199 | (destructor)bytearray_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3200 | 0, /* tp_print */ |
| 3201 | 0, /* tp_getattr */ |
| 3202 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 3203 | 0, /* tp_reserved */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3204 | (reprfunc)bytearray_repr, /* tp_repr */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3205 | 0, /* tp_as_number */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3206 | &bytearray_as_sequence, /* tp_as_sequence */ |
| 3207 | &bytearray_as_mapping, /* tp_as_mapping */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3208 | 0, /* tp_hash */ |
| 3209 | 0, /* tp_call */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3210 | bytearray_str, /* tp_str */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3211 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3212 | 0, /* tp_setattro */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3213 | &bytearray_as_buffer, /* tp_as_buffer */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3214 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3215 | bytearray_doc, /* tp_doc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3216 | 0, /* tp_traverse */ |
| 3217 | 0, /* tp_clear */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3218 | (richcmpfunc)bytearray_richcompare, /* tp_richcompare */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3219 | 0, /* tp_weaklistoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3220 | bytearray_iter, /* tp_iter */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3221 | 0, /* tp_iternext */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3222 | bytearray_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3223 | 0, /* tp_members */ |
| 3224 | 0, /* tp_getset */ |
| 3225 | 0, /* tp_base */ |
| 3226 | 0, /* tp_dict */ |
| 3227 | 0, /* tp_descr_get */ |
| 3228 | 0, /* tp_descr_set */ |
| 3229 | 0, /* tp_dictoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3230 | (initproc)bytearray_init, /* tp_init */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3231 | PyType_GenericAlloc, /* tp_alloc */ |
| 3232 | PyType_GenericNew, /* tp_new */ |
| 3233 | PyObject_Del, /* tp_free */ |
| 3234 | }; |
| 3235 | |
| 3236 | /*********************** Bytes Iterator ****************************/ |
| 3237 | |
| 3238 | typedef struct { |
| 3239 | PyObject_HEAD |
| 3240 | Py_ssize_t it_index; |
| 3241 | PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 3242 | } bytesiterobject; |
| 3243 | |
| 3244 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3245 | bytearrayiter_dealloc(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3246 | { |
| 3247 | _PyObject_GC_UNTRACK(it); |
| 3248 | Py_XDECREF(it->it_seq); |
| 3249 | PyObject_GC_Del(it); |
| 3250 | } |
| 3251 | |
| 3252 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3253 | bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3254 | { |
| 3255 | Py_VISIT(it->it_seq); |
| 3256 | return 0; |
| 3257 | } |
| 3258 | |
| 3259 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3260 | bytearrayiter_next(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3261 | { |
| 3262 | PyByteArrayObject *seq; |
| 3263 | PyObject *item; |
| 3264 | |
| 3265 | assert(it != NULL); |
| 3266 | seq = it->it_seq; |
| 3267 | if (seq == NULL) |
| 3268 | return NULL; |
| 3269 | assert(PyByteArray_Check(seq)); |
| 3270 | |
| 3271 | if (it->it_index < PyByteArray_GET_SIZE(seq)) { |
| 3272 | item = PyLong_FromLong( |
| 3273 | (unsigned char)seq->ob_bytes[it->it_index]); |
| 3274 | if (item != NULL) |
| 3275 | ++it->it_index; |
| 3276 | return item; |
| 3277 | } |
| 3278 | |
| 3279 | Py_DECREF(seq); |
| 3280 | it->it_seq = NULL; |
| 3281 | return NULL; |
| 3282 | } |
| 3283 | |
| 3284 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3285 | bytesarrayiter_length_hint(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3286 | { |
| 3287 | Py_ssize_t len = 0; |
| 3288 | if (it->it_seq) |
| 3289 | len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index; |
| 3290 | return PyLong_FromSsize_t(len); |
| 3291 | } |
| 3292 | |
| 3293 | PyDoc_STRVAR(length_hint_doc, |
| 3294 | "Private method returning an estimate of len(list(it))."); |
| 3295 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3296 | static PyMethodDef bytearrayiter_methods[] = { |
| 3297 | {"__length_hint__", (PyCFunction)bytesarrayiter_length_hint, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3298 | length_hint_doc}, |
| 3299 | {NULL, NULL} /* sentinel */ |
| 3300 | }; |
| 3301 | |
| 3302 | PyTypeObject PyByteArrayIter_Type = { |
| 3303 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3304 | "bytearray_iterator", /* tp_name */ |
| 3305 | sizeof(bytesiterobject), /* tp_basicsize */ |
| 3306 | 0, /* tp_itemsize */ |
| 3307 | /* methods */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3308 | (destructor)bytearrayiter_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3309 | 0, /* tp_print */ |
| 3310 | 0, /* tp_getattr */ |
| 3311 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 3312 | 0, /* tp_reserved */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3313 | 0, /* tp_repr */ |
| 3314 | 0, /* tp_as_number */ |
| 3315 | 0, /* tp_as_sequence */ |
| 3316 | 0, /* tp_as_mapping */ |
| 3317 | 0, /* tp_hash */ |
| 3318 | 0, /* tp_call */ |
| 3319 | 0, /* tp_str */ |
| 3320 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3321 | 0, /* tp_setattro */ |
| 3322 | 0, /* tp_as_buffer */ |
| 3323 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 3324 | 0, /* tp_doc */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3325 | (traverseproc)bytearrayiter_traverse, /* tp_traverse */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3326 | 0, /* tp_clear */ |
| 3327 | 0, /* tp_richcompare */ |
| 3328 | 0, /* tp_weaklistoffset */ |
| 3329 | PyObject_SelfIter, /* tp_iter */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3330 | (iternextfunc)bytearrayiter_next, /* tp_iternext */ |
| 3331 | bytearrayiter_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3332 | 0, |
| 3333 | }; |
| 3334 | |
| 3335 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3336 | bytearray_iter(PyObject *seq) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3337 | { |
| 3338 | bytesiterobject *it; |
| 3339 | |
| 3340 | if (!PyByteArray_Check(seq)) { |
| 3341 | PyErr_BadInternalCall(); |
| 3342 | return NULL; |
| 3343 | } |
| 3344 | it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type); |
| 3345 | if (it == NULL) |
| 3346 | return NULL; |
| 3347 | it->it_index = 0; |
| 3348 | Py_INCREF(seq); |
| 3349 | it->it_seq = (PyByteArrayObject *)seq; |
| 3350 | _PyObject_GC_TRACK(it); |
| 3351 | return (PyObject *)it; |
| 3352 | } |