blob: b9a87d6a31594117dc8242fc5152650f57509214 [file] [log] [blame]
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001/* PyByteArray (bytearray) implementation */
2
3#define PY_SSIZE_T_CLEAN
4#include "Python.h"
5#include "structmember.h"
6#include "bytes_methods.h"
Ethan Furmanb95b5612015-01-23 20:05:18 -08007#include "bytesobject.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00008
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02009/*[clinic input]
10class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
11[clinic start generated code]*/
12/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/
13
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000014char _PyByteArray_empty_string[] = "";
Christian Heimes2c9c7a52008-05-26 13:42:13 +000015
16void
17PyByteArray_Fini(void)
18{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000019}
20
21int
22PyByteArray_Init(void)
23{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000024 return 1;
25}
26
27/* end nullbytes support */
28
29/* Helpers */
30
31static int
32_getbytevalue(PyObject* arg, int *value)
33{
34 long face_value;
35
36 if (PyLong_Check(arg)) {
37 face_value = PyLong_AsLong(arg);
Georg Brandl9a54d7c2008-07-16 23:15:30 +000038 } else {
39 PyObject *index = PyNumber_Index(arg);
40 if (index == NULL) {
41 PyErr_Format(PyExc_TypeError, "an integer is required");
Mark Dickinson10de93a2010-07-09 19:25:48 +000042 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000043 return 0;
44 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +000045 face_value = PyLong_AsLong(index);
46 Py_DECREF(index);
47 }
48
49 if (face_value < 0 || face_value >= 256) {
50 /* this includes the OverflowError in case the long is too large */
51 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
Mark Dickinson10de93a2010-07-09 19:25:48 +000052 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000053 return 0;
54 }
55
56 *value = face_value;
57 return 1;
58}
59
60static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +000061bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000062{
63 int ret;
64 void *ptr;
65 if (view == NULL) {
66 obj->ob_exports++;
67 return 0;
68 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000069 ptr = (void *) PyByteArray_AS_STRING(obj);
Martin v. Löwis423be952008-08-13 15:53:07 +000070 ret = PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000071 if (ret >= 0) {
72 obj->ob_exports++;
73 }
74 return ret;
75}
76
77static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +000078bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000079{
80 obj->ob_exports--;
81}
82
83static Py_ssize_t
84_getbuffer(PyObject *obj, Py_buffer *view)
85{
86 PyBufferProcs *buffer = Py_TYPE(obj)->tp_as_buffer;
87
88 if (buffer == NULL || buffer->bf_getbuffer == NULL)
89 {
90 PyErr_Format(PyExc_TypeError,
R David Murray861470c2014-10-05 11:47:01 -040091 "a bytes-like object is required, not '%.100s'",
Christian Heimes2c9c7a52008-05-26 13:42:13 +000092 Py_TYPE(obj)->tp_name);
93 return -1;
94 }
95
96 if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0)
97 return -1;
98 return view->len;
99}
100
Antoine Pitrou5504e892008-12-06 21:27:53 +0000101static int
102_canresize(PyByteArrayObject *self)
103{
104 if (self->ob_exports > 0) {
105 PyErr_SetString(PyExc_BufferError,
106 "Existing exports of data: object cannot be re-sized");
107 return 0;
108 }
109 return 1;
110}
111
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000112/* Direct API functions */
113
114PyObject *
115PyByteArray_FromObject(PyObject *input)
116{
117 return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type,
118 input, NULL);
119}
120
121PyObject *
122PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
123{
124 PyByteArrayObject *new;
125 Py_ssize_t alloc;
126
127 if (size < 0) {
128 PyErr_SetString(PyExc_SystemError,
129 "Negative size passed to PyByteArray_FromStringAndSize");
130 return NULL;
131 }
132
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000133 /* Prevent buffer overflow when setting alloc to size+1. */
134 if (size == PY_SSIZE_T_MAX) {
135 return PyErr_NoMemory();
136 }
137
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000138 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
139 if (new == NULL)
140 return NULL;
141
142 if (size == 0) {
143 new->ob_bytes = NULL;
144 alloc = 0;
145 }
146 else {
147 alloc = size + 1;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100148 new->ob_bytes = PyObject_Malloc(alloc);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000149 if (new->ob_bytes == NULL) {
150 Py_DECREF(new);
151 return PyErr_NoMemory();
152 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +0000153 if (bytes != NULL && size > 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000154 memcpy(new->ob_bytes, bytes, size);
155 new->ob_bytes[size] = '\0'; /* Trailing null byte */
156 }
157 Py_SIZE(new) = size;
158 new->ob_alloc = alloc;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200159 new->ob_start = new->ob_bytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000160 new->ob_exports = 0;
161
162 return (PyObject *)new;
163}
164
165Py_ssize_t
166PyByteArray_Size(PyObject *self)
167{
168 assert(self != NULL);
169 assert(PyByteArray_Check(self));
170
171 return PyByteArray_GET_SIZE(self);
172}
173
174char *
175PyByteArray_AsString(PyObject *self)
176{
177 assert(self != NULL);
178 assert(PyByteArray_Check(self));
179
180 return PyByteArray_AS_STRING(self);
181}
182
183int
Antoine Pitroucc231542014-11-02 18:40:09 +0100184PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000185{
186 void *sval;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200187 PyByteArrayObject *obj = ((PyByteArrayObject *)self);
Antoine Pitroucc231542014-11-02 18:40:09 +0100188 /* All computations are done unsigned to avoid integer overflows
189 (see issue #22335). */
190 size_t alloc = (size_t) obj->ob_alloc;
191 size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
192 size_t size = (size_t) requested_size;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000193
194 assert(self != NULL);
195 assert(PyByteArray_Check(self));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200196 assert(logical_offset <= alloc);
Antoine Pitroucc231542014-11-02 18:40:09 +0100197 assert(requested_size >= 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000198
Antoine Pitroucc231542014-11-02 18:40:09 +0100199 if (requested_size == Py_SIZE(self)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000200 return 0;
201 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200202 if (!_canresize(obj)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000203 return -1;
204 }
205
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200206 if (size + logical_offset + 1 < alloc) {
207 /* Current buffer is large enough to host the requested size,
208 decide on a strategy. */
209 if (size < alloc / 2) {
210 /* Major downsize; resize down to exact size */
211 alloc = size + 1;
212 }
213 else {
214 /* Minor downsize; quick exit */
215 Py_SIZE(self) = size;
216 PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
217 return 0;
218 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000219 }
220 else {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200221 /* Need growing, decide on a strategy */
222 if (size <= alloc * 1.125) {
223 /* Moderate upsize; overallocate similar to list_resize() */
224 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
225 }
226 else {
227 /* Major upsize; resize up to exact size */
228 alloc = size + 1;
229 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000230 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100231 if (alloc > PY_SSIZE_T_MAX) {
232 PyErr_NoMemory();
233 return -1;
234 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000235
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200236 if (logical_offset > 0) {
237 sval = PyObject_Malloc(alloc);
238 if (sval == NULL) {
239 PyErr_NoMemory();
240 return -1;
241 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100242 memcpy(sval, PyByteArray_AS_STRING(self),
243 Py_MIN(requested_size, Py_SIZE(self)));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200244 PyObject_Free(obj->ob_bytes);
245 }
246 else {
247 sval = PyObject_Realloc(obj->ob_bytes, alloc);
248 if (sval == NULL) {
249 PyErr_NoMemory();
250 return -1;
251 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000252 }
253
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200254 obj->ob_bytes = obj->ob_start = sval;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000255 Py_SIZE(self) = size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200256 obj->ob_alloc = alloc;
257 obj->ob_bytes[size] = '\0'; /* Trailing null byte */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000258
259 return 0;
260}
261
262PyObject *
263PyByteArray_Concat(PyObject *a, PyObject *b)
264{
265 Py_ssize_t size;
266 Py_buffer va, vb;
267 PyByteArrayObject *result = NULL;
268
269 va.len = -1;
270 vb.len = -1;
271 if (_getbuffer(a, &va) < 0 ||
272 _getbuffer(b, &vb) < 0) {
273 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
274 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
275 goto done;
276 }
277
278 size = va.len + vb.len;
279 if (size < 0) {
Benjamin Petersone0124bd2009-03-09 21:04:33 +0000280 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000281 goto done;
282 }
283
284 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, size);
285 if (result != NULL) {
286 memcpy(result->ob_bytes, va.buf, va.len);
287 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
288 }
289
290 done:
291 if (va.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000292 PyBuffer_Release(&va);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000293 if (vb.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000294 PyBuffer_Release(&vb);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000295 return (PyObject *)result;
296}
297
Ethan Furmanb95b5612015-01-23 20:05:18 -0800298static PyObject *
299bytearray_format(PyByteArrayObject *self, PyObject *args)
300{
301 PyObject *bytes_in, *bytes_out, *res;
302 char *bytestring;
303
304 if (self == NULL || !PyByteArray_Check(self) || args == NULL) {
305 PyErr_BadInternalCall();
306 return NULL;
307 }
308 bytestring = PyByteArray_AS_STRING(self);
309 bytes_in = PyBytes_FromString(bytestring);
310 if (bytes_in == NULL)
311 return NULL;
312 bytes_out = _PyBytes_Format(bytes_in, args);
313 Py_DECREF(bytes_in);
314 if (bytes_out == NULL)
315 return NULL;
316 res = PyByteArray_FromObject(bytes_out);
317 Py_DECREF(bytes_out);
318 if (res == NULL)
319 return NULL;
320 return res;
321}
322
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000323/* Functions stuffed into the type object */
324
325static Py_ssize_t
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000326bytearray_length(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000327{
328 return Py_SIZE(self);
329}
330
331static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000332bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000333{
334 Py_ssize_t mysize;
335 Py_ssize_t size;
336 Py_buffer vo;
337
338 if (_getbuffer(other, &vo) < 0) {
339 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
340 Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
341 return NULL;
342 }
343
344 mysize = Py_SIZE(self);
345 size = mysize + vo.len;
346 if (size < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000347 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000348 return PyErr_NoMemory();
349 }
350 if (size < self->ob_alloc) {
351 Py_SIZE(self) = size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200352 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0'; /* Trailing null byte */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000353 }
354 else if (PyByteArray_Resize((PyObject *)self, size) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000355 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000356 return NULL;
357 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200358 memcpy(PyByteArray_AS_STRING(self) + mysize, vo.buf, vo.len);
Martin v. Löwis423be952008-08-13 15:53:07 +0000359 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000360 Py_INCREF(self);
361 return (PyObject *)self;
362}
363
364static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000365bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000366{
367 PyByteArrayObject *result;
368 Py_ssize_t mysize;
369 Py_ssize_t size;
370
371 if (count < 0)
372 count = 0;
373 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000374 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000375 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000376 size = mysize * count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000377 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
378 if (result != NULL && size != 0) {
379 if (mysize == 1)
380 memset(result->ob_bytes, self->ob_bytes[0], size);
381 else {
382 Py_ssize_t i;
383 for (i = 0; i < count; i++)
384 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
385 }
386 }
387 return (PyObject *)result;
388}
389
390static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000391bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000392{
393 Py_ssize_t mysize;
394 Py_ssize_t size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200395 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000396
397 if (count < 0)
398 count = 0;
399 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000400 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000401 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000402 size = mysize * count;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200403 if (PyByteArray_Resize((PyObject *)self, size) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000404 return NULL;
405
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200406 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000407 if (mysize == 1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200408 memset(buf, buf[0], size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000409 else {
410 Py_ssize_t i;
411 for (i = 1; i < count; i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200412 memcpy(buf + i*mysize, buf, mysize);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000413 }
414
415 Py_INCREF(self);
416 return (PyObject *)self;
417}
418
419static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000420bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000421{
422 if (i < 0)
423 i += Py_SIZE(self);
424 if (i < 0 || i >= Py_SIZE(self)) {
425 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
426 return NULL;
427 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200428 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000429}
430
431static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000432bytearray_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000433{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000434 if (PyIndex_Check(index)) {
435 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000436
437 if (i == -1 && PyErr_Occurred())
438 return NULL;
439
440 if (i < 0)
441 i += PyByteArray_GET_SIZE(self);
442
443 if (i < 0 || i >= Py_SIZE(self)) {
444 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
445 return NULL;
446 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200447 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000448 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000449 else if (PySlice_Check(index)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000450 Py_ssize_t start, stop, step, slicelength, cur, i;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000451 if (PySlice_GetIndicesEx(index,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000452 PyByteArray_GET_SIZE(self),
453 &start, &stop, &step, &slicelength) < 0) {
454 return NULL;
455 }
456
457 if (slicelength <= 0)
458 return PyByteArray_FromStringAndSize("", 0);
459 else if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200460 return PyByteArray_FromStringAndSize(
461 PyByteArray_AS_STRING(self) + start, slicelength);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000462 }
463 else {
464 char *source_buf = PyByteArray_AS_STRING(self);
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000465 char *result_buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000466 PyObject *result;
467
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000468 result = PyByteArray_FromStringAndSize(NULL, slicelength);
469 if (result == NULL)
470 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000471
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000472 result_buf = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000473 for (cur = start, i = 0; i < slicelength;
474 cur += step, i++) {
475 result_buf[i] = source_buf[cur];
476 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000477 return result;
478 }
479 }
480 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400481 PyErr_Format(PyExc_TypeError,
482 "bytearray indices must be integers or slices, not %.200s",
483 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000484 return NULL;
485 }
486}
487
488static int
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200489bytearray_setslice_linear(PyByteArrayObject *self,
490 Py_ssize_t lo, Py_ssize_t hi,
491 char *bytes, Py_ssize_t bytes_len)
492{
493 Py_ssize_t avail = hi - lo;
494 char *buf = PyByteArray_AS_STRING(self);
495 Py_ssize_t growth = bytes_len - avail;
Victor Stinner84557232013-11-21 12:29:51 +0100496 int res = 0;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200497 assert(avail >= 0);
498
Victor Stinner84557232013-11-21 12:29:51 +0100499 if (growth < 0) {
500 if (!_canresize(self))
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200501 return -1;
Victor Stinner84557232013-11-21 12:29:51 +0100502
503 if (lo == 0) {
504 /* Shrink the buffer by advancing its logical start */
505 self->ob_start -= growth;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200506 /*
Victor Stinner84557232013-11-21 12:29:51 +0100507 0 lo hi old_size
508 | |<----avail----->|<-----tail------>|
509 | |<-bytes_len->|<-----tail------>|
510 0 new_lo new_hi new_size
511 */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200512 }
Victor Stinner84557232013-11-21 12:29:51 +0100513 else {
514 /*
515 0 lo hi old_size
516 | |<----avail----->|<-----tomove------>|
517 | |<-bytes_len->|<-----tomove------>|
518 0 lo new_hi new_size
519 */
520 memmove(buf + lo + bytes_len, buf + hi,
521 Py_SIZE(self) - hi);
522 }
523 if (PyByteArray_Resize((PyObject *)self,
524 Py_SIZE(self) + growth) < 0) {
525 /* Issue #19578: Handling the memory allocation failure here is
526 tricky here because the bytearray object has already been
527 modified. Depending on growth and lo, the behaviour is
528 different.
529
530 If growth < 0 and lo != 0, the operation is completed, but a
531 MemoryError is still raised and the memory block is not
532 shrinked. Otherwise, the bytearray is restored in its previous
533 state and a MemoryError is raised. */
534 if (lo == 0) {
535 self->ob_start += growth;
536 return -1;
537 }
538 /* memmove() removed bytes, the bytearray object cannot be
539 restored in its previous state. */
540 Py_SIZE(self) += growth;
541 res = -1;
542 }
543 buf = PyByteArray_AS_STRING(self);
544 }
545 else if (growth > 0) {
546 if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
547 PyErr_NoMemory();
548 return -1;
549 }
550
551 if (PyByteArray_Resize((PyObject *)self,
552 Py_SIZE(self) + growth) < 0) {
553 return -1;
554 }
555 buf = PyByteArray_AS_STRING(self);
556 /* Make the place for the additional bytes */
557 /*
558 0 lo hi old_size
559 | |<-avail->|<-----tomove------>|
560 | |<---bytes_len-->|<-----tomove------>|
561 0 lo new_hi new_size
562 */
563 memmove(buf + lo + bytes_len, buf + hi,
564 Py_SIZE(self) - lo - bytes_len);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200565 }
566
567 if (bytes_len > 0)
568 memcpy(buf + lo, bytes, bytes_len);
Victor Stinner84557232013-11-21 12:29:51 +0100569 return res;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200570}
571
572static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000573bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000574 PyObject *values)
575{
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200576 Py_ssize_t needed;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000577 void *bytes;
578 Py_buffer vbytes;
579 int res = 0;
580
581 vbytes.len = -1;
582 if (values == (PyObject *)self) {
583 /* Make a copy and call this function recursively */
584 int err;
585 values = PyByteArray_FromObject(values);
586 if (values == NULL)
587 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000588 err = bytearray_setslice(self, lo, hi, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000589 Py_DECREF(values);
590 return err;
591 }
592 if (values == NULL) {
593 /* del b[lo:hi] */
594 bytes = NULL;
595 needed = 0;
596 }
597 else {
598 if (_getbuffer(values, &vbytes) < 0) {
599 PyErr_Format(PyExc_TypeError,
Georg Brandl3dbca812008-07-23 16:10:53 +0000600 "can't set bytearray slice from %.100s",
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000601 Py_TYPE(values)->tp_name);
602 return -1;
603 }
604 needed = vbytes.len;
605 bytes = vbytes.buf;
606 }
607
608 if (lo < 0)
609 lo = 0;
610 if (hi < lo)
611 hi = lo;
612 if (hi > Py_SIZE(self))
613 hi = Py_SIZE(self);
614
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200615 res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000616 if (vbytes.len != -1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200617 PyBuffer_Release(&vbytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000618 return res;
619}
620
621static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000622bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000623{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000624 int ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000625
626 if (i < 0)
627 i += Py_SIZE(self);
628
629 if (i < 0 || i >= Py_SIZE(self)) {
630 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
631 return -1;
632 }
633
634 if (value == NULL)
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000635 return bytearray_setslice(self, i, i+1, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000636
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000637 if (!_getbytevalue(value, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000638 return -1;
639
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200640 PyByteArray_AS_STRING(self)[i] = ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000641 return 0;
642}
643
644static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000645bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000646{
647 Py_ssize_t start, stop, step, slicelen, needed;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200648 char *buf, *bytes;
649 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000650
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000651 if (PyIndex_Check(index)) {
652 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000653
654 if (i == -1 && PyErr_Occurred())
655 return -1;
656
657 if (i < 0)
658 i += PyByteArray_GET_SIZE(self);
659
660 if (i < 0 || i >= Py_SIZE(self)) {
661 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
662 return -1;
663 }
664
665 if (values == NULL) {
666 /* Fall through to slice assignment */
667 start = i;
668 stop = i + 1;
669 step = 1;
670 slicelen = 1;
671 }
672 else {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000673 int ival;
674 if (!_getbytevalue(values, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000675 return -1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200676 buf[i] = (char)ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000677 return 0;
678 }
679 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000680 else if (PySlice_Check(index)) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000681 if (PySlice_GetIndicesEx(index,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000682 PyByteArray_GET_SIZE(self),
683 &start, &stop, &step, &slicelen) < 0) {
684 return -1;
685 }
686 }
687 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400688 PyErr_Format(PyExc_TypeError,
689 "bytearray indices must be integers or slices, not %.200s",
690 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000691 return -1;
692 }
693
694 if (values == NULL) {
695 bytes = NULL;
696 needed = 0;
697 }
698 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
Christian Heimes6d26ade2012-11-03 23:07:59 +0100699 int err;
Ezio Melottic64bcbe2012-11-03 21:19:06 +0200700 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
701 PyErr_SetString(PyExc_TypeError,
702 "can assign only bytes, buffers, or iterables "
703 "of ints in range(0, 256)");
704 return -1;
705 }
Georg Brandlf3fa5682010-12-04 17:09:30 +0000706 /* Make a copy and call this function recursively */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000707 values = PyByteArray_FromObject(values);
708 if (values == NULL)
709 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000710 err = bytearray_ass_subscript(self, index, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000711 Py_DECREF(values);
712 return err;
713 }
714 else {
715 assert(PyByteArray_Check(values));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200716 bytes = PyByteArray_AS_STRING(values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000717 needed = Py_SIZE(values);
718 }
719 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
720 if ((step < 0 && start < stop) ||
721 (step > 0 && start > stop))
722 stop = start;
723 if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200724 return bytearray_setslice_linear(self, start, stop, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000725 }
726 else {
727 if (needed == 0) {
728 /* Delete slice */
Mark Dickinsonbc099642010-01-29 17:27:24 +0000729 size_t cur;
730 Py_ssize_t i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000731
Antoine Pitrou5504e892008-12-06 21:27:53 +0000732 if (!_canresize(self))
733 return -1;
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000734
735 if (slicelen == 0)
736 /* Nothing to do here. */
737 return 0;
738
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000739 if (step < 0) {
740 stop = start + 1;
741 start = stop + step * (slicelen - 1) - 1;
742 step = -step;
743 }
744 for (cur = start, i = 0;
745 i < slicelen; cur += step, i++) {
746 Py_ssize_t lim = step - 1;
747
Mark Dickinson66f575b2010-02-14 12:53:32 +0000748 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000749 lim = PyByteArray_GET_SIZE(self) - cur - 1;
750
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200751 memmove(buf + cur - i,
752 buf + cur + 1, lim);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000753 }
754 /* Move the tail of the bytes, in one chunk */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000755 cur = start + (size_t)slicelen*step;
Mark Dickinson66f575b2010-02-14 12:53:32 +0000756 if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200757 memmove(buf + cur - slicelen,
758 buf + cur,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000759 PyByteArray_GET_SIZE(self) - cur);
760 }
761 if (PyByteArray_Resize((PyObject *)self,
762 PyByteArray_GET_SIZE(self) - slicelen) < 0)
763 return -1;
764
765 return 0;
766 }
767 else {
768 /* Assign slice */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000769 Py_ssize_t i;
770 size_t cur;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000771
772 if (needed != slicelen) {
773 PyErr_Format(PyExc_ValueError,
774 "attempt to assign bytes of size %zd "
775 "to extended slice of size %zd",
776 needed, slicelen);
777 return -1;
778 }
779 for (cur = start, i = 0; i < slicelen; cur += step, i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200780 buf[cur] = bytes[i];
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000781 return 0;
782 }
783 }
784}
785
786static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000787bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000788{
789 static char *kwlist[] = {"source", "encoding", "errors", 0};
790 PyObject *arg = NULL;
791 const char *encoding = NULL;
792 const char *errors = NULL;
793 Py_ssize_t count;
794 PyObject *it;
795 PyObject *(*iternext)(PyObject *);
796
797 if (Py_SIZE(self) != 0) {
798 /* Empty previous contents (yes, do this first of all!) */
799 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
800 return -1;
801 }
802
803 /* Parse arguments */
Georg Brandl3dbca812008-07-23 16:10:53 +0000804 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000805 &arg, &encoding, &errors))
806 return -1;
807
808 /* Make a quick exit if no first argument */
809 if (arg == NULL) {
810 if (encoding != NULL || errors != NULL) {
811 PyErr_SetString(PyExc_TypeError,
812 "encoding or errors without sequence argument");
813 return -1;
814 }
815 return 0;
816 }
817
818 if (PyUnicode_Check(arg)) {
819 /* Encode via the codec registry */
820 PyObject *encoded, *new;
821 if (encoding == NULL) {
822 PyErr_SetString(PyExc_TypeError,
823 "string argument without an encoding");
824 return -1;
825 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000826 encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000827 if (encoded == NULL)
828 return -1;
829 assert(PyBytes_Check(encoded));
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000830 new = bytearray_iconcat(self, encoded);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000831 Py_DECREF(encoded);
832 if (new == NULL)
833 return -1;
834 Py_DECREF(new);
835 return 0;
836 }
837
838 /* If it's not unicode, there can't be encoding or errors */
839 if (encoding != NULL || errors != NULL) {
840 PyErr_SetString(PyExc_TypeError,
841 "encoding or errors without a string argument");
842 return -1;
843 }
844
845 /* Is it an int? */
Benjamin Peterson8380dd52010-04-16 22:51:37 +0000846 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
847 if (count == -1 && PyErr_Occurred()) {
848 if (PyErr_ExceptionMatches(PyExc_OverflowError))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000849 return -1;
Benjamin Peterson9c0e94f2010-04-16 23:00:53 +0000850 PyErr_Clear();
Benjamin Peterson8380dd52010-04-16 22:51:37 +0000851 }
852 else if (count < 0) {
853 PyErr_SetString(PyExc_ValueError, "negative count");
854 return -1;
855 }
856 else {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000857 if (count > 0) {
Victor Stinner2bc4d952014-06-02 22:22:42 +0200858 if (PyByteArray_Resize((PyObject *)self, count))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000859 return -1;
Victor Stinner2bc4d952014-06-02 22:22:42 +0200860 memset(PyByteArray_AS_STRING(self), 0, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000861 }
862 return 0;
863 }
864
865 /* Use the buffer API */
866 if (PyObject_CheckBuffer(arg)) {
867 Py_ssize_t size;
868 Py_buffer view;
869 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
870 return -1;
871 size = view.len;
872 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200873 if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
874 &view, size, 'C') < 0)
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200875 goto fail;
Martin v. Löwis423be952008-08-13 15:53:07 +0000876 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000877 return 0;
878 fail:
Martin v. Löwis423be952008-08-13 15:53:07 +0000879 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000880 return -1;
881 }
882
883 /* XXX Optimize this if the arguments is a list, tuple */
884
885 /* Get the iterator */
886 it = PyObject_GetIter(arg);
887 if (it == NULL)
888 return -1;
889 iternext = *Py_TYPE(it)->tp_iternext;
890
891 /* Run the iterator to exhaustion */
892 for (;;) {
893 PyObject *item;
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000894 int rc, value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000895
896 /* Get the next item */
897 item = iternext(it);
898 if (item == NULL) {
899 if (PyErr_Occurred()) {
900 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
901 goto error;
902 PyErr_Clear();
903 }
904 break;
905 }
906
907 /* Interpret it as an int (__index__) */
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000908 rc = _getbytevalue(item, &value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000909 Py_DECREF(item);
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000910 if (!rc)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000911 goto error;
912
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000913 /* Append the byte */
914 if (Py_SIZE(self) < self->ob_alloc)
915 Py_SIZE(self)++;
916 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
917 goto error;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200918 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000919 }
920
921 /* Clean up and return success */
922 Py_DECREF(it);
923 return 0;
924
925 error:
926 /* Error handling when it != NULL */
927 Py_DECREF(it);
928 return -1;
929}
930
931/* Mostly copied from string_repr, but without the
932 "smart quote" functionality. */
933static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000934bytearray_repr(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000935{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000936 const char *quote_prefix = "bytearray(b";
937 const char *quote_postfix = ")";
938 Py_ssize_t length = Py_SIZE(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200939 /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
Mark Dickinson66f575b2010-02-14 12:53:32 +0000940 size_t newsize;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000941 PyObject *v;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200942 Py_ssize_t i;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200943 char *bytes;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200944 char c;
945 char *p;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200946 int quote;
947 char *test, *start;
948 char *buffer;
949
950 if (length > (PY_SSIZE_T_MAX - 15) / 4) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000951 PyErr_SetString(PyExc_OverflowError,
952 "bytearray object is too large to make repr");
953 return NULL;
954 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200955
956 newsize = 15 + length * 4;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100957 buffer = PyObject_Malloc(newsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200958 if (buffer == NULL) {
959 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000960 return NULL;
961 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000962
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200963 /* Figure out which quote to use; single is preferred */
964 quote = '\'';
965 start = PyByteArray_AS_STRING(self);
966 for (test = start; test < start+length; ++test) {
967 if (*test == '"') {
968 quote = '\''; /* back to single */
969 break;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000970 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200971 else if (*test == '\'')
972 quote = '"';
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000973 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200974
975 p = buffer;
976 while (*quote_prefix)
977 *p++ = *quote_prefix++;
978 *p++ = quote;
979
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200980 bytes = PyByteArray_AS_STRING(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200981 for (i = 0; i < length; i++) {
982 /* There's at least enough room for a hex escape
983 and a closing quote. */
984 assert(newsize - (p - buffer) >= 5);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200985 c = bytes[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200986 if (c == '\'' || c == '\\')
987 *p++ = '\\', *p++ = c;
988 else if (c == '\t')
989 *p++ = '\\', *p++ = 't';
990 else if (c == '\n')
991 *p++ = '\\', *p++ = 'n';
992 else if (c == '\r')
993 *p++ = '\\', *p++ = 'r';
994 else if (c == 0)
995 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
996 else if (c < ' ' || c >= 0x7f) {
997 *p++ = '\\';
998 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200999 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
1000 *p++ = Py_hexdigits[c & 0xf];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001001 }
1002 else
1003 *p++ = c;
1004 }
1005 assert(newsize - (p - buffer) >= 1);
1006 *p++ = quote;
1007 while (*quote_postfix) {
1008 *p++ = *quote_postfix++;
1009 }
1010
1011 v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001012 PyObject_Free(buffer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001013 return v;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001014}
1015
1016static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001017bytearray_str(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001018{
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001019 if (Py_BytesWarningFlag) {
1020 if (PyErr_WarnEx(PyExc_BytesWarning,
1021 "str() on a bytearray instance", 1))
1022 return NULL;
1023 }
1024 return bytearray_repr((PyByteArrayObject*)op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001025}
1026
1027static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001028bytearray_richcompare(PyObject *self, PyObject *other, int op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001029{
1030 Py_ssize_t self_size, other_size;
1031 Py_buffer self_bytes, other_bytes;
1032 PyObject *res;
1033 Py_ssize_t minsize;
1034 int cmp;
1035
1036 /* Bytes can be compared to anything that supports the (binary)
1037 buffer API. Except that a comparison with Unicode is always an
1038 error, even if the comparison is for equality. */
1039 if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) ||
1040 PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) {
Barry Warsaw9e9dcd62008-10-17 01:50:37 +00001041 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001042 if (PyErr_WarnEx(PyExc_BytesWarning,
Georg Brandle5d68ac2008-06-04 11:30:26 +00001043 "Comparison between bytearray and string", 1))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001044 return NULL;
1045 }
1046
Brian Curtindfc80e32011-08-10 20:28:54 -05001047 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001048 }
1049
1050 self_size = _getbuffer(self, &self_bytes);
1051 if (self_size < 0) {
1052 PyErr_Clear();
Brian Curtindfc80e32011-08-10 20:28:54 -05001053 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001054 }
1055
1056 other_size = _getbuffer(other, &other_bytes);
1057 if (other_size < 0) {
1058 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00001059 PyBuffer_Release(&self_bytes);
Brian Curtindfc80e32011-08-10 20:28:54 -05001060 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001061 }
1062
1063 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1064 /* Shortcut: if the lengths differ, the objects differ */
1065 cmp = (op == Py_NE);
1066 }
1067 else {
1068 minsize = self_size;
1069 if (other_size < minsize)
1070 minsize = other_size;
1071
1072 cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
1073 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1074
1075 if (cmp == 0) {
1076 if (self_size < other_size)
1077 cmp = -1;
1078 else if (self_size > other_size)
1079 cmp = 1;
1080 }
1081
1082 switch (op) {
1083 case Py_LT: cmp = cmp < 0; break;
1084 case Py_LE: cmp = cmp <= 0; break;
1085 case Py_EQ: cmp = cmp == 0; break;
1086 case Py_NE: cmp = cmp != 0; break;
1087 case Py_GT: cmp = cmp > 0; break;
1088 case Py_GE: cmp = cmp >= 0; break;
1089 }
1090 }
1091
1092 res = cmp ? Py_True : Py_False;
Martin v. Löwis423be952008-08-13 15:53:07 +00001093 PyBuffer_Release(&self_bytes);
1094 PyBuffer_Release(&other_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001095 Py_INCREF(res);
1096 return res;
1097}
1098
1099static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001100bytearray_dealloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001101{
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001102 if (self->ob_exports > 0) {
1103 PyErr_SetString(PyExc_SystemError,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001104 "deallocated bytearray object has exported buffers");
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001105 PyErr_Print();
1106 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001107 if (self->ob_bytes != 0) {
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001108 PyObject_Free(self->ob_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001109 }
1110 Py_TYPE(self)->tp_free((PyObject *)self);
1111}
1112
1113
1114/* -------------------------------------------------------------------- */
1115/* Methods */
1116
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001117#define FASTSEARCH fastsearch
1118#define STRINGLIB(F) stringlib_##F
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001119#define STRINGLIB_CHAR char
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001120#define STRINGLIB_SIZEOF_CHAR 1
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001121#define STRINGLIB_LEN PyByteArray_GET_SIZE
1122#define STRINGLIB_STR PyByteArray_AS_STRING
1123#define STRINGLIB_NEW PyByteArray_FromStringAndSize
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001124#define STRINGLIB_ISSPACE Py_ISSPACE
1125#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001126#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1127#define STRINGLIB_MUTABLE 1
1128
1129#include "stringlib/fastsearch.h"
1130#include "stringlib/count.h"
1131#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001132#include "stringlib/join.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001133#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001134#include "stringlib/split.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001135#include "stringlib/ctype.h"
1136#include "stringlib/transmogrify.h"
1137
1138
1139/* The following Py_LOCAL_INLINE and Py_LOCAL functions
1140were copied from the old char* style string object. */
1141
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001142/* helper macro to fixup start/end slice values */
1143#define ADJUST_INDICES(start, end, len) \
1144 if (end > len) \
1145 end = len; \
1146 else if (end < 0) { \
1147 end += len; \
1148 if (end < 0) \
1149 end = 0; \
1150 } \
1151 if (start < 0) { \
1152 start += len; \
1153 if (start < 0) \
1154 start = 0; \
1155 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001156
1157Py_LOCAL_INLINE(Py_ssize_t)
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001158bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001159{
1160 PyObject *subobj;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001161 char byte;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001162 Py_buffer subbuf;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001163 const char *sub;
1164 Py_ssize_t sub_len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001165 Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
1166 Py_ssize_t res;
1167
Antoine Pitrouac65d962011-10-20 23:54:17 +02001168 if (!stringlib_parse_args_finds_byte("find/rfind/index/rindex",
1169 args, &subobj, &byte, &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001170 return -2;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001171
1172 if (subobj) {
1173 if (_getbuffer(subobj, &subbuf) < 0)
1174 return -2;
1175
1176 sub = subbuf.buf;
1177 sub_len = subbuf.len;
1178 }
1179 else {
1180 sub = &byte;
1181 sub_len = 1;
1182 }
1183
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001184 if (dir > 0)
1185 res = stringlib_find_slice(
1186 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
Antoine Pitrouac65d962011-10-20 23:54:17 +02001187 sub, sub_len, start, end);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001188 else
1189 res = stringlib_rfind_slice(
1190 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
Antoine Pitrouac65d962011-10-20 23:54:17 +02001191 sub, sub_len, start, end);
1192
1193 if (subobj)
1194 PyBuffer_Release(&subbuf);
1195
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001196 return res;
1197}
1198
1199PyDoc_STRVAR(find__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001200"B.find(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001201\n\
1202Return the lowest index in B where subsection sub is found,\n\
Senthil Kumaran53516a82011-07-27 23:33:54 +08001203such that sub is contained within B[start,end]. Optional\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001204arguments start and end are interpreted as in slice notation.\n\
1205\n\
1206Return -1 on failure.");
1207
1208static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001209bytearray_find(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001210{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001211 Py_ssize_t result = bytearray_find_internal(self, args, +1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001212 if (result == -2)
1213 return NULL;
1214 return PyLong_FromSsize_t(result);
1215}
1216
1217PyDoc_STRVAR(count__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001218"B.count(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001219\n\
1220Return the number of non-overlapping occurrences of subsection sub in\n\
1221bytes B[start:end]. Optional arguments start and end are interpreted\n\
1222as in slice notation.");
1223
1224static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001225bytearray_count(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001226{
1227 PyObject *sub_obj;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001228 const char *str = PyByteArray_AS_STRING(self), *sub;
1229 Py_ssize_t sub_len;
1230 char byte;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001231 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001232
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001233 Py_buffer vsub;
1234 PyObject *count_obj;
1235
Antoine Pitrouac65d962011-10-20 23:54:17 +02001236 if (!stringlib_parse_args_finds_byte("count", args, &sub_obj, &byte,
1237 &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001238 return NULL;
1239
Antoine Pitrouac65d962011-10-20 23:54:17 +02001240 if (sub_obj) {
1241 if (_getbuffer(sub_obj, &vsub) < 0)
1242 return NULL;
1243
1244 sub = vsub.buf;
1245 sub_len = vsub.len;
1246 }
1247 else {
1248 sub = &byte;
1249 sub_len = 1;
1250 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001251
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001252 ADJUST_INDICES(start, end, PyByteArray_GET_SIZE(self));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001253
1254 count_obj = PyLong_FromSsize_t(
Antoine Pitrouac65d962011-10-20 23:54:17 +02001255 stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001256 );
Antoine Pitrouac65d962011-10-20 23:54:17 +02001257
1258 if (sub_obj)
1259 PyBuffer_Release(&vsub);
1260
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001261 return count_obj;
1262}
1263
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001264/*[clinic input]
1265bytearray.clear
1266
1267 self: self(type="PyByteArrayObject *")
1268
1269Remove all items from the bytearray.
1270[clinic start generated code]*/
1271
1272PyDoc_STRVAR(bytearray_clear__doc__,
1273"clear($self, /)\n"
1274"--\n"
1275"\n"
1276"Remove all items from the bytearray.");
1277
1278#define BYTEARRAY_CLEAR_METHODDEF \
1279 {"clear", (PyCFunction)bytearray_clear, METH_NOARGS, bytearray_clear__doc__},
Eli Bendersky4db28d32011-03-03 18:21:02 +00001280
Victor Stinner6430fd52011-09-29 04:02:13 +02001281static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001282bytearray_clear_impl(PyByteArrayObject *self);
1283
1284static PyObject *
1285bytearray_clear(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
1286{
1287 return bytearray_clear_impl(self);
1288}
1289
1290static PyObject *
1291bytearray_clear_impl(PyByteArrayObject *self)
1292/*[clinic end generated code: output=5344093031e2f36c input=e524fd330abcdc18]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001293{
1294 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1295 return NULL;
1296 Py_RETURN_NONE;
1297}
1298
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001299/*[clinic input]
1300bytearray.copy
1301
1302 self: self(type="PyByteArrayObject *")
1303
1304Return a copy of B.
1305[clinic start generated code]*/
1306
1307PyDoc_STRVAR(bytearray_copy__doc__,
1308"copy($self, /)\n"
1309"--\n"
1310"\n"
1311"Return a copy of B.");
1312
1313#define BYTEARRAY_COPY_METHODDEF \
1314 {"copy", (PyCFunction)bytearray_copy, METH_NOARGS, bytearray_copy__doc__},
Eli Bendersky4db28d32011-03-03 18:21:02 +00001315
1316static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001317bytearray_copy_impl(PyByteArrayObject *self);
1318
1319static PyObject *
1320bytearray_copy(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
1321{
1322 return bytearray_copy_impl(self);
1323}
1324
1325static PyObject *
1326bytearray_copy_impl(PyByteArrayObject *self)
1327/*[clinic end generated code: output=8788ed299f7f2214 input=6d5d2975aa0f33f3]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001328{
1329 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1330 PyByteArray_GET_SIZE(self));
1331}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001332
1333PyDoc_STRVAR(index__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001334"B.index(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001335\n\
1336Like B.find() but raise ValueError when the subsection is not found.");
1337
1338static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001339bytearray_index(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001340{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001341 Py_ssize_t result = bytearray_find_internal(self, args, +1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001342 if (result == -2)
1343 return NULL;
1344 if (result == -1) {
1345 PyErr_SetString(PyExc_ValueError,
1346 "subsection not found");
1347 return NULL;
1348 }
1349 return PyLong_FromSsize_t(result);
1350}
1351
1352
1353PyDoc_STRVAR(rfind__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001354"B.rfind(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001355\n\
1356Return the highest index in B where subsection sub is found,\n\
Senthil Kumaran53516a82011-07-27 23:33:54 +08001357such that sub is contained within B[start,end]. Optional\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001358arguments start and end are interpreted as in slice notation.\n\
1359\n\
1360Return -1 on failure.");
1361
1362static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001363bytearray_rfind(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001364{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001365 Py_ssize_t result = bytearray_find_internal(self, args, -1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001366 if (result == -2)
1367 return NULL;
1368 return PyLong_FromSsize_t(result);
1369}
1370
1371
1372PyDoc_STRVAR(rindex__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001373"B.rindex(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001374\n\
1375Like B.rfind() but raise ValueError when the subsection is not found.");
1376
1377static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001378bytearray_rindex(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001379{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001380 Py_ssize_t result = bytearray_find_internal(self, args, -1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001381 if (result == -2)
1382 return NULL;
1383 if (result == -1) {
1384 PyErr_SetString(PyExc_ValueError,
1385 "subsection not found");
1386 return NULL;
1387 }
1388 return PyLong_FromSsize_t(result);
1389}
1390
1391
1392static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001393bytearray_contains(PyObject *self, PyObject *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001394{
1395 Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError);
1396 if (ival == -1 && PyErr_Occurred()) {
1397 Py_buffer varg;
Antoine Pitrou0010d372010-08-15 17:12:55 +00001398 Py_ssize_t pos;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001399 PyErr_Clear();
1400 if (_getbuffer(arg, &varg) < 0)
1401 return -1;
1402 pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self),
1403 varg.buf, varg.len, 0);
Martin v. Löwis423be952008-08-13 15:53:07 +00001404 PyBuffer_Release(&varg);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001405 return pos >= 0;
1406 }
1407 if (ival < 0 || ival >= 256) {
1408 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
1409 return -1;
1410 }
1411
Antoine Pitrou0010d372010-08-15 17:12:55 +00001412 return memchr(PyByteArray_AS_STRING(self), (int) ival, Py_SIZE(self)) != NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001413}
1414
1415
1416/* Matches the end (direction >= 0) or start (direction < 0) of self
1417 * against substr, using the start and end arguments. Returns
1418 * -1 on error, 0 if not found and 1 if found.
1419 */
1420Py_LOCAL(int)
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001421_bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001422 Py_ssize_t end, int direction)
1423{
1424 Py_ssize_t len = PyByteArray_GET_SIZE(self);
1425 const char* str;
1426 Py_buffer vsubstr;
1427 int rv = 0;
1428
1429 str = PyByteArray_AS_STRING(self);
1430
1431 if (_getbuffer(substr, &vsubstr) < 0)
1432 return -1;
1433
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001434 ADJUST_INDICES(start, end, len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001435
1436 if (direction < 0) {
1437 /* startswith */
1438 if (start+vsubstr.len > len) {
1439 goto done;
1440 }
1441 } else {
1442 /* endswith */
1443 if (end-start < vsubstr.len || start > len) {
1444 goto done;
1445 }
1446
1447 if (end-vsubstr.len > start)
1448 start = end - vsubstr.len;
1449 }
1450 if (end-start >= vsubstr.len)
1451 rv = ! memcmp(str+start, vsubstr.buf, vsubstr.len);
1452
1453done:
Martin v. Löwis423be952008-08-13 15:53:07 +00001454 PyBuffer_Release(&vsubstr);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001455 return rv;
1456}
1457
1458
1459PyDoc_STRVAR(startswith__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001460"B.startswith(prefix[, start[, end]]) -> bool\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001461\n\
1462Return True if B starts with the specified prefix, False otherwise.\n\
1463With optional start, test B beginning at that position.\n\
1464With optional end, stop comparing B at that position.\n\
Ezio Melottiba42fd52011-04-26 06:09:45 +03001465prefix can also be a tuple of bytes to try.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001466
1467static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001468bytearray_startswith(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001469{
1470 Py_ssize_t start = 0;
1471 Py_ssize_t end = PY_SSIZE_T_MAX;
1472 PyObject *subobj;
1473 int result;
1474
Jesus Ceaac451502011-04-20 17:09:23 +02001475 if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001476 return NULL;
1477 if (PyTuple_Check(subobj)) {
1478 Py_ssize_t i;
1479 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001480 result = _bytearray_tailmatch(self,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001481 PyTuple_GET_ITEM(subobj, i),
1482 start, end, -1);
1483 if (result == -1)
1484 return NULL;
1485 else if (result) {
1486 Py_RETURN_TRUE;
1487 }
1488 }
1489 Py_RETURN_FALSE;
1490 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001491 result = _bytearray_tailmatch(self, subobj, start, end, -1);
Ezio Melottiba42fd52011-04-26 06:09:45 +03001492 if (result == -1) {
1493 if (PyErr_ExceptionMatches(PyExc_TypeError))
1494 PyErr_Format(PyExc_TypeError, "startswith first arg must be bytes "
1495 "or a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001496 return NULL;
Ezio Melottiba42fd52011-04-26 06:09:45 +03001497 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001498 else
1499 return PyBool_FromLong(result);
1500}
1501
1502PyDoc_STRVAR(endswith__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001503"B.endswith(suffix[, start[, end]]) -> bool\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001504\n\
1505Return True if B ends with the specified suffix, False otherwise.\n\
1506With optional start, test B beginning at that position.\n\
1507With optional end, stop comparing B at that position.\n\
Ezio Melottiba42fd52011-04-26 06:09:45 +03001508suffix can also be a tuple of bytes to try.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001509
1510static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001511bytearray_endswith(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001512{
1513 Py_ssize_t start = 0;
1514 Py_ssize_t end = PY_SSIZE_T_MAX;
1515 PyObject *subobj;
1516 int result;
1517
Jesus Ceaac451502011-04-20 17:09:23 +02001518 if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001519 return NULL;
1520 if (PyTuple_Check(subobj)) {
1521 Py_ssize_t i;
1522 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001523 result = _bytearray_tailmatch(self,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001524 PyTuple_GET_ITEM(subobj, i),
1525 start, end, +1);
1526 if (result == -1)
1527 return NULL;
1528 else if (result) {
1529 Py_RETURN_TRUE;
1530 }
1531 }
1532 Py_RETURN_FALSE;
1533 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001534 result = _bytearray_tailmatch(self, subobj, start, end, +1);
Ezio Melottiba42fd52011-04-26 06:09:45 +03001535 if (result == -1) {
1536 if (PyErr_ExceptionMatches(PyExc_TypeError))
1537 PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or "
1538 "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001539 return NULL;
Ezio Melottiba42fd52011-04-26 06:09:45 +03001540 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001541 else
1542 return PyBool_FromLong(result);
1543}
1544
1545
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001546/*[clinic input]
1547bytearray.translate
1548
1549 self: self(type="PyByteArrayObject *")
1550 table: object
1551 Translation table, which must be a bytes object of length 256.
1552 [
1553 deletechars: object
1554 ]
1555 /
1556
1557Return a copy with each character mapped by the given translation table.
1558
1559All characters occurring in the optional argument deletechars are removed.
1560The remaining characters are mapped through the given translation table.
1561[clinic start generated code]*/
1562
1563PyDoc_STRVAR(bytearray_translate__doc__,
1564"translate(table, [deletechars])\n"
1565"Return a copy with each character mapped by the given translation table.\n"
1566"\n"
1567" table\n"
1568" Translation table, which must be a bytes object of length 256.\n"
1569"\n"
1570"All characters occurring in the optional argument deletechars are removed.\n"
1571"The remaining characters are mapped through the given translation table.");
1572
1573#define BYTEARRAY_TRANSLATE_METHODDEF \
1574 {"translate", (PyCFunction)bytearray_translate, METH_VARARGS, bytearray_translate__doc__},
1575
1576static PyObject *
1577bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, int group_right_1, PyObject *deletechars);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001578
1579static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001580bytearray_translate(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001581{
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001582 PyObject *return_value = NULL;
1583 PyObject *table;
1584 int group_right_1 = 0;
1585 PyObject *deletechars = NULL;
1586
1587 switch (PyTuple_GET_SIZE(args)) {
1588 case 1:
1589 if (!PyArg_ParseTuple(args, "O:translate", &table))
1590 goto exit;
1591 break;
1592 case 2:
1593 if (!PyArg_ParseTuple(args, "OO:translate", &table, &deletechars))
1594 goto exit;
1595 group_right_1 = 1;
1596 break;
1597 default:
1598 PyErr_SetString(PyExc_TypeError, "bytearray.translate requires 1 to 2 arguments");
1599 goto exit;
1600 }
1601 return_value = bytearray_translate_impl(self, table, group_right_1, deletechars);
1602
1603exit:
1604 return return_value;
1605}
1606
1607static PyObject *
1608bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, int group_right_1, PyObject *deletechars)
1609/*[clinic end generated code: output=a709df81d41db4b7 input=b749ad85f4860824]*/
1610{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001611 char *input, *output;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001612 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001613 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001614 PyObject *input_obj = (PyObject*)self;
1615 const char *output_start;
1616 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001617 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001618 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001619 Py_buffer vtable, vdel;
1620
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001621 if (table == Py_None) {
1622 table_chars = NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001623 table = NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001624 } else if (_getbuffer(table, &vtable) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001625 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001626 } else {
1627 if (vtable.len != 256) {
1628 PyErr_SetString(PyExc_ValueError,
1629 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001630 PyBuffer_Release(&vtable);
1631 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001632 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001633 table_chars = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001634 }
1635
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001636 if (deletechars != NULL) {
1637 if (_getbuffer(deletechars, &vdel) < 0) {
1638 if (table != NULL)
Georg Brandl953152f2009-07-22 12:03:59 +00001639 PyBuffer_Release(&vtable);
1640 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001641 }
1642 }
1643 else {
1644 vdel.buf = NULL;
1645 vdel.len = 0;
1646 }
1647
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001648 inlen = PyByteArray_GET_SIZE(input_obj);
1649 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1650 if (result == NULL)
1651 goto done;
1652 output_start = output = PyByteArray_AsString(result);
1653 input = PyByteArray_AS_STRING(input_obj);
1654
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001655 if (vdel.len == 0 && table_chars != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001656 /* If no deletions are required, use faster code */
1657 for (i = inlen; --i >= 0; ) {
1658 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001659 *output++ = table_chars[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001660 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001661 goto done;
1662 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001663
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001664 if (table_chars == NULL) {
Georg Brandlccc47b62008-12-28 11:44:14 +00001665 for (i = 0; i < 256; i++)
1666 trans_table[i] = Py_CHARMASK(i);
1667 } else {
1668 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001669 trans_table[i] = Py_CHARMASK(table_chars[i]);
Georg Brandlccc47b62008-12-28 11:44:14 +00001670 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001671
1672 for (i = 0; i < vdel.len; i++)
1673 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1674
1675 for (i = inlen; --i >= 0; ) {
1676 c = Py_CHARMASK(*input++);
1677 if (trans_table[c] != -1)
1678 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1679 continue;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001680 }
1681 /* Fix the size of the resulting string */
1682 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001683 if (PyByteArray_Resize(result, output - output_start) < 0) {
1684 Py_CLEAR(result);
1685 goto done;
1686 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001687
1688done:
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001689 if (table != NULL)
Georg Brandlccc47b62008-12-28 11:44:14 +00001690 PyBuffer_Release(&vtable);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001691 if (deletechars != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001692 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001693 return result;
1694}
1695
1696
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001697/*[clinic input]
1698
1699@staticmethod
1700bytearray.maketrans
1701
1702 frm: object
1703 to: object
1704 /
1705
1706Return a translation table useable for the bytes or bytearray translate method.
1707
1708The returned table will be one where each byte in frm is mapped to the byte at
1709the same position in to.
1710
1711The bytes objects frm and to must be of the same length.
1712[clinic start generated code]*/
1713
1714PyDoc_STRVAR(bytearray_maketrans__doc__,
1715"maketrans(frm, to, /)\n"
1716"--\n"
1717"\n"
1718"Return a translation table useable for the bytes or bytearray translate method.\n"
1719"\n"
1720"The returned table will be one where each byte in frm is mapped to the byte at\n"
1721"the same position in to.\n"
1722"\n"
1723"The bytes objects frm and to must be of the same length.");
1724
1725#define BYTEARRAY_MAKETRANS_METHODDEF \
1726 {"maketrans", (PyCFunction)bytearray_maketrans, METH_VARARGS|METH_STATIC, bytearray_maketrans__doc__},
1727
Georg Brandlabc38772009-04-12 15:51:51 +00001728static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001729bytearray_maketrans_impl(PyObject *frm, PyObject *to);
1730
1731static PyObject *
1732bytearray_maketrans(void *null, PyObject *args)
Georg Brandlabc38772009-04-12 15:51:51 +00001733{
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001734 PyObject *return_value = NULL;
1735 PyObject *frm;
1736 PyObject *to;
1737
1738 if (!PyArg_UnpackTuple(args, "maketrans",
1739 2, 2,
1740 &frm, &to))
1741 goto exit;
1742 return_value = bytearray_maketrans_impl(frm, to);
1743
1744exit:
1745 return return_value;
1746}
1747
1748static PyObject *
1749bytearray_maketrans_impl(PyObject *frm, PyObject *to)
1750/*[clinic end generated code: output=307752019d9b25b5 input=ea9bdc6b328c15e2]*/
1751{
1752 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00001753}
1754
1755
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001756/* find and count characters and substrings */
1757
1758#define findchar(target, target_len, c) \
1759 ((char *)memchr((const void *)(target), c, target_len))
1760
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001761
Benjamin Peterson0f3641c2008-11-19 22:05:52 +00001762/* Bytes ops must return a string, create a copy */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001763Py_LOCAL(PyByteArrayObject *)
1764return_self(PyByteArrayObject *self)
1765{
Georg Brandl1e7217d2008-05-30 12:02:38 +00001766 /* always return a new bytearray */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001767 return (PyByteArrayObject *)PyByteArray_FromStringAndSize(
1768 PyByteArray_AS_STRING(self),
1769 PyByteArray_GET_SIZE(self));
1770}
1771
1772Py_LOCAL_INLINE(Py_ssize_t)
1773countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount)
1774{
1775 Py_ssize_t count=0;
1776 const char *start=target;
1777 const char *end=target+target_len;
1778
1779 while ( (start=findchar(start, end-start, c)) != NULL ) {
1780 count++;
1781 if (count >= maxcount)
1782 break;
1783 start += 1;
1784 }
1785 return count;
1786}
1787
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001788
1789/* Algorithms for different cases of string replacement */
1790
1791/* len(self)>=1, from="", len(to)>=1, maxcount>=1 */
1792Py_LOCAL(PyByteArrayObject *)
1793replace_interleave(PyByteArrayObject *self,
1794 const char *to_s, Py_ssize_t to_len,
1795 Py_ssize_t maxcount)
1796{
1797 char *self_s, *result_s;
1798 Py_ssize_t self_len, result_len;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001799 Py_ssize_t count, i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001800 PyByteArrayObject *result;
1801
1802 self_len = PyByteArray_GET_SIZE(self);
1803
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001804 /* 1 at the end plus 1 after every character;
1805 count = min(maxcount, self_len + 1) */
1806 if (maxcount <= self_len)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001807 count = maxcount;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001808 else
1809 /* Can't overflow: self_len + 1 <= maxcount <= PY_SSIZE_T_MAX. */
1810 count = self_len + 1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001811
1812 /* Check for overflow */
1813 /* result_len = count * to_len + self_len; */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001814 assert(count > 0);
1815 if (to_len > (PY_SSIZE_T_MAX - self_len) / count) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001816 PyErr_SetString(PyExc_OverflowError,
1817 "replace string is too long");
1818 return NULL;
1819 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001820 result_len = count * to_len + self_len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001821
1822 if (! (result = (PyByteArrayObject *)
1823 PyByteArray_FromStringAndSize(NULL, result_len)) )
1824 return NULL;
1825
1826 self_s = PyByteArray_AS_STRING(self);
1827 result_s = PyByteArray_AS_STRING(result);
1828
1829 /* TODO: special case single character, which doesn't need memcpy */
1830
1831 /* Lay the first one down (guaranteed this will occur) */
1832 Py_MEMCPY(result_s, to_s, to_len);
1833 result_s += to_len;
1834 count -= 1;
1835
1836 for (i=0; i<count; i++) {
1837 *result_s++ = *self_s++;
1838 Py_MEMCPY(result_s, to_s, to_len);
1839 result_s += to_len;
1840 }
1841
1842 /* Copy the rest of the original string */
1843 Py_MEMCPY(result_s, self_s, self_len-i);
1844
1845 return result;
1846}
1847
1848/* Special case for deleting a single character */
1849/* len(self)>=1, len(from)==1, to="", maxcount>=1 */
1850Py_LOCAL(PyByteArrayObject *)
1851replace_delete_single_character(PyByteArrayObject *self,
1852 char from_c, Py_ssize_t maxcount)
1853{
1854 char *self_s, *result_s;
1855 char *start, *next, *end;
1856 Py_ssize_t self_len, result_len;
1857 Py_ssize_t count;
1858 PyByteArrayObject *result;
1859
1860 self_len = PyByteArray_GET_SIZE(self);
1861 self_s = PyByteArray_AS_STRING(self);
1862
1863 count = countchar(self_s, self_len, from_c, maxcount);
1864 if (count == 0) {
1865 return return_self(self);
1866 }
1867
1868 result_len = self_len - count; /* from_len == 1 */
1869 assert(result_len>=0);
1870
1871 if ( (result = (PyByteArrayObject *)
1872 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1873 return NULL;
1874 result_s = PyByteArray_AS_STRING(result);
1875
1876 start = self_s;
1877 end = self_s + self_len;
1878 while (count-- > 0) {
1879 next = findchar(start, end-start, from_c);
1880 if (next == NULL)
1881 break;
1882 Py_MEMCPY(result_s, start, next-start);
1883 result_s += (next-start);
1884 start = next+1;
1885 }
1886 Py_MEMCPY(result_s, start, end-start);
1887
1888 return result;
1889}
1890
1891/* len(self)>=1, len(from)>=2, to="", maxcount>=1 */
1892
1893Py_LOCAL(PyByteArrayObject *)
1894replace_delete_substring(PyByteArrayObject *self,
1895 const char *from_s, Py_ssize_t from_len,
1896 Py_ssize_t maxcount)
1897{
1898 char *self_s, *result_s;
1899 char *start, *next, *end;
1900 Py_ssize_t self_len, result_len;
1901 Py_ssize_t count, offset;
1902 PyByteArrayObject *result;
1903
1904 self_len = PyByteArray_GET_SIZE(self);
1905 self_s = PyByteArray_AS_STRING(self);
1906
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001907 count = stringlib_count(self_s, self_len,
1908 from_s, from_len,
1909 maxcount);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001910
1911 if (count == 0) {
1912 /* no matches */
1913 return return_self(self);
1914 }
1915
1916 result_len = self_len - (count * from_len);
1917 assert (result_len>=0);
1918
1919 if ( (result = (PyByteArrayObject *)
1920 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL )
1921 return NULL;
1922
1923 result_s = PyByteArray_AS_STRING(result);
1924
1925 start = self_s;
1926 end = self_s + self_len;
1927 while (count-- > 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001928 offset = stringlib_find(start, end-start,
1929 from_s, from_len,
1930 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001931 if (offset == -1)
1932 break;
1933 next = start + offset;
1934
1935 Py_MEMCPY(result_s, start, next-start);
1936
1937 result_s += (next-start);
1938 start = next+from_len;
1939 }
1940 Py_MEMCPY(result_s, start, end-start);
1941 return result;
1942}
1943
1944/* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */
1945Py_LOCAL(PyByteArrayObject *)
1946replace_single_character_in_place(PyByteArrayObject *self,
1947 char from_c, char to_c,
1948 Py_ssize_t maxcount)
1949{
Antoine Pitroud1188562010-06-09 16:38:55 +00001950 char *self_s, *result_s, *start, *end, *next;
1951 Py_ssize_t self_len;
1952 PyByteArrayObject *result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001953
Antoine Pitroud1188562010-06-09 16:38:55 +00001954 /* The result string will be the same size */
1955 self_s = PyByteArray_AS_STRING(self);
1956 self_len = PyByteArray_GET_SIZE(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001957
Antoine Pitroud1188562010-06-09 16:38:55 +00001958 next = findchar(self_s, self_len, from_c);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001959
Antoine Pitroud1188562010-06-09 16:38:55 +00001960 if (next == NULL) {
1961 /* No matches; return the original bytes */
1962 return return_self(self);
1963 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001964
Antoine Pitroud1188562010-06-09 16:38:55 +00001965 /* Need to make a new bytes */
1966 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len);
1967 if (result == NULL)
1968 return NULL;
1969 result_s = PyByteArray_AS_STRING(result);
1970 Py_MEMCPY(result_s, self_s, self_len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001971
Antoine Pitroud1188562010-06-09 16:38:55 +00001972 /* change everything in-place, starting with this one */
1973 start = result_s + (next-self_s);
1974 *start = to_c;
1975 start++;
1976 end = result_s + self_len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001977
Antoine Pitroud1188562010-06-09 16:38:55 +00001978 while (--maxcount > 0) {
1979 next = findchar(start, end-start, from_c);
1980 if (next == NULL)
1981 break;
1982 *next = to_c;
1983 start = next+1;
1984 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001985
Antoine Pitroud1188562010-06-09 16:38:55 +00001986 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001987}
1988
1989/* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */
1990Py_LOCAL(PyByteArrayObject *)
1991replace_substring_in_place(PyByteArrayObject *self,
1992 const char *from_s, Py_ssize_t from_len,
1993 const char *to_s, Py_ssize_t to_len,
1994 Py_ssize_t maxcount)
1995{
1996 char *result_s, *start, *end;
1997 char *self_s;
1998 Py_ssize_t self_len, offset;
1999 PyByteArrayObject *result;
2000
2001 /* The result bytes will be the same size */
2002
2003 self_s = PyByteArray_AS_STRING(self);
2004 self_len = PyByteArray_GET_SIZE(self);
2005
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002006 offset = stringlib_find(self_s, self_len,
2007 from_s, from_len,
2008 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002009 if (offset == -1) {
2010 /* No matches; return the original bytes */
2011 return return_self(self);
2012 }
2013
2014 /* Need to make a new bytes */
2015 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len);
2016 if (result == NULL)
2017 return NULL;
2018 result_s = PyByteArray_AS_STRING(result);
2019 Py_MEMCPY(result_s, self_s, self_len);
2020
2021 /* change everything in-place, starting with this one */
2022 start = result_s + offset;
2023 Py_MEMCPY(start, to_s, from_len);
2024 start += from_len;
2025 end = result_s + self_len;
2026
2027 while ( --maxcount > 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002028 offset = stringlib_find(start, end-start,
2029 from_s, from_len,
2030 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002031 if (offset==-1)
2032 break;
2033 Py_MEMCPY(start+offset, to_s, from_len);
2034 start += offset+from_len;
2035 }
2036
2037 return result;
2038}
2039
2040/* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */
2041Py_LOCAL(PyByteArrayObject *)
2042replace_single_character(PyByteArrayObject *self,
2043 char from_c,
2044 const char *to_s, Py_ssize_t to_len,
2045 Py_ssize_t maxcount)
2046{
2047 char *self_s, *result_s;
2048 char *start, *next, *end;
2049 Py_ssize_t self_len, result_len;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00002050 Py_ssize_t count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002051 PyByteArrayObject *result;
2052
2053 self_s = PyByteArray_AS_STRING(self);
2054 self_len = PyByteArray_GET_SIZE(self);
2055
2056 count = countchar(self_s, self_len, from_c, maxcount);
2057 if (count == 0) {
2058 /* no matches, return unchanged */
2059 return return_self(self);
2060 }
2061
2062 /* use the difference between current and new, hence the "-1" */
2063 /* result_len = self_len + count * (to_len-1) */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00002064 assert(count > 0);
2065 if (to_len - 1 > (PY_SSIZE_T_MAX - self_len) / count) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002066 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
2067 return NULL;
2068 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00002069 result_len = self_len + count * (to_len - 1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002070
2071 if ( (result = (PyByteArrayObject *)
2072 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
2073 return NULL;
2074 result_s = PyByteArray_AS_STRING(result);
2075
2076 start = self_s;
2077 end = self_s + self_len;
2078 while (count-- > 0) {
2079 next = findchar(start, end-start, from_c);
2080 if (next == NULL)
2081 break;
2082
2083 if (next == start) {
2084 /* replace with the 'to' */
2085 Py_MEMCPY(result_s, to_s, to_len);
2086 result_s += to_len;
2087 start += 1;
2088 } else {
2089 /* copy the unchanged old then the 'to' */
2090 Py_MEMCPY(result_s, start, next-start);
2091 result_s += (next-start);
2092 Py_MEMCPY(result_s, to_s, to_len);
2093 result_s += to_len;
2094 start = next+1;
2095 }
2096 }
2097 /* Copy the remainder of the remaining bytes */
2098 Py_MEMCPY(result_s, start, end-start);
2099
2100 return result;
2101}
2102
2103/* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */
2104Py_LOCAL(PyByteArrayObject *)
2105replace_substring(PyByteArrayObject *self,
2106 const char *from_s, Py_ssize_t from_len,
2107 const char *to_s, Py_ssize_t to_len,
2108 Py_ssize_t maxcount)
2109{
2110 char *self_s, *result_s;
2111 char *start, *next, *end;
2112 Py_ssize_t self_len, result_len;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00002113 Py_ssize_t count, offset;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002114 PyByteArrayObject *result;
2115
2116 self_s = PyByteArray_AS_STRING(self);
2117 self_len = PyByteArray_GET_SIZE(self);
2118
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002119 count = stringlib_count(self_s, self_len,
2120 from_s, from_len,
2121 maxcount);
2122
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002123 if (count == 0) {
2124 /* no matches, return unchanged */
2125 return return_self(self);
2126 }
2127
2128 /* Check for overflow */
2129 /* result_len = self_len + count * (to_len-from_len) */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00002130 assert(count > 0);
2131 if (to_len - from_len > (PY_SSIZE_T_MAX - self_len) / count) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002132 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
2133 return NULL;
2134 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00002135 result_len = self_len + count * (to_len - from_len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002136
2137 if ( (result = (PyByteArrayObject *)
2138 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
2139 return NULL;
2140 result_s = PyByteArray_AS_STRING(result);
2141
2142 start = self_s;
2143 end = self_s + self_len;
2144 while (count-- > 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002145 offset = stringlib_find(start, end-start,
2146 from_s, from_len,
2147 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002148 if (offset == -1)
2149 break;
2150 next = start+offset;
2151 if (next == start) {
2152 /* replace with the 'to' */
2153 Py_MEMCPY(result_s, to_s, to_len);
2154 result_s += to_len;
2155 start += from_len;
2156 } else {
2157 /* copy the unchanged old then the 'to' */
2158 Py_MEMCPY(result_s, start, next-start);
2159 result_s += (next-start);
2160 Py_MEMCPY(result_s, to_s, to_len);
2161 result_s += to_len;
2162 start = next+from_len;
2163 }
2164 }
2165 /* Copy the remainder of the remaining bytes */
2166 Py_MEMCPY(result_s, start, end-start);
2167
2168 return result;
2169}
2170
2171
2172Py_LOCAL(PyByteArrayObject *)
2173replace(PyByteArrayObject *self,
2174 const char *from_s, Py_ssize_t from_len,
2175 const char *to_s, Py_ssize_t to_len,
2176 Py_ssize_t maxcount)
2177{
2178 if (maxcount < 0) {
2179 maxcount = PY_SSIZE_T_MAX;
2180 } else if (maxcount == 0 || PyByteArray_GET_SIZE(self) == 0) {
2181 /* nothing to do; return the original bytes */
2182 return return_self(self);
2183 }
2184
2185 if (maxcount == 0 ||
2186 (from_len == 0 && to_len == 0)) {
2187 /* nothing to do; return the original bytes */
2188 return return_self(self);
2189 }
2190
2191 /* Handle zero-length special cases */
2192
2193 if (from_len == 0) {
2194 /* insert the 'to' bytes everywhere. */
2195 /* >>> "Python".replace("", ".") */
2196 /* '.P.y.t.h.o.n.' */
2197 return replace_interleave(self, to_s, to_len, maxcount);
2198 }
2199
2200 /* Except for "".replace("", "A") == "A" there is no way beyond this */
2201 /* point for an empty self bytes to generate a non-empty bytes */
2202 /* Special case so the remaining code always gets a non-empty bytes */
2203 if (PyByteArray_GET_SIZE(self) == 0) {
2204 return return_self(self);
2205 }
2206
2207 if (to_len == 0) {
Georg Brandl17cb8a82008-05-30 08:20:09 +00002208 /* delete all occurrences of 'from' bytes */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002209 if (from_len == 1) {
2210 return replace_delete_single_character(
2211 self, from_s[0], maxcount);
2212 } else {
2213 return replace_delete_substring(self, from_s, from_len, maxcount);
2214 }
2215 }
2216
2217 /* Handle special case where both bytes have the same length */
2218
2219 if (from_len == to_len) {
2220 if (from_len == 1) {
2221 return replace_single_character_in_place(
2222 self,
2223 from_s[0],
2224 to_s[0],
2225 maxcount);
2226 } else {
2227 return replace_substring_in_place(
2228 self, from_s, from_len, to_s, to_len, maxcount);
2229 }
2230 }
2231
2232 /* Otherwise use the more generic algorithms */
2233 if (from_len == 1) {
2234 return replace_single_character(self, from_s[0],
2235 to_s, to_len, maxcount);
2236 } else {
2237 /* len('from')>=2, len('to')>=1 */
2238 return replace_substring(self, from_s, from_len, to_s, to_len, maxcount);
2239 }
2240}
2241
2242
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002243/*[clinic input]
2244bytearray.replace
2245
2246 old: object
2247 new: object
2248 count: Py_ssize_t = -1
2249 Maximum number of occurrences to replace.
2250 -1 (the default value) means replace all occurrences.
2251 /
2252
2253Return a copy with all occurrences of substring old replaced by new.
2254
2255If the optional argument count is given, only the first count occurrences are
2256replaced.
2257[clinic start generated code]*/
2258
2259PyDoc_STRVAR(bytearray_replace__doc__,
2260"replace($self, old, new, count=-1, /)\n"
2261"--\n"
2262"\n"
2263"Return a copy with all occurrences of substring old replaced by new.\n"
2264"\n"
2265" count\n"
2266" Maximum number of occurrences to replace.\n"
2267" -1 (the default value) means replace all occurrences.\n"
2268"\n"
2269"If the optional argument count is given, only the first count occurrences are\n"
2270"replaced.");
2271
2272#define BYTEARRAY_REPLACE_METHODDEF \
2273 {"replace", (PyCFunction)bytearray_replace, METH_VARARGS, bytearray_replace__doc__},
2274
2275static PyObject *
2276bytearray_replace_impl(PyByteArrayObject *self, PyObject *old, PyObject *new, Py_ssize_t count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002277
2278static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002279bytearray_replace(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002280{
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002281 PyObject *return_value = NULL;
2282 PyObject *old;
2283 PyObject *new;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002284 Py_ssize_t count = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002285
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002286 if (!PyArg_ParseTuple(args,
2287 "OO|n:replace",
2288 &old, &new, &count))
2289 goto exit;
2290 return_value = bytearray_replace_impl(self, old, new, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002291
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002292exit:
2293 return return_value;
2294}
2295
2296static PyObject *
2297bytearray_replace_impl(PyByteArrayObject *self, PyObject *old, PyObject *new, Py_ssize_t count)
2298/*[clinic end generated code: output=4d2e3c9130da0f96 input=9aaaa123608dfc1f]*/
2299{
2300 PyObject *res;
2301 Py_buffer vold, vnew;
2302
2303 if (_getbuffer(old, &vold) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002304 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002305 if (_getbuffer(new, &vnew) < 0) {
2306 PyBuffer_Release(&vold);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002307 return NULL;
2308 }
2309
2310 res = (PyObject *)replace((PyByteArrayObject *) self,
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002311 vold.buf, vold.len,
2312 vnew.buf, vnew.len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002313
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002314 PyBuffer_Release(&vold);
2315 PyBuffer_Release(&vnew);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002316 return res;
2317}
2318
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002319/*[clinic input]
2320bytearray.split
2321
2322 sep: object = None
2323 The delimiter according which to split the bytearray.
2324 None (the default value) means split on ASCII whitespace characters
2325 (space, tab, return, newline, formfeed, vertical tab).
2326 maxsplit: Py_ssize_t = -1
2327 Maximum number of splits to do.
2328 -1 (the default value) means no limit.
2329
2330Return a list of the sections in the bytearray, using sep as the delimiter.
2331[clinic start generated code]*/
2332
2333PyDoc_STRVAR(bytearray_split__doc__,
2334"split($self, /, sep=None, maxsplit=-1)\n"
2335"--\n"
2336"\n"
2337"Return a list of the sections in the bytearray, using sep as the delimiter.\n"
2338"\n"
2339" sep\n"
2340" The delimiter according which to split the bytearray.\n"
2341" None (the default value) means split on ASCII whitespace characters\n"
2342" (space, tab, return, newline, formfeed, vertical tab).\n"
2343" maxsplit\n"
2344" Maximum number of splits to do.\n"
2345" -1 (the default value) means no limit.");
2346
2347#define BYTEARRAY_SPLIT_METHODDEF \
2348 {"split", (PyCFunction)bytearray_split, METH_VARARGS|METH_KEYWORDS, bytearray_split__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002349
2350static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002351bytearray_split_impl(PyByteArrayObject *self, PyObject *sep, Py_ssize_t maxsplit);
2352
2353static PyObject *
2354bytearray_split(PyByteArrayObject *self, PyObject *args, PyObject *kwargs)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002355{
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002356 PyObject *return_value = NULL;
2357 static char *_keywords[] = {"sep", "maxsplit", NULL};
2358 PyObject *sep = Py_None;
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002359 Py_ssize_t maxsplit = -1;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002360
2361 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
2362 "|On:split", _keywords,
2363 &sep, &maxsplit))
2364 goto exit;
2365 return_value = bytearray_split_impl(self, sep, maxsplit);
2366
2367exit:
2368 return return_value;
2369}
2370
2371static PyObject *
2372bytearray_split_impl(PyByteArrayObject *self, PyObject *sep, Py_ssize_t maxsplit)
2373/*[clinic end generated code: output=062a3d87d6f918fa input=24f82669f41bf523]*/
2374{
2375 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002376 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002377 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002378 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002379
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002380 if (maxsplit < 0)
2381 maxsplit = PY_SSIZE_T_MAX;
2382
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002383 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002384 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002385
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002386 if (_getbuffer(sep, &vsub) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002387 return NULL;
2388 sub = vsub.buf;
2389 n = vsub.len;
2390
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002391 list = stringlib_split(
2392 (PyObject*) self, s, len, sub, n, maxsplit
2393 );
Martin v. Löwis423be952008-08-13 15:53:07 +00002394 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002395 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002396}
2397
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002398/*[clinic input]
2399bytearray.partition
2400
2401 self: self(type="PyByteArrayObject *")
2402 sep: object
2403 /
2404
2405Partition the bytearray into three parts using the given separator.
2406
2407This will search for the separator sep in the bytearray. If the separator is
2408found, returns a 3-tuple containing the part before the separator, the
2409separator itself, and the part after it.
2410
2411If the separator is not found, returns a 3-tuple containing the original
2412bytearray object and two empty bytearray objects.
2413[clinic start generated code]*/
2414
2415PyDoc_STRVAR(bytearray_partition__doc__,
2416"partition($self, sep, /)\n"
2417"--\n"
2418"\n"
2419"Partition the bytearray into three parts using the given separator.\n"
2420"\n"
2421"This will search for the separator sep in the bytearray. If the separator is\n"
2422"found, returns a 3-tuple containing the part before the separator, the\n"
2423"separator itself, and the part after it.\n"
2424"\n"
2425"If the separator is not found, returns a 3-tuple containing the original\n"
2426"bytearray object and two empty bytearray objects.");
2427
2428#define BYTEARRAY_PARTITION_METHODDEF \
2429 {"partition", (PyCFunction)bytearray_partition, METH_O, bytearray_partition__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002430
2431static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002432bytearray_partition(PyByteArrayObject *self, PyObject *sep)
2433/*[clinic end generated code: output=2645138221fe6f4d input=7d7fe37b1696d506]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002434{
2435 PyObject *bytesep, *result;
2436
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002437 bytesep = PyByteArray_FromObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002438 if (! bytesep)
2439 return NULL;
2440
2441 result = stringlib_partition(
2442 (PyObject*) self,
2443 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2444 bytesep,
2445 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2446 );
2447
2448 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002449 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002450}
2451
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002452/*[clinic input]
2453bytearray.rpartition
2454
2455 self: self(type="PyByteArrayObject *")
2456 sep: object
2457 /
2458
2459Partition the bytes into three parts using the given separator.
2460
2461This will search for the separator sep in the bytearray, starting and the end.
2462If the separator is found, returns a 3-tuple containing the part before the
2463separator, the separator itself, and the part after it.
2464
2465If the separator is not found, returns a 3-tuple containing two empty bytearray
2466objects and the original bytearray object.
2467[clinic start generated code]*/
2468
2469PyDoc_STRVAR(bytearray_rpartition__doc__,
2470"rpartition($self, sep, /)\n"
2471"--\n"
2472"\n"
2473"Partition the bytes into three parts using the given separator.\n"
2474"\n"
2475"This will search for the separator sep in the bytearray, starting and the end.\n"
2476"If the separator is found, returns a 3-tuple containing the part before the\n"
2477"separator, the separator itself, and the part after it.\n"
2478"\n"
2479"If the separator is not found, returns a 3-tuple containing two empty bytearray\n"
2480"objects and the original bytearray object.");
2481
2482#define BYTEARRAY_RPARTITION_METHODDEF \
2483 {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, bytearray_rpartition__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002484
2485static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002486bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
2487/*[clinic end generated code: output=ed13e54605d007de input=9b8cd540c1b75853]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002488{
2489 PyObject *bytesep, *result;
2490
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002491 bytesep = PyByteArray_FromObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002492 if (! bytesep)
2493 return NULL;
2494
2495 result = stringlib_rpartition(
2496 (PyObject*) self,
2497 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2498 bytesep,
2499 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2500 );
2501
2502 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002503 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002504}
2505
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002506/*[clinic input]
2507bytearray.rsplit = bytearray.split
2508
2509Return a list of the sections in the bytearray, using sep as the delimiter.
2510
2511Splitting is done starting at the end of the bytearray and working to the front.
2512[clinic start generated code]*/
2513
2514PyDoc_STRVAR(bytearray_rsplit__doc__,
2515"rsplit($self, /, sep=None, maxsplit=-1)\n"
2516"--\n"
2517"\n"
2518"Return a list of the sections in the bytearray, using sep as the delimiter.\n"
2519"\n"
2520" sep\n"
2521" The delimiter according which to split the bytearray.\n"
2522" None (the default value) means split on ASCII whitespace characters\n"
2523" (space, tab, return, newline, formfeed, vertical tab).\n"
2524" maxsplit\n"
2525" Maximum number of splits to do.\n"
2526" -1 (the default value) means no limit.\n"
2527"\n"
2528"Splitting is done starting at the end of the bytearray and working to the front.");
2529
2530#define BYTEARRAY_RSPLIT_METHODDEF \
2531 {"rsplit", (PyCFunction)bytearray_rsplit, METH_VARARGS|METH_KEYWORDS, bytearray_rsplit__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002532
2533static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002534bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep, Py_ssize_t maxsplit);
2535
2536static PyObject *
2537bytearray_rsplit(PyByteArrayObject *self, PyObject *args, PyObject *kwargs)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002538{
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002539 PyObject *return_value = NULL;
2540 static char *_keywords[] = {"sep", "maxsplit", NULL};
2541 PyObject *sep = Py_None;
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002542 Py_ssize_t maxsplit = -1;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002543
2544 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
2545 "|On:rsplit", _keywords,
2546 &sep, &maxsplit))
2547 goto exit;
2548 return_value = bytearray_rsplit_impl(self, sep, maxsplit);
2549
2550exit:
2551 return return_value;
2552}
2553
2554static PyObject *
2555bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep, Py_ssize_t maxsplit)
2556/*[clinic end generated code: output=affaf9fc2aae8d41 input=a68286e4dd692ffe]*/
2557{
2558 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002559 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002560 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002561 Py_buffer vsub;
2562
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002563 if (maxsplit < 0)
2564 maxsplit = PY_SSIZE_T_MAX;
2565
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002566 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002567 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002568
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002569 if (_getbuffer(sep, &vsub) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002570 return NULL;
2571 sub = vsub.buf;
2572 n = vsub.len;
2573
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002574 list = stringlib_rsplit(
2575 (PyObject*) self, s, len, sub, n, maxsplit
2576 );
Martin v. Löwis423be952008-08-13 15:53:07 +00002577 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002578 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002579}
2580
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002581/*[clinic input]
2582bytearray.reverse
2583
2584 self: self(type="PyByteArrayObject *")
2585
2586Reverse the order of the values in B in place.
2587[clinic start generated code]*/
2588
2589PyDoc_STRVAR(bytearray_reverse__doc__,
2590"reverse($self, /)\n"
2591"--\n"
2592"\n"
2593"Reverse the order of the values in B in place.");
2594
2595#define BYTEARRAY_REVERSE_METHODDEF \
2596 {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, bytearray_reverse__doc__},
2597
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002598static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002599bytearray_reverse_impl(PyByteArrayObject *self);
2600
2601static PyObject *
2602bytearray_reverse(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
2603{
2604 return bytearray_reverse_impl(self);
2605}
2606
2607static PyObject *
2608bytearray_reverse_impl(PyByteArrayObject *self)
2609/*[clinic end generated code: output=5d5e5f0bfc67f476 input=7933a499b8597bd1]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002610{
2611 char swap, *head, *tail;
2612 Py_ssize_t i, j, n = Py_SIZE(self);
2613
2614 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002615 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002616 tail = head + n - 1;
2617 for (i = 0; i < j; i++) {
2618 swap = *head;
2619 *head++ = *tail;
2620 *tail-- = swap;
2621 }
2622
2623 Py_RETURN_NONE;
2624}
2625
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002626
2627/*[python input]
2628class bytesvalue_converter(CConverter):
2629 type = 'int'
2630 converter = '_getbytevalue'
2631[python start generated code]*/
2632/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
2633
2634
2635/*[clinic input]
2636bytearray.insert
2637
2638 self: self(type="PyByteArrayObject *")
2639 index: Py_ssize_t
2640 The index where the value is to be inserted.
2641 item: bytesvalue
2642 The item to be inserted.
2643 /
2644
2645Insert a single item into the bytearray before the given index.
2646[clinic start generated code]*/
2647
2648PyDoc_STRVAR(bytearray_insert__doc__,
2649"insert($self, index, item, /)\n"
2650"--\n"
2651"\n"
2652"Insert a single item into the bytearray before the given index.\n"
2653"\n"
2654" index\n"
2655" The index where the value is to be inserted.\n"
2656" item\n"
2657" The item to be inserted.");
2658
2659#define BYTEARRAY_INSERT_METHODDEF \
2660 {"insert", (PyCFunction)bytearray_insert, METH_VARARGS, bytearray_insert__doc__},
2661
2662static PyObject *
2663bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item);
2664
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002665static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002666bytearray_insert(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002667{
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002668 PyObject *return_value = NULL;
2669 Py_ssize_t index;
2670 int item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002671
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002672 if (!PyArg_ParseTuple(args,
2673 "nO&:insert",
2674 &index, _getbytevalue, &item))
2675 goto exit;
2676 return_value = bytearray_insert_impl(self, index, item);
2677
2678exit:
2679 return return_value;
2680}
2681
2682static PyObject *
2683bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
2684/*[clinic end generated code: output=5ec9340d4ad19080 input=833766836ba30e1e]*/
2685{
2686 Py_ssize_t n = Py_SIZE(self);
2687 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002688
2689 if (n == PY_SSIZE_T_MAX) {
2690 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00002691 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002692 return NULL;
2693 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002694 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2695 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002696 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002697
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002698 if (index < 0) {
2699 index += n;
2700 if (index < 0)
2701 index = 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002702 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002703 if (index > n)
2704 index = n;
2705 memmove(buf + index + 1, buf + index, n - index);
2706 buf[index] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002707
2708 Py_RETURN_NONE;
2709}
2710
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002711/*[clinic input]
2712bytearray.append
2713
2714 self: self(type="PyByteArrayObject *")
2715 item: bytesvalue
2716 The item to be appended.
2717 /
2718
2719Append a single item to the end of the bytearray.
2720[clinic start generated code]*/
2721
2722PyDoc_STRVAR(bytearray_append__doc__,
2723"append($self, item, /)\n"
2724"--\n"
2725"\n"
2726"Append a single item to the end of the bytearray.\n"
2727"\n"
2728" item\n"
2729" The item to be appended.");
2730
2731#define BYTEARRAY_APPEND_METHODDEF \
2732 {"append", (PyCFunction)bytearray_append, METH_VARARGS, bytearray_append__doc__},
2733
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002734static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002735bytearray_append_impl(PyByteArrayObject *self, int item);
2736
2737static PyObject *
2738bytearray_append(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002739{
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002740 PyObject *return_value = NULL;
2741 int item;
2742
2743 if (!PyArg_ParseTuple(args,
2744 "O&:append",
2745 _getbytevalue, &item))
2746 goto exit;
2747 return_value = bytearray_append_impl(self, item);
2748
2749exit:
2750 return return_value;
2751}
2752
2753static PyObject *
2754bytearray_append_impl(PyByteArrayObject *self, int item)
2755/*[clinic end generated code: output=b5b3325bb3bbaf85 input=ae56ea87380407cc]*/
2756{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002757 Py_ssize_t n = Py_SIZE(self);
2758
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002759 if (n == PY_SSIZE_T_MAX) {
2760 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00002761 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002762 return NULL;
2763 }
2764 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2765 return NULL;
2766
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002767 PyByteArray_AS_STRING(self)[n] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002768
2769 Py_RETURN_NONE;
2770}
2771
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002772/*[clinic input]
2773bytearray.extend
2774
2775 self: self(type="PyByteArrayObject *")
2776 iterable_of_ints: object
2777 The iterable of items to append.
2778 /
2779
2780Append all the items from the iterator or sequence to the end of the bytearray.
2781[clinic start generated code]*/
2782
2783PyDoc_STRVAR(bytearray_extend__doc__,
2784"extend($self, iterable_of_ints, /)\n"
2785"--\n"
2786"\n"
2787"Append all the items from the iterator or sequence to the end of the bytearray.\n"
2788"\n"
2789" iterable_of_ints\n"
2790" The iterable of items to append.");
2791
2792#define BYTEARRAY_EXTEND_METHODDEF \
2793 {"extend", (PyCFunction)bytearray_extend, METH_O, bytearray_extend__doc__},
2794
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002795static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002796bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
2797/*[clinic end generated code: output=13b0c13ad5110dfb input=ce83a5d75b70d850]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002798{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002799 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002800 Py_ssize_t buf_size = 0, len = 0;
2801 int value;
2802 char *buf;
2803
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002804 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002805 if (PyObject_CheckBuffer(iterable_of_ints)) {
2806 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002807 return NULL;
2808
2809 Py_RETURN_NONE;
2810 }
2811
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002812 it = PyObject_GetIter(iterable_of_ints);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002813 if (it == NULL)
2814 return NULL;
2815
Ezio Melotti42da6632011-03-15 05:18:48 +02002816 /* Try to determine the length of the argument. 32 is arbitrary. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002817 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002818 if (buf_size == -1) {
2819 Py_DECREF(it);
2820 return NULL;
2821 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002822
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002823 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002824 if (bytearray_obj == NULL) {
2825 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002826 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002827 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002828 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002829
2830 while ((item = PyIter_Next(it)) != NULL) {
2831 if (! _getbytevalue(item, &value)) {
2832 Py_DECREF(item);
2833 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002834 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002835 return NULL;
2836 }
2837 buf[len++] = value;
2838 Py_DECREF(item);
2839
2840 if (len >= buf_size) {
2841 buf_size = len + (len >> 1) + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002842 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002843 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002844 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002845 return NULL;
2846 }
2847 /* Recompute the `buf' pointer, since the resizing operation may
2848 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002849 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002850 }
2851 }
2852 Py_DECREF(it);
2853
2854 /* Resize down to exact size. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002855 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
2856 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002857 return NULL;
2858 }
2859
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002860 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
2861 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002862 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002863 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002864 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002865
2866 Py_RETURN_NONE;
2867}
2868
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002869/*[clinic input]
2870bytearray.pop
2871
2872 self: self(type="PyByteArrayObject *")
2873 index: Py_ssize_t = -1
2874 The index from where to remove the item.
2875 -1 (the default value) means remove the last item.
2876 /
2877
2878Remove and return a single item from B.
2879
2880If no index argument is given, will pop the last item.
2881[clinic start generated code]*/
2882
2883PyDoc_STRVAR(bytearray_pop__doc__,
2884"pop($self, index=-1, /)\n"
2885"--\n"
2886"\n"
2887"Remove and return a single item from B.\n"
2888"\n"
2889" index\n"
2890" The index from where to remove the item.\n"
2891" -1 (the default value) means remove the last item.\n"
2892"\n"
2893"If no index argument is given, will pop the last item.");
2894
2895#define BYTEARRAY_POP_METHODDEF \
2896 {"pop", (PyCFunction)bytearray_pop, METH_VARARGS, bytearray_pop__doc__},
2897
2898static PyObject *
2899bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index);
2900
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002901static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002902bytearray_pop(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002903{
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002904 PyObject *return_value = NULL;
2905 Py_ssize_t index = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002906
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002907 if (!PyArg_ParseTuple(args,
2908 "|n:pop",
2909 &index))
2910 goto exit;
2911 return_value = bytearray_pop_impl(self, index);
2912
2913exit:
2914 return return_value;
2915}
2916
2917static PyObject *
2918bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
2919/*[clinic end generated code: output=3b763e548e79af96 input=0797e6c0ca9d5a85]*/
2920{
2921 int value;
2922 Py_ssize_t n = Py_SIZE(self);
2923 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002924
2925 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00002926 PyErr_SetString(PyExc_IndexError,
2927 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002928 return NULL;
2929 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002930 if (index < 0)
2931 index += Py_SIZE(self);
2932 if (index < 0 || index >= Py_SIZE(self)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002933 PyErr_SetString(PyExc_IndexError, "pop index out of range");
2934 return NULL;
2935 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00002936 if (!_canresize(self))
2937 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002938
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002939 buf = PyByteArray_AS_STRING(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002940 value = buf[index];
2941 memmove(buf + index, buf + index + 1, n - index);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002942 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2943 return NULL;
2944
Mark Dickinson54a3db92009-09-06 10:19:23 +00002945 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002946}
2947
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002948/*[clinic input]
2949bytearray.remove
2950
2951 self: self(type="PyByteArrayObject *")
2952 value: bytesvalue
2953 The value to remove.
2954 /
2955
2956Remove the first occurrence of a value in the bytearray.
2957[clinic start generated code]*/
2958
2959PyDoc_STRVAR(bytearray_remove__doc__,
2960"remove($self, value, /)\n"
2961"--\n"
2962"\n"
2963"Remove the first occurrence of a value in the bytearray.\n"
2964"\n"
2965" value\n"
2966" The value to remove.");
2967
2968#define BYTEARRAY_REMOVE_METHODDEF \
2969 {"remove", (PyCFunction)bytearray_remove, METH_VARARGS, bytearray_remove__doc__},
2970
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002971static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002972bytearray_remove_impl(PyByteArrayObject *self, int value);
2973
2974static PyObject *
2975bytearray_remove(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002976{
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002977 PyObject *return_value = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002978 int value;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002979
2980 if (!PyArg_ParseTuple(args,
2981 "O&:remove",
2982 _getbytevalue, &value))
2983 goto exit;
2984 return_value = bytearray_remove_impl(self, value);
2985
2986exit:
2987 return return_value;
2988}
2989
2990static PyObject *
2991bytearray_remove_impl(PyByteArrayObject *self, int value)
2992/*[clinic end generated code: output=c71c8bcf4703abfc input=47560b11fd856c24]*/
2993{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002994 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002995 char *buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002996
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002997 for (where = 0; where < n; where++) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002998 if (buf[where] == value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002999 break;
3000 }
3001 if (where == n) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00003002 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003003 return NULL;
3004 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00003005 if (!_canresize(self))
3006 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003007
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02003008 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003009 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
3010 return NULL;
3011
3012 Py_RETURN_NONE;
3013}
3014
3015/* XXX These two helpers could be optimized if argsize == 1 */
3016
3017static Py_ssize_t
Antoine Pitrou5b720752013-10-05 21:24:10 +02003018lstrip_helper(char *myptr, Py_ssize_t mysize,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003019 void *argptr, Py_ssize_t argsize)
3020{
3021 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02003022 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003023 i++;
3024 return i;
3025}
3026
3027static Py_ssize_t
Antoine Pitrou5b720752013-10-05 21:24:10 +02003028rstrip_helper(char *myptr, Py_ssize_t mysize,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003029 void *argptr, Py_ssize_t argsize)
3030{
3031 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02003032 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003033 i--;
3034 return i + 1;
3035}
3036
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003037/*[clinic input]
3038bytearray.strip
3039
3040 bytes: object = None
3041 /
3042
3043Strip leading and trailing bytes contained in the argument.
3044
3045If the argument is omitted or None, strip leading and trailing ASCII whitespace.
3046[clinic start generated code]*/
3047
3048PyDoc_STRVAR(bytearray_strip__doc__,
3049"strip($self, bytes=None, /)\n"
3050"--\n"
3051"\n"
3052"Strip leading and trailing bytes contained in the argument.\n"
3053"\n"
3054"If the argument is omitted or None, strip leading and trailing ASCII whitespace.");
3055
3056#define BYTEARRAY_STRIP_METHODDEF \
3057 {"strip", (PyCFunction)bytearray_strip, METH_VARARGS, bytearray_strip__doc__},
3058
3059static PyObject *
3060bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes);
3061
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003062static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003063bytearray_strip(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003064{
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003065 PyObject *return_value = NULL;
3066 PyObject *bytes = Py_None;
3067
3068 if (!PyArg_UnpackTuple(args, "strip",
3069 0, 1,
3070 &bytes))
3071 goto exit;
3072 return_value = bytearray_strip_impl(self, bytes);
3073
3074exit:
3075 return return_value;
3076}
3077
3078static PyObject *
3079bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
3080/*[clinic end generated code: output=2e3d3358acc4c235 input=ef7bb59b09c21d62]*/
3081{
3082 Py_ssize_t left, right, mysize, byteslen;
3083 char *myptr, *bytesptr;
3084 Py_buffer vbytes;
3085
3086 if (bytes == Py_None) {
3087 bytesptr = "\t\n\r\f\v ";
3088 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003089 }
3090 else {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003091 if (_getbuffer(bytes, &vbytes) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003092 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003093 bytesptr = (char *) vbytes.buf;
3094 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003095 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02003096 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003097 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003098 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003099 if (left == mysize)
3100 right = left;
3101 else
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003102 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
3103 if (bytes != Py_None)
3104 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02003105 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003106}
3107
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003108/*[clinic input]
3109bytearray.lstrip
3110
3111 bytes: object = None
3112 /
3113
3114Strip leading bytes contained in the argument.
3115
3116If the argument is omitted or None, strip leading ASCII whitespace.
3117[clinic start generated code]*/
3118
3119PyDoc_STRVAR(bytearray_lstrip__doc__,
3120"lstrip($self, bytes=None, /)\n"
3121"--\n"
3122"\n"
3123"Strip leading bytes contained in the argument.\n"
3124"\n"
3125"If the argument is omitted or None, strip leading ASCII whitespace.");
3126
3127#define BYTEARRAY_LSTRIP_METHODDEF \
3128 {"lstrip", (PyCFunction)bytearray_lstrip, METH_VARARGS, bytearray_lstrip__doc__},
3129
3130static PyObject *
3131bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes);
3132
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003133static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003134bytearray_lstrip(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003135{
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003136 PyObject *return_value = NULL;
3137 PyObject *bytes = Py_None;
3138
3139 if (!PyArg_UnpackTuple(args, "lstrip",
3140 0, 1,
3141 &bytes))
3142 goto exit;
3143 return_value = bytearray_lstrip_impl(self, bytes);
3144
3145exit:
3146 return return_value;
3147}
3148
3149static PyObject *
3150bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
3151/*[clinic end generated code: output=2599309808a9ec02 input=80843f975dd7c480]*/
3152{
3153 Py_ssize_t left, right, mysize, byteslen;
3154 char *myptr, *bytesptr;
3155 Py_buffer vbytes;
3156
3157 if (bytes == Py_None) {
3158 bytesptr = "\t\n\r\f\v ";
3159 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003160 }
3161 else {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003162 if (_getbuffer(bytes, &vbytes) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003163 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003164 bytesptr = (char *) vbytes.buf;
3165 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003166 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02003167 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003168 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003169 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003170 right = mysize;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003171 if (bytes != Py_None)
3172 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02003173 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003174}
3175
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003176/*[clinic input]
3177bytearray.rstrip
3178
3179 bytes: object = None
3180 /
3181
3182Strip trailing bytes contained in the argument.
3183
3184If the argument is omitted or None, strip trailing ASCII whitespace.
3185[clinic start generated code]*/
3186
3187PyDoc_STRVAR(bytearray_rstrip__doc__,
3188"rstrip($self, bytes=None, /)\n"
3189"--\n"
3190"\n"
3191"Strip trailing bytes contained in the argument.\n"
3192"\n"
3193"If the argument is omitted or None, strip trailing ASCII whitespace.");
3194
3195#define BYTEARRAY_RSTRIP_METHODDEF \
3196 {"rstrip", (PyCFunction)bytearray_rstrip, METH_VARARGS, bytearray_rstrip__doc__},
3197
3198static PyObject *
3199bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes);
3200
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003201static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003202bytearray_rstrip(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003203{
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003204 PyObject *return_value = NULL;
3205 PyObject *bytes = Py_None;
3206
3207 if (!PyArg_UnpackTuple(args, "rstrip",
3208 0, 1,
3209 &bytes))
3210 goto exit;
3211 return_value = bytearray_rstrip_impl(self, bytes);
3212
3213exit:
3214 return return_value;
3215}
3216
3217static PyObject *
3218bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
3219/*[clinic end generated code: output=b5ca6259f4f4f2a3 input=e728b994954cfd91]*/
3220{
3221 Py_ssize_t right, mysize, byteslen;
3222 char *myptr, *bytesptr;
3223 Py_buffer vbytes;
3224
3225 if (bytes == Py_None) {
3226 bytesptr = "\t\n\r\f\v ";
3227 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003228 }
3229 else {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003230 if (_getbuffer(bytes, &vbytes) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003231 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003232 bytesptr = (char *) vbytes.buf;
3233 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003234 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02003235 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003236 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003237 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
3238 if (bytes != Py_None)
3239 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02003240 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003241}
3242
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003243/*[clinic input]
3244bytearray.decode
3245
3246 encoding: str(c_default="NULL") = 'utf-8'
3247 The encoding with which to decode the bytearray.
3248 errors: str(c_default="NULL") = 'strict'
3249 The error handling scheme to use for the handling of decoding errors.
3250 The default is 'strict' meaning that decoding errors raise a
3251 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
3252 as well as any other name registered with codecs.register_error that
3253 can handle UnicodeDecodeErrors.
3254
3255Decode the bytearray using the codec registered for encoding.
3256[clinic start generated code]*/
3257
3258PyDoc_STRVAR(bytearray_decode__doc__,
3259"decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
3260"--\n"
3261"\n"
3262"Decode the bytearray using the codec registered for encoding.\n"
3263"\n"
3264" encoding\n"
3265" The encoding with which to decode the bytearray.\n"
3266" errors\n"
3267" The error handling scheme to use for the handling of decoding errors.\n"
3268" The default is \'strict\' meaning that decoding errors raise a\n"
3269" UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
3270" as well as any other name registered with codecs.register_error that\n"
3271" can handle UnicodeDecodeErrors.");
3272
3273#define BYTEARRAY_DECODE_METHODDEF \
3274 {"decode", (PyCFunction)bytearray_decode, METH_VARARGS|METH_KEYWORDS, bytearray_decode__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003275
3276static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003277bytearray_decode_impl(PyByteArrayObject *self, const char *encoding, const char *errors);
3278
3279static PyObject *
3280bytearray_decode(PyByteArrayObject *self, PyObject *args, PyObject *kwargs)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003281{
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003282 PyObject *return_value = NULL;
3283 static char *_keywords[] = {"encoding", "errors", NULL};
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003284 const char *encoding = NULL;
3285 const char *errors = NULL;
3286
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003287 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
3288 "|ss:decode", _keywords,
3289 &encoding, &errors))
3290 goto exit;
3291 return_value = bytearray_decode_impl(self, encoding, errors);
3292
3293exit:
3294 return return_value;
3295}
3296
3297static PyObject *
3298bytearray_decode_impl(PyByteArrayObject *self, const char *encoding, const char *errors)
3299/*[clinic end generated code: output=38b83681f1e38a6c input=f28d8f903020257b]*/
3300{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003301 if (encoding == NULL)
3302 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis0efea322014-07-27 17:29:17 +02003303 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003304}
3305
3306PyDoc_STRVAR(alloc_doc,
3307"B.__alloc__() -> int\n\
3308\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00003309Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003310
3311static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003312bytearray_alloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003313{
3314 return PyLong_FromSsize_t(self->ob_alloc);
3315}
3316
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003317/*[clinic input]
3318bytearray.join
3319
3320 iterable_of_bytes: object
3321 /
3322
3323Concatenate any number of bytes/bytearray objects.
3324
3325The bytearray whose method is called is inserted in between each pair.
3326
3327The result is returned as a new bytearray object.
3328[clinic start generated code]*/
3329
3330PyDoc_STRVAR(bytearray_join__doc__,
3331"join($self, iterable_of_bytes, /)\n"
3332"--\n"
3333"\n"
3334"Concatenate any number of bytes/bytearray objects.\n"
3335"\n"
3336"The bytearray whose method is called is inserted in between each pair.\n"
3337"\n"
3338"The result is returned as a new bytearray object.");
3339
3340#define BYTEARRAY_JOIN_METHODDEF \
3341 {"join", (PyCFunction)bytearray_join, METH_O, bytearray_join__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003342
3343static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003344bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
3345/*[clinic end generated code: output=544e7430032dfdf4 input=aba6b1f9b30fcb8e]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003346{
Martin v. Löwis0efea322014-07-27 17:29:17 +02003347 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003348}
3349
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003350/*[clinic input]
3351bytearray.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00003352
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003353 keepends: int(py_default="False") = 0
3354
3355Return a list of the lines in the bytearray, breaking at line boundaries.
3356
3357Line breaks are not included in the resulting list unless keepends is given and
3358true.
3359[clinic start generated code]*/
3360
3361PyDoc_STRVAR(bytearray_splitlines__doc__,
3362"splitlines($self, /, keepends=False)\n"
3363"--\n"
3364"\n"
3365"Return a list of the lines in the bytearray, breaking at line boundaries.\n"
3366"\n"
3367"Line breaks are not included in the resulting list unless keepends is given and\n"
3368"true.");
3369
3370#define BYTEARRAY_SPLITLINES_METHODDEF \
3371 {"splitlines", (PyCFunction)bytearray_splitlines, METH_VARARGS|METH_KEYWORDS, bytearray_splitlines__doc__},
3372
3373static PyObject *
3374bytearray_splitlines_impl(PyByteArrayObject *self, int keepends);
3375
3376static PyObject *
3377bytearray_splitlines(PyByteArrayObject *self, PyObject *args, PyObject *kwargs)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00003378{
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003379 PyObject *return_value = NULL;
3380 static char *_keywords[] = {"keepends", NULL};
Antoine Pitrouf2c54842010-01-13 08:07:53 +00003381 int keepends = 0;
3382
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003383 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
3384 "|i:splitlines", _keywords,
3385 &keepends))
3386 goto exit;
3387 return_value = bytearray_splitlines_impl(self, keepends);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00003388
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003389exit:
3390 return return_value;
3391}
3392
3393static PyObject *
3394bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
3395/*[clinic end generated code: output=a837fd0512ad46ff input=36f0b25bc792f6c0]*/
3396{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00003397 return stringlib_splitlines(
3398 (PyObject*) self, PyByteArray_AS_STRING(self),
3399 PyByteArray_GET_SIZE(self), keepends
3400 );
3401}
3402
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003403static int
Victor Stinner6430fd52011-09-29 04:02:13 +02003404hex_digit_to_int(Py_UCS4 c)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003405{
3406 if (c >= 128)
3407 return -1;
Eric Smith6dc46f52009-04-27 20:39:49 +00003408 if (Py_ISDIGIT(c))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003409 return c - '0';
3410 else {
Eric Smith6dc46f52009-04-27 20:39:49 +00003411 if (Py_ISUPPER(c))
3412 c = Py_TOLOWER(c);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003413 if (c >= 'a' && c <= 'f')
3414 return c - 'a' + 10;
3415 }
3416 return -1;
3417}
3418
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003419/*[clinic input]
3420@classmethod
3421bytearray.fromhex
3422
3423 cls: self(type="PyObject*")
3424 string: unicode
3425 /
3426
3427Create a bytearray object from a string of hexadecimal numbers.
3428
3429Spaces between two numbers are accepted.
3430Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
3431[clinic start generated code]*/
3432
3433PyDoc_STRVAR(bytearray_fromhex__doc__,
3434"fromhex($type, string, /)\n"
3435"--\n"
3436"\n"
3437"Create a bytearray object from a string of hexadecimal numbers.\n"
3438"\n"
3439"Spaces between two numbers are accepted.\n"
3440"Example: bytearray.fromhex(\'B9 01EF\') -> bytearray(b\'\\\\xb9\\\\x01\\\\xef\')");
3441
3442#define BYTEARRAY_FROMHEX_METHODDEF \
3443 {"fromhex", (PyCFunction)bytearray_fromhex, METH_VARARGS|METH_CLASS, bytearray_fromhex__doc__},
3444
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003445static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003446bytearray_fromhex_impl(PyObject*cls, PyObject *string);
3447
3448static PyObject *
3449bytearray_fromhex(PyTypeObject *cls, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003450{
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003451 PyObject *return_value = NULL;
3452 PyObject *string;
3453
3454 if (!PyArg_ParseTuple(args,
3455 "U:fromhex",
3456 &string))
3457 goto exit;
3458 return_value = bytearray_fromhex_impl((PyObject*)cls, string);
3459
3460exit:
3461 return return_value;
3462}
3463
3464static PyObject *
3465bytearray_fromhex_impl(PyObject*cls, PyObject *string)
3466/*[clinic end generated code: output=adc3c804a74e56d4 input=907bbd2d34d9367a]*/
3467{
3468 PyObject *newbytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003469 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003470 Py_ssize_t hexlen, byteslen, i, j;
3471 int top, bot;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003472 void *data;
3473 unsigned int kind;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003474
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003475 assert(PyUnicode_Check(string));
3476 if (PyUnicode_READY(string))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003477 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003478 kind = PyUnicode_KIND(string);
3479 data = PyUnicode_DATA(string);
3480 hexlen = PyUnicode_GET_LENGTH(string);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003481
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003482 byteslen = hexlen/2; /* This overestimates if there are spaces */
3483 newbytes = PyByteArray_FromStringAndSize(NULL, byteslen);
3484 if (!newbytes)
3485 return NULL;
3486 buf = PyByteArray_AS_STRING(newbytes);
3487 for (i = j = 0; i < hexlen; i += 2) {
3488 /* skip over spaces in the input */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003489 while (PyUnicode_READ(kind, data, i) == ' ')
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003490 i++;
3491 if (i >= hexlen)
3492 break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003493 top = hex_digit_to_int(PyUnicode_READ(kind, data, i));
3494 bot = hex_digit_to_int(PyUnicode_READ(kind, data, i+1));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003495 if (top == -1 || bot == -1) {
3496 PyErr_Format(PyExc_ValueError,
3497 "non-hexadecimal number found in "
3498 "fromhex() arg at position %zd", i);
3499 goto error;
3500 }
3501 buf[j++] = (top << 4) + bot;
3502 }
3503 if (PyByteArray_Resize(newbytes, j) < 0)
3504 goto error;
3505 return newbytes;
3506
3507 error:
3508 Py_DECREF(newbytes);
3509 return NULL;
3510}
3511
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003512
3513static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01003514_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003515{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01003516 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003517 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02003518 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02003519
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02003520 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003521 if (dict == NULL) {
3522 PyErr_Clear();
3523 dict = Py_None;
3524 Py_INCREF(dict);
3525 }
3526
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02003527 buf = PyByteArray_AS_STRING(self);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01003528 if (proto < 3) {
3529 /* use str based reduction for backwards compatibility with Python 2.x */
3530 PyObject *latin1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02003531 if (Py_SIZE(self))
3532 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01003533 else
3534 latin1 = PyUnicode_FromString("");
3535 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
3536 }
3537 else {
3538 /* use more efficient byte based reduction */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02003539 if (Py_SIZE(self)) {
3540 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01003541 }
3542 else {
3543 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
3544 }
3545 }
3546}
3547
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003548/*[clinic input]
3549bytearray.__reduce__ as bytearray_reduce
3550
3551 self: self(type="PyByteArrayObject *")
3552
3553Return state information for pickling.
3554[clinic start generated code]*/
3555
3556PyDoc_STRVAR(bytearray_reduce__doc__,
3557"__reduce__($self, /)\n"
3558"--\n"
3559"\n"
3560"Return state information for pickling.");
3561
3562#define BYTEARRAY_REDUCE_METHODDEF \
3563 {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, bytearray_reduce__doc__},
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01003564
3565static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003566bytearray_reduce_impl(PyByteArrayObject *self);
3567
3568static PyObject *
3569bytearray_reduce(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
3570{
3571 return bytearray_reduce_impl(self);
3572}
3573
3574static PyObject *
3575bytearray_reduce_impl(PyByteArrayObject *self)
3576/*[clinic end generated code: output=b1b56fe87bf30fb0 input=fbb07de4d102a03a]*/
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01003577{
3578 return _common_reduce(self, 2);
3579}
3580
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003581/*[clinic input]
3582bytearray.__reduce_ex__ as bytearray_reduce_ex
3583
3584 self: self(type="PyByteArrayObject *")
3585 proto: int = 0
3586 /
3587
3588Return state information for pickling.
3589[clinic start generated code]*/
3590
3591PyDoc_STRVAR(bytearray_reduce_ex__doc__,
3592"__reduce_ex__($self, proto=0, /)\n"
3593"--\n"
3594"\n"
3595"Return state information for pickling.");
3596
3597#define BYTEARRAY_REDUCE_EX_METHODDEF \
3598 {"__reduce_ex__", (PyCFunction)bytearray_reduce_ex, METH_VARARGS, bytearray_reduce_ex__doc__},
3599
3600static PyObject *
3601bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01003602
3603static PyObject *
3604bytearray_reduce_ex(PyByteArrayObject *self, PyObject *args)
3605{
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003606 PyObject *return_value = NULL;
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01003607 int proto = 0;
3608
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003609 if (!PyArg_ParseTuple(args,
3610 "|i:__reduce_ex__",
3611 &proto))
3612 goto exit;
3613 return_value = bytearray_reduce_ex_impl(self, proto);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01003614
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003615exit:
3616 return return_value;
3617}
3618
3619static PyObject *
3620bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
3621/*[clinic end generated code: output=bbd9afb2f5953dc1 input=0e091a42ca6dbd91]*/
3622{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01003623 return _common_reduce(self, proto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003624}
3625
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003626/*[clinic input]
3627bytearray.__sizeof__ as bytearray_sizeof
3628
3629 self: self(type="PyByteArrayObject *")
3630
3631Returns the size of the bytearray object in memory, in bytes.
3632[clinic start generated code]*/
3633
3634PyDoc_STRVAR(bytearray_sizeof__doc__,
3635"__sizeof__($self, /)\n"
3636"--\n"
3637"\n"
3638"Returns the size of the bytearray object in memory, in bytes.");
3639
3640#define BYTEARRAY_SIZEOF_METHODDEF \
3641 {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, bytearray_sizeof__doc__},
3642
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00003643static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003644bytearray_sizeof_impl(PyByteArrayObject *self);
3645
3646static PyObject *
3647bytearray_sizeof(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
3648{
3649 return bytearray_sizeof_impl(self);
3650}
3651
3652static PyObject *
3653bytearray_sizeof_impl(PyByteArrayObject *self)
3654/*[clinic end generated code: output=4a2254b0a85630c6 input=6b23d305362b462b]*/
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00003655{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00003656 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00003657
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00003658 res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char);
3659 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00003660}
3661
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003662static PySequenceMethods bytearray_as_sequence = {
3663 (lenfunc)bytearray_length, /* sq_length */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003664 (binaryfunc)PyByteArray_Concat, /* sq_concat */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003665 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
3666 (ssizeargfunc)bytearray_getitem, /* sq_item */
3667 0, /* sq_slice */
3668 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
3669 0, /* sq_ass_slice */
3670 (objobjproc)bytearray_contains, /* sq_contains */
3671 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
3672 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003673};
3674
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003675static PyMappingMethods bytearray_as_mapping = {
3676 (lenfunc)bytearray_length,
3677 (binaryfunc)bytearray_subscript,
3678 (objobjargproc)bytearray_ass_subscript,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003679};
3680
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003681static PyBufferProcs bytearray_as_buffer = {
3682 (getbufferproc)bytearray_getbuffer,
3683 (releasebufferproc)bytearray_releasebuffer,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003684};
3685
3686static PyMethodDef
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003687bytearray_methods[] = {
3688 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003689 BYTEARRAY_REDUCE_METHODDEF
3690 BYTEARRAY_REDUCE_EX_METHODDEF
3691 BYTEARRAY_SIZEOF_METHODDEF
3692 BYTEARRAY_APPEND_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003693 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
3694 _Py_capitalize__doc__},
3695 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003696 BYTEARRAY_CLEAR_METHODDEF
3697 BYTEARRAY_COPY_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003698 {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003699 BYTEARRAY_DECODE_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003700 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__},
Ezio Melotti745d54d2013-11-16 19:10:57 +02003701 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003702 expandtabs__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003703 BYTEARRAY_EXTEND_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003704 {"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003705 BYTEARRAY_FROMHEX_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003706 {"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003707 BYTEARRAY_INSERT_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003708 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
3709 _Py_isalnum__doc__},
3710 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
3711 _Py_isalpha__doc__},
3712 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
3713 _Py_isdigit__doc__},
3714 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
3715 _Py_islower__doc__},
3716 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
3717 _Py_isspace__doc__},
3718 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
3719 _Py_istitle__doc__},
3720 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
3721 _Py_isupper__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003722 BYTEARRAY_JOIN_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003723 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__},
3724 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003725 BYTEARRAY_LSTRIP_METHODDEF
3726 BYTEARRAY_MAKETRANS_METHODDEF
3727 BYTEARRAY_PARTITION_METHODDEF
3728 BYTEARRAY_POP_METHODDEF
3729 BYTEARRAY_REMOVE_METHODDEF
3730 BYTEARRAY_REPLACE_METHODDEF
3731 BYTEARRAY_REVERSE_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003732 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__},
3733 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003734 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003735 BYTEARRAY_RPARTITION_METHODDEF
3736 BYTEARRAY_RSPLIT_METHODDEF
3737 BYTEARRAY_RSTRIP_METHODDEF
3738 BYTEARRAY_SPLIT_METHODDEF
3739 BYTEARRAY_SPLITLINES_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003740 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003741 startswith__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003742 BYTEARRAY_STRIP_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003743 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
3744 _Py_swapcase__doc__},
3745 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003746 BYTEARRAY_TRANSLATE_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003747 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
3748 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
3749 {NULL}
3750};
3751
Ethan Furmanb95b5612015-01-23 20:05:18 -08003752static PyObject *
3753bytearray_mod(PyObject *v, PyObject *w)
3754{
3755 if (!PyByteArray_Check(v))
3756 Py_RETURN_NOTIMPLEMENTED;
3757 return bytearray_format((PyByteArrayObject *)v, w);
3758}
3759
3760static PyNumberMethods bytearray_as_number = {
3761 0, /*nb_add*/
3762 0, /*nb_subtract*/
3763 0, /*nb_multiply*/
3764 bytearray_mod, /*nb_remainder*/
3765};
3766
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003767PyDoc_STRVAR(bytearray_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00003768"bytearray(iterable_of_ints) -> bytearray\n\
3769bytearray(string, encoding[, errors]) -> bytearray\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01003770bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
3771bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
3772bytearray() -> empty bytes array\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003773\n\
3774Construct an mutable bytearray object from:\n\
3775 - an iterable yielding integers in range(256)\n\
3776 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01003777 - a bytes or a buffer object\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003778 - any object implementing the buffer API.\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01003779 - an integer");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003780
3781
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003782static PyObject *bytearray_iter(PyObject *seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003783
3784PyTypeObject PyByteArray_Type = {
3785 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3786 "bytearray",
3787 sizeof(PyByteArrayObject),
3788 0,
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003789 (destructor)bytearray_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003790 0, /* tp_print */
3791 0, /* tp_getattr */
3792 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00003793 0, /* tp_reserved */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003794 (reprfunc)bytearray_repr, /* tp_repr */
Ethan Furmanb95b5612015-01-23 20:05:18 -08003795 &bytearray_as_number, /* tp_as_number */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003796 &bytearray_as_sequence, /* tp_as_sequence */
3797 &bytearray_as_mapping, /* tp_as_mapping */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003798 0, /* tp_hash */
3799 0, /* tp_call */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003800 bytearray_str, /* tp_str */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003801 PyObject_GenericGetAttr, /* tp_getattro */
3802 0, /* tp_setattro */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003803 &bytearray_as_buffer, /* tp_as_buffer */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003804 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003805 bytearray_doc, /* tp_doc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003806 0, /* tp_traverse */
3807 0, /* tp_clear */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003808 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003809 0, /* tp_weaklistoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003810 bytearray_iter, /* tp_iter */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003811 0, /* tp_iternext */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003812 bytearray_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003813 0, /* tp_members */
3814 0, /* tp_getset */
3815 0, /* tp_base */
3816 0, /* tp_dict */
3817 0, /* tp_descr_get */
3818 0, /* tp_descr_set */
3819 0, /* tp_dictoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003820 (initproc)bytearray_init, /* tp_init */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003821 PyType_GenericAlloc, /* tp_alloc */
3822 PyType_GenericNew, /* tp_new */
3823 PyObject_Del, /* tp_free */
3824};
3825
3826/*********************** Bytes Iterator ****************************/
3827
3828typedef struct {
3829 PyObject_HEAD
3830 Py_ssize_t it_index;
3831 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
3832} bytesiterobject;
3833
3834static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003835bytearrayiter_dealloc(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003836{
3837 _PyObject_GC_UNTRACK(it);
3838 Py_XDECREF(it->it_seq);
3839 PyObject_GC_Del(it);
3840}
3841
3842static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003843bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003844{
3845 Py_VISIT(it->it_seq);
3846 return 0;
3847}
3848
3849static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003850bytearrayiter_next(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003851{
3852 PyByteArrayObject *seq;
3853 PyObject *item;
3854
3855 assert(it != NULL);
3856 seq = it->it_seq;
3857 if (seq == NULL)
3858 return NULL;
3859 assert(PyByteArray_Check(seq));
3860
3861 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
3862 item = PyLong_FromLong(
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02003863 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003864 if (item != NULL)
3865 ++it->it_index;
3866 return item;
3867 }
3868
3869 Py_DECREF(seq);
3870 it->it_seq = NULL;
3871 return NULL;
3872}
3873
3874static PyObject *
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003875bytearrayiter_length_hint(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003876{
3877 Py_ssize_t len = 0;
3878 if (it->it_seq)
3879 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
3880 return PyLong_FromSsize_t(len);
3881}
3882
3883PyDoc_STRVAR(length_hint_doc,
3884 "Private method returning an estimate of len(list(it)).");
3885
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003886static PyObject *
3887bytearrayiter_reduce(bytesiterobject *it)
3888{
3889 if (it->it_seq != NULL) {
Antoine Pitroua7013882012-04-05 00:04:20 +02003890 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003891 it->it_seq, it->it_index);
3892 } else {
3893 PyObject *u = PyUnicode_FromUnicode(NULL, 0);
3894 if (u == NULL)
3895 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +02003896 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003897 }
3898}
3899
3900static PyObject *
3901bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
3902{
3903 Py_ssize_t index = PyLong_AsSsize_t(state);
3904 if (index == -1 && PyErr_Occurred())
3905 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003906 if (it->it_seq != NULL) {
3907 if (index < 0)
3908 index = 0;
3909 else if (index > PyByteArray_GET_SIZE(it->it_seq))
3910 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
3911 it->it_index = index;
3912 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003913 Py_RETURN_NONE;
3914}
3915
3916PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
3917
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003918static PyMethodDef bytearrayiter_methods[] = {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003919 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003920 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003921 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003922 bytearray_reduce__doc__},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003923 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
3924 setstate_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003925 {NULL, NULL} /* sentinel */
3926};
3927
3928PyTypeObject PyByteArrayIter_Type = {
3929 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3930 "bytearray_iterator", /* tp_name */
3931 sizeof(bytesiterobject), /* tp_basicsize */
3932 0, /* tp_itemsize */
3933 /* methods */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003934 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003935 0, /* tp_print */
3936 0, /* tp_getattr */
3937 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00003938 0, /* tp_reserved */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003939 0, /* tp_repr */
3940 0, /* tp_as_number */
3941 0, /* tp_as_sequence */
3942 0, /* tp_as_mapping */
3943 0, /* tp_hash */
3944 0, /* tp_call */
3945 0, /* tp_str */
3946 PyObject_GenericGetAttr, /* tp_getattro */
3947 0, /* tp_setattro */
3948 0, /* tp_as_buffer */
3949 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
3950 0, /* tp_doc */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003951 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003952 0, /* tp_clear */
3953 0, /* tp_richcompare */
3954 0, /* tp_weaklistoffset */
3955 PyObject_SelfIter, /* tp_iter */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003956 (iternextfunc)bytearrayiter_next, /* tp_iternext */
3957 bytearrayiter_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003958 0,
3959};
3960
3961static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003962bytearray_iter(PyObject *seq)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003963{
3964 bytesiterobject *it;
3965
3966 if (!PyByteArray_Check(seq)) {
3967 PyErr_BadInternalCall();
3968 return NULL;
3969 }
3970 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
3971 if (it == NULL)
3972 return NULL;
3973 it->it_index = 0;
3974 Py_INCREF(seq);
3975 it->it_seq = (PyByteArrayObject *)seq;
3976 _PyObject_GC_TRACK(it);
3977 return (PyObject *)it;
3978}