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