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