blob: 3fad6d80b59e5a717b8a4cb55c76de52ea27cdf5 [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{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000249 Py_buffer va, vb;
250 PyByteArrayObject *result = NULL;
251
252 va.len = -1;
253 vb.len = -1;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200254 if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
255 PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000256 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
257 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
258 goto done;
259 }
260
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300261 if (va.len > PY_SSIZE_T_MAX - vb.len) {
262 PyErr_NoMemory();
263 goto done;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000264 }
265
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300266 result = (PyByteArrayObject *) \
267 PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000268 if (result != NULL) {
269 memcpy(result->ob_bytes, va.buf, va.len);
270 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
271 }
272
273 done:
274 if (va.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000275 PyBuffer_Release(&va);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000276 if (vb.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000277 PyBuffer_Release(&vb);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000278 return (PyObject *)result;
279}
280
Ethan Furmanb95b5612015-01-23 20:05:18 -0800281static PyObject *
282bytearray_format(PyByteArrayObject *self, PyObject *args)
283{
284 PyObject *bytes_in, *bytes_out, *res;
285 char *bytestring;
Serhiy Storchakac9ad8b72016-12-28 09:54:22 +0200286 Py_ssize_t bytesize;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800287
288 if (self == NULL || !PyByteArray_Check(self) || args == NULL) {
289 PyErr_BadInternalCall();
290 return NULL;
291 }
292 bytestring = PyByteArray_AS_STRING(self);
Serhiy Storchakac9ad8b72016-12-28 09:54:22 +0200293 bytesize = PyByteArray_GET_SIZE(self);
294 bytes_in = PyBytes_FromStringAndSize(bytestring, bytesize);
Ethan Furmanb95b5612015-01-23 20:05:18 -0800295 if (bytes_in == NULL)
296 return NULL;
297 bytes_out = _PyBytes_Format(bytes_in, args);
298 Py_DECREF(bytes_in);
299 if (bytes_out == NULL)
300 return NULL;
301 res = PyByteArray_FromObject(bytes_out);
302 Py_DECREF(bytes_out);
303 if (res == NULL)
304 return NULL;
305 return res;
306}
307
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000308/* Functions stuffed into the type object */
309
310static Py_ssize_t
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000311bytearray_length(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000312{
313 return Py_SIZE(self);
314}
315
316static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000317bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000318{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000319 Py_ssize_t size;
320 Py_buffer vo;
321
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200322 if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000323 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
324 Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
325 return NULL;
326 }
327
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300328 size = Py_SIZE(self);
329 if (size > PY_SSIZE_T_MAX - vo.len) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000330 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000331 return PyErr_NoMemory();
332 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300333 if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000334 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000335 return NULL;
336 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300337 memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len);
Martin v. Löwis423be952008-08-13 15:53:07 +0000338 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000339 Py_INCREF(self);
340 return (PyObject *)self;
341}
342
343static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000344bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000345{
346 PyByteArrayObject *result;
347 Py_ssize_t mysize;
348 Py_ssize_t size;
349
350 if (count < 0)
351 count = 0;
352 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000353 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000354 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000355 size = mysize * count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000356 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
357 if (result != NULL && size != 0) {
358 if (mysize == 1)
359 memset(result->ob_bytes, self->ob_bytes[0], size);
360 else {
361 Py_ssize_t i;
362 for (i = 0; i < count; i++)
363 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
364 }
365 }
366 return (PyObject *)result;
367}
368
369static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000370bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000371{
372 Py_ssize_t mysize;
373 Py_ssize_t size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200374 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000375
376 if (count < 0)
377 count = 0;
378 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000379 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000380 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000381 size = mysize * count;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200382 if (PyByteArray_Resize((PyObject *)self, size) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000383 return NULL;
384
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200385 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000386 if (mysize == 1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200387 memset(buf, buf[0], size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000388 else {
389 Py_ssize_t i;
390 for (i = 1; i < count; i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200391 memcpy(buf + i*mysize, buf, mysize);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000392 }
393
394 Py_INCREF(self);
395 return (PyObject *)self;
396}
397
398static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000399bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000400{
401 if (i < 0)
402 i += Py_SIZE(self);
403 if (i < 0 || i >= Py_SIZE(self)) {
404 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
405 return NULL;
406 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200407 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000408}
409
410static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000411bytearray_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000412{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000413 if (PyIndex_Check(index)) {
414 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000415
416 if (i == -1 && PyErr_Occurred())
417 return NULL;
418
419 if (i < 0)
420 i += PyByteArray_GET_SIZE(self);
421
422 if (i < 0 || i >= Py_SIZE(self)) {
423 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
424 return NULL;
425 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200426 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000427 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000428 else if (PySlice_Check(index)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000429 Py_ssize_t start, stop, step, slicelength, cur, i;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000430 if (PySlice_GetIndicesEx(index,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000431 PyByteArray_GET_SIZE(self),
432 &start, &stop, &step, &slicelength) < 0) {
433 return NULL;
434 }
435
436 if (slicelength <= 0)
437 return PyByteArray_FromStringAndSize("", 0);
438 else if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200439 return PyByteArray_FromStringAndSize(
440 PyByteArray_AS_STRING(self) + start, slicelength);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000441 }
442 else {
443 char *source_buf = PyByteArray_AS_STRING(self);
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000444 char *result_buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000445 PyObject *result;
446
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000447 result = PyByteArray_FromStringAndSize(NULL, slicelength);
448 if (result == NULL)
449 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000450
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000451 result_buf = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000452 for (cur = start, i = 0; i < slicelength;
453 cur += step, i++) {
454 result_buf[i] = source_buf[cur];
455 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000456 return result;
457 }
458 }
459 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400460 PyErr_Format(PyExc_TypeError,
461 "bytearray indices must be integers or slices, not %.200s",
462 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000463 return NULL;
464 }
465}
466
467static int
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200468bytearray_setslice_linear(PyByteArrayObject *self,
469 Py_ssize_t lo, Py_ssize_t hi,
470 char *bytes, Py_ssize_t bytes_len)
471{
472 Py_ssize_t avail = hi - lo;
473 char *buf = PyByteArray_AS_STRING(self);
474 Py_ssize_t growth = bytes_len - avail;
Victor Stinner84557232013-11-21 12:29:51 +0100475 int res = 0;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200476 assert(avail >= 0);
477
Victor Stinner84557232013-11-21 12:29:51 +0100478 if (growth < 0) {
479 if (!_canresize(self))
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200480 return -1;
Victor Stinner84557232013-11-21 12:29:51 +0100481
482 if (lo == 0) {
483 /* Shrink the buffer by advancing its logical start */
484 self->ob_start -= growth;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200485 /*
Victor Stinner84557232013-11-21 12:29:51 +0100486 0 lo hi old_size
487 | |<----avail----->|<-----tail------>|
488 | |<-bytes_len->|<-----tail------>|
489 0 new_lo new_hi new_size
490 */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200491 }
Victor Stinner84557232013-11-21 12:29:51 +0100492 else {
493 /*
494 0 lo hi old_size
495 | |<----avail----->|<-----tomove------>|
496 | |<-bytes_len->|<-----tomove------>|
497 0 lo new_hi new_size
498 */
499 memmove(buf + lo + bytes_len, buf + hi,
500 Py_SIZE(self) - hi);
501 }
502 if (PyByteArray_Resize((PyObject *)self,
503 Py_SIZE(self) + growth) < 0) {
504 /* Issue #19578: Handling the memory allocation failure here is
505 tricky here because the bytearray object has already been
506 modified. Depending on growth and lo, the behaviour is
507 different.
508
509 If growth < 0 and lo != 0, the operation is completed, but a
510 MemoryError is still raised and the memory block is not
Martin Panter0be894b2016-09-07 12:03:06 +0000511 shrunk. Otherwise, the bytearray is restored in its previous
Victor Stinner84557232013-11-21 12:29:51 +0100512 state and a MemoryError is raised. */
513 if (lo == 0) {
514 self->ob_start += growth;
515 return -1;
516 }
517 /* memmove() removed bytes, the bytearray object cannot be
518 restored in its previous state. */
519 Py_SIZE(self) += growth;
520 res = -1;
521 }
522 buf = PyByteArray_AS_STRING(self);
523 }
524 else if (growth > 0) {
525 if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
526 PyErr_NoMemory();
527 return -1;
528 }
529
530 if (PyByteArray_Resize((PyObject *)self,
531 Py_SIZE(self) + growth) < 0) {
532 return -1;
533 }
534 buf = PyByteArray_AS_STRING(self);
535 /* Make the place for the additional bytes */
536 /*
537 0 lo hi old_size
538 | |<-avail->|<-----tomove------>|
539 | |<---bytes_len-->|<-----tomove------>|
540 0 lo new_hi new_size
541 */
542 memmove(buf + lo + bytes_len, buf + hi,
543 Py_SIZE(self) - lo - bytes_len);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200544 }
545
546 if (bytes_len > 0)
547 memcpy(buf + lo, bytes, bytes_len);
Victor Stinner84557232013-11-21 12:29:51 +0100548 return res;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200549}
550
551static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000552bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000553 PyObject *values)
554{
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200555 Py_ssize_t needed;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000556 void *bytes;
557 Py_buffer vbytes;
558 int res = 0;
559
560 vbytes.len = -1;
561 if (values == (PyObject *)self) {
562 /* Make a copy and call this function recursively */
563 int err;
564 values = PyByteArray_FromObject(values);
565 if (values == NULL)
566 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000567 err = bytearray_setslice(self, lo, hi, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000568 Py_DECREF(values);
569 return err;
570 }
571 if (values == NULL) {
572 /* del b[lo:hi] */
573 bytes = NULL;
574 needed = 0;
575 }
576 else {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200577 if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
578 PyErr_Format(PyExc_TypeError,
579 "can't set bytearray slice from %.100s",
580 Py_TYPE(values)->tp_name);
581 return -1;
582 }
583 needed = vbytes.len;
584 bytes = vbytes.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000585 }
586
587 if (lo < 0)
588 lo = 0;
589 if (hi < lo)
590 hi = lo;
591 if (hi > Py_SIZE(self))
592 hi = Py_SIZE(self);
593
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200594 res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000595 if (vbytes.len != -1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200596 PyBuffer_Release(&vbytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000597 return res;
598}
599
600static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000601bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000602{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000603 int ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000604
605 if (i < 0)
606 i += Py_SIZE(self);
607
608 if (i < 0 || i >= Py_SIZE(self)) {
609 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
610 return -1;
611 }
612
613 if (value == NULL)
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000614 return bytearray_setslice(self, i, i+1, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000615
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000616 if (!_getbytevalue(value, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000617 return -1;
618
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200619 PyByteArray_AS_STRING(self)[i] = ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000620 return 0;
621}
622
623static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000624bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000625{
626 Py_ssize_t start, stop, step, slicelen, needed;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200627 char *buf, *bytes;
628 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000629
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000630 if (PyIndex_Check(index)) {
631 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000632
633 if (i == -1 && PyErr_Occurred())
634 return -1;
635
636 if (i < 0)
637 i += PyByteArray_GET_SIZE(self);
638
639 if (i < 0 || i >= Py_SIZE(self)) {
640 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
641 return -1;
642 }
643
644 if (values == NULL) {
645 /* Fall through to slice assignment */
646 start = i;
647 stop = i + 1;
648 step = 1;
649 slicelen = 1;
650 }
651 else {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000652 int ival;
653 if (!_getbytevalue(values, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000654 return -1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200655 buf[i] = (char)ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000656 return 0;
657 }
658 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000659 else if (PySlice_Check(index)) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000660 if (PySlice_GetIndicesEx(index,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000661 PyByteArray_GET_SIZE(self),
662 &start, &stop, &step, &slicelen) < 0) {
663 return -1;
664 }
665 }
666 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400667 PyErr_Format(PyExc_TypeError,
668 "bytearray indices must be integers or slices, not %.200s",
669 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000670 return -1;
671 }
672
673 if (values == NULL) {
674 bytes = NULL;
675 needed = 0;
676 }
677 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
Christian Heimes6d26ade2012-11-03 23:07:59 +0100678 int err;
Ezio Melottic64bcbe2012-11-03 21:19:06 +0200679 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
680 PyErr_SetString(PyExc_TypeError,
681 "can assign only bytes, buffers, or iterables "
682 "of ints in range(0, 256)");
683 return -1;
684 }
Georg Brandlf3fa5682010-12-04 17:09:30 +0000685 /* Make a copy and call this function recursively */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000686 values = PyByteArray_FromObject(values);
687 if (values == NULL)
688 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000689 err = bytearray_ass_subscript(self, index, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000690 Py_DECREF(values);
691 return err;
692 }
693 else {
694 assert(PyByteArray_Check(values));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200695 bytes = PyByteArray_AS_STRING(values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000696 needed = Py_SIZE(values);
697 }
698 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
699 if ((step < 0 && start < stop) ||
700 (step > 0 && start > stop))
701 stop = start;
702 if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200703 return bytearray_setslice_linear(self, start, stop, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000704 }
705 else {
706 if (needed == 0) {
707 /* Delete slice */
Mark Dickinsonbc099642010-01-29 17:27:24 +0000708 size_t cur;
709 Py_ssize_t i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000710
Antoine Pitrou5504e892008-12-06 21:27:53 +0000711 if (!_canresize(self))
712 return -1;
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000713
714 if (slicelen == 0)
715 /* Nothing to do here. */
716 return 0;
717
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000718 if (step < 0) {
719 stop = start + 1;
720 start = stop + step * (slicelen - 1) - 1;
721 step = -step;
722 }
723 for (cur = start, i = 0;
724 i < slicelen; cur += step, i++) {
725 Py_ssize_t lim = step - 1;
726
Mark Dickinson66f575b2010-02-14 12:53:32 +0000727 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000728 lim = PyByteArray_GET_SIZE(self) - cur - 1;
729
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200730 memmove(buf + cur - i,
731 buf + cur + 1, lim);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000732 }
733 /* Move the tail of the bytes, in one chunk */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000734 cur = start + (size_t)slicelen*step;
Mark Dickinson66f575b2010-02-14 12:53:32 +0000735 if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200736 memmove(buf + cur - slicelen,
737 buf + cur,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000738 PyByteArray_GET_SIZE(self) - cur);
739 }
740 if (PyByteArray_Resize((PyObject *)self,
741 PyByteArray_GET_SIZE(self) - slicelen) < 0)
742 return -1;
743
744 return 0;
745 }
746 else {
747 /* Assign slice */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000748 Py_ssize_t i;
749 size_t cur;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000750
751 if (needed != slicelen) {
752 PyErr_Format(PyExc_ValueError,
753 "attempt to assign bytes of size %zd "
754 "to extended slice of size %zd",
755 needed, slicelen);
756 return -1;
757 }
758 for (cur = start, i = 0; i < slicelen; cur += step, i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200759 buf[cur] = bytes[i];
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000760 return 0;
761 }
762 }
763}
764
765static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000766bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000767{
768 static char *kwlist[] = {"source", "encoding", "errors", 0};
769 PyObject *arg = NULL;
770 const char *encoding = NULL;
771 const char *errors = NULL;
772 Py_ssize_t count;
773 PyObject *it;
774 PyObject *(*iternext)(PyObject *);
775
776 if (Py_SIZE(self) != 0) {
777 /* Empty previous contents (yes, do this first of all!) */
778 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
779 return -1;
780 }
781
782 /* Parse arguments */
Georg Brandl3dbca812008-07-23 16:10:53 +0000783 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000784 &arg, &encoding, &errors))
785 return -1;
786
787 /* Make a quick exit if no first argument */
788 if (arg == NULL) {
789 if (encoding != NULL || errors != NULL) {
790 PyErr_SetString(PyExc_TypeError,
791 "encoding or errors without sequence argument");
792 return -1;
793 }
794 return 0;
795 }
796
797 if (PyUnicode_Check(arg)) {
798 /* Encode via the codec registry */
799 PyObject *encoded, *new;
800 if (encoding == NULL) {
801 PyErr_SetString(PyExc_TypeError,
802 "string argument without an encoding");
803 return -1;
804 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000805 encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000806 if (encoded == NULL)
807 return -1;
808 assert(PyBytes_Check(encoded));
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000809 new = bytearray_iconcat(self, encoded);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000810 Py_DECREF(encoded);
811 if (new == NULL)
812 return -1;
813 Py_DECREF(new);
814 return 0;
815 }
816
817 /* If it's not unicode, there can't be encoding or errors */
818 if (encoding != NULL || errors != NULL) {
819 PyErr_SetString(PyExc_TypeError,
820 "encoding or errors without a string argument");
821 return -1;
822 }
823
824 /* Is it an int? */
Benjamin Peterson8380dd52010-04-16 22:51:37 +0000825 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
826 if (count == -1 && PyErr_Occurred()) {
827 if (PyErr_ExceptionMatches(PyExc_OverflowError))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000828 return -1;
Benjamin Peterson9c0e94f2010-04-16 23:00:53 +0000829 PyErr_Clear();
Benjamin Peterson8380dd52010-04-16 22:51:37 +0000830 }
831 else if (count < 0) {
832 PyErr_SetString(PyExc_ValueError, "negative count");
833 return -1;
834 }
835 else {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000836 if (count > 0) {
Victor Stinner2bc4d952014-06-02 22:22:42 +0200837 if (PyByteArray_Resize((PyObject *)self, count))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000838 return -1;
Victor Stinner2bc4d952014-06-02 22:22:42 +0200839 memset(PyByteArray_AS_STRING(self), 0, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000840 }
841 return 0;
842 }
843
844 /* Use the buffer API */
845 if (PyObject_CheckBuffer(arg)) {
846 Py_ssize_t size;
847 Py_buffer view;
848 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
849 return -1;
850 size = view.len;
851 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200852 if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
853 &view, size, 'C') < 0)
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200854 goto fail;
Martin v. Löwis423be952008-08-13 15:53:07 +0000855 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000856 return 0;
857 fail:
Martin v. Löwis423be952008-08-13 15:53:07 +0000858 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000859 return -1;
860 }
861
862 /* XXX Optimize this if the arguments is a list, tuple */
863
864 /* Get the iterator */
865 it = PyObject_GetIter(arg);
866 if (it == NULL)
867 return -1;
868 iternext = *Py_TYPE(it)->tp_iternext;
869
870 /* Run the iterator to exhaustion */
871 for (;;) {
872 PyObject *item;
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000873 int rc, value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000874
875 /* Get the next item */
876 item = iternext(it);
877 if (item == NULL) {
878 if (PyErr_Occurred()) {
879 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
880 goto error;
881 PyErr_Clear();
882 }
883 break;
884 }
885
886 /* Interpret it as an int (__index__) */
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000887 rc = _getbytevalue(item, &value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000888 Py_DECREF(item);
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000889 if (!rc)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000890 goto error;
891
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000892 /* Append the byte */
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300893 if (Py_SIZE(self) + 1 < self->ob_alloc) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000894 Py_SIZE(self)++;
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300895 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
896 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000897 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
898 goto error;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200899 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000900 }
901
902 /* Clean up and return success */
903 Py_DECREF(it);
904 return 0;
905
906 error:
907 /* Error handling when it != NULL */
908 Py_DECREF(it);
909 return -1;
910}
911
912/* Mostly copied from string_repr, but without the
913 "smart quote" functionality. */
914static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000915bytearray_repr(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000916{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000917 const char *quote_prefix = "bytearray(b";
918 const char *quote_postfix = ")";
919 Py_ssize_t length = Py_SIZE(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200920 /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
Mark Dickinson66f575b2010-02-14 12:53:32 +0000921 size_t newsize;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000922 PyObject *v;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200923 Py_ssize_t i;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200924 char *bytes;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200925 char c;
926 char *p;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200927 int quote;
928 char *test, *start;
929 char *buffer;
930
931 if (length > (PY_SSIZE_T_MAX - 15) / 4) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000932 PyErr_SetString(PyExc_OverflowError,
933 "bytearray object is too large to make repr");
934 return NULL;
935 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200936
937 newsize = 15 + length * 4;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100938 buffer = PyObject_Malloc(newsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200939 if (buffer == NULL) {
940 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000941 return NULL;
942 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000943
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200944 /* Figure out which quote to use; single is preferred */
945 quote = '\'';
946 start = PyByteArray_AS_STRING(self);
947 for (test = start; test < start+length; ++test) {
948 if (*test == '"') {
949 quote = '\''; /* back to single */
950 break;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000951 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200952 else if (*test == '\'')
953 quote = '"';
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000954 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200955
956 p = buffer;
957 while (*quote_prefix)
958 *p++ = *quote_prefix++;
959 *p++ = quote;
960
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200961 bytes = PyByteArray_AS_STRING(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200962 for (i = 0; i < length; i++) {
963 /* There's at least enough room for a hex escape
964 and a closing quote. */
965 assert(newsize - (p - buffer) >= 5);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200966 c = bytes[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200967 if (c == '\'' || c == '\\')
968 *p++ = '\\', *p++ = c;
969 else if (c == '\t')
970 *p++ = '\\', *p++ = 't';
971 else if (c == '\n')
972 *p++ = '\\', *p++ = 'n';
973 else if (c == '\r')
974 *p++ = '\\', *p++ = 'r';
975 else if (c == 0)
976 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
977 else if (c < ' ' || c >= 0x7f) {
978 *p++ = '\\';
979 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200980 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
981 *p++ = Py_hexdigits[c & 0xf];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200982 }
983 else
984 *p++ = c;
985 }
986 assert(newsize - (p - buffer) >= 1);
987 *p++ = quote;
988 while (*quote_postfix) {
989 *p++ = *quote_postfix++;
990 }
991
992 v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100993 PyObject_Free(buffer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200994 return v;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000995}
996
997static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000998bytearray_str(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000999{
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001000 if (Py_BytesWarningFlag) {
1001 if (PyErr_WarnEx(PyExc_BytesWarning,
1002 "str() on a bytearray instance", 1))
1003 return NULL;
1004 }
1005 return bytearray_repr((PyByteArrayObject*)op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001006}
1007
1008static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001009bytearray_richcompare(PyObject *self, PyObject *other, int op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001010{
1011 Py_ssize_t self_size, other_size;
1012 Py_buffer self_bytes, other_bytes;
1013 PyObject *res;
1014 Py_ssize_t minsize;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001015 int cmp, rc;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001016
1017 /* Bytes can be compared to anything that supports the (binary)
1018 buffer API. Except that a comparison with Unicode is always an
1019 error, even if the comparison is for equality. */
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001020 rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
1021 if (!rc)
1022 rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
1023 if (rc < 0)
1024 return NULL;
1025 if (rc) {
Barry Warsaw9e9dcd62008-10-17 01:50:37 +00001026 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001027 if (PyErr_WarnEx(PyExc_BytesWarning,
Georg Brandle5d68ac2008-06-04 11:30:26 +00001028 "Comparison between bytearray and string", 1))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001029 return NULL;
1030 }
1031
Brian Curtindfc80e32011-08-10 20:28:54 -05001032 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001033 }
1034
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001035 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001036 PyErr_Clear();
Brian Curtindfc80e32011-08-10 20:28:54 -05001037 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001038 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001039 self_size = self_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001040
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001041 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001042 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00001043 PyBuffer_Release(&self_bytes);
Brian Curtindfc80e32011-08-10 20:28:54 -05001044 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001045 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001046 other_size = other_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001047
1048 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1049 /* Shortcut: if the lengths differ, the objects differ */
1050 cmp = (op == Py_NE);
1051 }
1052 else {
1053 minsize = self_size;
1054 if (other_size < minsize)
1055 minsize = other_size;
1056
1057 cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
1058 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1059
1060 if (cmp == 0) {
1061 if (self_size < other_size)
1062 cmp = -1;
1063 else if (self_size > other_size)
1064 cmp = 1;
1065 }
1066
1067 switch (op) {
1068 case Py_LT: cmp = cmp < 0; break;
1069 case Py_LE: cmp = cmp <= 0; break;
1070 case Py_EQ: cmp = cmp == 0; break;
1071 case Py_NE: cmp = cmp != 0; break;
1072 case Py_GT: cmp = cmp > 0; break;
1073 case Py_GE: cmp = cmp >= 0; break;
1074 }
1075 }
1076
1077 res = cmp ? Py_True : Py_False;
Martin v. Löwis423be952008-08-13 15:53:07 +00001078 PyBuffer_Release(&self_bytes);
1079 PyBuffer_Release(&other_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001080 Py_INCREF(res);
1081 return res;
1082}
1083
1084static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001085bytearray_dealloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001086{
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001087 if (self->ob_exports > 0) {
1088 PyErr_SetString(PyExc_SystemError,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001089 "deallocated bytearray object has exported buffers");
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001090 PyErr_Print();
1091 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001092 if (self->ob_bytes != 0) {
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001093 PyObject_Free(self->ob_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001094 }
1095 Py_TYPE(self)->tp_free((PyObject *)self);
1096}
1097
1098
1099/* -------------------------------------------------------------------- */
1100/* Methods */
1101
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001102#define FASTSEARCH fastsearch
1103#define STRINGLIB(F) stringlib_##F
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001104#define STRINGLIB_CHAR char
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001105#define STRINGLIB_SIZEOF_CHAR 1
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001106#define STRINGLIB_LEN PyByteArray_GET_SIZE
1107#define STRINGLIB_STR PyByteArray_AS_STRING
1108#define STRINGLIB_NEW PyByteArray_FromStringAndSize
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001109#define STRINGLIB_ISSPACE Py_ISSPACE
1110#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001111#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1112#define STRINGLIB_MUTABLE 1
1113
1114#include "stringlib/fastsearch.h"
1115#include "stringlib/count.h"
1116#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001117#include "stringlib/join.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001118#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001119#include "stringlib/split.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001120#include "stringlib/ctype.h"
1121#include "stringlib/transmogrify.h"
1122
1123
1124/* The following Py_LOCAL_INLINE and Py_LOCAL functions
1125were copied from the old char* style string object. */
1126
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001127/* helper macro to fixup start/end slice values */
1128#define ADJUST_INDICES(start, end, len) \
1129 if (end > len) \
1130 end = len; \
1131 else if (end < 0) { \
1132 end += len; \
1133 if (end < 0) \
1134 end = 0; \
1135 } \
1136 if (start < 0) { \
1137 start += len; \
1138 if (start < 0) \
1139 start = 0; \
1140 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001141
1142Py_LOCAL_INLINE(Py_ssize_t)
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001143bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001144{
1145 PyObject *subobj;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001146 char byte;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001147 Py_buffer subbuf;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001148 const char *sub;
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001149 Py_ssize_t len, sub_len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001150 Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
1151 Py_ssize_t res;
1152
Antoine Pitrouac65d962011-10-20 23:54:17 +02001153 if (!stringlib_parse_args_finds_byte("find/rfind/index/rindex",
1154 args, &subobj, &byte, &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001155 return -2;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001156
1157 if (subobj) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001158 if (PyObject_GetBuffer(subobj, &subbuf, PyBUF_SIMPLE) != 0)
Antoine Pitrouac65d962011-10-20 23:54:17 +02001159 return -2;
1160
1161 sub = subbuf.buf;
1162 sub_len = subbuf.len;
1163 }
1164 else {
1165 sub = &byte;
1166 sub_len = 1;
1167 }
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001168 len = PyByteArray_GET_SIZE(self);
Antoine Pitrouac65d962011-10-20 23:54:17 +02001169
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001170 ADJUST_INDICES(start, end, len);
1171 if (end - start < sub_len)
1172 res = -1;
Serhiy Storchakad92d4ef2015-07-20 22:58:02 +03001173 else if (sub_len == 1
1174#ifndef HAVE_MEMRCHR
1175 && dir > 0
1176#endif
1177 ) {
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001178 unsigned char needle = *sub;
Serhiy Storchakad92d4ef2015-07-20 22:58:02 +03001179 int mode = (dir > 0) ? FAST_SEARCH : FAST_RSEARCH;
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001180 res = stringlib_fastsearch_memchr_1char(
1181 PyByteArray_AS_STRING(self) + start, end - start,
Serhiy Storchakad92d4ef2015-07-20 22:58:02 +03001182 needle, needle, mode);
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001183 if (res >= 0)
1184 res += start;
1185 }
1186 else {
1187 if (dir > 0)
1188 res = stringlib_find_slice(
1189 PyByteArray_AS_STRING(self), len,
1190 sub, sub_len, start, end);
1191 else
1192 res = stringlib_rfind_slice(
1193 PyByteArray_AS_STRING(self), len,
1194 sub, sub_len, start, end);
1195 }
Antoine Pitrouac65d962011-10-20 23:54:17 +02001196
1197 if (subobj)
1198 PyBuffer_Release(&subbuf);
1199
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001200 return res;
1201}
1202
1203PyDoc_STRVAR(find__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001204"B.find(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001205\n\
1206Return the lowest index in B where subsection sub is found,\n\
Senthil Kumaran53516a82011-07-27 23:33:54 +08001207such that sub is contained within B[start,end]. Optional\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001208arguments start and end are interpreted as in slice notation.\n\
1209\n\
1210Return -1 on failure.");
1211
1212static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001213bytearray_find(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001214{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001215 Py_ssize_t result = bytearray_find_internal(self, args, +1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001216 if (result == -2)
1217 return NULL;
1218 return PyLong_FromSsize_t(result);
1219}
1220
1221PyDoc_STRVAR(count__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001222"B.count(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001223\n\
1224Return the number of non-overlapping occurrences of subsection sub in\n\
1225bytes B[start:end]. Optional arguments start and end are interpreted\n\
1226as in slice notation.");
1227
1228static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001229bytearray_count(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001230{
1231 PyObject *sub_obj;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001232 const char *str = PyByteArray_AS_STRING(self), *sub;
1233 Py_ssize_t sub_len;
1234 char byte;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001235 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001236
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001237 Py_buffer vsub;
1238 PyObject *count_obj;
1239
Antoine Pitrouac65d962011-10-20 23:54:17 +02001240 if (!stringlib_parse_args_finds_byte("count", args, &sub_obj, &byte,
1241 &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001242 return NULL;
1243
Antoine Pitrouac65d962011-10-20 23:54:17 +02001244 if (sub_obj) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001245 if (PyObject_GetBuffer(sub_obj, &vsub, PyBUF_SIMPLE) != 0)
Antoine Pitrouac65d962011-10-20 23:54:17 +02001246 return NULL;
1247
1248 sub = vsub.buf;
1249 sub_len = vsub.len;
1250 }
1251 else {
1252 sub = &byte;
1253 sub_len = 1;
1254 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001255
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001256 ADJUST_INDICES(start, end, PyByteArray_GET_SIZE(self));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001257
1258 count_obj = PyLong_FromSsize_t(
Antoine Pitrouac65d962011-10-20 23:54:17 +02001259 stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001260 );
Antoine Pitrouac65d962011-10-20 23:54:17 +02001261
1262 if (sub_obj)
1263 PyBuffer_Release(&vsub);
1264
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001265 return count_obj;
1266}
1267
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001268/*[clinic input]
1269bytearray.clear
1270
1271 self: self(type="PyByteArrayObject *")
1272
1273Remove all items from the bytearray.
1274[clinic start generated code]*/
1275
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001276static PyObject *
1277bytearray_clear_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001278/*[clinic end generated code: output=85c2fe6aede0956c input=e524fd330abcdc18]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001279{
1280 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1281 return NULL;
1282 Py_RETURN_NONE;
1283}
1284
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001285/*[clinic input]
1286bytearray.copy
1287
1288 self: self(type="PyByteArrayObject *")
1289
1290Return a copy of B.
1291[clinic start generated code]*/
1292
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001293static PyObject *
1294bytearray_copy_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001295/*[clinic end generated code: output=68cfbcfed484c132 input=6d5d2975aa0f33f3]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001296{
1297 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1298 PyByteArray_GET_SIZE(self));
1299}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001300
1301PyDoc_STRVAR(index__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001302"B.index(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001303\n\
1304Like B.find() but raise ValueError when the subsection is not found.");
1305
1306static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001307bytearray_index(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001308{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001309 Py_ssize_t result = bytearray_find_internal(self, args, +1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001310 if (result == -2)
1311 return NULL;
1312 if (result == -1) {
1313 PyErr_SetString(PyExc_ValueError,
1314 "subsection not found");
1315 return NULL;
1316 }
1317 return PyLong_FromSsize_t(result);
1318}
1319
1320
1321PyDoc_STRVAR(rfind__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001322"B.rfind(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001323\n\
1324Return the highest index in B where subsection sub is found,\n\
Senthil Kumaran53516a82011-07-27 23:33:54 +08001325such that sub is contained within B[start,end]. Optional\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001326arguments start and end are interpreted as in slice notation.\n\
1327\n\
1328Return -1 on failure.");
1329
1330static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001331bytearray_rfind(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001332{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001333 Py_ssize_t result = bytearray_find_internal(self, args, -1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001334 if (result == -2)
1335 return NULL;
1336 return PyLong_FromSsize_t(result);
1337}
1338
1339
1340PyDoc_STRVAR(rindex__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001341"B.rindex(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001342\n\
1343Like B.rfind() but raise ValueError when the subsection is not found.");
1344
1345static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001346bytearray_rindex(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001347{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001348 Py_ssize_t result = bytearray_find_internal(self, args, -1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001349 if (result == -2)
1350 return NULL;
1351 if (result == -1) {
1352 PyErr_SetString(PyExc_ValueError,
1353 "subsection not found");
1354 return NULL;
1355 }
1356 return PyLong_FromSsize_t(result);
1357}
1358
1359
1360static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001361bytearray_contains(PyObject *self, PyObject *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001362{
1363 Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError);
1364 if (ival == -1 && PyErr_Occurred()) {
1365 Py_buffer varg;
Antoine Pitrou0010d372010-08-15 17:12:55 +00001366 Py_ssize_t pos;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001367 PyErr_Clear();
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001368 if (PyObject_GetBuffer(arg, &varg, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001369 return -1;
1370 pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self),
1371 varg.buf, varg.len, 0);
Martin v. Löwis423be952008-08-13 15:53:07 +00001372 PyBuffer_Release(&varg);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001373 return pos >= 0;
1374 }
1375 if (ival < 0 || ival >= 256) {
1376 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
1377 return -1;
1378 }
1379
Antoine Pitrou0010d372010-08-15 17:12:55 +00001380 return memchr(PyByteArray_AS_STRING(self), (int) ival, Py_SIZE(self)) != NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001381}
1382
1383
1384/* Matches the end (direction >= 0) or start (direction < 0) of self
1385 * against substr, using the start and end arguments. Returns
1386 * -1 on error, 0 if not found and 1 if found.
1387 */
1388Py_LOCAL(int)
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001389_bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001390 Py_ssize_t end, int direction)
1391{
1392 Py_ssize_t len = PyByteArray_GET_SIZE(self);
1393 const char* str;
1394 Py_buffer vsubstr;
1395 int rv = 0;
1396
1397 str = PyByteArray_AS_STRING(self);
1398
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001399 if (PyObject_GetBuffer(substr, &vsubstr, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001400 return -1;
1401
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001402 ADJUST_INDICES(start, end, len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001403
1404 if (direction < 0) {
1405 /* startswith */
1406 if (start+vsubstr.len > len) {
1407 goto done;
1408 }
1409 } else {
1410 /* endswith */
1411 if (end-start < vsubstr.len || start > len) {
1412 goto done;
1413 }
1414
1415 if (end-vsubstr.len > start)
1416 start = end - vsubstr.len;
1417 }
1418 if (end-start >= vsubstr.len)
1419 rv = ! memcmp(str+start, vsubstr.buf, vsubstr.len);
1420
1421done:
Martin v. Löwis423be952008-08-13 15:53:07 +00001422 PyBuffer_Release(&vsubstr);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001423 return rv;
1424}
1425
1426
1427PyDoc_STRVAR(startswith__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001428"B.startswith(prefix[, start[, end]]) -> bool\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001429\n\
1430Return True if B starts with the specified prefix, False otherwise.\n\
1431With optional start, test B beginning at that position.\n\
1432With optional end, stop comparing B at that position.\n\
Ezio Melottiba42fd52011-04-26 06:09:45 +03001433prefix can also be a tuple of bytes to try.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001434
1435static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001436bytearray_startswith(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001437{
1438 Py_ssize_t start = 0;
1439 Py_ssize_t end = PY_SSIZE_T_MAX;
1440 PyObject *subobj;
1441 int result;
1442
Jesus Ceaac451502011-04-20 17:09:23 +02001443 if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001444 return NULL;
1445 if (PyTuple_Check(subobj)) {
1446 Py_ssize_t i;
1447 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001448 result = _bytearray_tailmatch(self,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001449 PyTuple_GET_ITEM(subobj, i),
1450 start, end, -1);
1451 if (result == -1)
1452 return NULL;
1453 else if (result) {
1454 Py_RETURN_TRUE;
1455 }
1456 }
1457 Py_RETURN_FALSE;
1458 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001459 result = _bytearray_tailmatch(self, subobj, start, end, -1);
Ezio Melottiba42fd52011-04-26 06:09:45 +03001460 if (result == -1) {
1461 if (PyErr_ExceptionMatches(PyExc_TypeError))
1462 PyErr_Format(PyExc_TypeError, "startswith first arg must be bytes "
1463 "or a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001464 return NULL;
Ezio Melottiba42fd52011-04-26 06:09:45 +03001465 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001466 else
1467 return PyBool_FromLong(result);
1468}
1469
1470PyDoc_STRVAR(endswith__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001471"B.endswith(suffix[, start[, end]]) -> bool\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001472\n\
1473Return True if B ends with the specified suffix, False otherwise.\n\
1474With optional start, test B beginning at that position.\n\
1475With optional end, stop comparing B at that position.\n\
Ezio Melottiba42fd52011-04-26 06:09:45 +03001476suffix can also be a tuple of bytes to try.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001477
1478static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001479bytearray_endswith(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001480{
1481 Py_ssize_t start = 0;
1482 Py_ssize_t end = PY_SSIZE_T_MAX;
1483 PyObject *subobj;
1484 int result;
1485
Jesus Ceaac451502011-04-20 17:09:23 +02001486 if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001487 return NULL;
1488 if (PyTuple_Check(subobj)) {
1489 Py_ssize_t i;
1490 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001491 result = _bytearray_tailmatch(self,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001492 PyTuple_GET_ITEM(subobj, i),
1493 start, end, +1);
1494 if (result == -1)
1495 return NULL;
1496 else if (result) {
1497 Py_RETURN_TRUE;
1498 }
1499 }
1500 Py_RETURN_FALSE;
1501 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001502 result = _bytearray_tailmatch(self, subobj, start, end, +1);
Ezio Melottiba42fd52011-04-26 06:09:45 +03001503 if (result == -1) {
1504 if (PyErr_ExceptionMatches(PyExc_TypeError))
1505 PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or "
1506 "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001507 return NULL;
Ezio Melottiba42fd52011-04-26 06:09:45 +03001508 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001509 else
1510 return PyBool_FromLong(result);
1511}
1512
1513
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001514/*[clinic input]
1515bytearray.translate
1516
1517 self: self(type="PyByteArrayObject *")
1518 table: object
1519 Translation table, which must be a bytes object of length 256.
1520 [
1521 deletechars: object
1522 ]
1523 /
1524
1525Return a copy with each character mapped by the given translation table.
1526
1527All characters occurring in the optional argument deletechars are removed.
1528The remaining characters are mapped through the given translation table.
1529[clinic start generated code]*/
1530
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001531static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001532bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
1533 int group_right_1, PyObject *deletechars)
1534/*[clinic end generated code: output=2bebc86a9a1ff083 input=b749ad85f4860824]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001535{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001536 char *input, *output;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001537 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001538 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001539 PyObject *input_obj = (PyObject*)self;
1540 const char *output_start;
1541 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001542 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001543 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001544 Py_buffer vtable, vdel;
1545
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001546 if (table == Py_None) {
1547 table_chars = NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001548 table = NULL;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001549 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001550 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001551 } else {
1552 if (vtable.len != 256) {
1553 PyErr_SetString(PyExc_ValueError,
1554 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001555 PyBuffer_Release(&vtable);
1556 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001557 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001558 table_chars = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001559 }
1560
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001561 if (deletechars != NULL) {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001562 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001563 if (table != NULL)
Georg Brandl953152f2009-07-22 12:03:59 +00001564 PyBuffer_Release(&vtable);
1565 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001566 }
1567 }
1568 else {
1569 vdel.buf = NULL;
1570 vdel.len = 0;
1571 }
1572
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001573 inlen = PyByteArray_GET_SIZE(input_obj);
1574 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1575 if (result == NULL)
1576 goto done;
1577 output_start = output = PyByteArray_AsString(result);
1578 input = PyByteArray_AS_STRING(input_obj);
1579
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001580 if (vdel.len == 0 && table_chars != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001581 /* If no deletions are required, use faster code */
1582 for (i = inlen; --i >= 0; ) {
1583 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001584 *output++ = table_chars[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001585 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001586 goto done;
1587 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001588
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001589 if (table_chars == NULL) {
Georg Brandlccc47b62008-12-28 11:44:14 +00001590 for (i = 0; i < 256; i++)
1591 trans_table[i] = Py_CHARMASK(i);
1592 } else {
1593 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001594 trans_table[i] = Py_CHARMASK(table_chars[i]);
Georg Brandlccc47b62008-12-28 11:44:14 +00001595 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001596
1597 for (i = 0; i < vdel.len; i++)
1598 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1599
1600 for (i = inlen; --i >= 0; ) {
1601 c = Py_CHARMASK(*input++);
1602 if (trans_table[c] != -1)
1603 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1604 continue;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001605 }
1606 /* Fix the size of the resulting string */
1607 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001608 if (PyByteArray_Resize(result, output - output_start) < 0) {
1609 Py_CLEAR(result);
1610 goto done;
1611 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001612
1613done:
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001614 if (table != NULL)
Georg Brandlccc47b62008-12-28 11:44:14 +00001615 PyBuffer_Release(&vtable);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001616 if (deletechars != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001617 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001618 return result;
1619}
1620
1621
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001622/*[clinic input]
1623
1624@staticmethod
1625bytearray.maketrans
1626
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001627 frm: Py_buffer
1628 to: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001629 /
1630
1631Return a translation table useable for the bytes or bytearray translate method.
1632
1633The returned table will be one where each byte in frm is mapped to the byte at
1634the same position in to.
1635
1636The bytes objects frm and to must be of the same length.
1637[clinic start generated code]*/
1638
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001639static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001640bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001641/*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001642{
1643 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00001644}
1645
1646
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001647/* find and count characters and substrings */
1648
1649#define findchar(target, target_len, c) \
1650 ((char *)memchr((const void *)(target), c, target_len))
1651
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001652
Benjamin Peterson0f3641c2008-11-19 22:05:52 +00001653/* Bytes ops must return a string, create a copy */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001654Py_LOCAL(PyByteArrayObject *)
1655return_self(PyByteArrayObject *self)
1656{
Georg Brandl1e7217d2008-05-30 12:02:38 +00001657 /* always return a new bytearray */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001658 return (PyByteArrayObject *)PyByteArray_FromStringAndSize(
1659 PyByteArray_AS_STRING(self),
1660 PyByteArray_GET_SIZE(self));
1661}
1662
1663Py_LOCAL_INLINE(Py_ssize_t)
1664countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount)
1665{
1666 Py_ssize_t count=0;
1667 const char *start=target;
1668 const char *end=target+target_len;
1669
1670 while ( (start=findchar(start, end-start, c)) != NULL ) {
1671 count++;
1672 if (count >= maxcount)
1673 break;
1674 start += 1;
1675 }
1676 return count;
1677}
1678
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001679
1680/* Algorithms for different cases of string replacement */
1681
1682/* len(self)>=1, from="", len(to)>=1, maxcount>=1 */
1683Py_LOCAL(PyByteArrayObject *)
1684replace_interleave(PyByteArrayObject *self,
1685 const char *to_s, Py_ssize_t to_len,
1686 Py_ssize_t maxcount)
1687{
1688 char *self_s, *result_s;
1689 Py_ssize_t self_len, result_len;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001690 Py_ssize_t count, i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001691 PyByteArrayObject *result;
1692
1693 self_len = PyByteArray_GET_SIZE(self);
1694
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001695 /* 1 at the end plus 1 after every character;
1696 count = min(maxcount, self_len + 1) */
1697 if (maxcount <= self_len)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001698 count = maxcount;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001699 else
1700 /* Can't overflow: self_len + 1 <= maxcount <= PY_SSIZE_T_MAX. */
1701 count = self_len + 1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001702
1703 /* Check for overflow */
1704 /* result_len = count * to_len + self_len; */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001705 assert(count > 0);
1706 if (to_len > (PY_SSIZE_T_MAX - self_len) / count) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001707 PyErr_SetString(PyExc_OverflowError,
1708 "replace string is too long");
1709 return NULL;
1710 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001711 result_len = count * to_len + self_len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001712
1713 if (! (result = (PyByteArrayObject *)
1714 PyByteArray_FromStringAndSize(NULL, result_len)) )
1715 return NULL;
1716
1717 self_s = PyByteArray_AS_STRING(self);
1718 result_s = PyByteArray_AS_STRING(result);
1719
1720 /* TODO: special case single character, which doesn't need memcpy */
1721
1722 /* Lay the first one down (guaranteed this will occur) */
1723 Py_MEMCPY(result_s, to_s, to_len);
1724 result_s += to_len;
1725 count -= 1;
1726
1727 for (i=0; i<count; i++) {
1728 *result_s++ = *self_s++;
1729 Py_MEMCPY(result_s, to_s, to_len);
1730 result_s += to_len;
1731 }
1732
1733 /* Copy the rest of the original string */
1734 Py_MEMCPY(result_s, self_s, self_len-i);
1735
1736 return result;
1737}
1738
1739/* Special case for deleting a single character */
1740/* len(self)>=1, len(from)==1, to="", maxcount>=1 */
1741Py_LOCAL(PyByteArrayObject *)
1742replace_delete_single_character(PyByteArrayObject *self,
1743 char from_c, Py_ssize_t maxcount)
1744{
1745 char *self_s, *result_s;
1746 char *start, *next, *end;
1747 Py_ssize_t self_len, result_len;
1748 Py_ssize_t count;
1749 PyByteArrayObject *result;
1750
1751 self_len = PyByteArray_GET_SIZE(self);
1752 self_s = PyByteArray_AS_STRING(self);
1753
1754 count = countchar(self_s, self_len, from_c, maxcount);
1755 if (count == 0) {
1756 return return_self(self);
1757 }
1758
1759 result_len = self_len - count; /* from_len == 1 */
1760 assert(result_len>=0);
1761
1762 if ( (result = (PyByteArrayObject *)
1763 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1764 return NULL;
1765 result_s = PyByteArray_AS_STRING(result);
1766
1767 start = self_s;
1768 end = self_s + self_len;
1769 while (count-- > 0) {
1770 next = findchar(start, end-start, from_c);
1771 if (next == NULL)
1772 break;
1773 Py_MEMCPY(result_s, start, next-start);
1774 result_s += (next-start);
1775 start = next+1;
1776 }
1777 Py_MEMCPY(result_s, start, end-start);
1778
1779 return result;
1780}
1781
1782/* len(self)>=1, len(from)>=2, to="", maxcount>=1 */
1783
1784Py_LOCAL(PyByteArrayObject *)
1785replace_delete_substring(PyByteArrayObject *self,
1786 const char *from_s, Py_ssize_t from_len,
1787 Py_ssize_t maxcount)
1788{
1789 char *self_s, *result_s;
1790 char *start, *next, *end;
1791 Py_ssize_t self_len, result_len;
1792 Py_ssize_t count, offset;
1793 PyByteArrayObject *result;
1794
1795 self_len = PyByteArray_GET_SIZE(self);
1796 self_s = PyByteArray_AS_STRING(self);
1797
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001798 count = stringlib_count(self_s, self_len,
1799 from_s, from_len,
1800 maxcount);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001801
1802 if (count == 0) {
1803 /* no matches */
1804 return return_self(self);
1805 }
1806
1807 result_len = self_len - (count * from_len);
1808 assert (result_len>=0);
1809
1810 if ( (result = (PyByteArrayObject *)
1811 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL )
1812 return NULL;
1813
1814 result_s = PyByteArray_AS_STRING(result);
1815
1816 start = self_s;
1817 end = self_s + self_len;
1818 while (count-- > 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001819 offset = stringlib_find(start, end-start,
1820 from_s, from_len,
1821 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001822 if (offset == -1)
1823 break;
1824 next = start + offset;
1825
1826 Py_MEMCPY(result_s, start, next-start);
1827
1828 result_s += (next-start);
1829 start = next+from_len;
1830 }
1831 Py_MEMCPY(result_s, start, end-start);
1832 return result;
1833}
1834
1835/* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */
1836Py_LOCAL(PyByteArrayObject *)
1837replace_single_character_in_place(PyByteArrayObject *self,
1838 char from_c, char to_c,
1839 Py_ssize_t maxcount)
1840{
Antoine Pitroud1188562010-06-09 16:38:55 +00001841 char *self_s, *result_s, *start, *end, *next;
1842 Py_ssize_t self_len;
1843 PyByteArrayObject *result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001844
Antoine Pitroud1188562010-06-09 16:38:55 +00001845 /* The result string will be the same size */
1846 self_s = PyByteArray_AS_STRING(self);
1847 self_len = PyByteArray_GET_SIZE(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001848
Antoine Pitroud1188562010-06-09 16:38:55 +00001849 next = findchar(self_s, self_len, from_c);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001850
Antoine Pitroud1188562010-06-09 16:38:55 +00001851 if (next == NULL) {
1852 /* No matches; return the original bytes */
1853 return return_self(self);
1854 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001855
Antoine Pitroud1188562010-06-09 16:38:55 +00001856 /* Need to make a new bytes */
1857 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len);
1858 if (result == NULL)
1859 return NULL;
1860 result_s = PyByteArray_AS_STRING(result);
1861 Py_MEMCPY(result_s, self_s, self_len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001862
Antoine Pitroud1188562010-06-09 16:38:55 +00001863 /* change everything in-place, starting with this one */
1864 start = result_s + (next-self_s);
1865 *start = to_c;
1866 start++;
1867 end = result_s + self_len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001868
Antoine Pitroud1188562010-06-09 16:38:55 +00001869 while (--maxcount > 0) {
1870 next = findchar(start, end-start, from_c);
1871 if (next == NULL)
1872 break;
1873 *next = to_c;
1874 start = next+1;
1875 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001876
Antoine Pitroud1188562010-06-09 16:38:55 +00001877 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001878}
1879
1880/* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */
1881Py_LOCAL(PyByteArrayObject *)
1882replace_substring_in_place(PyByteArrayObject *self,
1883 const char *from_s, Py_ssize_t from_len,
1884 const char *to_s, Py_ssize_t to_len,
1885 Py_ssize_t maxcount)
1886{
1887 char *result_s, *start, *end;
1888 char *self_s;
1889 Py_ssize_t self_len, offset;
1890 PyByteArrayObject *result;
1891
1892 /* The result bytes will be the same size */
1893
1894 self_s = PyByteArray_AS_STRING(self);
1895 self_len = PyByteArray_GET_SIZE(self);
1896
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001897 offset = stringlib_find(self_s, self_len,
1898 from_s, from_len,
1899 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001900 if (offset == -1) {
1901 /* No matches; return the original bytes */
1902 return return_self(self);
1903 }
1904
1905 /* Need to make a new bytes */
1906 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len);
1907 if (result == NULL)
1908 return NULL;
1909 result_s = PyByteArray_AS_STRING(result);
1910 Py_MEMCPY(result_s, self_s, self_len);
1911
1912 /* change everything in-place, starting with this one */
1913 start = result_s + offset;
1914 Py_MEMCPY(start, to_s, from_len);
1915 start += from_len;
1916 end = result_s + self_len;
1917
1918 while ( --maxcount > 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001919 offset = stringlib_find(start, end-start,
1920 from_s, from_len,
1921 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001922 if (offset==-1)
1923 break;
1924 Py_MEMCPY(start+offset, to_s, from_len);
1925 start += offset+from_len;
1926 }
1927
1928 return result;
1929}
1930
1931/* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */
1932Py_LOCAL(PyByteArrayObject *)
1933replace_single_character(PyByteArrayObject *self,
1934 char from_c,
1935 const char *to_s, Py_ssize_t to_len,
1936 Py_ssize_t maxcount)
1937{
1938 char *self_s, *result_s;
1939 char *start, *next, *end;
1940 Py_ssize_t self_len, result_len;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001941 Py_ssize_t count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001942 PyByteArrayObject *result;
1943
1944 self_s = PyByteArray_AS_STRING(self);
1945 self_len = PyByteArray_GET_SIZE(self);
1946
1947 count = countchar(self_s, self_len, from_c, maxcount);
1948 if (count == 0) {
1949 /* no matches, return unchanged */
1950 return return_self(self);
1951 }
1952
1953 /* use the difference between current and new, hence the "-1" */
1954 /* result_len = self_len + count * (to_len-1) */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001955 assert(count > 0);
1956 if (to_len - 1 > (PY_SSIZE_T_MAX - self_len) / count) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001957 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
1958 return NULL;
1959 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001960 result_len = self_len + count * (to_len - 1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001961
1962 if ( (result = (PyByteArrayObject *)
1963 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1964 return NULL;
1965 result_s = PyByteArray_AS_STRING(result);
1966
1967 start = self_s;
1968 end = self_s + self_len;
1969 while (count-- > 0) {
1970 next = findchar(start, end-start, from_c);
1971 if (next == NULL)
1972 break;
1973
1974 if (next == start) {
1975 /* replace with the 'to' */
1976 Py_MEMCPY(result_s, to_s, to_len);
1977 result_s += to_len;
1978 start += 1;
1979 } else {
1980 /* copy the unchanged old then the 'to' */
1981 Py_MEMCPY(result_s, start, next-start);
1982 result_s += (next-start);
1983 Py_MEMCPY(result_s, to_s, to_len);
1984 result_s += to_len;
1985 start = next+1;
1986 }
1987 }
1988 /* Copy the remainder of the remaining bytes */
1989 Py_MEMCPY(result_s, start, end-start);
1990
1991 return result;
1992}
1993
1994/* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */
1995Py_LOCAL(PyByteArrayObject *)
1996replace_substring(PyByteArrayObject *self,
1997 const char *from_s, Py_ssize_t from_len,
1998 const char *to_s, Py_ssize_t to_len,
1999 Py_ssize_t maxcount)
2000{
2001 char *self_s, *result_s;
2002 char *start, *next, *end;
2003 Py_ssize_t self_len, result_len;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00002004 Py_ssize_t count, offset;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002005 PyByteArrayObject *result;
2006
2007 self_s = PyByteArray_AS_STRING(self);
2008 self_len = PyByteArray_GET_SIZE(self);
2009
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002010 count = stringlib_count(self_s, self_len,
2011 from_s, from_len,
2012 maxcount);
2013
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002014 if (count == 0) {
2015 /* no matches, return unchanged */
2016 return return_self(self);
2017 }
2018
2019 /* Check for overflow */
2020 /* result_len = self_len + count * (to_len-from_len) */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00002021 assert(count > 0);
2022 if (to_len - from_len > (PY_SSIZE_T_MAX - self_len) / count) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002023 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
2024 return NULL;
2025 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00002026 result_len = self_len + count * (to_len - from_len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002027
2028 if ( (result = (PyByteArrayObject *)
2029 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
2030 return NULL;
2031 result_s = PyByteArray_AS_STRING(result);
2032
2033 start = self_s;
2034 end = self_s + self_len;
2035 while (count-- > 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002036 offset = stringlib_find(start, end-start,
2037 from_s, from_len,
2038 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002039 if (offset == -1)
2040 break;
2041 next = start+offset;
2042 if (next == start) {
2043 /* replace with the 'to' */
2044 Py_MEMCPY(result_s, to_s, to_len);
2045 result_s += to_len;
2046 start += from_len;
2047 } else {
2048 /* copy the unchanged old then the 'to' */
2049 Py_MEMCPY(result_s, start, next-start);
2050 result_s += (next-start);
2051 Py_MEMCPY(result_s, to_s, to_len);
2052 result_s += to_len;
2053 start = next+from_len;
2054 }
2055 }
2056 /* Copy the remainder of the remaining bytes */
2057 Py_MEMCPY(result_s, start, end-start);
2058
2059 return result;
2060}
2061
2062
2063Py_LOCAL(PyByteArrayObject *)
2064replace(PyByteArrayObject *self,
2065 const char *from_s, Py_ssize_t from_len,
2066 const char *to_s, Py_ssize_t to_len,
2067 Py_ssize_t maxcount)
2068{
2069 if (maxcount < 0) {
2070 maxcount = PY_SSIZE_T_MAX;
2071 } else if (maxcount == 0 || PyByteArray_GET_SIZE(self) == 0) {
2072 /* nothing to do; return the original bytes */
2073 return return_self(self);
2074 }
2075
2076 if (maxcount == 0 ||
2077 (from_len == 0 && to_len == 0)) {
2078 /* nothing to do; return the original bytes */
2079 return return_self(self);
2080 }
2081
2082 /* Handle zero-length special cases */
2083
2084 if (from_len == 0) {
2085 /* insert the 'to' bytes everywhere. */
2086 /* >>> "Python".replace("", ".") */
2087 /* '.P.y.t.h.o.n.' */
2088 return replace_interleave(self, to_s, to_len, maxcount);
2089 }
2090
2091 /* Except for "".replace("", "A") == "A" there is no way beyond this */
2092 /* point for an empty self bytes to generate a non-empty bytes */
2093 /* Special case so the remaining code always gets a non-empty bytes */
2094 if (PyByteArray_GET_SIZE(self) == 0) {
2095 return return_self(self);
2096 }
2097
2098 if (to_len == 0) {
Georg Brandl17cb8a82008-05-30 08:20:09 +00002099 /* delete all occurrences of 'from' bytes */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002100 if (from_len == 1) {
2101 return replace_delete_single_character(
2102 self, from_s[0], maxcount);
2103 } else {
2104 return replace_delete_substring(self, from_s, from_len, maxcount);
2105 }
2106 }
2107
2108 /* Handle special case where both bytes have the same length */
2109
2110 if (from_len == to_len) {
2111 if (from_len == 1) {
2112 return replace_single_character_in_place(
2113 self,
2114 from_s[0],
2115 to_s[0],
2116 maxcount);
2117 } else {
2118 return replace_substring_in_place(
2119 self, from_s, from_len, to_s, to_len, maxcount);
2120 }
2121 }
2122
2123 /* Otherwise use the more generic algorithms */
2124 if (from_len == 1) {
2125 return replace_single_character(self, from_s[0],
2126 to_s, to_len, maxcount);
2127 } else {
2128 /* len('from')>=2, len('to')>=1 */
2129 return replace_substring(self, from_s, from_len, to_s, to_len, maxcount);
2130 }
2131}
2132
2133
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002134/*[clinic input]
2135bytearray.replace
2136
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002137 old: Py_buffer
2138 new: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002139 count: Py_ssize_t = -1
2140 Maximum number of occurrences to replace.
2141 -1 (the default value) means replace all occurrences.
2142 /
2143
2144Return a copy with all occurrences of substring old replaced by new.
2145
2146If the optional argument count is given, only the first count occurrences are
2147replaced.
2148[clinic start generated code]*/
2149
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002150static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04002151bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
2152 Py_buffer *new, Py_ssize_t count)
2153/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002154{
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002155 return (PyObject *)replace((PyByteArrayObject *) self,
2156 old->buf, old->len,
2157 new->buf, new->len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002158}
2159
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002160/*[clinic input]
2161bytearray.split
2162
2163 sep: object = None
2164 The delimiter according which to split the bytearray.
2165 None (the default value) means split on ASCII whitespace characters
2166 (space, tab, return, newline, formfeed, vertical tab).
2167 maxsplit: Py_ssize_t = -1
2168 Maximum number of splits to do.
2169 -1 (the default value) means no limit.
2170
2171Return a list of the sections in the bytearray, using sep as the delimiter.
2172[clinic start generated code]*/
2173
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002174static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04002175bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
2176 Py_ssize_t maxsplit)
2177/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002178{
2179 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002180 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002181 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002182 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002183
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002184 if (maxsplit < 0)
2185 maxsplit = PY_SSIZE_T_MAX;
2186
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002187 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002188 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002189
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002190 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002191 return NULL;
2192 sub = vsub.buf;
2193 n = vsub.len;
2194
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002195 list = stringlib_split(
2196 (PyObject*) self, s, len, sub, n, maxsplit
2197 );
Martin v. Löwis423be952008-08-13 15:53:07 +00002198 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002199 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002200}
2201
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002202/*[clinic input]
2203bytearray.partition
2204
2205 self: self(type="PyByteArrayObject *")
2206 sep: object
2207 /
2208
2209Partition the bytearray into three parts using the given separator.
2210
2211This will search for the separator sep in the bytearray. If the separator is
2212found, returns a 3-tuple containing the part before the separator, the
2213separator itself, and the part after it.
2214
2215If the separator is not found, returns a 3-tuple containing the original
2216bytearray object and two empty bytearray objects.
2217[clinic start generated code]*/
2218
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002219static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002220bytearray_partition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002221/*[clinic end generated code: output=45d2525ddd35f957 input=7d7fe37b1696d506]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002222{
2223 PyObject *bytesep, *result;
2224
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002225 bytesep = PyByteArray_FromObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002226 if (! bytesep)
2227 return NULL;
2228
2229 result = stringlib_partition(
2230 (PyObject*) self,
2231 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2232 bytesep,
2233 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2234 );
2235
2236 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002237 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002238}
2239
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002240/*[clinic input]
2241bytearray.rpartition
2242
2243 self: self(type="PyByteArrayObject *")
2244 sep: object
2245 /
2246
2247Partition the bytes into three parts using the given separator.
2248
2249This will search for the separator sep in the bytearray, starting and the end.
2250If the separator is found, returns a 3-tuple containing the part before the
2251separator, the separator itself, and the part after it.
2252
2253If the separator is not found, returns a 3-tuple containing two empty bytearray
2254objects and the original bytearray object.
2255[clinic start generated code]*/
2256
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002257static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002258bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002259/*[clinic end generated code: output=440de3c9426115e8 input=9b8cd540c1b75853]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002260{
2261 PyObject *bytesep, *result;
2262
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002263 bytesep = PyByteArray_FromObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002264 if (! bytesep)
2265 return NULL;
2266
2267 result = stringlib_rpartition(
2268 (PyObject*) self,
2269 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2270 bytesep,
2271 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2272 );
2273
2274 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002275 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002276}
2277
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002278/*[clinic input]
2279bytearray.rsplit = bytearray.split
2280
2281Return a list of the sections in the bytearray, using sep as the delimiter.
2282
2283Splitting is done starting at the end of the bytearray and working to the front.
2284[clinic start generated code]*/
2285
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002286static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04002287bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
2288 Py_ssize_t maxsplit)
2289/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002290{
2291 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002292 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002293 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002294 Py_buffer vsub;
2295
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002296 if (maxsplit < 0)
2297 maxsplit = PY_SSIZE_T_MAX;
2298
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002299 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002300 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002301
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002302 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002303 return NULL;
2304 sub = vsub.buf;
2305 n = vsub.len;
2306
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002307 list = stringlib_rsplit(
2308 (PyObject*) self, s, len, sub, n, maxsplit
2309 );
Martin v. Löwis423be952008-08-13 15:53:07 +00002310 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002311 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002312}
2313
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002314/*[clinic input]
2315bytearray.reverse
2316
2317 self: self(type="PyByteArrayObject *")
2318
2319Reverse the order of the values in B in place.
2320[clinic start generated code]*/
2321
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002322static PyObject *
2323bytearray_reverse_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002324/*[clinic end generated code: output=9f7616f29ab309d3 input=7933a499b8597bd1]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002325{
2326 char swap, *head, *tail;
2327 Py_ssize_t i, j, n = Py_SIZE(self);
2328
2329 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002330 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002331 tail = head + n - 1;
2332 for (i = 0; i < j; i++) {
2333 swap = *head;
2334 *head++ = *tail;
2335 *tail-- = swap;
2336 }
2337
2338 Py_RETURN_NONE;
2339}
2340
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002341
2342/*[python input]
2343class bytesvalue_converter(CConverter):
2344 type = 'int'
2345 converter = '_getbytevalue'
2346[python start generated code]*/
2347/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
2348
2349
2350/*[clinic input]
2351bytearray.insert
2352
2353 self: self(type="PyByteArrayObject *")
2354 index: Py_ssize_t
2355 The index where the value is to be inserted.
2356 item: bytesvalue
2357 The item to be inserted.
2358 /
2359
2360Insert a single item into the bytearray before the given index.
2361[clinic start generated code]*/
2362
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002363static PyObject *
2364bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002365/*[clinic end generated code: output=76c775a70e7b07b7 input=833766836ba30e1e]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002366{
2367 Py_ssize_t n = Py_SIZE(self);
2368 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002369
2370 if (n == PY_SSIZE_T_MAX) {
2371 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00002372 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002373 return NULL;
2374 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002375 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2376 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002377 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002378
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002379 if (index < 0) {
2380 index += n;
2381 if (index < 0)
2382 index = 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002383 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002384 if (index > n)
2385 index = n;
2386 memmove(buf + index + 1, buf + index, n - index);
2387 buf[index] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002388
2389 Py_RETURN_NONE;
2390}
2391
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002392/*[clinic input]
2393bytearray.append
2394
2395 self: self(type="PyByteArrayObject *")
2396 item: bytesvalue
2397 The item to be appended.
2398 /
2399
2400Append a single item to the end of the bytearray.
2401[clinic start generated code]*/
2402
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002403static PyObject *
2404bytearray_append_impl(PyByteArrayObject *self, int item)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002405/*[clinic end generated code: output=a154e19ed1886cb6 input=ae56ea87380407cc]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002406{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002407 Py_ssize_t n = Py_SIZE(self);
2408
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002409 if (n == PY_SSIZE_T_MAX) {
2410 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00002411 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002412 return NULL;
2413 }
2414 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2415 return NULL;
2416
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002417 PyByteArray_AS_STRING(self)[n] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002418
2419 Py_RETURN_NONE;
2420}
2421
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002422/*[clinic input]
2423bytearray.extend
2424
2425 self: self(type="PyByteArrayObject *")
2426 iterable_of_ints: object
2427 The iterable of items to append.
2428 /
2429
2430Append all the items from the iterator or sequence to the end of the bytearray.
2431[clinic start generated code]*/
2432
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002433static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002434bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002435/*[clinic end generated code: output=98155dbe249170b1 input=ce83a5d75b70d850]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002436{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002437 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002438 Py_ssize_t buf_size = 0, len = 0;
2439 int value;
2440 char *buf;
2441
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002442 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002443 if (PyObject_CheckBuffer(iterable_of_ints)) {
2444 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002445 return NULL;
2446
2447 Py_RETURN_NONE;
2448 }
2449
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002450 it = PyObject_GetIter(iterable_of_ints);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002451 if (it == NULL)
2452 return NULL;
2453
Ezio Melotti42da6632011-03-15 05:18:48 +02002454 /* Try to determine the length of the argument. 32 is arbitrary. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002455 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002456 if (buf_size == -1) {
2457 Py_DECREF(it);
2458 return NULL;
2459 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002460
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002461 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002462 if (bytearray_obj == NULL) {
2463 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002464 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002465 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002466 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002467
2468 while ((item = PyIter_Next(it)) != NULL) {
2469 if (! _getbytevalue(item, &value)) {
2470 Py_DECREF(item);
2471 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002472 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002473 return NULL;
2474 }
2475 buf[len++] = value;
2476 Py_DECREF(item);
2477
2478 if (len >= buf_size) {
Martin Panter371731e2016-07-18 07:53:13 +00002479 Py_ssize_t addition;
2480 if (len == PY_SSIZE_T_MAX) {
2481 Py_DECREF(it);
2482 Py_DECREF(bytearray_obj);
2483 return PyErr_NoMemory();
2484 }
2485 addition = len >> 1;
2486 if (addition > PY_SSIZE_T_MAX - len - 1)
2487 buf_size = PY_SSIZE_T_MAX;
2488 else
2489 buf_size = len + addition + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002490 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002491 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002492 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002493 return NULL;
2494 }
2495 /* Recompute the `buf' pointer, since the resizing operation may
2496 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002497 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002498 }
2499 }
2500 Py_DECREF(it);
2501
2502 /* Resize down to exact size. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002503 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
2504 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002505 return NULL;
2506 }
2507
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002508 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
2509 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002510 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002511 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002512 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002513
2514 Py_RETURN_NONE;
2515}
2516
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002517/*[clinic input]
2518bytearray.pop
2519
2520 self: self(type="PyByteArrayObject *")
2521 index: Py_ssize_t = -1
2522 The index from where to remove the item.
2523 -1 (the default value) means remove the last item.
2524 /
2525
2526Remove and return a single item from B.
2527
2528If no index argument is given, will pop the last item.
2529[clinic start generated code]*/
2530
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002531static PyObject *
2532bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002533/*[clinic end generated code: output=e0ccd401f8021da8 input=0797e6c0ca9d5a85]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002534{
2535 int value;
2536 Py_ssize_t n = Py_SIZE(self);
2537 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002538
2539 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00002540 PyErr_SetString(PyExc_IndexError,
2541 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002542 return NULL;
2543 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002544 if (index < 0)
2545 index += Py_SIZE(self);
2546 if (index < 0 || index >= Py_SIZE(self)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002547 PyErr_SetString(PyExc_IndexError, "pop index out of range");
2548 return NULL;
2549 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00002550 if (!_canresize(self))
2551 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002552
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002553 buf = PyByteArray_AS_STRING(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002554 value = buf[index];
2555 memmove(buf + index, buf + index + 1, n - index);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002556 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2557 return NULL;
2558
Mark Dickinson54a3db92009-09-06 10:19:23 +00002559 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002560}
2561
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002562/*[clinic input]
2563bytearray.remove
2564
2565 self: self(type="PyByteArrayObject *")
2566 value: bytesvalue
2567 The value to remove.
2568 /
2569
2570Remove the first occurrence of a value in the bytearray.
2571[clinic start generated code]*/
2572
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002573static PyObject *
2574bytearray_remove_impl(PyByteArrayObject *self, int value)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002575/*[clinic end generated code: output=d659e37866709c13 input=47560b11fd856c24]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002576{
Serhiy Storchaka7bf36da2016-05-16 22:15:38 +03002577 Py_ssize_t n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002578 char *buf = PyByteArray_AS_STRING(self);
Serhiy Storchaka7bf36da2016-05-16 22:15:38 +03002579 char *where = memchr(buf, value, n);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002580
Serhiy Storchaka7bf36da2016-05-16 22:15:38 +03002581 if (!where) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00002582 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002583 return NULL;
2584 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00002585 if (!_canresize(self))
2586 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002587
Serhiy Storchaka7bf36da2016-05-16 22:15:38 +03002588 memmove(where, where + 1, buf + n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002589 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2590 return NULL;
2591
2592 Py_RETURN_NONE;
2593}
2594
2595/* XXX These two helpers could be optimized if argsize == 1 */
2596
2597static Py_ssize_t
Antoine Pitrou5b720752013-10-05 21:24:10 +02002598lstrip_helper(char *myptr, Py_ssize_t mysize,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002599 void *argptr, Py_ssize_t argsize)
2600{
2601 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02002602 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002603 i++;
2604 return i;
2605}
2606
2607static Py_ssize_t
Antoine Pitrou5b720752013-10-05 21:24:10 +02002608rstrip_helper(char *myptr, Py_ssize_t mysize,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002609 void *argptr, Py_ssize_t argsize)
2610{
2611 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02002612 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002613 i--;
2614 return i + 1;
2615}
2616
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002617/*[clinic input]
2618bytearray.strip
2619
2620 bytes: object = None
2621 /
2622
2623Strip leading and trailing bytes contained in the argument.
2624
2625If the argument is omitted or None, strip leading and trailing ASCII whitespace.
2626[clinic start generated code]*/
2627
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002628static PyObject *
2629bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002630/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002631{
2632 Py_ssize_t left, right, mysize, byteslen;
2633 char *myptr, *bytesptr;
2634 Py_buffer vbytes;
2635
2636 if (bytes == Py_None) {
2637 bytesptr = "\t\n\r\f\v ";
2638 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002639 }
2640 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002641 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002642 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002643 bytesptr = (char *) vbytes.buf;
2644 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002645 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002646 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002647 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002648 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002649 if (left == mysize)
2650 right = left;
2651 else
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002652 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
2653 if (bytes != Py_None)
2654 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002655 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002656}
2657
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002658/*[clinic input]
2659bytearray.lstrip
2660
2661 bytes: object = None
2662 /
2663
2664Strip leading bytes contained in the argument.
2665
2666If the argument is omitted or None, strip leading ASCII whitespace.
2667[clinic start generated code]*/
2668
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002669static PyObject *
2670bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002671/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002672{
2673 Py_ssize_t left, right, mysize, byteslen;
2674 char *myptr, *bytesptr;
2675 Py_buffer vbytes;
2676
2677 if (bytes == Py_None) {
2678 bytesptr = "\t\n\r\f\v ";
2679 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002680 }
2681 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002682 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002683 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002684 bytesptr = (char *) vbytes.buf;
2685 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002686 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002687 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002688 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002689 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002690 right = mysize;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002691 if (bytes != Py_None)
2692 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002693 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002694}
2695
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002696/*[clinic input]
2697bytearray.rstrip
2698
2699 bytes: object = None
2700 /
2701
2702Strip trailing bytes contained in the argument.
2703
2704If the argument is omitted or None, strip trailing ASCII whitespace.
2705[clinic start generated code]*/
2706
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002707static PyObject *
2708bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002709/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002710{
2711 Py_ssize_t right, mysize, byteslen;
2712 char *myptr, *bytesptr;
2713 Py_buffer vbytes;
2714
2715 if (bytes == Py_None) {
2716 bytesptr = "\t\n\r\f\v ";
2717 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002718 }
2719 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002720 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002721 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002722 bytesptr = (char *) vbytes.buf;
2723 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002724 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002725 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002726 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002727 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
2728 if (bytes != Py_None)
2729 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002730 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002731}
2732
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002733/*[clinic input]
2734bytearray.decode
2735
2736 encoding: str(c_default="NULL") = 'utf-8'
2737 The encoding with which to decode the bytearray.
2738 errors: str(c_default="NULL") = 'strict'
2739 The error handling scheme to use for the handling of decoding errors.
2740 The default is 'strict' meaning that decoding errors raise a
2741 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
2742 as well as any other name registered with codecs.register_error that
2743 can handle UnicodeDecodeErrors.
2744
2745Decode the bytearray using the codec registered for encoding.
2746[clinic start generated code]*/
2747
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002748static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04002749bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
2750 const char *errors)
2751/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002752{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002753 if (encoding == NULL)
2754 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis0efea322014-07-27 17:29:17 +02002755 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002756}
2757
2758PyDoc_STRVAR(alloc_doc,
2759"B.__alloc__() -> int\n\
2760\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00002761Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002762
2763static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002764bytearray_alloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002765{
2766 return PyLong_FromSsize_t(self->ob_alloc);
2767}
2768
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002769/*[clinic input]
2770bytearray.join
2771
2772 iterable_of_bytes: object
2773 /
2774
2775Concatenate any number of bytes/bytearray objects.
2776
2777The bytearray whose method is called is inserted in between each pair.
2778
2779The result is returned as a new bytearray object.
2780[clinic start generated code]*/
2781
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002782static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002783bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002784/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002785{
Martin v. Löwis0efea322014-07-27 17:29:17 +02002786 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002787}
2788
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002789/*[clinic input]
2790bytearray.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002791
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +03002792 keepends: int(c_default="0") = False
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002793
2794Return a list of the lines in the bytearray, breaking at line boundaries.
2795
2796Line breaks are not included in the resulting list unless keepends is given and
2797true.
2798[clinic start generated code]*/
2799
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002800static PyObject *
2801bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +03002802/*[clinic end generated code: output=4223c94b895f6ad9 input=8ccade941e5ea0bd]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002803{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002804 return stringlib_splitlines(
2805 (PyObject*) self, PyByteArray_AS_STRING(self),
2806 PyByteArray_GET_SIZE(self), keepends
2807 );
2808}
2809
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002810static int
Victor Stinner6430fd52011-09-29 04:02:13 +02002811hex_digit_to_int(Py_UCS4 c)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002812{
2813 if (c >= 128)
2814 return -1;
Eric Smith6dc46f52009-04-27 20:39:49 +00002815 if (Py_ISDIGIT(c))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002816 return c - '0';
2817 else {
Eric Smith6dc46f52009-04-27 20:39:49 +00002818 if (Py_ISUPPER(c))
2819 c = Py_TOLOWER(c);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002820 if (c >= 'a' && c <= 'f')
2821 return c - 'a' + 10;
2822 }
2823 return -1;
2824}
2825
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002826/*[clinic input]
2827@classmethod
2828bytearray.fromhex
2829
2830 cls: self(type="PyObject*")
2831 string: unicode
2832 /
2833
2834Create a bytearray object from a string of hexadecimal numbers.
2835
2836Spaces between two numbers are accepted.
2837Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
2838[clinic start generated code]*/
2839
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002840static PyObject *
2841bytearray_fromhex_impl(PyObject*cls, PyObject *string)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002842/*[clinic end generated code: output=df3da60129b3700c input=907bbd2d34d9367a]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002843{
2844 PyObject *newbytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002845 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002846 Py_ssize_t hexlen, byteslen, i, j;
2847 int top, bot;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002848 void *data;
2849 unsigned int kind;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002850
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002851 assert(PyUnicode_Check(string));
2852 if (PyUnicode_READY(string))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002853 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002854 kind = PyUnicode_KIND(string);
2855 data = PyUnicode_DATA(string);
2856 hexlen = PyUnicode_GET_LENGTH(string);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002857
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002858 byteslen = hexlen/2; /* This overestimates if there are spaces */
2859 newbytes = PyByteArray_FromStringAndSize(NULL, byteslen);
2860 if (!newbytes)
2861 return NULL;
2862 buf = PyByteArray_AS_STRING(newbytes);
2863 for (i = j = 0; i < hexlen; i += 2) {
2864 /* skip over spaces in the input */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002865 while (PyUnicode_READ(kind, data, i) == ' ')
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002866 i++;
2867 if (i >= hexlen)
2868 break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002869 top = hex_digit_to_int(PyUnicode_READ(kind, data, i));
2870 bot = hex_digit_to_int(PyUnicode_READ(kind, data, i+1));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002871 if (top == -1 || bot == -1) {
2872 PyErr_Format(PyExc_ValueError,
2873 "non-hexadecimal number found in "
2874 "fromhex() arg at position %zd", i);
2875 goto error;
2876 }
2877 buf[j++] = (top << 4) + bot;
2878 }
2879 if (PyByteArray_Resize(newbytes, j) < 0)
2880 goto error;
2881 return newbytes;
2882
2883 error:
2884 Py_DECREF(newbytes);
2885 return NULL;
2886}
2887
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002888PyDoc_STRVAR(hex__doc__,
2889"B.hex() -> string\n\
2890\n\
2891Create a string of hexadecimal numbers from a bytearray object.\n\
2892Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'.");
2893
2894static PyObject *
2895bytearray_hex(PyBytesObject *self)
2896{
2897 char* argbuf = PyByteArray_AS_STRING(self);
2898 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
2899 return _Py_strhex(argbuf, arglen);
2900}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002901
2902static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002903_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002904{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002905 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002906 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002907 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002908
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002909 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002910 if (dict == NULL) {
2911 PyErr_Clear();
2912 dict = Py_None;
2913 Py_INCREF(dict);
2914 }
2915
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002916 buf = PyByteArray_AS_STRING(self);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002917 if (proto < 3) {
2918 /* use str based reduction for backwards compatibility with Python 2.x */
2919 PyObject *latin1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002920 if (Py_SIZE(self))
2921 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002922 else
2923 latin1 = PyUnicode_FromString("");
2924 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2925 }
2926 else {
2927 /* use more efficient byte based reduction */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002928 if (Py_SIZE(self)) {
2929 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002930 }
2931 else {
2932 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2933 }
2934 }
2935}
2936
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002937/*[clinic input]
2938bytearray.__reduce__ as bytearray_reduce
2939
2940 self: self(type="PyByteArrayObject *")
2941
2942Return state information for pickling.
2943[clinic start generated code]*/
2944
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002945static PyObject *
2946bytearray_reduce_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002947/*[clinic end generated code: output=52bf304086464cab input=fbb07de4d102a03a]*/
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002948{
2949 return _common_reduce(self, 2);
2950}
2951
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002952/*[clinic input]
2953bytearray.__reduce_ex__ as bytearray_reduce_ex
2954
2955 self: self(type="PyByteArrayObject *")
2956 proto: int = 0
2957 /
2958
2959Return state information for pickling.
2960[clinic start generated code]*/
2961
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002962static PyObject *
2963bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002964/*[clinic end generated code: output=52eac33377197520 input=0e091a42ca6dbd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002965{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002966 return _common_reduce(self, proto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002967}
2968
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002969/*[clinic input]
2970bytearray.__sizeof__ as bytearray_sizeof
2971
2972 self: self(type="PyByteArrayObject *")
2973
2974Returns the size of the bytearray object in memory, in bytes.
2975[clinic start generated code]*/
2976
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002977static PyObject *
2978bytearray_sizeof_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002979/*[clinic end generated code: output=738abdd17951c427 input=6b23d305362b462b]*/
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002980{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002981 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002982
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002983 res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002984 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002985}
2986
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002987static PySequenceMethods bytearray_as_sequence = {
2988 (lenfunc)bytearray_length, /* sq_length */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002989 (binaryfunc)PyByteArray_Concat, /* sq_concat */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002990 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2991 (ssizeargfunc)bytearray_getitem, /* sq_item */
2992 0, /* sq_slice */
2993 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2994 0, /* sq_ass_slice */
2995 (objobjproc)bytearray_contains, /* sq_contains */
2996 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2997 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002998};
2999
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003000static PyMappingMethods bytearray_as_mapping = {
3001 (lenfunc)bytearray_length,
3002 (binaryfunc)bytearray_subscript,
3003 (objobjargproc)bytearray_ass_subscript,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003004};
3005
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003006static PyBufferProcs bytearray_as_buffer = {
3007 (getbufferproc)bytearray_getbuffer,
3008 (releasebufferproc)bytearray_releasebuffer,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003009};
3010
3011static PyMethodDef
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003012bytearray_methods[] = {
3013 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003014 BYTEARRAY_REDUCE_METHODDEF
3015 BYTEARRAY_REDUCE_EX_METHODDEF
3016 BYTEARRAY_SIZEOF_METHODDEF
3017 BYTEARRAY_APPEND_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003018 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
3019 _Py_capitalize__doc__},
3020 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003021 BYTEARRAY_CLEAR_METHODDEF
3022 BYTEARRAY_COPY_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003023 {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003024 BYTEARRAY_DECODE_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003025 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__},
Ezio Melotti745d54d2013-11-16 19:10:57 +02003026 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003027 expandtabs__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003028 BYTEARRAY_EXTEND_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003029 {"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003030 BYTEARRAY_FROMHEX_METHODDEF
Gregory P. Smith8cb65692015-04-25 23:22:26 +00003031 {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__},
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003032 {"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003033 BYTEARRAY_INSERT_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003034 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
3035 _Py_isalnum__doc__},
3036 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
3037 _Py_isalpha__doc__},
3038 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
3039 _Py_isdigit__doc__},
3040 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
3041 _Py_islower__doc__},
3042 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
3043 _Py_isspace__doc__},
3044 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
3045 _Py_istitle__doc__},
3046 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
3047 _Py_isupper__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003048 BYTEARRAY_JOIN_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003049 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__},
3050 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003051 BYTEARRAY_LSTRIP_METHODDEF
3052 BYTEARRAY_MAKETRANS_METHODDEF
3053 BYTEARRAY_PARTITION_METHODDEF
3054 BYTEARRAY_POP_METHODDEF
3055 BYTEARRAY_REMOVE_METHODDEF
3056 BYTEARRAY_REPLACE_METHODDEF
3057 BYTEARRAY_REVERSE_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003058 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__},
3059 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003060 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003061 BYTEARRAY_RPARTITION_METHODDEF
3062 BYTEARRAY_RSPLIT_METHODDEF
3063 BYTEARRAY_RSTRIP_METHODDEF
3064 BYTEARRAY_SPLIT_METHODDEF
3065 BYTEARRAY_SPLITLINES_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003066 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003067 startswith__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003068 BYTEARRAY_STRIP_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003069 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
3070 _Py_swapcase__doc__},
3071 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003072 BYTEARRAY_TRANSLATE_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003073 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
3074 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
3075 {NULL}
3076};
3077
Ethan Furmanb95b5612015-01-23 20:05:18 -08003078static PyObject *
3079bytearray_mod(PyObject *v, PyObject *w)
3080{
3081 if (!PyByteArray_Check(v))
3082 Py_RETURN_NOTIMPLEMENTED;
3083 return bytearray_format((PyByteArrayObject *)v, w);
3084}
3085
3086static PyNumberMethods bytearray_as_number = {
3087 0, /*nb_add*/
3088 0, /*nb_subtract*/
3089 0, /*nb_multiply*/
3090 bytearray_mod, /*nb_remainder*/
3091};
3092
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003093PyDoc_STRVAR(bytearray_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00003094"bytearray(iterable_of_ints) -> bytearray\n\
3095bytearray(string, encoding[, errors]) -> bytearray\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01003096bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
3097bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
3098bytearray() -> empty bytes array\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003099\n\
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03003100Construct a mutable bytearray object from:\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003101 - an iterable yielding integers in range(256)\n\
3102 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01003103 - a bytes or a buffer object\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003104 - any object implementing the buffer API.\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01003105 - an integer");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003106
3107
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003108static PyObject *bytearray_iter(PyObject *seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003109
3110PyTypeObject PyByteArray_Type = {
3111 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3112 "bytearray",
3113 sizeof(PyByteArrayObject),
3114 0,
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003115 (destructor)bytearray_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003116 0, /* tp_print */
3117 0, /* tp_getattr */
3118 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00003119 0, /* tp_reserved */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003120 (reprfunc)bytearray_repr, /* tp_repr */
Ethan Furmanb95b5612015-01-23 20:05:18 -08003121 &bytearray_as_number, /* tp_as_number */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003122 &bytearray_as_sequence, /* tp_as_sequence */
3123 &bytearray_as_mapping, /* tp_as_mapping */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003124 0, /* tp_hash */
3125 0, /* tp_call */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003126 bytearray_str, /* tp_str */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003127 PyObject_GenericGetAttr, /* tp_getattro */
3128 0, /* tp_setattro */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003129 &bytearray_as_buffer, /* tp_as_buffer */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003130 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003131 bytearray_doc, /* tp_doc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003132 0, /* tp_traverse */
3133 0, /* tp_clear */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003134 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003135 0, /* tp_weaklistoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003136 bytearray_iter, /* tp_iter */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003137 0, /* tp_iternext */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003138 bytearray_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003139 0, /* tp_members */
3140 0, /* tp_getset */
3141 0, /* tp_base */
3142 0, /* tp_dict */
3143 0, /* tp_descr_get */
3144 0, /* tp_descr_set */
3145 0, /* tp_dictoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003146 (initproc)bytearray_init, /* tp_init */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003147 PyType_GenericAlloc, /* tp_alloc */
3148 PyType_GenericNew, /* tp_new */
3149 PyObject_Del, /* tp_free */
3150};
3151
3152/*********************** Bytes Iterator ****************************/
3153
3154typedef struct {
3155 PyObject_HEAD
3156 Py_ssize_t it_index;
3157 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
3158} bytesiterobject;
3159
3160static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003161bytearrayiter_dealloc(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003162{
3163 _PyObject_GC_UNTRACK(it);
3164 Py_XDECREF(it->it_seq);
3165 PyObject_GC_Del(it);
3166}
3167
3168static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003169bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003170{
3171 Py_VISIT(it->it_seq);
3172 return 0;
3173}
3174
3175static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003176bytearrayiter_next(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003177{
3178 PyByteArrayObject *seq;
3179 PyObject *item;
3180
3181 assert(it != NULL);
3182 seq = it->it_seq;
3183 if (seq == NULL)
3184 return NULL;
3185 assert(PyByteArray_Check(seq));
3186
3187 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
3188 item = PyLong_FromLong(
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02003189 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003190 if (item != NULL)
3191 ++it->it_index;
3192 return item;
3193 }
3194
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003195 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003196 Py_DECREF(seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003197 return NULL;
3198}
3199
3200static PyObject *
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003201bytearrayiter_length_hint(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003202{
3203 Py_ssize_t len = 0;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03003204 if (it->it_seq) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003205 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03003206 if (len < 0) {
3207 len = 0;
3208 }
3209 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003210 return PyLong_FromSsize_t(len);
3211}
3212
3213PyDoc_STRVAR(length_hint_doc,
3214 "Private method returning an estimate of len(list(it)).");
3215
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003216static PyObject *
3217bytearrayiter_reduce(bytesiterobject *it)
3218{
3219 if (it->it_seq != NULL) {
Antoine Pitroua7013882012-04-05 00:04:20 +02003220 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003221 it->it_seq, it->it_index);
3222 } else {
3223 PyObject *u = PyUnicode_FromUnicode(NULL, 0);
3224 if (u == NULL)
3225 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +02003226 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003227 }
3228}
3229
3230static PyObject *
3231bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
3232{
3233 Py_ssize_t index = PyLong_AsSsize_t(state);
3234 if (index == -1 && PyErr_Occurred())
3235 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003236 if (it->it_seq != NULL) {
3237 if (index < 0)
3238 index = 0;
3239 else if (index > PyByteArray_GET_SIZE(it->it_seq))
3240 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
3241 it->it_index = index;
3242 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003243 Py_RETURN_NONE;
3244}
3245
3246PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
3247
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003248static PyMethodDef bytearrayiter_methods[] = {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003249 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003250 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003251 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003252 bytearray_reduce__doc__},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003253 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
3254 setstate_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003255 {NULL, NULL} /* sentinel */
3256};
3257
3258PyTypeObject PyByteArrayIter_Type = {
3259 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3260 "bytearray_iterator", /* tp_name */
3261 sizeof(bytesiterobject), /* tp_basicsize */
3262 0, /* tp_itemsize */
3263 /* methods */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003264 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003265 0, /* tp_print */
3266 0, /* tp_getattr */
3267 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00003268 0, /* tp_reserved */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003269 0, /* tp_repr */
3270 0, /* tp_as_number */
3271 0, /* tp_as_sequence */
3272 0, /* tp_as_mapping */
3273 0, /* tp_hash */
3274 0, /* tp_call */
3275 0, /* tp_str */
3276 PyObject_GenericGetAttr, /* tp_getattro */
3277 0, /* tp_setattro */
3278 0, /* tp_as_buffer */
3279 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
3280 0, /* tp_doc */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003281 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003282 0, /* tp_clear */
3283 0, /* tp_richcompare */
3284 0, /* tp_weaklistoffset */
3285 PyObject_SelfIter, /* tp_iter */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003286 (iternextfunc)bytearrayiter_next, /* tp_iternext */
3287 bytearrayiter_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003288 0,
3289};
3290
3291static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003292bytearray_iter(PyObject *seq)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003293{
3294 bytesiterobject *it;
3295
3296 if (!PyByteArray_Check(seq)) {
3297 PyErr_BadInternalCall();
3298 return NULL;
3299 }
3300 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
3301 if (it == NULL)
3302 return NULL;
3303 it->it_index = 0;
3304 Py_INCREF(seq);
3305 it->it_seq = (PyByteArrayObject *)seq;
3306 _PyObject_GC_TRACK(it);
3307 return (PyObject *)it;
3308}