blob: c59ad2499adb189e2df76d81138e6d702d42af01 [file] [log] [blame]
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001/* PyByteArray (bytearray) implementation */
2
3#define PY_SSIZE_T_CLEAN
4#include "Python.h"
5#include "structmember.h"
6#include "bytes_methods.h"
Ethan Furmanb95b5612015-01-23 20:05:18 -08007#include "bytesobject.h"
Gregory P. Smith8cb65692015-04-25 23:22:26 +00008#include "pystrhex.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00009
Martin v. Löwis7252a6e2014-07-27 16:25:09 +020010/*[clinic input]
11class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
12[clinic start generated code]*/
13/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/
14
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000015char _PyByteArray_empty_string[] = "";
Christian Heimes2c9c7a52008-05-26 13:42:13 +000016
17void
18PyByteArray_Fini(void)
19{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000020}
21
22int
23PyByteArray_Init(void)
24{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000025 return 1;
26}
27
28/* end nullbytes support */
29
30/* Helpers */
31
32static int
33_getbytevalue(PyObject* arg, int *value)
34{
35 long face_value;
36
37 if (PyLong_Check(arg)) {
38 face_value = PyLong_AsLong(arg);
Georg Brandl9a54d7c2008-07-16 23:15:30 +000039 } else {
40 PyObject *index = PyNumber_Index(arg);
41 if (index == NULL) {
42 PyErr_Format(PyExc_TypeError, "an integer is required");
Mark Dickinson10de93a2010-07-09 19:25:48 +000043 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000044 return 0;
45 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +000046 face_value = PyLong_AsLong(index);
47 Py_DECREF(index);
48 }
49
50 if (face_value < 0 || face_value >= 256) {
51 /* this includes the OverflowError in case the long is too large */
52 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
Mark Dickinson10de93a2010-07-09 19:25:48 +000053 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000054 return 0;
55 }
56
57 *value = face_value;
58 return 1;
59}
60
61static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +000062bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000063{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000064 void *ptr;
65 if (view == NULL) {
Stefan Krah5178d912015-02-03 16:57:21 +010066 PyErr_SetString(PyExc_BufferError,
67 "bytearray_getbuffer: view==NULL argument is obsolete");
68 return -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000069 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000070 ptr = (void *) PyByteArray_AS_STRING(obj);
Stefan Krah5178d912015-02-03 16:57:21 +010071 /* cannot fail if view != NULL and readonly == 0 */
72 (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
73 obj->ob_exports++;
74 return 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000075}
76
77static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +000078bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000079{
80 obj->ob_exports--;
81}
82
Antoine Pitrou5504e892008-12-06 21:27:53 +000083static int
84_canresize(PyByteArrayObject *self)
85{
86 if (self->ob_exports > 0) {
87 PyErr_SetString(PyExc_BufferError,
88 "Existing exports of data: object cannot be re-sized");
89 return 0;
90 }
91 return 1;
92}
93
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030094#include "clinic/bytearrayobject.c.h"
95
Christian Heimes2c9c7a52008-05-26 13:42:13 +000096/* Direct API functions */
97
98PyObject *
99PyByteArray_FromObject(PyObject *input)
100{
101 return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type,
102 input, NULL);
103}
104
105PyObject *
106PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
107{
108 PyByteArrayObject *new;
109 Py_ssize_t alloc;
110
111 if (size < 0) {
112 PyErr_SetString(PyExc_SystemError,
113 "Negative size passed to PyByteArray_FromStringAndSize");
114 return NULL;
115 }
116
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000117 /* Prevent buffer overflow when setting alloc to size+1. */
118 if (size == PY_SSIZE_T_MAX) {
119 return PyErr_NoMemory();
120 }
121
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000122 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
123 if (new == NULL)
124 return NULL;
125
126 if (size == 0) {
127 new->ob_bytes = NULL;
128 alloc = 0;
129 }
130 else {
131 alloc = size + 1;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100132 new->ob_bytes = PyObject_Malloc(alloc);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000133 if (new->ob_bytes == NULL) {
134 Py_DECREF(new);
135 return PyErr_NoMemory();
136 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +0000137 if (bytes != NULL && size > 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000138 memcpy(new->ob_bytes, bytes, size);
139 new->ob_bytes[size] = '\0'; /* Trailing null byte */
140 }
141 Py_SIZE(new) = size;
142 new->ob_alloc = alloc;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200143 new->ob_start = new->ob_bytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000144 new->ob_exports = 0;
145
146 return (PyObject *)new;
147}
148
149Py_ssize_t
150PyByteArray_Size(PyObject *self)
151{
152 assert(self != NULL);
153 assert(PyByteArray_Check(self));
154
155 return PyByteArray_GET_SIZE(self);
156}
157
158char *
159PyByteArray_AsString(PyObject *self)
160{
161 assert(self != NULL);
162 assert(PyByteArray_Check(self));
163
164 return PyByteArray_AS_STRING(self);
165}
166
167int
Antoine Pitroucc231542014-11-02 18:40:09 +0100168PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000169{
170 void *sval;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200171 PyByteArrayObject *obj = ((PyByteArrayObject *)self);
Antoine Pitroucc231542014-11-02 18:40:09 +0100172 /* All computations are done unsigned to avoid integer overflows
173 (see issue #22335). */
174 size_t alloc = (size_t) obj->ob_alloc;
175 size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
176 size_t size = (size_t) requested_size;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000177
178 assert(self != NULL);
179 assert(PyByteArray_Check(self));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200180 assert(logical_offset <= alloc);
Antoine Pitroucc231542014-11-02 18:40:09 +0100181 assert(requested_size >= 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000182
Antoine Pitroucc231542014-11-02 18:40:09 +0100183 if (requested_size == Py_SIZE(self)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000184 return 0;
185 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200186 if (!_canresize(obj)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000187 return -1;
188 }
189
Antoine Pitrou25454112015-05-19 20:52:27 +0200190 if (size + logical_offset + 1 <= alloc) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200191 /* Current buffer is large enough to host the requested size,
192 decide on a strategy. */
193 if (size < alloc / 2) {
194 /* Major downsize; resize down to exact size */
195 alloc = size + 1;
196 }
197 else {
198 /* Minor downsize; quick exit */
199 Py_SIZE(self) = size;
200 PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
201 return 0;
202 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000203 }
204 else {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200205 /* Need growing, decide on a strategy */
206 if (size <= alloc * 1.125) {
207 /* Moderate upsize; overallocate similar to list_resize() */
208 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
209 }
210 else {
211 /* Major upsize; resize up to exact size */
212 alloc = size + 1;
213 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000214 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100215 if (alloc > PY_SSIZE_T_MAX) {
216 PyErr_NoMemory();
217 return -1;
218 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000219
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200220 if (logical_offset > 0) {
221 sval = PyObject_Malloc(alloc);
222 if (sval == NULL) {
223 PyErr_NoMemory();
224 return -1;
225 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100226 memcpy(sval, PyByteArray_AS_STRING(self),
227 Py_MIN(requested_size, Py_SIZE(self)));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200228 PyObject_Free(obj->ob_bytes);
229 }
230 else {
231 sval = PyObject_Realloc(obj->ob_bytes, alloc);
232 if (sval == NULL) {
233 PyErr_NoMemory();
234 return -1;
235 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000236 }
237
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200238 obj->ob_bytes = obj->ob_start = sval;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000239 Py_SIZE(self) = size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200240 obj->ob_alloc = alloc;
241 obj->ob_bytes[size] = '\0'; /* Trailing null byte */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000242
243 return 0;
244}
245
246PyObject *
247PyByteArray_Concat(PyObject *a, PyObject *b)
248{
249 Py_ssize_t size;
250 Py_buffer va, vb;
251 PyByteArrayObject *result = NULL;
252
253 va.len = -1;
254 vb.len = -1;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200255 if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
256 PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000257 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
258 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
259 goto done;
260 }
261
262 size = va.len + vb.len;
263 if (size < 0) {
Benjamin Petersone0124bd2009-03-09 21:04:33 +0000264 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000265 goto done;
266 }
267
268 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, size);
269 if (result != NULL) {
270 memcpy(result->ob_bytes, va.buf, va.len);
271 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
272 }
273
274 done:
275 if (va.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000276 PyBuffer_Release(&va);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000277 if (vb.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000278 PyBuffer_Release(&vb);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000279 return (PyObject *)result;
280}
281
Ethan Furmanb95b5612015-01-23 20:05:18 -0800282static PyObject *
283bytearray_format(PyByteArrayObject *self, PyObject *args)
284{
285 PyObject *bytes_in, *bytes_out, *res;
286 char *bytestring;
287
288 if (self == NULL || !PyByteArray_Check(self) || args == NULL) {
289 PyErr_BadInternalCall();
290 return NULL;
291 }
292 bytestring = PyByteArray_AS_STRING(self);
293 bytes_in = PyBytes_FromString(bytestring);
294 if (bytes_in == NULL)
295 return NULL;
296 bytes_out = _PyBytes_Format(bytes_in, args);
297 Py_DECREF(bytes_in);
298 if (bytes_out == NULL)
299 return NULL;
300 res = PyByteArray_FromObject(bytes_out);
301 Py_DECREF(bytes_out);
302 if (res == NULL)
303 return NULL;
304 return res;
305}
306
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000307/* Functions stuffed into the type object */
308
309static Py_ssize_t
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000310bytearray_length(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000311{
312 return Py_SIZE(self);
313}
314
315static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000316bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000317{
318 Py_ssize_t mysize;
319 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
328 mysize = Py_SIZE(self);
329 size = mysize + vo.len;
330 if (size < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000331 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000332 return PyErr_NoMemory();
333 }
Antoine Pitrou25454112015-05-19 20:52:27 +0200334 if (PyByteArray_Resize((PyObject *)self, size) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000335 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000336 return NULL;
337 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200338 memcpy(PyByteArray_AS_STRING(self) + mysize, vo.buf, vo.len);
Martin v. Löwis423be952008-08-13 15:53:07 +0000339 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000340 Py_INCREF(self);
341 return (PyObject *)self;
342}
343
344static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000345bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000346{
347 PyByteArrayObject *result;
348 Py_ssize_t mysize;
349 Py_ssize_t size;
350
351 if (count < 0)
352 count = 0;
353 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000354 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000355 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000356 size = mysize * count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000357 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
358 if (result != NULL && size != 0) {
359 if (mysize == 1)
360 memset(result->ob_bytes, self->ob_bytes[0], size);
361 else {
362 Py_ssize_t i;
363 for (i = 0; i < count; i++)
364 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
365 }
366 }
367 return (PyObject *)result;
368}
369
370static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000371bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000372{
373 Py_ssize_t mysize;
374 Py_ssize_t size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200375 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000376
377 if (count < 0)
378 count = 0;
379 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000380 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000381 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000382 size = mysize * count;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200383 if (PyByteArray_Resize((PyObject *)self, size) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000384 return NULL;
385
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200386 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000387 if (mysize == 1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200388 memset(buf, buf[0], size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000389 else {
390 Py_ssize_t i;
391 for (i = 1; i < count; i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200392 memcpy(buf + i*mysize, buf, mysize);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000393 }
394
395 Py_INCREF(self);
396 return (PyObject *)self;
397}
398
399static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000400bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000401{
402 if (i < 0)
403 i += Py_SIZE(self);
404 if (i < 0 || i >= Py_SIZE(self)) {
405 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
406 return NULL;
407 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200408 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000409}
410
411static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000412bytearray_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000413{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000414 if (PyIndex_Check(index)) {
415 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000416
417 if (i == -1 && PyErr_Occurred())
418 return NULL;
419
420 if (i < 0)
421 i += PyByteArray_GET_SIZE(self);
422
423 if (i < 0 || i >= Py_SIZE(self)) {
424 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
425 return NULL;
426 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200427 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000428 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000429 else if (PySlice_Check(index)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000430 Py_ssize_t start, stop, step, slicelength, cur, i;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000431 if (PySlice_GetIndicesEx(index,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000432 PyByteArray_GET_SIZE(self),
433 &start, &stop, &step, &slicelength) < 0) {
434 return NULL;
435 }
436
437 if (slicelength <= 0)
438 return PyByteArray_FromStringAndSize("", 0);
439 else if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200440 return PyByteArray_FromStringAndSize(
441 PyByteArray_AS_STRING(self) + start, slicelength);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000442 }
443 else {
444 char *source_buf = PyByteArray_AS_STRING(self);
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000445 char *result_buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000446 PyObject *result;
447
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000448 result = PyByteArray_FromStringAndSize(NULL, slicelength);
449 if (result == NULL)
450 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000451
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000452 result_buf = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000453 for (cur = start, i = 0; i < slicelength;
454 cur += step, i++) {
455 result_buf[i] = source_buf[cur];
456 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000457 return result;
458 }
459 }
460 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400461 PyErr_Format(PyExc_TypeError,
462 "bytearray indices must be integers or slices, not %.200s",
463 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000464 return NULL;
465 }
466}
467
468static int
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200469bytearray_setslice_linear(PyByteArrayObject *self,
470 Py_ssize_t lo, Py_ssize_t hi,
471 char *bytes, Py_ssize_t bytes_len)
472{
473 Py_ssize_t avail = hi - lo;
474 char *buf = PyByteArray_AS_STRING(self);
475 Py_ssize_t growth = bytes_len - avail;
Victor Stinner84557232013-11-21 12:29:51 +0100476 int res = 0;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200477 assert(avail >= 0);
478
Victor Stinner84557232013-11-21 12:29:51 +0100479 if (growth < 0) {
480 if (!_canresize(self))
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200481 return -1;
Victor Stinner84557232013-11-21 12:29:51 +0100482
483 if (lo == 0) {
484 /* Shrink the buffer by advancing its logical start */
485 self->ob_start -= growth;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200486 /*
Victor Stinner84557232013-11-21 12:29:51 +0100487 0 lo hi old_size
488 | |<----avail----->|<-----tail------>|
489 | |<-bytes_len->|<-----tail------>|
490 0 new_lo new_hi new_size
491 */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200492 }
Victor Stinner84557232013-11-21 12:29:51 +0100493 else {
494 /*
495 0 lo hi old_size
496 | |<----avail----->|<-----tomove------>|
497 | |<-bytes_len->|<-----tomove------>|
498 0 lo new_hi new_size
499 */
500 memmove(buf + lo + bytes_len, buf + hi,
501 Py_SIZE(self) - hi);
502 }
503 if (PyByteArray_Resize((PyObject *)self,
504 Py_SIZE(self) + growth) < 0) {
505 /* Issue #19578: Handling the memory allocation failure here is
506 tricky here because the bytearray object has already been
507 modified. Depending on growth and lo, the behaviour is
508 different.
509
510 If growth < 0 and lo != 0, the operation is completed, but a
511 MemoryError is still raised and the memory block is not
512 shrinked. Otherwise, the bytearray is restored in its previous
513 state and a MemoryError is raised. */
514 if (lo == 0) {
515 self->ob_start += growth;
516 return -1;
517 }
518 /* memmove() removed bytes, the bytearray object cannot be
519 restored in its previous state. */
520 Py_SIZE(self) += growth;
521 res = -1;
522 }
523 buf = PyByteArray_AS_STRING(self);
524 }
525 else if (growth > 0) {
526 if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
527 PyErr_NoMemory();
528 return -1;
529 }
530
531 if (PyByteArray_Resize((PyObject *)self,
532 Py_SIZE(self) + growth) < 0) {
533 return -1;
534 }
535 buf = PyByteArray_AS_STRING(self);
536 /* Make the place for the additional bytes */
537 /*
538 0 lo hi old_size
539 | |<-avail->|<-----tomove------>|
540 | |<---bytes_len-->|<-----tomove------>|
541 0 lo new_hi new_size
542 */
543 memmove(buf + lo + bytes_len, buf + hi,
544 Py_SIZE(self) - lo - bytes_len);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200545 }
546
547 if (bytes_len > 0)
548 memcpy(buf + lo, bytes, bytes_len);
Victor Stinner84557232013-11-21 12:29:51 +0100549 return res;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200550}
551
552static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000553bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000554 PyObject *values)
555{
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200556 Py_ssize_t needed;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000557 void *bytes;
558 Py_buffer vbytes;
559 int res = 0;
560
561 vbytes.len = -1;
562 if (values == (PyObject *)self) {
563 /* Make a copy and call this function recursively */
564 int err;
565 values = PyByteArray_FromObject(values);
566 if (values == NULL)
567 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000568 err = bytearray_setslice(self, lo, hi, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000569 Py_DECREF(values);
570 return err;
571 }
572 if (values == NULL) {
573 /* del b[lo:hi] */
574 bytes = NULL;
575 needed = 0;
576 }
577 else {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200578 if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
579 PyErr_Format(PyExc_TypeError,
580 "can't set bytearray slice from %.100s",
581 Py_TYPE(values)->tp_name);
582 return -1;
583 }
584 needed = vbytes.len;
585 bytes = vbytes.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000586 }
587
588 if (lo < 0)
589 lo = 0;
590 if (hi < lo)
591 hi = lo;
592 if (hi > Py_SIZE(self))
593 hi = Py_SIZE(self);
594
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200595 res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000596 if (vbytes.len != -1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200597 PyBuffer_Release(&vbytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000598 return res;
599}
600
601static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000602bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000603{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000604 int ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000605
606 if (i < 0)
607 i += Py_SIZE(self);
608
609 if (i < 0 || i >= Py_SIZE(self)) {
610 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
611 return -1;
612 }
613
614 if (value == NULL)
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000615 return bytearray_setslice(self, i, i+1, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000616
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000617 if (!_getbytevalue(value, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000618 return -1;
619
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200620 PyByteArray_AS_STRING(self)[i] = ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000621 return 0;
622}
623
624static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000625bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000626{
627 Py_ssize_t start, stop, step, slicelen, needed;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200628 char *buf, *bytes;
629 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000630
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000631 if (PyIndex_Check(index)) {
632 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000633
634 if (i == -1 && PyErr_Occurred())
635 return -1;
636
637 if (i < 0)
638 i += PyByteArray_GET_SIZE(self);
639
640 if (i < 0 || i >= Py_SIZE(self)) {
641 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
642 return -1;
643 }
644
645 if (values == NULL) {
646 /* Fall through to slice assignment */
647 start = i;
648 stop = i + 1;
649 step = 1;
650 slicelen = 1;
651 }
652 else {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000653 int ival;
654 if (!_getbytevalue(values, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000655 return -1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200656 buf[i] = (char)ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000657 return 0;
658 }
659 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000660 else if (PySlice_Check(index)) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000661 if (PySlice_GetIndicesEx(index,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000662 PyByteArray_GET_SIZE(self),
663 &start, &stop, &step, &slicelen) < 0) {
664 return -1;
665 }
666 }
667 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400668 PyErr_Format(PyExc_TypeError,
669 "bytearray indices must be integers or slices, not %.200s",
670 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000671 return -1;
672 }
673
674 if (values == NULL) {
675 bytes = NULL;
676 needed = 0;
677 }
678 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
Christian Heimes6d26ade2012-11-03 23:07:59 +0100679 int err;
Ezio Melottic64bcbe2012-11-03 21:19:06 +0200680 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
681 PyErr_SetString(PyExc_TypeError,
682 "can assign only bytes, buffers, or iterables "
683 "of ints in range(0, 256)");
684 return -1;
685 }
Georg Brandlf3fa5682010-12-04 17:09:30 +0000686 /* Make a copy and call this function recursively */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000687 values = PyByteArray_FromObject(values);
688 if (values == NULL)
689 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000690 err = bytearray_ass_subscript(self, index, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000691 Py_DECREF(values);
692 return err;
693 }
694 else {
695 assert(PyByteArray_Check(values));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200696 bytes = PyByteArray_AS_STRING(values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000697 needed = Py_SIZE(values);
698 }
699 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
700 if ((step < 0 && start < stop) ||
701 (step > 0 && start > stop))
702 stop = start;
703 if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200704 return bytearray_setslice_linear(self, start, stop, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000705 }
706 else {
707 if (needed == 0) {
708 /* Delete slice */
Mark Dickinsonbc099642010-01-29 17:27:24 +0000709 size_t cur;
710 Py_ssize_t i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000711
Antoine Pitrou5504e892008-12-06 21:27:53 +0000712 if (!_canresize(self))
713 return -1;
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000714
715 if (slicelen == 0)
716 /* Nothing to do here. */
717 return 0;
718
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000719 if (step < 0) {
720 stop = start + 1;
721 start = stop + step * (slicelen - 1) - 1;
722 step = -step;
723 }
724 for (cur = start, i = 0;
725 i < slicelen; cur += step, i++) {
726 Py_ssize_t lim = step - 1;
727
Mark Dickinson66f575b2010-02-14 12:53:32 +0000728 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000729 lim = PyByteArray_GET_SIZE(self) - cur - 1;
730
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200731 memmove(buf + cur - i,
732 buf + cur + 1, lim);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000733 }
734 /* Move the tail of the bytes, in one chunk */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000735 cur = start + (size_t)slicelen*step;
Mark Dickinson66f575b2010-02-14 12:53:32 +0000736 if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200737 memmove(buf + cur - slicelen,
738 buf + cur,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000739 PyByteArray_GET_SIZE(self) - cur);
740 }
741 if (PyByteArray_Resize((PyObject *)self,
742 PyByteArray_GET_SIZE(self) - slicelen) < 0)
743 return -1;
744
745 return 0;
746 }
747 else {
748 /* Assign slice */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000749 Py_ssize_t i;
750 size_t cur;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000751
752 if (needed != slicelen) {
753 PyErr_Format(PyExc_ValueError,
754 "attempt to assign bytes of size %zd "
755 "to extended slice of size %zd",
756 needed, slicelen);
757 return -1;
758 }
759 for (cur = start, i = 0; i < slicelen; cur += step, i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200760 buf[cur] = bytes[i];
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000761 return 0;
762 }
763 }
764}
765
766static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000767bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000768{
769 static char *kwlist[] = {"source", "encoding", "errors", 0};
770 PyObject *arg = NULL;
771 const char *encoding = NULL;
772 const char *errors = NULL;
773 Py_ssize_t count;
774 PyObject *it;
775 PyObject *(*iternext)(PyObject *);
776
777 if (Py_SIZE(self) != 0) {
778 /* Empty previous contents (yes, do this first of all!) */
779 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
780 return -1;
781 }
782
783 /* Parse arguments */
Georg Brandl3dbca812008-07-23 16:10:53 +0000784 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000785 &arg, &encoding, &errors))
786 return -1;
787
788 /* Make a quick exit if no first argument */
789 if (arg == NULL) {
790 if (encoding != NULL || errors != NULL) {
791 PyErr_SetString(PyExc_TypeError,
792 "encoding or errors without sequence argument");
793 return -1;
794 }
795 return 0;
796 }
797
798 if (PyUnicode_Check(arg)) {
799 /* Encode via the codec registry */
800 PyObject *encoded, *new;
801 if (encoding == NULL) {
802 PyErr_SetString(PyExc_TypeError,
803 "string argument without an encoding");
804 return -1;
805 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000806 encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000807 if (encoded == NULL)
808 return -1;
809 assert(PyBytes_Check(encoded));
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000810 new = bytearray_iconcat(self, encoded);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000811 Py_DECREF(encoded);
812 if (new == NULL)
813 return -1;
814 Py_DECREF(new);
815 return 0;
816 }
817
818 /* If it's not unicode, there can't be encoding or errors */
819 if (encoding != NULL || errors != NULL) {
820 PyErr_SetString(PyExc_TypeError,
821 "encoding or errors without a string argument");
822 return -1;
823 }
824
825 /* Is it an int? */
Benjamin Peterson8380dd52010-04-16 22:51:37 +0000826 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
827 if (count == -1 && PyErr_Occurred()) {
828 if (PyErr_ExceptionMatches(PyExc_OverflowError))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000829 return -1;
Benjamin Peterson9c0e94f2010-04-16 23:00:53 +0000830 PyErr_Clear();
Benjamin Peterson8380dd52010-04-16 22:51:37 +0000831 }
832 else if (count < 0) {
833 PyErr_SetString(PyExc_ValueError, "negative count");
834 return -1;
835 }
836 else {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000837 if (count > 0) {
Victor Stinner2bc4d952014-06-02 22:22:42 +0200838 if (PyByteArray_Resize((PyObject *)self, count))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000839 return -1;
Victor Stinner2bc4d952014-06-02 22:22:42 +0200840 memset(PyByteArray_AS_STRING(self), 0, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000841 }
842 return 0;
843 }
844
845 /* Use the buffer API */
846 if (PyObject_CheckBuffer(arg)) {
847 Py_ssize_t size;
848 Py_buffer view;
849 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
850 return -1;
851 size = view.len;
852 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200853 if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
854 &view, size, 'C') < 0)
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200855 goto fail;
Martin v. Löwis423be952008-08-13 15:53:07 +0000856 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000857 return 0;
858 fail:
Martin v. Löwis423be952008-08-13 15:53:07 +0000859 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000860 return -1;
861 }
862
863 /* XXX Optimize this if the arguments is a list, tuple */
864
865 /* Get the iterator */
866 it = PyObject_GetIter(arg);
867 if (it == NULL)
868 return -1;
869 iternext = *Py_TYPE(it)->tp_iternext;
870
871 /* Run the iterator to exhaustion */
872 for (;;) {
873 PyObject *item;
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000874 int rc, value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000875
876 /* Get the next item */
877 item = iternext(it);
878 if (item == NULL) {
879 if (PyErr_Occurred()) {
880 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
881 goto error;
882 PyErr_Clear();
883 }
884 break;
885 }
886
887 /* Interpret it as an int (__index__) */
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000888 rc = _getbytevalue(item, &value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000889 Py_DECREF(item);
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000890 if (!rc)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000891 goto error;
892
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000893 /* Append the byte */
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300894 if (Py_SIZE(self) + 1 < self->ob_alloc) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000895 Py_SIZE(self)++;
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300896 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
897 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000898 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
899 goto error;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200900 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000901 }
902
903 /* Clean up and return success */
904 Py_DECREF(it);
905 return 0;
906
907 error:
908 /* Error handling when it != NULL */
909 Py_DECREF(it);
910 return -1;
911}
912
913/* Mostly copied from string_repr, but without the
914 "smart quote" functionality. */
915static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000916bytearray_repr(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000917{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000918 const char *quote_prefix = "bytearray(b";
919 const char *quote_postfix = ")";
920 Py_ssize_t length = Py_SIZE(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200921 /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
Mark Dickinson66f575b2010-02-14 12:53:32 +0000922 size_t newsize;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000923 PyObject *v;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200924 Py_ssize_t i;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200925 char *bytes;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200926 char c;
927 char *p;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200928 int quote;
929 char *test, *start;
930 char *buffer;
931
932 if (length > (PY_SSIZE_T_MAX - 15) / 4) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000933 PyErr_SetString(PyExc_OverflowError,
934 "bytearray object is too large to make repr");
935 return NULL;
936 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200937
938 newsize = 15 + length * 4;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100939 buffer = PyObject_Malloc(newsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200940 if (buffer == NULL) {
941 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000942 return NULL;
943 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000944
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200945 /* Figure out which quote to use; single is preferred */
946 quote = '\'';
947 start = PyByteArray_AS_STRING(self);
948 for (test = start; test < start+length; ++test) {
949 if (*test == '"') {
950 quote = '\''; /* back to single */
951 break;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000952 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200953 else if (*test == '\'')
954 quote = '"';
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000955 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200956
957 p = buffer;
958 while (*quote_prefix)
959 *p++ = *quote_prefix++;
960 *p++ = quote;
961
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200962 bytes = PyByteArray_AS_STRING(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200963 for (i = 0; i < length; i++) {
964 /* There's at least enough room for a hex escape
965 and a closing quote. */
966 assert(newsize - (p - buffer) >= 5);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200967 c = bytes[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200968 if (c == '\'' || c == '\\')
969 *p++ = '\\', *p++ = c;
970 else if (c == '\t')
971 *p++ = '\\', *p++ = 't';
972 else if (c == '\n')
973 *p++ = '\\', *p++ = 'n';
974 else if (c == '\r')
975 *p++ = '\\', *p++ = 'r';
976 else if (c == 0)
977 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
978 else if (c < ' ' || c >= 0x7f) {
979 *p++ = '\\';
980 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200981 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
982 *p++ = Py_hexdigits[c & 0xf];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200983 }
984 else
985 *p++ = c;
986 }
987 assert(newsize - (p - buffer) >= 1);
988 *p++ = quote;
989 while (*quote_postfix) {
990 *p++ = *quote_postfix++;
991 }
992
993 v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100994 PyObject_Free(buffer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200995 return v;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000996}
997
998static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000999bytearray_str(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001000{
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001001 if (Py_BytesWarningFlag) {
1002 if (PyErr_WarnEx(PyExc_BytesWarning,
1003 "str() on a bytearray instance", 1))
1004 return NULL;
1005 }
1006 return bytearray_repr((PyByteArrayObject*)op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001007}
1008
1009static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001010bytearray_richcompare(PyObject *self, PyObject *other, int op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001011{
1012 Py_ssize_t self_size, other_size;
1013 Py_buffer self_bytes, other_bytes;
1014 PyObject *res;
1015 Py_ssize_t minsize;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001016 int cmp, rc;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001017
1018 /* Bytes can be compared to anything that supports the (binary)
1019 buffer API. Except that a comparison with Unicode is always an
1020 error, even if the comparison is for equality. */
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001021 rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
1022 if (!rc)
1023 rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
1024 if (rc < 0)
1025 return NULL;
1026 if (rc) {
Barry Warsaw9e9dcd62008-10-17 01:50:37 +00001027 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001028 if (PyErr_WarnEx(PyExc_BytesWarning,
Georg Brandle5d68ac2008-06-04 11:30:26 +00001029 "Comparison between bytearray and string", 1))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001030 return NULL;
1031 }
1032
Brian Curtindfc80e32011-08-10 20:28:54 -05001033 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001034 }
1035
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001036 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001037 PyErr_Clear();
Brian Curtindfc80e32011-08-10 20:28:54 -05001038 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001039 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001040 self_size = self_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001041
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001042 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001043 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00001044 PyBuffer_Release(&self_bytes);
Brian Curtindfc80e32011-08-10 20:28:54 -05001045 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001046 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001047 other_size = other_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001048
1049 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1050 /* Shortcut: if the lengths differ, the objects differ */
1051 cmp = (op == Py_NE);
1052 }
1053 else {
1054 minsize = self_size;
1055 if (other_size < minsize)
1056 minsize = other_size;
1057
1058 cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
1059 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1060
1061 if (cmp == 0) {
1062 if (self_size < other_size)
1063 cmp = -1;
1064 else if (self_size > other_size)
1065 cmp = 1;
1066 }
1067
1068 switch (op) {
1069 case Py_LT: cmp = cmp < 0; break;
1070 case Py_LE: cmp = cmp <= 0; break;
1071 case Py_EQ: cmp = cmp == 0; break;
1072 case Py_NE: cmp = cmp != 0; break;
1073 case Py_GT: cmp = cmp > 0; break;
1074 case Py_GE: cmp = cmp >= 0; break;
1075 }
1076 }
1077
1078 res = cmp ? Py_True : Py_False;
Martin v. Löwis423be952008-08-13 15:53:07 +00001079 PyBuffer_Release(&self_bytes);
1080 PyBuffer_Release(&other_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001081 Py_INCREF(res);
1082 return res;
1083}
1084
1085static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001086bytearray_dealloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001087{
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001088 if (self->ob_exports > 0) {
1089 PyErr_SetString(PyExc_SystemError,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001090 "deallocated bytearray object has exported buffers");
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001091 PyErr_Print();
1092 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001093 if (self->ob_bytes != 0) {
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001094 PyObject_Free(self->ob_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001095 }
1096 Py_TYPE(self)->tp_free((PyObject *)self);
1097}
1098
1099
1100/* -------------------------------------------------------------------- */
1101/* Methods */
1102
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001103#define FASTSEARCH fastsearch
1104#define STRINGLIB(F) stringlib_##F
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001105#define STRINGLIB_CHAR char
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001106#define STRINGLIB_SIZEOF_CHAR 1
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001107#define STRINGLIB_LEN PyByteArray_GET_SIZE
1108#define STRINGLIB_STR PyByteArray_AS_STRING
1109#define STRINGLIB_NEW PyByteArray_FromStringAndSize
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001110#define STRINGLIB_ISSPACE Py_ISSPACE
1111#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001112#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1113#define STRINGLIB_MUTABLE 1
1114
1115#include "stringlib/fastsearch.h"
1116#include "stringlib/count.h"
1117#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001118#include "stringlib/join.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001119#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001120#include "stringlib/split.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001121#include "stringlib/ctype.h"
1122#include "stringlib/transmogrify.h"
1123
1124
1125/* The following Py_LOCAL_INLINE and Py_LOCAL functions
1126were copied from the old char* style string object. */
1127
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001128/* helper macro to fixup start/end slice values */
1129#define ADJUST_INDICES(start, end, len) \
1130 if (end > len) \
1131 end = len; \
1132 else if (end < 0) { \
1133 end += len; \
1134 if (end < 0) \
1135 end = 0; \
1136 } \
1137 if (start < 0) { \
1138 start += len; \
1139 if (start < 0) \
1140 start = 0; \
1141 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001142
1143Py_LOCAL_INLINE(Py_ssize_t)
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001144bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001145{
1146 PyObject *subobj;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001147 char byte;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001148 Py_buffer subbuf;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001149 const char *sub;
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001150 Py_ssize_t len, sub_len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001151 Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
1152 Py_ssize_t res;
1153
Antoine Pitrouac65d962011-10-20 23:54:17 +02001154 if (!stringlib_parse_args_finds_byte("find/rfind/index/rindex",
1155 args, &subobj, &byte, &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001156 return -2;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001157
1158 if (subobj) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001159 if (PyObject_GetBuffer(subobj, &subbuf, PyBUF_SIMPLE) != 0)
Antoine Pitrouac65d962011-10-20 23:54:17 +02001160 return -2;
1161
1162 sub = subbuf.buf;
1163 sub_len = subbuf.len;
1164 }
1165 else {
1166 sub = &byte;
1167 sub_len = 1;
1168 }
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001169 len = PyByteArray_GET_SIZE(self);
Antoine Pitrouac65d962011-10-20 23:54:17 +02001170
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001171 ADJUST_INDICES(start, end, len);
1172 if (end - start < sub_len)
1173 res = -1;
Serhiy Storchakad92d4ef2015-07-20 22:58:02 +03001174 else if (sub_len == 1
1175#ifndef HAVE_MEMRCHR
1176 && dir > 0
1177#endif
1178 ) {
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001179 unsigned char needle = *sub;
Serhiy Storchakad92d4ef2015-07-20 22:58:02 +03001180 int mode = (dir > 0) ? FAST_SEARCH : FAST_RSEARCH;
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001181 res = stringlib_fastsearch_memchr_1char(
1182 PyByteArray_AS_STRING(self) + start, end - start,
Serhiy Storchakad92d4ef2015-07-20 22:58:02 +03001183 needle, needle, mode);
Serhiy Storchakad9d769f2015-03-24 21:55:47 +02001184 if (res >= 0)
1185 res += start;
1186 }
1187 else {
1188 if (dir > 0)
1189 res = stringlib_find_slice(
1190 PyByteArray_AS_STRING(self), len,
1191 sub, sub_len, start, end);
1192 else
1193 res = stringlib_rfind_slice(
1194 PyByteArray_AS_STRING(self), len,
1195 sub, sub_len, start, end);
1196 }
Antoine Pitrouac65d962011-10-20 23:54:17 +02001197
1198 if (subobj)
1199 PyBuffer_Release(&subbuf);
1200
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001201 return res;
1202}
1203
1204PyDoc_STRVAR(find__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001205"B.find(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001206\n\
1207Return the lowest index in B where subsection sub is found,\n\
Senthil Kumaran53516a82011-07-27 23:33:54 +08001208such that sub is contained within B[start,end]. Optional\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001209arguments start and end are interpreted as in slice notation.\n\
1210\n\
1211Return -1 on failure.");
1212
1213static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001214bytearray_find(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001215{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001216 Py_ssize_t result = bytearray_find_internal(self, args, +1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001217 if (result == -2)
1218 return NULL;
1219 return PyLong_FromSsize_t(result);
1220}
1221
1222PyDoc_STRVAR(count__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001223"B.count(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001224\n\
1225Return the number of non-overlapping occurrences of subsection sub in\n\
1226bytes B[start:end]. Optional arguments start and end are interpreted\n\
1227as in slice notation.");
1228
1229static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001230bytearray_count(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001231{
1232 PyObject *sub_obj;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001233 const char *str = PyByteArray_AS_STRING(self), *sub;
1234 Py_ssize_t sub_len;
1235 char byte;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001236 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
Antoine Pitrouac65d962011-10-20 23:54:17 +02001237
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001238 Py_buffer vsub;
1239 PyObject *count_obj;
1240
Antoine Pitrouac65d962011-10-20 23:54:17 +02001241 if (!stringlib_parse_args_finds_byte("count", args, &sub_obj, &byte,
1242 &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001243 return NULL;
1244
Antoine Pitrouac65d962011-10-20 23:54:17 +02001245 if (sub_obj) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001246 if (PyObject_GetBuffer(sub_obj, &vsub, PyBUF_SIMPLE) != 0)
Antoine Pitrouac65d962011-10-20 23:54:17 +02001247 return NULL;
1248
1249 sub = vsub.buf;
1250 sub_len = vsub.len;
1251 }
1252 else {
1253 sub = &byte;
1254 sub_len = 1;
1255 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001256
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001257 ADJUST_INDICES(start, end, PyByteArray_GET_SIZE(self));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001258
1259 count_obj = PyLong_FromSsize_t(
Antoine Pitrouac65d962011-10-20 23:54:17 +02001260 stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001261 );
Antoine Pitrouac65d962011-10-20 23:54:17 +02001262
1263 if (sub_obj)
1264 PyBuffer_Release(&vsub);
1265
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001266 return count_obj;
1267}
1268
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001269/*[clinic input]
1270bytearray.clear
1271
1272 self: self(type="PyByteArrayObject *")
1273
1274Remove all items from the bytearray.
1275[clinic start generated code]*/
1276
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001277static PyObject *
1278bytearray_clear_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001279/*[clinic end generated code: output=85c2fe6aede0956c input=e524fd330abcdc18]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001280{
1281 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1282 return NULL;
1283 Py_RETURN_NONE;
1284}
1285
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001286/*[clinic input]
1287bytearray.copy
1288
1289 self: self(type="PyByteArrayObject *")
1290
1291Return a copy of B.
1292[clinic start generated code]*/
1293
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001294static PyObject *
1295bytearray_copy_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001296/*[clinic end generated code: output=68cfbcfed484c132 input=6d5d2975aa0f33f3]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001297{
1298 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1299 PyByteArray_GET_SIZE(self));
1300}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001301
1302PyDoc_STRVAR(index__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001303"B.index(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001304\n\
1305Like B.find() but raise ValueError when the subsection is not found.");
1306
1307static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001308bytearray_index(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001309{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001310 Py_ssize_t result = bytearray_find_internal(self, args, +1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001311 if (result == -2)
1312 return NULL;
1313 if (result == -1) {
1314 PyErr_SetString(PyExc_ValueError,
1315 "subsection not found");
1316 return NULL;
1317 }
1318 return PyLong_FromSsize_t(result);
1319}
1320
1321
1322PyDoc_STRVAR(rfind__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001323"B.rfind(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001324\n\
1325Return the highest index in B where subsection sub is found,\n\
Senthil Kumaran53516a82011-07-27 23:33:54 +08001326such that sub is contained within B[start,end]. Optional\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001327arguments start and end are interpreted as in slice notation.\n\
1328\n\
1329Return -1 on failure.");
1330
1331static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001332bytearray_rfind(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001333{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001334 Py_ssize_t result = bytearray_find_internal(self, args, -1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001335 if (result == -2)
1336 return NULL;
1337 return PyLong_FromSsize_t(result);
1338}
1339
1340
1341PyDoc_STRVAR(rindex__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001342"B.rindex(sub[, start[, end]]) -> int\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001343\n\
1344Like B.rfind() but raise ValueError when the subsection is not found.");
1345
1346static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001347bytearray_rindex(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001348{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001349 Py_ssize_t result = bytearray_find_internal(self, args, -1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001350 if (result == -2)
1351 return NULL;
1352 if (result == -1) {
1353 PyErr_SetString(PyExc_ValueError,
1354 "subsection not found");
1355 return NULL;
1356 }
1357 return PyLong_FromSsize_t(result);
1358}
1359
1360
1361static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001362bytearray_contains(PyObject *self, PyObject *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001363{
1364 Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError);
1365 if (ival == -1 && PyErr_Occurred()) {
1366 Py_buffer varg;
Antoine Pitrou0010d372010-08-15 17:12:55 +00001367 Py_ssize_t pos;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001368 PyErr_Clear();
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001369 if (PyObject_GetBuffer(arg, &varg, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001370 return -1;
1371 pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self),
1372 varg.buf, varg.len, 0);
Martin v. Löwis423be952008-08-13 15:53:07 +00001373 PyBuffer_Release(&varg);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001374 return pos >= 0;
1375 }
1376 if (ival < 0 || ival >= 256) {
1377 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
1378 return -1;
1379 }
1380
Antoine Pitrou0010d372010-08-15 17:12:55 +00001381 return memchr(PyByteArray_AS_STRING(self), (int) ival, Py_SIZE(self)) != NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001382}
1383
1384
1385/* Matches the end (direction >= 0) or start (direction < 0) of self
1386 * against substr, using the start and end arguments. Returns
1387 * -1 on error, 0 if not found and 1 if found.
1388 */
1389Py_LOCAL(int)
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001390_bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001391 Py_ssize_t end, int direction)
1392{
1393 Py_ssize_t len = PyByteArray_GET_SIZE(self);
1394 const char* str;
1395 Py_buffer vsubstr;
1396 int rv = 0;
1397
1398 str = PyByteArray_AS_STRING(self);
1399
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001400 if (PyObject_GetBuffer(substr, &vsubstr, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001401 return -1;
1402
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001403 ADJUST_INDICES(start, end, len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001404
1405 if (direction < 0) {
1406 /* startswith */
1407 if (start+vsubstr.len > len) {
1408 goto done;
1409 }
1410 } else {
1411 /* endswith */
1412 if (end-start < vsubstr.len || start > len) {
1413 goto done;
1414 }
1415
1416 if (end-vsubstr.len > start)
1417 start = end - vsubstr.len;
1418 }
1419 if (end-start >= vsubstr.len)
1420 rv = ! memcmp(str+start, vsubstr.buf, vsubstr.len);
1421
1422done:
Martin v. Löwis423be952008-08-13 15:53:07 +00001423 PyBuffer_Release(&vsubstr);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001424 return rv;
1425}
1426
1427
1428PyDoc_STRVAR(startswith__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001429"B.startswith(prefix[, start[, end]]) -> bool\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001430\n\
1431Return True if B starts with the specified prefix, False otherwise.\n\
1432With optional start, test B beginning at that position.\n\
1433With optional end, stop comparing B at that position.\n\
Ezio Melottiba42fd52011-04-26 06:09:45 +03001434prefix can also be a tuple of bytes to try.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001435
1436static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001437bytearray_startswith(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001438{
1439 Py_ssize_t start = 0;
1440 Py_ssize_t end = PY_SSIZE_T_MAX;
1441 PyObject *subobj;
1442 int result;
1443
Jesus Ceaac451502011-04-20 17:09:23 +02001444 if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001445 return NULL;
1446 if (PyTuple_Check(subobj)) {
1447 Py_ssize_t i;
1448 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001449 result = _bytearray_tailmatch(self,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001450 PyTuple_GET_ITEM(subobj, i),
1451 start, end, -1);
1452 if (result == -1)
1453 return NULL;
1454 else if (result) {
1455 Py_RETURN_TRUE;
1456 }
1457 }
1458 Py_RETURN_FALSE;
1459 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001460 result = _bytearray_tailmatch(self, subobj, start, end, -1);
Ezio Melottiba42fd52011-04-26 06:09:45 +03001461 if (result == -1) {
1462 if (PyErr_ExceptionMatches(PyExc_TypeError))
1463 PyErr_Format(PyExc_TypeError, "startswith first arg must be bytes "
1464 "or a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001465 return NULL;
Ezio Melottiba42fd52011-04-26 06:09:45 +03001466 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001467 else
1468 return PyBool_FromLong(result);
1469}
1470
1471PyDoc_STRVAR(endswith__doc__,
Georg Brandl17cb8a82008-05-30 08:20:09 +00001472"B.endswith(suffix[, start[, end]]) -> bool\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001473\n\
1474Return True if B ends with the specified suffix, False otherwise.\n\
1475With optional start, test B beginning at that position.\n\
1476With optional end, stop comparing B at that position.\n\
Ezio Melottiba42fd52011-04-26 06:09:45 +03001477suffix can also be a tuple of bytes to try.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001478
1479static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001480bytearray_endswith(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001481{
1482 Py_ssize_t start = 0;
1483 Py_ssize_t end = PY_SSIZE_T_MAX;
1484 PyObject *subobj;
1485 int result;
1486
Jesus Ceaac451502011-04-20 17:09:23 +02001487 if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001488 return NULL;
1489 if (PyTuple_Check(subobj)) {
1490 Py_ssize_t i;
1491 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001492 result = _bytearray_tailmatch(self,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001493 PyTuple_GET_ITEM(subobj, i),
1494 start, end, +1);
1495 if (result == -1)
1496 return NULL;
1497 else if (result) {
1498 Py_RETURN_TRUE;
1499 }
1500 }
1501 Py_RETURN_FALSE;
1502 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001503 result = _bytearray_tailmatch(self, subobj, start, end, +1);
Ezio Melottiba42fd52011-04-26 06:09:45 +03001504 if (result == -1) {
1505 if (PyErr_ExceptionMatches(PyExc_TypeError))
1506 PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or "
1507 "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001508 return NULL;
Ezio Melottiba42fd52011-04-26 06:09:45 +03001509 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001510 else
1511 return PyBool_FromLong(result);
1512}
1513
1514
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001515/*[clinic input]
1516bytearray.translate
1517
1518 self: self(type="PyByteArrayObject *")
1519 table: object
1520 Translation table, which must be a bytes object of length 256.
1521 [
1522 deletechars: object
1523 ]
1524 /
1525
1526Return a copy with each character mapped by the given translation table.
1527
1528All characters occurring in the optional argument deletechars are removed.
1529The remaining characters are mapped through the given translation table.
1530[clinic start generated code]*/
1531
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001532static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001533bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
1534 int group_right_1, PyObject *deletechars)
1535/*[clinic end generated code: output=2bebc86a9a1ff083 input=b749ad85f4860824]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001536{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001537 char *input, *output;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001538 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001539 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001540 PyObject *input_obj = (PyObject*)self;
1541 const char *output_start;
1542 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001543 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001544 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001545 Py_buffer vtable, vdel;
1546
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001547 if (table == Py_None) {
1548 table_chars = NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001549 table = NULL;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001550 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001551 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001552 } else {
1553 if (vtable.len != 256) {
1554 PyErr_SetString(PyExc_ValueError,
1555 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001556 PyBuffer_Release(&vtable);
1557 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001558 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001559 table_chars = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001560 }
1561
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001562 if (deletechars != NULL) {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001563 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001564 if (table != NULL)
Georg Brandl953152f2009-07-22 12:03:59 +00001565 PyBuffer_Release(&vtable);
1566 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001567 }
1568 }
1569 else {
1570 vdel.buf = NULL;
1571 vdel.len = 0;
1572 }
1573
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001574 inlen = PyByteArray_GET_SIZE(input_obj);
1575 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1576 if (result == NULL)
1577 goto done;
1578 output_start = output = PyByteArray_AsString(result);
1579 input = PyByteArray_AS_STRING(input_obj);
1580
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001581 if (vdel.len == 0 && table_chars != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001582 /* If no deletions are required, use faster code */
1583 for (i = inlen; --i >= 0; ) {
1584 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001585 *output++ = table_chars[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001586 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001587 goto done;
1588 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001589
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001590 if (table_chars == NULL) {
Georg Brandlccc47b62008-12-28 11:44:14 +00001591 for (i = 0; i < 256; i++)
1592 trans_table[i] = Py_CHARMASK(i);
1593 } else {
1594 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001595 trans_table[i] = Py_CHARMASK(table_chars[i]);
Georg Brandlccc47b62008-12-28 11:44:14 +00001596 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001597
1598 for (i = 0; i < vdel.len; i++)
1599 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1600
1601 for (i = inlen; --i >= 0; ) {
1602 c = Py_CHARMASK(*input++);
1603 if (trans_table[c] != -1)
1604 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1605 continue;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001606 }
1607 /* Fix the size of the resulting string */
1608 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001609 if (PyByteArray_Resize(result, output - output_start) < 0) {
1610 Py_CLEAR(result);
1611 goto done;
1612 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001613
1614done:
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001615 if (table != NULL)
Georg Brandlccc47b62008-12-28 11:44:14 +00001616 PyBuffer_Release(&vtable);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001617 if (deletechars != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001618 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001619 return result;
1620}
1621
1622
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001623/*[clinic input]
1624
1625@staticmethod
1626bytearray.maketrans
1627
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001628 frm: Py_buffer
1629 to: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001630 /
1631
1632Return a translation table useable for the bytes or bytearray translate method.
1633
1634The returned table will be one where each byte in frm is mapped to the byte at
1635the same position in to.
1636
1637The bytes objects frm and to must be of the same length.
1638[clinic start generated code]*/
1639
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001640static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001641bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001642/*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001643{
1644 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00001645}
1646
1647
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001648/* find and count characters and substrings */
1649
1650#define findchar(target, target_len, c) \
1651 ((char *)memchr((const void *)(target), c, target_len))
1652
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001653
Benjamin Peterson0f3641c2008-11-19 22:05:52 +00001654/* Bytes ops must return a string, create a copy */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001655Py_LOCAL(PyByteArrayObject *)
1656return_self(PyByteArrayObject *self)
1657{
Georg Brandl1e7217d2008-05-30 12:02:38 +00001658 /* always return a new bytearray */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001659 return (PyByteArrayObject *)PyByteArray_FromStringAndSize(
1660 PyByteArray_AS_STRING(self),
1661 PyByteArray_GET_SIZE(self));
1662}
1663
1664Py_LOCAL_INLINE(Py_ssize_t)
1665countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount)
1666{
1667 Py_ssize_t count=0;
1668 const char *start=target;
1669 const char *end=target+target_len;
1670
1671 while ( (start=findchar(start, end-start, c)) != NULL ) {
1672 count++;
1673 if (count >= maxcount)
1674 break;
1675 start += 1;
1676 }
1677 return count;
1678}
1679
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001680
1681/* Algorithms for different cases of string replacement */
1682
1683/* len(self)>=1, from="", len(to)>=1, maxcount>=1 */
1684Py_LOCAL(PyByteArrayObject *)
1685replace_interleave(PyByteArrayObject *self,
1686 const char *to_s, Py_ssize_t to_len,
1687 Py_ssize_t maxcount)
1688{
1689 char *self_s, *result_s;
1690 Py_ssize_t self_len, result_len;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001691 Py_ssize_t count, i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001692 PyByteArrayObject *result;
1693
1694 self_len = PyByteArray_GET_SIZE(self);
1695
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001696 /* 1 at the end plus 1 after every character;
1697 count = min(maxcount, self_len + 1) */
1698 if (maxcount <= self_len)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001699 count = maxcount;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001700 else
1701 /* Can't overflow: self_len + 1 <= maxcount <= PY_SSIZE_T_MAX. */
1702 count = self_len + 1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001703
1704 /* Check for overflow */
1705 /* result_len = count * to_len + self_len; */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001706 assert(count > 0);
1707 if (to_len > (PY_SSIZE_T_MAX - self_len) / count) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001708 PyErr_SetString(PyExc_OverflowError,
1709 "replace string is too long");
1710 return NULL;
1711 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001712 result_len = count * to_len + self_len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001713
1714 if (! (result = (PyByteArrayObject *)
1715 PyByteArray_FromStringAndSize(NULL, result_len)) )
1716 return NULL;
1717
1718 self_s = PyByteArray_AS_STRING(self);
1719 result_s = PyByteArray_AS_STRING(result);
1720
1721 /* TODO: special case single character, which doesn't need memcpy */
1722
1723 /* Lay the first one down (guaranteed this will occur) */
1724 Py_MEMCPY(result_s, to_s, to_len);
1725 result_s += to_len;
1726 count -= 1;
1727
1728 for (i=0; i<count; i++) {
1729 *result_s++ = *self_s++;
1730 Py_MEMCPY(result_s, to_s, to_len);
1731 result_s += to_len;
1732 }
1733
1734 /* Copy the rest of the original string */
1735 Py_MEMCPY(result_s, self_s, self_len-i);
1736
1737 return result;
1738}
1739
1740/* Special case for deleting a single character */
1741/* len(self)>=1, len(from)==1, to="", maxcount>=1 */
1742Py_LOCAL(PyByteArrayObject *)
1743replace_delete_single_character(PyByteArrayObject *self,
1744 char from_c, Py_ssize_t maxcount)
1745{
1746 char *self_s, *result_s;
1747 char *start, *next, *end;
1748 Py_ssize_t self_len, result_len;
1749 Py_ssize_t count;
1750 PyByteArrayObject *result;
1751
1752 self_len = PyByteArray_GET_SIZE(self);
1753 self_s = PyByteArray_AS_STRING(self);
1754
1755 count = countchar(self_s, self_len, from_c, maxcount);
1756 if (count == 0) {
1757 return return_self(self);
1758 }
1759
1760 result_len = self_len - count; /* from_len == 1 */
1761 assert(result_len>=0);
1762
1763 if ( (result = (PyByteArrayObject *)
1764 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1765 return NULL;
1766 result_s = PyByteArray_AS_STRING(result);
1767
1768 start = self_s;
1769 end = self_s + self_len;
1770 while (count-- > 0) {
1771 next = findchar(start, end-start, from_c);
1772 if (next == NULL)
1773 break;
1774 Py_MEMCPY(result_s, start, next-start);
1775 result_s += (next-start);
1776 start = next+1;
1777 }
1778 Py_MEMCPY(result_s, start, end-start);
1779
1780 return result;
1781}
1782
1783/* len(self)>=1, len(from)>=2, to="", maxcount>=1 */
1784
1785Py_LOCAL(PyByteArrayObject *)
1786replace_delete_substring(PyByteArrayObject *self,
1787 const char *from_s, Py_ssize_t from_len,
1788 Py_ssize_t maxcount)
1789{
1790 char *self_s, *result_s;
1791 char *start, *next, *end;
1792 Py_ssize_t self_len, result_len;
1793 Py_ssize_t count, offset;
1794 PyByteArrayObject *result;
1795
1796 self_len = PyByteArray_GET_SIZE(self);
1797 self_s = PyByteArray_AS_STRING(self);
1798
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001799 count = stringlib_count(self_s, self_len,
1800 from_s, from_len,
1801 maxcount);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001802
1803 if (count == 0) {
1804 /* no matches */
1805 return return_self(self);
1806 }
1807
1808 result_len = self_len - (count * from_len);
1809 assert (result_len>=0);
1810
1811 if ( (result = (PyByteArrayObject *)
1812 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL )
1813 return NULL;
1814
1815 result_s = PyByteArray_AS_STRING(result);
1816
1817 start = self_s;
1818 end = self_s + self_len;
1819 while (count-- > 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001820 offset = stringlib_find(start, end-start,
1821 from_s, from_len,
1822 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001823 if (offset == -1)
1824 break;
1825 next = start + offset;
1826
1827 Py_MEMCPY(result_s, start, next-start);
1828
1829 result_s += (next-start);
1830 start = next+from_len;
1831 }
1832 Py_MEMCPY(result_s, start, end-start);
1833 return result;
1834}
1835
1836/* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */
1837Py_LOCAL(PyByteArrayObject *)
1838replace_single_character_in_place(PyByteArrayObject *self,
1839 char from_c, char to_c,
1840 Py_ssize_t maxcount)
1841{
Antoine Pitroud1188562010-06-09 16:38:55 +00001842 char *self_s, *result_s, *start, *end, *next;
1843 Py_ssize_t self_len;
1844 PyByteArrayObject *result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001845
Antoine Pitroud1188562010-06-09 16:38:55 +00001846 /* The result string will be the same size */
1847 self_s = PyByteArray_AS_STRING(self);
1848 self_len = PyByteArray_GET_SIZE(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001849
Antoine Pitroud1188562010-06-09 16:38:55 +00001850 next = findchar(self_s, self_len, from_c);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001851
Antoine Pitroud1188562010-06-09 16:38:55 +00001852 if (next == NULL) {
1853 /* No matches; return the original bytes */
1854 return return_self(self);
1855 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001856
Antoine Pitroud1188562010-06-09 16:38:55 +00001857 /* Need to make a new bytes */
1858 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len);
1859 if (result == NULL)
1860 return NULL;
1861 result_s = PyByteArray_AS_STRING(result);
1862 Py_MEMCPY(result_s, self_s, self_len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001863
Antoine Pitroud1188562010-06-09 16:38:55 +00001864 /* change everything in-place, starting with this one */
1865 start = result_s + (next-self_s);
1866 *start = to_c;
1867 start++;
1868 end = result_s + self_len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001869
Antoine Pitroud1188562010-06-09 16:38:55 +00001870 while (--maxcount > 0) {
1871 next = findchar(start, end-start, from_c);
1872 if (next == NULL)
1873 break;
1874 *next = to_c;
1875 start = next+1;
1876 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001877
Antoine Pitroud1188562010-06-09 16:38:55 +00001878 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001879}
1880
1881/* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */
1882Py_LOCAL(PyByteArrayObject *)
1883replace_substring_in_place(PyByteArrayObject *self,
1884 const char *from_s, Py_ssize_t from_len,
1885 const char *to_s, Py_ssize_t to_len,
1886 Py_ssize_t maxcount)
1887{
1888 char *result_s, *start, *end;
1889 char *self_s;
1890 Py_ssize_t self_len, offset;
1891 PyByteArrayObject *result;
1892
1893 /* The result bytes will be the same size */
1894
1895 self_s = PyByteArray_AS_STRING(self);
1896 self_len = PyByteArray_GET_SIZE(self);
1897
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001898 offset = stringlib_find(self_s, self_len,
1899 from_s, from_len,
1900 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001901 if (offset == -1) {
1902 /* No matches; return the original bytes */
1903 return return_self(self);
1904 }
1905
1906 /* Need to make a new bytes */
1907 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len);
1908 if (result == NULL)
1909 return NULL;
1910 result_s = PyByteArray_AS_STRING(result);
1911 Py_MEMCPY(result_s, self_s, self_len);
1912
1913 /* change everything in-place, starting with this one */
1914 start = result_s + offset;
1915 Py_MEMCPY(start, to_s, from_len);
1916 start += from_len;
1917 end = result_s + self_len;
1918
1919 while ( --maxcount > 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001920 offset = stringlib_find(start, end-start,
1921 from_s, from_len,
1922 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001923 if (offset==-1)
1924 break;
1925 Py_MEMCPY(start+offset, to_s, from_len);
1926 start += offset+from_len;
1927 }
1928
1929 return result;
1930}
1931
1932/* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */
1933Py_LOCAL(PyByteArrayObject *)
1934replace_single_character(PyByteArrayObject *self,
1935 char from_c,
1936 const char *to_s, Py_ssize_t to_len,
1937 Py_ssize_t maxcount)
1938{
1939 char *self_s, *result_s;
1940 char *start, *next, *end;
1941 Py_ssize_t self_len, result_len;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001942 Py_ssize_t count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001943 PyByteArrayObject *result;
1944
1945 self_s = PyByteArray_AS_STRING(self);
1946 self_len = PyByteArray_GET_SIZE(self);
1947
1948 count = countchar(self_s, self_len, from_c, maxcount);
1949 if (count == 0) {
1950 /* no matches, return unchanged */
1951 return return_self(self);
1952 }
1953
1954 /* use the difference between current and new, hence the "-1" */
1955 /* result_len = self_len + count * (to_len-1) */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001956 assert(count > 0);
1957 if (to_len - 1 > (PY_SSIZE_T_MAX - self_len) / count) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001958 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
1959 return NULL;
1960 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001961 result_len = self_len + count * (to_len - 1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001962
1963 if ( (result = (PyByteArrayObject *)
1964 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
1965 return NULL;
1966 result_s = PyByteArray_AS_STRING(result);
1967
1968 start = self_s;
1969 end = self_s + self_len;
1970 while (count-- > 0) {
1971 next = findchar(start, end-start, from_c);
1972 if (next == NULL)
1973 break;
1974
1975 if (next == start) {
1976 /* replace with the 'to' */
1977 Py_MEMCPY(result_s, to_s, to_len);
1978 result_s += to_len;
1979 start += 1;
1980 } else {
1981 /* copy the unchanged old then the 'to' */
1982 Py_MEMCPY(result_s, start, next-start);
1983 result_s += (next-start);
1984 Py_MEMCPY(result_s, to_s, to_len);
1985 result_s += to_len;
1986 start = next+1;
1987 }
1988 }
1989 /* Copy the remainder of the remaining bytes */
1990 Py_MEMCPY(result_s, start, end-start);
1991
1992 return result;
1993}
1994
1995/* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */
1996Py_LOCAL(PyByteArrayObject *)
1997replace_substring(PyByteArrayObject *self,
1998 const char *from_s, Py_ssize_t from_len,
1999 const char *to_s, Py_ssize_t to_len,
2000 Py_ssize_t maxcount)
2001{
2002 char *self_s, *result_s;
2003 char *start, *next, *end;
2004 Py_ssize_t self_len, result_len;
Mark Dickinsoncf940c72010-08-10 18:35:01 +00002005 Py_ssize_t count, offset;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002006 PyByteArrayObject *result;
2007
2008 self_s = PyByteArray_AS_STRING(self);
2009 self_len = PyByteArray_GET_SIZE(self);
2010
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002011 count = stringlib_count(self_s, self_len,
2012 from_s, from_len,
2013 maxcount);
2014
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002015 if (count == 0) {
2016 /* no matches, return unchanged */
2017 return return_self(self);
2018 }
2019
2020 /* Check for overflow */
2021 /* result_len = self_len + count * (to_len-from_len) */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00002022 assert(count > 0);
2023 if (to_len - from_len > (PY_SSIZE_T_MAX - self_len) / count) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002024 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
2025 return NULL;
2026 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00002027 result_len = self_len + count * (to_len - from_len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002028
2029 if ( (result = (PyByteArrayObject *)
2030 PyByteArray_FromStringAndSize(NULL, result_len)) == NULL)
2031 return NULL;
2032 result_s = PyByteArray_AS_STRING(result);
2033
2034 start = self_s;
2035 end = self_s + self_len;
2036 while (count-- > 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002037 offset = stringlib_find(start, end-start,
2038 from_s, from_len,
2039 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002040 if (offset == -1)
2041 break;
2042 next = start+offset;
2043 if (next == start) {
2044 /* replace with the 'to' */
2045 Py_MEMCPY(result_s, to_s, to_len);
2046 result_s += to_len;
2047 start += from_len;
2048 } else {
2049 /* copy the unchanged old then the 'to' */
2050 Py_MEMCPY(result_s, start, next-start);
2051 result_s += (next-start);
2052 Py_MEMCPY(result_s, to_s, to_len);
2053 result_s += to_len;
2054 start = next+from_len;
2055 }
2056 }
2057 /* Copy the remainder of the remaining bytes */
2058 Py_MEMCPY(result_s, start, end-start);
2059
2060 return result;
2061}
2062
2063
2064Py_LOCAL(PyByteArrayObject *)
2065replace(PyByteArrayObject *self,
2066 const char *from_s, Py_ssize_t from_len,
2067 const char *to_s, Py_ssize_t to_len,
2068 Py_ssize_t maxcount)
2069{
2070 if (maxcount < 0) {
2071 maxcount = PY_SSIZE_T_MAX;
2072 } else if (maxcount == 0 || PyByteArray_GET_SIZE(self) == 0) {
2073 /* nothing to do; return the original bytes */
2074 return return_self(self);
2075 }
2076
2077 if (maxcount == 0 ||
2078 (from_len == 0 && to_len == 0)) {
2079 /* nothing to do; return the original bytes */
2080 return return_self(self);
2081 }
2082
2083 /* Handle zero-length special cases */
2084
2085 if (from_len == 0) {
2086 /* insert the 'to' bytes everywhere. */
2087 /* >>> "Python".replace("", ".") */
2088 /* '.P.y.t.h.o.n.' */
2089 return replace_interleave(self, to_s, to_len, maxcount);
2090 }
2091
2092 /* Except for "".replace("", "A") == "A" there is no way beyond this */
2093 /* point for an empty self bytes to generate a non-empty bytes */
2094 /* Special case so the remaining code always gets a non-empty bytes */
2095 if (PyByteArray_GET_SIZE(self) == 0) {
2096 return return_self(self);
2097 }
2098
2099 if (to_len == 0) {
Georg Brandl17cb8a82008-05-30 08:20:09 +00002100 /* delete all occurrences of 'from' bytes */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002101 if (from_len == 1) {
2102 return replace_delete_single_character(
2103 self, from_s[0], maxcount);
2104 } else {
2105 return replace_delete_substring(self, from_s, from_len, maxcount);
2106 }
2107 }
2108
2109 /* Handle special case where both bytes have the same length */
2110
2111 if (from_len == to_len) {
2112 if (from_len == 1) {
2113 return replace_single_character_in_place(
2114 self,
2115 from_s[0],
2116 to_s[0],
2117 maxcount);
2118 } else {
2119 return replace_substring_in_place(
2120 self, from_s, from_len, to_s, to_len, maxcount);
2121 }
2122 }
2123
2124 /* Otherwise use the more generic algorithms */
2125 if (from_len == 1) {
2126 return replace_single_character(self, from_s[0],
2127 to_s, to_len, maxcount);
2128 } else {
2129 /* len('from')>=2, len('to')>=1 */
2130 return replace_substring(self, from_s, from_len, to_s, to_len, maxcount);
2131 }
2132}
2133
2134
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002135/*[clinic input]
2136bytearray.replace
2137
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002138 old: Py_buffer
2139 new: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002140 count: Py_ssize_t = -1
2141 Maximum number of occurrences to replace.
2142 -1 (the default value) means replace all occurrences.
2143 /
2144
2145Return a copy with all occurrences of substring old replaced by new.
2146
2147If the optional argument count is given, only the first count occurrences are
2148replaced.
2149[clinic start generated code]*/
2150
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002151static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04002152bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
2153 Py_buffer *new, Py_ssize_t count)
2154/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002155{
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002156 return (PyObject *)replace((PyByteArrayObject *) self,
2157 old->buf, old->len,
2158 new->buf, new->len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002159}
2160
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002161/*[clinic input]
2162bytearray.split
2163
2164 sep: object = None
2165 The delimiter according which to split the bytearray.
2166 None (the default value) means split on ASCII whitespace characters
2167 (space, tab, return, newline, formfeed, vertical tab).
2168 maxsplit: Py_ssize_t = -1
2169 Maximum number of splits to do.
2170 -1 (the default value) means no limit.
2171
2172Return a list of the sections in the bytearray, using sep as the delimiter.
2173[clinic start generated code]*/
2174
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002175static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04002176bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
2177 Py_ssize_t maxsplit)
2178/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002179{
2180 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002181 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002182 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002183 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002184
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002185 if (maxsplit < 0)
2186 maxsplit = PY_SSIZE_T_MAX;
2187
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002188 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002189 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002190
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002191 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002192 return NULL;
2193 sub = vsub.buf;
2194 n = vsub.len;
2195
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002196 list = stringlib_split(
2197 (PyObject*) self, s, len, sub, n, maxsplit
2198 );
Martin v. Löwis423be952008-08-13 15:53:07 +00002199 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002200 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002201}
2202
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002203/*[clinic input]
2204bytearray.partition
2205
2206 self: self(type="PyByteArrayObject *")
2207 sep: object
2208 /
2209
2210Partition the bytearray into three parts using the given separator.
2211
2212This will search for the separator sep in the bytearray. If the separator is
2213found, returns a 3-tuple containing the part before the separator, the
2214separator itself, and the part after it.
2215
2216If the separator is not found, returns a 3-tuple containing the original
2217bytearray object and two empty bytearray objects.
2218[clinic start generated code]*/
2219
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002220static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002221bytearray_partition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002222/*[clinic end generated code: output=45d2525ddd35f957 input=7d7fe37b1696d506]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002223{
2224 PyObject *bytesep, *result;
2225
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002226 bytesep = PyByteArray_FromObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002227 if (! bytesep)
2228 return NULL;
2229
2230 result = stringlib_partition(
2231 (PyObject*) self,
2232 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2233 bytesep,
2234 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2235 );
2236
2237 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002238 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002239}
2240
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002241/*[clinic input]
2242bytearray.rpartition
2243
2244 self: self(type="PyByteArrayObject *")
2245 sep: object
2246 /
2247
2248Partition the bytes into three parts using the given separator.
2249
2250This will search for the separator sep in the bytearray, starting and the end.
2251If the separator is found, returns a 3-tuple containing the part before the
2252separator, the separator itself, and the part after it.
2253
2254If the separator is not found, returns a 3-tuple containing two empty bytearray
2255objects and the original bytearray object.
2256[clinic start generated code]*/
2257
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002258static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002259bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002260/*[clinic end generated code: output=440de3c9426115e8 input=9b8cd540c1b75853]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002261{
2262 PyObject *bytesep, *result;
2263
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002264 bytesep = PyByteArray_FromObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002265 if (! bytesep)
2266 return NULL;
2267
2268 result = stringlib_rpartition(
2269 (PyObject*) self,
2270 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2271 bytesep,
2272 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2273 );
2274
2275 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002276 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002277}
2278
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002279/*[clinic input]
2280bytearray.rsplit = bytearray.split
2281
2282Return a list of the sections in the bytearray, using sep as the delimiter.
2283
2284Splitting is done starting at the end of the bytearray and working to the front.
2285[clinic start generated code]*/
2286
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002287static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04002288bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
2289 Py_ssize_t maxsplit)
2290/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002291{
2292 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002293 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002294 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002295 Py_buffer vsub;
2296
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002297 if (maxsplit < 0)
2298 maxsplit = PY_SSIZE_T_MAX;
2299
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002300 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002301 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002302
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002303 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002304 return NULL;
2305 sub = vsub.buf;
2306 n = vsub.len;
2307
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002308 list = stringlib_rsplit(
2309 (PyObject*) self, s, len, sub, n, maxsplit
2310 );
Martin v. Löwis423be952008-08-13 15:53:07 +00002311 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002312 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002313}
2314
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002315/*[clinic input]
2316bytearray.reverse
2317
2318 self: self(type="PyByteArrayObject *")
2319
2320Reverse the order of the values in B in place.
2321[clinic start generated code]*/
2322
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002323static PyObject *
2324bytearray_reverse_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002325/*[clinic end generated code: output=9f7616f29ab309d3 input=7933a499b8597bd1]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002326{
2327 char swap, *head, *tail;
2328 Py_ssize_t i, j, n = Py_SIZE(self);
2329
2330 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002331 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002332 tail = head + n - 1;
2333 for (i = 0; i < j; i++) {
2334 swap = *head;
2335 *head++ = *tail;
2336 *tail-- = swap;
2337 }
2338
2339 Py_RETURN_NONE;
2340}
2341
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002342
2343/*[python input]
2344class bytesvalue_converter(CConverter):
2345 type = 'int'
2346 converter = '_getbytevalue'
2347[python start generated code]*/
2348/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
2349
2350
2351/*[clinic input]
2352bytearray.insert
2353
2354 self: self(type="PyByteArrayObject *")
2355 index: Py_ssize_t
2356 The index where the value is to be inserted.
2357 item: bytesvalue
2358 The item to be inserted.
2359 /
2360
2361Insert a single item into the bytearray before the given index.
2362[clinic start generated code]*/
2363
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002364static PyObject *
2365bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002366/*[clinic end generated code: output=76c775a70e7b07b7 input=833766836ba30e1e]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002367{
2368 Py_ssize_t n = Py_SIZE(self);
2369 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002370
2371 if (n == PY_SSIZE_T_MAX) {
2372 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00002373 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002374 return NULL;
2375 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002376 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2377 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002378 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002379
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002380 if (index < 0) {
2381 index += n;
2382 if (index < 0)
2383 index = 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002384 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002385 if (index > n)
2386 index = n;
2387 memmove(buf + index + 1, buf + index, n - index);
2388 buf[index] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002389
2390 Py_RETURN_NONE;
2391}
2392
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002393/*[clinic input]
2394bytearray.append
2395
2396 self: self(type="PyByteArrayObject *")
2397 item: bytesvalue
2398 The item to be appended.
2399 /
2400
2401Append a single item to the end of the bytearray.
2402[clinic start generated code]*/
2403
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002404static PyObject *
2405bytearray_append_impl(PyByteArrayObject *self, int item)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002406/*[clinic end generated code: output=a154e19ed1886cb6 input=ae56ea87380407cc]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002407{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002408 Py_ssize_t n = Py_SIZE(self);
2409
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002410 if (n == PY_SSIZE_T_MAX) {
2411 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00002412 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002413 return NULL;
2414 }
2415 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2416 return NULL;
2417
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002418 PyByteArray_AS_STRING(self)[n] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002419
2420 Py_RETURN_NONE;
2421}
2422
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002423/*[clinic input]
2424bytearray.extend
2425
2426 self: self(type="PyByteArrayObject *")
2427 iterable_of_ints: object
2428 The iterable of items to append.
2429 /
2430
2431Append all the items from the iterator or sequence to the end of the bytearray.
2432[clinic start generated code]*/
2433
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002434static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002435bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002436/*[clinic end generated code: output=98155dbe249170b1 input=ce83a5d75b70d850]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002437{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002438 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002439 Py_ssize_t buf_size = 0, len = 0;
2440 int value;
2441 char *buf;
2442
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002443 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002444 if (PyObject_CheckBuffer(iterable_of_ints)) {
2445 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002446 return NULL;
2447
2448 Py_RETURN_NONE;
2449 }
2450
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002451 it = PyObject_GetIter(iterable_of_ints);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002452 if (it == NULL)
2453 return NULL;
2454
Ezio Melotti42da6632011-03-15 05:18:48 +02002455 /* Try to determine the length of the argument. 32 is arbitrary. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002456 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002457 if (buf_size == -1) {
2458 Py_DECREF(it);
2459 return NULL;
2460 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002461
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002462 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002463 if (bytearray_obj == NULL) {
2464 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002465 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002466 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002467 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002468
2469 while ((item = PyIter_Next(it)) != NULL) {
2470 if (! _getbytevalue(item, &value)) {
2471 Py_DECREF(item);
2472 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002473 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002474 return NULL;
2475 }
2476 buf[len++] = value;
2477 Py_DECREF(item);
2478
2479 if (len >= buf_size) {
2480 buf_size = len + (len >> 1) + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002481 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002482 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002483 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002484 return NULL;
2485 }
2486 /* Recompute the `buf' pointer, since the resizing operation may
2487 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002488 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002489 }
2490 }
2491 Py_DECREF(it);
2492
2493 /* Resize down to exact size. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002494 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
2495 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002496 return NULL;
2497 }
2498
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002499 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
2500 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002501 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02002502 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002503 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002504
2505 Py_RETURN_NONE;
2506}
2507
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002508/*[clinic input]
2509bytearray.pop
2510
2511 self: self(type="PyByteArrayObject *")
2512 index: Py_ssize_t = -1
2513 The index from where to remove the item.
2514 -1 (the default value) means remove the last item.
2515 /
2516
2517Remove and return a single item from B.
2518
2519If no index argument is given, will pop the last item.
2520[clinic start generated code]*/
2521
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002522static PyObject *
2523bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002524/*[clinic end generated code: output=e0ccd401f8021da8 input=0797e6c0ca9d5a85]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002525{
2526 int value;
2527 Py_ssize_t n = Py_SIZE(self);
2528 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002529
2530 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00002531 PyErr_SetString(PyExc_IndexError,
2532 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002533 return NULL;
2534 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002535 if (index < 0)
2536 index += Py_SIZE(self);
2537 if (index < 0 || index >= Py_SIZE(self)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002538 PyErr_SetString(PyExc_IndexError, "pop index out of range");
2539 return NULL;
2540 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00002541 if (!_canresize(self))
2542 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002543
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002544 buf = PyByteArray_AS_STRING(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002545 value = buf[index];
2546 memmove(buf + index, buf + index + 1, n - index);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002547 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2548 return NULL;
2549
Mark Dickinson54a3db92009-09-06 10:19:23 +00002550 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002551}
2552
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002553/*[clinic input]
2554bytearray.remove
2555
2556 self: self(type="PyByteArrayObject *")
2557 value: bytesvalue
2558 The value to remove.
2559 /
2560
2561Remove the first occurrence of a value in the bytearray.
2562[clinic start generated code]*/
2563
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002564static PyObject *
2565bytearray_remove_impl(PyByteArrayObject *self, int value)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002566/*[clinic end generated code: output=d659e37866709c13 input=47560b11fd856c24]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002567{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002568 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002569 char *buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002570
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002571 for (where = 0; where < n; where++) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002572 if (buf[where] == value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002573 break;
2574 }
2575 if (where == n) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00002576 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002577 return NULL;
2578 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00002579 if (!_canresize(self))
2580 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002581
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002582 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002583 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2584 return NULL;
2585
2586 Py_RETURN_NONE;
2587}
2588
2589/* XXX These two helpers could be optimized if argsize == 1 */
2590
2591static Py_ssize_t
Antoine Pitrou5b720752013-10-05 21:24:10 +02002592lstrip_helper(char *myptr, Py_ssize_t mysize,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002593 void *argptr, Py_ssize_t argsize)
2594{
2595 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02002596 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002597 i++;
2598 return i;
2599}
2600
2601static Py_ssize_t
Antoine Pitrou5b720752013-10-05 21:24:10 +02002602rstrip_helper(char *myptr, Py_ssize_t mysize,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002603 void *argptr, Py_ssize_t argsize)
2604{
2605 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02002606 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002607 i--;
2608 return i + 1;
2609}
2610
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002611/*[clinic input]
2612bytearray.strip
2613
2614 bytes: object = None
2615 /
2616
2617Strip leading and trailing bytes contained in the argument.
2618
2619If the argument is omitted or None, strip leading and trailing ASCII whitespace.
2620[clinic start generated code]*/
2621
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002622static PyObject *
2623bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002624/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002625{
2626 Py_ssize_t left, right, mysize, byteslen;
2627 char *myptr, *bytesptr;
2628 Py_buffer vbytes;
2629
2630 if (bytes == Py_None) {
2631 bytesptr = "\t\n\r\f\v ";
2632 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002633 }
2634 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002635 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002636 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002637 bytesptr = (char *) vbytes.buf;
2638 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002639 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002640 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002641 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002642 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002643 if (left == mysize)
2644 right = left;
2645 else
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002646 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
2647 if (bytes != Py_None)
2648 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002649 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002650}
2651
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002652/*[clinic input]
2653bytearray.lstrip
2654
2655 bytes: object = None
2656 /
2657
2658Strip leading bytes contained in the argument.
2659
2660If the argument is omitted or None, strip leading ASCII whitespace.
2661[clinic start generated code]*/
2662
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002663static PyObject *
2664bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002665/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002666{
2667 Py_ssize_t left, right, mysize, byteslen;
2668 char *myptr, *bytesptr;
2669 Py_buffer vbytes;
2670
2671 if (bytes == Py_None) {
2672 bytesptr = "\t\n\r\f\v ";
2673 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002674 }
2675 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002676 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002677 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002678 bytesptr = (char *) vbytes.buf;
2679 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002680 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002681 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002682 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002683 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002684 right = mysize;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002685 if (bytes != Py_None)
2686 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002687 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002688}
2689
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002690/*[clinic input]
2691bytearray.rstrip
2692
2693 bytes: object = None
2694 /
2695
2696Strip trailing bytes contained in the argument.
2697
2698If the argument is omitted or None, strip trailing ASCII whitespace.
2699[clinic start generated code]*/
2700
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002701static PyObject *
2702bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002703/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002704{
2705 Py_ssize_t right, mysize, byteslen;
2706 char *myptr, *bytesptr;
2707 Py_buffer vbytes;
2708
2709 if (bytes == Py_None) {
2710 bytesptr = "\t\n\r\f\v ";
2711 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002712 }
2713 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002714 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002715 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002716 bytesptr = (char *) vbytes.buf;
2717 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002718 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002719 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002720 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002721 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
2722 if (bytes != Py_None)
2723 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002724 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002725}
2726
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002727/*[clinic input]
2728bytearray.decode
2729
2730 encoding: str(c_default="NULL") = 'utf-8'
2731 The encoding with which to decode the bytearray.
2732 errors: str(c_default="NULL") = 'strict'
2733 The error handling scheme to use for the handling of decoding errors.
2734 The default is 'strict' meaning that decoding errors raise a
2735 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
2736 as well as any other name registered with codecs.register_error that
2737 can handle UnicodeDecodeErrors.
2738
2739Decode the bytearray using the codec registered for encoding.
2740[clinic start generated code]*/
2741
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002742static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04002743bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
2744 const char *errors)
2745/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002746{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002747 if (encoding == NULL)
2748 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis0efea322014-07-27 17:29:17 +02002749 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002750}
2751
2752PyDoc_STRVAR(alloc_doc,
2753"B.__alloc__() -> int\n\
2754\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00002755Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002756
2757static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002758bytearray_alloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002759{
2760 return PyLong_FromSsize_t(self->ob_alloc);
2761}
2762
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002763/*[clinic input]
2764bytearray.join
2765
2766 iterable_of_bytes: object
2767 /
2768
2769Concatenate any number of bytes/bytearray objects.
2770
2771The bytearray whose method is called is inserted in between each pair.
2772
2773The result is returned as a new bytearray object.
2774[clinic start generated code]*/
2775
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002776static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002777bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002778/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002779{
Martin v. Löwis0efea322014-07-27 17:29:17 +02002780 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002781}
2782
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002783/*[clinic input]
2784bytearray.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002785
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +03002786 keepends: int(c_default="0") = False
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002787
2788Return a list of the lines in the bytearray, breaking at line boundaries.
2789
2790Line breaks are not included in the resulting list unless keepends is given and
2791true.
2792[clinic start generated code]*/
2793
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002794static PyObject *
2795bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +03002796/*[clinic end generated code: output=4223c94b895f6ad9 input=8ccade941e5ea0bd]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002797{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002798 return stringlib_splitlines(
2799 (PyObject*) self, PyByteArray_AS_STRING(self),
2800 PyByteArray_GET_SIZE(self), keepends
2801 );
2802}
2803
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002804static int
Victor Stinner6430fd52011-09-29 04:02:13 +02002805hex_digit_to_int(Py_UCS4 c)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002806{
2807 if (c >= 128)
2808 return -1;
Eric Smith6dc46f52009-04-27 20:39:49 +00002809 if (Py_ISDIGIT(c))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002810 return c - '0';
2811 else {
Eric Smith6dc46f52009-04-27 20:39:49 +00002812 if (Py_ISUPPER(c))
2813 c = Py_TOLOWER(c);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002814 if (c >= 'a' && c <= 'f')
2815 return c - 'a' + 10;
2816 }
2817 return -1;
2818}
2819
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002820/*[clinic input]
2821@classmethod
2822bytearray.fromhex
2823
2824 cls: self(type="PyObject*")
2825 string: unicode
2826 /
2827
2828Create a bytearray object from a string of hexadecimal numbers.
2829
2830Spaces between two numbers are accepted.
2831Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
2832[clinic start generated code]*/
2833
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002834static PyObject *
2835bytearray_fromhex_impl(PyObject*cls, PyObject *string)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002836/*[clinic end generated code: output=df3da60129b3700c input=907bbd2d34d9367a]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002837{
2838 PyObject *newbytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002839 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002840 Py_ssize_t hexlen, byteslen, i, j;
2841 int top, bot;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002842 void *data;
2843 unsigned int kind;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002844
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002845 assert(PyUnicode_Check(string));
2846 if (PyUnicode_READY(string))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002847 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002848 kind = PyUnicode_KIND(string);
2849 data = PyUnicode_DATA(string);
2850 hexlen = PyUnicode_GET_LENGTH(string);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002851
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002852 byteslen = hexlen/2; /* This overestimates if there are spaces */
2853 newbytes = PyByteArray_FromStringAndSize(NULL, byteslen);
2854 if (!newbytes)
2855 return NULL;
2856 buf = PyByteArray_AS_STRING(newbytes);
2857 for (i = j = 0; i < hexlen; i += 2) {
2858 /* skip over spaces in the input */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002859 while (PyUnicode_READ(kind, data, i) == ' ')
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002860 i++;
2861 if (i >= hexlen)
2862 break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002863 top = hex_digit_to_int(PyUnicode_READ(kind, data, i));
2864 bot = hex_digit_to_int(PyUnicode_READ(kind, data, i+1));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002865 if (top == -1 || bot == -1) {
2866 PyErr_Format(PyExc_ValueError,
2867 "non-hexadecimal number found in "
2868 "fromhex() arg at position %zd", i);
2869 goto error;
2870 }
2871 buf[j++] = (top << 4) + bot;
2872 }
2873 if (PyByteArray_Resize(newbytes, j) < 0)
2874 goto error;
2875 return newbytes;
2876
2877 error:
2878 Py_DECREF(newbytes);
2879 return NULL;
2880}
2881
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002882PyDoc_STRVAR(hex__doc__,
2883"B.hex() -> string\n\
2884\n\
2885Create a string of hexadecimal numbers from a bytearray object.\n\
2886Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'.");
2887
2888static PyObject *
2889bytearray_hex(PyBytesObject *self)
2890{
2891 char* argbuf = PyByteArray_AS_STRING(self);
2892 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
2893 return _Py_strhex(argbuf, arglen);
2894}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002895
2896static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002897_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002898{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002899 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002900 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002901 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002902
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002903 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002904 if (dict == NULL) {
2905 PyErr_Clear();
2906 dict = Py_None;
2907 Py_INCREF(dict);
2908 }
2909
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002910 buf = PyByteArray_AS_STRING(self);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002911 if (proto < 3) {
2912 /* use str based reduction for backwards compatibility with Python 2.x */
2913 PyObject *latin1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002914 if (Py_SIZE(self))
2915 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002916 else
2917 latin1 = PyUnicode_FromString("");
2918 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2919 }
2920 else {
2921 /* use more efficient byte based reduction */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002922 if (Py_SIZE(self)) {
2923 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002924 }
2925 else {
2926 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2927 }
2928 }
2929}
2930
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002931/*[clinic input]
2932bytearray.__reduce__ as bytearray_reduce
2933
2934 self: self(type="PyByteArrayObject *")
2935
2936Return state information for pickling.
2937[clinic start generated code]*/
2938
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002939static PyObject *
2940bytearray_reduce_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002941/*[clinic end generated code: output=52bf304086464cab input=fbb07de4d102a03a]*/
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002942{
2943 return _common_reduce(self, 2);
2944}
2945
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002946/*[clinic input]
2947bytearray.__reduce_ex__ as bytearray_reduce_ex
2948
2949 self: self(type="PyByteArrayObject *")
2950 proto: int = 0
2951 /
2952
2953Return state information for pickling.
2954[clinic start generated code]*/
2955
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002956static PyObject *
2957bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002958/*[clinic end generated code: output=52eac33377197520 input=0e091a42ca6dbd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002959{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002960 return _common_reduce(self, proto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002961}
2962
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002963/*[clinic input]
2964bytearray.__sizeof__ as bytearray_sizeof
2965
2966 self: self(type="PyByteArrayObject *")
2967
2968Returns the size of the bytearray object in memory, in bytes.
2969[clinic start generated code]*/
2970
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002971static PyObject *
2972bytearray_sizeof_impl(PyByteArrayObject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002973/*[clinic end generated code: output=738abdd17951c427 input=6b23d305362b462b]*/
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002974{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002975 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002976
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002977 res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002978 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002979}
2980
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002981static PySequenceMethods bytearray_as_sequence = {
2982 (lenfunc)bytearray_length, /* sq_length */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002983 (binaryfunc)PyByteArray_Concat, /* sq_concat */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002984 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2985 (ssizeargfunc)bytearray_getitem, /* sq_item */
2986 0, /* sq_slice */
2987 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2988 0, /* sq_ass_slice */
2989 (objobjproc)bytearray_contains, /* sq_contains */
2990 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2991 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002992};
2993
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002994static PyMappingMethods bytearray_as_mapping = {
2995 (lenfunc)bytearray_length,
2996 (binaryfunc)bytearray_subscript,
2997 (objobjargproc)bytearray_ass_subscript,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002998};
2999
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003000static PyBufferProcs bytearray_as_buffer = {
3001 (getbufferproc)bytearray_getbuffer,
3002 (releasebufferproc)bytearray_releasebuffer,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003003};
3004
3005static PyMethodDef
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003006bytearray_methods[] = {
3007 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003008 BYTEARRAY_REDUCE_METHODDEF
3009 BYTEARRAY_REDUCE_EX_METHODDEF
3010 BYTEARRAY_SIZEOF_METHODDEF
3011 BYTEARRAY_APPEND_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003012 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
3013 _Py_capitalize__doc__},
3014 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003015 BYTEARRAY_CLEAR_METHODDEF
3016 BYTEARRAY_COPY_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003017 {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003018 BYTEARRAY_DECODE_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003019 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__},
Ezio Melotti745d54d2013-11-16 19:10:57 +02003020 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003021 expandtabs__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003022 BYTEARRAY_EXTEND_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003023 {"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003024 BYTEARRAY_FROMHEX_METHODDEF
Gregory P. Smith8cb65692015-04-25 23:22:26 +00003025 {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__},
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003026 {"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003027 BYTEARRAY_INSERT_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003028 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
3029 _Py_isalnum__doc__},
3030 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
3031 _Py_isalpha__doc__},
3032 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
3033 _Py_isdigit__doc__},
3034 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
3035 _Py_islower__doc__},
3036 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
3037 _Py_isspace__doc__},
3038 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
3039 _Py_istitle__doc__},
3040 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
3041 _Py_isupper__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003042 BYTEARRAY_JOIN_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003043 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__},
3044 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003045 BYTEARRAY_LSTRIP_METHODDEF
3046 BYTEARRAY_MAKETRANS_METHODDEF
3047 BYTEARRAY_PARTITION_METHODDEF
3048 BYTEARRAY_POP_METHODDEF
3049 BYTEARRAY_REMOVE_METHODDEF
3050 BYTEARRAY_REPLACE_METHODDEF
3051 BYTEARRAY_REVERSE_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003052 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__},
3053 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003054 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003055 BYTEARRAY_RPARTITION_METHODDEF
3056 BYTEARRAY_RSPLIT_METHODDEF
3057 BYTEARRAY_RSTRIP_METHODDEF
3058 BYTEARRAY_SPLIT_METHODDEF
3059 BYTEARRAY_SPLITLINES_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003060 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003061 startswith__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003062 BYTEARRAY_STRIP_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003063 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
3064 _Py_swapcase__doc__},
3065 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003066 BYTEARRAY_TRANSLATE_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003067 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
3068 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
3069 {NULL}
3070};
3071
Ethan Furmanb95b5612015-01-23 20:05:18 -08003072static PyObject *
3073bytearray_mod(PyObject *v, PyObject *w)
3074{
3075 if (!PyByteArray_Check(v))
3076 Py_RETURN_NOTIMPLEMENTED;
3077 return bytearray_format((PyByteArrayObject *)v, w);
3078}
3079
3080static PyNumberMethods bytearray_as_number = {
3081 0, /*nb_add*/
3082 0, /*nb_subtract*/
3083 0, /*nb_multiply*/
3084 bytearray_mod, /*nb_remainder*/
3085};
3086
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003087PyDoc_STRVAR(bytearray_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00003088"bytearray(iterable_of_ints) -> bytearray\n\
3089bytearray(string, encoding[, errors]) -> bytearray\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01003090bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
3091bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
3092bytearray() -> empty bytes array\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003093\n\
3094Construct an mutable bytearray object from:\n\
3095 - an iterable yielding integers in range(256)\n\
3096 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01003097 - a bytes or a buffer object\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003098 - any object implementing the buffer API.\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01003099 - an integer");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003100
3101
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003102static PyObject *bytearray_iter(PyObject *seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003103
3104PyTypeObject PyByteArray_Type = {
3105 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3106 "bytearray",
3107 sizeof(PyByteArrayObject),
3108 0,
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003109 (destructor)bytearray_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003110 0, /* tp_print */
3111 0, /* tp_getattr */
3112 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00003113 0, /* tp_reserved */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003114 (reprfunc)bytearray_repr, /* tp_repr */
Ethan Furmanb95b5612015-01-23 20:05:18 -08003115 &bytearray_as_number, /* tp_as_number */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003116 &bytearray_as_sequence, /* tp_as_sequence */
3117 &bytearray_as_mapping, /* tp_as_mapping */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003118 0, /* tp_hash */
3119 0, /* tp_call */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003120 bytearray_str, /* tp_str */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003121 PyObject_GenericGetAttr, /* tp_getattro */
3122 0, /* tp_setattro */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003123 &bytearray_as_buffer, /* tp_as_buffer */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003124 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003125 bytearray_doc, /* tp_doc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003126 0, /* tp_traverse */
3127 0, /* tp_clear */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003128 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003129 0, /* tp_weaklistoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003130 bytearray_iter, /* tp_iter */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003131 0, /* tp_iternext */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003132 bytearray_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003133 0, /* tp_members */
3134 0, /* tp_getset */
3135 0, /* tp_base */
3136 0, /* tp_dict */
3137 0, /* tp_descr_get */
3138 0, /* tp_descr_set */
3139 0, /* tp_dictoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003140 (initproc)bytearray_init, /* tp_init */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003141 PyType_GenericAlloc, /* tp_alloc */
3142 PyType_GenericNew, /* tp_new */
3143 PyObject_Del, /* tp_free */
3144};
3145
3146/*********************** Bytes Iterator ****************************/
3147
3148typedef struct {
3149 PyObject_HEAD
3150 Py_ssize_t it_index;
3151 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
3152} bytesiterobject;
3153
3154static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003155bytearrayiter_dealloc(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003156{
3157 _PyObject_GC_UNTRACK(it);
3158 Py_XDECREF(it->it_seq);
3159 PyObject_GC_Del(it);
3160}
3161
3162static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003163bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003164{
3165 Py_VISIT(it->it_seq);
3166 return 0;
3167}
3168
3169static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003170bytearrayiter_next(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003171{
3172 PyByteArrayObject *seq;
3173 PyObject *item;
3174
3175 assert(it != NULL);
3176 seq = it->it_seq;
3177 if (seq == NULL)
3178 return NULL;
3179 assert(PyByteArray_Check(seq));
3180
3181 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
3182 item = PyLong_FromLong(
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02003183 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003184 if (item != NULL)
3185 ++it->it_index;
3186 return item;
3187 }
3188
3189 Py_DECREF(seq);
3190 it->it_seq = NULL;
3191 return NULL;
3192}
3193
3194static PyObject *
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003195bytearrayiter_length_hint(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003196{
3197 Py_ssize_t len = 0;
3198 if (it->it_seq)
3199 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
3200 return PyLong_FromSsize_t(len);
3201}
3202
3203PyDoc_STRVAR(length_hint_doc,
3204 "Private method returning an estimate of len(list(it)).");
3205
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003206static PyObject *
3207bytearrayiter_reduce(bytesiterobject *it)
3208{
3209 if (it->it_seq != NULL) {
Antoine Pitroua7013882012-04-05 00:04:20 +02003210 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003211 it->it_seq, it->it_index);
3212 } else {
3213 PyObject *u = PyUnicode_FromUnicode(NULL, 0);
3214 if (u == NULL)
3215 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +02003216 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003217 }
3218}
3219
3220static PyObject *
3221bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
3222{
3223 Py_ssize_t index = PyLong_AsSsize_t(state);
3224 if (index == -1 && PyErr_Occurred())
3225 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003226 if (it->it_seq != NULL) {
3227 if (index < 0)
3228 index = 0;
3229 else if (index > PyByteArray_GET_SIZE(it->it_seq))
3230 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
3231 it->it_index = index;
3232 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003233 Py_RETURN_NONE;
3234}
3235
3236PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
3237
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003238static PyMethodDef bytearrayiter_methods[] = {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003239 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003240 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003241 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02003242 bytearray_reduce__doc__},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003243 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
3244 setstate_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003245 {NULL, NULL} /* sentinel */
3246};
3247
3248PyTypeObject PyByteArrayIter_Type = {
3249 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3250 "bytearray_iterator", /* tp_name */
3251 sizeof(bytesiterobject), /* tp_basicsize */
3252 0, /* tp_itemsize */
3253 /* methods */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003254 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003255 0, /* tp_print */
3256 0, /* tp_getattr */
3257 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00003258 0, /* tp_reserved */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003259 0, /* tp_repr */
3260 0, /* tp_as_number */
3261 0, /* tp_as_sequence */
3262 0, /* tp_as_mapping */
3263 0, /* tp_hash */
3264 0, /* tp_call */
3265 0, /* tp_str */
3266 PyObject_GenericGetAttr, /* tp_getattro */
3267 0, /* tp_setattro */
3268 0, /* tp_as_buffer */
3269 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
3270 0, /* tp_doc */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003271 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003272 0, /* tp_clear */
3273 0, /* tp_richcompare */
3274 0, /* tp_weaklistoffset */
3275 PyObject_SelfIter, /* tp_iter */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003276 (iternextfunc)bytearrayiter_next, /* tp_iternext */
3277 bytearrayiter_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003278 0,
3279};
3280
3281static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00003282bytearray_iter(PyObject *seq)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003283{
3284 bytesiterobject *it;
3285
3286 if (!PyByteArray_Check(seq)) {
3287 PyErr_BadInternalCall();
3288 return NULL;
3289 }
3290 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
3291 if (it == NULL)
3292 return NULL;
3293 it->it_index = 0;
3294 Py_INCREF(seq);
3295 it->it_seq = (PyByteArrayObject *)seq;
3296 _PyObject_GC_TRACK(it);
3297 return (PyObject *)it;
3298}