blob: 21031479b8b8b96c7c27d48f9f82f93651e8575f [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 Storchakad92d4ef2015-07-20 22:58:02 +03001162 else if (sub_len == 1
1163#ifndef HAVE_MEMRCHR
1164 && dir > 0
1165#endif
1166 ) {
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001167 unsigned char needle = *sub;
Serhiy Storchakad92d4ef2015-07-20 22:58:02 +03001168 int mode = (dir > 0) ? FAST_SEARCH : FAST_RSEARCH;
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001169 res = stringlib_fastsearch_memchr_1char(
1170 PyByteArray_AS_STRING(self) + start, end - start,
Serhiy Storchakad92d4ef2015-07-20 22:58:02 +03001171 needle, needle, mode);
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001172 if (res >= 0)
1173 res += start;
1174 }
1175 else {
1176 if (dir > 0)
1177 res = stringlib_find_slice(
1178 PyByteArray_AS_STRING(self), len,
1179 sub, sub_len, start, end);
1180 else
1181 res = stringlib_rfind_slice(
1182 PyByteArray_AS_STRING(self), len,
1183 sub, sub_len, start, end);
1184 }
Antoine Pitrouac65d962011-10-20 23:54:17 +02001185
1186 if (subobj)
1187 PyBuffer_Release(&subbuf);
1188
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001189 return res;
1190}
1191
1192PyDoc_STRVAR(find__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001193"B.find(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001194\n\
1195Return the lowest index in B where subsection sub is found,\n\
Senthil Kumaran53516a82011-07-27 23:33:54 +08001196such that sub is contained within B[start,end]. Optional\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001197arguments start and end are interpreted as in slice notation.\n\
1198\n\
1199Return -1 on failure.");
1200
1201static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001202bytearray_find(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001203{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001204 Py_ssize_t result = bytearray_find_internal(self, args, +1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001205 if (result == -2)
1206 return NULL;
1207 return PyLong_FromSsize_t(result);
1208}
1209
1210PyDoc_STRVAR(count__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001211"B.count(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001212\n\
1213Return the number of non-overlapping occurrences of subsection sub in\n\
1214bytes B[start:end]. Optional arguments start and end are interpreted\n\
1215as in slice notation.");
1216
1217static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001218bytearray_count(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001219{
1220 PyObject *sub_obj;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001221 const char *str = PyByteArray_AS_STRING(self), *sub;
1222 Py_ssize_t sub_len;
1223 char byte;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001224 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001225
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001226 Py_buffer vsub;
1227 PyObject *count_obj;
1228
Antoine Pitrouac65d962011-10-20 23:54:17 +02001229 if (!stringlib_parse_args_finds_byte("count", args, &sub_obj, &byte,
1230 &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001231 return NULL;
1232
Antoine Pitrouac65d962011-10-20 23:54:17 +02001233 if (sub_obj) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001234 if (PyObject_GetBuffer(sub_obj, &vsub, PyBUF_SIMPLE) != 0)
Antoine Pitrouac65d962011-10-20 23:54:17 +02001235 return NULL;
1236
1237 sub = vsub.buf;
1238 sub_len = vsub.len;
1239 }
1240 else {
1241 sub = &byte;
1242 sub_len = 1;
1243 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001244
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001245 ADJUST_INDICES(start, end, PyByteArray_GET_SIZE(self));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001246
1247 count_obj = PyLong_FromSsize_t(
Antoine Pitrouac65d962011-10-20 23:54:17 +02001248 stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001249 );
Antoine Pitrouac65d962011-10-20 23:54:17 +02001250
1251 if (sub_obj)
1252 PyBuffer_Release(&vsub);
1253
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001254 return count_obj;
1255}
1256
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001257/*[clinic input]
1258bytearray.clear
1259
1260 self: self(type="PyByteArrayObject *")
1261
1262Remove all items from the bytearray.
1263[clinic start generated code]*/
1264
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001265static PyObject *
1266bytearray_clear_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001267/*[clinic end generated code: output=85c2fe6aede0956c input=e524fd330abcdc18]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001268{
1269 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1270 return NULL;
1271 Py_RETURN_NONE;
1272}
1273
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001274/*[clinic input]
1275bytearray.copy
1276
1277 self: self(type="PyByteArrayObject *")
1278
1279Return a copy of B.
1280[clinic start generated code]*/
1281
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001282static PyObject *
1283bytearray_copy_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001284/*[clinic end generated code: output=68cfbcfed484c132 input=6d5d2975aa0f33f3]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001285{
1286 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1287 PyByteArray_GET_SIZE(self));
1288}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001289
1290PyDoc_STRVAR(index__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001291"B.index(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001292\n\
1293Like B.find() but raise ValueError when the subsection is not found.");
1294
1295static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001296bytearray_index(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001297{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001298 Py_ssize_t result = bytearray_find_internal(self, args, +1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001299 if (result == -2)
1300 return NULL;
1301 if (result == -1) {
1302 PyErr_SetString(PyExc_ValueError,
1303 "subsection not found");
1304 return NULL;
1305 }
1306 return PyLong_FromSsize_t(result);
1307}
1308
1309
1310PyDoc_STRVAR(rfind__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001311"B.rfind(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001312\n\
1313Return the highest index in B where subsection sub is found,\n\
Senthil Kumaran53516a82011-07-27 23:33:54 +08001314such that sub is contained within B[start,end]. Optional\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001315arguments start and end are interpreted as in slice notation.\n\
1316\n\
1317Return -1 on failure.");
1318
1319static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001320bytearray_rfind(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001321{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001322 Py_ssize_t result = bytearray_find_internal(self, args, -1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001323 if (result == -2)
1324 return NULL;
1325 return PyLong_FromSsize_t(result);
1326}
1327
1328
1329PyDoc_STRVAR(rindex__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001330"B.rindex(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001331\n\
1332Like B.rfind() but raise ValueError when the subsection is not found.");
1333
1334static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001335bytearray_rindex(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001336{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001337 Py_ssize_t result = bytearray_find_internal(self, args, -1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001338 if (result == -2)
1339 return NULL;
1340 if (result == -1) {
1341 PyErr_SetString(PyExc_ValueError,
1342 "subsection not found");
1343 return NULL;
1344 }
1345 return PyLong_FromSsize_t(result);
1346}
1347
1348
1349static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001350bytearray_contains(PyObject *self, PyObject *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001351{
1352 Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError);
1353 if (ival == -1 && PyErr_Occurred()) {
1354 Py_buffer varg;
Antoine Pitrou0010d372010-08-15 17:12:55 +00001355 Py_ssize_t pos;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001356 PyErr_Clear();
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001357 if (PyObject_GetBuffer(arg, &varg, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001358 return -1;
1359 pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self),
1360 varg.buf, varg.len, 0);
Martin v. Löwis423be952008-08-13 15:53:07 +00001361 PyBuffer_Release(&varg);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001362 return pos >= 0;
1363 }
1364 if (ival < 0 || ival >= 256) {
1365 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
1366 return -1;
1367 }
1368
Antoine Pitrou0010d372010-08-15 17:12:55 +00001369 return memchr(PyByteArray_AS_STRING(self), (int) ival, Py_SIZE(self)) != NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001370}
1371
1372
1373/* Matches the end (direction >= 0) or start (direction < 0) of self
1374 * against substr, using the start and end arguments. Returns
1375 * -1 on error, 0 if not found and 1 if found.
1376 */
1377Py_LOCAL(int)
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001378_bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001379 Py_ssize_t end, int direction)
1380{
1381 Py_ssize_t len = PyByteArray_GET_SIZE(self);
1382 const char* str;
1383 Py_buffer vsubstr;
1384 int rv = 0;
1385
1386 str = PyByteArray_AS_STRING(self);
1387
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001388 if (PyObject_GetBuffer(substr, &vsubstr, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001389 return -1;
1390
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001391 ADJUST_INDICES(start, end, len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001392
1393 if (direction < 0) {
1394 /* startswith */
1395 if (start+vsubstr.len > len) {
1396 goto done;
1397 }
1398 } else {
1399 /* endswith */
1400 if (end-start < vsubstr.len || start > len) {
1401 goto done;
1402 }
1403
1404 if (end-vsubstr.len > start)
1405 start = end - vsubstr.len;
1406 }
1407 if (end-start >= vsubstr.len)
1408 rv = ! memcmp(str+start, vsubstr.buf, vsubstr.len);
1409
1410done:
Martin v. Löwis423be952008-08-13 15:53:07 +00001411 PyBuffer_Release(&vsubstr);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001412 return rv;
1413}
1414
1415
1416PyDoc_STRVAR(startswith__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001417"B.startswith(prefix[, start[, end]]) -> bool\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001418\n\
1419Return True if B starts with the specified prefix, False otherwise.\n\
1420With optional start, test B beginning at that position.\n\
1421With optional end, stop comparing B at that position.\n\
Ezio Melottiba42fd52011-04-26 06:09:45 +03001422prefix can also be a tuple of bytes to try.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001423
1424static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001425bytearray_startswith(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001426{
1427 Py_ssize_t start = 0;
1428 Py_ssize_t end = PY_SSIZE_T_MAX;
1429 PyObject *subobj;
1430 int result;
1431
Jesus Ceaac451502011-04-20 17:09:23 +02001432 if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001433 return NULL;
1434 if (PyTuple_Check(subobj)) {
1435 Py_ssize_t i;
1436 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001437 result = _bytearray_tailmatch(self,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001438 PyTuple_GET_ITEM(subobj, i),
1439 start, end, -1);
1440 if (result == -1)
1441 return NULL;
1442 else if (result) {
1443 Py_RETURN_TRUE;
1444 }
1445 }
1446 Py_RETURN_FALSE;
1447 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001448 result = _bytearray_tailmatch(self, subobj, start, end, -1);
Ezio Melottiba42fd52011-04-26 06:09:45 +03001449 if (result == -1) {
1450 if (PyErr_ExceptionMatches(PyExc_TypeError))
1451 PyErr_Format(PyExc_TypeError, "startswith first arg must be bytes "
1452 "or a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001453 return NULL;
Ezio Melottiba42fd52011-04-26 06:09:45 +03001454 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001455 else
1456 return PyBool_FromLong(result);
1457}
1458
1459PyDoc_STRVAR(endswith__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001460"B.endswith(suffix[, start[, end]]) -> bool\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001461\n\
1462Return True if B ends with the specified suffix, False otherwise.\n\
1463With optional start, test B beginning at that position.\n\
1464With optional end, stop comparing B at that position.\n\
Ezio Melottiba42fd52011-04-26 06:09:45 +03001465suffix can also be a tuple of bytes to try.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001466
1467static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001468bytearray_endswith(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001469{
1470 Py_ssize_t start = 0;
1471 Py_ssize_t end = PY_SSIZE_T_MAX;
1472 PyObject *subobj;
1473 int result;
1474
Jesus Ceaac451502011-04-20 17:09:23 +02001475 if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001476 return NULL;
1477 if (PyTuple_Check(subobj)) {
1478 Py_ssize_t i;
1479 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001480 result = _bytearray_tailmatch(self,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001481 PyTuple_GET_ITEM(subobj, i),
1482 start, end, +1);
1483 if (result == -1)
1484 return NULL;
1485 else if (result) {
1486 Py_RETURN_TRUE;
1487 }
1488 }
1489 Py_RETURN_FALSE;
1490 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001491 result = _bytearray_tailmatch(self, subobj, start, end, +1);
Ezio Melottiba42fd52011-04-26 06:09:45 +03001492 if (result == -1) {
1493 if (PyErr_ExceptionMatches(PyExc_TypeError))
1494 PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or "
1495 "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001496 return NULL;
Ezio Melottiba42fd52011-04-26 06:09:45 +03001497 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001498 else
1499 return PyBool_FromLong(result);
1500}
1501
1502
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001503/*[clinic input]
1504bytearray.translate
1505
1506 self: self(type="PyByteArrayObject *")
1507 table: object
1508 Translation table, which must be a bytes object of length 256.
1509 [
1510 deletechars: object
1511 ]
1512 /
1513
1514Return a copy with each character mapped by the given translation table.
1515
1516All characters occurring in the optional argument deletechars are removed.
1517The remaining characters are mapped through the given translation table.
1518[clinic start generated code]*/
1519
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001520static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001521bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
1522 int group_right_1, PyObject *deletechars)
1523/*[clinic end generated code: output=2bebc86a9a1ff083 input=b749ad85f4860824]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001524{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001525 char *input, *output;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001526 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001527 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001528 PyObject *input_obj = (PyObject*)self;
1529 const char *output_start;
1530 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001531 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001532 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001533 Py_buffer vtable, vdel;
1534
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001535 if (table == Py_None) {
1536 table_chars = NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001537 table = NULL;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001538 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001539 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001540 } else {
1541 if (vtable.len != 256) {
1542 PyErr_SetString(PyExc_ValueError,
1543 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001544 PyBuffer_Release(&vtable);
1545 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001546 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001547 table_chars = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001548 }
1549
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001550 if (deletechars != NULL) {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001551 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001552 if (table != NULL)
Georg Brandl953152f2009-07-22 12:03:59 +00001553 PyBuffer_Release(&vtable);
1554 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001555 }
1556 }
1557 else {
1558 vdel.buf = NULL;
1559 vdel.len = 0;
1560 }
1561
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001562 inlen = PyByteArray_GET_SIZE(input_obj);
1563 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1564 if (result == NULL)
1565 goto done;
1566 output_start = output = PyByteArray_AsString(result);
1567 input = PyByteArray_AS_STRING(input_obj);
1568
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001569 if (vdel.len == 0 && table_chars != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001570 /* If no deletions are required, use faster code */
1571 for (i = inlen; --i >= 0; ) {
1572 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001573 *output++ = table_chars[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001574 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001575 goto done;
1576 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001577
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001578 if (table_chars == NULL) {
Georg Brandlccc47b62008-12-28 11:44:14 +00001579 for (i = 0; i < 256; i++)
1580 trans_table[i] = Py_CHARMASK(i);
1581 } else {
1582 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001583 trans_table[i] = Py_CHARMASK(table_chars[i]);
Georg Brandlccc47b62008-12-28 11:44:14 +00001584 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001585
1586 for (i = 0; i < vdel.len; i++)
1587 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1588
1589 for (i = inlen; --i >= 0; ) {
1590 c = Py_CHARMASK(*input++);
1591 if (trans_table[c] != -1)
1592 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1593 continue;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001594 }
1595 /* Fix the size of the resulting string */
1596 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001597 if (PyByteArray_Resize(result, output - output_start) < 0) {
1598 Py_CLEAR(result);
1599 goto done;
1600 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001601
1602done:
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001603 if (table != NULL)
Georg Brandlccc47b62008-12-28 11:44:14 +00001604 PyBuffer_Release(&vtable);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001605 if (deletechars != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001606 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001607 return result;
1608}
1609
1610
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001611/*[clinic input]
1612
1613@staticmethod
1614bytearray.maketrans
1615
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001616 frm: Py_buffer
1617 to: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001618 /
1619
1620Return a translation table useable for the bytes or bytearray translate method.
1621
1622The returned table will be one where each byte in frm is mapped to the byte at
1623the same position in to.
1624
1625The bytes objects frm and to must be of the same length.
1626[clinic start generated code]*/
1627
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001628static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001629bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001630/*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001631{
1632 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00001633}
1634
1635
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001636/* find and count characters and substrings */
1637
1638#define findchar(target, target_len, c) \
1639 ((char *)memchr((const void *)(target), c, target_len))
1640
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001641
Benjamin Peterson0f3641c2008-11-19 22:05:52 +00001642/* Bytes ops must return a string, create a copy */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001643Py_LOCAL(PyByteArrayObject *)
1644return_self(PyByteArrayObject *self)
1645{
Georg Brandl1e7217d2008-05-30 12:02:38 +00001646 /* always return a new bytearray */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001647 return (PyByteArrayObject *)PyByteArray_FromStringAndSize(
1648 PyByteArray_AS_STRING(self),
1649 PyByteArray_GET_SIZE(self));
1650}
1651
1652Py_LOCAL_INLINE(Py_ssize_t)
1653countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount)
1654{
1655 Py_ssize_t count=0;
1656 const char *start=target;
1657 const char *end=target+target_len;
1658
1659 while ( (start=findchar(start, end-start, c)) != NULL ) {
1660 count++;
1661 if (count >= maxcount)
1662 break;
1663 start += 1;
1664 }
1665 return count;
1666}
1667
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001668
1669/* Algorithms for different cases of string replacement */
1670
1671/* len(self)>=1, from="", len(to)>=1, maxcount>=1 */
1672Py_LOCAL(PyByteArrayObject *)
1673replace_interleave(PyByteArrayObject *self,
1674 const char *to_s, Py_ssize_t to_len,
1675 Py_ssize_t maxcount)
1676{
1677 char *self_s, *result_s;
1678 Py_ssize_t self_len, result_len;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001679 Py_ssize_t count, i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001680 PyByteArrayObject *result;
1681
1682 self_len = PyByteArray_GET_SIZE(self);
1683
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001684 /* 1 at the end plus 1 after every character;
1685 count = min(maxcount, self_len + 1) */
1686 if (maxcount <= self_len)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001687 count = maxcount;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001688 else
1689 /* Can't overflow: self_len + 1 <= maxcount <= PY_SSIZE_T_MAX. */
1690 count = self_len + 1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001691
1692 /* Check for overflow */
1693 /* result_len = count * to_len + self_len; */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001694 assert(count > 0);
1695 if (to_len > (PY_SSIZE_T_MAX - self_len) / count) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001696 PyErr_SetString(PyExc_OverflowError,
1697 "replace string is too long");
1698 return NULL;
1699 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001700 result_len = count * to_len + self_len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001701
1702 if (! (result = (PyByteArrayObject *)
1703 PyByteArray_FromStringAndSize(NULL, result_len)) )
1704 return NULL;
1705
1706 self_s = PyByteArray_AS_STRING(self);
1707 result_s = PyByteArray_AS_STRING(result);
1708
1709 /* TODO: special case single character, which doesn't need memcpy */
1710
1711 /* Lay the first one down (guaranteed this will occur) */
1712 Py_MEMCPY(result_s, to_s, to_len);
1713 result_s += to_len;
1714 count -= 1;
1715
1716 for (i=0; i<count; i++) {
1717 *result_s++ = *self_s++;
1718 Py_MEMCPY(result_s, to_s, to_len);
1719 result_s += to_len;
1720 }
1721
1722 /* Copy the rest of the original string */
1723 Py_MEMCPY(result_s, self_s, self_len-i);
1724
1725 return result;
1726}
1727
1728/* Special case for deleting a single character */
1729/* len(self)>=1, len(from)==1, to="", maxcount>=1 */
1730Py_LOCAL(PyByteArrayObject *)
1731replace_delete_single_character(PyByteArrayObject *self,
1732 char from_c, Py_ssize_t maxcount)
1733{
1734 char *self_s, *result_s;
1735 char *start, *next, *end;
1736 Py_ssize_t self_len, result_len;
1737 Py_ssize_t count;
1738 PyByteArrayObject *result;
1739
1740 self_len = PyByteArray_GET_SIZE(self);
1741 self_s = PyByteArray_AS_STRING(self);
1742
1743 count = countchar(self_s, self_len, from_c, maxcount);
1744 if (count == 0) {
1745 return return_self(self);
1746 }
1747
1748 result_len = self_len - count; /* from_len == 1 */
1749 assert(result_len>=0);
1750
1751 if ( (result = (PyByteArrayObject *)
1752 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1753 return NULL;
1754 result_s = PyByteArray_AS_STRING(result);
1755
1756 start = self_s;
1757 end = self_s + self_len;
1758 while (count-- > 0) {
1759 next = findchar(start, end-start, from_c);
1760 if (next == NULL)
1761 break;
1762 Py_MEMCPY(result_s, start, next-start);
1763 result_s += (next-start);
1764 start = next+1;
1765 }
1766 Py_MEMCPY(result_s, start, end-start);
1767
1768 return result;
1769}
1770
1771/* len(self)>=1, len(from)>=2, to="", maxcount>=1 */
1772
1773Py_LOCAL(PyByteArrayObject *)
1774replace_delete_substring(PyByteArrayObject *self,
1775 const char *from_s, Py_ssize_t from_len,
1776 Py_ssize_t maxcount)
1777{
1778 char *self_s, *result_s;
1779 char *start, *next, *end;
1780 Py_ssize_t self_len, result_len;
1781 Py_ssize_t count, offset;
1782 PyByteArrayObject *result;
1783
1784 self_len = PyByteArray_GET_SIZE(self);
1785 self_s = PyByteArray_AS_STRING(self);
1786
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001787 count = stringlib_count(self_s, self_len,
1788 from_s, from_len,
1789 maxcount);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001790
1791 if (count == 0) {
1792 /* no matches */
1793 return return_self(self);
1794 }
1795
1796 result_len = self_len - (count * from_len);
1797 assert (result_len>=0);
1798
1799 if ( (result = (PyByteArrayObject *)
1800 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL )
1801 return NULL;
1802
1803 result_s = PyByteArray_AS_STRING(result);
1804
1805 start = self_s;
1806 end = self_s + self_len;
1807 while (count-- > 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001808 offset = stringlib_find(start, end-start,
1809 from_s, from_len,
1810 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001811 if (offset == -1)
1812 break;
1813 next = start + offset;
1814
1815 Py_MEMCPY(result_s, start, next-start);
1816
1817 result_s += (next-start);
1818 start = next+from_len;
1819 }
1820 Py_MEMCPY(result_s, start, end-start);
1821 return result;
1822}
1823
1824/* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */
1825Py_LOCAL(PyByteArrayObject *)
1826replace_single_character_in_place(PyByteArrayObject *self,
1827 char from_c, char to_c,
1828 Py_ssize_t maxcount)
1829{
Antoine Pitroud1188562010-06-09 16:38:55 +00001830 char *self_s, *result_s, *start, *end, *next;
1831 Py_ssize_t self_len;
1832 PyByteArrayObject *result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001833
Antoine Pitroud1188562010-06-09 16:38:55 +00001834 /* The result string will be the same size */
1835 self_s = PyByteArray_AS_STRING(self);
1836 self_len = PyByteArray_GET_SIZE(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001837
Antoine Pitroud1188562010-06-09 16:38:55 +00001838 next = findchar(self_s, self_len, from_c);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001839
Antoine Pitroud1188562010-06-09 16:38:55 +00001840 if (next == NULL) {
1841 /* No matches; return the original bytes */
1842 return return_self(self);
1843 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001844
Antoine Pitroud1188562010-06-09 16:38:55 +00001845 /* Need to make a new bytes */
1846 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len);
1847 if (result == NULL)
1848 return NULL;
1849 result_s = PyByteArray_AS_STRING(result);
1850 Py_MEMCPY(result_s, self_s, self_len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001851
Antoine Pitroud1188562010-06-09 16:38:55 +00001852 /* change everything in-place, starting with this one */
1853 start = result_s + (next-self_s);
1854 *start = to_c;
1855 start++;
1856 end = result_s + self_len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001857
Antoine Pitroud1188562010-06-09 16:38:55 +00001858 while (--maxcount > 0) {
1859 next = findchar(start, end-start, from_c);
1860 if (next == NULL)
1861 break;
1862 *next = to_c;
1863 start = next+1;
1864 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001865
Antoine Pitroud1188562010-06-09 16:38:55 +00001866 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001867}
1868
1869/* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */
1870Py_LOCAL(PyByteArrayObject *)
1871replace_substring_in_place(PyByteArrayObject *self,
1872 const char *from_s, Py_ssize_t from_len,
1873 const char *to_s, Py_ssize_t to_len,
1874 Py_ssize_t maxcount)
1875{
1876 char *result_s, *start, *end;
1877 char *self_s;
1878 Py_ssize_t self_len, offset;
1879 PyByteArrayObject *result;
1880
1881 /* The result bytes will be the same size */
1882
1883 self_s = PyByteArray_AS_STRING(self);
1884 self_len = PyByteArray_GET_SIZE(self);
1885
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001886 offset = stringlib_find(self_s, self_len,
1887 from_s, from_len,
1888 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001889 if (offset == -1) {
1890 /* No matches; return the original bytes */
1891 return return_self(self);
1892 }
1893
1894 /* Need to make a new bytes */
1895 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len);
1896 if (result == NULL)
1897 return NULL;
1898 result_s = PyByteArray_AS_STRING(result);
1899 Py_MEMCPY(result_s, self_s, self_len);
1900
1901 /* change everything in-place, starting with this one */
1902 start = result_s + offset;
1903 Py_MEMCPY(start, to_s, from_len);
1904 start += from_len;
1905 end = result_s + self_len;
1906
1907 while ( --maxcount > 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001908 offset = stringlib_find(start, end-start,
1909 from_s, from_len,
1910 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001911 if (offset==-1)
1912 break;
1913 Py_MEMCPY(start+offset, to_s, from_len);
1914 start += offset+from_len;
1915 }
1916
1917 return result;
1918}
1919
1920/* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */
1921Py_LOCAL(PyByteArrayObject *)
1922replace_single_character(PyByteArrayObject *self,
1923 char from_c,
1924 const char *to_s, Py_ssize_t to_len,
1925 Py_ssize_t maxcount)
1926{
1927 char *self_s, *result_s;
1928 char *start, *next, *end;
1929 Py_ssize_t self_len, result_len;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001930 Py_ssize_t count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001931 PyByteArrayObject *result;
1932
1933 self_s = PyByteArray_AS_STRING(self);
1934 self_len = PyByteArray_GET_SIZE(self);
1935
1936 count = countchar(self_s, self_len, from_c, maxcount);
1937 if (count == 0) {
1938 /* no matches, return unchanged */
1939 return return_self(self);
1940 }
1941
1942 /* use the difference between current and new, hence the "-1" */
1943 /* result_len = self_len + count * (to_len-1) */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001944 assert(count > 0);
1945 if (to_len - 1 > (PY_SSIZE_T_MAX - self_len) / count) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001946 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
1947 return NULL;
1948 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001949 result_len = self_len + count * (to_len - 1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001950
1951 if ( (result = (PyByteArrayObject *)
1952 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1953 return NULL;
1954 result_s = PyByteArray_AS_STRING(result);
1955
1956 start = self_s;
1957 end = self_s + self_len;
1958 while (count-- > 0) {
1959 next = findchar(start, end-start, from_c);
1960 if (next == NULL)
1961 break;
1962
1963 if (next == start) {
1964 /* replace with the 'to' */
1965 Py_MEMCPY(result_s, to_s, to_len);
1966 result_s += to_len;
1967 start += 1;
1968 } else {
1969 /* copy the unchanged old then the 'to' */
1970 Py_MEMCPY(result_s, start, next-start);
1971 result_s += (next-start);
1972 Py_MEMCPY(result_s, to_s, to_len);
1973 result_s += to_len;
1974 start = next+1;
1975 }
1976 }
1977 /* Copy the remainder of the remaining bytes */
1978 Py_MEMCPY(result_s, start, end-start);
1979
1980 return result;
1981}
1982
1983/* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */
1984Py_LOCAL(PyByteArrayObject *)
1985replace_substring(PyByteArrayObject *self,
1986 const char *from_s, Py_ssize_t from_len,
1987 const char *to_s, Py_ssize_t to_len,
1988 Py_ssize_t maxcount)
1989{
1990 char *self_s, *result_s;
1991 char *start, *next, *end;
1992 Py_ssize_t self_len, result_len;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001993 Py_ssize_t count, offset;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001994 PyByteArrayObject *result;
1995
1996 self_s = PyByteArray_AS_STRING(self);
1997 self_len = PyByteArray_GET_SIZE(self);
1998
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001999 count = stringlib_count(self_s, self_len,
2000 from_s, from_len,
2001 maxcount);
2002
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002003 if (count == 0) {
2004 /* no matches, return unchanged */
2005 return return_self(self);
2006 }
2007
2008 /* Check for overflow */
2009 /* result_len = self_len + count * (to_len-from_len) */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00002010 assert(count > 0);
2011 if (to_len - from_len > (PY_SSIZE_T_MAX - self_len) / count) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002012 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
2013 return NULL;
2014 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00002015 result_len = self_len + count * (to_len - from_len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002016
2017 if ( (result = (PyByteArrayObject *)
2018 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
2019 return NULL;
2020 result_s = PyByteArray_AS_STRING(result);
2021
2022 start = self_s;
2023 end = self_s + self_len;
2024 while (count-- > 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002025 offset = stringlib_find(start, end-start,
2026 from_s, from_len,
2027 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002028 if (offset == -1)
2029 break;
2030 next = start+offset;
2031 if (next == start) {
2032 /* replace with the 'to' */
2033 Py_MEMCPY(result_s, to_s, to_len);
2034 result_s += to_len;
2035 start += from_len;
2036 } else {
2037 /* copy the unchanged old then the 'to' */
2038 Py_MEMCPY(result_s, start, next-start);
2039 result_s += (next-start);
2040 Py_MEMCPY(result_s, to_s, to_len);
2041 result_s += to_len;
2042 start = next+from_len;
2043 }
2044 }
2045 /* Copy the remainder of the remaining bytes */
2046 Py_MEMCPY(result_s, start, end-start);
2047
2048 return result;
2049}
2050
2051
2052Py_LOCAL(PyByteArrayObject *)
2053replace(PyByteArrayObject *self,
2054 const char *from_s, Py_ssize_t from_len,
2055 const char *to_s, Py_ssize_t to_len,
2056 Py_ssize_t maxcount)
2057{
2058 if (maxcount < 0) {
2059 maxcount = PY_SSIZE_T_MAX;
2060 } else if (maxcount == 0 || PyByteArray_GET_SIZE(self) == 0) {
2061 /* nothing to do; return the original bytes */
2062 return return_self(self);
2063 }
2064
2065 if (maxcount == 0 ||
2066 (from_len == 0 && to_len == 0)) {
2067 /* nothing to do; return the original bytes */
2068 return return_self(self);
2069 }
2070
2071 /* Handle zero-length special cases */
2072
2073 if (from_len == 0) {
2074 /* insert the 'to' bytes everywhere. */
2075 /* >>> "Python".replace("", ".") */
2076 /* '.P.y.t.h.o.n.' */
2077 return replace_interleave(self, to_s, to_len, maxcount);
2078 }
2079
2080 /* Except for "".replace("", "A") == "A" there is no way beyond this */
2081 /* point for an empty self bytes to generate a non-empty bytes */
2082 /* Special case so the remaining code always gets a non-empty bytes */
2083 if (PyByteArray_GET_SIZE(self) == 0) {
2084 return return_self(self);
2085 }
2086
2087 if (to_len == 0) {
Georg Brandl17cb8a82008-05-30 08:20:09 +00002088 /* delete all occurrences of 'from' bytes */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002089 if (from_len == 1) {
2090 return replace_delete_single_character(
2091 self, from_s[0], maxcount);
2092 } else {
2093 return replace_delete_substring(self, from_s, from_len, maxcount);
2094 }
2095 }
2096
2097 /* Handle special case where both bytes have the same length */
2098
2099 if (from_len == to_len) {
2100 if (from_len == 1) {
2101 return replace_single_character_in_place(
2102 self,
2103 from_s[0],
2104 to_s[0],
2105 maxcount);
2106 } else {
2107 return replace_substring_in_place(
2108 self, from_s, from_len, to_s, to_len, maxcount);
2109 }
2110 }
2111
2112 /* Otherwise use the more generic algorithms */
2113 if (from_len == 1) {
2114 return replace_single_character(self, from_s[0],
2115 to_s, to_len, maxcount);
2116 } else {
2117 /* len('from')>=2, len('to')>=1 */
2118 return replace_substring(self, from_s, from_len, to_s, to_len, maxcount);
2119 }
2120}
2121
2122
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002123/*[clinic input]
2124bytearray.replace
2125
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002126 old: Py_buffer
2127 new: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002128 count: Py_ssize_t = -1
2129 Maximum number of occurrences to replace.
2130 -1 (the default value) means replace all occurrences.
2131 /
2132
2133Return a copy with all occurrences of substring old replaced by new.
2134
2135If the optional argument count is given, only the first count occurrences are
2136replaced.
2137[clinic start generated code]*/
2138
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002139static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04002140bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
2141 Py_buffer *new, Py_ssize_t count)
2142/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002143{
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002144 return (PyObject *)replace((PyByteArrayObject *) self,
2145 old->buf, old->len,
2146 new->buf, new->len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002147}
2148
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002149/*[clinic input]
2150bytearray.split
2151
2152 sep: object = None
2153 The delimiter according which to split the bytearray.
2154 None (the default value) means split on ASCII whitespace characters
2155 (space, tab, return, newline, formfeed, vertical tab).
2156 maxsplit: Py_ssize_t = -1
2157 Maximum number of splits to do.
2158 -1 (the default value) means no limit.
2159
2160Return a list of the sections in the bytearray, using sep as the delimiter.
2161[clinic start generated code]*/
2162
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002163static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04002164bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
2165 Py_ssize_t maxsplit)
2166/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002167{
2168 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002169 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002170 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002171 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002172
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002173 if (maxsplit < 0)
2174 maxsplit = PY_SSIZE_T_MAX;
2175
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002176 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002177 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002178
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002179 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002180 return NULL;
2181 sub = vsub.buf;
2182 n = vsub.len;
2183
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002184 list = stringlib_split(
2185 (PyObject*) self, s, len, sub, n, maxsplit
2186 );
Martin v. Löwis423be952008-08-13 15:53:07 +00002187 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002188 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002189}
2190
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002191/*[clinic input]
2192bytearray.partition
2193
2194 self: self(type="PyByteArrayObject *")
2195 sep: object
2196 /
2197
2198Partition the bytearray into three parts using the given separator.
2199
2200This will search for the separator sep in the bytearray. If the separator is
2201found, returns a 3-tuple containing the part before the separator, the
2202separator itself, and the part after it.
2203
2204If the separator is not found, returns a 3-tuple containing the original
2205bytearray object and two empty bytearray objects.
2206[clinic start generated code]*/
2207
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002208static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002209bytearray_partition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002210/*[clinic end generated code: output=45d2525ddd35f957 input=7d7fe37b1696d506]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002211{
2212 PyObject *bytesep, *result;
2213
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002214 bytesep = PyByteArray_FromObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002215 if (! bytesep)
2216 return NULL;
2217
2218 result = stringlib_partition(
2219 (PyObject*) self,
2220 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2221 bytesep,
2222 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2223 );
2224
2225 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002226 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002227}
2228
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002229/*[clinic input]
2230bytearray.rpartition
2231
2232 self: self(type="PyByteArrayObject *")
2233 sep: object
2234 /
2235
2236Partition the bytes into three parts using the given separator.
2237
2238This will search for the separator sep in the bytearray, starting and the end.
2239If the separator is found, returns a 3-tuple containing the part before the
2240separator, the separator itself, and the part after it.
2241
2242If the separator is not found, returns a 3-tuple containing two empty bytearray
2243objects and the original bytearray object.
2244[clinic start generated code]*/
2245
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002246static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002247bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002248/*[clinic end generated code: output=440de3c9426115e8 input=9b8cd540c1b75853]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002249{
2250 PyObject *bytesep, *result;
2251
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002252 bytesep = PyByteArray_FromObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002253 if (! bytesep)
2254 return NULL;
2255
2256 result = stringlib_rpartition(
2257 (PyObject*) self,
2258 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2259 bytesep,
2260 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2261 );
2262
2263 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002264 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002265}
2266
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002267/*[clinic input]
2268bytearray.rsplit = bytearray.split
2269
2270Return a list of the sections in the bytearray, using sep as the delimiter.
2271
2272Splitting is done starting at the end of the bytearray and working to the front.
2273[clinic start generated code]*/
2274
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002275static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04002276bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
2277 Py_ssize_t maxsplit)
2278/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002279{
2280 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002281 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002282 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002283 Py_buffer vsub;
2284
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002285 if (maxsplit < 0)
2286 maxsplit = PY_SSIZE_T_MAX;
2287
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002288 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002289 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002290
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002291 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002292 return NULL;
2293 sub = vsub.buf;
2294 n = vsub.len;
2295
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002296 list = stringlib_rsplit(
2297 (PyObject*) self, s, len, sub, n, maxsplit
2298 );
Martin v. Löwis423be952008-08-13 15:53:07 +00002299 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002300 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002301}
2302
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002303/*[clinic input]
2304bytearray.reverse
2305
2306 self: self(type="PyByteArrayObject *")
2307
2308Reverse the order of the values in B in place.
2309[clinic start generated code]*/
2310
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002311static PyObject *
2312bytearray_reverse_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002313/*[clinic end generated code: output=9f7616f29ab309d3 input=7933a499b8597bd1]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002314{
2315 char swap, *head, *tail;
2316 Py_ssize_t i, j, n = Py_SIZE(self);
2317
2318 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002319 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002320 tail = head + n - 1;
2321 for (i = 0; i < j; i++) {
2322 swap = *head;
2323 *head++ = *tail;
2324 *tail-- = swap;
2325 }
2326
2327 Py_RETURN_NONE;
2328}
2329
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002330
2331/*[python input]
2332class bytesvalue_converter(CConverter):
2333 type = 'int'
2334 converter = '_getbytevalue'
2335[python start generated code]*/
2336/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
2337
2338
2339/*[clinic input]
2340bytearray.insert
2341
2342 self: self(type="PyByteArrayObject *")
2343 index: Py_ssize_t
2344 The index where the value is to be inserted.
2345 item: bytesvalue
2346 The item to be inserted.
2347 /
2348
2349Insert a single item into the bytearray before the given index.
2350[clinic start generated code]*/
2351
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002352static PyObject *
2353bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002354/*[clinic end generated code: output=76c775a70e7b07b7 input=833766836ba30e1e]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002355{
2356 Py_ssize_t n = Py_SIZE(self);
2357 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002358
2359 if (n == PY_SSIZE_T_MAX) {
2360 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00002361 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002362 return NULL;
2363 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002364 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2365 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002366 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002367
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002368 if (index < 0) {
2369 index += n;
2370 if (index < 0)
2371 index = 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002372 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002373 if (index > n)
2374 index = n;
2375 memmove(buf + index + 1, buf + index, n - index);
2376 buf[index] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002377
2378 Py_RETURN_NONE;
2379}
2380
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002381/*[clinic input]
2382bytearray.append
2383
2384 self: self(type="PyByteArrayObject *")
2385 item: bytesvalue
2386 The item to be appended.
2387 /
2388
2389Append a single item to the end of the bytearray.
2390[clinic start generated code]*/
2391
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002392static PyObject *
2393bytearray_append_impl(PyByteArrayObject *self, int item)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002394/*[clinic end generated code: output=a154e19ed1886cb6 input=ae56ea87380407cc]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002395{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002396 Py_ssize_t n = Py_SIZE(self);
2397
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002398 if (n == PY_SSIZE_T_MAX) {
2399 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00002400 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002401 return NULL;
2402 }
2403 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2404 return NULL;
2405
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002406 PyByteArray_AS_STRING(self)[n] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002407
2408 Py_RETURN_NONE;
2409}
2410
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002411/*[clinic input]
2412bytearray.extend
2413
2414 self: self(type="PyByteArrayObject *")
2415 iterable_of_ints: object
2416 The iterable of items to append.
2417 /
2418
2419Append all the items from the iterator or sequence to the end of the bytearray.
2420[clinic start generated code]*/
2421
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002422static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002423bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002424/*[clinic end generated code: output=98155dbe249170b1 input=ce83a5d75b70d850]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002425{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002426 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002427 Py_ssize_t buf_size = 0, len = 0;
2428 int value;
2429 char *buf;
2430
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002431 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002432 if (PyObject_CheckBuffer(iterable_of_ints)) {
2433 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002434 return NULL;
2435
2436 Py_RETURN_NONE;
2437 }
2438
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002439 it = PyObject_GetIter(iterable_of_ints);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002440 if (it == NULL)
2441 return NULL;
2442
Ezio Melotti42da6632011-03-15 05:18:48 +02002443 /* Try to determine the length of the argument. 32 is arbitrary. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002444 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002445 if (buf_size == -1) {
2446 Py_DECREF(it);
2447 return NULL;
2448 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002449
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002450 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002451 if (bytearray_obj == NULL) {
2452 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002453 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002454 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002455 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002456
2457 while ((item = PyIter_Next(it)) != NULL) {
2458 if (! _getbytevalue(item, &value)) {
2459 Py_DECREF(item);
2460 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002461 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002462 return NULL;
2463 }
2464 buf[len++] = value;
2465 Py_DECREF(item);
2466
2467 if (len >= buf_size) {
2468 buf_size = len + (len >> 1) + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002469 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002470 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002471 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002472 return NULL;
2473 }
2474 /* Recompute the `buf' pointer, since the resizing operation may
2475 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002476 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002477 }
2478 }
2479 Py_DECREF(it);
2480
2481 /* Resize down to exact size. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002482 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
2483 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002484 return NULL;
2485 }
2486
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002487 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
2488 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002489 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002490 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002491 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002492
2493 Py_RETURN_NONE;
2494}
2495
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002496/*[clinic input]
2497bytearray.pop
2498
2499 self: self(type="PyByteArrayObject *")
2500 index: Py_ssize_t = -1
2501 The index from where to remove the item.
2502 -1 (the default value) means remove the last item.
2503 /
2504
2505Remove and return a single item from B.
2506
2507If no index argument is given, will pop the last item.
2508[clinic start generated code]*/
2509
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002510static PyObject *
2511bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002512/*[clinic end generated code: output=e0ccd401f8021da8 input=0797e6c0ca9d5a85]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002513{
2514 int value;
2515 Py_ssize_t n = Py_SIZE(self);
2516 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002517
2518 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00002519 PyErr_SetString(PyExc_IndexError,
2520 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002521 return NULL;
2522 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002523 if (index < 0)
2524 index += Py_SIZE(self);
2525 if (index < 0 || index >= Py_SIZE(self)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002526 PyErr_SetString(PyExc_IndexError, "pop index out of range");
2527 return NULL;
2528 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00002529 if (!_canresize(self))
2530 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002531
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002532 buf = PyByteArray_AS_STRING(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002533 value = buf[index];
2534 memmove(buf + index, buf + index + 1, n - index);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002535 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2536 return NULL;
2537
Mark Dickinson54a3db92009-09-06 10:19:23 +00002538 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002539}
2540
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002541/*[clinic input]
2542bytearray.remove
2543
2544 self: self(type="PyByteArrayObject *")
2545 value: bytesvalue
2546 The value to remove.
2547 /
2548
2549Remove the first occurrence of a value in the bytearray.
2550[clinic start generated code]*/
2551
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002552static PyObject *
2553bytearray_remove_impl(PyByteArrayObject *self, int value)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002554/*[clinic end generated code: output=d659e37866709c13 input=47560b11fd856c24]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002555{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002556 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002557 char *buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002558
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002559 for (where = 0; where < n; where++) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002560 if (buf[where] == value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002561 break;
2562 }
2563 if (where == n) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00002564 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002565 return NULL;
2566 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00002567 if (!_canresize(self))
2568 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002569
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002570 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002571 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2572 return NULL;
2573
2574 Py_RETURN_NONE;
2575}
2576
2577/* XXX These two helpers could be optimized if argsize == 1 */
2578
2579static Py_ssize_t
Antoine Pitrou5b720752013-10-05 21:24:10 +02002580lstrip_helper(char *myptr, Py_ssize_t mysize,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002581 void *argptr, Py_ssize_t argsize)
2582{
2583 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02002584 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002585 i++;
2586 return i;
2587}
2588
2589static Py_ssize_t
Antoine Pitrou5b720752013-10-05 21:24:10 +02002590rstrip_helper(char *myptr, Py_ssize_t mysize,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002591 void *argptr, Py_ssize_t argsize)
2592{
2593 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02002594 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002595 i--;
2596 return i + 1;
2597}
2598
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002599/*[clinic input]
2600bytearray.strip
2601
2602 bytes: object = None
2603 /
2604
2605Strip leading and trailing bytes contained in the argument.
2606
2607If the argument is omitted or None, strip leading and trailing ASCII whitespace.
2608[clinic start generated code]*/
2609
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002610static PyObject *
2611bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002612/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002613{
2614 Py_ssize_t left, right, mysize, byteslen;
2615 char *myptr, *bytesptr;
2616 Py_buffer vbytes;
2617
2618 if (bytes == Py_None) {
2619 bytesptr = "\t\n\r\f\v ";
2620 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002621 }
2622 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002623 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002624 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002625 bytesptr = (char *) vbytes.buf;
2626 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002627 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002628 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002629 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002630 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002631 if (left == mysize)
2632 right = left;
2633 else
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002634 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
2635 if (bytes != Py_None)
2636 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002637 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002638}
2639
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002640/*[clinic input]
2641bytearray.lstrip
2642
2643 bytes: object = None
2644 /
2645
2646Strip leading bytes contained in the argument.
2647
2648If the argument is omitted or None, strip leading ASCII whitespace.
2649[clinic start generated code]*/
2650
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002651static PyObject *
2652bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002653/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002654{
2655 Py_ssize_t left, right, mysize, byteslen;
2656 char *myptr, *bytesptr;
2657 Py_buffer vbytes;
2658
2659 if (bytes == Py_None) {
2660 bytesptr = "\t\n\r\f\v ";
2661 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002662 }
2663 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002664 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002665 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002666 bytesptr = (char *) vbytes.buf;
2667 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002668 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002669 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002670 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002671 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002672 right = mysize;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002673 if (bytes != Py_None)
2674 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002675 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002676}
2677
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002678/*[clinic input]
2679bytearray.rstrip
2680
2681 bytes: object = None
2682 /
2683
2684Strip trailing bytes contained in the argument.
2685
2686If the argument is omitted or None, strip trailing ASCII whitespace.
2687[clinic start generated code]*/
2688
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002689static PyObject *
2690bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002691/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002692{
2693 Py_ssize_t right, mysize, byteslen;
2694 char *myptr, *bytesptr;
2695 Py_buffer vbytes;
2696
2697 if (bytes == Py_None) {
2698 bytesptr = "\t\n\r\f\v ";
2699 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002700 }
2701 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002702 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002703 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002704 bytesptr = (char *) vbytes.buf;
2705 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002706 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002707 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002708 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002709 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
2710 if (bytes != Py_None)
2711 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002712 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002713}
2714
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002715/*[clinic input]
2716bytearray.decode
2717
2718 encoding: str(c_default="NULL") = 'utf-8'
2719 The encoding with which to decode the bytearray.
2720 errors: str(c_default="NULL") = 'strict'
2721 The error handling scheme to use for the handling of decoding errors.
2722 The default is 'strict' meaning that decoding errors raise a
2723 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
2724 as well as any other name registered with codecs.register_error that
2725 can handle UnicodeDecodeErrors.
2726
2727Decode the bytearray using the codec registered for encoding.
2728[clinic start generated code]*/
2729
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002730static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04002731bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
2732 const char *errors)
2733/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002734{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002735 if (encoding == NULL)
2736 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis0efea322014-07-27 17:29:17 +02002737 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002738}
2739
2740PyDoc_STRVAR(alloc_doc,
2741"B.__alloc__() -> int\n\
2742\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00002743Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002744
2745static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002746bytearray_alloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002747{
2748 return PyLong_FromSsize_t(self->ob_alloc);
2749}
2750
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002751/*[clinic input]
2752bytearray.join
2753
2754 iterable_of_bytes: object
2755 /
2756
2757Concatenate any number of bytes/bytearray objects.
2758
2759The bytearray whose method is called is inserted in between each pair.
2760
2761The result is returned as a new bytearray object.
2762[clinic start generated code]*/
2763
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002764static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002765bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002766/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002767{
Martin v. Löwis0efea322014-07-27 17:29:17 +02002768 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002769}
2770
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002771/*[clinic input]
2772bytearray.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002773
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +03002774 keepends: int(c_default="0") = False
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002775
2776Return a list of the lines in the bytearray, breaking at line boundaries.
2777
2778Line breaks are not included in the resulting list unless keepends is given and
2779true.
2780[clinic start generated code]*/
2781
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002782static PyObject *
2783bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +03002784/*[clinic end generated code: output=4223c94b895f6ad9 input=8ccade941e5ea0bd]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002785{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002786 return stringlib_splitlines(
2787 (PyObject*) self, PyByteArray_AS_STRING(self),
2788 PyByteArray_GET_SIZE(self), keepends
2789 );
2790}
2791
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002792/*[clinic input]
2793@classmethod
2794bytearray.fromhex
2795
2796 cls: self(type="PyObject*")
2797 string: unicode
2798 /
2799
2800Create a bytearray object from a string of hexadecimal numbers.
2801
2802Spaces between two numbers are accepted.
2803Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
2804[clinic start generated code]*/
2805
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002806static PyObject *
2807bytearray_fromhex_impl(PyObject*cls, PyObject *string)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002808/*[clinic end generated code: output=df3da60129b3700c input=907bbd2d34d9367a]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002809{
Victor Stinner2bf89932015-10-14 11:25:33 +02002810 return _PyBytes_FromHex(string, 1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002811}
2812
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002813PyDoc_STRVAR(hex__doc__,
2814"B.hex() -> string\n\
2815\n\
2816Create a string of hexadecimal numbers from a bytearray object.\n\
2817Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'.");
2818
2819static PyObject *
2820bytearray_hex(PyBytesObject *self)
2821{
2822 char* argbuf = PyByteArray_AS_STRING(self);
2823 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
2824 return _Py_strhex(argbuf, arglen);
2825}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002826
2827static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002828_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002829{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002830 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002831 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002832 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002833
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002834 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002835 if (dict == NULL) {
2836 PyErr_Clear();
2837 dict = Py_None;
2838 Py_INCREF(dict);
2839 }
2840
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002841 buf = PyByteArray_AS_STRING(self);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002842 if (proto < 3) {
2843 /* use str based reduction for backwards compatibility with Python 2.x */
2844 PyObject *latin1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002845 if (Py_SIZE(self))
2846 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002847 else
2848 latin1 = PyUnicode_FromString("");
2849 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2850 }
2851 else {
2852 /* use more efficient byte based reduction */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002853 if (Py_SIZE(self)) {
2854 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002855 }
2856 else {
2857 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2858 }
2859 }
2860}
2861
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002862/*[clinic input]
2863bytearray.__reduce__ as bytearray_reduce
2864
2865 self: self(type="PyByteArrayObject *")
2866
2867Return state information for pickling.
2868[clinic start generated code]*/
2869
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002870static PyObject *
2871bytearray_reduce_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002872/*[clinic end generated code: output=52bf304086464cab input=fbb07de4d102a03a]*/
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002873{
2874 return _common_reduce(self, 2);
2875}
2876
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002877/*[clinic input]
2878bytearray.__reduce_ex__ as bytearray_reduce_ex
2879
2880 self: self(type="PyByteArrayObject *")
2881 proto: int = 0
2882 /
2883
2884Return state information for pickling.
2885[clinic start generated code]*/
2886
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002887static PyObject *
2888bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002889/*[clinic end generated code: output=52eac33377197520 input=0e091a42ca6dbd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002890{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002891 return _common_reduce(self, proto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002892}
2893
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002894/*[clinic input]
2895bytearray.__sizeof__ as bytearray_sizeof
2896
2897 self: self(type="PyByteArrayObject *")
2898
2899Returns the size of the bytearray object in memory, in bytes.
2900[clinic start generated code]*/
2901
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002902static PyObject *
2903bytearray_sizeof_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002904/*[clinic end generated code: output=738abdd17951c427 input=6b23d305362b462b]*/
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002905{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002906 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002907
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002908 res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char);
2909 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002910}
2911
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002912static PySequenceMethods bytearray_as_sequence = {
2913 (lenfunc)bytearray_length, /* sq_length */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002914 (binaryfunc)PyByteArray_Concat, /* sq_concat */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002915 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2916 (ssizeargfunc)bytearray_getitem, /* sq_item */
2917 0, /* sq_slice */
2918 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2919 0, /* sq_ass_slice */
2920 (objobjproc)bytearray_contains, /* sq_contains */
2921 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2922 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002923};
2924
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002925static PyMappingMethods bytearray_as_mapping = {
2926 (lenfunc)bytearray_length,
2927 (binaryfunc)bytearray_subscript,
2928 (objobjargproc)bytearray_ass_subscript,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002929};
2930
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002931static PyBufferProcs bytearray_as_buffer = {
2932 (getbufferproc)bytearray_getbuffer,
2933 (releasebufferproc)bytearray_releasebuffer,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002934};
2935
2936static PyMethodDef
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002937bytearray_methods[] = {
2938 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002939 BYTEARRAY_REDUCE_METHODDEF
2940 BYTEARRAY_REDUCE_EX_METHODDEF
2941 BYTEARRAY_SIZEOF_METHODDEF
2942 BYTEARRAY_APPEND_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002943 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
2944 _Py_capitalize__doc__},
2945 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002946 BYTEARRAY_CLEAR_METHODDEF
2947 BYTEARRAY_COPY_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002948 {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002949 BYTEARRAY_DECODE_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002950 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__},
Ezio Melotti745d54d2013-11-16 19:10:57 +02002951 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002952 expandtabs__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002953 BYTEARRAY_EXTEND_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002954 {"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002955 BYTEARRAY_FROMHEX_METHODDEF
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002956 {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__},
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002957 {"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002958 BYTEARRAY_INSERT_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002959 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
2960 _Py_isalnum__doc__},
2961 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
2962 _Py_isalpha__doc__},
2963 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
2964 _Py_isdigit__doc__},
2965 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
2966 _Py_islower__doc__},
2967 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
2968 _Py_isspace__doc__},
2969 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
2970 _Py_istitle__doc__},
2971 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
2972 _Py_isupper__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002973 BYTEARRAY_JOIN_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002974 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__},
2975 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002976 BYTEARRAY_LSTRIP_METHODDEF
2977 BYTEARRAY_MAKETRANS_METHODDEF
2978 BYTEARRAY_PARTITION_METHODDEF
2979 BYTEARRAY_POP_METHODDEF
2980 BYTEARRAY_REMOVE_METHODDEF
2981 BYTEARRAY_REPLACE_METHODDEF
2982 BYTEARRAY_REVERSE_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002983 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__},
2984 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002985 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002986 BYTEARRAY_RPARTITION_METHODDEF
2987 BYTEARRAY_RSPLIT_METHODDEF
2988 BYTEARRAY_RSTRIP_METHODDEF
2989 BYTEARRAY_SPLIT_METHODDEF
2990 BYTEARRAY_SPLITLINES_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002991 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002992 startswith__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002993 BYTEARRAY_STRIP_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002994 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
2995 _Py_swapcase__doc__},
2996 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002997 BYTEARRAY_TRANSLATE_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002998 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
2999 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
3000 {NULL}
3001};
3002
Ethan Furmanb95b5612015-01-23 20:05:18 -08003003static PyObject *
3004bytearray_mod(PyObject *v, PyObject *w)
3005{
3006 if (!PyByteArray_Check(v))
3007 Py_RETURN_NOTIMPLEMENTED;
3008 return bytearray_format((PyByteArrayObject *)v, w);
3009}
3010
3011static PyNumberMethods bytearray_as_number = {
3012 0, /*nb_add*/
3013 0, /*nb_subtract*/
3014 0, /*nb_multiply*/
3015 bytearray_mod, /*nb_remainder*/
3016};
3017
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003018PyDoc_STRVAR(bytearray_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00003019"bytearray(iterable_of_ints) -> bytearray\n\
3020bytearray(string, encoding[, errors]) -> bytearray\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01003021bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
3022bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
3023bytearray() -> empty bytes array\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003024\n\
3025Construct an mutable bytearray object from:\n\
3026 - an iterable yielding integers in range(256)\n\
3027 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01003028 - a bytes or a buffer object\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003029 - any object implementing the buffer API.\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01003030 - an integer");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003031
3032
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003033static PyObject *bytearray_iter(PyObject *seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003034
3035PyTypeObject PyByteArray_Type = {
3036 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3037 "bytearray",
3038 sizeof(PyByteArrayObject),
3039 0,
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003040 (destructor)bytearray_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003041 0, /* tp_print */
3042 0, /* tp_getattr */
3043 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00003044 0, /* tp_reserved */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003045 (reprfunc)bytearray_repr, /* tp_repr */
Ethan Furmanb95b5612015-01-23 20:05:18 -08003046 &bytearray_as_number, /* tp_as_number */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003047 &bytearray_as_sequence, /* tp_as_sequence */
3048 &bytearray_as_mapping, /* tp_as_mapping */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003049 0, /* tp_hash */
3050 0, /* tp_call */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003051 bytearray_str, /* tp_str */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003052 PyObject_GenericGetAttr, /* tp_getattro */
3053 0, /* tp_setattro */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003054 &bytearray_as_buffer, /* tp_as_buffer */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003055 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003056 bytearray_doc, /* tp_doc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003057 0, /* tp_traverse */
3058 0, /* tp_clear */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003059 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003060 0, /* tp_weaklistoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003061 bytearray_iter, /* tp_iter */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003062 0, /* tp_iternext */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003063 bytearray_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003064 0, /* tp_members */
3065 0, /* tp_getset */
3066 0, /* tp_base */
3067 0, /* tp_dict */
3068 0, /* tp_descr_get */
3069 0, /* tp_descr_set */
3070 0, /* tp_dictoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003071 (initproc)bytearray_init, /* tp_init */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003072 PyType_GenericAlloc, /* tp_alloc */
3073 PyType_GenericNew, /* tp_new */
3074 PyObject_Del, /* tp_free */
3075};
3076
3077/*********************** Bytes Iterator ****************************/
3078
3079typedef struct {
3080 PyObject_HEAD
3081 Py_ssize_t it_index;
3082 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
3083} bytesiterobject;
3084
3085static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003086bytearrayiter_dealloc(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003087{
3088 _PyObject_GC_UNTRACK(it);
3089 Py_XDECREF(it->it_seq);
3090 PyObject_GC_Del(it);
3091}
3092
3093static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003094bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003095{
3096 Py_VISIT(it->it_seq);
3097 return 0;
3098}
3099
3100static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003101bytearrayiter_next(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003102{
3103 PyByteArrayObject *seq;
3104 PyObject *item;
3105
3106 assert(it != NULL);
3107 seq = it->it_seq;
3108 if (seq == NULL)
3109 return NULL;
3110 assert(PyByteArray_Check(seq));
3111
3112 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
3113 item = PyLong_FromLong(
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02003114 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003115 if (item != NULL)
3116 ++it->it_index;
3117 return item;
3118 }
3119
3120 Py_DECREF(seq);
3121 it->it_seq = NULL;
3122 return NULL;
3123}
3124
3125static PyObject *
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003126bytearrayiter_length_hint(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003127{
3128 Py_ssize_t len = 0;
3129 if (it->it_seq)
3130 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
3131 return PyLong_FromSsize_t(len);
3132}
3133
3134PyDoc_STRVAR(length_hint_doc,
3135 "Private method returning an estimate of len(list(it)).");
3136
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003137static PyObject *
3138bytearrayiter_reduce(bytesiterobject *it)
3139{
3140 if (it->it_seq != NULL) {
Antoine Pitroua7013882012-04-05 00:04:20 +02003141 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003142 it->it_seq, it->it_index);
3143 } else {
3144 PyObject *u = PyUnicode_FromUnicode(NULL, 0);
3145 if (u == NULL)
3146 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +02003147 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003148 }
3149}
3150
3151static PyObject *
3152bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
3153{
3154 Py_ssize_t index = PyLong_AsSsize_t(state);
3155 if (index == -1 && PyErr_Occurred())
3156 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003157 if (it->it_seq != NULL) {
3158 if (index < 0)
3159 index = 0;
3160 else if (index > PyByteArray_GET_SIZE(it->it_seq))
3161 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
3162 it->it_index = index;
3163 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003164 Py_RETURN_NONE;
3165}
3166
3167PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
3168
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003169static PyMethodDef bytearrayiter_methods[] = {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003170 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003171 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003172 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003173 bytearray_reduce__doc__},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003174 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
3175 setstate_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003176 {NULL, NULL} /* sentinel */
3177};
3178
3179PyTypeObject PyByteArrayIter_Type = {
3180 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3181 "bytearray_iterator", /* tp_name */
3182 sizeof(bytesiterobject), /* tp_basicsize */
3183 0, /* tp_itemsize */
3184 /* methods */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003185 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003186 0, /* tp_print */
3187 0, /* tp_getattr */
3188 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00003189 0, /* tp_reserved */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003190 0, /* tp_repr */
3191 0, /* tp_as_number */
3192 0, /* tp_as_sequence */
3193 0, /* tp_as_mapping */
3194 0, /* tp_hash */
3195 0, /* tp_call */
3196 0, /* tp_str */
3197 PyObject_GenericGetAttr, /* tp_getattro */
3198 0, /* tp_setattro */
3199 0, /* tp_as_buffer */
3200 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
3201 0, /* tp_doc */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003202 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003203 0, /* tp_clear */
3204 0, /* tp_richcompare */
3205 0, /* tp_weaklistoffset */
3206 PyObject_SelfIter, /* tp_iter */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003207 (iternextfunc)bytearrayiter_next, /* tp_iternext */
3208 bytearrayiter_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003209 0,
3210};
3211
3212static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003213bytearray_iter(PyObject *seq)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003214{
3215 bytesiterobject *it;
3216
3217 if (!PyByteArray_Check(seq)) {
3218 PyErr_BadInternalCall();
3219 return NULL;
3220 }
3221 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
3222 if (it == NULL)
3223 return NULL;
3224 it->it_index = 0;
3225 Py_INCREF(seq);
3226 it->it_seq = (PyByteArrayObject *)seq;
3227 _PyObject_GC_TRACK(it);
3228 return (PyObject *)it;
3229}