blob: 9e8ba3999298d5b1a19bbfbb4f7a7eace2a5c2ce [file] [log] [blame]
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001/* PyByteArray (bytearray) implementation */
2
3#define PY_SSIZE_T_CLEAN
4#include "Python.h"
5#include "structmember.h"
6#include "bytes_methods.h"
Ethan Furmanb95b5612015-01-23 20:05:18 -08007#include "bytesobject.h"
Gregory P. Smith8cb65692015-04-25 23:22:26 +00008#include "pystrhex.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00009
Martin v. Löwis7252a6e2014-07-27 16:25:09 +020010/*[clinic input]
11class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
12[clinic start generated code]*/
13/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/
14
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000015char _PyByteArray_empty_string[] = "";
Christian Heimes2c9c7a52008-05-26 13:42:13 +000016
17void
18PyByteArray_Fini(void)
19{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000020}
21
22int
23PyByteArray_Init(void)
24{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000025 return 1;
26}
27
28/* end nullbytes support */
29
30/* Helpers */
31
32static int
33_getbytevalue(PyObject* arg, int *value)
34{
35 long face_value;
36
37 if (PyLong_Check(arg)) {
38 face_value = PyLong_AsLong(arg);
Georg Brandl9a54d7c2008-07-16 23:15:30 +000039 } else {
40 PyObject *index = PyNumber_Index(arg);
41 if (index == NULL) {
42 PyErr_Format(PyExc_TypeError, "an integer is required");
Mark Dickinson10de93a2010-07-09 19:25:48 +000043 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000044 return 0;
45 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +000046 face_value = PyLong_AsLong(index);
47 Py_DECREF(index);
48 }
49
50 if (face_value < 0 || face_value >= 256) {
51 /* this includes the OverflowError in case the long is too large */
52 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
Mark Dickinson10de93a2010-07-09 19:25:48 +000053 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000054 return 0;
55 }
56
57 *value = face_value;
58 return 1;
59}
60
61static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +000062bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000063{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000064 void *ptr;
65 if (view == NULL) {
Stefan Krah5178d912015-02-03 16:57:21 +010066 PyErr_SetString(PyExc_BufferError,
67 "bytearray_getbuffer: view==NULL argument is obsolete");
68 return -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000069 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000070 ptr = (void *) PyByteArray_AS_STRING(obj);
Stefan Krah5178d912015-02-03 16:57:21 +010071 /* cannot fail if view != NULL and readonly == 0 */
72 (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
73 obj->ob_exports++;
74 return 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000075}
76
77static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +000078bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000079{
80 obj->ob_exports--;
81}
82
Antoine Pitrou5504e892008-12-06 21:27:53 +000083static int
84_canresize(PyByteArrayObject *self)
85{
86 if (self->ob_exports > 0) {
87 PyErr_SetString(PyExc_BufferError,
88 "Existing exports of data: object cannot be re-sized");
89 return 0;
90 }
91 return 1;
92}
93
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030094#include "clinic/bytearrayobject.c.h"
95
Christian Heimes2c9c7a52008-05-26 13:42:13 +000096/* Direct API functions */
97
98PyObject *
99PyByteArray_FromObject(PyObject *input)
100{
101 return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type,
102 input, NULL);
103}
104
105PyObject *
106PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
107{
108 PyByteArrayObject *new;
109 Py_ssize_t alloc;
110
111 if (size < 0) {
112 PyErr_SetString(PyExc_SystemError,
113 "Negative size passed to PyByteArray_FromStringAndSize");
114 return NULL;
115 }
116
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000117 /* Prevent buffer overflow when setting alloc to size+1. */
118 if (size == PY_SSIZE_T_MAX) {
119 return PyErr_NoMemory();
120 }
121
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000122 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
123 if (new == NULL)
124 return NULL;
125
126 if (size == 0) {
127 new->ob_bytes = NULL;
128 alloc = 0;
129 }
130 else {
131 alloc = size + 1;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100132 new->ob_bytes = PyObject_Malloc(alloc);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000133 if (new->ob_bytes == NULL) {
134 Py_DECREF(new);
135 return PyErr_NoMemory();
136 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +0000137 if (bytes != NULL && size > 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000138 memcpy(new->ob_bytes, bytes, size);
139 new->ob_bytes[size] = '\0'; /* Trailing null byte */
140 }
141 Py_SIZE(new) = size;
142 new->ob_alloc = alloc;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200143 new->ob_start = new->ob_bytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000144 new->ob_exports = 0;
145
146 return (PyObject *)new;
147}
148
149Py_ssize_t
150PyByteArray_Size(PyObject *self)
151{
152 assert(self != NULL);
153 assert(PyByteArray_Check(self));
154
155 return PyByteArray_GET_SIZE(self);
156}
157
158char *
159PyByteArray_AsString(PyObject *self)
160{
161 assert(self != NULL);
162 assert(PyByteArray_Check(self));
163
164 return PyByteArray_AS_STRING(self);
165}
166
167int
Antoine Pitroucc231542014-11-02 18:40:09 +0100168PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000169{
170 void *sval;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200171 PyByteArrayObject *obj = ((PyByteArrayObject *)self);
Antoine Pitroucc231542014-11-02 18:40:09 +0100172 /* All computations are done unsigned to avoid integer overflows
173 (see issue #22335). */
174 size_t alloc = (size_t) obj->ob_alloc;
175 size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
176 size_t size = (size_t) requested_size;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000177
178 assert(self != NULL);
179 assert(PyByteArray_Check(self));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200180 assert(logical_offset <= alloc);
Antoine Pitroucc231542014-11-02 18:40:09 +0100181 assert(requested_size >= 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000182
Antoine Pitroucc231542014-11-02 18:40:09 +0100183 if (requested_size == Py_SIZE(self)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000184 return 0;
185 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200186 if (!_canresize(obj)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000187 return -1;
188 }
189
Antoine Pitrou25454112015-05-19 20:52:27 +0200190 if (size + logical_offset + 1 <= alloc) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200191 /* Current buffer is large enough to host the requested size,
192 decide on a strategy. */
193 if (size < alloc / 2) {
194 /* Major downsize; resize down to exact size */
195 alloc = size + 1;
196 }
197 else {
198 /* Minor downsize; quick exit */
199 Py_SIZE(self) = size;
200 PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
201 return 0;
202 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000203 }
204 else {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200205 /* Need growing, decide on a strategy */
206 if (size <= alloc * 1.125) {
207 /* Moderate upsize; overallocate similar to list_resize() */
208 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
209 }
210 else {
211 /* Major upsize; resize up to exact size */
212 alloc = size + 1;
213 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000214 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100215 if (alloc > PY_SSIZE_T_MAX) {
216 PyErr_NoMemory();
217 return -1;
218 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000219
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200220 if (logical_offset > 0) {
221 sval = PyObject_Malloc(alloc);
222 if (sval == NULL) {
223 PyErr_NoMemory();
224 return -1;
225 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100226 memcpy(sval, PyByteArray_AS_STRING(self),
227 Py_MIN(requested_size, Py_SIZE(self)));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200228 PyObject_Free(obj->ob_bytes);
229 }
230 else {
231 sval = PyObject_Realloc(obj->ob_bytes, alloc);
232 if (sval == NULL) {
233 PyErr_NoMemory();
234 return -1;
235 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000236 }
237
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200238 obj->ob_bytes = obj->ob_start = sval;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000239 Py_SIZE(self) = size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200240 obj->ob_alloc = alloc;
241 obj->ob_bytes[size] = '\0'; /* Trailing null byte */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000242
243 return 0;
244}
245
246PyObject *
247PyByteArray_Concat(PyObject *a, PyObject *b)
248{
249 Py_ssize_t size;
250 Py_buffer va, vb;
251 PyByteArrayObject *result = NULL;
252
253 va.len = -1;
254 vb.len = -1;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200255 if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
256 PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000257 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
258 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
259 goto done;
260 }
261
262 size = va.len + vb.len;
263 if (size < 0) {
Benjamin Petersone0124bd2009-03-09 21:04:33 +0000264 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000265 goto done;
266 }
267
268 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, size);
269 if (result != NULL) {
270 memcpy(result->ob_bytes, va.buf, va.len);
271 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
272 }
273
274 done:
275 if (va.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000276 PyBuffer_Release(&va);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000277 if (vb.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000278 PyBuffer_Release(&vb);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000279 return (PyObject *)result;
280}
281
Ethan Furmanb95b5612015-01-23 20:05:18 -0800282static PyObject *
283bytearray_format(PyByteArrayObject *self, PyObject *args)
284{
Victor Stinner772b2b02015-10-14 09:56:53 +0200285 if (self == NULL || !PyByteArray_Check(self)) {
Ethan Furmanb95b5612015-01-23 20:05:18 -0800286 PyErr_BadInternalCall();
287 return NULL;
288 }
Victor Stinner772b2b02015-10-14 09:56:53 +0200289
290 return _PyBytes_FormatEx(PyByteArray_AS_STRING(self),
291 PyByteArray_GET_SIZE(self),
292 args, 1);
Ethan Furmanb95b5612015-01-23 20:05:18 -0800293}
294
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000295/* Functions stuffed into the type object */
296
297static Py_ssize_t
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000298bytearray_length(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000299{
300 return Py_SIZE(self);
301}
302
303static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000304bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000305{
306 Py_ssize_t mysize;
307 Py_ssize_t size;
308 Py_buffer vo;
309
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200310 if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000311 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
312 Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
313 return NULL;
314 }
315
316 mysize = Py_SIZE(self);
317 size = mysize + vo.len;
318 if (size < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000319 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000320 return PyErr_NoMemory();
321 }
Antoine Pitrou25454112015-05-19 20:52:27 +0200322 if (PyByteArray_Resize((PyObject *)self, size) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000323 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000324 return NULL;
325 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200326 memcpy(PyByteArray_AS_STRING(self) + mysize, vo.buf, vo.len);
Martin v. Löwis423be952008-08-13 15:53:07 +0000327 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000328 Py_INCREF(self);
329 return (PyObject *)self;
330}
331
332static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000333bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000334{
335 PyByteArrayObject *result;
336 Py_ssize_t mysize;
337 Py_ssize_t size;
338
339 if (count < 0)
340 count = 0;
341 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000342 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000343 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000344 size = mysize * count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000345 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
346 if (result != NULL && size != 0) {
347 if (mysize == 1)
348 memset(result->ob_bytes, self->ob_bytes[0], size);
349 else {
350 Py_ssize_t i;
351 for (i = 0; i < count; i++)
352 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
353 }
354 }
355 return (PyObject *)result;
356}
357
358static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000359bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000360{
361 Py_ssize_t mysize;
362 Py_ssize_t size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200363 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000364
365 if (count < 0)
366 count = 0;
367 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000368 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000369 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000370 size = mysize * count;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200371 if (PyByteArray_Resize((PyObject *)self, size) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000372 return NULL;
373
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200374 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000375 if (mysize == 1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200376 memset(buf, buf[0], size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000377 else {
378 Py_ssize_t i;
379 for (i = 1; i < count; i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200380 memcpy(buf + i*mysize, buf, mysize);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000381 }
382
383 Py_INCREF(self);
384 return (PyObject *)self;
385}
386
387static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000388bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000389{
390 if (i < 0)
391 i += Py_SIZE(self);
392 if (i < 0 || i >= Py_SIZE(self)) {
393 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
394 return NULL;
395 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200396 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000397}
398
399static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000400bytearray_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000401{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000402 if (PyIndex_Check(index)) {
403 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000404
405 if (i == -1 && PyErr_Occurred())
406 return NULL;
407
408 if (i < 0)
409 i += PyByteArray_GET_SIZE(self);
410
411 if (i < 0 || i >= Py_SIZE(self)) {
412 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
413 return NULL;
414 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200415 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000416 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000417 else if (PySlice_Check(index)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000418 Py_ssize_t start, stop, step, slicelength, cur, i;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000419 if (PySlice_GetIndicesEx(index,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000420 PyByteArray_GET_SIZE(self),
421 &start, &stop, &step, &slicelength) < 0) {
422 return NULL;
423 }
424
425 if (slicelength <= 0)
426 return PyByteArray_FromStringAndSize("", 0);
427 else if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200428 return PyByteArray_FromStringAndSize(
429 PyByteArray_AS_STRING(self) + start, slicelength);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000430 }
431 else {
432 char *source_buf = PyByteArray_AS_STRING(self);
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000433 char *result_buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000434 PyObject *result;
435
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000436 result = PyByteArray_FromStringAndSize(NULL, slicelength);
437 if (result == NULL)
438 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000439
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000440 result_buf = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000441 for (cur = start, i = 0; i < slicelength;
442 cur += step, i++) {
443 result_buf[i] = source_buf[cur];
444 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000445 return result;
446 }
447 }
448 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400449 PyErr_Format(PyExc_TypeError,
450 "bytearray indices must be integers or slices, not %.200s",
451 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000452 return NULL;
453 }
454}
455
456static int
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200457bytearray_setslice_linear(PyByteArrayObject *self,
458 Py_ssize_t lo, Py_ssize_t hi,
459 char *bytes, Py_ssize_t bytes_len)
460{
461 Py_ssize_t avail = hi - lo;
462 char *buf = PyByteArray_AS_STRING(self);
463 Py_ssize_t growth = bytes_len - avail;
Victor Stinner84557232013-11-21 12:29:51 +0100464 int res = 0;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200465 assert(avail >= 0);
466
Victor Stinner84557232013-11-21 12:29:51 +0100467 if (growth < 0) {
468 if (!_canresize(self))
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200469 return -1;
Victor Stinner84557232013-11-21 12:29:51 +0100470
471 if (lo == 0) {
472 /* Shrink the buffer by advancing its logical start */
473 self->ob_start -= growth;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200474 /*
Victor Stinner84557232013-11-21 12:29:51 +0100475 0 lo hi old_size
476 | |<----avail----->|<-----tail------>|
477 | |<-bytes_len->|<-----tail------>|
478 0 new_lo new_hi new_size
479 */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200480 }
Victor Stinner84557232013-11-21 12:29:51 +0100481 else {
482 /*
483 0 lo hi old_size
484 | |<----avail----->|<-----tomove------>|
485 | |<-bytes_len->|<-----tomove------>|
486 0 lo new_hi new_size
487 */
488 memmove(buf + lo + bytes_len, buf + hi,
489 Py_SIZE(self) - hi);
490 }
491 if (PyByteArray_Resize((PyObject *)self,
492 Py_SIZE(self) + growth) < 0) {
493 /* Issue #19578: Handling the memory allocation failure here is
494 tricky here because the bytearray object has already been
495 modified. Depending on growth and lo, the behaviour is
496 different.
497
498 If growth < 0 and lo != 0, the operation is completed, but a
499 MemoryError is still raised and the memory block is not
500 shrinked. Otherwise, the bytearray is restored in its previous
501 state and a MemoryError is raised. */
502 if (lo == 0) {
503 self->ob_start += growth;
504 return -1;
505 }
506 /* memmove() removed bytes, the bytearray object cannot be
507 restored in its previous state. */
508 Py_SIZE(self) += growth;
509 res = -1;
510 }
511 buf = PyByteArray_AS_STRING(self);
512 }
513 else if (growth > 0) {
514 if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
515 PyErr_NoMemory();
516 return -1;
517 }
518
519 if (PyByteArray_Resize((PyObject *)self,
520 Py_SIZE(self) + growth) < 0) {
521 return -1;
522 }
523 buf = PyByteArray_AS_STRING(self);
524 /* Make the place for the additional bytes */
525 /*
526 0 lo hi old_size
527 | |<-avail->|<-----tomove------>|
528 | |<---bytes_len-->|<-----tomove------>|
529 0 lo new_hi new_size
530 */
531 memmove(buf + lo + bytes_len, buf + hi,
532 Py_SIZE(self) - lo - bytes_len);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200533 }
534
535 if (bytes_len > 0)
536 memcpy(buf + lo, bytes, bytes_len);
Victor Stinner84557232013-11-21 12:29:51 +0100537 return res;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200538}
539
540static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000541bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000542 PyObject *values)
543{
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200544 Py_ssize_t needed;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000545 void *bytes;
546 Py_buffer vbytes;
547 int res = 0;
548
549 vbytes.len = -1;
550 if (values == (PyObject *)self) {
551 /* Make a copy and call this function recursively */
552 int err;
553 values = PyByteArray_FromObject(values);
554 if (values == NULL)
555 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000556 err = bytearray_setslice(self, lo, hi, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000557 Py_DECREF(values);
558 return err;
559 }
560 if (values == NULL) {
561 /* del b[lo:hi] */
562 bytes = NULL;
563 needed = 0;
564 }
565 else {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200566 if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
567 PyErr_Format(PyExc_TypeError,
568 "can't set bytearray slice from %.100s",
569 Py_TYPE(values)->tp_name);
570 return -1;
571 }
572 needed = vbytes.len;
573 bytes = vbytes.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000574 }
575
576 if (lo < 0)
577 lo = 0;
578 if (hi < lo)
579 hi = lo;
580 if (hi > Py_SIZE(self))
581 hi = Py_SIZE(self);
582
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200583 res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000584 if (vbytes.len != -1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200585 PyBuffer_Release(&vbytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000586 return res;
587}
588
589static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000590bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000591{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000592 int ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000593
594 if (i < 0)
595 i += Py_SIZE(self);
596
597 if (i < 0 || i >= Py_SIZE(self)) {
598 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
599 return -1;
600 }
601
602 if (value == NULL)
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000603 return bytearray_setslice(self, i, i+1, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000604
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000605 if (!_getbytevalue(value, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000606 return -1;
607
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200608 PyByteArray_AS_STRING(self)[i] = ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000609 return 0;
610}
611
612static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000613bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000614{
615 Py_ssize_t start, stop, step, slicelen, needed;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200616 char *buf, *bytes;
617 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000618
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000619 if (PyIndex_Check(index)) {
620 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000621
622 if (i == -1 && PyErr_Occurred())
623 return -1;
624
625 if (i < 0)
626 i += PyByteArray_GET_SIZE(self);
627
628 if (i < 0 || i >= Py_SIZE(self)) {
629 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
630 return -1;
631 }
632
633 if (values == NULL) {
634 /* Fall through to slice assignment */
635 start = i;
636 stop = i + 1;
637 step = 1;
638 slicelen = 1;
639 }
640 else {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000641 int ival;
642 if (!_getbytevalue(values, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000643 return -1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200644 buf[i] = (char)ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000645 return 0;
646 }
647 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000648 else if (PySlice_Check(index)) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000649 if (PySlice_GetIndicesEx(index,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000650 PyByteArray_GET_SIZE(self),
651 &start, &stop, &step, &slicelen) < 0) {
652 return -1;
653 }
654 }
655 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400656 PyErr_Format(PyExc_TypeError,
657 "bytearray indices must be integers or slices, not %.200s",
658 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000659 return -1;
660 }
661
662 if (values == NULL) {
663 bytes = NULL;
664 needed = 0;
665 }
666 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
Christian Heimes6d26ade2012-11-03 23:07:59 +0100667 int err;
Ezio Melottic64bcbe2012-11-03 21:19:06 +0200668 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
669 PyErr_SetString(PyExc_TypeError,
670 "can assign only bytes, buffers, or iterables "
671 "of ints in range(0, 256)");
672 return -1;
673 }
Georg Brandlf3fa5682010-12-04 17:09:30 +0000674 /* Make a copy and call this function recursively */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000675 values = PyByteArray_FromObject(values);
676 if (values == NULL)
677 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000678 err = bytearray_ass_subscript(self, index, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000679 Py_DECREF(values);
680 return err;
681 }
682 else {
683 assert(PyByteArray_Check(values));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200684 bytes = PyByteArray_AS_STRING(values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000685 needed = Py_SIZE(values);
686 }
687 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
688 if ((step < 0 && start < stop) ||
689 (step > 0 && start > stop))
690 stop = start;
691 if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200692 return bytearray_setslice_linear(self, start, stop, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000693 }
694 else {
695 if (needed == 0) {
696 /* Delete slice */
Mark Dickinsonbc099642010-01-29 17:27:24 +0000697 size_t cur;
698 Py_ssize_t i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000699
Antoine Pitrou5504e892008-12-06 21:27:53 +0000700 if (!_canresize(self))
701 return -1;
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000702
703 if (slicelen == 0)
704 /* Nothing to do here. */
705 return 0;
706
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000707 if (step < 0) {
708 stop = start + 1;
709 start = stop + step * (slicelen - 1) - 1;
710 step = -step;
711 }
712 for (cur = start, i = 0;
713 i < slicelen; cur += step, i++) {
714 Py_ssize_t lim = step - 1;
715
Mark Dickinson66f575b2010-02-14 12:53:32 +0000716 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000717 lim = PyByteArray_GET_SIZE(self) - cur - 1;
718
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200719 memmove(buf + cur - i,
720 buf + cur + 1, lim);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000721 }
722 /* Move the tail of the bytes, in one chunk */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000723 cur = start + (size_t)slicelen*step;
Mark Dickinson66f575b2010-02-14 12:53:32 +0000724 if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200725 memmove(buf + cur - slicelen,
726 buf + cur,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000727 PyByteArray_GET_SIZE(self) - cur);
728 }
729 if (PyByteArray_Resize((PyObject *)self,
730 PyByteArray_GET_SIZE(self) - slicelen) < 0)
731 return -1;
732
733 return 0;
734 }
735 else {
736 /* Assign slice */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000737 Py_ssize_t i;
738 size_t cur;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000739
740 if (needed != slicelen) {
741 PyErr_Format(PyExc_ValueError,
742 "attempt to assign bytes of size %zd "
743 "to extended slice of size %zd",
744 needed, slicelen);
745 return -1;
746 }
747 for (cur = start, i = 0; i < slicelen; cur += step, i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200748 buf[cur] = bytes[i];
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000749 return 0;
750 }
751 }
752}
753
754static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000755bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000756{
757 static char *kwlist[] = {"source", "encoding", "errors", 0};
758 PyObject *arg = NULL;
759 const char *encoding = NULL;
760 const char *errors = NULL;
761 Py_ssize_t count;
762 PyObject *it;
763 PyObject *(*iternext)(PyObject *);
764
765 if (Py_SIZE(self) != 0) {
766 /* Empty previous contents (yes, do this first of all!) */
767 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
768 return -1;
769 }
770
771 /* Parse arguments */
Georg Brandl3dbca812008-07-23 16:10:53 +0000772 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000773 &arg, &encoding, &errors))
774 return -1;
775
776 /* Make a quick exit if no first argument */
777 if (arg == NULL) {
778 if (encoding != NULL || errors != NULL) {
779 PyErr_SetString(PyExc_TypeError,
780 "encoding or errors without sequence argument");
781 return -1;
782 }
783 return 0;
784 }
785
786 if (PyUnicode_Check(arg)) {
787 /* Encode via the codec registry */
788 PyObject *encoded, *new;
789 if (encoding == NULL) {
790 PyErr_SetString(PyExc_TypeError,
791 "string argument without an encoding");
792 return -1;
793 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000794 encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000795 if (encoded == NULL)
796 return -1;
797 assert(PyBytes_Check(encoded));
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000798 new = bytearray_iconcat(self, encoded);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000799 Py_DECREF(encoded);
800 if (new == NULL)
801 return -1;
802 Py_DECREF(new);
803 return 0;
804 }
805
806 /* If it's not unicode, there can't be encoding or errors */
807 if (encoding != NULL || errors != NULL) {
808 PyErr_SetString(PyExc_TypeError,
809 "encoding or errors without a string argument");
810 return -1;
811 }
812
813 /* Is it an int? */
Benjamin Peterson8380dd52010-04-16 22:51:37 +0000814 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
815 if (count == -1 && PyErr_Occurred()) {
816 if (PyErr_ExceptionMatches(PyExc_OverflowError))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000817 return -1;
Benjamin Peterson9c0e94f2010-04-16 23:00:53 +0000818 PyErr_Clear();
Benjamin Peterson8380dd52010-04-16 22:51:37 +0000819 }
820 else if (count < 0) {
821 PyErr_SetString(PyExc_ValueError, "negative count");
822 return -1;
823 }
824 else {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000825 if (count > 0) {
Victor Stinner2bc4d952014-06-02 22:22:42 +0200826 if (PyByteArray_Resize((PyObject *)self, count))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000827 return -1;
Victor Stinner2bc4d952014-06-02 22:22:42 +0200828 memset(PyByteArray_AS_STRING(self), 0, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000829 }
830 return 0;
831 }
832
833 /* Use the buffer API */
834 if (PyObject_CheckBuffer(arg)) {
835 Py_ssize_t size;
836 Py_buffer view;
837 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
838 return -1;
839 size = view.len;
840 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200841 if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
842 &view, size, 'C') < 0)
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200843 goto fail;
Martin v. Löwis423be952008-08-13 15:53:07 +0000844 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000845 return 0;
846 fail:
Martin v. Löwis423be952008-08-13 15:53:07 +0000847 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000848 return -1;
849 }
850
851 /* XXX Optimize this if the arguments is a list, tuple */
852
853 /* Get the iterator */
854 it = PyObject_GetIter(arg);
855 if (it == NULL)
856 return -1;
857 iternext = *Py_TYPE(it)->tp_iternext;
858
859 /* Run the iterator to exhaustion */
860 for (;;) {
861 PyObject *item;
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000862 int rc, value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000863
864 /* Get the next item */
865 item = iternext(it);
866 if (item == NULL) {
867 if (PyErr_Occurred()) {
868 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
869 goto error;
870 PyErr_Clear();
871 }
872 break;
873 }
874
875 /* Interpret it as an int (__index__) */
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000876 rc = _getbytevalue(item, &value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000877 Py_DECREF(item);
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000878 if (!rc)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000879 goto error;
880
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000881 /* Append the byte */
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300882 if (Py_SIZE(self) + 1 < self->ob_alloc) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000883 Py_SIZE(self)++;
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300884 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
885 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000886 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
887 goto error;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200888 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000889 }
890
891 /* Clean up and return success */
892 Py_DECREF(it);
893 return 0;
894
895 error:
896 /* Error handling when it != NULL */
897 Py_DECREF(it);
898 return -1;
899}
900
901/* Mostly copied from string_repr, but without the
902 "smart quote" functionality. */
903static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000904bytearray_repr(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000905{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000906 const char *quote_prefix = "bytearray(b";
907 const char *quote_postfix = ")";
908 Py_ssize_t length = Py_SIZE(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200909 /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
Mark Dickinson66f575b2010-02-14 12:53:32 +0000910 size_t newsize;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000911 PyObject *v;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200912 Py_ssize_t i;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200913 char *bytes;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200914 char c;
915 char *p;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200916 int quote;
917 char *test, *start;
918 char *buffer;
919
920 if (length > (PY_SSIZE_T_MAX - 15) / 4) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000921 PyErr_SetString(PyExc_OverflowError,
922 "bytearray object is too large to make repr");
923 return NULL;
924 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200925
926 newsize = 15 + length * 4;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100927 buffer = PyObject_Malloc(newsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200928 if (buffer == NULL) {
929 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000930 return NULL;
931 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000932
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200933 /* Figure out which quote to use; single is preferred */
934 quote = '\'';
935 start = PyByteArray_AS_STRING(self);
936 for (test = start; test < start+length; ++test) {
937 if (*test == '"') {
938 quote = '\''; /* back to single */
939 break;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000940 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200941 else if (*test == '\'')
942 quote = '"';
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000943 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200944
945 p = buffer;
946 while (*quote_prefix)
947 *p++ = *quote_prefix++;
948 *p++ = quote;
949
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200950 bytes = PyByteArray_AS_STRING(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200951 for (i = 0; i < length; i++) {
952 /* There's at least enough room for a hex escape
953 and a closing quote. */
954 assert(newsize - (p - buffer) >= 5);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200955 c = bytes[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200956 if (c == '\'' || c == '\\')
957 *p++ = '\\', *p++ = c;
958 else if (c == '\t')
959 *p++ = '\\', *p++ = 't';
960 else if (c == '\n')
961 *p++ = '\\', *p++ = 'n';
962 else if (c == '\r')
963 *p++ = '\\', *p++ = 'r';
964 else if (c == 0)
965 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
966 else if (c < ' ' || c >= 0x7f) {
967 *p++ = '\\';
968 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200969 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
970 *p++ = Py_hexdigits[c & 0xf];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200971 }
972 else
973 *p++ = c;
974 }
975 assert(newsize - (p - buffer) >= 1);
976 *p++ = quote;
977 while (*quote_postfix) {
978 *p++ = *quote_postfix++;
979 }
980
981 v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100982 PyObject_Free(buffer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200983 return v;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000984}
985
986static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000987bytearray_str(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000988{
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000989 if (Py_BytesWarningFlag) {
990 if (PyErr_WarnEx(PyExc_BytesWarning,
991 "str() on a bytearray instance", 1))
992 return NULL;
993 }
994 return bytearray_repr((PyByteArrayObject*)op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000995}
996
997static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000998bytearray_richcompare(PyObject *self, PyObject *other, int op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000999{
1000 Py_ssize_t self_size, other_size;
1001 Py_buffer self_bytes, other_bytes;
1002 PyObject *res;
1003 Py_ssize_t minsize;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001004 int cmp, rc;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001005
1006 /* Bytes can be compared to anything that supports the (binary)
1007 buffer API. Except that a comparison with Unicode is always an
1008 error, even if the comparison is for equality. */
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001009 rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
1010 if (!rc)
1011 rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
1012 if (rc < 0)
1013 return NULL;
1014 if (rc) {
Barry Warsaw9e9dcd62008-10-17 01:50:37 +00001015 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001016 if (PyErr_WarnEx(PyExc_BytesWarning,
Georg Brandle5d68ac2008-06-04 11:30:26 +00001017 "Comparison between bytearray and string", 1))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001018 return NULL;
1019 }
1020
Brian Curtindfc80e32011-08-10 20:28:54 -05001021 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001022 }
1023
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001024 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001025 PyErr_Clear();
Brian Curtindfc80e32011-08-10 20:28:54 -05001026 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001027 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001028 self_size = self_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001029
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001030 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001031 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00001032 PyBuffer_Release(&self_bytes);
Brian Curtindfc80e32011-08-10 20:28:54 -05001033 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001034 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001035 other_size = other_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001036
1037 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1038 /* Shortcut: if the lengths differ, the objects differ */
1039 cmp = (op == Py_NE);
1040 }
1041 else {
1042 minsize = self_size;
1043 if (other_size < minsize)
1044 minsize = other_size;
1045
1046 cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
1047 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1048
1049 if (cmp == 0) {
1050 if (self_size < other_size)
1051 cmp = -1;
1052 else if (self_size > other_size)
1053 cmp = 1;
1054 }
1055
1056 switch (op) {
1057 case Py_LT: cmp = cmp < 0; break;
1058 case Py_LE: cmp = cmp <= 0; break;
1059 case Py_EQ: cmp = cmp == 0; break;
1060 case Py_NE: cmp = cmp != 0; break;
1061 case Py_GT: cmp = cmp > 0; break;
1062 case Py_GE: cmp = cmp >= 0; break;
1063 }
1064 }
1065
1066 res = cmp ? Py_True : Py_False;
Martin v. Löwis423be952008-08-13 15:53:07 +00001067 PyBuffer_Release(&self_bytes);
1068 PyBuffer_Release(&other_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001069 Py_INCREF(res);
1070 return res;
1071}
1072
1073static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001074bytearray_dealloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001075{
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001076 if (self->ob_exports > 0) {
1077 PyErr_SetString(PyExc_SystemError,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001078 "deallocated bytearray object has exported buffers");
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001079 PyErr_Print();
1080 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001081 if (self->ob_bytes != 0) {
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001082 PyObject_Free(self->ob_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001083 }
1084 Py_TYPE(self)->tp_free((PyObject *)self);
1085}
1086
1087
1088/* -------------------------------------------------------------------- */
1089/* Methods */
1090
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001091#define FASTSEARCH fastsearch
1092#define STRINGLIB(F) stringlib_##F
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001093#define STRINGLIB_CHAR char
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001094#define STRINGLIB_SIZEOF_CHAR 1
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001095#define STRINGLIB_LEN PyByteArray_GET_SIZE
1096#define STRINGLIB_STR PyByteArray_AS_STRING
1097#define STRINGLIB_NEW PyByteArray_FromStringAndSize
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001098#define STRINGLIB_ISSPACE Py_ISSPACE
1099#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001100#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1101#define STRINGLIB_MUTABLE 1
1102
1103#include "stringlib/fastsearch.h"
1104#include "stringlib/count.h"
1105#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001106#include "stringlib/join.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001107#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001108#include "stringlib/split.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001109#include "stringlib/ctype.h"
1110#include "stringlib/transmogrify.h"
1111
1112
1113/* The following Py_LOCAL_INLINE and Py_LOCAL functions
1114were copied from the old char* style string object. */
1115
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001116/* helper macro to fixup start/end slice values */
1117#define ADJUST_INDICES(start, end, len) \
1118 if (end > len) \
1119 end = len; \
1120 else if (end < 0) { \
1121 end += len; \
1122 if (end < 0) \
1123 end = 0; \
1124 } \
1125 if (start < 0) { \
1126 start += len; \
1127 if (start < 0) \
1128 start = 0; \
1129 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001130
1131Py_LOCAL_INLINE(Py_ssize_t)
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001132bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001133{
1134 PyObject *subobj;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001135 char byte;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001136 Py_buffer subbuf;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001137 const char *sub;
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001138 Py_ssize_t len, sub_len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001139 Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
1140 Py_ssize_t res;
1141
Antoine Pitrouac65d962011-10-20 23:54:17 +02001142 if (!stringlib_parse_args_finds_byte("find/rfind/index/rindex",
1143 args, &subobj, &byte, &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001144 return -2;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001145
1146 if (subobj) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001147 if (PyObject_GetBuffer(subobj, &subbuf, PyBUF_SIMPLE) != 0)
Antoine Pitrouac65d962011-10-20 23:54:17 +02001148 return -2;
1149
1150 sub = subbuf.buf;
1151 sub_len = subbuf.len;
1152 }
1153 else {
1154 sub = &byte;
1155 sub_len = 1;
1156 }
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001157 len = PyByteArray_GET_SIZE(self);
Antoine Pitrouac65d962011-10-20 23:54:17 +02001158
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001159 ADJUST_INDICES(start, end, len);
1160 if (end - start < sub_len)
1161 res = -1;
Serhiy Storchaka413fdce2015-11-14 15:42:17 +02001162 else if (sub_len == 1) {
1163 if (dir > 0)
1164 res = stringlib_find_char(
1165 PyByteArray_AS_STRING(self) + start, end - start,
1166 *sub);
1167 else
1168 res = stringlib_rfind_char(
1169 PyByteArray_AS_STRING(self) + start, end - start,
1170 *sub);
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001171 if (res >= 0)
1172 res += start;
1173 }
1174 else {
1175 if (dir > 0)
1176 res = stringlib_find_slice(
1177 PyByteArray_AS_STRING(self), len,
1178 sub, sub_len, start, end);
1179 else
1180 res = stringlib_rfind_slice(
1181 PyByteArray_AS_STRING(self), len,
1182 sub, sub_len, start, end);
1183 }
Antoine Pitrouac65d962011-10-20 23:54:17 +02001184
1185 if (subobj)
1186 PyBuffer_Release(&subbuf);
1187
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001188 return res;
1189}
1190
1191PyDoc_STRVAR(find__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001192"B.find(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001193\n\
1194Return the lowest index in B where subsection sub is found,\n\
Senthil Kumaran53516a82011-07-27 23:33:54 +08001195such that sub is contained within B[start,end]. Optional\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001196arguments start and end are interpreted as in slice notation.\n\
1197\n\
1198Return -1 on failure.");
1199
1200static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001201bytearray_find(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001202{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001203 Py_ssize_t result = bytearray_find_internal(self, args, +1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001204 if (result == -2)
1205 return NULL;
1206 return PyLong_FromSsize_t(result);
1207}
1208
1209PyDoc_STRVAR(count__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001210"B.count(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001211\n\
1212Return the number of non-overlapping occurrences of subsection sub in\n\
1213bytes B[start:end]. Optional arguments start and end are interpreted\n\
1214as in slice notation.");
1215
1216static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001217bytearray_count(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001218{
1219 PyObject *sub_obj;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001220 const char *str = PyByteArray_AS_STRING(self), *sub;
1221 Py_ssize_t sub_len;
1222 char byte;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001223 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001224
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001225 Py_buffer vsub;
1226 PyObject *count_obj;
1227
Antoine Pitrouac65d962011-10-20 23:54:17 +02001228 if (!stringlib_parse_args_finds_byte("count", args, &sub_obj, &byte,
1229 &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001230 return NULL;
1231
Antoine Pitrouac65d962011-10-20 23:54:17 +02001232 if (sub_obj) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001233 if (PyObject_GetBuffer(sub_obj, &vsub, PyBUF_SIMPLE) != 0)
Antoine Pitrouac65d962011-10-20 23:54:17 +02001234 return NULL;
1235
1236 sub = vsub.buf;
1237 sub_len = vsub.len;
1238 }
1239 else {
1240 sub = &byte;
1241 sub_len = 1;
1242 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001243
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001244 ADJUST_INDICES(start, end, PyByteArray_GET_SIZE(self));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001245
1246 count_obj = PyLong_FromSsize_t(
Antoine Pitrouac65d962011-10-20 23:54:17 +02001247 stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001248 );
Antoine Pitrouac65d962011-10-20 23:54:17 +02001249
1250 if (sub_obj)
1251 PyBuffer_Release(&vsub);
1252
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001253 return count_obj;
1254}
1255
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001256/*[clinic input]
1257bytearray.clear
1258
1259 self: self(type="PyByteArrayObject *")
1260
1261Remove all items from the bytearray.
1262[clinic start generated code]*/
1263
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001264static PyObject *
1265bytearray_clear_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001266/*[clinic end generated code: output=85c2fe6aede0956c input=e524fd330abcdc18]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001267{
1268 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1269 return NULL;
1270 Py_RETURN_NONE;
1271}
1272
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001273/*[clinic input]
1274bytearray.copy
1275
1276 self: self(type="PyByteArrayObject *")
1277
1278Return a copy of B.
1279[clinic start generated code]*/
1280
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001281static PyObject *
1282bytearray_copy_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001283/*[clinic end generated code: output=68cfbcfed484c132 input=6d5d2975aa0f33f3]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001284{
1285 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1286 PyByteArray_GET_SIZE(self));
1287}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001288
1289PyDoc_STRVAR(index__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001290"B.index(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001291\n\
1292Like B.find() but raise ValueError when the subsection is not found.");
1293
1294static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001295bytearray_index(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001296{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001297 Py_ssize_t result = bytearray_find_internal(self, args, +1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001298 if (result == -2)
1299 return NULL;
1300 if (result == -1) {
1301 PyErr_SetString(PyExc_ValueError,
1302 "subsection not found");
1303 return NULL;
1304 }
1305 return PyLong_FromSsize_t(result);
1306}
1307
1308
1309PyDoc_STRVAR(rfind__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001310"B.rfind(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001311\n\
1312Return the highest index in B where subsection sub is found,\n\
Senthil Kumaran53516a82011-07-27 23:33:54 +08001313such that sub is contained within B[start,end]. Optional\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001314arguments start and end are interpreted as in slice notation.\n\
1315\n\
1316Return -1 on failure.");
1317
1318static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001319bytearray_rfind(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001320{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001321 Py_ssize_t result = bytearray_find_internal(self, args, -1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001322 if (result == -2)
1323 return NULL;
1324 return PyLong_FromSsize_t(result);
1325}
1326
1327
1328PyDoc_STRVAR(rindex__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001329"B.rindex(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001330\n\
1331Like B.rfind() but raise ValueError when the subsection is not found.");
1332
1333static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001334bytearray_rindex(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001335{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001336 Py_ssize_t result = bytearray_find_internal(self, args, -1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001337 if (result == -2)
1338 return NULL;
1339 if (result == -1) {
1340 PyErr_SetString(PyExc_ValueError,
1341 "subsection not found");
1342 return NULL;
1343 }
1344 return PyLong_FromSsize_t(result);
1345}
1346
1347
1348static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001349bytearray_contains(PyObject *self, PyObject *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001350{
1351 Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError);
1352 if (ival == -1 && PyErr_Occurred()) {
1353 Py_buffer varg;
Antoine Pitrou0010d372010-08-15 17:12:55 +00001354 Py_ssize_t pos;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001355 PyErr_Clear();
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001356 if (PyObject_GetBuffer(arg, &varg, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001357 return -1;
1358 pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self),
1359 varg.buf, varg.len, 0);
Martin v. Löwis423be952008-08-13 15:53:07 +00001360 PyBuffer_Release(&varg);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001361 return pos >= 0;
1362 }
1363 if (ival < 0 || ival >= 256) {
1364 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
1365 return -1;
1366 }
1367
Antoine Pitrou0010d372010-08-15 17:12:55 +00001368 return memchr(PyByteArray_AS_STRING(self), (int) ival, Py_SIZE(self)) != NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001369}
1370
1371
1372/* Matches the end (direction >= 0) or start (direction < 0) of self
1373 * against substr, using the start and end arguments. Returns
1374 * -1 on error, 0 if not found and 1 if found.
1375 */
1376Py_LOCAL(int)
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001377_bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001378 Py_ssize_t end, int direction)
1379{
1380 Py_ssize_t len = PyByteArray_GET_SIZE(self);
1381 const char* str;
1382 Py_buffer vsubstr;
1383 int rv = 0;
1384
1385 str = PyByteArray_AS_STRING(self);
1386
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001387 if (PyObject_GetBuffer(substr, &vsubstr, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001388 return -1;
1389
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001390 ADJUST_INDICES(start, end, len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001391
1392 if (direction < 0) {
1393 /* startswith */
1394 if (start+vsubstr.len > len) {
1395 goto done;
1396 }
1397 } else {
1398 /* endswith */
1399 if (end-start < vsubstr.len || start > len) {
1400 goto done;
1401 }
1402
1403 if (end-vsubstr.len > start)
1404 start = end - vsubstr.len;
1405 }
1406 if (end-start >= vsubstr.len)
1407 rv = ! memcmp(str+start, vsubstr.buf, vsubstr.len);
1408
1409done:
Martin v. Löwis423be952008-08-13 15:53:07 +00001410 PyBuffer_Release(&vsubstr);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001411 return rv;
1412}
1413
1414
1415PyDoc_STRVAR(startswith__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001416"B.startswith(prefix[, start[, end]]) -> bool\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001417\n\
1418Return True if B starts with the specified prefix, False otherwise.\n\
1419With optional start, test B beginning at that position.\n\
1420With optional end, stop comparing B at that position.\n\
Ezio Melottiba42fd52011-04-26 06:09:45 +03001421prefix can also be a tuple of bytes to try.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001422
1423static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001424bytearray_startswith(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001425{
1426 Py_ssize_t start = 0;
1427 Py_ssize_t end = PY_SSIZE_T_MAX;
1428 PyObject *subobj;
1429 int result;
1430
Jesus Ceaac451502011-04-20 17:09:23 +02001431 if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001432 return NULL;
1433 if (PyTuple_Check(subobj)) {
1434 Py_ssize_t i;
1435 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001436 result = _bytearray_tailmatch(self,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001437 PyTuple_GET_ITEM(subobj, i),
1438 start, end, -1);
1439 if (result == -1)
1440 return NULL;
1441 else if (result) {
1442 Py_RETURN_TRUE;
1443 }
1444 }
1445 Py_RETURN_FALSE;
1446 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001447 result = _bytearray_tailmatch(self, subobj, start, end, -1);
Ezio Melottiba42fd52011-04-26 06:09:45 +03001448 if (result == -1) {
1449 if (PyErr_ExceptionMatches(PyExc_TypeError))
1450 PyErr_Format(PyExc_TypeError, "startswith first arg must be bytes "
1451 "or a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001452 return NULL;
Ezio Melottiba42fd52011-04-26 06:09:45 +03001453 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001454 else
1455 return PyBool_FromLong(result);
1456}
1457
1458PyDoc_STRVAR(endswith__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001459"B.endswith(suffix[, start[, end]]) -> bool\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001460\n\
1461Return True if B ends with the specified suffix, False otherwise.\n\
1462With optional start, test B beginning at that position.\n\
1463With optional end, stop comparing B at that position.\n\
Ezio Melottiba42fd52011-04-26 06:09:45 +03001464suffix can also be a tuple of bytes to try.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001465
1466static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001467bytearray_endswith(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001468{
1469 Py_ssize_t start = 0;
1470 Py_ssize_t end = PY_SSIZE_T_MAX;
1471 PyObject *subobj;
1472 int result;
1473
Jesus Ceaac451502011-04-20 17:09:23 +02001474 if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001475 return NULL;
1476 if (PyTuple_Check(subobj)) {
1477 Py_ssize_t i;
1478 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001479 result = _bytearray_tailmatch(self,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001480 PyTuple_GET_ITEM(subobj, i),
1481 start, end, +1);
1482 if (result == -1)
1483 return NULL;
1484 else if (result) {
1485 Py_RETURN_TRUE;
1486 }
1487 }
1488 Py_RETURN_FALSE;
1489 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001490 result = _bytearray_tailmatch(self, subobj, start, end, +1);
Ezio Melottiba42fd52011-04-26 06:09:45 +03001491 if (result == -1) {
1492 if (PyErr_ExceptionMatches(PyExc_TypeError))
1493 PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or "
1494 "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001495 return NULL;
Ezio Melottiba42fd52011-04-26 06:09:45 +03001496 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001497 else
1498 return PyBool_FromLong(result);
1499}
1500
1501
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001502/*[clinic input]
1503bytearray.translate
1504
1505 self: self(type="PyByteArrayObject *")
1506 table: object
1507 Translation table, which must be a bytes object of length 256.
1508 [
1509 deletechars: object
1510 ]
1511 /
1512
1513Return a copy with each character mapped by the given translation table.
1514
1515All characters occurring in the optional argument deletechars are removed.
1516The remaining characters are mapped through the given translation table.
1517[clinic start generated code]*/
1518
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001519static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001520bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
1521 int group_right_1, PyObject *deletechars)
1522/*[clinic end generated code: output=2bebc86a9a1ff083 input=b749ad85f4860824]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001523{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001524 char *input, *output;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001525 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001526 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001527 PyObject *input_obj = (PyObject*)self;
1528 const char *output_start;
1529 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001530 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001531 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001532 Py_buffer vtable, vdel;
1533
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001534 if (table == Py_None) {
1535 table_chars = NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001536 table = NULL;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001537 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001538 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001539 } else {
1540 if (vtable.len != 256) {
1541 PyErr_SetString(PyExc_ValueError,
1542 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001543 PyBuffer_Release(&vtable);
1544 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001545 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001546 table_chars = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001547 }
1548
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001549 if (deletechars != NULL) {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001550 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001551 if (table != NULL)
Georg Brandl953152f2009-07-22 12:03:59 +00001552 PyBuffer_Release(&vtable);
1553 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001554 }
1555 }
1556 else {
1557 vdel.buf = NULL;
1558 vdel.len = 0;
1559 }
1560
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001561 inlen = PyByteArray_GET_SIZE(input_obj);
1562 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1563 if (result == NULL)
1564 goto done;
1565 output_start = output = PyByteArray_AsString(result);
1566 input = PyByteArray_AS_STRING(input_obj);
1567
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001568 if (vdel.len == 0 && table_chars != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001569 /* If no deletions are required, use faster code */
1570 for (i = inlen; --i >= 0; ) {
1571 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001572 *output++ = table_chars[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001573 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001574 goto done;
1575 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001576
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001577 if (table_chars == NULL) {
Georg Brandlccc47b62008-12-28 11:44:14 +00001578 for (i = 0; i < 256; i++)
1579 trans_table[i] = Py_CHARMASK(i);
1580 } else {
1581 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001582 trans_table[i] = Py_CHARMASK(table_chars[i]);
Georg Brandlccc47b62008-12-28 11:44:14 +00001583 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001584
1585 for (i = 0; i < vdel.len; i++)
1586 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1587
1588 for (i = inlen; --i >= 0; ) {
1589 c = Py_CHARMASK(*input++);
1590 if (trans_table[c] != -1)
1591 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1592 continue;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001593 }
1594 /* Fix the size of the resulting string */
1595 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001596 if (PyByteArray_Resize(result, output - output_start) < 0) {
1597 Py_CLEAR(result);
1598 goto done;
1599 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001600
1601done:
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001602 if (table != NULL)
Georg Brandlccc47b62008-12-28 11:44:14 +00001603 PyBuffer_Release(&vtable);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001604 if (deletechars != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001605 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001606 return result;
1607}
1608
1609
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001610/*[clinic input]
1611
1612@staticmethod
1613bytearray.maketrans
1614
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001615 frm: Py_buffer
1616 to: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001617 /
1618
1619Return a translation table useable for the bytes or bytearray translate method.
1620
1621The returned table will be one where each byte in frm is mapped to the byte at
1622the same position in to.
1623
1624The bytes objects frm and to must be of the same length.
1625[clinic start generated code]*/
1626
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001627static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001628bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001629/*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001630{
1631 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00001632}
1633
1634
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001635/* find and count characters and substrings */
1636
1637#define findchar(target, target_len, c) \
1638 ((char *)memchr((const void *)(target), c, target_len))
1639
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001640
Benjamin Peterson0f3641c2008-11-19 22:05:52 +00001641/* Bytes ops must return a string, create a copy */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001642Py_LOCAL(PyByteArrayObject *)
1643return_self(PyByteArrayObject *self)
1644{
Georg Brandl1e7217d2008-05-30 12:02:38 +00001645 /* always return a new bytearray */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001646 return (PyByteArrayObject *)PyByteArray_FromStringAndSize(
1647 PyByteArray_AS_STRING(self),
1648 PyByteArray_GET_SIZE(self));
1649}
1650
1651Py_LOCAL_INLINE(Py_ssize_t)
1652countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount)
1653{
1654 Py_ssize_t count=0;
1655 const char *start=target;
1656 const char *end=target+target_len;
1657
1658 while ( (start=findchar(start, end-start, c)) != NULL ) {
1659 count++;
1660 if (count >= maxcount)
1661 break;
1662 start += 1;
1663 }
1664 return count;
1665}
1666
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001667
1668/* Algorithms for different cases of string replacement */
1669
1670/* len(self)>=1, from="", len(to)>=1, maxcount>=1 */
1671Py_LOCAL(PyByteArrayObject *)
1672replace_interleave(PyByteArrayObject *self,
1673 const char *to_s, Py_ssize_t to_len,
1674 Py_ssize_t maxcount)
1675{
1676 char *self_s, *result_s;
1677 Py_ssize_t self_len, result_len;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001678 Py_ssize_t count, i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001679 PyByteArrayObject *result;
1680
1681 self_len = PyByteArray_GET_SIZE(self);
1682
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001683 /* 1 at the end plus 1 after every character;
1684 count = min(maxcount, self_len + 1) */
1685 if (maxcount <= self_len)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001686 count = maxcount;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001687 else
1688 /* Can't overflow: self_len + 1 <= maxcount <= PY_SSIZE_T_MAX. */
1689 count = self_len + 1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001690
1691 /* Check for overflow */
1692 /* result_len = count * to_len + self_len; */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001693 assert(count > 0);
1694 if (to_len > (PY_SSIZE_T_MAX - self_len) / count) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001695 PyErr_SetString(PyExc_OverflowError,
1696 "replace string is too long");
1697 return NULL;
1698 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001699 result_len = count * to_len + self_len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001700
1701 if (! (result = (PyByteArrayObject *)
1702 PyByteArray_FromStringAndSize(NULL, result_len)) )
1703 return NULL;
1704
1705 self_s = PyByteArray_AS_STRING(self);
1706 result_s = PyByteArray_AS_STRING(result);
1707
1708 /* TODO: special case single character, which doesn't need memcpy */
1709
1710 /* Lay the first one down (guaranteed this will occur) */
1711 Py_MEMCPY(result_s, to_s, to_len);
1712 result_s += to_len;
1713 count -= 1;
1714
1715 for (i=0; i<count; i++) {
1716 *result_s++ = *self_s++;
1717 Py_MEMCPY(result_s, to_s, to_len);
1718 result_s += to_len;
1719 }
1720
1721 /* Copy the rest of the original string */
1722 Py_MEMCPY(result_s, self_s, self_len-i);
1723
1724 return result;
1725}
1726
1727/* Special case for deleting a single character */
1728/* len(self)>=1, len(from)==1, to="", maxcount>=1 */
1729Py_LOCAL(PyByteArrayObject *)
1730replace_delete_single_character(PyByteArrayObject *self,
1731 char from_c, Py_ssize_t maxcount)
1732{
1733 char *self_s, *result_s;
1734 char *start, *next, *end;
1735 Py_ssize_t self_len, result_len;
1736 Py_ssize_t count;
1737 PyByteArrayObject *result;
1738
1739 self_len = PyByteArray_GET_SIZE(self);
1740 self_s = PyByteArray_AS_STRING(self);
1741
1742 count = countchar(self_s, self_len, from_c, maxcount);
1743 if (count == 0) {
1744 return return_self(self);
1745 }
1746
1747 result_len = self_len - count; /* from_len == 1 */
1748 assert(result_len>=0);
1749
1750 if ( (result = (PyByteArrayObject *)
1751 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1752 return NULL;
1753 result_s = PyByteArray_AS_STRING(result);
1754
1755 start = self_s;
1756 end = self_s + self_len;
1757 while (count-- > 0) {
1758 next = findchar(start, end-start, from_c);
1759 if (next == NULL)
1760 break;
1761 Py_MEMCPY(result_s, start, next-start);
1762 result_s += (next-start);
1763 start = next+1;
1764 }
1765 Py_MEMCPY(result_s, start, end-start);
1766
1767 return result;
1768}
1769
1770/* len(self)>=1, len(from)>=2, to="", maxcount>=1 */
1771
1772Py_LOCAL(PyByteArrayObject *)
1773replace_delete_substring(PyByteArrayObject *self,
1774 const char *from_s, Py_ssize_t from_len,
1775 Py_ssize_t maxcount)
1776{
1777 char *self_s, *result_s;
1778 char *start, *next, *end;
1779 Py_ssize_t self_len, result_len;
1780 Py_ssize_t count, offset;
1781 PyByteArrayObject *result;
1782
1783 self_len = PyByteArray_GET_SIZE(self);
1784 self_s = PyByteArray_AS_STRING(self);
1785
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001786 count = stringlib_count(self_s, self_len,
1787 from_s, from_len,
1788 maxcount);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001789
1790 if (count == 0) {
1791 /* no matches */
1792 return return_self(self);
1793 }
1794
1795 result_len = self_len - (count * from_len);
1796 assert (result_len>=0);
1797
1798 if ( (result = (PyByteArrayObject *)
1799 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL )
1800 return NULL;
1801
1802 result_s = PyByteArray_AS_STRING(result);
1803
1804 start = self_s;
1805 end = self_s + self_len;
1806 while (count-- > 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001807 offset = stringlib_find(start, end-start,
1808 from_s, from_len,
1809 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001810 if (offset == -1)
1811 break;
1812 next = start + offset;
1813
1814 Py_MEMCPY(result_s, start, next-start);
1815
1816 result_s += (next-start);
1817 start = next+from_len;
1818 }
1819 Py_MEMCPY(result_s, start, end-start);
1820 return result;
1821}
1822
1823/* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */
1824Py_LOCAL(PyByteArrayObject *)
1825replace_single_character_in_place(PyByteArrayObject *self,
1826 char from_c, char to_c,
1827 Py_ssize_t maxcount)
1828{
Antoine Pitroud1188562010-06-09 16:38:55 +00001829 char *self_s, *result_s, *start, *end, *next;
1830 Py_ssize_t self_len;
1831 PyByteArrayObject *result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001832
Antoine Pitroud1188562010-06-09 16:38:55 +00001833 /* The result string will be the same size */
1834 self_s = PyByteArray_AS_STRING(self);
1835 self_len = PyByteArray_GET_SIZE(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001836
Antoine Pitroud1188562010-06-09 16:38:55 +00001837 next = findchar(self_s, self_len, from_c);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001838
Antoine Pitroud1188562010-06-09 16:38:55 +00001839 if (next == NULL) {
1840 /* No matches; return the original bytes */
1841 return return_self(self);
1842 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001843
Antoine Pitroud1188562010-06-09 16:38:55 +00001844 /* Need to make a new bytes */
1845 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len);
1846 if (result == NULL)
1847 return NULL;
1848 result_s = PyByteArray_AS_STRING(result);
1849 Py_MEMCPY(result_s, self_s, self_len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001850
Antoine Pitroud1188562010-06-09 16:38:55 +00001851 /* change everything in-place, starting with this one */
1852 start = result_s + (next-self_s);
1853 *start = to_c;
1854 start++;
1855 end = result_s + self_len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001856
Antoine Pitroud1188562010-06-09 16:38:55 +00001857 while (--maxcount > 0) {
1858 next = findchar(start, end-start, from_c);
1859 if (next == NULL)
1860 break;
1861 *next = to_c;
1862 start = next+1;
1863 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001864
Antoine Pitroud1188562010-06-09 16:38:55 +00001865 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001866}
1867
1868/* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */
1869Py_LOCAL(PyByteArrayObject *)
1870replace_substring_in_place(PyByteArrayObject *self,
1871 const char *from_s, Py_ssize_t from_len,
1872 const char *to_s, Py_ssize_t to_len,
1873 Py_ssize_t maxcount)
1874{
1875 char *result_s, *start, *end;
1876 char *self_s;
1877 Py_ssize_t self_len, offset;
1878 PyByteArrayObject *result;
1879
1880 /* The result bytes will be the same size */
1881
1882 self_s = PyByteArray_AS_STRING(self);
1883 self_len = PyByteArray_GET_SIZE(self);
1884
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001885 offset = stringlib_find(self_s, self_len,
1886 from_s, from_len,
1887 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001888 if (offset == -1) {
1889 /* No matches; return the original bytes */
1890 return return_self(self);
1891 }
1892
1893 /* Need to make a new bytes */
1894 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len);
1895 if (result == NULL)
1896 return NULL;
1897 result_s = PyByteArray_AS_STRING(result);
1898 Py_MEMCPY(result_s, self_s, self_len);
1899
1900 /* change everything in-place, starting with this one */
1901 start = result_s + offset;
1902 Py_MEMCPY(start, to_s, from_len);
1903 start += from_len;
1904 end = result_s + self_len;
1905
1906 while ( --maxcount > 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001907 offset = stringlib_find(start, end-start,
1908 from_s, from_len,
1909 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001910 if (offset==-1)
1911 break;
1912 Py_MEMCPY(start+offset, to_s, from_len);
1913 start += offset+from_len;
1914 }
1915
1916 return result;
1917}
1918
1919/* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */
1920Py_LOCAL(PyByteArrayObject *)
1921replace_single_character(PyByteArrayObject *self,
1922 char from_c,
1923 const char *to_s, Py_ssize_t to_len,
1924 Py_ssize_t maxcount)
1925{
1926 char *self_s, *result_s;
1927 char *start, *next, *end;
1928 Py_ssize_t self_len, result_len;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001929 Py_ssize_t count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001930 PyByteArrayObject *result;
1931
1932 self_s = PyByteArray_AS_STRING(self);
1933 self_len = PyByteArray_GET_SIZE(self);
1934
1935 count = countchar(self_s, self_len, from_c, maxcount);
1936 if (count == 0) {
1937 /* no matches, return unchanged */
1938 return return_self(self);
1939 }
1940
1941 /* use the difference between current and new, hence the "-1" */
1942 /* result_len = self_len + count * (to_len-1) */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001943 assert(count > 0);
1944 if (to_len - 1 > (PY_SSIZE_T_MAX - self_len) / count) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001945 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
1946 return NULL;
1947 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001948 result_len = self_len + count * (to_len - 1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001949
1950 if ( (result = (PyByteArrayObject *)
1951 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1952 return NULL;
1953 result_s = PyByteArray_AS_STRING(result);
1954
1955 start = self_s;
1956 end = self_s + self_len;
1957 while (count-- > 0) {
1958 next = findchar(start, end-start, from_c);
1959 if (next == NULL)
1960 break;
1961
1962 if (next == start) {
1963 /* replace with the 'to' */
1964 Py_MEMCPY(result_s, to_s, to_len);
1965 result_s += to_len;
1966 start += 1;
1967 } else {
1968 /* copy the unchanged old then the 'to' */
1969 Py_MEMCPY(result_s, start, next-start);
1970 result_s += (next-start);
1971 Py_MEMCPY(result_s, to_s, to_len);
1972 result_s += to_len;
1973 start = next+1;
1974 }
1975 }
1976 /* Copy the remainder of the remaining bytes */
1977 Py_MEMCPY(result_s, start, end-start);
1978
1979 return result;
1980}
1981
1982/* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */
1983Py_LOCAL(PyByteArrayObject *)
1984replace_substring(PyByteArrayObject *self,
1985 const char *from_s, Py_ssize_t from_len,
1986 const char *to_s, Py_ssize_t to_len,
1987 Py_ssize_t maxcount)
1988{
1989 char *self_s, *result_s;
1990 char *start, *next, *end;
1991 Py_ssize_t self_len, result_len;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001992 Py_ssize_t count, offset;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001993 PyByteArrayObject *result;
1994
1995 self_s = PyByteArray_AS_STRING(self);
1996 self_len = PyByteArray_GET_SIZE(self);
1997
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001998 count = stringlib_count(self_s, self_len,
1999 from_s, from_len,
2000 maxcount);
2001
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002002 if (count == 0) {
2003 /* no matches, return unchanged */
2004 return return_self(self);
2005 }
2006
2007 /* Check for overflow */
2008 /* result_len = self_len + count * (to_len-from_len) */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00002009 assert(count > 0);
2010 if (to_len - from_len > (PY_SSIZE_T_MAX - self_len) / count) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002011 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
2012 return NULL;
2013 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00002014 result_len = self_len + count * (to_len - from_len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002015
2016 if ( (result = (PyByteArrayObject *)
2017 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
2018 return NULL;
2019 result_s = PyByteArray_AS_STRING(result);
2020
2021 start = self_s;
2022 end = self_s + self_len;
2023 while (count-- > 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002024 offset = stringlib_find(start, end-start,
2025 from_s, from_len,
2026 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002027 if (offset == -1)
2028 break;
2029 next = start+offset;
2030 if (next == start) {
2031 /* replace with the 'to' */
2032 Py_MEMCPY(result_s, to_s, to_len);
2033 result_s += to_len;
2034 start += from_len;
2035 } else {
2036 /* copy the unchanged old then the 'to' */
2037 Py_MEMCPY(result_s, start, next-start);
2038 result_s += (next-start);
2039 Py_MEMCPY(result_s, to_s, to_len);
2040 result_s += to_len;
2041 start = next+from_len;
2042 }
2043 }
2044 /* Copy the remainder of the remaining bytes */
2045 Py_MEMCPY(result_s, start, end-start);
2046
2047 return result;
2048}
2049
2050
2051Py_LOCAL(PyByteArrayObject *)
2052replace(PyByteArrayObject *self,
2053 const char *from_s, Py_ssize_t from_len,
2054 const char *to_s, Py_ssize_t to_len,
2055 Py_ssize_t maxcount)
2056{
2057 if (maxcount < 0) {
2058 maxcount = PY_SSIZE_T_MAX;
2059 } else if (maxcount == 0 || PyByteArray_GET_SIZE(self) == 0) {
2060 /* nothing to do; return the original bytes */
2061 return return_self(self);
2062 }
2063
2064 if (maxcount == 0 ||
2065 (from_len == 0 && to_len == 0)) {
2066 /* nothing to do; return the original bytes */
2067 return return_self(self);
2068 }
2069
2070 /* Handle zero-length special cases */
2071
2072 if (from_len == 0) {
2073 /* insert the 'to' bytes everywhere. */
2074 /* >>> "Python".replace("", ".") */
2075 /* '.P.y.t.h.o.n.' */
2076 return replace_interleave(self, to_s, to_len, maxcount);
2077 }
2078
2079 /* Except for "".replace("", "A") == "A" there is no way beyond this */
2080 /* point for an empty self bytes to generate a non-empty bytes */
2081 /* Special case so the remaining code always gets a non-empty bytes */
2082 if (PyByteArray_GET_SIZE(self) == 0) {
2083 return return_self(self);
2084 }
2085
2086 if (to_len == 0) {
Georg Brandl17cb8a82008-05-30 08:20:09 +00002087 /* delete all occurrences of 'from' bytes */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002088 if (from_len == 1) {
2089 return replace_delete_single_character(
2090 self, from_s[0], maxcount);
2091 } else {
2092 return replace_delete_substring(self, from_s, from_len, maxcount);
2093 }
2094 }
2095
2096 /* Handle special case where both bytes have the same length */
2097
2098 if (from_len == to_len) {
2099 if (from_len == 1) {
2100 return replace_single_character_in_place(
2101 self,
2102 from_s[0],
2103 to_s[0],
2104 maxcount);
2105 } else {
2106 return replace_substring_in_place(
2107 self, from_s, from_len, to_s, to_len, maxcount);
2108 }
2109 }
2110
2111 /* Otherwise use the more generic algorithms */
2112 if (from_len == 1) {
2113 return replace_single_character(self, from_s[0],
2114 to_s, to_len, maxcount);
2115 } else {
2116 /* len('from')>=2, len('to')>=1 */
2117 return replace_substring(self, from_s, from_len, to_s, to_len, maxcount);
2118 }
2119}
2120
2121
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002122/*[clinic input]
2123bytearray.replace
2124
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002125 old: Py_buffer
2126 new: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002127 count: Py_ssize_t = -1
2128 Maximum number of occurrences to replace.
2129 -1 (the default value) means replace all occurrences.
2130 /
2131
2132Return a copy with all occurrences of substring old replaced by new.
2133
2134If the optional argument count is given, only the first count occurrences are
2135replaced.
2136[clinic start generated code]*/
2137
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002138static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04002139bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
2140 Py_buffer *new, Py_ssize_t count)
2141/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002142{
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002143 return (PyObject *)replace((PyByteArrayObject *) self,
2144 old->buf, old->len,
2145 new->buf, new->len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002146}
2147
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002148/*[clinic input]
2149bytearray.split
2150
2151 sep: object = None
2152 The delimiter according which to split the bytearray.
2153 None (the default value) means split on ASCII whitespace characters
2154 (space, tab, return, newline, formfeed, vertical tab).
2155 maxsplit: Py_ssize_t = -1
2156 Maximum number of splits to do.
2157 -1 (the default value) means no limit.
2158
2159Return a list of the sections in the bytearray, using sep as the delimiter.
2160[clinic start generated code]*/
2161
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002162static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04002163bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
2164 Py_ssize_t maxsplit)
2165/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002166{
2167 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002168 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002169 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002170 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002171
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002172 if (maxsplit < 0)
2173 maxsplit = PY_SSIZE_T_MAX;
2174
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002175 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002176 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002177
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002178 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002179 return NULL;
2180 sub = vsub.buf;
2181 n = vsub.len;
2182
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002183 list = stringlib_split(
2184 (PyObject*) self, s, len, sub, n, maxsplit
2185 );
Martin v. Löwis423be952008-08-13 15:53:07 +00002186 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002187 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002188}
2189
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002190/*[clinic input]
2191bytearray.partition
2192
2193 self: self(type="PyByteArrayObject *")
2194 sep: object
2195 /
2196
2197Partition the bytearray into three parts using the given separator.
2198
2199This will search for the separator sep in the bytearray. If the separator is
2200found, returns a 3-tuple containing the part before the separator, the
2201separator itself, and the part after it.
2202
2203If the separator is not found, returns a 3-tuple containing the original
2204bytearray object and two empty bytearray objects.
2205[clinic start generated code]*/
2206
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002207static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002208bytearray_partition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002209/*[clinic end generated code: output=45d2525ddd35f957 input=7d7fe37b1696d506]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002210{
2211 PyObject *bytesep, *result;
2212
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002213 bytesep = PyByteArray_FromObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002214 if (! bytesep)
2215 return NULL;
2216
2217 result = stringlib_partition(
2218 (PyObject*) self,
2219 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2220 bytesep,
2221 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2222 );
2223
2224 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002225 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002226}
2227
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002228/*[clinic input]
2229bytearray.rpartition
2230
2231 self: self(type="PyByteArrayObject *")
2232 sep: object
2233 /
2234
2235Partition the bytes into three parts using the given separator.
2236
2237This will search for the separator sep in the bytearray, starting and the end.
2238If the separator is found, returns a 3-tuple containing the part before the
2239separator, the separator itself, and the part after it.
2240
2241If the separator is not found, returns a 3-tuple containing two empty bytearray
2242objects and the original bytearray object.
2243[clinic start generated code]*/
2244
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002245static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002246bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002247/*[clinic end generated code: output=440de3c9426115e8 input=9b8cd540c1b75853]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002248{
2249 PyObject *bytesep, *result;
2250
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002251 bytesep = PyByteArray_FromObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002252 if (! bytesep)
2253 return NULL;
2254
2255 result = stringlib_rpartition(
2256 (PyObject*) self,
2257 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2258 bytesep,
2259 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2260 );
2261
2262 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002263 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002264}
2265
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002266/*[clinic input]
2267bytearray.rsplit = bytearray.split
2268
2269Return a list of the sections in the bytearray, using sep as the delimiter.
2270
2271Splitting is done starting at the end of the bytearray and working to the front.
2272[clinic start generated code]*/
2273
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002274static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04002275bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
2276 Py_ssize_t maxsplit)
2277/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002278{
2279 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002280 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002281 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002282 Py_buffer vsub;
2283
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002284 if (maxsplit < 0)
2285 maxsplit = PY_SSIZE_T_MAX;
2286
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002287 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002288 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002289
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002290 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002291 return NULL;
2292 sub = vsub.buf;
2293 n = vsub.len;
2294
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002295 list = stringlib_rsplit(
2296 (PyObject*) self, s, len, sub, n, maxsplit
2297 );
Martin v. Löwis423be952008-08-13 15:53:07 +00002298 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002299 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002300}
2301
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002302/*[clinic input]
2303bytearray.reverse
2304
2305 self: self(type="PyByteArrayObject *")
2306
2307Reverse the order of the values in B in place.
2308[clinic start generated code]*/
2309
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002310static PyObject *
2311bytearray_reverse_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002312/*[clinic end generated code: output=9f7616f29ab309d3 input=7933a499b8597bd1]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002313{
2314 char swap, *head, *tail;
2315 Py_ssize_t i, j, n = Py_SIZE(self);
2316
2317 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002318 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002319 tail = head + n - 1;
2320 for (i = 0; i < j; i++) {
2321 swap = *head;
2322 *head++ = *tail;
2323 *tail-- = swap;
2324 }
2325
2326 Py_RETURN_NONE;
2327}
2328
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002329
2330/*[python input]
2331class bytesvalue_converter(CConverter):
2332 type = 'int'
2333 converter = '_getbytevalue'
2334[python start generated code]*/
2335/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
2336
2337
2338/*[clinic input]
2339bytearray.insert
2340
2341 self: self(type="PyByteArrayObject *")
2342 index: Py_ssize_t
2343 The index where the value is to be inserted.
2344 item: bytesvalue
2345 The item to be inserted.
2346 /
2347
2348Insert a single item into the bytearray before the given index.
2349[clinic start generated code]*/
2350
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002351static PyObject *
2352bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002353/*[clinic end generated code: output=76c775a70e7b07b7 input=833766836ba30e1e]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002354{
2355 Py_ssize_t n = Py_SIZE(self);
2356 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002357
2358 if (n == PY_SSIZE_T_MAX) {
2359 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00002360 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002361 return NULL;
2362 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002363 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2364 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002365 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002366
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002367 if (index < 0) {
2368 index += n;
2369 if (index < 0)
2370 index = 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002371 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002372 if (index > n)
2373 index = n;
2374 memmove(buf + index + 1, buf + index, n - index);
2375 buf[index] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002376
2377 Py_RETURN_NONE;
2378}
2379
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002380/*[clinic input]
2381bytearray.append
2382
2383 self: self(type="PyByteArrayObject *")
2384 item: bytesvalue
2385 The item to be appended.
2386 /
2387
2388Append a single item to the end of the bytearray.
2389[clinic start generated code]*/
2390
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002391static PyObject *
2392bytearray_append_impl(PyByteArrayObject *self, int item)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002393/*[clinic end generated code: output=a154e19ed1886cb6 input=ae56ea87380407cc]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002394{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002395 Py_ssize_t n = Py_SIZE(self);
2396
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002397 if (n == PY_SSIZE_T_MAX) {
2398 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00002399 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002400 return NULL;
2401 }
2402 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2403 return NULL;
2404
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002405 PyByteArray_AS_STRING(self)[n] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002406
2407 Py_RETURN_NONE;
2408}
2409
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002410/*[clinic input]
2411bytearray.extend
2412
2413 self: self(type="PyByteArrayObject *")
2414 iterable_of_ints: object
2415 The iterable of items to append.
2416 /
2417
2418Append all the items from the iterator or sequence to the end of the bytearray.
2419[clinic start generated code]*/
2420
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002421static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002422bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002423/*[clinic end generated code: output=98155dbe249170b1 input=ce83a5d75b70d850]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002424{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002425 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002426 Py_ssize_t buf_size = 0, len = 0;
2427 int value;
2428 char *buf;
2429
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002430 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002431 if (PyObject_CheckBuffer(iterable_of_ints)) {
2432 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002433 return NULL;
2434
2435 Py_RETURN_NONE;
2436 }
2437
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002438 it = PyObject_GetIter(iterable_of_ints);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002439 if (it == NULL)
2440 return NULL;
2441
Ezio Melotti42da6632011-03-15 05:18:48 +02002442 /* Try to determine the length of the argument. 32 is arbitrary. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002443 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002444 if (buf_size == -1) {
2445 Py_DECREF(it);
2446 return NULL;
2447 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002448
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002449 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002450 if (bytearray_obj == NULL) {
2451 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002452 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002453 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002454 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002455
2456 while ((item = PyIter_Next(it)) != NULL) {
2457 if (! _getbytevalue(item, &value)) {
2458 Py_DECREF(item);
2459 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002460 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002461 return NULL;
2462 }
2463 buf[len++] = value;
2464 Py_DECREF(item);
2465
2466 if (len >= buf_size) {
2467 buf_size = len + (len >> 1) + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002468 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002469 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002470 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002471 return NULL;
2472 }
2473 /* Recompute the `buf' pointer, since the resizing operation may
2474 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002475 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002476 }
2477 }
2478 Py_DECREF(it);
2479
2480 /* Resize down to exact size. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002481 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
2482 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002483 return NULL;
2484 }
2485
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002486 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
2487 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002488 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002489 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002490 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002491
2492 Py_RETURN_NONE;
2493}
2494
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002495/*[clinic input]
2496bytearray.pop
2497
2498 self: self(type="PyByteArrayObject *")
2499 index: Py_ssize_t = -1
2500 The index from where to remove the item.
2501 -1 (the default value) means remove the last item.
2502 /
2503
2504Remove and return a single item from B.
2505
2506If no index argument is given, will pop the last item.
2507[clinic start generated code]*/
2508
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002509static PyObject *
2510bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002511/*[clinic end generated code: output=e0ccd401f8021da8 input=0797e6c0ca9d5a85]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002512{
2513 int value;
2514 Py_ssize_t n = Py_SIZE(self);
2515 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002516
2517 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00002518 PyErr_SetString(PyExc_IndexError,
2519 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002520 return NULL;
2521 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002522 if (index < 0)
2523 index += Py_SIZE(self);
2524 if (index < 0 || index >= Py_SIZE(self)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002525 PyErr_SetString(PyExc_IndexError, "pop index out of range");
2526 return NULL;
2527 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00002528 if (!_canresize(self))
2529 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002530
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002531 buf = PyByteArray_AS_STRING(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002532 value = buf[index];
2533 memmove(buf + index, buf + index + 1, n - index);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002534 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2535 return NULL;
2536
Mark Dickinson54a3db92009-09-06 10:19:23 +00002537 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002538}
2539
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002540/*[clinic input]
2541bytearray.remove
2542
2543 self: self(type="PyByteArrayObject *")
2544 value: bytesvalue
2545 The value to remove.
2546 /
2547
2548Remove the first occurrence of a value in the bytearray.
2549[clinic start generated code]*/
2550
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002551static PyObject *
2552bytearray_remove_impl(PyByteArrayObject *self, int value)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002553/*[clinic end generated code: output=d659e37866709c13 input=47560b11fd856c24]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002554{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002555 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002556 char *buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002557
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002558 for (where = 0; where < n; where++) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002559 if (buf[where] == value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002560 break;
2561 }
2562 if (where == n) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00002563 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002564 return NULL;
2565 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00002566 if (!_canresize(self))
2567 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002568
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002569 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002570 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2571 return NULL;
2572
2573 Py_RETURN_NONE;
2574}
2575
2576/* XXX These two helpers could be optimized if argsize == 1 */
2577
2578static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002579lstrip_helper(const char *myptr, Py_ssize_t mysize,
2580 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002581{
2582 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02002583 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002584 i++;
2585 return i;
2586}
2587
2588static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002589rstrip_helper(const char *myptr, Py_ssize_t mysize,
2590 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002591{
2592 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02002593 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002594 i--;
2595 return i + 1;
2596}
2597
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002598/*[clinic input]
2599bytearray.strip
2600
2601 bytes: object = None
2602 /
2603
2604Strip leading and trailing bytes contained in the argument.
2605
2606If the argument is omitted or None, strip leading and trailing ASCII whitespace.
2607[clinic start generated code]*/
2608
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002609static PyObject *
2610bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002611/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002612{
2613 Py_ssize_t left, right, mysize, byteslen;
2614 char *myptr, *bytesptr;
2615 Py_buffer vbytes;
2616
2617 if (bytes == Py_None) {
2618 bytesptr = "\t\n\r\f\v ";
2619 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002620 }
2621 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002622 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002623 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002624 bytesptr = (char *) vbytes.buf;
2625 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002626 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002627 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002628 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002629 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002630 if (left == mysize)
2631 right = left;
2632 else
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002633 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
2634 if (bytes != Py_None)
2635 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002636 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002637}
2638
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002639/*[clinic input]
2640bytearray.lstrip
2641
2642 bytes: object = None
2643 /
2644
2645Strip leading bytes contained in the argument.
2646
2647If the argument is omitted or None, strip leading ASCII whitespace.
2648[clinic start generated code]*/
2649
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002650static PyObject *
2651bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002652/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002653{
2654 Py_ssize_t left, right, mysize, byteslen;
2655 char *myptr, *bytesptr;
2656 Py_buffer vbytes;
2657
2658 if (bytes == Py_None) {
2659 bytesptr = "\t\n\r\f\v ";
2660 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002661 }
2662 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002663 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002664 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002665 bytesptr = (char *) vbytes.buf;
2666 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002667 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002668 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002669 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002670 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002671 right = mysize;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002672 if (bytes != Py_None)
2673 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002674 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002675}
2676
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002677/*[clinic input]
2678bytearray.rstrip
2679
2680 bytes: object = None
2681 /
2682
2683Strip trailing bytes contained in the argument.
2684
2685If the argument is omitted or None, strip trailing ASCII whitespace.
2686[clinic start generated code]*/
2687
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002688static PyObject *
2689bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002690/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002691{
2692 Py_ssize_t right, mysize, byteslen;
2693 char *myptr, *bytesptr;
2694 Py_buffer vbytes;
2695
2696 if (bytes == Py_None) {
2697 bytesptr = "\t\n\r\f\v ";
2698 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002699 }
2700 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002701 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002702 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002703 bytesptr = (char *) vbytes.buf;
2704 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002705 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002706 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002707 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002708 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
2709 if (bytes != Py_None)
2710 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002711 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002712}
2713
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002714/*[clinic input]
2715bytearray.decode
2716
2717 encoding: str(c_default="NULL") = 'utf-8'
2718 The encoding with which to decode the bytearray.
2719 errors: str(c_default="NULL") = 'strict'
2720 The error handling scheme to use for the handling of decoding errors.
2721 The default is 'strict' meaning that decoding errors raise a
2722 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
2723 as well as any other name registered with codecs.register_error that
2724 can handle UnicodeDecodeErrors.
2725
2726Decode the bytearray using the codec registered for encoding.
2727[clinic start generated code]*/
2728
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002729static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04002730bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
2731 const char *errors)
2732/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002733{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002734 if (encoding == NULL)
2735 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis0efea322014-07-27 17:29:17 +02002736 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002737}
2738
2739PyDoc_STRVAR(alloc_doc,
2740"B.__alloc__() -> int\n\
2741\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00002742Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002743
2744static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002745bytearray_alloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002746{
2747 return PyLong_FromSsize_t(self->ob_alloc);
2748}
2749
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002750/*[clinic input]
2751bytearray.join
2752
2753 iterable_of_bytes: object
2754 /
2755
2756Concatenate any number of bytes/bytearray objects.
2757
2758The bytearray whose method is called is inserted in between each pair.
2759
2760The result is returned as a new bytearray object.
2761[clinic start generated code]*/
2762
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002763static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002764bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002765/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002766{
Martin v. Löwis0efea322014-07-27 17:29:17 +02002767 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002768}
2769
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002770/*[clinic input]
2771bytearray.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002772
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +03002773 keepends: int(c_default="0") = False
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002774
2775Return a list of the lines in the bytearray, breaking at line boundaries.
2776
2777Line breaks are not included in the resulting list unless keepends is given and
2778true.
2779[clinic start generated code]*/
2780
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002781static PyObject *
2782bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +03002783/*[clinic end generated code: output=4223c94b895f6ad9 input=8ccade941e5ea0bd]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002784{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002785 return stringlib_splitlines(
2786 (PyObject*) self, PyByteArray_AS_STRING(self),
2787 PyByteArray_GET_SIZE(self), keepends
2788 );
2789}
2790
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002791/*[clinic input]
2792@classmethod
2793bytearray.fromhex
2794
2795 cls: self(type="PyObject*")
2796 string: unicode
2797 /
2798
2799Create a bytearray object from a string of hexadecimal numbers.
2800
2801Spaces between two numbers are accepted.
2802Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
2803[clinic start generated code]*/
2804
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002805static PyObject *
2806bytearray_fromhex_impl(PyObject*cls, PyObject *string)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002807/*[clinic end generated code: output=df3da60129b3700c input=907bbd2d34d9367a]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002808{
Victor Stinner2bf89932015-10-14 11:25:33 +02002809 return _PyBytes_FromHex(string, 1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002810}
2811
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002812PyDoc_STRVAR(hex__doc__,
2813"B.hex() -> string\n\
2814\n\
2815Create a string of hexadecimal numbers from a bytearray object.\n\
2816Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'.");
2817
2818static PyObject *
2819bytearray_hex(PyBytesObject *self)
2820{
2821 char* argbuf = PyByteArray_AS_STRING(self);
2822 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
2823 return _Py_strhex(argbuf, arglen);
2824}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002825
2826static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002827_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002828{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002829 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002830 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002831 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002832
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002833 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002834 if (dict == NULL) {
2835 PyErr_Clear();
2836 dict = Py_None;
2837 Py_INCREF(dict);
2838 }
2839
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002840 buf = PyByteArray_AS_STRING(self);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002841 if (proto < 3) {
2842 /* use str based reduction for backwards compatibility with Python 2.x */
2843 PyObject *latin1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002844 if (Py_SIZE(self))
2845 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002846 else
2847 latin1 = PyUnicode_FromString("");
2848 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2849 }
2850 else {
2851 /* use more efficient byte based reduction */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002852 if (Py_SIZE(self)) {
2853 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002854 }
2855 else {
2856 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2857 }
2858 }
2859}
2860
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002861/*[clinic input]
2862bytearray.__reduce__ as bytearray_reduce
2863
2864 self: self(type="PyByteArrayObject *")
2865
2866Return state information for pickling.
2867[clinic start generated code]*/
2868
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002869static PyObject *
2870bytearray_reduce_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002871/*[clinic end generated code: output=52bf304086464cab input=fbb07de4d102a03a]*/
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002872{
2873 return _common_reduce(self, 2);
2874}
2875
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002876/*[clinic input]
2877bytearray.__reduce_ex__ as bytearray_reduce_ex
2878
2879 self: self(type="PyByteArrayObject *")
2880 proto: int = 0
2881 /
2882
2883Return state information for pickling.
2884[clinic start generated code]*/
2885
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002886static PyObject *
2887bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002888/*[clinic end generated code: output=52eac33377197520 input=0e091a42ca6dbd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002889{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002890 return _common_reduce(self, proto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002891}
2892
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002893/*[clinic input]
2894bytearray.__sizeof__ as bytearray_sizeof
2895
2896 self: self(type="PyByteArrayObject *")
2897
2898Returns the size of the bytearray object in memory, in bytes.
2899[clinic start generated code]*/
2900
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002901static PyObject *
2902bytearray_sizeof_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002903/*[clinic end generated code: output=738abdd17951c427 input=6b23d305362b462b]*/
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002904{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002905 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002906
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002907 res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002908 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002909}
2910
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002911static PySequenceMethods bytearray_as_sequence = {
2912 (lenfunc)bytearray_length, /* sq_length */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002913 (binaryfunc)PyByteArray_Concat, /* sq_concat */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002914 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2915 (ssizeargfunc)bytearray_getitem, /* sq_item */
2916 0, /* sq_slice */
2917 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2918 0, /* sq_ass_slice */
2919 (objobjproc)bytearray_contains, /* sq_contains */
2920 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2921 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002922};
2923
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002924static PyMappingMethods bytearray_as_mapping = {
2925 (lenfunc)bytearray_length,
2926 (binaryfunc)bytearray_subscript,
2927 (objobjargproc)bytearray_ass_subscript,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002928};
2929
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002930static PyBufferProcs bytearray_as_buffer = {
2931 (getbufferproc)bytearray_getbuffer,
2932 (releasebufferproc)bytearray_releasebuffer,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002933};
2934
2935static PyMethodDef
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002936bytearray_methods[] = {
2937 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002938 BYTEARRAY_REDUCE_METHODDEF
2939 BYTEARRAY_REDUCE_EX_METHODDEF
2940 BYTEARRAY_SIZEOF_METHODDEF
2941 BYTEARRAY_APPEND_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002942 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
2943 _Py_capitalize__doc__},
2944 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002945 BYTEARRAY_CLEAR_METHODDEF
2946 BYTEARRAY_COPY_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002947 {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002948 BYTEARRAY_DECODE_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002949 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__},
Ezio Melotti745d54d2013-11-16 19:10:57 +02002950 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002951 expandtabs__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002952 BYTEARRAY_EXTEND_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002953 {"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002954 BYTEARRAY_FROMHEX_METHODDEF
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002955 {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__},
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002956 {"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002957 BYTEARRAY_INSERT_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002958 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
2959 _Py_isalnum__doc__},
2960 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
2961 _Py_isalpha__doc__},
2962 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
2963 _Py_isdigit__doc__},
2964 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
2965 _Py_islower__doc__},
2966 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
2967 _Py_isspace__doc__},
2968 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
2969 _Py_istitle__doc__},
2970 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
2971 _Py_isupper__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002972 BYTEARRAY_JOIN_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002973 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__},
2974 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002975 BYTEARRAY_LSTRIP_METHODDEF
2976 BYTEARRAY_MAKETRANS_METHODDEF
2977 BYTEARRAY_PARTITION_METHODDEF
2978 BYTEARRAY_POP_METHODDEF
2979 BYTEARRAY_REMOVE_METHODDEF
2980 BYTEARRAY_REPLACE_METHODDEF
2981 BYTEARRAY_REVERSE_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002982 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__},
2983 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002984 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002985 BYTEARRAY_RPARTITION_METHODDEF
2986 BYTEARRAY_RSPLIT_METHODDEF
2987 BYTEARRAY_RSTRIP_METHODDEF
2988 BYTEARRAY_SPLIT_METHODDEF
2989 BYTEARRAY_SPLITLINES_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002990 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002991 startswith__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002992 BYTEARRAY_STRIP_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002993 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
2994 _Py_swapcase__doc__},
2995 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002996 BYTEARRAY_TRANSLATE_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002997 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
2998 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
2999 {NULL}
3000};
3001
Ethan Furmanb95b5612015-01-23 20:05:18 -08003002static PyObject *
3003bytearray_mod(PyObject *v, PyObject *w)
3004{
3005 if (!PyByteArray_Check(v))
3006 Py_RETURN_NOTIMPLEMENTED;
3007 return bytearray_format((PyByteArrayObject *)v, w);
3008}
3009
3010static PyNumberMethods bytearray_as_number = {
3011 0, /*nb_add*/
3012 0, /*nb_subtract*/
3013 0, /*nb_multiply*/
3014 bytearray_mod, /*nb_remainder*/
3015};
3016
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003017PyDoc_STRVAR(bytearray_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00003018"bytearray(iterable_of_ints) -> bytearray\n\
3019bytearray(string, encoding[, errors]) -> bytearray\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01003020bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
3021bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
3022bytearray() -> empty bytes array\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003023\n\
3024Construct an mutable bytearray object from:\n\
3025 - an iterable yielding integers in range(256)\n\
3026 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01003027 - a bytes or a buffer object\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003028 - any object implementing the buffer API.\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01003029 - an integer");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003030
3031
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003032static PyObject *bytearray_iter(PyObject *seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003033
3034PyTypeObject PyByteArray_Type = {
3035 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3036 "bytearray",
3037 sizeof(PyByteArrayObject),
3038 0,
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003039 (destructor)bytearray_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003040 0, /* tp_print */
3041 0, /* tp_getattr */
3042 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00003043 0, /* tp_reserved */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003044 (reprfunc)bytearray_repr, /* tp_repr */
Ethan Furmanb95b5612015-01-23 20:05:18 -08003045 &bytearray_as_number, /* tp_as_number */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003046 &bytearray_as_sequence, /* tp_as_sequence */
3047 &bytearray_as_mapping, /* tp_as_mapping */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003048 0, /* tp_hash */
3049 0, /* tp_call */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003050 bytearray_str, /* tp_str */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003051 PyObject_GenericGetAttr, /* tp_getattro */
3052 0, /* tp_setattro */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003053 &bytearray_as_buffer, /* tp_as_buffer */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003054 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003055 bytearray_doc, /* tp_doc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003056 0, /* tp_traverse */
3057 0, /* tp_clear */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003058 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003059 0, /* tp_weaklistoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003060 bytearray_iter, /* tp_iter */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003061 0, /* tp_iternext */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003062 bytearray_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003063 0, /* tp_members */
3064 0, /* tp_getset */
3065 0, /* tp_base */
3066 0, /* tp_dict */
3067 0, /* tp_descr_get */
3068 0, /* tp_descr_set */
3069 0, /* tp_dictoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003070 (initproc)bytearray_init, /* tp_init */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003071 PyType_GenericAlloc, /* tp_alloc */
3072 PyType_GenericNew, /* tp_new */
3073 PyObject_Del, /* tp_free */
3074};
3075
3076/*********************** Bytes Iterator ****************************/
3077
3078typedef struct {
3079 PyObject_HEAD
3080 Py_ssize_t it_index;
3081 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
3082} bytesiterobject;
3083
3084static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003085bytearrayiter_dealloc(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003086{
3087 _PyObject_GC_UNTRACK(it);
3088 Py_XDECREF(it->it_seq);
3089 PyObject_GC_Del(it);
3090}
3091
3092static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003093bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003094{
3095 Py_VISIT(it->it_seq);
3096 return 0;
3097}
3098
3099static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003100bytearrayiter_next(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003101{
3102 PyByteArrayObject *seq;
3103 PyObject *item;
3104
3105 assert(it != NULL);
3106 seq = it->it_seq;
3107 if (seq == NULL)
3108 return NULL;
3109 assert(PyByteArray_Check(seq));
3110
3111 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
3112 item = PyLong_FromLong(
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02003113 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003114 if (item != NULL)
3115 ++it->it_index;
3116 return item;
3117 }
3118
3119 Py_DECREF(seq);
3120 it->it_seq = NULL;
3121 return NULL;
3122}
3123
3124static PyObject *
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003125bytearrayiter_length_hint(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003126{
3127 Py_ssize_t len = 0;
3128 if (it->it_seq)
3129 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
3130 return PyLong_FromSsize_t(len);
3131}
3132
3133PyDoc_STRVAR(length_hint_doc,
3134 "Private method returning an estimate of len(list(it)).");
3135
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003136static PyObject *
3137bytearrayiter_reduce(bytesiterobject *it)
3138{
3139 if (it->it_seq != NULL) {
Antoine Pitroua7013882012-04-05 00:04:20 +02003140 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003141 it->it_seq, it->it_index);
3142 } else {
3143 PyObject *u = PyUnicode_FromUnicode(NULL, 0);
3144 if (u == NULL)
3145 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +02003146 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003147 }
3148}
3149
3150static PyObject *
3151bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
3152{
3153 Py_ssize_t index = PyLong_AsSsize_t(state);
3154 if (index == -1 && PyErr_Occurred())
3155 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003156 if (it->it_seq != NULL) {
3157 if (index < 0)
3158 index = 0;
3159 else if (index > PyByteArray_GET_SIZE(it->it_seq))
3160 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
3161 it->it_index = index;
3162 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003163 Py_RETURN_NONE;
3164}
3165
3166PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
3167
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003168static PyMethodDef bytearrayiter_methods[] = {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003169 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003170 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003171 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003172 bytearray_reduce__doc__},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003173 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
3174 setstate_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003175 {NULL, NULL} /* sentinel */
3176};
3177
3178PyTypeObject PyByteArrayIter_Type = {
3179 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3180 "bytearray_iterator", /* tp_name */
3181 sizeof(bytesiterobject), /* tp_basicsize */
3182 0, /* tp_itemsize */
3183 /* methods */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003184 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003185 0, /* tp_print */
3186 0, /* tp_getattr */
3187 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00003188 0, /* tp_reserved */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003189 0, /* tp_repr */
3190 0, /* tp_as_number */
3191 0, /* tp_as_sequence */
3192 0, /* tp_as_mapping */
3193 0, /* tp_hash */
3194 0, /* tp_call */
3195 0, /* tp_str */
3196 PyObject_GenericGetAttr, /* tp_getattro */
3197 0, /* tp_setattro */
3198 0, /* tp_as_buffer */
3199 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
3200 0, /* tp_doc */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003201 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003202 0, /* tp_clear */
3203 0, /* tp_richcompare */
3204 0, /* tp_weaklistoffset */
3205 PyObject_SelfIter, /* tp_iter */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003206 (iternextfunc)bytearrayiter_next, /* tp_iternext */
3207 bytearrayiter_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003208 0,
3209};
3210
3211static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003212bytearray_iter(PyObject *seq)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003213{
3214 bytesiterobject *it;
3215
3216 if (!PyByteArray_Check(seq)) {
3217 PyErr_BadInternalCall();
3218 return NULL;
3219 }
3220 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
3221 if (it == NULL)
3222 return NULL;
3223 it->it_index = 0;
3224 Py_INCREF(seq);
3225 it->it_seq = (PyByteArrayObject *)seq;
3226 _PyObject_GC_TRACK(it);
3227 return (PyObject *)it;
3228}