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