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