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 | |
Antoine Pitrou | fc8d6f4 | 2010-01-17 12:38:54 +0000 | [diff] [blame] | 8 | char _PyByteArray_empty_string[] = ""; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 9 | |
| 10 | void |
| 11 | PyByteArray_Fini(void) |
| 12 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | int |
| 16 | PyByteArray_Init(void) |
| 17 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 18 | return 1; |
| 19 | } |
| 20 | |
| 21 | /* end nullbytes support */ |
| 22 | |
| 23 | /* Helpers */ |
| 24 | |
| 25 | static int |
| 26 | _getbytevalue(PyObject* arg, int *value) |
| 27 | { |
| 28 | long face_value; |
| 29 | |
| 30 | if (PyLong_Check(arg)) { |
| 31 | face_value = PyLong_AsLong(arg); |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 32 | } else { |
| 33 | PyObject *index = PyNumber_Index(arg); |
| 34 | if (index == NULL) { |
| 35 | PyErr_Format(PyExc_TypeError, "an integer is required"); |
Mark Dickinson | 10de93a | 2010-07-09 19:25:48 +0000 | [diff] [blame] | 36 | *value = -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 37 | return 0; |
| 38 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 39 | face_value = PyLong_AsLong(index); |
| 40 | Py_DECREF(index); |
| 41 | } |
| 42 | |
| 43 | if (face_value < 0 || face_value >= 256) { |
| 44 | /* this includes the OverflowError in case the long is too large */ |
| 45 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
Mark Dickinson | 10de93a | 2010-07-09 19:25:48 +0000 | [diff] [blame] | 46 | *value = -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | *value = face_value; |
| 51 | return 1; |
| 52 | } |
| 53 | |
| 54 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 55 | bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 56 | { |
| 57 | int ret; |
| 58 | void *ptr; |
| 59 | if (view == NULL) { |
| 60 | obj->ob_exports++; |
| 61 | return 0; |
| 62 | } |
Antoine Pitrou | fc8d6f4 | 2010-01-17 12:38:54 +0000 | [diff] [blame] | 63 | ptr = (void *) PyByteArray_AS_STRING(obj); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 64 | ret = PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 65 | if (ret >= 0) { |
| 66 | obj->ob_exports++; |
| 67 | } |
| 68 | return ret; |
| 69 | } |
| 70 | |
| 71 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 72 | bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 73 | { |
| 74 | obj->ob_exports--; |
| 75 | } |
| 76 | |
| 77 | static Py_ssize_t |
| 78 | _getbuffer(PyObject *obj, Py_buffer *view) |
| 79 | { |
| 80 | PyBufferProcs *buffer = Py_TYPE(obj)->tp_as_buffer; |
| 81 | |
| 82 | if (buffer == NULL || buffer->bf_getbuffer == NULL) |
| 83 | { |
| 84 | PyErr_Format(PyExc_TypeError, |
| 85 | "Type %.100s doesn't support the buffer API", |
| 86 | Py_TYPE(obj)->tp_name); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0) |
| 91 | return -1; |
| 92 | return view->len; |
| 93 | } |
| 94 | |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 95 | static int |
| 96 | _canresize(PyByteArrayObject *self) |
| 97 | { |
| 98 | if (self->ob_exports > 0) { |
| 99 | PyErr_SetString(PyExc_BufferError, |
| 100 | "Existing exports of data: object cannot be re-sized"); |
| 101 | return 0; |
| 102 | } |
| 103 | return 1; |
| 104 | } |
| 105 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 106 | /* Direct API functions */ |
| 107 | |
| 108 | PyObject * |
| 109 | PyByteArray_FromObject(PyObject *input) |
| 110 | { |
| 111 | return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type, |
| 112 | input, NULL); |
| 113 | } |
| 114 | |
| 115 | PyObject * |
| 116 | PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size) |
| 117 | { |
| 118 | PyByteArrayObject *new; |
| 119 | Py_ssize_t alloc; |
| 120 | |
| 121 | if (size < 0) { |
| 122 | PyErr_SetString(PyExc_SystemError, |
| 123 | "Negative size passed to PyByteArray_FromStringAndSize"); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 127 | /* Prevent buffer overflow when setting alloc to size+1. */ |
| 128 | if (size == PY_SSIZE_T_MAX) { |
| 129 | return PyErr_NoMemory(); |
| 130 | } |
| 131 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 132 | new = PyObject_New(PyByteArrayObject, &PyByteArray_Type); |
| 133 | if (new == NULL) |
| 134 | return NULL; |
| 135 | |
| 136 | if (size == 0) { |
| 137 | new->ob_bytes = NULL; |
| 138 | alloc = 0; |
| 139 | } |
| 140 | else { |
| 141 | alloc = size + 1; |
| 142 | new->ob_bytes = PyMem_Malloc(alloc); |
| 143 | if (new->ob_bytes == NULL) { |
| 144 | Py_DECREF(new); |
| 145 | return PyErr_NoMemory(); |
| 146 | } |
Antoine Pitrou | fc8d6f4 | 2010-01-17 12:38:54 +0000 | [diff] [blame] | 147 | if (bytes != NULL && size > 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 148 | memcpy(new->ob_bytes, bytes, size); |
| 149 | new->ob_bytes[size] = '\0'; /* Trailing null byte */ |
| 150 | } |
| 151 | Py_SIZE(new) = size; |
| 152 | new->ob_alloc = alloc; |
| 153 | new->ob_exports = 0; |
| 154 | |
| 155 | return (PyObject *)new; |
| 156 | } |
| 157 | |
| 158 | Py_ssize_t |
| 159 | PyByteArray_Size(PyObject *self) |
| 160 | { |
| 161 | assert(self != NULL); |
| 162 | assert(PyByteArray_Check(self)); |
| 163 | |
| 164 | return PyByteArray_GET_SIZE(self); |
| 165 | } |
| 166 | |
| 167 | char * |
| 168 | PyByteArray_AsString(PyObject *self) |
| 169 | { |
| 170 | assert(self != NULL); |
| 171 | assert(PyByteArray_Check(self)); |
| 172 | |
| 173 | return PyByteArray_AS_STRING(self); |
| 174 | } |
| 175 | |
| 176 | int |
| 177 | PyByteArray_Resize(PyObject *self, Py_ssize_t size) |
| 178 | { |
| 179 | void *sval; |
| 180 | Py_ssize_t alloc = ((PyByteArrayObject *)self)->ob_alloc; |
| 181 | |
| 182 | assert(self != NULL); |
| 183 | assert(PyByteArray_Check(self)); |
| 184 | assert(size >= 0); |
| 185 | |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 186 | if (size == Py_SIZE(self)) { |
| 187 | return 0; |
| 188 | } |
| 189 | if (!_canresize((PyByteArrayObject *)self)) { |
| 190 | return -1; |
| 191 | } |
| 192 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 193 | if (size < alloc / 2) { |
| 194 | /* Major downsize; resize down to exact size */ |
| 195 | alloc = size + 1; |
| 196 | } |
| 197 | else if (size < alloc) { |
| 198 | /* Within allocated size; quick exit */ |
| 199 | Py_SIZE(self) = size; |
| 200 | ((PyByteArrayObject *)self)->ob_bytes[size] = '\0'; /* Trailing null */ |
| 201 | return 0; |
| 202 | } |
| 203 | else if (size <= alloc * 1.125) { |
| 204 | /* Moderate upsize; overallocate similar to list_resize() */ |
| 205 | alloc = size + (size >> 3) + (size < 9 ? 3 : 6); |
| 206 | } |
| 207 | else { |
| 208 | /* Major upsize; resize up to exact size */ |
| 209 | alloc = size + 1; |
| 210 | } |
| 211 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 212 | sval = PyMem_Realloc(((PyByteArrayObject *)self)->ob_bytes, alloc); |
| 213 | if (sval == NULL) { |
| 214 | PyErr_NoMemory(); |
| 215 | return -1; |
| 216 | } |
| 217 | |
| 218 | ((PyByteArrayObject *)self)->ob_bytes = sval; |
| 219 | Py_SIZE(self) = size; |
| 220 | ((PyByteArrayObject *)self)->ob_alloc = alloc; |
| 221 | ((PyByteArrayObject *)self)->ob_bytes[size] = '\0'; /* Trailing null byte */ |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | PyObject * |
| 227 | PyByteArray_Concat(PyObject *a, PyObject *b) |
| 228 | { |
| 229 | Py_ssize_t size; |
| 230 | Py_buffer va, vb; |
| 231 | PyByteArrayObject *result = NULL; |
| 232 | |
| 233 | va.len = -1; |
| 234 | vb.len = -1; |
| 235 | if (_getbuffer(a, &va) < 0 || |
| 236 | _getbuffer(b, &vb) < 0) { |
| 237 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 238 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 239 | goto done; |
| 240 | } |
| 241 | |
| 242 | size = va.len + vb.len; |
| 243 | if (size < 0) { |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 244 | PyErr_NoMemory(); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 245 | goto done; |
| 246 | } |
| 247 | |
| 248 | result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, size); |
| 249 | if (result != NULL) { |
| 250 | memcpy(result->ob_bytes, va.buf, va.len); |
| 251 | memcpy(result->ob_bytes + va.len, vb.buf, vb.len); |
| 252 | } |
| 253 | |
| 254 | done: |
| 255 | if (va.len != -1) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 256 | PyBuffer_Release(&va); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 257 | if (vb.len != -1) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 258 | PyBuffer_Release(&vb); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 259 | return (PyObject *)result; |
| 260 | } |
| 261 | |
| 262 | /* Functions stuffed into the type object */ |
| 263 | |
| 264 | static Py_ssize_t |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 265 | bytearray_length(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 266 | { |
| 267 | return Py_SIZE(self); |
| 268 | } |
| 269 | |
| 270 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 271 | bytearray_iconcat(PyByteArrayObject *self, PyObject *other) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 272 | { |
| 273 | Py_ssize_t mysize; |
| 274 | Py_ssize_t size; |
| 275 | Py_buffer vo; |
| 276 | |
| 277 | if (_getbuffer(other, &vo) < 0) { |
| 278 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 279 | Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name); |
| 280 | return NULL; |
| 281 | } |
| 282 | |
| 283 | mysize = Py_SIZE(self); |
| 284 | size = mysize + vo.len; |
| 285 | if (size < 0) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 286 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 287 | return PyErr_NoMemory(); |
| 288 | } |
| 289 | if (size < self->ob_alloc) { |
| 290 | Py_SIZE(self) = size; |
| 291 | self->ob_bytes[Py_SIZE(self)] = '\0'; /* Trailing null byte */ |
| 292 | } |
| 293 | else if (PyByteArray_Resize((PyObject *)self, size) < 0) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 294 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 295 | return NULL; |
| 296 | } |
| 297 | memcpy(self->ob_bytes + mysize, vo.buf, vo.len); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 298 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 299 | Py_INCREF(self); |
| 300 | return (PyObject *)self; |
| 301 | } |
| 302 | |
| 303 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 304 | bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 305 | { |
| 306 | PyByteArrayObject *result; |
| 307 | Py_ssize_t mysize; |
| 308 | Py_ssize_t size; |
| 309 | |
| 310 | if (count < 0) |
| 311 | count = 0; |
| 312 | mysize = Py_SIZE(self); |
| 313 | size = mysize * count; |
| 314 | if (count != 0 && size / count != mysize) |
| 315 | return PyErr_NoMemory(); |
| 316 | result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size); |
| 317 | if (result != NULL && size != 0) { |
| 318 | if (mysize == 1) |
| 319 | memset(result->ob_bytes, self->ob_bytes[0], size); |
| 320 | else { |
| 321 | Py_ssize_t i; |
| 322 | for (i = 0; i < count; i++) |
| 323 | memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize); |
| 324 | } |
| 325 | } |
| 326 | return (PyObject *)result; |
| 327 | } |
| 328 | |
| 329 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 330 | bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 331 | { |
| 332 | Py_ssize_t mysize; |
| 333 | Py_ssize_t size; |
| 334 | |
| 335 | if (count < 0) |
| 336 | count = 0; |
| 337 | mysize = Py_SIZE(self); |
| 338 | size = mysize * count; |
| 339 | if (count != 0 && size / count != mysize) |
| 340 | return PyErr_NoMemory(); |
| 341 | if (size < self->ob_alloc) { |
| 342 | Py_SIZE(self) = size; |
| 343 | self->ob_bytes[Py_SIZE(self)] = '\0'; /* Trailing null byte */ |
| 344 | } |
| 345 | else if (PyByteArray_Resize((PyObject *)self, size) < 0) |
| 346 | return NULL; |
| 347 | |
| 348 | if (mysize == 1) |
| 349 | memset(self->ob_bytes, self->ob_bytes[0], size); |
| 350 | else { |
| 351 | Py_ssize_t i; |
| 352 | for (i = 1; i < count; i++) |
| 353 | memcpy(self->ob_bytes + i*mysize, self->ob_bytes, mysize); |
| 354 | } |
| 355 | |
| 356 | Py_INCREF(self); |
| 357 | return (PyObject *)self; |
| 358 | } |
| 359 | |
| 360 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 361 | bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 362 | { |
| 363 | if (i < 0) |
| 364 | i += Py_SIZE(self); |
| 365 | if (i < 0 || i >= Py_SIZE(self)) { |
| 366 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 367 | return NULL; |
| 368 | } |
| 369 | return PyLong_FromLong((unsigned char)(self->ob_bytes[i])); |
| 370 | } |
| 371 | |
| 372 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 373 | bytearray_subscript(PyByteArrayObject *self, PyObject *index) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 374 | { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 375 | if (PyIndex_Check(index)) { |
| 376 | Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 377 | |
| 378 | if (i == -1 && PyErr_Occurred()) |
| 379 | return NULL; |
| 380 | |
| 381 | if (i < 0) |
| 382 | i += PyByteArray_GET_SIZE(self); |
| 383 | |
| 384 | if (i < 0 || i >= Py_SIZE(self)) { |
| 385 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 386 | return NULL; |
| 387 | } |
| 388 | return PyLong_FromLong((unsigned char)(self->ob_bytes[i])); |
| 389 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 390 | else if (PySlice_Check(index)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 391 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 392 | if (PySlice_GetIndicesEx((PySliceObject *)index, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 393 | PyByteArray_GET_SIZE(self), |
| 394 | &start, &stop, &step, &slicelength) < 0) { |
| 395 | return NULL; |
| 396 | } |
| 397 | |
| 398 | if (slicelength <= 0) |
| 399 | return PyByteArray_FromStringAndSize("", 0); |
| 400 | else if (step == 1) { |
| 401 | return PyByteArray_FromStringAndSize(self->ob_bytes + start, |
| 402 | slicelength); |
| 403 | } |
| 404 | else { |
| 405 | char *source_buf = PyByteArray_AS_STRING(self); |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 406 | char *result_buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 407 | PyObject *result; |
| 408 | |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 409 | result = PyByteArray_FromStringAndSize(NULL, slicelength); |
| 410 | if (result == NULL) |
| 411 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 412 | |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 413 | result_buf = PyByteArray_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 414 | for (cur = start, i = 0; i < slicelength; |
| 415 | cur += step, i++) { |
| 416 | result_buf[i] = source_buf[cur]; |
| 417 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 418 | return result; |
| 419 | } |
| 420 | } |
| 421 | else { |
| 422 | PyErr_SetString(PyExc_TypeError, "bytearray indices must be integers"); |
| 423 | return NULL; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 428 | bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 429 | PyObject *values) |
| 430 | { |
| 431 | Py_ssize_t avail, needed; |
| 432 | void *bytes; |
| 433 | Py_buffer vbytes; |
| 434 | int res = 0; |
| 435 | |
| 436 | vbytes.len = -1; |
| 437 | if (values == (PyObject *)self) { |
| 438 | /* Make a copy and call this function recursively */ |
| 439 | int err; |
| 440 | values = PyByteArray_FromObject(values); |
| 441 | if (values == NULL) |
| 442 | return -1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 443 | err = bytearray_setslice(self, lo, hi, values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 444 | Py_DECREF(values); |
| 445 | return err; |
| 446 | } |
| 447 | if (values == NULL) { |
| 448 | /* del b[lo:hi] */ |
| 449 | bytes = NULL; |
| 450 | needed = 0; |
| 451 | } |
| 452 | else { |
| 453 | if (_getbuffer(values, &vbytes) < 0) { |
| 454 | PyErr_Format(PyExc_TypeError, |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 455 | "can't set bytearray slice from %.100s", |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 456 | Py_TYPE(values)->tp_name); |
| 457 | return -1; |
| 458 | } |
| 459 | needed = vbytes.len; |
| 460 | bytes = vbytes.buf; |
| 461 | } |
| 462 | |
| 463 | if (lo < 0) |
| 464 | lo = 0; |
| 465 | if (hi < lo) |
| 466 | hi = lo; |
| 467 | if (hi > Py_SIZE(self)) |
| 468 | hi = Py_SIZE(self); |
| 469 | |
| 470 | avail = hi - lo; |
| 471 | if (avail < 0) |
| 472 | lo = hi = avail = 0; |
| 473 | |
| 474 | if (avail != needed) { |
| 475 | if (avail > needed) { |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 476 | if (!_canresize(self)) { |
| 477 | res = -1; |
| 478 | goto finish; |
| 479 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 480 | /* |
| 481 | 0 lo hi old_size |
| 482 | | |<----avail----->|<-----tomove------>| |
| 483 | | |<-needed->|<-----tomove------>| |
| 484 | 0 lo new_hi new_size |
| 485 | */ |
| 486 | memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi, |
| 487 | Py_SIZE(self) - hi); |
| 488 | } |
| 489 | /* XXX(nnorwitz): need to verify this can't overflow! */ |
| 490 | if (PyByteArray_Resize((PyObject *)self, |
| 491 | Py_SIZE(self) + needed - avail) < 0) { |
| 492 | res = -1; |
| 493 | goto finish; |
| 494 | } |
| 495 | if (avail < needed) { |
| 496 | /* |
| 497 | 0 lo hi old_size |
| 498 | | |<-avail->|<-----tomove------>| |
| 499 | | |<----needed---->|<-----tomove------>| |
| 500 | 0 lo new_hi new_size |
| 501 | */ |
| 502 | memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi, |
| 503 | Py_SIZE(self) - lo - needed); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | if (needed > 0) |
| 508 | memcpy(self->ob_bytes + lo, bytes, needed); |
| 509 | |
| 510 | |
| 511 | finish: |
| 512 | if (vbytes.len != -1) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 513 | PyBuffer_Release(&vbytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 514 | return res; |
| 515 | } |
| 516 | |
| 517 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 518 | bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 519 | { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 520 | int ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 521 | |
| 522 | if (i < 0) |
| 523 | i += Py_SIZE(self); |
| 524 | |
| 525 | if (i < 0 || i >= Py_SIZE(self)) { |
| 526 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 527 | return -1; |
| 528 | } |
| 529 | |
| 530 | if (value == NULL) |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 531 | return bytearray_setslice(self, i, i+1, NULL); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 532 | |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 533 | if (!_getbytevalue(value, &ival)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 534 | return -1; |
| 535 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 536 | self->ob_bytes[i] = ival; |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 541 | bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 542 | { |
| 543 | Py_ssize_t start, stop, step, slicelen, needed; |
| 544 | char *bytes; |
| 545 | |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 546 | if (PyIndex_Check(index)) { |
| 547 | Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 548 | |
| 549 | if (i == -1 && PyErr_Occurred()) |
| 550 | return -1; |
| 551 | |
| 552 | if (i < 0) |
| 553 | i += PyByteArray_GET_SIZE(self); |
| 554 | |
| 555 | if (i < 0 || i >= Py_SIZE(self)) { |
| 556 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 557 | return -1; |
| 558 | } |
| 559 | |
| 560 | if (values == NULL) { |
| 561 | /* Fall through to slice assignment */ |
| 562 | start = i; |
| 563 | stop = i + 1; |
| 564 | step = 1; |
| 565 | slicelen = 1; |
| 566 | } |
| 567 | else { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 568 | int ival; |
| 569 | if (!_getbytevalue(values, &ival)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 570 | return -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 571 | self->ob_bytes[i] = (char)ival; |
| 572 | return 0; |
| 573 | } |
| 574 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 575 | else if (PySlice_Check(index)) { |
| 576 | if (PySlice_GetIndicesEx((PySliceObject *)index, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 577 | PyByteArray_GET_SIZE(self), |
| 578 | &start, &stop, &step, &slicelen) < 0) { |
| 579 | return -1; |
| 580 | } |
| 581 | } |
| 582 | else { |
| 583 | PyErr_SetString(PyExc_TypeError, "bytearray indices must be integer"); |
| 584 | return -1; |
| 585 | } |
| 586 | |
| 587 | if (values == NULL) { |
| 588 | bytes = NULL; |
| 589 | needed = 0; |
| 590 | } |
| 591 | else if (values == (PyObject *)self || !PyByteArray_Check(values)) { |
| 592 | /* Make a copy an call this function recursively */ |
| 593 | int err; |
| 594 | values = PyByteArray_FromObject(values); |
| 595 | if (values == NULL) |
| 596 | return -1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 597 | err = bytearray_ass_subscript(self, index, values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 598 | Py_DECREF(values); |
| 599 | return err; |
| 600 | } |
| 601 | else { |
| 602 | assert(PyByteArray_Check(values)); |
| 603 | bytes = ((PyByteArrayObject *)values)->ob_bytes; |
| 604 | needed = Py_SIZE(values); |
| 605 | } |
| 606 | /* Make sure b[5:2] = ... inserts before 5, not before 2. */ |
| 607 | if ((step < 0 && start < stop) || |
| 608 | (step > 0 && start > stop)) |
| 609 | stop = start; |
| 610 | if (step == 1) { |
| 611 | if (slicelen != needed) { |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 612 | if (!_canresize(self)) |
| 613 | return -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 614 | if (slicelen > needed) { |
| 615 | /* |
| 616 | 0 start stop old_size |
| 617 | | |<---slicelen--->|<-----tomove------>| |
| 618 | | |<-needed->|<-----tomove------>| |
| 619 | 0 lo new_hi new_size |
| 620 | */ |
| 621 | memmove(self->ob_bytes + start + needed, self->ob_bytes + stop, |
| 622 | Py_SIZE(self) - stop); |
| 623 | } |
| 624 | if (PyByteArray_Resize((PyObject *)self, |
| 625 | Py_SIZE(self) + needed - slicelen) < 0) |
| 626 | return -1; |
| 627 | if (slicelen < needed) { |
| 628 | /* |
| 629 | 0 lo hi old_size |
| 630 | | |<-avail->|<-----tomove------>| |
| 631 | | |<----needed---->|<-----tomove------>| |
| 632 | 0 lo new_hi new_size |
| 633 | */ |
| 634 | memmove(self->ob_bytes + start + needed, self->ob_bytes + stop, |
| 635 | Py_SIZE(self) - start - needed); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | if (needed > 0) |
| 640 | memcpy(self->ob_bytes + start, bytes, needed); |
| 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | else { |
| 645 | if (needed == 0) { |
| 646 | /* Delete slice */ |
Mark Dickinson | bc09964 | 2010-01-29 17:27:24 +0000 | [diff] [blame] | 647 | size_t cur; |
| 648 | Py_ssize_t i; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 649 | |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 650 | if (!_canresize(self)) |
| 651 | return -1; |
Mark Dickinson | 7e3b948 | 2010-08-06 21:33:18 +0000 | [diff] [blame] | 652 | |
| 653 | if (slicelen == 0) |
| 654 | /* Nothing to do here. */ |
| 655 | return 0; |
| 656 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 657 | if (step < 0) { |
| 658 | stop = start + 1; |
| 659 | start = stop + step * (slicelen - 1) - 1; |
| 660 | step = -step; |
| 661 | } |
| 662 | for (cur = start, i = 0; |
| 663 | i < slicelen; cur += step, i++) { |
| 664 | Py_ssize_t lim = step - 1; |
| 665 | |
Mark Dickinson | 66f575b | 2010-02-14 12:53:32 +0000 | [diff] [blame] | 666 | if (cur + step >= (size_t)PyByteArray_GET_SIZE(self)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 667 | lim = PyByteArray_GET_SIZE(self) - cur - 1; |
| 668 | |
| 669 | memmove(self->ob_bytes + cur - i, |
| 670 | self->ob_bytes + cur + 1, lim); |
| 671 | } |
| 672 | /* Move the tail of the bytes, in one chunk */ |
Mark Dickinson | 7e3b948 | 2010-08-06 21:33:18 +0000 | [diff] [blame] | 673 | cur = start + (size_t)slicelen*step; |
Mark Dickinson | 66f575b | 2010-02-14 12:53:32 +0000 | [diff] [blame] | 674 | if (cur < (size_t)PyByteArray_GET_SIZE(self)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 675 | memmove(self->ob_bytes + cur - slicelen, |
| 676 | self->ob_bytes + cur, |
| 677 | PyByteArray_GET_SIZE(self) - cur); |
| 678 | } |
| 679 | if (PyByteArray_Resize((PyObject *)self, |
| 680 | PyByteArray_GET_SIZE(self) - slicelen) < 0) |
| 681 | return -1; |
| 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | else { |
| 686 | /* Assign slice */ |
Mark Dickinson | 7e3b948 | 2010-08-06 21:33:18 +0000 | [diff] [blame] | 687 | Py_ssize_t i; |
| 688 | size_t cur; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 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 | 8380dd5 | 2010-04-16 22:51:37 +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 | 9c0e94f | 2010-04-16 23:00:53 +0000 | [diff] [blame] | 768 | PyErr_Clear(); |
Benjamin Peterson | 8380dd5 | 2010-04-16 22:51:37 +0000 | [diff] [blame] | 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 | 66f575b | 2010-02-14 12:53:32 +0000 | [diff] [blame] | 858 | size_t newsize; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 859 | PyObject *v; |
Mark Dickinson | 66f575b | 2010-02-14 12:53:32 +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 | 66f575b | 2010-02-14 12:53:32 +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 |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1041 | #define STRINGLIB_LEN PyByteArray_GET_SIZE |
| 1042 | #define STRINGLIB_STR PyByteArray_AS_STRING |
| 1043 | #define STRINGLIB_NEW PyByteArray_FromStringAndSize |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1044 | #define STRINGLIB_ISSPACE Py_ISSPACE |
| 1045 | #define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r')) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1046 | #define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact |
| 1047 | #define STRINGLIB_MUTABLE 1 |
| 1048 | |
| 1049 | #include "stringlib/fastsearch.h" |
| 1050 | #include "stringlib/count.h" |
| 1051 | #include "stringlib/find.h" |
| 1052 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1053 | #include "stringlib/split.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 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 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1061 | /* helper macro to fixup start/end slice values */ |
| 1062 | #define ADJUST_INDICES(start, end, len) \ |
| 1063 | if (end > len) \ |
| 1064 | end = len; \ |
| 1065 | else if (end < 0) { \ |
| 1066 | end += len; \ |
| 1067 | if (end < 0) \ |
| 1068 | end = 0; \ |
| 1069 | } \ |
| 1070 | if (start < 0) { \ |
| 1071 | start += len; \ |
| 1072 | if (start < 0) \ |
| 1073 | start = 0; \ |
| 1074 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1075 | |
| 1076 | Py_LOCAL_INLINE(Py_ssize_t) |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1077 | bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1078 | { |
| 1079 | PyObject *subobj; |
| 1080 | Py_buffer subbuf; |
| 1081 | Py_ssize_t start=0, end=PY_SSIZE_T_MAX; |
| 1082 | Py_ssize_t res; |
| 1083 | |
| 1084 | if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", &subobj, |
| 1085 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 1086 | return -2; |
| 1087 | if (_getbuffer(subobj, &subbuf) < 0) |
| 1088 | return -2; |
| 1089 | if (dir > 0) |
| 1090 | res = stringlib_find_slice( |
| 1091 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 1092 | subbuf.buf, subbuf.len, start, end); |
| 1093 | else |
| 1094 | res = stringlib_rfind_slice( |
| 1095 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 1096 | subbuf.buf, subbuf.len, start, end); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1097 | PyBuffer_Release(&subbuf); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1098 | return res; |
| 1099 | } |
| 1100 | |
| 1101 | PyDoc_STRVAR(find__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1102 | "B.find(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1103 | \n\ |
| 1104 | Return the lowest index in B where subsection sub is found,\n\ |
| 1105 | such that sub is contained within s[start,end]. Optional\n\ |
| 1106 | arguments start and end are interpreted as in slice notation.\n\ |
| 1107 | \n\ |
| 1108 | Return -1 on failure."); |
| 1109 | |
| 1110 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1111 | bytearray_find(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1112 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1113 | Py_ssize_t result = bytearray_find_internal(self, args, +1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1114 | if (result == -2) |
| 1115 | return NULL; |
| 1116 | return PyLong_FromSsize_t(result); |
| 1117 | } |
| 1118 | |
| 1119 | PyDoc_STRVAR(count__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1120 | "B.count(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1121 | \n\ |
| 1122 | Return the number of non-overlapping occurrences of subsection sub in\n\ |
| 1123 | bytes B[start:end]. Optional arguments start and end are interpreted\n\ |
| 1124 | as in slice notation."); |
| 1125 | |
| 1126 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1127 | bytearray_count(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1128 | { |
| 1129 | PyObject *sub_obj; |
| 1130 | const char *str = PyByteArray_AS_STRING(self); |
| 1131 | Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
| 1132 | Py_buffer vsub; |
| 1133 | PyObject *count_obj; |
| 1134 | |
| 1135 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj, |
| 1136 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 1137 | return NULL; |
| 1138 | |
| 1139 | if (_getbuffer(sub_obj, &vsub) < 0) |
| 1140 | return NULL; |
| 1141 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1142 | ADJUST_INDICES(start, end, PyByteArray_GET_SIZE(self)); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1143 | |
| 1144 | count_obj = PyLong_FromSsize_t( |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1145 | stringlib_count(str + start, end - start, vsub.buf, vsub.len, PY_SSIZE_T_MAX) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1146 | ); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1147 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1148 | return count_obj; |
| 1149 | } |
| 1150 | |
| 1151 | |
| 1152 | PyDoc_STRVAR(index__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1153 | "B.index(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1154 | \n\ |
| 1155 | Like B.find() but raise ValueError when the subsection is not found."); |
| 1156 | |
| 1157 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1158 | bytearray_index(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1159 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1160 | Py_ssize_t result = bytearray_find_internal(self, args, +1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1161 | if (result == -2) |
| 1162 | return NULL; |
| 1163 | if (result == -1) { |
| 1164 | PyErr_SetString(PyExc_ValueError, |
| 1165 | "subsection not found"); |
| 1166 | return NULL; |
| 1167 | } |
| 1168 | return PyLong_FromSsize_t(result); |
| 1169 | } |
| 1170 | |
| 1171 | |
| 1172 | PyDoc_STRVAR(rfind__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1173 | "B.rfind(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1174 | \n\ |
| 1175 | Return the highest index in B where subsection sub is found,\n\ |
| 1176 | such that sub is contained within s[start,end]. Optional\n\ |
| 1177 | arguments start and end are interpreted as in slice notation.\n\ |
| 1178 | \n\ |
| 1179 | Return -1 on failure."); |
| 1180 | |
| 1181 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1182 | bytearray_rfind(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1183 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1184 | Py_ssize_t result = bytearray_find_internal(self, args, -1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1185 | if (result == -2) |
| 1186 | return NULL; |
| 1187 | return PyLong_FromSsize_t(result); |
| 1188 | } |
| 1189 | |
| 1190 | |
| 1191 | PyDoc_STRVAR(rindex__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1192 | "B.rindex(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1193 | \n\ |
| 1194 | Like B.rfind() but raise ValueError when the subsection is not found."); |
| 1195 | |
| 1196 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1197 | bytearray_rindex(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1198 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1199 | Py_ssize_t result = bytearray_find_internal(self, args, -1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1200 | if (result == -2) |
| 1201 | return NULL; |
| 1202 | if (result == -1) { |
| 1203 | PyErr_SetString(PyExc_ValueError, |
| 1204 | "subsection not found"); |
| 1205 | return NULL; |
| 1206 | } |
| 1207 | return PyLong_FromSsize_t(result); |
| 1208 | } |
| 1209 | |
| 1210 | |
| 1211 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1212 | bytearray_contains(PyObject *self, PyObject *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1213 | { |
| 1214 | Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError); |
| 1215 | if (ival == -1 && PyErr_Occurred()) { |
| 1216 | Py_buffer varg; |
| 1217 | int pos; |
| 1218 | PyErr_Clear(); |
| 1219 | if (_getbuffer(arg, &varg) < 0) |
| 1220 | return -1; |
| 1221 | pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self), |
| 1222 | varg.buf, varg.len, 0); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1223 | PyBuffer_Release(&varg); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1224 | return pos >= 0; |
| 1225 | } |
| 1226 | if (ival < 0 || ival >= 256) { |
| 1227 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
| 1228 | return -1; |
| 1229 | } |
| 1230 | |
| 1231 | return memchr(PyByteArray_AS_STRING(self), ival, Py_SIZE(self)) != NULL; |
| 1232 | } |
| 1233 | |
| 1234 | |
| 1235 | /* Matches the end (direction >= 0) or start (direction < 0) of self |
| 1236 | * against substr, using the start and end arguments. Returns |
| 1237 | * -1 on error, 0 if not found and 1 if found. |
| 1238 | */ |
| 1239 | Py_LOCAL(int) |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1240 | _bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1241 | Py_ssize_t end, int direction) |
| 1242 | { |
| 1243 | Py_ssize_t len = PyByteArray_GET_SIZE(self); |
| 1244 | const char* str; |
| 1245 | Py_buffer vsubstr; |
| 1246 | int rv = 0; |
| 1247 | |
| 1248 | str = PyByteArray_AS_STRING(self); |
| 1249 | |
| 1250 | if (_getbuffer(substr, &vsubstr) < 0) |
| 1251 | return -1; |
| 1252 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1253 | ADJUST_INDICES(start, end, len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1254 | |
| 1255 | if (direction < 0) { |
| 1256 | /* startswith */ |
| 1257 | if (start+vsubstr.len > len) { |
| 1258 | goto done; |
| 1259 | } |
| 1260 | } else { |
| 1261 | /* endswith */ |
| 1262 | if (end-start < vsubstr.len || start > len) { |
| 1263 | goto done; |
| 1264 | } |
| 1265 | |
| 1266 | if (end-vsubstr.len > start) |
| 1267 | start = end - vsubstr.len; |
| 1268 | } |
| 1269 | if (end-start >= vsubstr.len) |
| 1270 | rv = ! memcmp(str+start, vsubstr.buf, vsubstr.len); |
| 1271 | |
| 1272 | done: |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1273 | PyBuffer_Release(&vsubstr); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1274 | return rv; |
| 1275 | } |
| 1276 | |
| 1277 | |
| 1278 | PyDoc_STRVAR(startswith__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1279 | "B.startswith(prefix[, start[, end]]) -> bool\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1280 | \n\ |
| 1281 | Return True if B starts with the specified prefix, False otherwise.\n\ |
| 1282 | With optional start, test B beginning at that position.\n\ |
| 1283 | With optional end, stop comparing B at that position.\n\ |
| 1284 | prefix can also be a tuple of strings to try."); |
| 1285 | |
| 1286 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1287 | bytearray_startswith(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1288 | { |
| 1289 | Py_ssize_t start = 0; |
| 1290 | Py_ssize_t end = PY_SSIZE_T_MAX; |
| 1291 | PyObject *subobj; |
| 1292 | int result; |
| 1293 | |
| 1294 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
| 1295 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 1296 | return NULL; |
| 1297 | if (PyTuple_Check(subobj)) { |
| 1298 | Py_ssize_t i; |
| 1299 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1300 | result = _bytearray_tailmatch(self, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1301 | PyTuple_GET_ITEM(subobj, i), |
| 1302 | start, end, -1); |
| 1303 | if (result == -1) |
| 1304 | return NULL; |
| 1305 | else if (result) { |
| 1306 | Py_RETURN_TRUE; |
| 1307 | } |
| 1308 | } |
| 1309 | Py_RETURN_FALSE; |
| 1310 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1311 | result = _bytearray_tailmatch(self, subobj, start, end, -1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1312 | if (result == -1) |
| 1313 | return NULL; |
| 1314 | else |
| 1315 | return PyBool_FromLong(result); |
| 1316 | } |
| 1317 | |
| 1318 | PyDoc_STRVAR(endswith__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1319 | "B.endswith(suffix[, start[, end]]) -> bool\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1320 | \n\ |
| 1321 | Return True if B ends with the specified suffix, False otherwise.\n\ |
| 1322 | With optional start, test B beginning at that position.\n\ |
| 1323 | With optional end, stop comparing B at that position.\n\ |
| 1324 | suffix can also be a tuple of strings to try."); |
| 1325 | |
| 1326 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1327 | bytearray_endswith(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1328 | { |
| 1329 | Py_ssize_t start = 0; |
| 1330 | Py_ssize_t end = PY_SSIZE_T_MAX; |
| 1331 | PyObject *subobj; |
| 1332 | int result; |
| 1333 | |
| 1334 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 1335 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 1336 | return NULL; |
| 1337 | if (PyTuple_Check(subobj)) { |
| 1338 | Py_ssize_t i; |
| 1339 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1340 | result = _bytearray_tailmatch(self, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1341 | PyTuple_GET_ITEM(subobj, i), |
| 1342 | start, end, +1); |
| 1343 | if (result == -1) |
| 1344 | return NULL; |
| 1345 | else if (result) { |
| 1346 | Py_RETURN_TRUE; |
| 1347 | } |
| 1348 | } |
| 1349 | Py_RETURN_FALSE; |
| 1350 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1351 | result = _bytearray_tailmatch(self, subobj, start, end, +1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1352 | if (result == -1) |
| 1353 | return NULL; |
| 1354 | else |
| 1355 | return PyBool_FromLong(result); |
| 1356 | } |
| 1357 | |
| 1358 | |
| 1359 | PyDoc_STRVAR(translate__doc__, |
| 1360 | "B.translate(table[, deletechars]) -> bytearray\n\ |
| 1361 | \n\ |
| 1362 | Return a copy of B, where all characters occurring in the\n\ |
| 1363 | optional argument deletechars are removed, and the remaining\n\ |
| 1364 | characters have been mapped through the given translation\n\ |
| 1365 | table, which must be a bytes object of length 256."); |
| 1366 | |
| 1367 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1368 | bytearray_translate(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1369 | { |
| 1370 | register char *input, *output; |
| 1371 | register const char *table; |
Benjamin Peterson | 0f3641c | 2008-11-19 22:05:52 +0000 | [diff] [blame] | 1372 | register Py_ssize_t i, c; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1373 | PyObject *input_obj = (PyObject*)self; |
| 1374 | const char *output_start; |
| 1375 | Py_ssize_t inlen; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1376 | PyObject *result = NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1377 | int trans_table[256]; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1378 | PyObject *tableobj = NULL, *delobj = NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1379 | Py_buffer vtable, vdel; |
| 1380 | |
| 1381 | if (!PyArg_UnpackTuple(args, "translate", 1, 2, |
| 1382 | &tableobj, &delobj)) |
| 1383 | return NULL; |
| 1384 | |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1385 | if (tableobj == Py_None) { |
| 1386 | table = NULL; |
| 1387 | tableobj = NULL; |
| 1388 | } else if (_getbuffer(tableobj, &vtable) < 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1389 | return NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1390 | } else { |
| 1391 | if (vtable.len != 256) { |
| 1392 | PyErr_SetString(PyExc_ValueError, |
| 1393 | "translation table must be 256 characters long"); |
Georg Brandl | 953152f | 2009-07-22 12:03:59 +0000 | [diff] [blame] | 1394 | PyBuffer_Release(&vtable); |
| 1395 | return NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1396 | } |
| 1397 | table = (const char*)vtable.buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | if (delobj != NULL) { |
| 1401 | if (_getbuffer(delobj, &vdel) < 0) { |
Georg Brandl | 953152f | 2009-07-22 12:03:59 +0000 | [diff] [blame] | 1402 | if (tableobj != NULL) |
| 1403 | PyBuffer_Release(&vtable); |
| 1404 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1405 | } |
| 1406 | } |
| 1407 | else { |
| 1408 | vdel.buf = NULL; |
| 1409 | vdel.len = 0; |
| 1410 | } |
| 1411 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1412 | inlen = PyByteArray_GET_SIZE(input_obj); |
| 1413 | result = PyByteArray_FromStringAndSize((char *)NULL, inlen); |
| 1414 | if (result == NULL) |
| 1415 | goto done; |
| 1416 | output_start = output = PyByteArray_AsString(result); |
| 1417 | input = PyByteArray_AS_STRING(input_obj); |
| 1418 | |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1419 | if (vdel.len == 0 && table != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1420 | /* If no deletions are required, use faster code */ |
| 1421 | for (i = inlen; --i >= 0; ) { |
| 1422 | c = Py_CHARMASK(*input++); |
Benjamin Peterson | 0f3641c | 2008-11-19 22:05:52 +0000 | [diff] [blame] | 1423 | *output++ = table[c]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1424 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1425 | goto done; |
| 1426 | } |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1427 | |
| 1428 | if (table == NULL) { |
| 1429 | for (i = 0; i < 256; i++) |
| 1430 | trans_table[i] = Py_CHARMASK(i); |
| 1431 | } else { |
| 1432 | for (i = 0; i < 256; i++) |
| 1433 | trans_table[i] = Py_CHARMASK(table[i]); |
| 1434 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1435 | |
| 1436 | for (i = 0; i < vdel.len; i++) |
| 1437 | trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1; |
| 1438 | |
| 1439 | for (i = inlen; --i >= 0; ) { |
| 1440 | c = Py_CHARMASK(*input++); |
| 1441 | if (trans_table[c] != -1) |
| 1442 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 1443 | continue; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1444 | } |
| 1445 | /* Fix the size of the resulting string */ |
| 1446 | if (inlen > 0) |
| 1447 | PyByteArray_Resize(result, output - output_start); |
| 1448 | |
| 1449 | done: |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1450 | if (tableobj != NULL) |
| 1451 | PyBuffer_Release(&vtable); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1452 | if (delobj != NULL) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1453 | PyBuffer_Release(&vdel); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1454 | return result; |
| 1455 | } |
| 1456 | |
| 1457 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1458 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1459 | bytearray_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1460 | { |
| 1461 | return _Py_bytes_maketrans(args); |
| 1462 | } |
| 1463 | |
| 1464 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1465 | /* find and count characters and substrings */ |
| 1466 | |
| 1467 | #define findchar(target, target_len, c) \ |
| 1468 | ((char *)memchr((const void *)(target), c, target_len)) |
| 1469 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1470 | |
Benjamin Peterson | 0f3641c | 2008-11-19 22:05:52 +0000 | [diff] [blame] | 1471 | /* Bytes ops must return a string, create a copy */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1472 | Py_LOCAL(PyByteArrayObject *) |
| 1473 | return_self(PyByteArrayObject *self) |
| 1474 | { |
Georg Brandl | 1e7217d | 2008-05-30 12:02:38 +0000 | [diff] [blame] | 1475 | /* always return a new bytearray */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1476 | return (PyByteArrayObject *)PyByteArray_FromStringAndSize( |
| 1477 | PyByteArray_AS_STRING(self), |
| 1478 | PyByteArray_GET_SIZE(self)); |
| 1479 | } |
| 1480 | |
| 1481 | Py_LOCAL_INLINE(Py_ssize_t) |
| 1482 | countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount) |
| 1483 | { |
| 1484 | Py_ssize_t count=0; |
| 1485 | const char *start=target; |
| 1486 | const char *end=target+target_len; |
| 1487 | |
| 1488 | while ( (start=findchar(start, end-start, c)) != NULL ) { |
| 1489 | count++; |
| 1490 | if (count >= maxcount) |
| 1491 | break; |
| 1492 | start += 1; |
| 1493 | } |
| 1494 | return count; |
| 1495 | } |
| 1496 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1497 | |
| 1498 | /* Algorithms for different cases of string replacement */ |
| 1499 | |
| 1500 | /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
| 1501 | Py_LOCAL(PyByteArrayObject *) |
| 1502 | replace_interleave(PyByteArrayObject *self, |
| 1503 | const char *to_s, Py_ssize_t to_len, |
| 1504 | Py_ssize_t maxcount) |
| 1505 | { |
| 1506 | char *self_s, *result_s; |
| 1507 | Py_ssize_t self_len, result_len; |
| 1508 | Py_ssize_t count, i, product; |
| 1509 | PyByteArrayObject *result; |
| 1510 | |
| 1511 | self_len = PyByteArray_GET_SIZE(self); |
| 1512 | |
| 1513 | /* 1 at the end plus 1 after every character */ |
| 1514 | count = self_len+1; |
| 1515 | if (maxcount < count) |
| 1516 | count = maxcount; |
| 1517 | |
| 1518 | /* Check for overflow */ |
| 1519 | /* result_len = count * to_len + self_len; */ |
| 1520 | product = count * to_len; |
| 1521 | if (product / to_len != count) { |
| 1522 | PyErr_SetString(PyExc_OverflowError, |
| 1523 | "replace string is too long"); |
| 1524 | return NULL; |
| 1525 | } |
| 1526 | result_len = product + self_len; |
| 1527 | if (result_len < 0) { |
| 1528 | PyErr_SetString(PyExc_OverflowError, |
| 1529 | "replace string is too long"); |
| 1530 | return NULL; |
| 1531 | } |
| 1532 | |
| 1533 | if (! (result = (PyByteArrayObject *) |
| 1534 | PyByteArray_FromStringAndSize(NULL, result_len)) ) |
| 1535 | return NULL; |
| 1536 | |
| 1537 | self_s = PyByteArray_AS_STRING(self); |
| 1538 | result_s = PyByteArray_AS_STRING(result); |
| 1539 | |
| 1540 | /* TODO: special case single character, which doesn't need memcpy */ |
| 1541 | |
| 1542 | /* Lay the first one down (guaranteed this will occur) */ |
| 1543 | Py_MEMCPY(result_s, to_s, to_len); |
| 1544 | result_s += to_len; |
| 1545 | count -= 1; |
| 1546 | |
| 1547 | for (i=0; i<count; i++) { |
| 1548 | *result_s++ = *self_s++; |
| 1549 | Py_MEMCPY(result_s, to_s, to_len); |
| 1550 | result_s += to_len; |
| 1551 | } |
| 1552 | |
| 1553 | /* Copy the rest of the original string */ |
| 1554 | Py_MEMCPY(result_s, self_s, self_len-i); |
| 1555 | |
| 1556 | return result; |
| 1557 | } |
| 1558 | |
| 1559 | /* Special case for deleting a single character */ |
| 1560 | /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
| 1561 | Py_LOCAL(PyByteArrayObject *) |
| 1562 | replace_delete_single_character(PyByteArrayObject *self, |
| 1563 | char from_c, Py_ssize_t maxcount) |
| 1564 | { |
| 1565 | char *self_s, *result_s; |
| 1566 | char *start, *next, *end; |
| 1567 | Py_ssize_t self_len, result_len; |
| 1568 | Py_ssize_t count; |
| 1569 | PyByteArrayObject *result; |
| 1570 | |
| 1571 | self_len = PyByteArray_GET_SIZE(self); |
| 1572 | self_s = PyByteArray_AS_STRING(self); |
| 1573 | |
| 1574 | count = countchar(self_s, self_len, from_c, maxcount); |
| 1575 | if (count == 0) { |
| 1576 | return return_self(self); |
| 1577 | } |
| 1578 | |
| 1579 | result_len = self_len - count; /* from_len == 1 */ |
| 1580 | assert(result_len>=0); |
| 1581 | |
| 1582 | if ( (result = (PyByteArrayObject *) |
| 1583 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL) |
| 1584 | return NULL; |
| 1585 | result_s = PyByteArray_AS_STRING(result); |
| 1586 | |
| 1587 | start = self_s; |
| 1588 | end = self_s + self_len; |
| 1589 | while (count-- > 0) { |
| 1590 | next = findchar(start, end-start, from_c); |
| 1591 | if (next == NULL) |
| 1592 | break; |
| 1593 | Py_MEMCPY(result_s, start, next-start); |
| 1594 | result_s += (next-start); |
| 1595 | start = next+1; |
| 1596 | } |
| 1597 | Py_MEMCPY(result_s, start, end-start); |
| 1598 | |
| 1599 | return result; |
| 1600 | } |
| 1601 | |
| 1602 | /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
| 1603 | |
| 1604 | Py_LOCAL(PyByteArrayObject *) |
| 1605 | replace_delete_substring(PyByteArrayObject *self, |
| 1606 | const char *from_s, Py_ssize_t from_len, |
| 1607 | Py_ssize_t maxcount) |
| 1608 | { |
| 1609 | char *self_s, *result_s; |
| 1610 | char *start, *next, *end; |
| 1611 | Py_ssize_t self_len, result_len; |
| 1612 | Py_ssize_t count, offset; |
| 1613 | PyByteArrayObject *result; |
| 1614 | |
| 1615 | self_len = PyByteArray_GET_SIZE(self); |
| 1616 | self_s = PyByteArray_AS_STRING(self); |
| 1617 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1618 | count = stringlib_count(self_s, self_len, |
| 1619 | from_s, from_len, |
| 1620 | maxcount); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1621 | |
| 1622 | if (count == 0) { |
| 1623 | /* no matches */ |
| 1624 | return return_self(self); |
| 1625 | } |
| 1626 | |
| 1627 | result_len = self_len - (count * from_len); |
| 1628 | assert (result_len>=0); |
| 1629 | |
| 1630 | if ( (result = (PyByteArrayObject *) |
| 1631 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL ) |
| 1632 | return NULL; |
| 1633 | |
| 1634 | result_s = PyByteArray_AS_STRING(result); |
| 1635 | |
| 1636 | start = self_s; |
| 1637 | end = self_s + self_len; |
| 1638 | while (count-- > 0) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1639 | offset = stringlib_find(start, end-start, |
| 1640 | from_s, from_len, |
| 1641 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1642 | if (offset == -1) |
| 1643 | break; |
| 1644 | next = start + offset; |
| 1645 | |
| 1646 | Py_MEMCPY(result_s, start, next-start); |
| 1647 | |
| 1648 | result_s += (next-start); |
| 1649 | start = next+from_len; |
| 1650 | } |
| 1651 | Py_MEMCPY(result_s, start, end-start); |
| 1652 | return result; |
| 1653 | } |
| 1654 | |
| 1655 | /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
| 1656 | Py_LOCAL(PyByteArrayObject *) |
| 1657 | replace_single_character_in_place(PyByteArrayObject *self, |
| 1658 | char from_c, char to_c, |
| 1659 | Py_ssize_t maxcount) |
| 1660 | { |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1661 | char *self_s, *result_s, *start, *end, *next; |
| 1662 | Py_ssize_t self_len; |
| 1663 | PyByteArrayObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1664 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1665 | /* The result string will be the same size */ |
| 1666 | self_s = PyByteArray_AS_STRING(self); |
| 1667 | self_len = PyByteArray_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1668 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1669 | next = findchar(self_s, self_len, from_c); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1670 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1671 | if (next == NULL) { |
| 1672 | /* No matches; return the original bytes */ |
| 1673 | return return_self(self); |
| 1674 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1675 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1676 | /* Need to make a new bytes */ |
| 1677 | result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len); |
| 1678 | if (result == NULL) |
| 1679 | return NULL; |
| 1680 | result_s = PyByteArray_AS_STRING(result); |
| 1681 | Py_MEMCPY(result_s, self_s, self_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1682 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1683 | /* change everything in-place, starting with this one */ |
| 1684 | start = result_s + (next-self_s); |
| 1685 | *start = to_c; |
| 1686 | start++; |
| 1687 | end = result_s + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1688 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1689 | while (--maxcount > 0) { |
| 1690 | next = findchar(start, end-start, from_c); |
| 1691 | if (next == NULL) |
| 1692 | break; |
| 1693 | *next = to_c; |
| 1694 | start = next+1; |
| 1695 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1696 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1697 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
| 1701 | Py_LOCAL(PyByteArrayObject *) |
| 1702 | replace_substring_in_place(PyByteArrayObject *self, |
| 1703 | const char *from_s, Py_ssize_t from_len, |
| 1704 | const char *to_s, Py_ssize_t to_len, |
| 1705 | Py_ssize_t maxcount) |
| 1706 | { |
| 1707 | char *result_s, *start, *end; |
| 1708 | char *self_s; |
| 1709 | Py_ssize_t self_len, offset; |
| 1710 | PyByteArrayObject *result; |
| 1711 | |
| 1712 | /* The result bytes will be the same size */ |
| 1713 | |
| 1714 | self_s = PyByteArray_AS_STRING(self); |
| 1715 | self_len = PyByteArray_GET_SIZE(self); |
| 1716 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1717 | offset = stringlib_find(self_s, self_len, |
| 1718 | from_s, from_len, |
| 1719 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1720 | if (offset == -1) { |
| 1721 | /* No matches; return the original bytes */ |
| 1722 | return return_self(self); |
| 1723 | } |
| 1724 | |
| 1725 | /* Need to make a new bytes */ |
| 1726 | result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len); |
| 1727 | if (result == NULL) |
| 1728 | return NULL; |
| 1729 | result_s = PyByteArray_AS_STRING(result); |
| 1730 | Py_MEMCPY(result_s, self_s, self_len); |
| 1731 | |
| 1732 | /* change everything in-place, starting with this one */ |
| 1733 | start = result_s + offset; |
| 1734 | Py_MEMCPY(start, to_s, from_len); |
| 1735 | start += from_len; |
| 1736 | end = result_s + self_len; |
| 1737 | |
| 1738 | while ( --maxcount > 0) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1739 | offset = stringlib_find(start, end-start, |
| 1740 | from_s, from_len, |
| 1741 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1742 | if (offset==-1) |
| 1743 | break; |
| 1744 | Py_MEMCPY(start+offset, to_s, from_len); |
| 1745 | start += offset+from_len; |
| 1746 | } |
| 1747 | |
| 1748 | return result; |
| 1749 | } |
| 1750 | |
| 1751 | /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
| 1752 | Py_LOCAL(PyByteArrayObject *) |
| 1753 | replace_single_character(PyByteArrayObject *self, |
| 1754 | char from_c, |
| 1755 | const char *to_s, Py_ssize_t to_len, |
| 1756 | Py_ssize_t maxcount) |
| 1757 | { |
| 1758 | char *self_s, *result_s; |
| 1759 | char *start, *next, *end; |
| 1760 | Py_ssize_t self_len, result_len; |
| 1761 | Py_ssize_t count, product; |
| 1762 | PyByteArrayObject *result; |
| 1763 | |
| 1764 | self_s = PyByteArray_AS_STRING(self); |
| 1765 | self_len = PyByteArray_GET_SIZE(self); |
| 1766 | |
| 1767 | count = countchar(self_s, self_len, from_c, maxcount); |
| 1768 | if (count == 0) { |
| 1769 | /* no matches, return unchanged */ |
| 1770 | return return_self(self); |
| 1771 | } |
| 1772 | |
| 1773 | /* use the difference between current and new, hence the "-1" */ |
| 1774 | /* result_len = self_len + count * (to_len-1) */ |
| 1775 | product = count * (to_len-1); |
| 1776 | if (product / (to_len-1) != count) { |
| 1777 | PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
| 1778 | return NULL; |
| 1779 | } |
| 1780 | result_len = self_len + product; |
| 1781 | if (result_len < 0) { |
| 1782 | PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
| 1783 | return NULL; |
| 1784 | } |
| 1785 | |
| 1786 | if ( (result = (PyByteArrayObject *) |
| 1787 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL) |
| 1788 | return NULL; |
| 1789 | result_s = PyByteArray_AS_STRING(result); |
| 1790 | |
| 1791 | start = self_s; |
| 1792 | end = self_s + self_len; |
| 1793 | while (count-- > 0) { |
| 1794 | next = findchar(start, end-start, from_c); |
| 1795 | if (next == NULL) |
| 1796 | break; |
| 1797 | |
| 1798 | if (next == start) { |
| 1799 | /* replace with the 'to' */ |
| 1800 | Py_MEMCPY(result_s, to_s, to_len); |
| 1801 | result_s += to_len; |
| 1802 | start += 1; |
| 1803 | } else { |
| 1804 | /* copy the unchanged old then the 'to' */ |
| 1805 | Py_MEMCPY(result_s, start, next-start); |
| 1806 | result_s += (next-start); |
| 1807 | Py_MEMCPY(result_s, to_s, to_len); |
| 1808 | result_s += to_len; |
| 1809 | start = next+1; |
| 1810 | } |
| 1811 | } |
| 1812 | /* Copy the remainder of the remaining bytes */ |
| 1813 | Py_MEMCPY(result_s, start, end-start); |
| 1814 | |
| 1815 | return result; |
| 1816 | } |
| 1817 | |
| 1818 | /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
| 1819 | Py_LOCAL(PyByteArrayObject *) |
| 1820 | replace_substring(PyByteArrayObject *self, |
| 1821 | const char *from_s, Py_ssize_t from_len, |
| 1822 | const char *to_s, Py_ssize_t to_len, |
| 1823 | Py_ssize_t maxcount) |
| 1824 | { |
| 1825 | char *self_s, *result_s; |
| 1826 | char *start, *next, *end; |
| 1827 | Py_ssize_t self_len, result_len; |
| 1828 | Py_ssize_t count, offset, product; |
| 1829 | PyByteArrayObject *result; |
| 1830 | |
| 1831 | self_s = PyByteArray_AS_STRING(self); |
| 1832 | self_len = PyByteArray_GET_SIZE(self); |
| 1833 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1834 | count = stringlib_count(self_s, self_len, |
| 1835 | from_s, from_len, |
| 1836 | maxcount); |
| 1837 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1838 | if (count == 0) { |
| 1839 | /* no matches, return unchanged */ |
| 1840 | return return_self(self); |
| 1841 | } |
| 1842 | |
| 1843 | /* Check for overflow */ |
| 1844 | /* result_len = self_len + count * (to_len-from_len) */ |
| 1845 | product = count * (to_len-from_len); |
| 1846 | if (product / (to_len-from_len) != count) { |
| 1847 | PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
| 1848 | return NULL; |
| 1849 | } |
| 1850 | result_len = self_len + product; |
| 1851 | if (result_len < 0) { |
| 1852 | PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
| 1853 | return NULL; |
| 1854 | } |
| 1855 | |
| 1856 | if ( (result = (PyByteArrayObject *) |
| 1857 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL) |
| 1858 | return NULL; |
| 1859 | result_s = PyByteArray_AS_STRING(result); |
| 1860 | |
| 1861 | start = self_s; |
| 1862 | end = self_s + self_len; |
| 1863 | while (count-- > 0) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1864 | offset = stringlib_find(start, end-start, |
| 1865 | from_s, from_len, |
| 1866 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1867 | if (offset == -1) |
| 1868 | break; |
| 1869 | next = start+offset; |
| 1870 | if (next == start) { |
| 1871 | /* replace with the 'to' */ |
| 1872 | Py_MEMCPY(result_s, to_s, to_len); |
| 1873 | result_s += to_len; |
| 1874 | start += from_len; |
| 1875 | } else { |
| 1876 | /* copy the unchanged old then the 'to' */ |
| 1877 | Py_MEMCPY(result_s, start, next-start); |
| 1878 | result_s += (next-start); |
| 1879 | Py_MEMCPY(result_s, to_s, to_len); |
| 1880 | result_s += to_len; |
| 1881 | start = next+from_len; |
| 1882 | } |
| 1883 | } |
| 1884 | /* Copy the remainder of the remaining bytes */ |
| 1885 | Py_MEMCPY(result_s, start, end-start); |
| 1886 | |
| 1887 | return result; |
| 1888 | } |
| 1889 | |
| 1890 | |
| 1891 | Py_LOCAL(PyByteArrayObject *) |
| 1892 | replace(PyByteArrayObject *self, |
| 1893 | const char *from_s, Py_ssize_t from_len, |
| 1894 | const char *to_s, Py_ssize_t to_len, |
| 1895 | Py_ssize_t maxcount) |
| 1896 | { |
| 1897 | if (maxcount < 0) { |
| 1898 | maxcount = PY_SSIZE_T_MAX; |
| 1899 | } else if (maxcount == 0 || PyByteArray_GET_SIZE(self) == 0) { |
| 1900 | /* nothing to do; return the original bytes */ |
| 1901 | return return_self(self); |
| 1902 | } |
| 1903 | |
| 1904 | if (maxcount == 0 || |
| 1905 | (from_len == 0 && to_len == 0)) { |
| 1906 | /* nothing to do; return the original bytes */ |
| 1907 | return return_self(self); |
| 1908 | } |
| 1909 | |
| 1910 | /* Handle zero-length special cases */ |
| 1911 | |
| 1912 | if (from_len == 0) { |
| 1913 | /* insert the 'to' bytes everywhere. */ |
| 1914 | /* >>> "Python".replace("", ".") */ |
| 1915 | /* '.P.y.t.h.o.n.' */ |
| 1916 | return replace_interleave(self, to_s, to_len, maxcount); |
| 1917 | } |
| 1918 | |
| 1919 | /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
| 1920 | /* point for an empty self bytes to generate a non-empty bytes */ |
| 1921 | /* Special case so the remaining code always gets a non-empty bytes */ |
| 1922 | if (PyByteArray_GET_SIZE(self) == 0) { |
| 1923 | return return_self(self); |
| 1924 | } |
| 1925 | |
| 1926 | if (to_len == 0) { |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1927 | /* delete all occurrences of 'from' bytes */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1928 | if (from_len == 1) { |
| 1929 | return replace_delete_single_character( |
| 1930 | self, from_s[0], maxcount); |
| 1931 | } else { |
| 1932 | return replace_delete_substring(self, from_s, from_len, maxcount); |
| 1933 | } |
| 1934 | } |
| 1935 | |
| 1936 | /* Handle special case where both bytes have the same length */ |
| 1937 | |
| 1938 | if (from_len == to_len) { |
| 1939 | if (from_len == 1) { |
| 1940 | return replace_single_character_in_place( |
| 1941 | self, |
| 1942 | from_s[0], |
| 1943 | to_s[0], |
| 1944 | maxcount); |
| 1945 | } else { |
| 1946 | return replace_substring_in_place( |
| 1947 | self, from_s, from_len, to_s, to_len, maxcount); |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | /* Otherwise use the more generic algorithms */ |
| 1952 | if (from_len == 1) { |
| 1953 | return replace_single_character(self, from_s[0], |
| 1954 | to_s, to_len, maxcount); |
| 1955 | } else { |
| 1956 | /* len('from')>=2, len('to')>=1 */ |
| 1957 | return replace_substring(self, from_s, from_len, to_s, to_len, maxcount); |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | |
| 1962 | PyDoc_STRVAR(replace__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1963 | "B.replace(old, new[, count]) -> bytearray\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1964 | \n\ |
| 1965 | Return a copy of B with all occurrences of subsection\n\ |
| 1966 | old replaced by new. If the optional argument count is\n\ |
| 1967 | given, only the first count occurrences are replaced."); |
| 1968 | |
| 1969 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1970 | bytearray_replace(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1971 | { |
| 1972 | Py_ssize_t count = -1; |
| 1973 | PyObject *from, *to, *res; |
| 1974 | Py_buffer vfrom, vto; |
| 1975 | |
| 1976 | if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count)) |
| 1977 | return NULL; |
| 1978 | |
| 1979 | if (_getbuffer(from, &vfrom) < 0) |
| 1980 | return NULL; |
| 1981 | if (_getbuffer(to, &vto) < 0) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1982 | PyBuffer_Release(&vfrom); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1983 | return NULL; |
| 1984 | } |
| 1985 | |
| 1986 | res = (PyObject *)replace((PyByteArrayObject *) self, |
| 1987 | vfrom.buf, vfrom.len, |
| 1988 | vto.buf, vto.len, count); |
| 1989 | |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1990 | PyBuffer_Release(&vfrom); |
| 1991 | PyBuffer_Release(&vto); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1992 | return res; |
| 1993 | } |
| 1994 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1995 | PyDoc_STRVAR(split__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1996 | "B.split([sep[, maxsplit]]) -> list of bytearrays\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1997 | \n\ |
| 1998 | Return a list of the sections in B, using sep as the delimiter.\n\ |
| 1999 | If sep is not given, B is split on ASCII whitespace characters\n\ |
| 2000 | (space, tab, return, newline, formfeed, vertical tab).\n\ |
| 2001 | If maxsplit is given, at most maxsplit splits are done."); |
| 2002 | |
| 2003 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2004 | bytearray_split(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2005 | { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2006 | Py_ssize_t len = PyByteArray_GET_SIZE(self), n; |
| 2007 | Py_ssize_t maxsplit = -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2008 | const char *s = PyByteArray_AS_STRING(self), *sub; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2009 | PyObject *list, *subobj = Py_None; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2010 | Py_buffer vsub; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2011 | |
| 2012 | if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit)) |
| 2013 | return NULL; |
| 2014 | if (maxsplit < 0) |
| 2015 | maxsplit = PY_SSIZE_T_MAX; |
| 2016 | |
| 2017 | if (subobj == Py_None) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2018 | return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2019 | |
| 2020 | if (_getbuffer(subobj, &vsub) < 0) |
| 2021 | return NULL; |
| 2022 | sub = vsub.buf; |
| 2023 | n = vsub.len; |
| 2024 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2025 | list = stringlib_split( |
| 2026 | (PyObject*) self, s, len, sub, n, maxsplit |
| 2027 | ); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2028 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2029 | return list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | PyDoc_STRVAR(partition__doc__, |
| 2033 | "B.partition(sep) -> (head, sep, tail)\n\ |
| 2034 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2035 | 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] | 2036 | the separator itself, and the part after it. If the separator is not\n\ |
| 2037 | found, returns B and two empty bytearray objects."); |
| 2038 | |
| 2039 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2040 | bytearray_partition(PyByteArrayObject *self, PyObject *sep_obj) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2041 | { |
| 2042 | PyObject *bytesep, *result; |
| 2043 | |
| 2044 | bytesep = PyByteArray_FromObject(sep_obj); |
| 2045 | if (! bytesep) |
| 2046 | return NULL; |
| 2047 | |
| 2048 | result = stringlib_partition( |
| 2049 | (PyObject*) self, |
| 2050 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 2051 | bytesep, |
| 2052 | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
| 2053 | ); |
| 2054 | |
| 2055 | Py_DECREF(bytesep); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2056 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 2060 | "B.rpartition(sep) -> (head, sep, tail)\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2061 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2062 | Search for the separator sep in B, starting at the end of B,\n\ |
| 2063 | and return the part before it, the separator itself, and the\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2064 | part after it. If the separator is not found, returns two empty\n\ |
| 2065 | bytearray objects and B."); |
| 2066 | |
| 2067 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2068 | bytearray_rpartition(PyByteArrayObject *self, PyObject *sep_obj) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2069 | { |
| 2070 | PyObject *bytesep, *result; |
| 2071 | |
| 2072 | bytesep = PyByteArray_FromObject(sep_obj); |
| 2073 | if (! bytesep) |
| 2074 | return NULL; |
| 2075 | |
| 2076 | result = stringlib_rpartition( |
| 2077 | (PyObject*) self, |
| 2078 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 2079 | bytesep, |
| 2080 | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
| 2081 | ); |
| 2082 | |
| 2083 | Py_DECREF(bytesep); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2084 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | PyDoc_STRVAR(rsplit__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2088 | "B.rsplit(sep[, maxsplit]) -> list of bytearrays\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2089 | \n\ |
| 2090 | Return a list of the sections in B, using sep as the delimiter,\n\ |
| 2091 | starting at the end of B and working to the front.\n\ |
| 2092 | If sep is not given, B is split on ASCII whitespace characters\n\ |
| 2093 | (space, tab, return, newline, formfeed, vertical tab).\n\ |
| 2094 | If maxsplit is given, at most maxsplit splits are done."); |
| 2095 | |
| 2096 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2097 | bytearray_rsplit(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2098 | { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2099 | Py_ssize_t len = PyByteArray_GET_SIZE(self), n; |
| 2100 | Py_ssize_t maxsplit = -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2101 | const char *s = PyByteArray_AS_STRING(self), *sub; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2102 | PyObject *list, *subobj = Py_None; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2103 | Py_buffer vsub; |
| 2104 | |
| 2105 | if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit)) |
| 2106 | return NULL; |
| 2107 | if (maxsplit < 0) |
| 2108 | maxsplit = PY_SSIZE_T_MAX; |
| 2109 | |
| 2110 | if (subobj == Py_None) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2111 | return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2112 | |
| 2113 | if (_getbuffer(subobj, &vsub) < 0) |
| 2114 | return NULL; |
| 2115 | sub = vsub.buf; |
| 2116 | n = vsub.len; |
| 2117 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2118 | list = stringlib_rsplit( |
| 2119 | (PyObject*) self, s, len, sub, n, maxsplit |
| 2120 | ); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2121 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2122 | return list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | PyDoc_STRVAR(reverse__doc__, |
| 2126 | "B.reverse() -> None\n\ |
| 2127 | \n\ |
| 2128 | Reverse the order of the values in B in place."); |
| 2129 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2130 | bytearray_reverse(PyByteArrayObject *self, PyObject *unused) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2131 | { |
| 2132 | char swap, *head, *tail; |
| 2133 | Py_ssize_t i, j, n = Py_SIZE(self); |
| 2134 | |
| 2135 | j = n / 2; |
| 2136 | head = self->ob_bytes; |
| 2137 | tail = head + n - 1; |
| 2138 | for (i = 0; i < j; i++) { |
| 2139 | swap = *head; |
| 2140 | *head++ = *tail; |
| 2141 | *tail-- = swap; |
| 2142 | } |
| 2143 | |
| 2144 | Py_RETURN_NONE; |
| 2145 | } |
| 2146 | |
| 2147 | PyDoc_STRVAR(insert__doc__, |
| 2148 | "B.insert(index, int) -> None\n\ |
| 2149 | \n\ |
| 2150 | Insert a single item into the bytearray before the given index."); |
| 2151 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2152 | bytearray_insert(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2153 | { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 2154 | PyObject *value; |
| 2155 | int ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2156 | Py_ssize_t where, n = Py_SIZE(self); |
| 2157 | |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 2158 | if (!PyArg_ParseTuple(args, "nO:insert", &where, &value)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2159 | return NULL; |
| 2160 | |
| 2161 | if (n == PY_SSIZE_T_MAX) { |
| 2162 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 2163 | "cannot add more objects to bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2164 | return NULL; |
| 2165 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 2166 | if (!_getbytevalue(value, &ival)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2167 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2168 | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
| 2169 | return NULL; |
| 2170 | |
| 2171 | if (where < 0) { |
| 2172 | where += n; |
| 2173 | if (where < 0) |
| 2174 | where = 0; |
| 2175 | } |
| 2176 | if (where > n) |
| 2177 | where = n; |
| 2178 | memmove(self->ob_bytes + where + 1, self->ob_bytes + where, n - where); |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 2179 | self->ob_bytes[where] = ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2180 | |
| 2181 | Py_RETURN_NONE; |
| 2182 | } |
| 2183 | |
| 2184 | PyDoc_STRVAR(append__doc__, |
| 2185 | "B.append(int) -> None\n\ |
| 2186 | \n\ |
| 2187 | Append a single item to the end of B."); |
| 2188 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2189 | bytearray_append(PyByteArrayObject *self, PyObject *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2190 | { |
| 2191 | int value; |
| 2192 | Py_ssize_t n = Py_SIZE(self); |
| 2193 | |
| 2194 | if (! _getbytevalue(arg, &value)) |
| 2195 | return NULL; |
| 2196 | if (n == PY_SSIZE_T_MAX) { |
| 2197 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 2198 | "cannot add more objects to bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2199 | return NULL; |
| 2200 | } |
| 2201 | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
| 2202 | return NULL; |
| 2203 | |
| 2204 | self->ob_bytes[n] = value; |
| 2205 | |
| 2206 | Py_RETURN_NONE; |
| 2207 | } |
| 2208 | |
| 2209 | PyDoc_STRVAR(extend__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2210 | "B.extend(iterable_of_ints) -> None\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2211 | \n\ |
| 2212 | Append all the elements from the iterator or sequence to the\n\ |
| 2213 | end of B."); |
| 2214 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2215 | bytearray_extend(PyByteArrayObject *self, PyObject *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2216 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2217 | PyObject *it, *item, *bytearray_obj; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2218 | Py_ssize_t buf_size = 0, len = 0; |
| 2219 | int value; |
| 2220 | char *buf; |
| 2221 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2222 | /* bytearray_setslice code only accepts something supporting PEP 3118. */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2223 | if (PyObject_CheckBuffer(arg)) { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2224 | if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), arg) == -1) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2225 | return NULL; |
| 2226 | |
| 2227 | Py_RETURN_NONE; |
| 2228 | } |
| 2229 | |
| 2230 | it = PyObject_GetIter(arg); |
| 2231 | if (it == NULL) |
| 2232 | return NULL; |
| 2233 | |
| 2234 | /* Try to determine the length of the argument. 32 is abitrary. */ |
| 2235 | buf_size = _PyObject_LengthHint(arg, 32); |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2236 | if (buf_size == -1) { |
| 2237 | Py_DECREF(it); |
| 2238 | return NULL; |
| 2239 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2240 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2241 | bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size); |
| 2242 | if (bytearray_obj == NULL) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2243 | return NULL; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2244 | buf = PyByteArray_AS_STRING(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2245 | |
| 2246 | while ((item = PyIter_Next(it)) != NULL) { |
| 2247 | if (! _getbytevalue(item, &value)) { |
| 2248 | Py_DECREF(item); |
| 2249 | Py_DECREF(it); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2250 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2251 | return NULL; |
| 2252 | } |
| 2253 | buf[len++] = value; |
| 2254 | Py_DECREF(item); |
| 2255 | |
| 2256 | if (len >= buf_size) { |
| 2257 | buf_size = len + (len >> 1) + 1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2258 | if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2259 | Py_DECREF(it); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2260 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2261 | return NULL; |
| 2262 | } |
| 2263 | /* Recompute the `buf' pointer, since the resizing operation may |
| 2264 | have invalidated it. */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2265 | buf = PyByteArray_AS_STRING(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2266 | } |
| 2267 | } |
| 2268 | Py_DECREF(it); |
| 2269 | |
| 2270 | /* Resize down to exact size. */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2271 | if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) { |
| 2272 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2273 | return NULL; |
| 2274 | } |
| 2275 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2276 | 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] | 2277 | return NULL; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2278 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2279 | |
| 2280 | Py_RETURN_NONE; |
| 2281 | } |
| 2282 | |
| 2283 | PyDoc_STRVAR(pop__doc__, |
| 2284 | "B.pop([index]) -> int\n\ |
| 2285 | \n\ |
| 2286 | Remove and return a single item from B. If no index\n\ |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 2287 | argument is given, will pop the last value."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2288 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2289 | bytearray_pop(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2290 | { |
| 2291 | int value; |
| 2292 | Py_ssize_t where = -1, n = Py_SIZE(self); |
| 2293 | |
| 2294 | if (!PyArg_ParseTuple(args, "|n:pop", &where)) |
| 2295 | return NULL; |
| 2296 | |
| 2297 | if (n == 0) { |
| 2298 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 2299 | "cannot pop an empty bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2300 | return NULL; |
| 2301 | } |
| 2302 | if (where < 0) |
| 2303 | where += Py_SIZE(self); |
| 2304 | if (where < 0 || where >= Py_SIZE(self)) { |
| 2305 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 2306 | return NULL; |
| 2307 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 2308 | if (!_canresize(self)) |
| 2309 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2310 | |
| 2311 | value = self->ob_bytes[where]; |
| 2312 | memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where); |
| 2313 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 2314 | return NULL; |
| 2315 | |
Mark Dickinson | 54a3db9 | 2009-09-06 10:19:23 +0000 | [diff] [blame] | 2316 | return PyLong_FromLong((unsigned char)value); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | PyDoc_STRVAR(remove__doc__, |
| 2320 | "B.remove(int) -> None\n\ |
| 2321 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2322 | Remove the first occurrence of a value in B."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2323 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2324 | bytearray_remove(PyByteArrayObject *self, PyObject *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2325 | { |
| 2326 | int value; |
| 2327 | Py_ssize_t where, n = Py_SIZE(self); |
| 2328 | |
| 2329 | if (! _getbytevalue(arg, &value)) |
| 2330 | return NULL; |
| 2331 | |
| 2332 | for (where = 0; where < n; where++) { |
| 2333 | if (self->ob_bytes[where] == value) |
| 2334 | break; |
| 2335 | } |
| 2336 | if (where == n) { |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 2337 | PyErr_SetString(PyExc_ValueError, "value not found in bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2338 | return NULL; |
| 2339 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 2340 | if (!_canresize(self)) |
| 2341 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2342 | |
| 2343 | memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where); |
| 2344 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 2345 | return NULL; |
| 2346 | |
| 2347 | Py_RETURN_NONE; |
| 2348 | } |
| 2349 | |
| 2350 | /* XXX These two helpers could be optimized if argsize == 1 */ |
| 2351 | |
| 2352 | static Py_ssize_t |
| 2353 | lstrip_helper(unsigned char *myptr, Py_ssize_t mysize, |
| 2354 | void *argptr, Py_ssize_t argsize) |
| 2355 | { |
| 2356 | Py_ssize_t i = 0; |
| 2357 | while (i < mysize && memchr(argptr, myptr[i], argsize)) |
| 2358 | i++; |
| 2359 | return i; |
| 2360 | } |
| 2361 | |
| 2362 | static Py_ssize_t |
| 2363 | rstrip_helper(unsigned char *myptr, Py_ssize_t mysize, |
| 2364 | void *argptr, Py_ssize_t argsize) |
| 2365 | { |
| 2366 | Py_ssize_t i = mysize - 1; |
| 2367 | while (i >= 0 && memchr(argptr, myptr[i], argsize)) |
| 2368 | i--; |
| 2369 | return i + 1; |
| 2370 | } |
| 2371 | |
| 2372 | PyDoc_STRVAR(strip__doc__, |
| 2373 | "B.strip([bytes]) -> bytearray\n\ |
| 2374 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2375 | Strip leading and trailing bytes contained in the argument\n\ |
| 2376 | and return the result as a new bytearray.\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2377 | If the argument is omitted, strip ASCII whitespace."); |
| 2378 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2379 | bytearray_strip(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2380 | { |
| 2381 | Py_ssize_t left, right, mysize, argsize; |
| 2382 | void *myptr, *argptr; |
| 2383 | PyObject *arg = Py_None; |
| 2384 | Py_buffer varg; |
| 2385 | if (!PyArg_ParseTuple(args, "|O:strip", &arg)) |
| 2386 | return NULL; |
| 2387 | if (arg == Py_None) { |
| 2388 | argptr = "\t\n\r\f\v "; |
| 2389 | argsize = 6; |
| 2390 | } |
| 2391 | else { |
| 2392 | if (_getbuffer(arg, &varg) < 0) |
| 2393 | return NULL; |
| 2394 | argptr = varg.buf; |
| 2395 | argsize = varg.len; |
| 2396 | } |
| 2397 | myptr = self->ob_bytes; |
| 2398 | mysize = Py_SIZE(self); |
| 2399 | left = lstrip_helper(myptr, mysize, argptr, argsize); |
| 2400 | if (left == mysize) |
| 2401 | right = left; |
| 2402 | else |
| 2403 | right = rstrip_helper(myptr, mysize, argptr, argsize); |
| 2404 | if (arg != Py_None) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2405 | PyBuffer_Release(&varg); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2406 | return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left); |
| 2407 | } |
| 2408 | |
| 2409 | PyDoc_STRVAR(lstrip__doc__, |
| 2410 | "B.lstrip([bytes]) -> bytearray\n\ |
| 2411 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2412 | Strip leading bytes contained in the argument\n\ |
| 2413 | and return the result as a new bytearray.\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2414 | If the argument is omitted, strip leading ASCII whitespace."); |
| 2415 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2416 | bytearray_lstrip(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2417 | { |
| 2418 | Py_ssize_t left, right, mysize, argsize; |
| 2419 | void *myptr, *argptr; |
| 2420 | PyObject *arg = Py_None; |
| 2421 | Py_buffer varg; |
| 2422 | if (!PyArg_ParseTuple(args, "|O:lstrip", &arg)) |
| 2423 | return NULL; |
| 2424 | if (arg == Py_None) { |
| 2425 | argptr = "\t\n\r\f\v "; |
| 2426 | argsize = 6; |
| 2427 | } |
| 2428 | else { |
| 2429 | if (_getbuffer(arg, &varg) < 0) |
| 2430 | return NULL; |
| 2431 | argptr = varg.buf; |
| 2432 | argsize = varg.len; |
| 2433 | } |
| 2434 | myptr = self->ob_bytes; |
| 2435 | mysize = Py_SIZE(self); |
| 2436 | left = lstrip_helper(myptr, mysize, argptr, argsize); |
| 2437 | right = mysize; |
| 2438 | if (arg != Py_None) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2439 | PyBuffer_Release(&varg); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2440 | return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left); |
| 2441 | } |
| 2442 | |
| 2443 | PyDoc_STRVAR(rstrip__doc__, |
| 2444 | "B.rstrip([bytes]) -> bytearray\n\ |
| 2445 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2446 | Strip trailing bytes contained in the argument\n\ |
| 2447 | and return the result as a new bytearray.\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2448 | If the argument is omitted, strip trailing ASCII whitespace."); |
| 2449 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2450 | bytearray_rstrip(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2451 | { |
| 2452 | Py_ssize_t left, right, mysize, argsize; |
| 2453 | void *myptr, *argptr; |
| 2454 | PyObject *arg = Py_None; |
| 2455 | Py_buffer varg; |
| 2456 | if (!PyArg_ParseTuple(args, "|O:rstrip", &arg)) |
| 2457 | return NULL; |
| 2458 | if (arg == Py_None) { |
| 2459 | argptr = "\t\n\r\f\v "; |
| 2460 | argsize = 6; |
| 2461 | } |
| 2462 | else { |
| 2463 | if (_getbuffer(arg, &varg) < 0) |
| 2464 | return NULL; |
| 2465 | argptr = varg.buf; |
| 2466 | argsize = varg.len; |
| 2467 | } |
| 2468 | myptr = self->ob_bytes; |
| 2469 | mysize = Py_SIZE(self); |
| 2470 | left = 0; |
| 2471 | right = rstrip_helper(myptr, mysize, argptr, argsize); |
| 2472 | if (arg != Py_None) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2473 | PyBuffer_Release(&varg); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2474 | return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left); |
| 2475 | } |
| 2476 | |
| 2477 | PyDoc_STRVAR(decode_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2478 | "B.decode([encoding[, errors]]) -> str\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2479 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2480 | Decode B using the codec registered for encoding. encoding defaults\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2481 | to the default encoding. errors may be given to set a different error\n\ |
| 2482 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 2483 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 2484 | as well as any other name registered with codecs.register_error that is\n\ |
| 2485 | able to handle UnicodeDecodeErrors."); |
| 2486 | |
| 2487 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 2488 | bytearray_decode(PyObject *self, PyObject *args, PyObject *kwargs) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2489 | { |
| 2490 | const char *encoding = NULL; |
| 2491 | const char *errors = NULL; |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 2492 | static char *kwlist[] = {"encoding", "errors", 0}; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2493 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 2494 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", kwlist, &encoding, &errors)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2495 | return NULL; |
| 2496 | if (encoding == NULL) |
| 2497 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2498 | return PyUnicode_FromEncodedObject(self, encoding, errors); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | PyDoc_STRVAR(alloc_doc, |
| 2502 | "B.__alloc__() -> int\n\ |
| 2503 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2504 | Return the number of bytes actually allocated."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2505 | |
| 2506 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2507 | bytearray_alloc(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2508 | { |
| 2509 | return PyLong_FromSsize_t(self->ob_alloc); |
| 2510 | } |
| 2511 | |
| 2512 | PyDoc_STRVAR(join_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2513 | "B.join(iterable_of_bytes) -> bytearray\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2514 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2515 | Concatenate any number of bytes/bytearray objects, with B\n\ |
| 2516 | in between each pair, and return the result as a new bytearray."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2517 | |
| 2518 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2519 | bytearray_join(PyByteArrayObject *self, PyObject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2520 | { |
| 2521 | PyObject *seq; |
| 2522 | Py_ssize_t mysize = Py_SIZE(self); |
| 2523 | Py_ssize_t i; |
| 2524 | Py_ssize_t n; |
| 2525 | PyObject **items; |
| 2526 | Py_ssize_t totalsize = 0; |
| 2527 | PyObject *result; |
| 2528 | char *dest; |
| 2529 | |
| 2530 | seq = PySequence_Fast(it, "can only join an iterable"); |
| 2531 | if (seq == NULL) |
| 2532 | return NULL; |
| 2533 | n = PySequence_Fast_GET_SIZE(seq); |
| 2534 | items = PySequence_Fast_ITEMS(seq); |
| 2535 | |
| 2536 | /* Compute the total size, and check that they are all bytes */ |
| 2537 | /* XXX Shouldn't we use _getbuffer() on these items instead? */ |
| 2538 | for (i = 0; i < n; i++) { |
| 2539 | PyObject *obj = items[i]; |
| 2540 | if (!PyByteArray_Check(obj) && !PyBytes_Check(obj)) { |
| 2541 | PyErr_Format(PyExc_TypeError, |
| 2542 | "can only join an iterable of bytes " |
| 2543 | "(item %ld has type '%.100s')", |
| 2544 | /* XXX %ld isn't right on Win64 */ |
| 2545 | (long)i, Py_TYPE(obj)->tp_name); |
| 2546 | goto error; |
| 2547 | } |
| 2548 | if (i > 0) |
| 2549 | totalsize += mysize; |
| 2550 | totalsize += Py_SIZE(obj); |
| 2551 | if (totalsize < 0) { |
| 2552 | PyErr_NoMemory(); |
| 2553 | goto error; |
| 2554 | } |
| 2555 | } |
| 2556 | |
| 2557 | /* Allocate the result, and copy the bytes */ |
| 2558 | result = PyByteArray_FromStringAndSize(NULL, totalsize); |
| 2559 | if (result == NULL) |
| 2560 | goto error; |
| 2561 | dest = PyByteArray_AS_STRING(result); |
| 2562 | for (i = 0; i < n; i++) { |
| 2563 | PyObject *obj = items[i]; |
| 2564 | Py_ssize_t size = Py_SIZE(obj); |
| 2565 | char *buf; |
| 2566 | if (PyByteArray_Check(obj)) |
| 2567 | buf = PyByteArray_AS_STRING(obj); |
| 2568 | else |
| 2569 | buf = PyBytes_AS_STRING(obj); |
| 2570 | if (i) { |
| 2571 | memcpy(dest, self->ob_bytes, mysize); |
| 2572 | dest += mysize; |
| 2573 | } |
| 2574 | memcpy(dest, buf, size); |
| 2575 | dest += size; |
| 2576 | } |
| 2577 | |
| 2578 | /* Done */ |
| 2579 | Py_DECREF(seq); |
| 2580 | return result; |
| 2581 | |
| 2582 | /* Error handling */ |
| 2583 | error: |
| 2584 | Py_DECREF(seq); |
| 2585 | return NULL; |
| 2586 | } |
| 2587 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2588 | PyDoc_STRVAR(splitlines__doc__, |
| 2589 | "B.splitlines([keepends]) -> list of lines\n\ |
| 2590 | \n\ |
| 2591 | Return a list of the lines in B, breaking at line boundaries.\n\ |
| 2592 | Line breaks are not included in the resulting list unless keepends\n\ |
| 2593 | is given and true."); |
| 2594 | |
| 2595 | static PyObject* |
| 2596 | bytearray_splitlines(PyObject *self, PyObject *args) |
| 2597 | { |
| 2598 | int keepends = 0; |
| 2599 | |
| 2600 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
| 2601 | return NULL; |
| 2602 | |
| 2603 | return stringlib_splitlines( |
| 2604 | (PyObject*) self, PyByteArray_AS_STRING(self), |
| 2605 | PyByteArray_GET_SIZE(self), keepends |
| 2606 | ); |
| 2607 | } |
| 2608 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2609 | PyDoc_STRVAR(fromhex_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2610 | "bytearray.fromhex(string) -> bytearray (static method)\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2611 | \n\ |
| 2612 | Create a bytearray object from a string of hexadecimal numbers.\n\ |
| 2613 | Spaces between two numbers are accepted.\n\ |
| 2614 | Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')."); |
| 2615 | |
| 2616 | static int |
| 2617 | hex_digit_to_int(Py_UNICODE c) |
| 2618 | { |
| 2619 | if (c >= 128) |
| 2620 | return -1; |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 2621 | if (Py_ISDIGIT(c)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2622 | return c - '0'; |
| 2623 | else { |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 2624 | if (Py_ISUPPER(c)) |
| 2625 | c = Py_TOLOWER(c); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2626 | if (c >= 'a' && c <= 'f') |
| 2627 | return c - 'a' + 10; |
| 2628 | } |
| 2629 | return -1; |
| 2630 | } |
| 2631 | |
| 2632 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2633 | bytearray_fromhex(PyObject *cls, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2634 | { |
| 2635 | PyObject *newbytes, *hexobj; |
| 2636 | char *buf; |
| 2637 | Py_UNICODE *hex; |
| 2638 | Py_ssize_t hexlen, byteslen, i, j; |
| 2639 | int top, bot; |
| 2640 | |
| 2641 | if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj)) |
| 2642 | return NULL; |
| 2643 | assert(PyUnicode_Check(hexobj)); |
| 2644 | hexlen = PyUnicode_GET_SIZE(hexobj); |
| 2645 | hex = PyUnicode_AS_UNICODE(hexobj); |
| 2646 | byteslen = hexlen/2; /* This overestimates if there are spaces */ |
| 2647 | newbytes = PyByteArray_FromStringAndSize(NULL, byteslen); |
| 2648 | if (!newbytes) |
| 2649 | return NULL; |
| 2650 | buf = PyByteArray_AS_STRING(newbytes); |
| 2651 | for (i = j = 0; i < hexlen; i += 2) { |
| 2652 | /* skip over spaces in the input */ |
| 2653 | while (hex[i] == ' ') |
| 2654 | i++; |
| 2655 | if (i >= hexlen) |
| 2656 | break; |
| 2657 | top = hex_digit_to_int(hex[i]); |
| 2658 | bot = hex_digit_to_int(hex[i+1]); |
| 2659 | if (top == -1 || bot == -1) { |
| 2660 | PyErr_Format(PyExc_ValueError, |
| 2661 | "non-hexadecimal number found in " |
| 2662 | "fromhex() arg at position %zd", i); |
| 2663 | goto error; |
| 2664 | } |
| 2665 | buf[j++] = (top << 4) + bot; |
| 2666 | } |
| 2667 | if (PyByteArray_Resize(newbytes, j) < 0) |
| 2668 | goto error; |
| 2669 | return newbytes; |
| 2670 | |
| 2671 | error: |
| 2672 | Py_DECREF(newbytes); |
| 2673 | return NULL; |
| 2674 | } |
| 2675 | |
| 2676 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 2677 | |
| 2678 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2679 | bytearray_reduce(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2680 | { |
| 2681 | PyObject *latin1, *dict; |
| 2682 | if (self->ob_bytes) |
| 2683 | latin1 = PyUnicode_DecodeLatin1(self->ob_bytes, |
| 2684 | Py_SIZE(self), NULL); |
| 2685 | else |
| 2686 | latin1 = PyUnicode_FromString(""); |
| 2687 | |
| 2688 | dict = PyObject_GetAttrString((PyObject *)self, "__dict__"); |
| 2689 | if (dict == NULL) { |
| 2690 | PyErr_Clear(); |
| 2691 | dict = Py_None; |
| 2692 | Py_INCREF(dict); |
| 2693 | } |
| 2694 | |
| 2695 | return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict); |
| 2696 | } |
| 2697 | |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2698 | PyDoc_STRVAR(sizeof_doc, |
| 2699 | "B.__sizeof__() -> int\n\ |
| 2700 | \n\ |
| 2701 | Returns the size of B in memory, in bytes"); |
| 2702 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2703 | bytearray_sizeof(PyByteArrayObject *self) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2704 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2705 | Py_ssize_t res; |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2706 | |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2707 | res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char); |
| 2708 | return PyLong_FromSsize_t(res); |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2711 | static PySequenceMethods bytearray_as_sequence = { |
| 2712 | (lenfunc)bytearray_length, /* sq_length */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2713 | (binaryfunc)PyByteArray_Concat, /* sq_concat */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2714 | (ssizeargfunc)bytearray_repeat, /* sq_repeat */ |
| 2715 | (ssizeargfunc)bytearray_getitem, /* sq_item */ |
| 2716 | 0, /* sq_slice */ |
| 2717 | (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */ |
| 2718 | 0, /* sq_ass_slice */ |
| 2719 | (objobjproc)bytearray_contains, /* sq_contains */ |
| 2720 | (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */ |
| 2721 | (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2722 | }; |
| 2723 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2724 | static PyMappingMethods bytearray_as_mapping = { |
| 2725 | (lenfunc)bytearray_length, |
| 2726 | (binaryfunc)bytearray_subscript, |
| 2727 | (objobjargproc)bytearray_ass_subscript, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2728 | }; |
| 2729 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2730 | static PyBufferProcs bytearray_as_buffer = { |
| 2731 | (getbufferproc)bytearray_getbuffer, |
| 2732 | (releasebufferproc)bytearray_releasebuffer, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2733 | }; |
| 2734 | |
| 2735 | static PyMethodDef |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2736 | bytearray_methods[] = { |
| 2737 | {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc}, |
| 2738 | {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, reduce_doc}, |
| 2739 | {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, sizeof_doc}, |
| 2740 | {"append", (PyCFunction)bytearray_append, METH_O, append__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2741 | {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, |
| 2742 | _Py_capitalize__doc__}, |
| 2743 | {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2744 | {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__}, |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 2745 | {"decode", (PyCFunction)bytearray_decode, METH_VARARGS | METH_KEYWORDS, decode_doc}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2746 | {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2747 | {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS, |
| 2748 | expandtabs__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2749 | {"extend", (PyCFunction)bytearray_extend, METH_O, extend__doc__}, |
| 2750 | {"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__}, |
| 2751 | {"fromhex", (PyCFunction)bytearray_fromhex, METH_VARARGS|METH_CLASS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2752 | fromhex_doc}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2753 | {"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__}, |
| 2754 | {"insert", (PyCFunction)bytearray_insert, METH_VARARGS, insert__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2755 | {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, |
| 2756 | _Py_isalnum__doc__}, |
| 2757 | {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, |
| 2758 | _Py_isalpha__doc__}, |
| 2759 | {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, |
| 2760 | _Py_isdigit__doc__}, |
| 2761 | {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, |
| 2762 | _Py_islower__doc__}, |
| 2763 | {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, |
| 2764 | _Py_isspace__doc__}, |
| 2765 | {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, |
| 2766 | _Py_istitle__doc__}, |
| 2767 | {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, |
| 2768 | _Py_isupper__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2769 | {"join", (PyCFunction)bytearray_join, METH_O, join_doc}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2770 | {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__}, |
| 2771 | {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2772 | {"lstrip", (PyCFunction)bytearray_lstrip, METH_VARARGS, lstrip__doc__}, |
| 2773 | {"maketrans", (PyCFunction)bytearray_maketrans, METH_VARARGS|METH_STATIC, |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 2774 | _Py_maketrans__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2775 | {"partition", (PyCFunction)bytearray_partition, METH_O, partition__doc__}, |
| 2776 | {"pop", (PyCFunction)bytearray_pop, METH_VARARGS, pop__doc__}, |
| 2777 | {"remove", (PyCFunction)bytearray_remove, METH_O, remove__doc__}, |
| 2778 | {"replace", (PyCFunction)bytearray_replace, METH_VARARGS, replace__doc__}, |
| 2779 | {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, reverse__doc__}, |
| 2780 | {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__}, |
| 2781 | {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2782 | {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2783 | {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, rpartition__doc__}, |
| 2784 | {"rsplit", (PyCFunction)bytearray_rsplit, METH_VARARGS, rsplit__doc__}, |
| 2785 | {"rstrip", (PyCFunction)bytearray_rstrip, METH_VARARGS, rstrip__doc__}, |
| 2786 | {"split", (PyCFunction)bytearray_split, METH_VARARGS, split__doc__}, |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2787 | {"splitlines", (PyCFunction)bytearray_splitlines, METH_VARARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2788 | splitlines__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2789 | {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS , |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2790 | startswith__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2791 | {"strip", (PyCFunction)bytearray_strip, METH_VARARGS, strip__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2792 | {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, |
| 2793 | _Py_swapcase__doc__}, |
| 2794 | {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2795 | {"translate", (PyCFunction)bytearray_translate, METH_VARARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2796 | translate__doc__}, |
| 2797 | {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
| 2798 | {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__}, |
| 2799 | {NULL} |
| 2800 | }; |
| 2801 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2802 | PyDoc_STRVAR(bytearray_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2803 | "bytearray(iterable_of_ints) -> bytearray\n\ |
| 2804 | bytearray(string, encoding[, errors]) -> bytearray\n\ |
| 2805 | bytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray\n\ |
| 2806 | bytearray(memory_view) -> bytearray\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2807 | \n\ |
| 2808 | Construct an mutable bytearray object from:\n\ |
| 2809 | - an iterable yielding integers in range(256)\n\ |
| 2810 | - a text string encoded using the specified encoding\n\ |
| 2811 | - a bytes or a bytearray object\n\ |
| 2812 | - any object implementing the buffer API.\n\ |
| 2813 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2814 | bytearray(int) -> bytearray\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2815 | \n\ |
| 2816 | Construct a zero-initialized bytearray of the given length."); |
| 2817 | |
| 2818 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2819 | static PyObject *bytearray_iter(PyObject *seq); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2820 | |
| 2821 | PyTypeObject PyByteArray_Type = { |
| 2822 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2823 | "bytearray", |
| 2824 | sizeof(PyByteArrayObject), |
| 2825 | 0, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2826 | (destructor)bytearray_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2827 | 0, /* tp_print */ |
| 2828 | 0, /* tp_getattr */ |
| 2829 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 2830 | 0, /* tp_reserved */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2831 | (reprfunc)bytearray_repr, /* tp_repr */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2832 | 0, /* tp_as_number */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2833 | &bytearray_as_sequence, /* tp_as_sequence */ |
| 2834 | &bytearray_as_mapping, /* tp_as_mapping */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2835 | 0, /* tp_hash */ |
| 2836 | 0, /* tp_call */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2837 | bytearray_str, /* tp_str */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2838 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2839 | 0, /* tp_setattro */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2840 | &bytearray_as_buffer, /* tp_as_buffer */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2841 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2842 | bytearray_doc, /* tp_doc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2843 | 0, /* tp_traverse */ |
| 2844 | 0, /* tp_clear */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2845 | (richcmpfunc)bytearray_richcompare, /* tp_richcompare */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2846 | 0, /* tp_weaklistoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2847 | bytearray_iter, /* tp_iter */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2848 | 0, /* tp_iternext */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2849 | bytearray_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2850 | 0, /* tp_members */ |
| 2851 | 0, /* tp_getset */ |
| 2852 | 0, /* tp_base */ |
| 2853 | 0, /* tp_dict */ |
| 2854 | 0, /* tp_descr_get */ |
| 2855 | 0, /* tp_descr_set */ |
| 2856 | 0, /* tp_dictoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2857 | (initproc)bytearray_init, /* tp_init */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2858 | PyType_GenericAlloc, /* tp_alloc */ |
| 2859 | PyType_GenericNew, /* tp_new */ |
| 2860 | PyObject_Del, /* tp_free */ |
| 2861 | }; |
| 2862 | |
| 2863 | /*********************** Bytes Iterator ****************************/ |
| 2864 | |
| 2865 | typedef struct { |
| 2866 | PyObject_HEAD |
| 2867 | Py_ssize_t it_index; |
| 2868 | PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 2869 | } bytesiterobject; |
| 2870 | |
| 2871 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2872 | bytearrayiter_dealloc(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2873 | { |
| 2874 | _PyObject_GC_UNTRACK(it); |
| 2875 | Py_XDECREF(it->it_seq); |
| 2876 | PyObject_GC_Del(it); |
| 2877 | } |
| 2878 | |
| 2879 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2880 | bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2881 | { |
| 2882 | Py_VISIT(it->it_seq); |
| 2883 | return 0; |
| 2884 | } |
| 2885 | |
| 2886 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2887 | bytearrayiter_next(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2888 | { |
| 2889 | PyByteArrayObject *seq; |
| 2890 | PyObject *item; |
| 2891 | |
| 2892 | assert(it != NULL); |
| 2893 | seq = it->it_seq; |
| 2894 | if (seq == NULL) |
| 2895 | return NULL; |
| 2896 | assert(PyByteArray_Check(seq)); |
| 2897 | |
| 2898 | if (it->it_index < PyByteArray_GET_SIZE(seq)) { |
| 2899 | item = PyLong_FromLong( |
| 2900 | (unsigned char)seq->ob_bytes[it->it_index]); |
| 2901 | if (item != NULL) |
| 2902 | ++it->it_index; |
| 2903 | return item; |
| 2904 | } |
| 2905 | |
| 2906 | Py_DECREF(seq); |
| 2907 | it->it_seq = NULL; |
| 2908 | return NULL; |
| 2909 | } |
| 2910 | |
| 2911 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2912 | bytesarrayiter_length_hint(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2913 | { |
| 2914 | Py_ssize_t len = 0; |
| 2915 | if (it->it_seq) |
| 2916 | len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index; |
| 2917 | return PyLong_FromSsize_t(len); |
| 2918 | } |
| 2919 | |
| 2920 | PyDoc_STRVAR(length_hint_doc, |
| 2921 | "Private method returning an estimate of len(list(it))."); |
| 2922 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2923 | static PyMethodDef bytearrayiter_methods[] = { |
| 2924 | {"__length_hint__", (PyCFunction)bytesarrayiter_length_hint, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2925 | length_hint_doc}, |
| 2926 | {NULL, NULL} /* sentinel */ |
| 2927 | }; |
| 2928 | |
| 2929 | PyTypeObject PyByteArrayIter_Type = { |
| 2930 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2931 | "bytearray_iterator", /* tp_name */ |
| 2932 | sizeof(bytesiterobject), /* tp_basicsize */ |
| 2933 | 0, /* tp_itemsize */ |
| 2934 | /* methods */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2935 | (destructor)bytearrayiter_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2936 | 0, /* tp_print */ |
| 2937 | 0, /* tp_getattr */ |
| 2938 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 2939 | 0, /* tp_reserved */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2940 | 0, /* tp_repr */ |
| 2941 | 0, /* tp_as_number */ |
| 2942 | 0, /* tp_as_sequence */ |
| 2943 | 0, /* tp_as_mapping */ |
| 2944 | 0, /* tp_hash */ |
| 2945 | 0, /* tp_call */ |
| 2946 | 0, /* tp_str */ |
| 2947 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2948 | 0, /* tp_setattro */ |
| 2949 | 0, /* tp_as_buffer */ |
| 2950 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 2951 | 0, /* tp_doc */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2952 | (traverseproc)bytearrayiter_traverse, /* tp_traverse */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2953 | 0, /* tp_clear */ |
| 2954 | 0, /* tp_richcompare */ |
| 2955 | 0, /* tp_weaklistoffset */ |
| 2956 | PyObject_SelfIter, /* tp_iter */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2957 | (iternextfunc)bytearrayiter_next, /* tp_iternext */ |
| 2958 | bytearrayiter_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2959 | 0, |
| 2960 | }; |
| 2961 | |
| 2962 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2963 | bytearray_iter(PyObject *seq) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2964 | { |
| 2965 | bytesiterobject *it; |
| 2966 | |
| 2967 | if (!PyByteArray_Check(seq)) { |
| 2968 | PyErr_BadInternalCall(); |
| 2969 | return NULL; |
| 2970 | } |
| 2971 | it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type); |
| 2972 | if (it == NULL) |
| 2973 | return NULL; |
| 2974 | it->it_index = 0; |
| 2975 | Py_INCREF(seq); |
| 2976 | it->it_seq = (PyByteArrayObject *)seq; |
| 2977 | _PyObject_GC_TRACK(it); |
| 2978 | return (PyObject *)it; |
| 2979 | } |