blob: d72abb7bb23fdb2b79b3d381329aa567c96e2332 [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"
7
Antoine Pitroufc8d6f42010-01-17 12:38:54 +00008char _PyByteArray_empty_string[] = "";
Christian Heimes2c9c7a52008-05-26 13:42:13 +00009
10void
11PyByteArray_Fini(void)
12{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000013}
14
15int
16PyByteArray_Init(void)
17{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000018 return 1;
19}
20
21/* end nullbytes support */
22
23/* Helpers */
24
25static int
26_getbytevalue(PyObject* arg, int *value)
27{
28 long face_value;
29
30 if (PyLong_Check(arg)) {
31 face_value = PyLong_AsLong(arg);
Georg Brandl9a54d7c2008-07-16 23:15:30 +000032 } else {
33 PyObject *index = PyNumber_Index(arg);
34 if (index == NULL) {
35 PyErr_Format(PyExc_TypeError, "an integer is required");
Mark Dickinson10de93a2010-07-09 19:25:48 +000036 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000037 return 0;
38 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +000039 face_value = PyLong_AsLong(index);
40 Py_DECREF(index);
41 }
42
43 if (face_value < 0 || face_value >= 256) {
44 /* this includes the OverflowError in case the long is too large */
45 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
Mark Dickinson10de93a2010-07-09 19:25:48 +000046 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000047 return 0;
48 }
49
50 *value = face_value;
51 return 1;
52}
53
54static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +000055bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000056{
57 int ret;
58 void *ptr;
59 if (view == NULL) {
60 obj->ob_exports++;
61 return 0;
62 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000063 ptr = (void *) PyByteArray_AS_STRING(obj);
Martin v. Löwis423be952008-08-13 15:53:07 +000064 ret = PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000065 if (ret >= 0) {
66 obj->ob_exports++;
67 }
68 return ret;
69}
70
71static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +000072bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000073{
74 obj->ob_exports--;
75}
76
77static Py_ssize_t
78_getbuffer(PyObject *obj, Py_buffer *view)
79{
80 PyBufferProcs *buffer = Py_TYPE(obj)->tp_as_buffer;
81
82 if (buffer == NULL || buffer->bf_getbuffer == NULL)
83 {
84 PyErr_Format(PyExc_TypeError,
85 "Type %.100s doesn't support the buffer API",
86 Py_TYPE(obj)->tp_name);
87 return -1;
88 }
89
90 if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0)
91 return -1;
92 return view->len;
93}
94
Antoine Pitrou5504e892008-12-06 21:27:53 +000095static int
96_canresize(PyByteArrayObject *self)
97{
98 if (self->ob_exports > 0) {
99 PyErr_SetString(PyExc_BufferError,
100 "Existing exports of data: object cannot be re-sized");
101 return 0;
102 }
103 return 1;
104}
105
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000106/* Direct API functions */
107
108PyObject *
109PyByteArray_FromObject(PyObject *input)
110{
111 return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type,
112 input, NULL);
113}
114
115PyObject *
116PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
117{
118 PyByteArrayObject *new;
119 Py_ssize_t alloc;
120
121 if (size < 0) {
122 PyErr_SetString(PyExc_SystemError,
123 "Negative size passed to PyByteArray_FromStringAndSize");
124 return NULL;
125 }
126
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000127 /* Prevent buffer overflow when setting alloc to size+1. */
128 if (size == PY_SSIZE_T_MAX) {
129 return PyErr_NoMemory();
130 }
131
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000132 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
133 if (new == NULL)
134 return NULL;
135
136 if (size == 0) {
137 new->ob_bytes = NULL;
138 alloc = 0;
139 }
140 else {
141 alloc = size + 1;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100142 new->ob_bytes = PyObject_Malloc(alloc);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000143 if (new->ob_bytes == NULL) {
144 Py_DECREF(new);
145 return PyErr_NoMemory();
146 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +0000147 if (bytes != NULL && size > 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000148 memcpy(new->ob_bytes, bytes, size);
149 new->ob_bytes[size] = '\0'; /* Trailing null byte */
150 }
151 Py_SIZE(new) = size;
152 new->ob_alloc = alloc;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200153 new->ob_start = new->ob_bytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000154 new->ob_exports = 0;
155
156 return (PyObject *)new;
157}
158
159Py_ssize_t
160PyByteArray_Size(PyObject *self)
161{
162 assert(self != NULL);
163 assert(PyByteArray_Check(self));
164
165 return PyByteArray_GET_SIZE(self);
166}
167
168char *
169PyByteArray_AsString(PyObject *self)
170{
171 assert(self != NULL);
172 assert(PyByteArray_Check(self));
173
174 return PyByteArray_AS_STRING(self);
175}
176
177int
Antoine Pitroucc231542014-11-02 18:40:09 +0100178PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000179{
180 void *sval;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200181 PyByteArrayObject *obj = ((PyByteArrayObject *)self);
Antoine Pitroucc231542014-11-02 18:40:09 +0100182 /* All computations are done unsigned to avoid integer overflows
183 (see issue #22335). */
184 size_t alloc = (size_t) obj->ob_alloc;
185 size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
186 size_t size = (size_t) requested_size;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000187
188 assert(self != NULL);
189 assert(PyByteArray_Check(self));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200190 assert(logical_offset <= alloc);
Antoine Pitroucc231542014-11-02 18:40:09 +0100191 assert(requested_size >= 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000192
Antoine Pitroucc231542014-11-02 18:40:09 +0100193 if (requested_size == Py_SIZE(self)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000194 return 0;
195 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200196 if (!_canresize(obj)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000197 return -1;
198 }
199
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200200 if (size + logical_offset + 1 < alloc) {
201 /* Current buffer is large enough to host the requested size,
202 decide on a strategy. */
203 if (size < alloc / 2) {
204 /* Major downsize; resize down to exact size */
205 alloc = size + 1;
206 }
207 else {
208 /* Minor downsize; quick exit */
209 Py_SIZE(self) = size;
210 PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
211 return 0;
212 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000213 }
214 else {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200215 /* Need growing, decide on a strategy */
216 if (size <= alloc * 1.125) {
217 /* Moderate upsize; overallocate similar to list_resize() */
218 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
219 }
220 else {
221 /* Major upsize; resize up to exact size */
222 alloc = size + 1;
223 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000224 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100225 if (alloc > PY_SSIZE_T_MAX) {
226 PyErr_NoMemory();
227 return -1;
228 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000229
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200230 if (logical_offset > 0) {
231 sval = PyObject_Malloc(alloc);
232 if (sval == NULL) {
233 PyErr_NoMemory();
234 return -1;
235 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100236 memcpy(sval, PyByteArray_AS_STRING(self),
237 Py_MIN(requested_size, Py_SIZE(self)));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200238 PyObject_Free(obj->ob_bytes);
239 }
240 else {
241 sval = PyObject_Realloc(obj->ob_bytes, alloc);
242 if (sval == NULL) {
243 PyErr_NoMemory();
244 return -1;
245 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000246 }
247
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200248 obj->ob_bytes = obj->ob_start = sval;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000249 Py_SIZE(self) = size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200250 obj->ob_alloc = alloc;
251 obj->ob_bytes[size] = '\0'; /* Trailing null byte */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000252
253 return 0;
254}
255
256PyObject *
257PyByteArray_Concat(PyObject *a, PyObject *b)
258{
259 Py_ssize_t size;
260 Py_buffer va, vb;
261 PyByteArrayObject *result = NULL;
262
263 va.len = -1;
264 vb.len = -1;
265 if (_getbuffer(a, &va) < 0 ||
266 _getbuffer(b, &vb) < 0) {
267 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
268 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
269 goto done;
270 }
271
272 size = va.len + vb.len;
273 if (size < 0) {
Benjamin Petersone0124bd2009-03-09 21:04:33 +0000274 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000275 goto done;
276 }
277
278 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, size);
279 if (result != NULL) {
280 memcpy(result->ob_bytes, va.buf, va.len);
281 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
282 }
283
284 done:
285 if (va.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000286 PyBuffer_Release(&va);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000287 if (vb.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000288 PyBuffer_Release(&vb);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000289 return (PyObject *)result;
290}
291
292/* Functions stuffed into the type object */
293
294static Py_ssize_t
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000295bytearray_length(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000296{
297 return Py_SIZE(self);
298}
299
300static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000301bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000302{
303 Py_ssize_t mysize;
304 Py_ssize_t size;
305 Py_buffer vo;
306
307 if (_getbuffer(other, &vo) < 0) {
308 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
309 Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
310 return NULL;
311 }
312
313 mysize = Py_SIZE(self);
314 size = mysize + vo.len;
315 if (size < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000316 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000317 return PyErr_NoMemory();
318 }
319 if (size < self->ob_alloc) {
320 Py_SIZE(self) = size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200321 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0'; /* Trailing null byte */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000322 }
323 else if (PyByteArray_Resize((PyObject *)self, size) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000324 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000325 return NULL;
326 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200327 memcpy(PyByteArray_AS_STRING(self) + mysize, vo.buf, vo.len);
Martin v. Löwis423be952008-08-13 15:53:07 +0000328 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000329 Py_INCREF(self);
330 return (PyObject *)self;
331}
332
333static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000334bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000335{
336 PyByteArrayObject *result;
337 Py_ssize_t mysize;
338 Py_ssize_t size;
339
340 if (count < 0)
341 count = 0;
342 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000343 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000344 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000345 size = mysize * count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000346 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
347 if (result != NULL && size != 0) {
348 if (mysize == 1)
349 memset(result->ob_bytes, self->ob_bytes[0], size);
350 else {
351 Py_ssize_t i;
352 for (i = 0; i < count; i++)
353 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
354 }
355 }
356 return (PyObject *)result;
357}
358
359static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000360bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000361{
362 Py_ssize_t mysize;
363 Py_ssize_t size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200364 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000365
366 if (count < 0)
367 count = 0;
368 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000369 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000370 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000371 size = mysize * count;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200372 if (PyByteArray_Resize((PyObject *)self, size) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000373 return NULL;
374
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200375 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000376 if (mysize == 1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200377 memset(buf, buf[0], size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000378 else {
379 Py_ssize_t i;
380 for (i = 1; i < count; i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200381 memcpy(buf + i*mysize, buf, mysize);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000382 }
383
384 Py_INCREF(self);
385 return (PyObject *)self;
386}
387
388static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000389bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000390{
391 if (i < 0)
392 i += Py_SIZE(self);
393 if (i < 0 || i >= Py_SIZE(self)) {
394 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
395 return NULL;
396 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200397 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000398}
399
400static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000401bytearray_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000402{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000403 if (PyIndex_Check(index)) {
404 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000405
406 if (i == -1 && PyErr_Occurred())
407 return NULL;
408
409 if (i < 0)
410 i += PyByteArray_GET_SIZE(self);
411
412 if (i < 0 || i >= Py_SIZE(self)) {
413 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
414 return NULL;
415 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200416 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000417 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000418 else if (PySlice_Check(index)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000419 Py_ssize_t start, stop, step, slicelength, cur, i;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000420 if (PySlice_GetIndicesEx(index,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000421 PyByteArray_GET_SIZE(self),
422 &start, &stop, &step, &slicelength) < 0) {
423 return NULL;
424 }
425
426 if (slicelength <= 0)
427 return PyByteArray_FromStringAndSize("", 0);
428 else if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200429 return PyByteArray_FromStringAndSize(
430 PyByteArray_AS_STRING(self) + start, slicelength);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000431 }
432 else {
433 char *source_buf = PyByteArray_AS_STRING(self);
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000434 char *result_buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000435 PyObject *result;
436
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000437 result = PyByteArray_FromStringAndSize(NULL, slicelength);
438 if (result == NULL)
439 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000440
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000441 result_buf = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000442 for (cur = start, i = 0; i < slicelength;
443 cur += step, i++) {
444 result_buf[i] = source_buf[cur];
445 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000446 return result;
447 }
448 }
449 else {
450 PyErr_SetString(PyExc_TypeError, "bytearray indices must be integers");
451 return NULL;
452 }
453}
454
455static int
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200456bytearray_setslice_linear(PyByteArrayObject *self,
457 Py_ssize_t lo, Py_ssize_t hi,
458 char *bytes, Py_ssize_t bytes_len)
459{
460 Py_ssize_t avail = hi - lo;
461 char *buf = PyByteArray_AS_STRING(self);
462 Py_ssize_t growth = bytes_len - avail;
Victor Stinner84557232013-11-21 12:29:51 +0100463 int res = 0;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200464 assert(avail >= 0);
465
Victor Stinner84557232013-11-21 12:29:51 +0100466 if (growth < 0) {
467 if (!_canresize(self))
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200468 return -1;
Victor Stinner84557232013-11-21 12:29:51 +0100469
470 if (lo == 0) {
471 /* Shrink the buffer by advancing its logical start */
472 self->ob_start -= growth;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200473 /*
Victor Stinner84557232013-11-21 12:29:51 +0100474 0 lo hi old_size
475 | |<----avail----->|<-----tail------>|
476 | |<-bytes_len->|<-----tail------>|
477 0 new_lo new_hi new_size
478 */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200479 }
Victor Stinner84557232013-11-21 12:29:51 +0100480 else {
481 /*
482 0 lo hi old_size
483 | |<----avail----->|<-----tomove------>|
484 | |<-bytes_len->|<-----tomove------>|
485 0 lo new_hi new_size
486 */
487 memmove(buf + lo + bytes_len, buf + hi,
488 Py_SIZE(self) - hi);
489 }
490 if (PyByteArray_Resize((PyObject *)self,
491 Py_SIZE(self) + growth) < 0) {
492 /* Issue #19578: Handling the memory allocation failure here is
493 tricky here because the bytearray object has already been
494 modified. Depending on growth and lo, the behaviour is
495 different.
496
497 If growth < 0 and lo != 0, the operation is completed, but a
498 MemoryError is still raised and the memory block is not
499 shrinked. Otherwise, the bytearray is restored in its previous
500 state and a MemoryError is raised. */
501 if (lo == 0) {
502 self->ob_start += growth;
503 return -1;
504 }
505 /* memmove() removed bytes, the bytearray object cannot be
506 restored in its previous state. */
507 Py_SIZE(self) += growth;
508 res = -1;
509 }
510 buf = PyByteArray_AS_STRING(self);
511 }
512 else if (growth > 0) {
513 if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
514 PyErr_NoMemory();
515 return -1;
516 }
517
518 if (PyByteArray_Resize((PyObject *)self,
519 Py_SIZE(self) + growth) < 0) {
520 return -1;
521 }
522 buf = PyByteArray_AS_STRING(self);
523 /* Make the place for the additional bytes */
524 /*
525 0 lo hi old_size
526 | |<-avail->|<-----tomove------>|
527 | |<---bytes_len-->|<-----tomove------>|
528 0 lo new_hi new_size
529 */
530 memmove(buf + lo + bytes_len, buf + hi,
531 Py_SIZE(self) - lo - bytes_len);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200532 }
533
534 if (bytes_len > 0)
535 memcpy(buf + lo, bytes, bytes_len);
Victor Stinner84557232013-11-21 12:29:51 +0100536 return res;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200537}
538
539static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000540bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000541 PyObject *values)
542{
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200543 Py_ssize_t needed;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000544 void *bytes;
545 Py_buffer vbytes;
546 int res = 0;
547
548 vbytes.len = -1;
549 if (values == (PyObject *)self) {
550 /* Make a copy and call this function recursively */
551 int err;
552 values = PyByteArray_FromObject(values);
553 if (values == NULL)
554 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000555 err = bytearray_setslice(self, lo, hi, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000556 Py_DECREF(values);
557 return err;
558 }
559 if (values == NULL) {
560 /* del b[lo:hi] */
561 bytes = NULL;
562 needed = 0;
563 }
564 else {
565 if (_getbuffer(values, &vbytes) < 0) {
566 PyErr_Format(PyExc_TypeError,
Georg Brandl3dbca812008-07-23 16:10:53 +0000567 "can't set bytearray slice from %.100s",
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000568 Py_TYPE(values)->tp_name);
569 return -1;
570 }
571 needed = vbytes.len;
572 bytes = vbytes.buf;
573 }
574
575 if (lo < 0)
576 lo = 0;
577 if (hi < lo)
578 hi = lo;
579 if (hi > Py_SIZE(self))
580 hi = Py_SIZE(self);
581
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200582 res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000583 if (vbytes.len != -1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200584 PyBuffer_Release(&vbytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000585 return res;
586}
587
588static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000589bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000590{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000591 int ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000592
593 if (i < 0)
594 i += Py_SIZE(self);
595
596 if (i < 0 || i >= Py_SIZE(self)) {
597 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
598 return -1;
599 }
600
601 if (value == NULL)
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000602 return bytearray_setslice(self, i, i+1, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000603
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000604 if (!_getbytevalue(value, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000605 return -1;
606
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200607 PyByteArray_AS_STRING(self)[i] = ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000608 return 0;
609}
610
611static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000612bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000613{
614 Py_ssize_t start, stop, step, slicelen, needed;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200615 char *buf, *bytes;
616 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000617
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000618 if (PyIndex_Check(index)) {
619 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000620
621 if (i == -1 && PyErr_Occurred())
622 return -1;
623
624 if (i < 0)
625 i += PyByteArray_GET_SIZE(self);
626
627 if (i < 0 || i >= Py_SIZE(self)) {
628 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
629 return -1;
630 }
631
632 if (values == NULL) {
633 /* Fall through to slice assignment */
634 start = i;
635 stop = i + 1;
636 step = 1;
637 slicelen = 1;
638 }
639 else {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000640 int ival;
641 if (!_getbytevalue(values, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000642 return -1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200643 buf[i] = (char)ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000644 return 0;
645 }
646 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000647 else if (PySlice_Check(index)) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000648 if (PySlice_GetIndicesEx(index,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000649 PyByteArray_GET_SIZE(self),
650 &start, &stop, &step, &slicelen) < 0) {
651 return -1;
652 }
653 }
654 else {
655 PyErr_SetString(PyExc_TypeError, "bytearray indices must be integer");
656 return -1;
657 }
658
659 if (values == NULL) {
660 bytes = NULL;
661 needed = 0;
662 }
663 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
Christian Heimes6d26ade2012-11-03 23:07:59 +0100664 int err;
Ezio Melottic64bcbe2012-11-03 21:19:06 +0200665 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
666 PyErr_SetString(PyExc_TypeError,
667 "can assign only bytes, buffers, or iterables "
668 "of ints in range(0, 256)");
669 return -1;
670 }
Georg Brandlf3fa5682010-12-04 17:09:30 +0000671 /* Make a copy and call this function recursively */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000672 values = PyByteArray_FromObject(values);
673 if (values == NULL)
674 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000675 err = bytearray_ass_subscript(self, index, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000676 Py_DECREF(values);
677 return err;
678 }
679 else {
680 assert(PyByteArray_Check(values));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200681 bytes = PyByteArray_AS_STRING(values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000682 needed = Py_SIZE(values);
683 }
684 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
685 if ((step < 0 && start < stop) ||
686 (step > 0 && start > stop))
687 stop = start;
688 if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200689 return bytearray_setslice_linear(self, start, stop, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000690 }
691 else {
692 if (needed == 0) {
693 /* Delete slice */
Mark Dickinsonbc099642010-01-29 17:27:24 +0000694 size_t cur;
695 Py_ssize_t i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000696
Antoine Pitrou5504e892008-12-06 21:27:53 +0000697 if (!_canresize(self))
698 return -1;
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000699
700 if (slicelen == 0)
701 /* Nothing to do here. */
702 return 0;
703
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000704 if (step < 0) {
705 stop = start + 1;
706 start = stop + step * (slicelen - 1) - 1;
707 step = -step;
708 }
709 for (cur = start, i = 0;
710 i < slicelen; cur += step, i++) {
711 Py_ssize_t lim = step - 1;
712
Mark Dickinson66f575b2010-02-14 12:53:32 +0000713 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000714 lim = PyByteArray_GET_SIZE(self) - cur - 1;
715
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200716 memmove(buf + cur - i,
717 buf + cur + 1, lim);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000718 }
719 /* Move the tail of the bytes, in one chunk */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000720 cur = start + (size_t)slicelen*step;
Mark Dickinson66f575b2010-02-14 12:53:32 +0000721 if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200722 memmove(buf + cur - slicelen,
723 buf + cur,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000724 PyByteArray_GET_SIZE(self) - cur);
725 }
726 if (PyByteArray_Resize((PyObject *)self,
727 PyByteArray_GET_SIZE(self) - slicelen) < 0)
728 return -1;
729
730 return 0;
731 }
732 else {
733 /* Assign slice */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000734 Py_ssize_t i;
735 size_t cur;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000736
737 if (needed != slicelen) {
738 PyErr_Format(PyExc_ValueError,
739 "attempt to assign bytes of size %zd "
740 "to extended slice of size %zd",
741 needed, slicelen);
742 return -1;
743 }
744 for (cur = start, i = 0; i < slicelen; cur += step, i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200745 buf[cur] = bytes[i];
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000746 return 0;
747 }
748 }
749}
750
751static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000752bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000753{
754 static char *kwlist[] = {"source", "encoding", "errors", 0};
755 PyObject *arg = NULL;
756 const char *encoding = NULL;
757 const char *errors = NULL;
758 Py_ssize_t count;
759 PyObject *it;
760 PyObject *(*iternext)(PyObject *);
761
762 if (Py_SIZE(self) != 0) {
763 /* Empty previous contents (yes, do this first of all!) */
764 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
765 return -1;
766 }
767
768 /* Parse arguments */
Georg Brandl3dbca812008-07-23 16:10:53 +0000769 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000770 &arg, &encoding, &errors))
771 return -1;
772
773 /* Make a quick exit if no first argument */
774 if (arg == NULL) {
775 if (encoding != NULL || errors != NULL) {
776 PyErr_SetString(PyExc_TypeError,
777 "encoding or errors without sequence argument");
778 return -1;
779 }
780 return 0;
781 }
782
783 if (PyUnicode_Check(arg)) {
784 /* Encode via the codec registry */
785 PyObject *encoded, *new;
786 if (encoding == NULL) {
787 PyErr_SetString(PyExc_TypeError,
788 "string argument without an encoding");
789 return -1;
790 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000791 encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000792 if (encoded == NULL)
793 return -1;
794 assert(PyBytes_Check(encoded));
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000795 new = bytearray_iconcat(self, encoded);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000796 Py_DECREF(encoded);
797 if (new == NULL)
798 return -1;
799 Py_DECREF(new);
800 return 0;
801 }
802
803 /* If it's not unicode, there can't be encoding or errors */
804 if (encoding != NULL || errors != NULL) {
805 PyErr_SetString(PyExc_TypeError,
806 "encoding or errors without a string argument");
807 return -1;
808 }
809
810 /* Is it an int? */
Benjamin Peterson8380dd52010-04-16 22:51:37 +0000811 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
812 if (count == -1 && PyErr_Occurred()) {
813 if (PyErr_ExceptionMatches(PyExc_OverflowError))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000814 return -1;
Benjamin Peterson9c0e94f2010-04-16 23:00:53 +0000815 PyErr_Clear();
Benjamin Peterson8380dd52010-04-16 22:51:37 +0000816 }
817 else if (count < 0) {
818 PyErr_SetString(PyExc_ValueError, "negative count");
819 return -1;
820 }
821 else {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000822 if (count > 0) {
823 if (PyByteArray_Resize((PyObject *)self, count))
824 return -1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200825 memset(PyByteArray_AS_STRING(self), 0, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000826 }
827 return 0;
828 }
829
830 /* Use the buffer API */
831 if (PyObject_CheckBuffer(arg)) {
832 Py_ssize_t size;
833 Py_buffer view;
834 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
835 return -1;
836 size = view.len;
837 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200838 if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
839 &view, size, 'C') < 0)
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200840 goto fail;
Martin v. Löwis423be952008-08-13 15:53:07 +0000841 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000842 return 0;
843 fail:
Martin v. Löwis423be952008-08-13 15:53:07 +0000844 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000845 return -1;
846 }
847
848 /* XXX Optimize this if the arguments is a list, tuple */
849
850 /* Get the iterator */
851 it = PyObject_GetIter(arg);
852 if (it == NULL)
853 return -1;
854 iternext = *Py_TYPE(it)->tp_iternext;
855
856 /* Run the iterator to exhaustion */
857 for (;;) {
858 PyObject *item;
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000859 int rc, value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000860
861 /* Get the next item */
862 item = iternext(it);
863 if (item == NULL) {
864 if (PyErr_Occurred()) {
865 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
866 goto error;
867 PyErr_Clear();
868 }
869 break;
870 }
871
872 /* Interpret it as an int (__index__) */
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000873 rc = _getbytevalue(item, &value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000874 Py_DECREF(item);
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000875 if (!rc)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000876 goto error;
877
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000878 /* Append the byte */
879 if (Py_SIZE(self) < self->ob_alloc)
880 Py_SIZE(self)++;
881 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
882 goto error;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200883 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000884 }
885
886 /* Clean up and return success */
887 Py_DECREF(it);
888 return 0;
889
890 error:
891 /* Error handling when it != NULL */
892 Py_DECREF(it);
893 return -1;
894}
895
896/* Mostly copied from string_repr, but without the
897 "smart quote" functionality. */
898static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000899bytearray_repr(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000900{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000901 const char *quote_prefix = "bytearray(b";
902 const char *quote_postfix = ")";
903 Py_ssize_t length = Py_SIZE(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200904 /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
Mark Dickinson66f575b2010-02-14 12:53:32 +0000905 size_t newsize;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000906 PyObject *v;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200907 Py_ssize_t i;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200908 char *bytes;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200909 char c;
910 char *p;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200911 int quote;
912 char *test, *start;
913 char *buffer;
914
915 if (length > (PY_SSIZE_T_MAX - 15) / 4) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000916 PyErr_SetString(PyExc_OverflowError,
917 "bytearray object is too large to make repr");
918 return NULL;
919 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200920
921 newsize = 15 + length * 4;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100922 buffer = PyObject_Malloc(newsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200923 if (buffer == NULL) {
924 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000925 return NULL;
926 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000927
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200928 /* Figure out which quote to use; single is preferred */
929 quote = '\'';
930 start = PyByteArray_AS_STRING(self);
931 for (test = start; test < start+length; ++test) {
932 if (*test == '"') {
933 quote = '\''; /* back to single */
934 break;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000935 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200936 else if (*test == '\'')
937 quote = '"';
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000938 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200939
940 p = buffer;
941 while (*quote_prefix)
942 *p++ = *quote_prefix++;
943 *p++ = quote;
944
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200945 bytes = PyByteArray_AS_STRING(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200946 for (i = 0; i < length; i++) {
947 /* There's at least enough room for a hex escape
948 and a closing quote. */
949 assert(newsize - (p - buffer) >= 5);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200950 c = bytes[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200951 if (c == '\'' || c == '\\')
952 *p++ = '\\', *p++ = c;
953 else if (c == '\t')
954 *p++ = '\\', *p++ = 't';
955 else if (c == '\n')
956 *p++ = '\\', *p++ = 'n';
957 else if (c == '\r')
958 *p++ = '\\', *p++ = 'r';
959 else if (c == 0)
960 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
961 else if (c < ' ' || c >= 0x7f) {
962 *p++ = '\\';
963 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200964 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
965 *p++ = Py_hexdigits[c & 0xf];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200966 }
967 else
968 *p++ = c;
969 }
970 assert(newsize - (p - buffer) >= 1);
971 *p++ = quote;
972 while (*quote_postfix) {
973 *p++ = *quote_postfix++;
974 }
975
976 v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100977 PyObject_Free(buffer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200978 return v;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000979}
980
981static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000982bytearray_str(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000983{
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000984 if (Py_BytesWarningFlag) {
985 if (PyErr_WarnEx(PyExc_BytesWarning,
986 "str() on a bytearray instance", 1))
987 return NULL;
988 }
989 return bytearray_repr((PyByteArrayObject*)op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000990}
991
992static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000993bytearray_richcompare(PyObject *self, PyObject *other, int op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000994{
995 Py_ssize_t self_size, other_size;
996 Py_buffer self_bytes, other_bytes;
997 PyObject *res;
998 Py_ssize_t minsize;
999 int cmp;
1000
1001 /* Bytes can be compared to anything that supports the (binary)
1002 buffer API. Except that a comparison with Unicode is always an
1003 error, even if the comparison is for equality. */
1004 if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) ||
1005 PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) {
Barry Warsaw9e9dcd62008-10-17 01:50:37 +00001006 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001007 if (PyErr_WarnEx(PyExc_BytesWarning,
Georg Brandle5d68ac2008-06-04 11:30:26 +00001008 "Comparison between bytearray and string", 1))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001009 return NULL;
1010 }
1011
Brian Curtindfc80e32011-08-10 20:28:54 -05001012 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001013 }
1014
1015 self_size = _getbuffer(self, &self_bytes);
1016 if (self_size < 0) {
1017 PyErr_Clear();
Brian Curtindfc80e32011-08-10 20:28:54 -05001018 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001019 }
1020
1021 other_size = _getbuffer(other, &other_bytes);
1022 if (other_size < 0) {
1023 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00001024 PyBuffer_Release(&self_bytes);
Brian Curtindfc80e32011-08-10 20:28:54 -05001025 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001026 }
1027
1028 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1029 /* Shortcut: if the lengths differ, the objects differ */
1030 cmp = (op == Py_NE);
1031 }
1032 else {
1033 minsize = self_size;
1034 if (other_size < minsize)
1035 minsize = other_size;
1036
1037 cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
1038 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1039
1040 if (cmp == 0) {
1041 if (self_size < other_size)
1042 cmp = -1;
1043 else if (self_size > other_size)
1044 cmp = 1;
1045 }
1046
1047 switch (op) {
1048 case Py_LT: cmp = cmp < 0; break;
1049 case Py_LE: cmp = cmp <= 0; break;
1050 case Py_EQ: cmp = cmp == 0; break;
1051 case Py_NE: cmp = cmp != 0; break;
1052 case Py_GT: cmp = cmp > 0; break;
1053 case Py_GE: cmp = cmp >= 0; break;
1054 }
1055 }
1056
1057 res = cmp ? Py_True : Py_False;
Martin v. Löwis423be952008-08-13 15:53:07 +00001058 PyBuffer_Release(&self_bytes);
1059 PyBuffer_Release(&other_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001060 Py_INCREF(res);
1061 return res;
1062}
1063
1064static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001065bytearray_dealloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001066{
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001067 if (self->ob_exports > 0) {
1068 PyErr_SetString(PyExc_SystemError,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001069 "deallocated bytearray object has exported buffers");
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001070 PyErr_Print();
1071 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001072 if (self->ob_bytes != 0) {
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001073 PyObject_Free(self->ob_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001074 }
1075 Py_TYPE(self)->tp_free((PyObject *)self);
1076}
1077
1078
1079/* -------------------------------------------------------------------- */
1080/* Methods */
1081
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001082#define FASTSEARCH fastsearch
1083#define STRINGLIB(F) stringlib_##F
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001084#define STRINGLIB_CHAR char
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001085#define STRINGLIB_SIZEOF_CHAR 1
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001086#define STRINGLIB_LEN PyByteArray_GET_SIZE
1087#define STRINGLIB_STR PyByteArray_AS_STRING
1088#define STRINGLIB_NEW PyByteArray_FromStringAndSize
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001089#define STRINGLIB_ISSPACE Py_ISSPACE
1090#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001091#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1092#define STRINGLIB_MUTABLE 1
1093
1094#include "stringlib/fastsearch.h"
1095#include "stringlib/count.h"
1096#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001097#include "stringlib/join.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001098#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001099#include "stringlib/split.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001100#include "stringlib/ctype.h"
1101#include "stringlib/transmogrify.h"
1102
1103
1104/* The following Py_LOCAL_INLINE and Py_LOCAL functions
1105were copied from the old char* style string object. */
1106
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001107/* helper macro to fixup start/end slice values */
1108#define ADJUST_INDICES(start, end, len) \
1109 if (end > len) \
1110 end = len; \
1111 else if (end < 0) { \
1112 end += len; \
1113 if (end < 0) \
1114 end = 0; \
1115 } \
1116 if (start < 0) { \
1117 start += len; \
1118 if (start < 0) \
1119 start = 0; \
1120 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001121
1122Py_LOCAL_INLINE(Py_ssize_t)
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001123bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001124{
1125 PyObject *subobj;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001126 char byte;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001127 Py_buffer subbuf;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001128 const char *sub;
1129 Py_ssize_t sub_len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001130 Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
1131 Py_ssize_t res;
1132
Antoine Pitrouac65d962011-10-20 23:54:17 +02001133 if (!stringlib_parse_args_finds_byte("find/rfind/index/rindex",
1134 args, &subobj, &byte, &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001135 return -2;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001136
1137 if (subobj) {
1138 if (_getbuffer(subobj, &subbuf) < 0)
1139 return -2;
1140
1141 sub = subbuf.buf;
1142 sub_len = subbuf.len;
1143 }
1144 else {
1145 sub = &byte;
1146 sub_len = 1;
1147 }
1148
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001149 if (dir > 0)
1150 res = stringlib_find_slice(
1151 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
Antoine Pitrouac65d962011-10-20 23:54:17 +02001152 sub, sub_len, start, end);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001153 else
1154 res = stringlib_rfind_slice(
1155 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
Antoine Pitrouac65d962011-10-20 23:54:17 +02001156 sub, sub_len, start, end);
1157
1158 if (subobj)
1159 PyBuffer_Release(&subbuf);
1160
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001161 return res;
1162}
1163
1164PyDoc_STRVAR(find__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001165"B.find(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001166\n\
1167Return the lowest index in B where subsection sub is found,\n\
Senthil Kumaran53516a82011-07-27 23:33:54 +08001168such that sub is contained within B[start,end]. Optional\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001169arguments start and end are interpreted as in slice notation.\n\
1170\n\
1171Return -1 on failure.");
1172
1173static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001174bytearray_find(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001175{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001176 Py_ssize_t result = bytearray_find_internal(self, args, +1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001177 if (result == -2)
1178 return NULL;
1179 return PyLong_FromSsize_t(result);
1180}
1181
1182PyDoc_STRVAR(count__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001183"B.count(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001184\n\
1185Return the number of non-overlapping occurrences of subsection sub in\n\
1186bytes B[start:end]. Optional arguments start and end are interpreted\n\
1187as in slice notation.");
1188
1189static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001190bytearray_count(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001191{
1192 PyObject *sub_obj;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001193 const char *str = PyByteArray_AS_STRING(self), *sub;
1194 Py_ssize_t sub_len;
1195 char byte;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001196 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001197
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001198 Py_buffer vsub;
1199 PyObject *count_obj;
1200
Antoine Pitrouac65d962011-10-20 23:54:17 +02001201 if (!stringlib_parse_args_finds_byte("count", args, &sub_obj, &byte,
1202 &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001203 return NULL;
1204
Antoine Pitrouac65d962011-10-20 23:54:17 +02001205 if (sub_obj) {
1206 if (_getbuffer(sub_obj, &vsub) < 0)
1207 return NULL;
1208
1209 sub = vsub.buf;
1210 sub_len = vsub.len;
1211 }
1212 else {
1213 sub = &byte;
1214 sub_len = 1;
1215 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001216
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001217 ADJUST_INDICES(start, end, PyByteArray_GET_SIZE(self));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001218
1219 count_obj = PyLong_FromSsize_t(
Antoine Pitrouac65d962011-10-20 23:54:17 +02001220 stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001221 );
Antoine Pitrouac65d962011-10-20 23:54:17 +02001222
1223 if (sub_obj)
1224 PyBuffer_Release(&vsub);
1225
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001226 return count_obj;
1227}
1228
Eli Bendersky4db28d32011-03-03 18:21:02 +00001229PyDoc_STRVAR(clear__doc__,
1230"B.clear() -> None\n\
1231\n\
1232Remove all items from B.");
1233
Victor Stinner6430fd52011-09-29 04:02:13 +02001234static PyObject *
Eli Bendersky4db28d32011-03-03 18:21:02 +00001235bytearray_clear(PyByteArrayObject *self)
1236{
1237 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1238 return NULL;
1239 Py_RETURN_NONE;
1240}
1241
1242PyDoc_STRVAR(copy__doc__,
1243"B.copy() -> bytearray\n\
1244\n\
1245Return a copy of B.");
1246
1247static PyObject *
1248bytearray_copy(PyByteArrayObject *self)
1249{
1250 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1251 PyByteArray_GET_SIZE(self));
1252}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001253
1254PyDoc_STRVAR(index__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001255"B.index(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001256\n\
1257Like B.find() but raise ValueError when the subsection is not found.");
1258
1259static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001260bytearray_index(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001261{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001262 Py_ssize_t result = bytearray_find_internal(self, args, +1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001263 if (result == -2)
1264 return NULL;
1265 if (result == -1) {
1266 PyErr_SetString(PyExc_ValueError,
1267 "subsection not found");
1268 return NULL;
1269 }
1270 return PyLong_FromSsize_t(result);
1271}
1272
1273
1274PyDoc_STRVAR(rfind__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001275"B.rfind(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001276\n\
1277Return the highest index in B where subsection sub is found,\n\
Senthil Kumaran53516a82011-07-27 23:33:54 +08001278such that sub is contained within B[start,end]. Optional\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001279arguments start and end are interpreted as in slice notation.\n\
1280\n\
1281Return -1 on failure.");
1282
1283static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001284bytearray_rfind(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001285{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001286 Py_ssize_t result = bytearray_find_internal(self, args, -1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001287 if (result == -2)
1288 return NULL;
1289 return PyLong_FromSsize_t(result);
1290}
1291
1292
1293PyDoc_STRVAR(rindex__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001294"B.rindex(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001295\n\
1296Like B.rfind() but raise ValueError when the subsection is not found.");
1297
1298static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001299bytearray_rindex(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001300{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001301 Py_ssize_t result = bytearray_find_internal(self, args, -1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001302 if (result == -2)
1303 return NULL;
1304 if (result == -1) {
1305 PyErr_SetString(PyExc_ValueError,
1306 "subsection not found");
1307 return NULL;
1308 }
1309 return PyLong_FromSsize_t(result);
1310}
1311
1312
1313static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001314bytearray_contains(PyObject *self, PyObject *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001315{
1316 Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError);
1317 if (ival == -1 && PyErr_Occurred()) {
1318 Py_buffer varg;
Antoine Pitrou0010d372010-08-15 17:12:55 +00001319 Py_ssize_t pos;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001320 PyErr_Clear();
1321 if (_getbuffer(arg, &varg) < 0)
1322 return -1;
1323 pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self),
1324 varg.buf, varg.len, 0);
Martin v. Löwis423be952008-08-13 15:53:07 +00001325 PyBuffer_Release(&varg);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001326 return pos >= 0;
1327 }
1328 if (ival < 0 || ival >= 256) {
1329 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
1330 return -1;
1331 }
1332
Antoine Pitrou0010d372010-08-15 17:12:55 +00001333 return memchr(PyByteArray_AS_STRING(self), (int) ival, Py_SIZE(self)) != NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001334}
1335
1336
1337/* Matches the end (direction >= 0) or start (direction < 0) of self
1338 * against substr, using the start and end arguments. Returns
1339 * -1 on error, 0 if not found and 1 if found.
1340 */
1341Py_LOCAL(int)
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001342_bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001343 Py_ssize_t end, int direction)
1344{
1345 Py_ssize_t len = PyByteArray_GET_SIZE(self);
1346 const char* str;
1347 Py_buffer vsubstr;
1348 int rv = 0;
1349
1350 str = PyByteArray_AS_STRING(self);
1351
1352 if (_getbuffer(substr, &vsubstr) < 0)
1353 return -1;
1354
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001355 ADJUST_INDICES(start, end, len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001356
1357 if (direction < 0) {
1358 /* startswith */
1359 if (start+vsubstr.len > len) {
1360 goto done;
1361 }
1362 } else {
1363 /* endswith */
1364 if (end-start < vsubstr.len || start > len) {
1365 goto done;
1366 }
1367
1368 if (end-vsubstr.len > start)
1369 start = end - vsubstr.len;
1370 }
1371 if (end-start >= vsubstr.len)
1372 rv = ! memcmp(str+start, vsubstr.buf, vsubstr.len);
1373
1374done:
Martin v. Löwis423be952008-08-13 15:53:07 +00001375 PyBuffer_Release(&vsubstr);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001376 return rv;
1377}
1378
1379
1380PyDoc_STRVAR(startswith__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001381"B.startswith(prefix[, start[, end]]) -> bool\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001382\n\
1383Return True if B starts with the specified prefix, False otherwise.\n\
1384With optional start, test B beginning at that position.\n\
1385With optional end, stop comparing B at that position.\n\
Ezio Melottiba42fd52011-04-26 06:09:45 +03001386prefix can also be a tuple of bytes to try.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001387
1388static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001389bytearray_startswith(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001390{
1391 Py_ssize_t start = 0;
1392 Py_ssize_t end = PY_SSIZE_T_MAX;
1393 PyObject *subobj;
1394 int result;
1395
Jesus Ceaac451502011-04-20 17:09:23 +02001396 if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001397 return NULL;
1398 if (PyTuple_Check(subobj)) {
1399 Py_ssize_t i;
1400 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001401 result = _bytearray_tailmatch(self,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001402 PyTuple_GET_ITEM(subobj, i),
1403 start, end, -1);
1404 if (result == -1)
1405 return NULL;
1406 else if (result) {
1407 Py_RETURN_TRUE;
1408 }
1409 }
1410 Py_RETURN_FALSE;
1411 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001412 result = _bytearray_tailmatch(self, subobj, start, end, -1);
Ezio Melottiba42fd52011-04-26 06:09:45 +03001413 if (result == -1) {
1414 if (PyErr_ExceptionMatches(PyExc_TypeError))
1415 PyErr_Format(PyExc_TypeError, "startswith first arg must be bytes "
1416 "or a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001417 return NULL;
Ezio Melottiba42fd52011-04-26 06:09:45 +03001418 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001419 else
1420 return PyBool_FromLong(result);
1421}
1422
1423PyDoc_STRVAR(endswith__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001424"B.endswith(suffix[, start[, end]]) -> bool\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001425\n\
1426Return True if B ends with the specified suffix, False otherwise.\n\
1427With optional start, test B beginning at that position.\n\
1428With optional end, stop comparing B at that position.\n\
Ezio Melottiba42fd52011-04-26 06:09:45 +03001429suffix can also be a tuple of bytes to try.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001430
1431static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001432bytearray_endswith(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001433{
1434 Py_ssize_t start = 0;
1435 Py_ssize_t end = PY_SSIZE_T_MAX;
1436 PyObject *subobj;
1437 int result;
1438
Jesus Ceaac451502011-04-20 17:09:23 +02001439 if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001440 return NULL;
1441 if (PyTuple_Check(subobj)) {
1442 Py_ssize_t i;
1443 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001444 result = _bytearray_tailmatch(self,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001445 PyTuple_GET_ITEM(subobj, i),
1446 start, end, +1);
1447 if (result == -1)
1448 return NULL;
1449 else if (result) {
1450 Py_RETURN_TRUE;
1451 }
1452 }
1453 Py_RETURN_FALSE;
1454 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001455 result = _bytearray_tailmatch(self, subobj, start, end, +1);
Ezio Melottiba42fd52011-04-26 06:09:45 +03001456 if (result == -1) {
1457 if (PyErr_ExceptionMatches(PyExc_TypeError))
1458 PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or "
1459 "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001460 return NULL;
Ezio Melottiba42fd52011-04-26 06:09:45 +03001461 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001462 else
1463 return PyBool_FromLong(result);
1464}
1465
1466
1467PyDoc_STRVAR(translate__doc__,
1468"B.translate(table[, deletechars]) -> bytearray\n\
1469\n\
1470Return a copy of B, where all characters occurring in the\n\
1471optional argument deletechars are removed, and the remaining\n\
1472characters have been mapped through the given translation\n\
1473table, which must be a bytes object of length 256.");
1474
1475static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001476bytearray_translate(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001477{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001478 char *input, *output;
1479 const char *table;
1480 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001481 PyObject *input_obj = (PyObject*)self;
1482 const char *output_start;
1483 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001484 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001485 int trans_table[256];
Georg Brandlccc47b62008-12-28 11:44:14 +00001486 PyObject *tableobj = NULL, *delobj = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001487 Py_buffer vtable, vdel;
1488
1489 if (!PyArg_UnpackTuple(args, "translate", 1, 2,
1490 &tableobj, &delobj))
1491 return NULL;
1492
Georg Brandlccc47b62008-12-28 11:44:14 +00001493 if (tableobj == Py_None) {
1494 table = NULL;
1495 tableobj = NULL;
1496 } else if (_getbuffer(tableobj, &vtable) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001497 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001498 } else {
1499 if (vtable.len != 256) {
1500 PyErr_SetString(PyExc_ValueError,
1501 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001502 PyBuffer_Release(&vtable);
1503 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001504 }
1505 table = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001506 }
1507
1508 if (delobj != NULL) {
1509 if (_getbuffer(delobj, &vdel) < 0) {
Georg Brandl953152f2009-07-22 12:03:59 +00001510 if (tableobj != NULL)
1511 PyBuffer_Release(&vtable);
1512 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001513 }
1514 }
1515 else {
1516 vdel.buf = NULL;
1517 vdel.len = 0;
1518 }
1519
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001520 inlen = PyByteArray_GET_SIZE(input_obj);
1521 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1522 if (result == NULL)
1523 goto done;
1524 output_start = output = PyByteArray_AsString(result);
1525 input = PyByteArray_AS_STRING(input_obj);
1526
Georg Brandlccc47b62008-12-28 11:44:14 +00001527 if (vdel.len == 0 && table != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001528 /* If no deletions are required, use faster code */
1529 for (i = inlen; --i >= 0; ) {
1530 c = Py_CHARMASK(*input++);
Benjamin Peterson0f3641c2008-11-19 22:05:52 +00001531 *output++ = table[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001532 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001533 goto done;
1534 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001535
1536 if (table == NULL) {
1537 for (i = 0; i < 256; i++)
1538 trans_table[i] = Py_CHARMASK(i);
1539 } else {
1540 for (i = 0; i < 256; i++)
1541 trans_table[i] = Py_CHARMASK(table[i]);
1542 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001543
1544 for (i = 0; i < vdel.len; i++)
1545 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1546
1547 for (i = inlen; --i >= 0; ) {
1548 c = Py_CHARMASK(*input++);
1549 if (trans_table[c] != -1)
1550 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1551 continue;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001552 }
1553 /* Fix the size of the resulting string */
1554 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001555 if (PyByteArray_Resize(result, output - output_start) < 0) {
1556 Py_CLEAR(result);
1557 goto done;
1558 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001559
1560done:
Georg Brandlccc47b62008-12-28 11:44:14 +00001561 if (tableobj != NULL)
1562 PyBuffer_Release(&vtable);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001563 if (delobj != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001564 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001565 return result;
1566}
1567
1568
Georg Brandlabc38772009-04-12 15:51:51 +00001569static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001570bytearray_maketrans(PyObject *null, PyObject *args)
Georg Brandlabc38772009-04-12 15:51:51 +00001571{
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001572 return _Py_bytes_maketrans(args);
Georg Brandlabc38772009-04-12 15:51:51 +00001573}
1574
1575
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001576/* find and count characters and substrings */
1577
1578#define findchar(target, target_len, c) \
1579 ((char *)memchr((const void *)(target), c, target_len))
1580
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001581
Benjamin Peterson0f3641c2008-11-19 22:05:52 +00001582/* Bytes ops must return a string, create a copy */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001583Py_LOCAL(PyByteArrayObject *)
1584return_self(PyByteArrayObject *self)
1585{
Georg Brandl1e7217d2008-05-30 12:02:38 +00001586 /* always return a new bytearray */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001587 return (PyByteArrayObject *)PyByteArray_FromStringAndSize(
1588 PyByteArray_AS_STRING(self),
1589 PyByteArray_GET_SIZE(self));
1590}
1591
1592Py_LOCAL_INLINE(Py_ssize_t)
1593countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount)
1594{
1595 Py_ssize_t count=0;
1596 const char *start=target;
1597 const char *end=target+target_len;
1598
1599 while ( (start=findchar(start, end-start, c)) != NULL ) {
1600 count++;
1601 if (count >= maxcount)
1602 break;
1603 start += 1;
1604 }
1605 return count;
1606}
1607
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001608
1609/* Algorithms for different cases of string replacement */
1610
1611/* len(self)>=1, from="", len(to)>=1, maxcount>=1 */
1612Py_LOCAL(PyByteArrayObject *)
1613replace_interleave(PyByteArrayObject *self,
1614 const char *to_s, Py_ssize_t to_len,
1615 Py_ssize_t maxcount)
1616{
1617 char *self_s, *result_s;
1618 Py_ssize_t self_len, result_len;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001619 Py_ssize_t count, i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001620 PyByteArrayObject *result;
1621
1622 self_len = PyByteArray_GET_SIZE(self);
1623
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001624 /* 1 at the end plus 1 after every character;
1625 count = min(maxcount, self_len + 1) */
1626 if (maxcount <= self_len)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001627 count = maxcount;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001628 else
1629 /* Can't overflow: self_len + 1 <= maxcount <= PY_SSIZE_T_MAX. */
1630 count = self_len + 1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001631
1632 /* Check for overflow */
1633 /* result_len = count * to_len + self_len; */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001634 assert(count > 0);
1635 if (to_len > (PY_SSIZE_T_MAX - self_len) / count) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001636 PyErr_SetString(PyExc_OverflowError,
1637 "replace string is too long");
1638 return NULL;
1639 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001640 result_len = count * to_len + self_len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001641
1642 if (! (result = (PyByteArrayObject *)
1643 PyByteArray_FromStringAndSize(NULL, result_len)) )
1644 return NULL;
1645
1646 self_s = PyByteArray_AS_STRING(self);
1647 result_s = PyByteArray_AS_STRING(result);
1648
1649 /* TODO: special case single character, which doesn't need memcpy */
1650
1651 /* Lay the first one down (guaranteed this will occur) */
1652 Py_MEMCPY(result_s, to_s, to_len);
1653 result_s += to_len;
1654 count -= 1;
1655
1656 for (i=0; i<count; i++) {
1657 *result_s++ = *self_s++;
1658 Py_MEMCPY(result_s, to_s, to_len);
1659 result_s += to_len;
1660 }
1661
1662 /* Copy the rest of the original string */
1663 Py_MEMCPY(result_s, self_s, self_len-i);
1664
1665 return result;
1666}
1667
1668/* Special case for deleting a single character */
1669/* len(self)>=1, len(from)==1, to="", maxcount>=1 */
1670Py_LOCAL(PyByteArrayObject *)
1671replace_delete_single_character(PyByteArrayObject *self,
1672 char from_c, Py_ssize_t maxcount)
1673{
1674 char *self_s, *result_s;
1675 char *start, *next, *end;
1676 Py_ssize_t self_len, result_len;
1677 Py_ssize_t count;
1678 PyByteArrayObject *result;
1679
1680 self_len = PyByteArray_GET_SIZE(self);
1681 self_s = PyByteArray_AS_STRING(self);
1682
1683 count = countchar(self_s, self_len, from_c, maxcount);
1684 if (count == 0) {
1685 return return_self(self);
1686 }
1687
1688 result_len = self_len - count; /* from_len == 1 */
1689 assert(result_len>=0);
1690
1691 if ( (result = (PyByteArrayObject *)
1692 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1693 return NULL;
1694 result_s = PyByteArray_AS_STRING(result);
1695
1696 start = self_s;
1697 end = self_s + self_len;
1698 while (count-- > 0) {
1699 next = findchar(start, end-start, from_c);
1700 if (next == NULL)
1701 break;
1702 Py_MEMCPY(result_s, start, next-start);
1703 result_s += (next-start);
1704 start = next+1;
1705 }
1706 Py_MEMCPY(result_s, start, end-start);
1707
1708 return result;
1709}
1710
1711/* len(self)>=1, len(from)>=2, to="", maxcount>=1 */
1712
1713Py_LOCAL(PyByteArrayObject *)
1714replace_delete_substring(PyByteArrayObject *self,
1715 const char *from_s, Py_ssize_t from_len,
1716 Py_ssize_t maxcount)
1717{
1718 char *self_s, *result_s;
1719 char *start, *next, *end;
1720 Py_ssize_t self_len, result_len;
1721 Py_ssize_t count, offset;
1722 PyByteArrayObject *result;
1723
1724 self_len = PyByteArray_GET_SIZE(self);
1725 self_s = PyByteArray_AS_STRING(self);
1726
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001727 count = stringlib_count(self_s, self_len,
1728 from_s, from_len,
1729 maxcount);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001730
1731 if (count == 0) {
1732 /* no matches */
1733 return return_self(self);
1734 }
1735
1736 result_len = self_len - (count * from_len);
1737 assert (result_len>=0);
1738
1739 if ( (result = (PyByteArrayObject *)
1740 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL )
1741 return NULL;
1742
1743 result_s = PyByteArray_AS_STRING(result);
1744
1745 start = self_s;
1746 end = self_s + self_len;
1747 while (count-- > 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001748 offset = stringlib_find(start, end-start,
1749 from_s, from_len,
1750 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001751 if (offset == -1)
1752 break;
1753 next = start + offset;
1754
1755 Py_MEMCPY(result_s, start, next-start);
1756
1757 result_s += (next-start);
1758 start = next+from_len;
1759 }
1760 Py_MEMCPY(result_s, start, end-start);
1761 return result;
1762}
1763
1764/* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */
1765Py_LOCAL(PyByteArrayObject *)
1766replace_single_character_in_place(PyByteArrayObject *self,
1767 char from_c, char to_c,
1768 Py_ssize_t maxcount)
1769{
Antoine Pitroud1188562010-06-09 16:38:55 +00001770 char *self_s, *result_s, *start, *end, *next;
1771 Py_ssize_t self_len;
1772 PyByteArrayObject *result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001773
Antoine Pitroud1188562010-06-09 16:38:55 +00001774 /* The result string will be the same size */
1775 self_s = PyByteArray_AS_STRING(self);
1776 self_len = PyByteArray_GET_SIZE(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001777
Antoine Pitroud1188562010-06-09 16:38:55 +00001778 next = findchar(self_s, self_len, from_c);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001779
Antoine Pitroud1188562010-06-09 16:38:55 +00001780 if (next == NULL) {
1781 /* No matches; return the original bytes */
1782 return return_self(self);
1783 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001784
Antoine Pitroud1188562010-06-09 16:38:55 +00001785 /* Need to make a new bytes */
1786 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len);
1787 if (result == NULL)
1788 return NULL;
1789 result_s = PyByteArray_AS_STRING(result);
1790 Py_MEMCPY(result_s, self_s, self_len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001791
Antoine Pitroud1188562010-06-09 16:38:55 +00001792 /* change everything in-place, starting with this one */
1793 start = result_s + (next-self_s);
1794 *start = to_c;
1795 start++;
1796 end = result_s + self_len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001797
Antoine Pitroud1188562010-06-09 16:38:55 +00001798 while (--maxcount > 0) {
1799 next = findchar(start, end-start, from_c);
1800 if (next == NULL)
1801 break;
1802 *next = to_c;
1803 start = next+1;
1804 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001805
Antoine Pitroud1188562010-06-09 16:38:55 +00001806 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001807}
1808
1809/* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */
1810Py_LOCAL(PyByteArrayObject *)
1811replace_substring_in_place(PyByteArrayObject *self,
1812 const char *from_s, Py_ssize_t from_len,
1813 const char *to_s, Py_ssize_t to_len,
1814 Py_ssize_t maxcount)
1815{
1816 char *result_s, *start, *end;
1817 char *self_s;
1818 Py_ssize_t self_len, offset;
1819 PyByteArrayObject *result;
1820
1821 /* The result bytes will be the same size */
1822
1823 self_s = PyByteArray_AS_STRING(self);
1824 self_len = PyByteArray_GET_SIZE(self);
1825
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001826 offset = stringlib_find(self_s, self_len,
1827 from_s, from_len,
1828 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001829 if (offset == -1) {
1830 /* No matches; return the original bytes */
1831 return return_self(self);
1832 }
1833
1834 /* Need to make a new bytes */
1835 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len);
1836 if (result == NULL)
1837 return NULL;
1838 result_s = PyByteArray_AS_STRING(result);
1839 Py_MEMCPY(result_s, self_s, self_len);
1840
1841 /* change everything in-place, starting with this one */
1842 start = result_s + offset;
1843 Py_MEMCPY(start, to_s, from_len);
1844 start += from_len;
1845 end = result_s + self_len;
1846
1847 while ( --maxcount > 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001848 offset = stringlib_find(start, end-start,
1849 from_s, from_len,
1850 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001851 if (offset==-1)
1852 break;
1853 Py_MEMCPY(start+offset, to_s, from_len);
1854 start += offset+from_len;
1855 }
1856
1857 return result;
1858}
1859
1860/* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */
1861Py_LOCAL(PyByteArrayObject *)
1862replace_single_character(PyByteArrayObject *self,
1863 char from_c,
1864 const char *to_s, Py_ssize_t to_len,
1865 Py_ssize_t maxcount)
1866{
1867 char *self_s, *result_s;
1868 char *start, *next, *end;
1869 Py_ssize_t self_len, result_len;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001870 Py_ssize_t count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001871 PyByteArrayObject *result;
1872
1873 self_s = PyByteArray_AS_STRING(self);
1874 self_len = PyByteArray_GET_SIZE(self);
1875
1876 count = countchar(self_s, self_len, from_c, maxcount);
1877 if (count == 0) {
1878 /* no matches, return unchanged */
1879 return return_self(self);
1880 }
1881
1882 /* use the difference between current and new, hence the "-1" */
1883 /* result_len = self_len + count * (to_len-1) */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001884 assert(count > 0);
1885 if (to_len - 1 > (PY_SSIZE_T_MAX - self_len) / count) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001886 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
1887 return NULL;
1888 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001889 result_len = self_len + count * (to_len - 1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001890
1891 if ( (result = (PyByteArrayObject *)
1892 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1893 return NULL;
1894 result_s = PyByteArray_AS_STRING(result);
1895
1896 start = self_s;
1897 end = self_s + self_len;
1898 while (count-- > 0) {
1899 next = findchar(start, end-start, from_c);
1900 if (next == NULL)
1901 break;
1902
1903 if (next == start) {
1904 /* replace with the 'to' */
1905 Py_MEMCPY(result_s, to_s, to_len);
1906 result_s += to_len;
1907 start += 1;
1908 } else {
1909 /* copy the unchanged old then the 'to' */
1910 Py_MEMCPY(result_s, start, next-start);
1911 result_s += (next-start);
1912 Py_MEMCPY(result_s, to_s, to_len);
1913 result_s += to_len;
1914 start = next+1;
1915 }
1916 }
1917 /* Copy the remainder of the remaining bytes */
1918 Py_MEMCPY(result_s, start, end-start);
1919
1920 return result;
1921}
1922
1923/* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */
1924Py_LOCAL(PyByteArrayObject *)
1925replace_substring(PyByteArrayObject *self,
1926 const char *from_s, Py_ssize_t from_len,
1927 const char *to_s, Py_ssize_t to_len,
1928 Py_ssize_t maxcount)
1929{
1930 char *self_s, *result_s;
1931 char *start, *next, *end;
1932 Py_ssize_t self_len, result_len;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001933 Py_ssize_t count, offset;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001934 PyByteArrayObject *result;
1935
1936 self_s = PyByteArray_AS_STRING(self);
1937 self_len = PyByteArray_GET_SIZE(self);
1938
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001939 count = stringlib_count(self_s, self_len,
1940 from_s, from_len,
1941 maxcount);
1942
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001943 if (count == 0) {
1944 /* no matches, return unchanged */
1945 return return_self(self);
1946 }
1947
1948 /* Check for overflow */
1949 /* result_len = self_len + count * (to_len-from_len) */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001950 assert(count > 0);
1951 if (to_len - from_len > (PY_SSIZE_T_MAX - self_len) / count) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001952 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
1953 return NULL;
1954 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001955 result_len = self_len + count * (to_len - from_len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001956
1957 if ( (result = (PyByteArrayObject *)
1958 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1959 return NULL;
1960 result_s = PyByteArray_AS_STRING(result);
1961
1962 start = self_s;
1963 end = self_s + self_len;
1964 while (count-- > 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001965 offset = stringlib_find(start, end-start,
1966 from_s, from_len,
1967 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001968 if (offset == -1)
1969 break;
1970 next = start+offset;
1971 if (next == start) {
1972 /* replace with the 'to' */
1973 Py_MEMCPY(result_s, to_s, to_len);
1974 result_s += to_len;
1975 start += from_len;
1976 } else {
1977 /* copy the unchanged old then the 'to' */
1978 Py_MEMCPY(result_s, start, next-start);
1979 result_s += (next-start);
1980 Py_MEMCPY(result_s, to_s, to_len);
1981 result_s += to_len;
1982 start = next+from_len;
1983 }
1984 }
1985 /* Copy the remainder of the remaining bytes */
1986 Py_MEMCPY(result_s, start, end-start);
1987
1988 return result;
1989}
1990
1991
1992Py_LOCAL(PyByteArrayObject *)
1993replace(PyByteArrayObject *self,
1994 const char *from_s, Py_ssize_t from_len,
1995 const char *to_s, Py_ssize_t to_len,
1996 Py_ssize_t maxcount)
1997{
1998 if (maxcount < 0) {
1999 maxcount = PY_SSIZE_T_MAX;
2000 } else if (maxcount == 0 || PyByteArray_GET_SIZE(self) == 0) {
2001 /* nothing to do; return the original bytes */
2002 return return_self(self);
2003 }
2004
2005 if (maxcount == 0 ||
2006 (from_len == 0 && to_len == 0)) {
2007 /* nothing to do; return the original bytes */
2008 return return_self(self);
2009 }
2010
2011 /* Handle zero-length special cases */
2012
2013 if (from_len == 0) {
2014 /* insert the 'to' bytes everywhere. */
2015 /* >>> "Python".replace("", ".") */
2016 /* '.P.y.t.h.o.n.' */
2017 return replace_interleave(self, to_s, to_len, maxcount);
2018 }
2019
2020 /* Except for "".replace("", "A") == "A" there is no way beyond this */
2021 /* point for an empty self bytes to generate a non-empty bytes */
2022 /* Special case so the remaining code always gets a non-empty bytes */
2023 if (PyByteArray_GET_SIZE(self) == 0) {
2024 return return_self(self);
2025 }
2026
2027 if (to_len == 0) {
Georg Brandl17cb8a82008-05-30 08:20:09 +00002028 /* delete all occurrences of 'from' bytes */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002029 if (from_len == 1) {
2030 return replace_delete_single_character(
2031 self, from_s[0], maxcount);
2032 } else {
2033 return replace_delete_substring(self, from_s, from_len, maxcount);
2034 }
2035 }
2036
2037 /* Handle special case where both bytes have the same length */
2038
2039 if (from_len == to_len) {
2040 if (from_len == 1) {
2041 return replace_single_character_in_place(
2042 self,
2043 from_s[0],
2044 to_s[0],
2045 maxcount);
2046 } else {
2047 return replace_substring_in_place(
2048 self, from_s, from_len, to_s, to_len, maxcount);
2049 }
2050 }
2051
2052 /* Otherwise use the more generic algorithms */
2053 if (from_len == 1) {
2054 return replace_single_character(self, from_s[0],
2055 to_s, to_len, maxcount);
2056 } else {
2057 /* len('from')>=2, len('to')>=1 */
2058 return replace_substring(self, from_s, from_len, to_s, to_len, maxcount);
2059 }
2060}
2061
2062
2063PyDoc_STRVAR(replace__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00002064"B.replace(old, new[, count]) -> bytearray\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002065\n\
2066Return a copy of B with all occurrences of subsection\n\
2067old replaced by new. If the optional argument count is\n\
2068given, only the first count occurrences are replaced.");
2069
2070static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002071bytearray_replace(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002072{
2073 Py_ssize_t count = -1;
2074 PyObject *from, *to, *res;
2075 Py_buffer vfrom, vto;
2076
2077 if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count))
2078 return NULL;
2079
2080 if (_getbuffer(from, &vfrom) < 0)
2081 return NULL;
2082 if (_getbuffer(to, &vto) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +00002083 PyBuffer_Release(&vfrom);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002084 return NULL;
2085 }
2086
2087 res = (PyObject *)replace((PyByteArrayObject *) self,
2088 vfrom.buf, vfrom.len,
2089 vto.buf, vto.len, count);
2090
Martin v. Löwis423be952008-08-13 15:53:07 +00002091 PyBuffer_Release(&vfrom);
2092 PyBuffer_Release(&vto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002093 return res;
2094}
2095
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002096PyDoc_STRVAR(split__doc__,
Ezio Melotticda6b6d2012-02-26 09:39:55 +02002097"B.split(sep=None, maxsplit=-1) -> list of bytearrays\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002098\n\
2099Return a list of the sections in B, using sep as the delimiter.\n\
2100If sep is not given, B is split on ASCII whitespace characters\n\
2101(space, tab, return, newline, formfeed, vertical tab).\n\
2102If maxsplit is given, at most maxsplit splits are done.");
2103
2104static PyObject *
Ezio Melotticda6b6d2012-02-26 09:39:55 +02002105bytearray_split(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002106{
Ezio Melotticda6b6d2012-02-26 09:39:55 +02002107 static char *kwlist[] = {"sep", "maxsplit", 0};
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002108 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
2109 Py_ssize_t maxsplit = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002110 const char *s = PyByteArray_AS_STRING(self), *sub;
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002111 PyObject *list, *subobj = Py_None;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002112 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002113
Ezio Melotticda6b6d2012-02-26 09:39:55 +02002114 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split",
2115 kwlist, &subobj, &maxsplit))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002116 return NULL;
2117 if (maxsplit < 0)
2118 maxsplit = PY_SSIZE_T_MAX;
2119
2120 if (subobj == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002121 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002122
2123 if (_getbuffer(subobj, &vsub) < 0)
2124 return NULL;
2125 sub = vsub.buf;
2126 n = vsub.len;
2127
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002128 list = stringlib_split(
2129 (PyObject*) self, s, len, sub, n, maxsplit
2130 );
Martin v. Löwis423be952008-08-13 15:53:07 +00002131 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002132 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002133}
2134
2135PyDoc_STRVAR(partition__doc__,
2136"B.partition(sep) -> (head, sep, tail)\n\
2137\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00002138Search for the separator sep in B, and return the part before it,\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002139the separator itself, and the part after it. If the separator is not\n\
2140found, returns B and two empty bytearray objects.");
2141
2142static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002143bytearray_partition(PyByteArrayObject *self, PyObject *sep_obj)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002144{
2145 PyObject *bytesep, *result;
2146
2147 bytesep = PyByteArray_FromObject(sep_obj);
2148 if (! bytesep)
2149 return NULL;
2150
2151 result = stringlib_partition(
2152 (PyObject*) self,
2153 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2154 bytesep,
2155 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2156 );
2157
2158 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002159 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002160}
2161
2162PyDoc_STRVAR(rpartition__doc__,
Ezio Melotti5b2b2422010-01-25 11:58:28 +00002163"B.rpartition(sep) -> (head, sep, tail)\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002164\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00002165Search for the separator sep in B, starting at the end of B,\n\
2166and return the part before it, the separator itself, and the\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002167part after it. If the separator is not found, returns two empty\n\
2168bytearray objects and B.");
2169
2170static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002171bytearray_rpartition(PyByteArrayObject *self, PyObject *sep_obj)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002172{
2173 PyObject *bytesep, *result;
2174
2175 bytesep = PyByteArray_FromObject(sep_obj);
2176 if (! bytesep)
2177 return NULL;
2178
2179 result = stringlib_rpartition(
2180 (PyObject*) self,
2181 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2182 bytesep,
2183 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2184 );
2185
2186 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002187 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002188}
2189
2190PyDoc_STRVAR(rsplit__doc__,
Ezio Melotticda6b6d2012-02-26 09:39:55 +02002191"B.rsplit(sep=None, maxsplit=-1) -> list of bytearrays\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002192\n\
2193Return a list of the sections in B, using sep as the delimiter,\n\
2194starting at the end of B and working to the front.\n\
2195If sep is not given, B is split on ASCII whitespace characters\n\
2196(space, tab, return, newline, formfeed, vertical tab).\n\
2197If maxsplit is given, at most maxsplit splits are done.");
2198
2199static PyObject *
Ezio Melotticda6b6d2012-02-26 09:39:55 +02002200bytearray_rsplit(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002201{
Ezio Melotticda6b6d2012-02-26 09:39:55 +02002202 static char *kwlist[] = {"sep", "maxsplit", 0};
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002203 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
2204 Py_ssize_t maxsplit = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002205 const char *s = PyByteArray_AS_STRING(self), *sub;
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002206 PyObject *list, *subobj = Py_None;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002207 Py_buffer vsub;
2208
Ezio Melotticda6b6d2012-02-26 09:39:55 +02002209 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit",
2210 kwlist, &subobj, &maxsplit))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002211 return NULL;
2212 if (maxsplit < 0)
2213 maxsplit = PY_SSIZE_T_MAX;
2214
2215 if (subobj == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002216 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002217
2218 if (_getbuffer(subobj, &vsub) < 0)
2219 return NULL;
2220 sub = vsub.buf;
2221 n = vsub.len;
2222
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002223 list = stringlib_rsplit(
2224 (PyObject*) self, s, len, sub, n, maxsplit
2225 );
Martin v. Löwis423be952008-08-13 15:53:07 +00002226 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002227 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002228}
2229
2230PyDoc_STRVAR(reverse__doc__,
2231"B.reverse() -> None\n\
2232\n\
2233Reverse the order of the values in B in place.");
2234static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002235bytearray_reverse(PyByteArrayObject *self, PyObject *unused)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002236{
2237 char swap, *head, *tail;
2238 Py_ssize_t i, j, n = Py_SIZE(self);
2239
2240 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002241 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002242 tail = head + n - 1;
2243 for (i = 0; i < j; i++) {
2244 swap = *head;
2245 *head++ = *tail;
2246 *tail-- = swap;
2247 }
2248
2249 Py_RETURN_NONE;
2250}
2251
2252PyDoc_STRVAR(insert__doc__,
2253"B.insert(index, int) -> None\n\
2254\n\
2255Insert a single item into the bytearray before the given index.");
2256static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002257bytearray_insert(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002258{
Georg Brandl9a54d7c2008-07-16 23:15:30 +00002259 PyObject *value;
2260 int ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002261 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002262 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002263
Georg Brandl9a54d7c2008-07-16 23:15:30 +00002264 if (!PyArg_ParseTuple(args, "nO:insert", &where, &value))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002265 return NULL;
2266
2267 if (n == PY_SSIZE_T_MAX) {
2268 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00002269 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002270 return NULL;
2271 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +00002272 if (!_getbytevalue(value, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002273 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002274 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2275 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002276 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002277
2278 if (where < 0) {
2279 where += n;
2280 if (where < 0)
2281 where = 0;
2282 }
2283 if (where > n)
2284 where = n;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002285 memmove(buf + where + 1, buf + where, n - where);
2286 buf[where] = ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002287
2288 Py_RETURN_NONE;
2289}
2290
2291PyDoc_STRVAR(append__doc__,
2292"B.append(int) -> None\n\
2293\n\
2294Append a single item to the end of B.");
2295static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002296bytearray_append(PyByteArrayObject *self, PyObject *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002297{
2298 int value;
2299 Py_ssize_t n = Py_SIZE(self);
2300
2301 if (! _getbytevalue(arg, &value))
2302 return NULL;
2303 if (n == PY_SSIZE_T_MAX) {
2304 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00002305 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002306 return NULL;
2307 }
2308 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2309 return NULL;
2310
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002311 PyByteArray_AS_STRING(self)[n] = value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002312
2313 Py_RETURN_NONE;
2314}
2315
2316PyDoc_STRVAR(extend__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00002317"B.extend(iterable_of_ints) -> None\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002318\n\
2319Append all the elements from the iterator or sequence to the\n\
2320end of B.");
2321static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002322bytearray_extend(PyByteArrayObject *self, PyObject *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002323{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002324 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002325 Py_ssize_t buf_size = 0, len = 0;
2326 int value;
2327 char *buf;
2328
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002329 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002330 if (PyObject_CheckBuffer(arg)) {
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002331 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), arg) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002332 return NULL;
2333
2334 Py_RETURN_NONE;
2335 }
2336
2337 it = PyObject_GetIter(arg);
2338 if (it == NULL)
2339 return NULL;
2340
Ezio Melotti42da6632011-03-15 05:18:48 +02002341 /* Try to determine the length of the argument. 32 is arbitrary. */
Armin Ronacheraa9a79d2012-10-06 14:03:24 +02002342 buf_size = PyObject_LengthHint(arg, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002343 if (buf_size == -1) {
2344 Py_DECREF(it);
2345 return NULL;
2346 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002347
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002348 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002349 if (bytearray_obj == NULL) {
2350 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002351 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002352 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002353 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002354
2355 while ((item = PyIter_Next(it)) != NULL) {
2356 if (! _getbytevalue(item, &value)) {
2357 Py_DECREF(item);
2358 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002359 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002360 return NULL;
2361 }
2362 buf[len++] = value;
2363 Py_DECREF(item);
2364
2365 if (len >= buf_size) {
2366 buf_size = len + (len >> 1) + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002367 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002368 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002369 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002370 return NULL;
2371 }
2372 /* Recompute the `buf' pointer, since the resizing operation may
2373 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002374 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002375 }
2376 }
2377 Py_DECREF(it);
2378
2379 /* Resize down to exact size. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002380 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
2381 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002382 return NULL;
2383 }
2384
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002385 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
2386 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002387 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002388 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002389 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002390
2391 Py_RETURN_NONE;
2392}
2393
2394PyDoc_STRVAR(pop__doc__,
2395"B.pop([index]) -> int\n\
2396\n\
2397Remove and return a single item from B. If no index\n\
Benjamin Petersondcf97b92008-07-02 17:30:14 +00002398argument is given, will pop the last value.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002399static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002400bytearray_pop(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002401{
2402 int value;
2403 Py_ssize_t where = -1, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002404 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002405
2406 if (!PyArg_ParseTuple(args, "|n:pop", &where))
2407 return NULL;
2408
2409 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00002410 PyErr_SetString(PyExc_IndexError,
2411 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002412 return NULL;
2413 }
2414 if (where < 0)
2415 where += Py_SIZE(self);
2416 if (where < 0 || where >= Py_SIZE(self)) {
2417 PyErr_SetString(PyExc_IndexError, "pop index out of range");
2418 return NULL;
2419 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00002420 if (!_canresize(self))
2421 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002422
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002423 buf = PyByteArray_AS_STRING(self);
2424 value = buf[where];
2425 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002426 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2427 return NULL;
2428
Mark Dickinson54a3db92009-09-06 10:19:23 +00002429 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002430}
2431
2432PyDoc_STRVAR(remove__doc__,
2433"B.remove(int) -> None\n\
2434\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00002435Remove the first occurrence of a value in B.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002436static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002437bytearray_remove(PyByteArrayObject *self, PyObject *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002438{
2439 int value;
2440 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002441 char *buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002442
2443 if (! _getbytevalue(arg, &value))
2444 return NULL;
2445
2446 for (where = 0; where < n; where++) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002447 if (buf[where] == value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002448 break;
2449 }
2450 if (where == n) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00002451 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002452 return NULL;
2453 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00002454 if (!_canresize(self))
2455 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002456
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002457 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002458 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2459 return NULL;
2460
2461 Py_RETURN_NONE;
2462}
2463
2464/* XXX These two helpers could be optimized if argsize == 1 */
2465
2466static Py_ssize_t
Antoine Pitrou5b720752013-10-05 21:24:10 +02002467lstrip_helper(char *myptr, Py_ssize_t mysize,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002468 void *argptr, Py_ssize_t argsize)
2469{
2470 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02002471 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002472 i++;
2473 return i;
2474}
2475
2476static Py_ssize_t
Antoine Pitrou5b720752013-10-05 21:24:10 +02002477rstrip_helper(char *myptr, Py_ssize_t mysize,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002478 void *argptr, Py_ssize_t argsize)
2479{
2480 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02002481 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002482 i--;
2483 return i + 1;
2484}
2485
2486PyDoc_STRVAR(strip__doc__,
2487"B.strip([bytes]) -> bytearray\n\
2488\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00002489Strip leading and trailing bytes contained in the argument\n\
2490and return the result as a new bytearray.\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002491If the argument is omitted, strip ASCII whitespace.");
2492static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002493bytearray_strip(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002494{
2495 Py_ssize_t left, right, mysize, argsize;
Antoine Pitrou5b720752013-10-05 21:24:10 +02002496 char *myptr, *argptr;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002497 PyObject *arg = Py_None;
2498 Py_buffer varg;
2499 if (!PyArg_ParseTuple(args, "|O:strip", &arg))
2500 return NULL;
2501 if (arg == Py_None) {
2502 argptr = "\t\n\r\f\v ";
2503 argsize = 6;
2504 }
2505 else {
2506 if (_getbuffer(arg, &varg) < 0)
2507 return NULL;
Antoine Pitrou5b720752013-10-05 21:24:10 +02002508 argptr = (char *) varg.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002509 argsize = varg.len;
2510 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002511 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002512 mysize = Py_SIZE(self);
2513 left = lstrip_helper(myptr, mysize, argptr, argsize);
2514 if (left == mysize)
2515 right = left;
2516 else
2517 right = rstrip_helper(myptr, mysize, argptr, argsize);
2518 if (arg != Py_None)
Martin v. Löwis423be952008-08-13 15:53:07 +00002519 PyBuffer_Release(&varg);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002520 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002521}
2522
2523PyDoc_STRVAR(lstrip__doc__,
2524"B.lstrip([bytes]) -> bytearray\n\
2525\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00002526Strip leading bytes contained in the argument\n\
2527and return the result as a new bytearray.\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002528If the argument is omitted, strip leading ASCII whitespace.");
2529static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002530bytearray_lstrip(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002531{
2532 Py_ssize_t left, right, mysize, argsize;
Antoine Pitrou5b720752013-10-05 21:24:10 +02002533 char *myptr, *argptr;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002534 PyObject *arg = Py_None;
2535 Py_buffer varg;
2536 if (!PyArg_ParseTuple(args, "|O:lstrip", &arg))
2537 return NULL;
2538 if (arg == Py_None) {
2539 argptr = "\t\n\r\f\v ";
2540 argsize = 6;
2541 }
2542 else {
2543 if (_getbuffer(arg, &varg) < 0)
2544 return NULL;
Antoine Pitrou5b720752013-10-05 21:24:10 +02002545 argptr = (char *) varg.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002546 argsize = varg.len;
2547 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002548 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002549 mysize = Py_SIZE(self);
2550 left = lstrip_helper(myptr, mysize, argptr, argsize);
2551 right = mysize;
2552 if (arg != Py_None)
Martin v. Löwis423be952008-08-13 15:53:07 +00002553 PyBuffer_Release(&varg);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002554 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002555}
2556
2557PyDoc_STRVAR(rstrip__doc__,
2558"B.rstrip([bytes]) -> bytearray\n\
2559\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00002560Strip trailing bytes contained in the argument\n\
2561and return the result as a new bytearray.\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002562If the argument is omitted, strip trailing ASCII whitespace.");
2563static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002564bytearray_rstrip(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002565{
Brett Cannonb94767f2011-02-22 20:15:44 +00002566 Py_ssize_t right, mysize, argsize;
Antoine Pitrou5b720752013-10-05 21:24:10 +02002567 char *myptr, *argptr;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002568 PyObject *arg = Py_None;
2569 Py_buffer varg;
2570 if (!PyArg_ParseTuple(args, "|O:rstrip", &arg))
2571 return NULL;
2572 if (arg == Py_None) {
2573 argptr = "\t\n\r\f\v ";
2574 argsize = 6;
2575 }
2576 else {
2577 if (_getbuffer(arg, &varg) < 0)
2578 return NULL;
Antoine Pitrou5b720752013-10-05 21:24:10 +02002579 argptr = (char *) varg.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002580 argsize = varg.len;
2581 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002582 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002583 mysize = Py_SIZE(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002584 right = rstrip_helper(myptr, mysize, argptr, argsize);
2585 if (arg != Py_None)
Martin v. Löwis423be952008-08-13 15:53:07 +00002586 PyBuffer_Release(&varg);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002587 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002588}
2589
2590PyDoc_STRVAR(decode_doc,
Victor Stinnerc911bbf2010-11-07 19:04:46 +00002591"B.decode(encoding='utf-8', errors='strict') -> str\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002592\n\
Victor Stinnere14e2122010-11-07 18:41:46 +00002593Decode B using the codec registered for encoding. Default encoding\n\
2594is 'utf-8'. errors may be given to set a different error\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002595handling scheme. Default is 'strict' meaning that encoding errors raise\n\
2596a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\
2597as well as any other name registered with codecs.register_error that is\n\
2598able to handle UnicodeDecodeErrors.");
2599
2600static PyObject *
Benjamin Peterson308d6372009-09-18 21:42:35 +00002601bytearray_decode(PyObject *self, PyObject *args, PyObject *kwargs)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002602{
2603 const char *encoding = NULL;
2604 const char *errors = NULL;
Benjamin Peterson308d6372009-09-18 21:42:35 +00002605 static char *kwlist[] = {"encoding", "errors", 0};
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002606
Benjamin Peterson308d6372009-09-18 21:42:35 +00002607 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", kwlist, &encoding, &errors))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002608 return NULL;
2609 if (encoding == NULL)
2610 encoding = PyUnicode_GetDefaultEncoding();
Marc-André Lemburgb2750b52008-06-06 12:18:17 +00002611 return PyUnicode_FromEncodedObject(self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002612}
2613
2614PyDoc_STRVAR(alloc_doc,
2615"B.__alloc__() -> int\n\
2616\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00002617Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002618
2619static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002620bytearray_alloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002621{
2622 return PyLong_FromSsize_t(self->ob_alloc);
2623}
2624
2625PyDoc_STRVAR(join_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00002626"B.join(iterable_of_bytes) -> bytearray\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002627\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00002628Concatenate any number of bytes/bytearray objects, with B\n\
2629in between each pair, and return the result as a new bytearray.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002630
2631static PyObject *
Antoine Pitroucfc22b42012-10-16 21:07:23 +02002632bytearray_join(PyObject *self, PyObject *iterable)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002633{
Antoine Pitroucfc22b42012-10-16 21:07:23 +02002634 return stringlib_bytes_join(self, iterable);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002635}
2636
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002637PyDoc_STRVAR(splitlines__doc__,
2638"B.splitlines([keepends]) -> list of lines\n\
2639\n\
2640Return a list of the lines in B, breaking at line boundaries.\n\
2641Line breaks are not included in the resulting list unless keepends\n\
2642is given and true.");
2643
2644static PyObject*
Mark Dickinson0d5f6ad2011-09-24 09:14:39 +01002645bytearray_splitlines(PyObject *self, PyObject *args, PyObject *kwds)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002646{
Mark Dickinson0d5f6ad2011-09-24 09:14:39 +01002647 static char *kwlist[] = {"keepends", 0};
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002648 int keepends = 0;
2649
Mark Dickinson0d5f6ad2011-09-24 09:14:39 +01002650 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines",
2651 kwlist, &keepends))
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002652 return NULL;
2653
2654 return stringlib_splitlines(
2655 (PyObject*) self, PyByteArray_AS_STRING(self),
2656 PyByteArray_GET_SIZE(self), keepends
2657 );
2658}
2659
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002660PyDoc_STRVAR(fromhex_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00002661"bytearray.fromhex(string) -> bytearray (static method)\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002662\n\
2663Create a bytearray object from a string of hexadecimal numbers.\n\
2664Spaces between two numbers are accepted.\n\
2665Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef').");
2666
2667static int
Victor Stinner6430fd52011-09-29 04:02:13 +02002668hex_digit_to_int(Py_UCS4 c)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002669{
2670 if (c >= 128)
2671 return -1;
Eric Smith6dc46f52009-04-27 20:39:49 +00002672 if (Py_ISDIGIT(c))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002673 return c - '0';
2674 else {
Eric Smith6dc46f52009-04-27 20:39:49 +00002675 if (Py_ISUPPER(c))
2676 c = Py_TOLOWER(c);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002677 if (c >= 'a' && c <= 'f')
2678 return c - 'a' + 10;
2679 }
2680 return -1;
2681}
2682
2683static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002684bytearray_fromhex(PyObject *cls, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002685{
2686 PyObject *newbytes, *hexobj;
2687 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002688 Py_ssize_t hexlen, byteslen, i, j;
2689 int top, bot;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002690 void *data;
2691 unsigned int kind;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002692
2693 if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj))
2694 return NULL;
2695 assert(PyUnicode_Check(hexobj));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002696 if (PyUnicode_READY(hexobj))
2697 return NULL;
2698 kind = PyUnicode_KIND(hexobj);
2699 data = PyUnicode_DATA(hexobj);
2700 hexlen = PyUnicode_GET_LENGTH(hexobj);
2701
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002702 byteslen = hexlen/2; /* This overestimates if there are spaces */
2703 newbytes = PyByteArray_FromStringAndSize(NULL, byteslen);
2704 if (!newbytes)
2705 return NULL;
2706 buf = PyByteArray_AS_STRING(newbytes);
2707 for (i = j = 0; i < hexlen; i += 2) {
2708 /* skip over spaces in the input */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002709 while (PyUnicode_READ(kind, data, i) == ' ')
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002710 i++;
2711 if (i >= hexlen)
2712 break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002713 top = hex_digit_to_int(PyUnicode_READ(kind, data, i));
2714 bot = hex_digit_to_int(PyUnicode_READ(kind, data, i+1));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002715 if (top == -1 || bot == -1) {
2716 PyErr_Format(PyExc_ValueError,
2717 "non-hexadecimal number found in "
2718 "fromhex() arg at position %zd", i);
2719 goto error;
2720 }
2721 buf[j++] = (top << 4) + bot;
2722 }
2723 if (PyByteArray_Resize(newbytes, j) < 0)
2724 goto error;
2725 return newbytes;
2726
2727 error:
2728 Py_DECREF(newbytes);
2729 return NULL;
2730}
2731
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002732
2733static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002734_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002735{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002736 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002737 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002738 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002739
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002740 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002741 if (dict == NULL) {
2742 PyErr_Clear();
2743 dict = Py_None;
2744 Py_INCREF(dict);
2745 }
2746
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002747 buf = PyByteArray_AS_STRING(self);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002748 if (proto < 3) {
2749 /* use str based reduction for backwards compatibility with Python 2.x */
2750 PyObject *latin1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002751 if (Py_SIZE(self))
2752 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002753 else
2754 latin1 = PyUnicode_FromString("");
2755 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2756 }
2757 else {
2758 /* use more efficient byte based reduction */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002759 if (Py_SIZE(self)) {
2760 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002761 }
2762 else {
2763 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2764 }
2765 }
2766}
2767
2768PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2769
2770static PyObject *
2771bytearray_reduce(PyByteArrayObject *self)
2772{
2773 return _common_reduce(self, 2);
2774}
2775
2776PyDoc_STRVAR(reduce_ex_doc, "Return state information for pickling.");
2777
2778static PyObject *
2779bytearray_reduce_ex(PyByteArrayObject *self, PyObject *args)
2780{
2781 int proto = 0;
2782
2783 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
2784 return NULL;
2785
2786 return _common_reduce(self, proto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002787}
2788
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002789PyDoc_STRVAR(sizeof_doc,
2790"B.__sizeof__() -> int\n\
2791 \n\
2792Returns the size of B in memory, in bytes");
2793static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002794bytearray_sizeof(PyByteArrayObject *self)
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002795{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002796 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002797
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002798 res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char);
2799 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002800}
2801
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002802static PySequenceMethods bytearray_as_sequence = {
2803 (lenfunc)bytearray_length, /* sq_length */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002804 (binaryfunc)PyByteArray_Concat, /* sq_concat */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002805 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2806 (ssizeargfunc)bytearray_getitem, /* sq_item */
2807 0, /* sq_slice */
2808 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2809 0, /* sq_ass_slice */
2810 (objobjproc)bytearray_contains, /* sq_contains */
2811 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2812 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002813};
2814
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002815static PyMappingMethods bytearray_as_mapping = {
2816 (lenfunc)bytearray_length,
2817 (binaryfunc)bytearray_subscript,
2818 (objobjargproc)bytearray_ass_subscript,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002819};
2820
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002821static PyBufferProcs bytearray_as_buffer = {
2822 (getbufferproc)bytearray_getbuffer,
2823 (releasebufferproc)bytearray_releasebuffer,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002824};
2825
2826static PyMethodDef
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002827bytearray_methods[] = {
2828 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
2829 {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, reduce_doc},
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002830 {"__reduce_ex__", (PyCFunction)bytearray_reduce_ex, METH_VARARGS, reduce_ex_doc},
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002831 {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, sizeof_doc},
2832 {"append", (PyCFunction)bytearray_append, METH_O, append__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002833 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
2834 _Py_capitalize__doc__},
2835 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__},
Eli Bendersky4db28d32011-03-03 18:21:02 +00002836 {"clear", (PyCFunction)bytearray_clear, METH_NOARGS, clear__doc__},
2837 {"copy", (PyCFunction)bytearray_copy, METH_NOARGS, copy__doc__},
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002838 {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__},
Benjamin Peterson308d6372009-09-18 21:42:35 +00002839 {"decode", (PyCFunction)bytearray_decode, METH_VARARGS | METH_KEYWORDS, decode_doc},
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002840 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__},
Ezio Melotti745d54d2013-11-16 19:10:57 +02002841 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002842 expandtabs__doc__},
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002843 {"extend", (PyCFunction)bytearray_extend, METH_O, extend__doc__},
2844 {"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__},
2845 {"fromhex", (PyCFunction)bytearray_fromhex, METH_VARARGS|METH_CLASS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002846 fromhex_doc},
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002847 {"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__},
2848 {"insert", (PyCFunction)bytearray_insert, METH_VARARGS, insert__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002849 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
2850 _Py_isalnum__doc__},
2851 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
2852 _Py_isalpha__doc__},
2853 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
2854 _Py_isdigit__doc__},
2855 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
2856 _Py_islower__doc__},
2857 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
2858 _Py_isspace__doc__},
2859 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
2860 _Py_istitle__doc__},
2861 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
2862 _Py_isupper__doc__},
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002863 {"join", (PyCFunction)bytearray_join, METH_O, join_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002864 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__},
2865 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002866 {"lstrip", (PyCFunction)bytearray_lstrip, METH_VARARGS, lstrip__doc__},
2867 {"maketrans", (PyCFunction)bytearray_maketrans, METH_VARARGS|METH_STATIC,
Georg Brandlabc38772009-04-12 15:51:51 +00002868 _Py_maketrans__doc__},
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002869 {"partition", (PyCFunction)bytearray_partition, METH_O, partition__doc__},
2870 {"pop", (PyCFunction)bytearray_pop, METH_VARARGS, pop__doc__},
2871 {"remove", (PyCFunction)bytearray_remove, METH_O, remove__doc__},
2872 {"replace", (PyCFunction)bytearray_replace, METH_VARARGS, replace__doc__},
2873 {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, reverse__doc__},
2874 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__},
2875 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002876 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__},
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002877 {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, rpartition__doc__},
Ezio Melotticda6b6d2012-02-26 09:39:55 +02002878 {"rsplit", (PyCFunction)bytearray_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__},
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002879 {"rstrip", (PyCFunction)bytearray_rstrip, METH_VARARGS, rstrip__doc__},
Ezio Melotticda6b6d2012-02-26 09:39:55 +02002880 {"split", (PyCFunction)bytearray_split, METH_VARARGS | METH_KEYWORDS, split__doc__},
Mark Dickinson0d5f6ad2011-09-24 09:14:39 +01002881 {"splitlines", (PyCFunction)bytearray_splitlines,
2882 METH_VARARGS | METH_KEYWORDS, splitlines__doc__},
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002883 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002884 startswith__doc__},
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002885 {"strip", (PyCFunction)bytearray_strip, METH_VARARGS, strip__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002886 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
2887 _Py_swapcase__doc__},
2888 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002889 {"translate", (PyCFunction)bytearray_translate, METH_VARARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002890 translate__doc__},
2891 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
2892 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
2893 {NULL}
2894};
2895
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002896PyDoc_STRVAR(bytearray_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00002897"bytearray(iterable_of_ints) -> bytearray\n\
2898bytearray(string, encoding[, errors]) -> bytearray\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002899bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2900bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
2901bytearray() -> empty bytes array\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002902\n\
2903Construct an mutable bytearray object from:\n\
2904 - an iterable yielding integers in range(256)\n\
2905 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002906 - a bytes or a buffer object\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002907 - any object implementing the buffer API.\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002908 - an integer");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002909
2910
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002911static PyObject *bytearray_iter(PyObject *seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002912
2913PyTypeObject PyByteArray_Type = {
2914 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2915 "bytearray",
2916 sizeof(PyByteArrayObject),
2917 0,
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002918 (destructor)bytearray_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002919 0, /* tp_print */
2920 0, /* tp_getattr */
2921 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002922 0, /* tp_reserved */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002923 (reprfunc)bytearray_repr, /* tp_repr */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002924 0, /* tp_as_number */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002925 &bytearray_as_sequence, /* tp_as_sequence */
2926 &bytearray_as_mapping, /* tp_as_mapping */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002927 0, /* tp_hash */
2928 0, /* tp_call */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002929 bytearray_str, /* tp_str */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002930 PyObject_GenericGetAttr, /* tp_getattro */
2931 0, /* tp_setattro */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002932 &bytearray_as_buffer, /* tp_as_buffer */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002933 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002934 bytearray_doc, /* tp_doc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002935 0, /* tp_traverse */
2936 0, /* tp_clear */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002937 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002938 0, /* tp_weaklistoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002939 bytearray_iter, /* tp_iter */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002940 0, /* tp_iternext */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002941 bytearray_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002942 0, /* tp_members */
2943 0, /* tp_getset */
2944 0, /* tp_base */
2945 0, /* tp_dict */
2946 0, /* tp_descr_get */
2947 0, /* tp_descr_set */
2948 0, /* tp_dictoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002949 (initproc)bytearray_init, /* tp_init */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002950 PyType_GenericAlloc, /* tp_alloc */
2951 PyType_GenericNew, /* tp_new */
2952 PyObject_Del, /* tp_free */
2953};
2954
2955/*********************** Bytes Iterator ****************************/
2956
2957typedef struct {
2958 PyObject_HEAD
2959 Py_ssize_t it_index;
2960 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
2961} bytesiterobject;
2962
2963static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002964bytearrayiter_dealloc(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002965{
2966 _PyObject_GC_UNTRACK(it);
2967 Py_XDECREF(it->it_seq);
2968 PyObject_GC_Del(it);
2969}
2970
2971static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002972bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002973{
2974 Py_VISIT(it->it_seq);
2975 return 0;
2976}
2977
2978static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002979bytearrayiter_next(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002980{
2981 PyByteArrayObject *seq;
2982 PyObject *item;
2983
2984 assert(it != NULL);
2985 seq = it->it_seq;
2986 if (seq == NULL)
2987 return NULL;
2988 assert(PyByteArray_Check(seq));
2989
2990 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
2991 item = PyLong_FromLong(
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002992 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002993 if (item != NULL)
2994 ++it->it_index;
2995 return item;
2996 }
2997
2998 Py_DECREF(seq);
2999 it->it_seq = NULL;
3000 return NULL;
3001}
3002
3003static PyObject *
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003004bytearrayiter_length_hint(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003005{
3006 Py_ssize_t len = 0;
3007 if (it->it_seq)
3008 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
3009 return PyLong_FromSsize_t(len);
3010}
3011
3012PyDoc_STRVAR(length_hint_doc,
3013 "Private method returning an estimate of len(list(it)).");
3014
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003015static PyObject *
3016bytearrayiter_reduce(bytesiterobject *it)
3017{
3018 if (it->it_seq != NULL) {
Antoine Pitroua7013882012-04-05 00:04:20 +02003019 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003020 it->it_seq, it->it_index);
3021 } else {
3022 PyObject *u = PyUnicode_FromUnicode(NULL, 0);
3023 if (u == NULL)
3024 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +02003025 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003026 }
3027}
3028
3029static PyObject *
3030bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
3031{
3032 Py_ssize_t index = PyLong_AsSsize_t(state);
3033 if (index == -1 && PyErr_Occurred())
3034 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003035 if (it->it_seq != NULL) {
3036 if (index < 0)
3037 index = 0;
3038 else if (index > PyByteArray_GET_SIZE(it->it_seq))
3039 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
3040 it->it_index = index;
3041 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003042 Py_RETURN_NONE;
3043}
3044
3045PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
3046
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003047static PyMethodDef bytearrayiter_methods[] = {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003048 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003049 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003050 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
3051 reduce_doc},
3052 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
3053 setstate_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003054 {NULL, NULL} /* sentinel */
3055};
3056
3057PyTypeObject PyByteArrayIter_Type = {
3058 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3059 "bytearray_iterator", /* tp_name */
3060 sizeof(bytesiterobject), /* tp_basicsize */
3061 0, /* tp_itemsize */
3062 /* methods */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003063 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003064 0, /* tp_print */
3065 0, /* tp_getattr */
3066 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00003067 0, /* tp_reserved */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003068 0, /* tp_repr */
3069 0, /* tp_as_number */
3070 0, /* tp_as_sequence */
3071 0, /* tp_as_mapping */
3072 0, /* tp_hash */
3073 0, /* tp_call */
3074 0, /* tp_str */
3075 PyObject_GenericGetAttr, /* tp_getattro */
3076 0, /* tp_setattro */
3077 0, /* tp_as_buffer */
3078 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
3079 0, /* tp_doc */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003080 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003081 0, /* tp_clear */
3082 0, /* tp_richcompare */
3083 0, /* tp_weaklistoffset */
3084 PyObject_SelfIter, /* tp_iter */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003085 (iternextfunc)bytearrayiter_next, /* tp_iternext */
3086 bytearrayiter_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003087 0,
3088};
3089
3090static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003091bytearray_iter(PyObject *seq)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003092{
3093 bytesiterobject *it;
3094
3095 if (!PyByteArray_Check(seq)) {
3096 PyErr_BadInternalCall();
3097 return NULL;
3098 }
3099 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
3100 if (it == NULL)
3101 return NULL;
3102 it->it_index = 0;
3103 Py_INCREF(seq);
3104 it->it_seq = (PyByteArrayObject *)seq;
3105 _PyObject_GC_TRACK(it);
3106 return (PyObject *)it;
3107}