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