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